🚀. Socket Launch Week Day 2:Introducing Manifest Alerts.Learn more
Sign In

@kubb/ast

Package Overview
Dependencies
Maintainers
1
Versions
168
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kubb/ast - npm Package Compare versions

Comparing version
5.0.0-beta.61
to
5.0.0-beta.62
+115
dist/defineMacro-BryDiuQl.cjs
const require_response = require("./response-BeG6hq2D.cjs");
const require_visitor = require("./visitor-B7Zlnp4c.cjs");
//#region src/defineMacro.ts
/**
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
* list keeps its authored order.
*/
function enforceWeight(enforce) {
if (enforce === "pre") return 0;
if (enforce === "post") return 2;
return 1;
}
/**
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
* Adds no runtime behavior.
*
* @example
* ```ts
* const macroUntagged = defineMacro({
* name: 'untagged',
* operation(node) {
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
* },
* })
* ```
*/
function defineMacro(macro) {
return macro;
}
/**
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
* original reference (structural sharing).
*/
function chain({ macros, key, node, context }) {
let current = node;
for (const macro of macros) {
const callback = macro[key];
if (!callback) continue;
if (macro.when && !macro.when(current)) continue;
const next = callback(current, context);
if (next != null) current = next;
}
return current === node ? void 0 : current;
}
/**
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
* has no names, ordering, or composition.
*
* @example
* ```ts
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
* const next = transform(root, visitor)
* ```
*/
function composeMacros(macros) {
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
const visitor = {};
for (const key of require_response.visitorKeys) {
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
const callback = (node, context) => chain({
macros: ordered,
key,
node,
context
});
visitor[key] = callback;
}
return visitor;
}
/**
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
* `depth: 'shallow'` to rewrite the root node only.
*
* @example
* ```ts
* const next = applyMacros(root, [macroIntegerToString])
* ```
*
* @example Apply to the root node only
* ```ts
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
* ```
*/
function applyMacros(root, macros, options) {
if (macros.length === 0) return root;
return require_visitor.transform(root, {
...composeMacros(macros),
...options
});
}
//#endregion
Object.defineProperty(exports, "applyMacros", {
enumerable: true,
get: function() {
return applyMacros;
}
});
Object.defineProperty(exports, "composeMacros", {
enumerable: true,
get: function() {
return composeMacros;
}
});
Object.defineProperty(exports, "defineMacro", {
enumerable: true,
get: function() {
return defineMacro;
}
});
//# sourceMappingURL=defineMacro-BryDiuQl.cjs.map
{"version":3,"file":"defineMacro-BryDiuQl.cjs","names":["visitorKeys","transform"],"sources":["../src/defineMacro.ts"],"sourcesContent":["import type { VisitorDepth } from './constants.ts'\nimport type { VisitorKey } from './defineNode.ts'\nimport { visitorKeys } from './defineNode.ts'\nimport type { Node } from './nodes/index.ts'\nimport type { Visitor, VisitorContext } from './visitor.ts'\nimport { transform } from './visitor.ts'\n\n/**\n * Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,\n * and `undefined` keeps declaration order.\n */\nexport type Enforce = 'pre' | 'post'\n\n/**\n * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain\n * list keeps its authored order.\n */\nfunction enforceWeight(enforce?: Enforce): number {\n if (enforce === 'pre') return 0\n if (enforce === 'post') return 2\n return 1\n}\n\n/**\n * A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a\n * {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an\n * optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter\n * and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).\n */\nexport type Macro = Visitor & {\n /**\n * Macro identifier, surfaced in diagnostics. Follows the `macro<Name>` convention.\n */\n name: string\n /**\n * Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.\n * Ordering within a bucket follows list order.\n */\n enforce?: Enforce\n /**\n * Gate checked against the current node before any callback runs. When it returns `false`\n * the macro is skipped for that node.\n */\n when?: (node: Node) => boolean\n}\n\n/**\n * Types a macro for inference and a single construction site, mirroring `definePlugin`.\n * Adds no runtime behavior.\n *\n * @example\n * ```ts\n * const macroUntagged = defineMacro({\n * name: 'untagged',\n * operation(node) {\n * return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }\n * },\n * })\n * ```\n */\nexport function defineMacro(macro: Macro): Macro {\n return macro\n}\n\ntype MacroCallback = (node: Node, context: VisitorContext) => Node | null | undefined\n\ntype ChainProps = {\n macros: ReadonlyArray<Macro>\n key: VisitorKey\n node: Node\n context: VisitorContext\n}\n\n/**\n * Runs every macro's callback for one node kind in order, chaining the result so each macro sees\n * the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the\n * original reference (structural sharing).\n */\nfunction chain({ macros, key, node, context }: ChainProps): Node | undefined {\n let current = node\n\n for (const macro of macros) {\n const callback = macro[key] as MacroCallback | undefined\n if (!callback) continue\n if (macro.when && !macro.when(current)) continue\n\n const next = callback(current, context)\n if (next != null) current = next\n }\n\n return current === node ? undefined : current\n}\n\n/**\n * Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin\n * transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied\n * sequentially per node so later macros see earlier output. This differs from a plain visitor, which\n * has no names, ordering, or composition.\n *\n * @example\n * ```ts\n * const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])\n * const next = transform(root, visitor)\n * ```\n */\nexport function composeMacros(macros: ReadonlyArray<Macro>): Visitor {\n const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce))\n\n const visitor: Visitor = {}\n for (const key of visitorKeys) {\n if (!ordered.some((macro) => typeof macro[key] === 'function')) continue\n\n const callback = (node: Node, context: VisitorContext) => chain({ macros: ordered, key, node, context })\n ;(visitor as Record<VisitorKey, MacroCallback>)[key] = callback\n }\n\n return visitor\n}\n\n/**\n * Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s\n * structural sharing, so an empty or no-op macro list returns the same reference. Pass\n * `depth: 'shallow'` to rewrite the root node only.\n *\n * @example\n * ```ts\n * const next = applyMacros(root, [macroIntegerToString])\n * ```\n *\n * @example Apply to the root node only\n * ```ts\n * const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })\n * ```\n */\nexport function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: { depth?: VisitorDepth }): TNode {\n if (macros.length === 0) return root\n\n return transform(root, { ...composeMacros(macros), ...options }) as TNode\n}\n"],"mappings":";;;;;;;AAiBA,SAAS,cAAc,SAA2B;CAChD,IAAI,YAAY,OAAO,OAAO;CAC9B,IAAI,YAAY,QAAQ,OAAO;CAC/B,OAAO;AACT;;;;;;;;;;;;;;;AAuCA,SAAgB,YAAY,OAAqB;CAC/C,OAAO;AACT;;;;;;AAgBA,SAAS,MAAM,EAAE,QAAQ,KAAK,MAAM,WAAyC;CAC3E,IAAI,UAAU;CAEd,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,WAAW,MAAM;EACvB,IAAI,CAAC,UAAU;EACf,IAAI,MAAM,QAAQ,CAAC,MAAM,KAAK,OAAO,GAAG;EAExC,MAAM,OAAO,SAAS,SAAS,OAAO;EACtC,IAAI,QAAQ,MAAM,UAAU;CAC9B;CAEA,OAAO,YAAY,OAAO,KAAA,IAAY;AACxC;;;;;;;;;;;;;AAcA,SAAgB,cAAc,QAAuC;CACnE,MAAM,UAAU,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM,cAAc,EAAE,OAAO,IAAI,cAAc,EAAE,OAAO,CAAC;CAE9F,MAAM,UAAmB,CAAC;CAC1B,KAAK,MAAM,OAAOA,iBAAAA,aAAa;EAC7B,IAAI,CAAC,QAAQ,MAAM,UAAU,OAAO,MAAM,SAAS,UAAU,GAAG;EAEhE,MAAM,YAAY,MAAY,YAA4B,MAAM;GAAE,QAAQ;GAAS;GAAK;GAAM;EAAQ,CAAC;EACtG,QAA+C,OAAO;CACzD;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,YAAgC,MAAa,QAA8B,SAA2C;CACpI,IAAI,OAAO,WAAW,GAAG,OAAO;CAEhC,OAAOC,gBAAAA,UAAU,MAAM;EAAE,GAAG,cAAc,MAAM;EAAG,GAAG;CAAQ,CAAC;AACjE"}
import "./chunk-CNktS9qV.js";
import { r as transform } from "./visitor-DepQEKyp.js";
import { tt as visitorKeys } from "./response-Rd1uisM1.js";
//#region src/defineMacro.ts
/**
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
* list keeps its authored order.
*/
function enforceWeight(enforce) {
if (enforce === "pre") return 0;
if (enforce === "post") return 2;
return 1;
}
/**
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
* Adds no runtime behavior.
*
* @example
* ```ts
* const macroUntagged = defineMacro({
* name: 'untagged',
* operation(node) {
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
* },
* })
* ```
*/
function defineMacro(macro) {
return macro;
}
/**
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
* original reference (structural sharing).
*/
function chain({ macros, key, node, context }) {
let current = node;
for (const macro of macros) {
const callback = macro[key];
if (!callback) continue;
if (macro.when && !macro.when(current)) continue;
const next = callback(current, context);
if (next != null) current = next;
}
return current === node ? void 0 : current;
}
/**
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
* has no names, ordering, or composition.
*
* @example
* ```ts
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
* const next = transform(root, visitor)
* ```
*/
function composeMacros(macros) {
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
const visitor = {};
for (const key of visitorKeys) {
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
const callback = (node, context) => chain({
macros: ordered,
key,
node,
context
});
visitor[key] = callback;
}
return visitor;
}
/**
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
* `depth: 'shallow'` to rewrite the root node only.
*
* @example
* ```ts
* const next = applyMacros(root, [macroIntegerToString])
* ```
*
* @example Apply to the root node only
* ```ts
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
* ```
*/
function applyMacros(root, macros, options) {
if (macros.length === 0) return root;
return transform(root, {
...composeMacros(macros),
...options
});
}
//#endregion
export { composeMacros as n, defineMacro as r, applyMacros as t };
//# sourceMappingURL=defineMacro-CNkUpxia.js.map
{"version":3,"file":"defineMacro-CNkUpxia.js","names":[],"sources":["../src/defineMacro.ts"],"sourcesContent":["import type { VisitorDepth } from './constants.ts'\nimport type { VisitorKey } from './defineNode.ts'\nimport { visitorKeys } from './defineNode.ts'\nimport type { Node } from './nodes/index.ts'\nimport type { Visitor, VisitorContext } from './visitor.ts'\nimport { transform } from './visitor.ts'\n\n/**\n * Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,\n * and `undefined` keeps declaration order.\n */\nexport type Enforce = 'pre' | 'post'\n\n/**\n * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain\n * list keeps its authored order.\n */\nfunction enforceWeight(enforce?: Enforce): number {\n if (enforce === 'pre') return 0\n if (enforce === 'post') return 2\n return 1\n}\n\n/**\n * A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a\n * {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an\n * optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter\n * and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).\n */\nexport type Macro = Visitor & {\n /**\n * Macro identifier, surfaced in diagnostics. Follows the `macro<Name>` convention.\n */\n name: string\n /**\n * Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.\n * Ordering within a bucket follows list order.\n */\n enforce?: Enforce\n /**\n * Gate checked against the current node before any callback runs. When it returns `false`\n * the macro is skipped for that node.\n */\n when?: (node: Node) => boolean\n}\n\n/**\n * Types a macro for inference and a single construction site, mirroring `definePlugin`.\n * Adds no runtime behavior.\n *\n * @example\n * ```ts\n * const macroUntagged = defineMacro({\n * name: 'untagged',\n * operation(node) {\n * return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }\n * },\n * })\n * ```\n */\nexport function defineMacro(macro: Macro): Macro {\n return macro\n}\n\ntype MacroCallback = (node: Node, context: VisitorContext) => Node | null | undefined\n\ntype ChainProps = {\n macros: ReadonlyArray<Macro>\n key: VisitorKey\n node: Node\n context: VisitorContext\n}\n\n/**\n * Runs every macro's callback for one node kind in order, chaining the result so each macro sees\n * the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the\n * original reference (structural sharing).\n */\nfunction chain({ macros, key, node, context }: ChainProps): Node | undefined {\n let current = node\n\n for (const macro of macros) {\n const callback = macro[key] as MacroCallback | undefined\n if (!callback) continue\n if (macro.when && !macro.when(current)) continue\n\n const next = callback(current, context)\n if (next != null) current = next\n }\n\n return current === node ? undefined : current\n}\n\n/**\n * Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin\n * transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied\n * sequentially per node so later macros see earlier output. This differs from a plain visitor, which\n * has no names, ordering, or composition.\n *\n * @example\n * ```ts\n * const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])\n * const next = transform(root, visitor)\n * ```\n */\nexport function composeMacros(macros: ReadonlyArray<Macro>): Visitor {\n const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce))\n\n const visitor: Visitor = {}\n for (const key of visitorKeys) {\n if (!ordered.some((macro) => typeof macro[key] === 'function')) continue\n\n const callback = (node: Node, context: VisitorContext) => chain({ macros: ordered, key, node, context })\n ;(visitor as Record<VisitorKey, MacroCallback>)[key] = callback\n }\n\n return visitor\n}\n\n/**\n * Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s\n * structural sharing, so an empty or no-op macro list returns the same reference. Pass\n * `depth: 'shallow'` to rewrite the root node only.\n *\n * @example\n * ```ts\n * const next = applyMacros(root, [macroIntegerToString])\n * ```\n *\n * @example Apply to the root node only\n * ```ts\n * const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })\n * ```\n */\nexport function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: { depth?: VisitorDepth }): TNode {\n if (macros.length === 0) return root\n\n return transform(root, { ...composeMacros(macros), ...options }) as TNode\n}\n"],"mappings":";;;;;;;;AAiBA,SAAS,cAAc,SAA2B;CAChD,IAAI,YAAY,OAAO,OAAO;CAC9B,IAAI,YAAY,QAAQ,OAAO;CAC/B,OAAO;AACT;;;;;;;;;;;;;;;AAuCA,SAAgB,YAAY,OAAqB;CAC/C,OAAO;AACT;;;;;;AAgBA,SAAS,MAAM,EAAE,QAAQ,KAAK,MAAM,WAAyC;CAC3E,IAAI,UAAU;CAEd,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,WAAW,MAAM;EACvB,IAAI,CAAC,UAAU;EACf,IAAI,MAAM,QAAQ,CAAC,MAAM,KAAK,OAAO,GAAG;EAExC,MAAM,OAAO,SAAS,SAAS,OAAO;EACtC,IAAI,QAAQ,MAAM,UAAU;CAC9B;CAEA,OAAO,YAAY,OAAO,KAAA,IAAY;AACxC;;;;;;;;;;;;;AAcA,SAAgB,cAAc,QAAuC;CACnE,MAAM,UAAU,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM,cAAc,EAAE,OAAO,IAAI,cAAc,EAAE,OAAO,CAAC;CAE9F,MAAM,UAAmB,CAAC;CAC1B,KAAK,MAAM,OAAO,aAAa;EAC7B,IAAI,CAAC,QAAQ,MAAM,UAAU,OAAO,MAAM,SAAS,UAAU,GAAG;EAEhE,MAAM,YAAY,MAAY,YAA4B,MAAM;GAAE,QAAQ;GAAS;GAAK;GAAM;EAAQ,CAAC;EACtG,QAA+C,OAAO;CACzD;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,YAAgC,MAAa,QAA8B,SAA2C;CACpI,IAAI,OAAO,WAAW,GAAG,OAAO;CAEhC,OAAO,UAAU,MAAM;EAAE,GAAG,cAAc,MAAM;EAAG,GAAG;CAAQ,CAAC;AACjE"}
import { n as __name } from "./chunk-CNktS9qV.js";
import { C as ParameterNode, Et as PropertyNode, _t as SchemaNode, f as OperationNode, h as ResponseNode, n as OutputNode, o as InputNode, rt as ContentNode, t as Node, y as RequestBodyNode } from "./index-CTaN_ee_.js";
//#region src/constants.d.ts
/**
* Traversal depth for AST visitor utilities.
*
* - `'shallow'` visits only the immediate node, skipping children.
* - `'deep'` recursively visits all descendant nodes.
*/
type VisitorDepth = 'shallow' | 'deep';
/**
* Schema type discriminators used by all AST schema nodes.
*
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/
declare const schemaTypes: {
/**
* Text value.
*/
readonly string: "string";
/**
* Floating-point number (`float`, `double`).
*/
readonly number: "number";
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
readonly integer: "integer";
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
readonly bigint: "bigint";
/**
* Boolean value.
*/
readonly boolean: "boolean";
/**
* Explicit null value.
*/
readonly null: "null";
/**
* Any value (no type restriction).
*/
readonly any: "any";
/**
* Unknown value (must be narrowed before usage).
*/
readonly unknown: "unknown";
/**
* No return value (`void`).
*/
readonly void: "void";
/**
* Object with named properties.
*/
readonly object: "object";
/**
* Sequential list of items.
*/
readonly array: "array";
/**
* Fixed-length list with position-specific items.
*/
readonly tuple: "tuple";
/**
* "One of" multiple schema members.
*/
readonly union: "union";
/**
* "All of" multiple schema members.
*/
readonly intersection: "intersection";
/**
* Enum schema.
*/
readonly enum: "enum";
/**
* Reference to another schema.
*/
readonly ref: "ref";
/**
* Calendar date (for example `2026-03-24`).
*/
readonly date: "date";
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
readonly datetime: "datetime";
/**
* Time-only value (for example `09:00:00`).
*/
readonly time: "time";
/**
* UUID value.
*/
readonly uuid: "uuid";
/**
* Email address value.
*/
readonly email: "email";
/**
* URL value.
*/
readonly url: "url";
/**
* IPv4 address value.
*/
readonly ipv4: "ipv4";
/**
* IPv6 address value.
*/
readonly ipv6: "ipv6";
/**
* Binary/blob value.
*/
readonly blob: "blob";
/**
* Impossible value (`never`).
*/
readonly never: "never";
};
//#endregion
//#region src/visitor.d.ts
/**
* Ordered mapping of `[NodeType, ParentType]` pairs.
*
* `ParentOf` uses this map to find parent types.
*/
type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [OperationNode, InputNode], [RequestBodyNode, OperationNode], [ContentNode, RequestBodyNode | ResponseNode], [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
/**
* Resolves the parent node type for a given AST node type.
*
* Visitor context relies on this so `ctx.parent` is typed for each callback.
*
* @example
* ```ts
* type InputParent = ParentOf<InputNode>
* // undefined
* ```
*
* @example
* ```ts
* type PropertyParent = ParentOf<PropertyNode>
* // SchemaNode
* ```
*
* @example
* ```ts
* type SchemaParent = ParentOf<SchemaNode>
* // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
* ```
*/
type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [infer TEntry extends [Node, unknown], ...infer TRest extends ReadonlyArray<[Node, unknown]>] ? T extends TEntry[0] ? TEntry[1] : ParentOf<T, TRest> : Node;
/**
* Traversal context passed as the second argument to every visitor callback.
* `parent` is typed from the current node type.
*
* @example
* ```ts
* const visitor: Visitor = {
* schema(node, { parent }) {
* // parent type is narrowed by node kind
* },
* }
* ```
*/
type VisitorContext<T extends Node = Node> = {
/**
* Parent node of the currently visited node.
* For `InputNode`, this is `undefined`.
*/
parent?: ParentOf<T>;
};
/**
* Synchronous visitor consumed by `transform`. Each optional callback runs
* for the matching node type. Return a new node to replace it, or `undefined`
* to leave it untouched.
*
* Plugins typically expose `transformer` so users can supply a `Visitor` that
* rewrites the AST before printing.
*
* @example Prefix every operationId
* ```ts
* const visitor: Visitor = {
* operation(node) {
* return { ...node, operationId: `api_${node.operationId}` }
* },
* }
* ```
*
* @example Strip schema descriptions
* ```ts
* const visitor: Visitor = {
* schema(node) {
* return { ...node, description: undefined }
* },
* }
* ```
*/
type Visitor = {
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode;
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
};
/**
* A visitor callback result that may be sync or async.
*/
type MaybePromise<T> = T | Promise<T>;
/**
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
*
* @example
* ```ts
* const visitor: AsyncVisitor = {
* async operation(node) {
* await Promise.resolve(node.operationId)
* },
* }
* ```
*/
type AsyncVisitor = {
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>;
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>;
};
/**
* Visitor used by `collect`.
*
* @example
* ```ts
* const visitor: CollectVisitor<string> = {
* operation(node) {
* return node.operationId
* },
* }
* ```
*/
type CollectVisitor<T> = {
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
};
/**
* Options for `transform`.
*
* @example
* ```ts
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
* ```
*
* @example
* ```ts
* // Only transform the current node, not nested children
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
* ```
*/
type TransformOptions = Visitor & {
/**
* Traversal depth.
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Internal parent override used during recursion.
*/
parent?: Node;
};
/**
* Options for `walk`.
*
* @example
* ```ts
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
* ```
*/
type WalkOptions = AsyncVisitor & {
/**
* Traversal depth.
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Maximum number of sibling nodes visited concurrently.
* @default 30
*/
concurrency?: number;
};
/**
* Options for `collect`.
*
* @example
* ```ts
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
* ```
*/
type CollectOptions<T> = CollectVisitor<T> & {
/**
* Traversal depth.
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Internal parent override used during recursion.
*/
parent?: Node;
};
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
declare function walk(node: Node, options: WalkOptions): Promise<void>;
/**
* Synchronous depth-first transform. Each visitor callback can return a
* replacement node. Returning `undefined` keeps the original.
*
* The original tree is never mutated, a new tree is returned. Pass
* `depth: 'shallow'` to skip recursion into children.
*
* @example Prefix every operationId
* ```ts
* const next = transform(root, {
* operation(node) {
* return { ...node, operationId: `prefixed_${node.operationId}` }
* },
* })
* ```
*
* @example Replace only the root node
* ```ts
* const next = transform(root, {
* depth: 'shallow',
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
* })
* ```
*/
declare function transform(node: InputNode, options: TransformOptions): InputNode;
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
declare function transform(node: Node, options: TransformOptions): Node;
/**
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
//#endregion
//#region src/defineMacro.d.ts
/**
* Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,
* and `undefined` keeps declaration order.
*/
type Enforce = 'pre' | 'post';
/**
* A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a
* {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an
* optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter
* and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).
*/
type Macro = Visitor & {
/**
* Macro identifier, surfaced in diagnostics. Follows the `macro<Name>` convention.
*/
name: string;
/**
* Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.
* Ordering within a bucket follows list order.
*/
enforce?: Enforce;
/**
* Gate checked against the current node before any callback runs. When it returns `false`
* the macro is skipped for that node.
*/
when?: (node: Node) => boolean;
};
/**
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
* Adds no runtime behavior.
*
* @example
* ```ts
* const macroUntagged = defineMacro({
* name: 'untagged',
* operation(node) {
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
* },
* })
* ```
*/
declare function defineMacro(macro: Macro): Macro;
/**
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
* has no names, ordering, or composition.
*
* @example
* ```ts
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
* const next = transform(root, visitor)
* ```
*/
declare function composeMacros(macros: ReadonlyArray<Macro>): Visitor;
/**
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
* `depth: 'shallow'` to rewrite the root node only.
*
* @example
* ```ts
* const next = applyMacros(root, [macroIntegerToString])
* ```
*
* @example Apply to the root node only
* ```ts
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
* ```
*/
declare function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: {
depth?: VisitorDepth;
}): TNode;
//#endregion
export { defineMacro as a, VisitorContext as c, walk as d, schemaTypes as f, composeMacros as i, collect as l, Macro as n, ParentOf as o, applyMacros as r, Visitor as s, Enforce as t, transform as u };
//# sourceMappingURL=defineMacro-Vw-xQvq7.d.ts.map
{"version":3,"file":"factory.cjs","names":[],"sources":["../src/factory.ts"],"sourcesContent":["import type { Node } from './nodes/index.ts'\n\n// Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes\n// `ts.factory.createX`. Aggregating them here lets `export * as factory from './factory.ts'` in the\n// barrel surface every `createX` alongside the `createFile`/`update` helpers from a single module.\nexport { createArrowFunction, createBreak, createConst, createFunction, createJsx, createText, createType } from './nodes/code.ts'\nexport { createContent } from './nodes/content.ts'\nexport { createExport, createFile, createImport, createSource } from './nodes/file.ts'\nexport type { UserFileNode } from './nodes/file.ts'\nexport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts'\nexport { createInput } from './nodes/input.ts'\nexport { createOperation } from './nodes/operation.ts'\nexport { createOutput } from './nodes/output.ts'\nexport { createParameter } from './nodes/parameter.ts'\nexport { createProperty } from './nodes/property.ts'\nexport { createRequestBody } from './nodes/requestBody.ts'\nexport { createResponse } from './nodes/response.ts'\nexport { createSchema } from './nodes/schema.ts'\n\n/**\n * Identity-preserving node update: returns `node` unchanged when every field in\n * `changes` already equals (by reference) the current value, otherwise a new node\n * with the changes applied.\n *\n * Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the\n * structural sharing in {@link transform} so a no-op rewrite doesn't allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is\n * shallow: a structurally-equal but newly-allocated array/object counts as a change.\n *\n * @example\n * ```ts\n * update(node, { name: node.name }) // -> same `node` reference\n * update(node, { name: 'renamed' }) // -> new node, `name` replaced\n * ```\n */\nexport function update<T extends Node>(node: T, changes: Partial<T>): T {\n for (const key in changes) {\n if (changes[key] !== node[key as keyof T]) {\n return { ...node, ...changes }\n }\n }\n\n return node\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAAgB,OAAuB,MAAS,SAAwB;CACtE,KAAK,MAAM,OAAO,SAChB,IAAI,QAAQ,SAAS,KAAK,MACxB,OAAO;EAAE,GAAG;EAAM,GAAG;CAAQ;CAIjC,OAAO;AACT"}
{"version":3,"file":"factory.js","names":[],"sources":["../src/factory.ts"],"sourcesContent":["import type { Node } from './nodes/index.ts'\n\n// Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes\n// `ts.factory.createX`. Aggregating them here lets `export * as factory from './factory.ts'` in the\n// barrel surface every `createX` alongside the `createFile`/`update` helpers from a single module.\nexport { createArrowFunction, createBreak, createConst, createFunction, createJsx, createText, createType } from './nodes/code.ts'\nexport { createContent } from './nodes/content.ts'\nexport { createExport, createFile, createImport, createSource } from './nodes/file.ts'\nexport type { UserFileNode } from './nodes/file.ts'\nexport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts'\nexport { createInput } from './nodes/input.ts'\nexport { createOperation } from './nodes/operation.ts'\nexport { createOutput } from './nodes/output.ts'\nexport { createParameter } from './nodes/parameter.ts'\nexport { createProperty } from './nodes/property.ts'\nexport { createRequestBody } from './nodes/requestBody.ts'\nexport { createResponse } from './nodes/response.ts'\nexport { createSchema } from './nodes/schema.ts'\n\n/**\n * Identity-preserving node update: returns `node` unchanged when every field in\n * `changes` already equals (by reference) the current value, otherwise a new node\n * with the changes applied.\n *\n * Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the\n * structural sharing in {@link transform} so a no-op rewrite doesn't allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is\n * shallow: a structurally-equal but newly-allocated array/object counts as a change.\n *\n * @example\n * ```ts\n * update(node, { name: node.name }) // -> same `node` reference\n * update(node, { name: 'renamed' }) // -> new node, `name` replaced\n * ```\n */\nexport function update<T extends Node>(node: T, changes: Partial<T>): T {\n for (const key in changes) {\n if (changes[key] !== node[key as keyof T]) {\n return { ...node, ...changes }\n }\n }\n\n return node\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAAgB,OAAuB,MAAS,SAAwB;CACtE,KAAK,MAAM,OAAO,SAChB,IAAI,QAAQ,SAAS,KAAK,MACxB,OAAO;EAAE,GAAG;EAAM,GAAG;CAAQ;CAIjC,OAAO;AACT"}

Sorry, the diff of this file is too big to display

import { n as __name } from "./chunk-CNktS9qV.js";
import { C as ParameterNode, M as TypeExpression, N as TypeLiteralNode, O as FunctionParameterNode, f as OperationNode, k as FunctionParametersNode } from "./index-CTaN_ee_.js";
//#region src/utils/operationParams.d.ts
declare function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
/**
* Named type for a group of parameters (query or header) emitted as a single typed parameter.
*/
type ParamGroupType = {
/**
* Type expression for the group, a plain group-name reference.
*/
type: TypeExpression;
/**
* Whether the parameter group is optional.
*/
optional: boolean;
};
/**
* Resolver interface for {@link createOperationParams}.
*
* `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.
*/
type OperationParamsResolver = {
/**
* Resolves the type name for an individual parameter.
*
* @example Individual path parameter name
* `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
*/
resolveParamName(node: OperationNode, param: ParameterNode): string;
/**
* Resolves the request body type name.
*
* @example Request body type name
* `resolver.resolveDataName(node) // → 'CreatePetData'`
*/
resolveDataName(node: OperationNode): string;
/**
* Resolves the grouped path parameters type name.
* When the return value equals `resolveParamName`, no indexed access is emitted.
*
* @example Grouped path params type name
* `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
*/
resolvePathParamsName(node: OperationNode, param: ParameterNode): string;
/**
* Resolves the grouped query parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped query params type name
* `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
*/
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string;
/**
* Resolves the grouped header parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped header params type name
* `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
*/
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string;
};
/**
* Options for {@link createOperationParams}.
*/
type CreateOperationParamsOptions = {
/**
* How all operation parameters are grouped in the function signature.
* - `'object'` wraps all params into a single destructured object `{ petId, data, params }`
* - `'inline'` emits each param category as a separate top-level parameter
*/
paramsType: 'object' | 'inline';
/**
* How path parameters are emitted when `paramsType` is `'inline'`.
* - `'object'` groups them as `{ petId, storeId }: PathParams`
* - `'inline'` spreads them as individual parameters `petId: string, storeId: string`
* - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`
*/
pathParamsType: 'object' | 'inline' | 'inlineSpread';
/**
* Converts parameter names to camelCase before output.
*/
paramsCasing?: 'camelcase';
/**
* Resolver for parameter and request body type names.
* Pass `ResolverTs` from `@kubb/plugin-ts` directly.
* When omitted, falls back to the schema primitive or `'unknown'`.
*/
resolver?: OperationParamsResolver;
/**
* Default value for the path parameters binding when `pathParamsType` is `'object'`.
* Falls back to `'{}'` when all path params are optional.
*/
pathParamsDefault?: string;
/**
* Extra parameters appended after the standard operation parameters.
*
* @example Plugin-specific trailing parameter
* ```ts
* extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]
* ```
*/
extraParams?: Array<FunctionParameterNode>;
/**
* Override the default parameter names used for body, query, header, and rest-path groups.
*
* Useful when targeting languages or frameworks with different naming conventions.
*
* @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }
*/
paramNames?: {
/**
* Name for the request body parameter.
* @default 'data'
*/
data?: string;
/**
* Name for the query parameters group parameter.
* @default 'params'
*/
params?: string;
/**
* Name for the header parameters group parameter.
* @default 'headers'
*/
headers?: string;
/**
* Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.
* @default 'pathParams'
*/
path?: string;
};
/**
* Transforms every resolved type name before it lands in a parameter node, for framework-level
* type wrappers.
*
* @example Vue Query, wrap every parameter type with `MaybeRefOrGetter`
* `typeWrapper: (t) => \`MaybeRefOrGetter<${t}>\``
*/
typeWrapper?: (type: string) => string;
};
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, it falls back to the schema primitive (a plain type-name string). When the
* parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like
* `GroupParams['petId']`, otherwise the resolved individual name.
*/
declare function resolveParamType({
node,
param,
resolver
}: {
node: OperationNode;
param: ParameterNode;
resolver: OperationParamsResolver | undefined;
}): TypeExpression;
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
declare function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode;
/**
* Shared arguments for building a query or header parameter group.
*/
type BuildGroupArgs = {
name: string;
node: OperationNode;
params: Array<ParameterNode>;
groupType: ParamGroupType | null;
resolver: OperationParamsResolver | undefined;
wrapType: (type: string) => string;
};
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
declare function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode>;
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
declare function buildTypeLiteral({
node,
params,
resolver
}: {
node: OperationNode;
params: Array<ParameterNode>;
resolver: OperationParamsResolver | undefined;
}): TypeLiteralNode;
//#endregion
export { buildTypeLiteral as a, resolveParamType as c, buildGroupParam as i, OperationParamsResolver as n, caseParams as o, ParamGroupType as r, createOperationParams as s, BuildGroupArgs as t };
//# sourceMappingURL=operationParams-B44p6EQy.d.ts.map
//#region \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
value,
configurable: true
});
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __exportAll = (all, no_symbols) => {
let target = {};
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
return target;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
let node_crypto = require("node:crypto");
let node_path = require("node:path");
node_path = __toESM(node_path, 1);
//#region src/defineNode.ts
/**
* Visitor callback names, one per traversable node kind, in traversal order.
* Kept in sync with the keys of `Visitor` in `visitor.ts`.
*/
const visitorKeys = [
"input",
"output",
"operation",
"schema",
"property",
"parameter",
"response"
];
/**
* Builds a type guard that matches nodes of the given `kind`.
*/
function isKind(kind) {
return (node) => node.kind === kind;
}
/**
* Defines a node once and derives its `create` builder, `is` guard, and traversal
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
* `kind`, so node construction lives in one place without scattered `as` casts.
*
* @example Simple node
* ```ts
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
* const createImport = importDef.create
* ```
*
* @example Node with a build hook
* ```ts
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
* kind: 'Property',
* build: (props) => ({ ...props, required: props.required ?? false }),
* children: ['schema'],
* visitorKey: 'property',
* })
* ```
*/
function defineNode(config) {
const { kind, defaults, build, children, visitorKey } = config;
function create(input, dialect) {
const base = build ? build(input, dialect) : input;
return {
...defaults,
...base,
kind
};
}
return {
kind,
create,
is: isKind(kind),
children,
visitorKey
};
}
//#endregion
//#region src/nodes/schema.ts
/**
* Maps schema `type` to its underlying `primitive`.
* Primitive types map to themselves. Special string formats map to `'string'`.
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
*/
const TYPE_TO_PRIMITIVE = {
string: "string",
number: "number",
integer: "integer",
bigint: "bigint",
boolean: "boolean",
null: "null",
any: "any",
unknown: "unknown",
void: "void",
never: "never",
object: "object",
array: "array",
date: "date",
uuid: "string",
email: "string",
url: "string",
datetime: "string",
time: "string"
};
/**
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
*/
const schemaDef = defineNode({
kind: "Schema",
build: (props) => {
if (props.type === "object") return {
properties: [],
primitive: "object",
...props
};
return {
primitive: TYPE_TO_PRIMITIVE[props.type],
...props
};
},
children: [
"properties",
"items",
"members",
"additionalProperties"
],
visitorKey: "schema"
});
function createSchema(props) {
return schemaDef.create(props);
}
//#endregion
//#region ../../internals/utils/src/casing.ts
/**
* Shared implementation for camelCase and PascalCase conversion.
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
* and capitalizes each word according to `pascal`.
*
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
*/
function toCamelOrPascal(text, pascal) {
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
if (word.length > 1 && word === word.toUpperCase()) return word;
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
}).join("").replace(/[^a-zA-Z0-9]/g, "");
}
/**
* Converts `text` to camelCase.
*
* @example Word boundaries
* `camelCase('hello-world') // 'helloWorld'`
*
* @example With a prefix
* `camelCase('tag', { prefix: 'create' }) // 'createTag'`
*/
function camelCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
}
/**
* Converts `text` to PascalCase.
*
* @example Word boundaries
* `pascalCase('hello-world') // 'HelloWorld'`
*
* @example With a suffix
* `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
*/
function pascalCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
}
//#endregion
//#region ../../internals/utils/src/fs.ts
/**
* Strips the file extension from a path or file name.
* Only removes the last `.ext` segment when the dot is not part of a directory name.
*
* @example
* trimExtName('petStore.ts') // 'petStore'
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
* trimExtName('noExtension') // 'noExtension'
*/
function trimExtName(text) {
const dotIndex = text.lastIndexOf(".");
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
return text;
}
//#endregion
//#region src/utils/extractStringsFromNodes.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
function extractStringsFromNodes(nodes) {
if (!nodes?.length) return "";
return nodes.map((node) => {
if (typeof node === "string") return node;
if (node.kind === "Text") return node.value;
if (node.kind === "Break") return "";
if (node.kind === "Jsx") return node.value;
const parts = [];
if ("params" in node && node.params) parts.push(node.params);
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
if ("returnType" in node && node.returnType) parts.push(node.returnType);
if ("type" in node && typeof node.type === "string") parts.push(node.type);
const nested = extractStringsFromNodes(node.nodes);
if (nested) parts.push(nested);
return parts.join("\n");
}).filter(Boolean).join("\n");
}
//#endregion
//#region src/nodes/code.ts
/**
* Definition for the {@link ConstNode}.
*/
const constDef = defineNode({ kind: "Const" });
/**
* Definition for the {@link TypeNode}.
*/
const typeDef = defineNode({ kind: "Type" });
/**
* Definition for the {@link FunctionNode}.
*/
const functionDef = defineNode({ kind: "Function" });
/**
* Definition for the {@link ArrowFunctionNode}.
*/
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
/**
* Definition for the {@link TextNode}.
*/
const textDef = defineNode({
kind: "Text",
build: (value) => ({ value })
});
/**
* Definition for the {@link BreakNode}.
*/
const breakDef = defineNode({
kind: "Break",
build: () => ({})
});
/**
* Definition for the {@link JsxNode}.
*/
const jsxDef = defineNode({
kind: "Jsx",
build: (value) => ({ value })
});
/**
* Creates a `ConstNode` representing a TypeScript `const` declaration.
*
* @example Exported constant with type and `as const`
* ```ts
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
* // export const pets: Pet[] = ... as const
* ```
*/
const createConst = constDef.create;
/**
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
*
* @example
* ```ts
* createType({ name: 'Pet', export: true })
* // export type Pet = ...
* ```
*/
const createType = typeDef.create;
/**
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
*
* @example
* ```ts
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
* // export async function fetchPet(): Promise<Pet> { ... }
* ```
*/
const createFunction = functionDef.create;
/**
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
*
* @example
* ```ts
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
* // export const double = (n: number) => ...
* ```
*/
const createArrowFunction = arrowFunctionDef.create;
/**
* Creates a {@link TextNode} representing a raw string fragment in the source output.
*
* @example
* ```ts
* createText('return fetch(id)')
* // { kind: 'Text', value: 'return fetch(id)' }
* ```
*/
const createText = textDef.create;
/**
* Creates a {@link BreakNode} representing a line break in the source output.
*
* @example
* ```ts
* createBreak()
* // { kind: 'Break' }
* ```
*/
function createBreak() {
return breakDef.create();
}
/**
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
*
* @example
* ```ts
* createJsx('<>\n <a href={href}>Open</a>\n</>')
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
* ```
*/
const createJsx = jsxDef.create;
//#endregion
//#region src/nodes/content.ts
/**
* Definition for the {@link ContentNode}.
*/
const contentDef = defineNode({
kind: "Content",
children: ["schema"]
});
/**
* Creates a `ContentNode` for a single request-body or response content type.
*/
const createContent = contentDef.create;
//#endregion
//#region src/utils/fileMerge.ts
function sourceKey(source) {
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
}
function pathTypeKey(path, isTypeOnly) {
return `${path}:${isTypeOnly ?? false}`;
}
function exportKey(path, name, isTypeOnly, asAlias) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
}
function importKey(path, name, isTypeOnly) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
}
/**
* Computes a multi-level sort key for exports and imports:
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
*/
function sortKey(node) {
const isArray = Array.isArray(node.name) ? "1" : "0";
const typeOnly = node.isTypeOnly ? "0" : "1";
const hasName = node.name != null ? "1" : "0";
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
}
/**
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
*
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
*/
function combineSources(sources) {
const seen = /* @__PURE__ */ new Map();
for (const source of sources) {
const key = sourceKey(source);
if (!seen.has(key)) seen.set(key, source);
}
return [...seen.values()];
}
/**
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
*
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
*/
function mergeNameArrays(existing, incoming) {
const merged = new Set(existing);
for (const name of incoming) merged.add(name);
return [...merged];
}
/**
* Deduplicates and merges `ExportNode` objects by path and type.
*
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
*/
function combineExports(exports) {
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = exports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
const { name, path, isTypeOnly, asAlias } = curr;
if (Array.isArray(name)) {
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name: [...new Set(name)]
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
const key = exportKey(path, name, isTypeOnly, asAlias);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
/**
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
*
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
*/
function combineImports(imports, exports, source) {
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
const importNameMemo = /* @__PURE__ */ new Map();
const canonicalizeName = (n) => {
if (typeof n === "string") return n;
const key = `${n.propertyName}:${n.name ?? ""}`;
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
return importNameMemo.get(key);
};
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
for (const node of imports) {
if (!Array.isArray(node.name)) continue;
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
}
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = imports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
if (curr.path === curr.root) continue;
const { path, isTypeOnly } = curr;
let { name } = curr;
if (Array.isArray(name)) {
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
const key = importKey(path, name, isTypeOnly);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
//#endregion
//#region src/nodes/file.ts
/**
* Definition for the {@link ImportNode}.
*/
const importDef = defineNode({ kind: "Import" });
/**
* Definition for the {@link ExportNode}.
*/
const exportDef = defineNode({ kind: "Export" });
/**
* Definition for the {@link SourceNode}.
*/
const sourceDef = defineNode({ kind: "Source" });
/**
* Definition for the {@link FileNode}. The fully resolved builder lives in
* `createFile`, so this definition only supplies the guard.
*/
const fileDef = defineNode({ kind: "File" });
/**
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
*
* @example Named import
* ```ts
* createImport({ name: ['useState'], path: 'react' })
* // import { useState } from 'react'
* ```
*/
const createImport = importDef.create;
/**
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
*
* @example Named export
* ```ts
* createExport({ name: ['Pet'], path: './Pet' })
* // export { Pet } from './Pet'
* ```
*/
const createExport = exportDef.create;
/**
* Creates a `SourceNode` representing a fragment of source code within a file.
*
* @example
* ```ts
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
* ```
*/
const createSource = sourceDef.create;
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
function createFile(input) {
const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
if (!kept.length) return [];
return [kept.length === imp.name.length ? imp : {
...imp,
name: kept
}];
});
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
return {
kind: "File",
...input,
id: (0, node_crypto.hash)("sha256", input.path, "hex"),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? {}
};
}
//#endregion
//#region src/nodes/function.ts
/**
* Definition for the {@link TypeLiteralNode}.
*/
const typeLiteralDef = defineNode({ kind: "TypeLiteral" });
/**
* Definition for the {@link IndexedAccessTypeNode}.
*/
const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" });
/**
* Definition for the {@link ObjectBindingPatternNode}.
*/
const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" });
/**
* Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`.
* Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name
* paired with a {@link TypeLiteralNode} type.
*/
const functionParameterDef = defineNode({
kind: "FunctionParameter",
build: (input) => {
if ("properties" in input) return {
name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }),
type: createTypeLiteral({ members: input.properties.map((p) => ({
name: p.name,
type: p.type,
optional: p.optional ?? false
})) }),
optional: input.optional ?? false,
...input.default !== void 0 ? { default: input.default } : {}
};
return {
optional: false,
...input
};
}
});
/**
* Definition for the {@link FunctionParametersNode}.
*/
const functionParametersDef = defineNode({
kind: "FunctionParameters",
defaults: { params: [] }
});
/**
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
*
* @example
* ```ts
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
* // { petId: string }
* ```
*/
const createTypeLiteral = typeLiteralDef.create;
/**
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
*
* @example
* ```ts
* createIndexedAccessType({ target: 'DeletePetPathParams', key: 'petId' })
* // DeletePetPathParams['petId']
* ```
*/
const createIndexedAccessType = indexedAccessTypeDef.create;
/**
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
*
* @example
* ```ts
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
* // { id, name }
* ```
*/
const createObjectBindingPattern = objectBindingPatternDef.create;
/**
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.
*
* @example Optional param
* ```ts
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
* // → params?: QueryParams
* ```
*
* @example Destructured group
* ```ts
* createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' })
* // → { id, name }: { id: string; name?: string } = {}
* ```
*/
const createFunctionParameter = functionParameterDef.create;
/**
* Creates a `FunctionParametersNode` from an ordered list of parameters.
*
* @example
* ```ts
* const empty = createFunctionParameters()
* // { kind: 'FunctionParameters', params: [] }
* ```
*/
function createFunctionParameters(props = {}) {
return functionParametersDef.create(props);
}
//#endregion
//#region src/nodes/input.ts
/**
* Definition for the {@link InputNode}.
*/
const inputDef = defineNode({
kind: "Input",
defaults: {
schemas: [],
operations: [],
meta: {
circularNames: [],
enumNames: []
}
},
children: ["schemas", "operations"],
visitorKey: "input"
});
/**
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
* `operations` are `AsyncIterable` sources. Otherwise it builds the eager variant with array
* `schemas`/`operations`. Both variants get the defaulted `meta`.
*
* @example Eager
* ```ts
* const input = createInput()
* // { kind: 'Input', schemas: [], operations: [] }
* ```
*
* @example Streaming
* ```ts
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
* ```
*/
function createInput(options = {}) {
const { stream, ...overrides } = options;
if (stream) return {
kind: "Input",
meta: {
circularNames: [],
enumNames: []
},
...overrides
};
return inputDef.create(overrides);
}
//#endregion
//#region src/nodes/requestBody.ts
/**
* Definition for the {@link RequestBodyNode}. Content entries are built upfront with
* {@link createContent}, mirroring how `parameters` and `responses` take prebuilt nodes.
*/
const requestBodyDef = defineNode({
kind: "RequestBody",
children: ["content"]
});
/**
* Creates a `RequestBodyNode`.
*/
const createRequestBody = requestBodyDef.create;
//#endregion
//#region src/nodes/operation.ts
/**
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
* normalized into a `RequestBodyNode`.
*/
const operationDef = defineNode({
kind: "Operation",
build: (props) => {
const { requestBody, ...rest } = props;
const isHttp = rest.method !== void 0 && rest.path !== void 0;
return {
tags: [],
parameters: [],
responses: [],
...rest,
...isHttp ? { protocol: "http" } : {},
requestBody: requestBody ? createRequestBody(requestBody) : void 0
};
},
children: [
"parameters",
"requestBody",
"responses"
],
visitorKey: "operation"
});
function createOperation(props) {
return operationDef.create(props);
}
//#endregion
//#region src/nodes/output.ts
/**
* Definition for the {@link OutputNode}.
*/
const outputDef = defineNode({
kind: "Output",
defaults: { files: [] },
visitorKey: "output"
});
/**
* Creates an `OutputNode` with a stable default for `files`.
*
* @example
* ```ts
* const output = createOutput()
* // { kind: 'Output', files: [] }
* ```
*/
function createOutput(overrides = {}) {
return outputDef.create(overrides);
}
//#endregion
//#region src/nodes/parameter.ts
/**
* Definition for the {@link ParameterNode}. `required` defaults to `false`. When a `dialect` is
* passed to `create`, the schema's `optional`/`nullish` flags are derived through its
* `optionality`; without one, the schema is left as-is.
*/
const parameterDef = defineNode({
kind: "Parameter",
build: (props, dialect) => {
const required = props.required ?? false;
return {
...props,
required,
schema: dialect ? dialect.schema.optionality(props.schema, required) : props.schema
};
},
children: ["schema"],
visitorKey: "parameter"
});
/**
* Creates a `ParameterNode`.
*
* @example
* ```ts
* const param = createParameter({
* name: 'petId',
* in: 'path',
* required: true,
* schema: createSchema({ type: 'string' }),
* })
* ```
*/
const createParameter = parameterDef.create;
//#endregion
//#region src/nodes/property.ts
/**
* Definition for the {@link PropertyNode}. `required` defaults to `false`. When a `dialect` is
* passed to `create`, the schema's `optional`/`nullish` flags are derived through its
* `optionality`; without one, the schema is left as-is.
*/
const propertyDef = defineNode({
kind: "Property",
build: (props, dialect) => {
const required = props.required ?? false;
return {
...props,
required,
schema: dialect ? dialect.schema.optionality(props.schema, required) : props.schema
};
},
children: ["schema"],
visitorKey: "property"
});
/**
* Creates a `PropertyNode`.
*
* @example
* ```ts
* const property = createProperty({
* name: 'status',
* required: true,
* schema: createSchema({ type: 'string', nullable: true }),
* })
* // required=true, no optional/nullish
* ```
*/
const createProperty = propertyDef.create;
//#endregion
//#region src/nodes/response.ts
/**
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
*/
const responseDef = defineNode({
kind: "Response",
build: (props) => {
const { schema, mediaType, keysToOmit, content, ...rest } = props;
const entries = content ?? (schema ? [createContent({
contentType: mediaType ?? "application/json",
schema,
keysToOmit: keysToOmit ?? null
})] : void 0);
return {
...rest,
content: entries
};
},
children: ["content"],
visitorKey: "response"
});
/**
* Creates a `ResponseNode`.
*
* @example
* ```ts
* const response = createResponse({
* statusCode: '200',
* content: [createContent({ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) })],
* })
* ```
*/
const createResponse = responseDef.create;
//#endregion
Object.defineProperty(exports, "__exportAll", {
enumerable: true,
get: function() {
return __exportAll;
}
});
Object.defineProperty(exports, "__name", {
enumerable: true,
get: function() {
return __name;
}
});
Object.defineProperty(exports, "arrowFunctionDef", {
enumerable: true,
get: function() {
return arrowFunctionDef;
}
});
Object.defineProperty(exports, "breakDef", {
enumerable: true,
get: function() {
return breakDef;
}
});
Object.defineProperty(exports, "camelCase", {
enumerable: true,
get: function() {
return camelCase;
}
});
Object.defineProperty(exports, "constDef", {
enumerable: true,
get: function() {
return constDef;
}
});
Object.defineProperty(exports, "contentDef", {
enumerable: true,
get: function() {
return contentDef;
}
});
Object.defineProperty(exports, "createArrowFunction", {
enumerable: true,
get: function() {
return createArrowFunction;
}
});
Object.defineProperty(exports, "createBreak", {
enumerable: true,
get: function() {
return createBreak;
}
});
Object.defineProperty(exports, "createConst", {
enumerable: true,
get: function() {
return createConst;
}
});
Object.defineProperty(exports, "createContent", {
enumerable: true,
get: function() {
return createContent;
}
});
Object.defineProperty(exports, "createExport", {
enumerable: true,
get: function() {
return createExport;
}
});
Object.defineProperty(exports, "createFile", {
enumerable: true,
get: function() {
return createFile;
}
});
Object.defineProperty(exports, "createFunction", {
enumerable: true,
get: function() {
return createFunction;
}
});
Object.defineProperty(exports, "createFunctionParameter", {
enumerable: true,
get: function() {
return createFunctionParameter;
}
});
Object.defineProperty(exports, "createFunctionParameters", {
enumerable: true,
get: function() {
return createFunctionParameters;
}
});
Object.defineProperty(exports, "createImport", {
enumerable: true,
get: function() {
return createImport;
}
});
Object.defineProperty(exports, "createIndexedAccessType", {
enumerable: true,
get: function() {
return createIndexedAccessType;
}
});
Object.defineProperty(exports, "createInput", {
enumerable: true,
get: function() {
return createInput;
}
});
Object.defineProperty(exports, "createJsx", {
enumerable: true,
get: function() {
return createJsx;
}
});
Object.defineProperty(exports, "createObjectBindingPattern", {
enumerable: true,
get: function() {
return createObjectBindingPattern;
}
});
Object.defineProperty(exports, "createOperation", {
enumerable: true,
get: function() {
return createOperation;
}
});
Object.defineProperty(exports, "createOutput", {
enumerable: true,
get: function() {
return createOutput;
}
});
Object.defineProperty(exports, "createParameter", {
enumerable: true,
get: function() {
return createParameter;
}
});
Object.defineProperty(exports, "createProperty", {
enumerable: true,
get: function() {
return createProperty;
}
});
Object.defineProperty(exports, "createRequestBody", {
enumerable: true,
get: function() {
return createRequestBody;
}
});
Object.defineProperty(exports, "createResponse", {
enumerable: true,
get: function() {
return createResponse;
}
});
Object.defineProperty(exports, "createSchema", {
enumerable: true,
get: function() {
return createSchema;
}
});
Object.defineProperty(exports, "createSource", {
enumerable: true,
get: function() {
return createSource;
}
});
Object.defineProperty(exports, "createText", {
enumerable: true,
get: function() {
return createText;
}
});
Object.defineProperty(exports, "createType", {
enumerable: true,
get: function() {
return createType;
}
});
Object.defineProperty(exports, "createTypeLiteral", {
enumerable: true,
get: function() {
return createTypeLiteral;
}
});
Object.defineProperty(exports, "defineNode", {
enumerable: true,
get: function() {
return defineNode;
}
});
Object.defineProperty(exports, "exportDef", {
enumerable: true,
get: function() {
return exportDef;
}
});
Object.defineProperty(exports, "extractStringsFromNodes", {
enumerable: true,
get: function() {
return extractStringsFromNodes;
}
});
Object.defineProperty(exports, "fileDef", {
enumerable: true,
get: function() {
return fileDef;
}
});
Object.defineProperty(exports, "functionDef", {
enumerable: true,
get: function() {
return functionDef;
}
});
Object.defineProperty(exports, "functionParameterDef", {
enumerable: true,
get: function() {
return functionParameterDef;
}
});
Object.defineProperty(exports, "functionParametersDef", {
enumerable: true,
get: function() {
return functionParametersDef;
}
});
Object.defineProperty(exports, "importDef", {
enumerable: true,
get: function() {
return importDef;
}
});
Object.defineProperty(exports, "indexedAccessTypeDef", {
enumerable: true,
get: function() {
return indexedAccessTypeDef;
}
});
Object.defineProperty(exports, "inputDef", {
enumerable: true,
get: function() {
return inputDef;
}
});
Object.defineProperty(exports, "jsxDef", {
enumerable: true,
get: function() {
return jsxDef;
}
});
Object.defineProperty(exports, "objectBindingPatternDef", {
enumerable: true,
get: function() {
return objectBindingPatternDef;
}
});
Object.defineProperty(exports, "operationDef", {
enumerable: true,
get: function() {
return operationDef;
}
});
Object.defineProperty(exports, "outputDef", {
enumerable: true,
get: function() {
return outputDef;
}
});
Object.defineProperty(exports, "parameterDef", {
enumerable: true,
get: function() {
return parameterDef;
}
});
Object.defineProperty(exports, "pascalCase", {
enumerable: true,
get: function() {
return pascalCase;
}
});
Object.defineProperty(exports, "propertyDef", {
enumerable: true,
get: function() {
return propertyDef;
}
});
Object.defineProperty(exports, "requestBodyDef", {
enumerable: true,
get: function() {
return requestBodyDef;
}
});
Object.defineProperty(exports, "responseDef", {
enumerable: true,
get: function() {
return responseDef;
}
});
Object.defineProperty(exports, "schemaDef", {
enumerable: true,
get: function() {
return schemaDef;
}
});
Object.defineProperty(exports, "sourceDef", {
enumerable: true,
get: function() {
return sourceDef;
}
});
Object.defineProperty(exports, "textDef", {
enumerable: true,
get: function() {
return textDef;
}
});
Object.defineProperty(exports, "typeDef", {
enumerable: true,
get: function() {
return typeDef;
}
});
Object.defineProperty(exports, "typeLiteralDef", {
enumerable: true,
get: function() {
return typeLiteralDef;
}
});
Object.defineProperty(exports, "visitorKeys", {
enumerable: true,
get: function() {
return visitorKeys;
}
});
//# sourceMappingURL=response-BeG6hq2D.cjs.map

Sorry, the diff of this file is too big to display

import "./chunk-CNktS9qV.js";
import { hash } from "node:crypto";
import path from "node:path";
//#region src/defineNode.ts
/**
* Visitor callback names, one per traversable node kind, in traversal order.
* Kept in sync with the keys of `Visitor` in `visitor.ts`.
*/
const visitorKeys = [
"input",
"output",
"operation",
"schema",
"property",
"parameter",
"response"
];
/**
* Builds a type guard that matches nodes of the given `kind`.
*/
function isKind(kind) {
return (node) => node.kind === kind;
}
/**
* Defines a node once and derives its `create` builder, `is` guard, and traversal
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
* `kind`, so node construction lives in one place without scattered `as` casts.
*
* @example Simple node
* ```ts
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
* const createImport = importDef.create
* ```
*
* @example Node with a build hook
* ```ts
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
* kind: 'Property',
* build: (props) => ({ ...props, required: props.required ?? false }),
* children: ['schema'],
* visitorKey: 'property',
* })
* ```
*/
function defineNode(config) {
const { kind, defaults, build, children, visitorKey } = config;
function create(input, dialect) {
const base = build ? build(input, dialect) : input;
return {
...defaults,
...base,
kind
};
}
return {
kind,
create,
is: isKind(kind),
children,
visitorKey
};
}
//#endregion
//#region src/nodes/schema.ts
/**
* Maps schema `type` to its underlying `primitive`.
* Primitive types map to themselves. Special string formats map to `'string'`.
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
*/
const TYPE_TO_PRIMITIVE = {
string: "string",
number: "number",
integer: "integer",
bigint: "bigint",
boolean: "boolean",
null: "null",
any: "any",
unknown: "unknown",
void: "void",
never: "never",
object: "object",
array: "array",
date: "date",
uuid: "string",
email: "string",
url: "string",
datetime: "string",
time: "string"
};
/**
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
*/
const schemaDef = defineNode({
kind: "Schema",
build: (props) => {
if (props.type === "object") return {
properties: [],
primitive: "object",
...props
};
return {
primitive: TYPE_TO_PRIMITIVE[props.type],
...props
};
},
children: [
"properties",
"items",
"members",
"additionalProperties"
],
visitorKey: "schema"
});
function createSchema(props) {
return schemaDef.create(props);
}
//#endregion
//#region ../../internals/utils/src/casing.ts
/**
* Shared implementation for camelCase and PascalCase conversion.
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
* and capitalizes each word according to `pascal`.
*
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
*/
function toCamelOrPascal(text, pascal) {
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
if (word.length > 1 && word === word.toUpperCase()) return word;
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
}).join("").replace(/[^a-zA-Z0-9]/g, "");
}
/**
* Converts `text` to camelCase.
*
* @example Word boundaries
* `camelCase('hello-world') // 'helloWorld'`
*
* @example With a prefix
* `camelCase('tag', { prefix: 'create' }) // 'createTag'`
*/
function camelCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
}
/**
* Converts `text` to PascalCase.
*
* @example Word boundaries
* `pascalCase('hello-world') // 'HelloWorld'`
*
* @example With a suffix
* `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
*/
function pascalCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
}
//#endregion
//#region ../../internals/utils/src/fs.ts
/**
* Strips the file extension from a path or file name.
* Only removes the last `.ext` segment when the dot is not part of a directory name.
*
* @example
* trimExtName('petStore.ts') // 'petStore'
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
* trimExtName('noExtension') // 'noExtension'
*/
function trimExtName(text) {
const dotIndex = text.lastIndexOf(".");
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
return text;
}
//#endregion
//#region src/utils/extractStringsFromNodes.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
function extractStringsFromNodes(nodes) {
if (!nodes?.length) return "";
return nodes.map((node) => {
if (typeof node === "string") return node;
if (node.kind === "Text") return node.value;
if (node.kind === "Break") return "";
if (node.kind === "Jsx") return node.value;
const parts = [];
if ("params" in node && node.params) parts.push(node.params);
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
if ("returnType" in node && node.returnType) parts.push(node.returnType);
if ("type" in node && typeof node.type === "string") parts.push(node.type);
const nested = extractStringsFromNodes(node.nodes);
if (nested) parts.push(nested);
return parts.join("\n");
}).filter(Boolean).join("\n");
}
//#endregion
//#region src/nodes/code.ts
/**
* Definition for the {@link ConstNode}.
*/
const constDef = defineNode({ kind: "Const" });
/**
* Definition for the {@link TypeNode}.
*/
const typeDef = defineNode({ kind: "Type" });
/**
* Definition for the {@link FunctionNode}.
*/
const functionDef = defineNode({ kind: "Function" });
/**
* Definition for the {@link ArrowFunctionNode}.
*/
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
/**
* Definition for the {@link TextNode}.
*/
const textDef = defineNode({
kind: "Text",
build: (value) => ({ value })
});
/**
* Definition for the {@link BreakNode}.
*/
const breakDef = defineNode({
kind: "Break",
build: () => ({})
});
/**
* Definition for the {@link JsxNode}.
*/
const jsxDef = defineNode({
kind: "Jsx",
build: (value) => ({ value })
});
/**
* Creates a `ConstNode` representing a TypeScript `const` declaration.
*
* @example Exported constant with type and `as const`
* ```ts
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
* // export const pets: Pet[] = ... as const
* ```
*/
const createConst = constDef.create;
/**
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
*
* @example
* ```ts
* createType({ name: 'Pet', export: true })
* // export type Pet = ...
* ```
*/
const createType = typeDef.create;
/**
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
*
* @example
* ```ts
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
* // export async function fetchPet(): Promise<Pet> { ... }
* ```
*/
const createFunction = functionDef.create;
/**
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
*
* @example
* ```ts
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
* // export const double = (n: number) => ...
* ```
*/
const createArrowFunction = arrowFunctionDef.create;
/**
* Creates a {@link TextNode} representing a raw string fragment in the source output.
*
* @example
* ```ts
* createText('return fetch(id)')
* // { kind: 'Text', value: 'return fetch(id)' }
* ```
*/
const createText = textDef.create;
/**
* Creates a {@link BreakNode} representing a line break in the source output.
*
* @example
* ```ts
* createBreak()
* // { kind: 'Break' }
* ```
*/
function createBreak() {
return breakDef.create();
}
/**
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
*
* @example
* ```ts
* createJsx('<>\n <a href={href}>Open</a>\n</>')
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
* ```
*/
const createJsx = jsxDef.create;
//#endregion
//#region src/nodes/content.ts
/**
* Definition for the {@link ContentNode}.
*/
const contentDef = defineNode({
kind: "Content",
children: ["schema"]
});
/**
* Creates a `ContentNode` for a single request-body or response content type.
*/
const createContent = contentDef.create;
//#endregion
//#region src/utils/fileMerge.ts
function sourceKey(source) {
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
}
function pathTypeKey(path, isTypeOnly) {
return `${path}:${isTypeOnly ?? false}`;
}
function exportKey(path, name, isTypeOnly, asAlias) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
}
function importKey(path, name, isTypeOnly) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
}
/**
* Computes a multi-level sort key for exports and imports:
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
*/
function sortKey(node) {
const isArray = Array.isArray(node.name) ? "1" : "0";
const typeOnly = node.isTypeOnly ? "0" : "1";
const hasName = node.name != null ? "1" : "0";
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
}
/**
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
*
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
*/
function combineSources(sources) {
const seen = /* @__PURE__ */ new Map();
for (const source of sources) {
const key = sourceKey(source);
if (!seen.has(key)) seen.set(key, source);
}
return [...seen.values()];
}
/**
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
*
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
*/
function mergeNameArrays(existing, incoming) {
const merged = new Set(existing);
for (const name of incoming) merged.add(name);
return [...merged];
}
/**
* Deduplicates and merges `ExportNode` objects by path and type.
*
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
*/
function combineExports(exports) {
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = exports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
const { name, path, isTypeOnly, asAlias } = curr;
if (Array.isArray(name)) {
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name: [...new Set(name)]
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
const key = exportKey(path, name, isTypeOnly, asAlias);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
/**
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
*
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
*/
function combineImports(imports, exports, source) {
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
const importNameMemo = /* @__PURE__ */ new Map();
const canonicalizeName = (n) => {
if (typeof n === "string") return n;
const key = `${n.propertyName}:${n.name ?? ""}`;
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
return importNameMemo.get(key);
};
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
for (const node of imports) {
if (!Array.isArray(node.name)) continue;
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
}
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = imports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
if (curr.path === curr.root) continue;
const { path, isTypeOnly } = curr;
let { name } = curr;
if (Array.isArray(name)) {
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
const key = importKey(path, name, isTypeOnly);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
//#endregion
//#region src/nodes/file.ts
/**
* Definition for the {@link ImportNode}.
*/
const importDef = defineNode({ kind: "Import" });
/**
* Definition for the {@link ExportNode}.
*/
const exportDef = defineNode({ kind: "Export" });
/**
* Definition for the {@link SourceNode}.
*/
const sourceDef = defineNode({ kind: "Source" });
/**
* Definition for the {@link FileNode}. The fully resolved builder lives in
* `createFile`, so this definition only supplies the guard.
*/
const fileDef = defineNode({ kind: "File" });
/**
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
*
* @example Named import
* ```ts
* createImport({ name: ['useState'], path: 'react' })
* // import { useState } from 'react'
* ```
*/
const createImport = importDef.create;
/**
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
*
* @example Named export
* ```ts
* createExport({ name: ['Pet'], path: './Pet' })
* // export { Pet } from './Pet'
* ```
*/
const createExport = exportDef.create;
/**
* Creates a `SourceNode` representing a fragment of source code within a file.
*
* @example
* ```ts
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
* ```
*/
const createSource = sourceDef.create;
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
function createFile(input) {
const extname = path.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
if (!kept.length) return [];
return [kept.length === imp.name.length ? imp : {
...imp,
name: kept
}];
});
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
return {
kind: "File",
...input,
id: hash("sha256", input.path, "hex"),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? {}
};
}
//#endregion
//#region src/nodes/function.ts
/**
* Definition for the {@link TypeLiteralNode}.
*/
const typeLiteralDef = defineNode({ kind: "TypeLiteral" });
/**
* Definition for the {@link IndexedAccessTypeNode}.
*/
const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" });
/**
* Definition for the {@link ObjectBindingPatternNode}.
*/
const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" });
/**
* Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`.
* Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name
* paired with a {@link TypeLiteralNode} type.
*/
const functionParameterDef = defineNode({
kind: "FunctionParameter",
build: (input) => {
if ("properties" in input) return {
name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }),
type: createTypeLiteral({ members: input.properties.map((p) => ({
name: p.name,
type: p.type,
optional: p.optional ?? false
})) }),
optional: input.optional ?? false,
...input.default !== void 0 ? { default: input.default } : {}
};
return {
optional: false,
...input
};
}
});
/**
* Definition for the {@link FunctionParametersNode}.
*/
const functionParametersDef = defineNode({
kind: "FunctionParameters",
defaults: { params: [] }
});
/**
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
*
* @example
* ```ts
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
* // { petId: string }
* ```
*/
const createTypeLiteral = typeLiteralDef.create;
/**
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
*
* @example
* ```ts
* createIndexedAccessType({ target: 'DeletePetPathParams', key: 'petId' })
* // DeletePetPathParams['petId']
* ```
*/
const createIndexedAccessType = indexedAccessTypeDef.create;
/**
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
*
* @example
* ```ts
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
* // { id, name }
* ```
*/
const createObjectBindingPattern = objectBindingPatternDef.create;
/**
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.
*
* @example Optional param
* ```ts
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
* // → params?: QueryParams
* ```
*
* @example Destructured group
* ```ts
* createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' })
* // → { id, name }: { id: string; name?: string } = {}
* ```
*/
const createFunctionParameter = functionParameterDef.create;
/**
* Creates a `FunctionParametersNode` from an ordered list of parameters.
*
* @example
* ```ts
* const empty = createFunctionParameters()
* // { kind: 'FunctionParameters', params: [] }
* ```
*/
function createFunctionParameters(props = {}) {
return functionParametersDef.create(props);
}
//#endregion
//#region src/nodes/input.ts
/**
* Definition for the {@link InputNode}.
*/
const inputDef = defineNode({
kind: "Input",
defaults: {
schemas: [],
operations: [],
meta: {
circularNames: [],
enumNames: []
}
},
children: ["schemas", "operations"],
visitorKey: "input"
});
/**
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
* `operations` are `AsyncIterable` sources. Otherwise it builds the eager variant with array
* `schemas`/`operations`. Both variants get the defaulted `meta`.
*
* @example Eager
* ```ts
* const input = createInput()
* // { kind: 'Input', schemas: [], operations: [] }
* ```
*
* @example Streaming
* ```ts
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
* ```
*/
function createInput(options = {}) {
const { stream, ...overrides } = options;
if (stream) return {
kind: "Input",
meta: {
circularNames: [],
enumNames: []
},
...overrides
};
return inputDef.create(overrides);
}
//#endregion
//#region src/nodes/requestBody.ts
/**
* Definition for the {@link RequestBodyNode}. Content entries are built upfront with
* {@link createContent}, mirroring how `parameters` and `responses` take prebuilt nodes.
*/
const requestBodyDef = defineNode({
kind: "RequestBody",
children: ["content"]
});
/**
* Creates a `RequestBodyNode`.
*/
const createRequestBody = requestBodyDef.create;
//#endregion
//#region src/nodes/operation.ts
/**
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
* normalized into a `RequestBodyNode`.
*/
const operationDef = defineNode({
kind: "Operation",
build: (props) => {
const { requestBody, ...rest } = props;
const isHttp = rest.method !== void 0 && rest.path !== void 0;
return {
tags: [],
parameters: [],
responses: [],
...rest,
...isHttp ? { protocol: "http" } : {},
requestBody: requestBody ? createRequestBody(requestBody) : void 0
};
},
children: [
"parameters",
"requestBody",
"responses"
],
visitorKey: "operation"
});
function createOperation(props) {
return operationDef.create(props);
}
//#endregion
//#region src/nodes/output.ts
/**
* Definition for the {@link OutputNode}.
*/
const outputDef = defineNode({
kind: "Output",
defaults: { files: [] },
visitorKey: "output"
});
/**
* Creates an `OutputNode` with a stable default for `files`.
*
* @example
* ```ts
* const output = createOutput()
* // { kind: 'Output', files: [] }
* ```
*/
function createOutput(overrides = {}) {
return outputDef.create(overrides);
}
//#endregion
//#region src/nodes/parameter.ts
/**
* Definition for the {@link ParameterNode}. `required` defaults to `false`. When a `dialect` is
* passed to `create`, the schema's `optional`/`nullish` flags are derived through its
* `optionality`; without one, the schema is left as-is.
*/
const parameterDef = defineNode({
kind: "Parameter",
build: (props, dialect) => {
const required = props.required ?? false;
return {
...props,
required,
schema: dialect ? dialect.schema.optionality(props.schema, required) : props.schema
};
},
children: ["schema"],
visitorKey: "parameter"
});
/**
* Creates a `ParameterNode`.
*
* @example
* ```ts
* const param = createParameter({
* name: 'petId',
* in: 'path',
* required: true,
* schema: createSchema({ type: 'string' }),
* })
* ```
*/
const createParameter = parameterDef.create;
//#endregion
//#region src/nodes/property.ts
/**
* Definition for the {@link PropertyNode}. `required` defaults to `false`. When a `dialect` is
* passed to `create`, the schema's `optional`/`nullish` flags are derived through its
* `optionality`; without one, the schema is left as-is.
*/
const propertyDef = defineNode({
kind: "Property",
build: (props, dialect) => {
const required = props.required ?? false;
return {
...props,
required,
schema: dialect ? dialect.schema.optionality(props.schema, required) : props.schema
};
},
children: ["schema"],
visitorKey: "property"
});
/**
* Creates a `PropertyNode`.
*
* @example
* ```ts
* const property = createProperty({
* name: 'status',
* required: true,
* schema: createSchema({ type: 'string', nullable: true }),
* })
* // required=true, no optional/nullish
* ```
*/
const createProperty = propertyDef.create;
//#endregion
//#region src/nodes/response.ts
/**
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
*/
const responseDef = defineNode({
kind: "Response",
build: (props) => {
const { schema, mediaType, keysToOmit, content, ...rest } = props;
const entries = content ?? (schema ? [createContent({
contentType: mediaType ?? "application/json",
schema,
keysToOmit: keysToOmit ?? null
})] : void 0);
return {
...rest,
content: entries
};
},
children: ["content"],
visitorKey: "response"
});
/**
* Creates a `ResponseNode`.
*
* @example
* ```ts
* const response = createResponse({
* statusCode: '200',
* content: [createContent({ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) })],
* })
* ```
*/
const createResponse = responseDef.create;
//#endregion
export { schemaDef as $, fileDef as A, createConst as B, objectBindingPatternDef as C, createImport as D, createFile as E, arrowFunctionDef as F, functionDef as G, createJsx as H, breakDef as I, typeDef as J, jsxDef as K, constDef as L, sourceDef as M, contentDef as N, createSource as O, createContent as P, createSchema as Q, createArrowFunction as R, indexedAccessTypeDef as S, createExport as T, createText as U, createFunction as V, createType as W, camelCase as X, extractStringsFromNodes as Y, pascalCase as Z, createIndexedAccessType as _, createParameter as a, functionParameterDef as b, outputDef as c, createRequestBody as d, defineNode as et, requestBodyDef as f, createFunctionParameters as g, createFunctionParameter as h, propertyDef as i, importDef as j, exportDef as k, createOperation as l, inputDef as m, responseDef as n, parameterDef as o, createInput as p, textDef as q, createProperty as r, createOutput as s, createResponse as t, visitorKeys as tt, operationDef as u, createObjectBindingPattern as v, typeLiteralDef as w, functionParametersDef as x, createTypeLiteral as y, createBreak as z };
//# sourceMappingURL=response-Rd1uisM1.js.map

Sorry, the diff of this file is too big to display

import { n as __name } from "./chunk-CNktS9qV.js";
import { _t as SchemaNode, f as OperationNode, t as Node, vt as SchemaNodeByType, yt as SchemaType } from "./index-CTaN_ee_.js";
//#region src/dedupe.d.ts
/**
* A canonical destination for a deduplicated shape: the shared schema name and
* the synthetic `$ref` path that points at it.
*/
type DedupeCanonical = {
/**
* Canonical schema name every duplicate occurrence refers to.
*/
name: string;
/**
* `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).
*/
ref: string;
};
/**
* The result of {@link buildDedupePlan}: a lookup from structural signature to its
* canonical target, plus the freshly hoisted definitions that must be added to
* the schema list.
*/
type DedupePlan = {
/**
* Maps a structural signature to the canonical schema that represents it.
*/
canonicalBySignature: Map<string, DedupeCanonical>;
/**
* Maps the name of a top-level schema that duplicates a canonical one to that canonical, so
* references to the duplicate can be repointed at the first schema with the same content.
*/
aliasNames: Map<string, DedupeCanonical>;
/**
* New top-level schema definitions created for inline shapes that had no existing
* named component. Nested duplicates inside each definition are already collapsed.
*/
hoisted: Array<SchemaNode>;
};
/**
* The lookups {@link applyDedupe} needs from a {@link DedupePlan}.
*/
type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>;
/**
* Options that inject the naming and candidate policy into {@link buildDedupePlan}.
* The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.
*/
type BuildDedupePlanOptions = {
/**
* Returns `true` when a node should be deduplicated. This is the only gate, so it must
* reject both ineligible kinds (return `false` for anything other than, say, enums and
* objects) and unsafe shapes (e.g. nodes that reference a circular schema).
*/
isCandidate: (node: SchemaNode) => boolean;
/**
* Produces the canonical name for an inline shape with no existing named component.
* Return `null` to leave the shape inline (for example when no contextual name exists).
*/
nameFor: (node: SchemaNode, signature: string) => string | null;
/**
* Builds the `$ref` path for a canonical name.
*/
refFor: (name: string) => string;
/**
* Minimum number of occurrences before a shape is deduplicated.
*
* @default 2
*/
minOccurrences?: number;
};
/**
* Rewrites a node, replacing every candidate sub-schema whose signature has a canonical
* target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,
* so nested duplicates inside a replaced shape are not visited again. A `ref` that points
* at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with
* the same content.
*
* Pass `skipRootMatch` when rewriting a canonical definition so its own root is not
* turned into a reference to itself. Nested duplicates are still collapsed.
*
* @example
* ```ts
* const next = applyDedupe(operationNode, plan)
* ```
*/
declare function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode;
declare function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode;
/**
* Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
*
* A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
* is a named top-level schema, the first one becomes the canonical (so other top-level
* duplicates and inline copies turn into references to it). Every other top-level name with
* the same content is recorded in `aliasNames`, so refs to it can be repointed at the
* canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied
* per node with {@link applyDedupe}.
*
* @example
* ```ts
* const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
* isCandidate: (node) => node.type === 'enum' || node.type === 'object',
* nameFor: (node) => node.name ?? null,
* refFor: (name) => `#/components/schemas/${name}`,
* })
* ```
*/
declare function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan;
//#endregion
//#region src/definePrinter.d.ts
/**
* Runtime context passed as `this` to printer handlers.
*
* `this.transform` dispatches to node-level handlers from `nodes`.
*
* @example
* ```ts
* const context: PrinterHandlerContext<string, {}> = {
* options: {},
* transform: () => 'value',
* }
* ```
*/
type PrinterHandlerContext<TOutput, TOptions extends object> = {
/**
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
*/
transform: (node: SchemaNode) => TOutput | null;
/**
* Options for this printer instance.
*/
options: TOptions;
};
/**
* Handler for one schema node type.
*
* Use a regular function (not an arrow function) if you need `this`.
*
* @example
* ```ts
* const handler: PrinterHandler<string, {}, 'string'> = function () {
* return 'string'
* }
* ```
*/
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
/**
* Partial map of per-node-type handler overrides for a printer.
*
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
* Supply only the handlers you want to replace. The printer's built-in
* defaults fill in the rest.
*
* @example
* ```ts
* pluginZod({
* printer: {
* nodes: {
* date(): string {
* return 'z.string().date()'
* },
* } satisfies PrinterPartial<string, PrinterZodOptions>,
* },
* })
* ```
*/
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
/**
* Generic shape used by `definePrinter`.
*
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
* - `TOptions` options passed to and stored on the printer instance
* - `TOutput` the type emitted by node handlers
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
*
* @example
* ```ts
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
* ```
*/
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
name: TName;
options: TOptions;
output: TOutput;
printOutput: TPrintOutput;
};
/**
* Printer instance returned by a printer factory.
*
* @example
* ```ts
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
* ```
*/
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
/**
* Unique identifier supplied at creation time.
*/
name: T['name'];
/**
* Options for this printer instance.
*/
options: T['options'];
/**
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
* Always dispatches through the `nodes` map. Never calls the `print` override.
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
*/
transform: (node: SchemaNode) => T['output'] | null;
/**
* Public printer. If the builder provides a root-level `print`, this calls that
* higher-level function (which may produce full declarations).
* Otherwise, falls back to the node-level dispatcher.
*/
print: (node: SchemaNode) => T['printOutput'] | null;
};
/**
* Builder function passed to `definePrinter`.
*
* It receives resolved options and returns:
* - `name`
* - `options`
* - `nodes` handlers
* - optional top-level `print` override
*
* @example
* ```ts
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
* ```
*/
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
name: T['name'];
/**
* Options to store on the printer.
*/
options: T['options'];
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
/**
* Optional root-level print override. When provided, becomes the public `printer.print`.
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
* not the override itself, so recursion is safe.
*/
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
};
/**
* Defines a schema printer: a function that takes a `SchemaNode` and emits
* code in your target language. Each plugin that produces code from schemas
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
* with this helper.
*
* The builder receives resolved options and returns:
*
* - `name` unique identifier for the printer.
* - `options` stored on the returned printer instance.
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
* output (a string, a TypeScript AST node, ...) for that schema type.
* - `print` (optional), top-level override exposed as `printer.print`.
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
*
* Without a `print` override, `printer.print` falls back to `printer.transform`
* (the node-level dispatcher).
*
* @example Tiny Zod printer
* ```ts
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
*
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
*
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
* name: 'zod',
* options: { strict: options.strict ?? true },
* nodes: {
* string: () => 'z.string()',
* object(node) {
* const props = node.properties
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
* .join(', ')
* return `z.object({ ${props} })`
* },
* },
* }))
* ```
*/
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
/**
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
*
* @example
* ```ts
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
* (node) => node.kind,
* )
* ```
*/
declare function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null): <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
name: T["name"];
options: T["options"];
nodes: Partial<{ [K in TKey]: (this: {
transform: (node: TNode) => T["output"] | null;
options: T["options"];
}, node: TNodeByKey[K]) => T["output"] | null }>;
print?: (this: {
transform: (node: TNode) => T["output"] | null;
options: T["options"];
}, node: TNode) => T["printOutput"] | null;
}) => (options?: T["options"]) => {
name: T["name"];
options: T["options"];
transform: (node: TNode) => T["output"] | null;
print: (node: TNode) => T["printOutput"] | null;
};
//#endregion
export { definePrinter as a, DedupePlan as c, createPrinterFactory as i, applyDedupe as l, PrinterFactoryOptions as n, DedupeCanonical as o, PrinterPartial as r, DedupeLookups as s, Printer as t, buildDedupePlan as u };
//# sourceMappingURL=types-DQTgVSwE.d.ts.map
const require_response = require("./response-BeG6hq2D.cjs");
const require_visitor = require("./visitor-B7Zlnp4c.cjs");
//#region ../../internals/utils/src/promise.ts
/**
* Wraps `factory` with a keyed cache backed by the provided store.
*
* Pass a `WeakMap` for object keys (results are GC-eligible when the key is
* collected) or a `Map` for primitive keys. For multi-argument functions,
* nest two `memoize` calls — the outer keyed by the first argument, the
* inner (created once per outer miss) keyed by the second.
*
* Because the cache is owned by the caller, it can be shared, inspected, or
* cleared independently of the memoized function.
*
* @example Single WeakMap key
* ```ts
* const cache = new WeakMap<SchemaNode, Set<string>>()
* const getRefs = memoize(cache, (node) => collectRefs(node))
* ```
*
* @example Single Map key (primitive)
* ```ts
* const cache = new Map<string, Resolver>()
* const getResolver = memoize(cache, (name) => buildResolver(name))
* ```
*
* @example Two-level (object + primitive)
* ```ts
* const outer = new WeakMap<Params[], Map<string, Params[]>>()
* const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
* fn(params)('camelcase')
* ```
*/
function memoize(store, factory) {
return (key) => {
if (store.has(key)) return store.get(key);
const value = factory(key);
store.set(key, value);
return value;
};
}
//#endregion
//#region ../../internals/utils/src/reserved.ts
/**
* JavaScript and Java reserved words.
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
*/
const reservedWords = new Set([
"abstract",
"arguments",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"double",
"else",
"enum",
"eval",
"export",
"extends",
"false",
"final",
"finally",
"float",
"for",
"function",
"goto",
"if",
"implements",
"import",
"in",
"instanceof",
"int",
"interface",
"let",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"typeof",
"var",
"void",
"volatile",
"while",
"with",
"yield",
"Array",
"Date",
"hasOwnProperty",
"Infinity",
"isFinite",
"isNaN",
"isPrototypeOf",
"length",
"Math",
"name",
"NaN",
"Number",
"Object",
"prototype",
"String",
"toString",
"undefined",
"valueOf"
]);
/**
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
*
* @example
* ```ts
* isValidVarName('status') // true
* isValidVarName('class') // false (reserved word)
* isValidVarName('42foo') // false (starts with digit)
* ```
*/
function isValidVarName(name) {
if (!name || reservedWords.has(name)) return false;
return isIdentifier(name);
}
/**
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
*
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
* deciding whether an object key needs quoting.
*
* @example
* ```ts
* isIdentifier('name') // true
* isIdentifier('x-total')// false
* ```
*/
function isIdentifier(name) {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
}
//#endregion
//#region ../../internals/utils/src/string.ts
/**
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
* any backslash or single quote in the content.
*
* @example
* ```ts
* singleQuote('foo') // "'foo'"
* singleQuote("o'clock") // "'o\\'clock'"
* ```
*/
function singleQuote(value) {
if (value === void 0 || value === null) return "''";
return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
}
//#endregion
//#region src/utils/codegen.ts
/**
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no
* comments so callers always get a usable string.
*
* @example
* ```ts
* buildJSDoc(['@type string', '@example hello'])
* // '/**\n * @type string\n * @example hello\n *\/\n '
* ```
*/
function buildJSDoc(comments, options = {}) {
const { indent = " * ", suffix = "\n ", fallback = " " } = options;
if (comments.length === 0) return fallback;
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
}
/**
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
*/
function indentLines(text) {
if (!text) return "";
return text.split("\n").map((line) => line.trim() ? `${require_visitor.INDENT}${line}` : "").join("\n");
}
/**
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
*
* @example
* ```ts
* objectKey('name') // 'name'
* objectKey('x-total') // "'x-total'"
* ```
*/
function objectKey(name) {
return isIdentifier(name) ? name : singleQuote(name);
}
/**
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
* level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
* so callers never re-parse the generated code. A trailing comma is added per entry to match the
* formatter's multi-line style.
*
* @example
* ```ts
* buildObject(['id: z.number()', 'name: z.string()'])
* // '{\n id: z.number(),\n name: z.string(),\n}'
* ```
*/
function buildObject(entries) {
if (entries.length === 0) return "{}";
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
}
/**
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
* one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
* `z.array([…])`, and similar member lists so objects inside them nest correctly.
*
* @example
* ```ts
* buildList(['z.string()', 'z.number()'])
* // '[z.string(), z.number()]'
* ```
*/
function buildList(items, brackets = ["[", "]"]) {
const [open, close] = brackets;
if (items.length === 0) return `${open}${close}`;
if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
}
//#endregion
//#region src/utils/schemaMerge.ts
/**
* Merges a run of adjacent anonymous object members into one. Named or non-object members break the
* run and pass through. Stays a construction-time helper, not a macro, so callers keep control of the
* member boundaries (such as keeping synthetic discriminant objects out of a run).
*
* @example
* ```ts
* const merged = [...mergeAdjacentObjectsLazy([objectA, objectB])]
* ```
*/
function* mergeAdjacentObjectsLazy(members) {
let acc;
for (const member of members) {
const objectMember = require_visitor.narrowSchema(member, "object");
if (objectMember && !objectMember.name && acc !== void 0) {
const accObject = require_visitor.narrowSchema(acc, "object");
if (accObject && !accObject.name) {
acc = require_response.createSchema({
...accObject,
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
});
continue;
}
}
if (acc !== void 0) yield acc;
acc = member;
}
if (acc !== void 0) yield acc;
}
//#endregion
//#region src/utils/strings.ts
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
function trimQuotes(text) {
if (text.length >= 2) {
const first = text[0];
const last = text[text.length - 1];
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
}
return text;
}
/**
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
*
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
* code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
function stringify(value) {
if (value === void 0 || value === null) return "''";
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
}
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
function jsStringEscape(input) {
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
switch (character) {
case "\"":
case "'":
case "\\": return `\\${character}`;
case "\n": return "\\n";
case "\r": return "\\r";
case "\u2028": return "\\u2028";
case "\u2029": return "\\u2029";
default: return "";
}
});
}
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
function toRegExpString(text, func = "RegExp") {
const raw = trimQuotes(text);
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
const replacementTarget = match?.[1] ?? "";
const matchedFlags = match?.[2];
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
const { source, flags } = new RegExp(cleaned, matchedFlags);
if (func === null) return `/${source}/${flags}`;
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
}
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
function stringifyObject(value) {
return Object.entries(value).map(([key, val]) => {
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
return `${key}: ${val}`;
}).filter(Boolean).join(",\n");
}
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
function getNestedAccessor(param, accessor) {
const parts = Array.isArray(param) ? param : param.split(".");
if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
}
//#endregion
//#region src/utils/schemaGraph.ts
/**
* Collects every named schema referenced transitively from a node through its ref edges.
*
* Refs are followed by name only, so the resolved `node.schema` is never traversed inline.
*
* @example Collect refs from a single schema
* ```ts
* const names = collectReferencedSchemaNames(petSchema)
* // → Set { 'Category', 'Tag' }
* ```
*
* @example Accumulate refs from multiple schemas into one set
* ```ts
* const out = new Set<string>()
* for (const schema of schemas) {
* collectReferencedSchemaNames(schema, out)
* }
* ```
*/
const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
const refs = /* @__PURE__ */ new Set();
require_visitor.collect(node, { schema(child) {
if (child.type === "ref") {
const name = require_visitor.resolveRefName(child);
if (name) refs.add(name);
}
} });
return refs;
});
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
if (!node) return out;
for (const name of collectSchemaRefs(node)) out.add(name);
return out;
}
/**
* Collects the names of all top-level schemas transitively used by a set of operations.
*
* An operation uses a schema when its parameters, request body, or responses reference it, directly
* or through other named schemas. The walk is iterative, so reference cycles are safe.
*
* Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.
*
* @example Only generate schemas referenced by included operations
* ```ts
* const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)
* const allowed = collectUsedSchemaNames(includedOps, schemas)
*
* for (const schema of schemas) {
* if (schema.name && !allowed.has(schema.name)) continue
* // … generate schema
* }
* ```
*/
const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
function computeUsedSchemaNames(operations, schemas) {
const schemaMap = /* @__PURE__ */ new Map();
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
const result = /* @__PURE__ */ new Set();
function visitSchema(schema) {
const directRefs = collectReferencedSchemaNames(schema);
for (const name of directRefs) if (!result.has(name)) {
result.add(name);
const namedSchema = schemaMap.get(name);
if (namedSchema) visitSchema(namedSchema);
}
}
for (const op of operations) for (const schema of require_visitor.collectLazy(op, {
depth: "shallow",
schema: (node) => node
})) visitSchema(schema);
return result;
}
function collectUsedSchemaNames(operations, schemas) {
return collectUsedSchemaNamesMemo(operations)(schemas);
}
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
const graph = /* @__PURE__ */ new Map();
for (const schema of schemas) {
if (!schema.name) continue;
graph.set(schema.name, collectReferencedSchemaNames(schema));
}
const circular = /* @__PURE__ */ new Set();
for (const start of graph.keys()) {
const visited = /* @__PURE__ */ new Set();
const stack = [...graph.get(start) ?? []];
while (stack.length > 0) {
const node = stack.pop();
if (node === start) {
circular.add(start);
break;
}
if (visited.has(node)) continue;
visited.add(node);
const next = graph.get(node);
if (next) for (const r of next) stack.push(r);
}
}
return circular;
});
/**
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
*
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
* the generated code does not recurse forever. Refs are followed by name only, so the walk stays
* linear in the size of the schema graph.
*
* @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.
*/
function findCircularSchemas(schemas) {
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
return findCircularSchemasMemo(schemas);
}
/**
* Returns `true` when a schema, or anything nested inside it, references a circular schema.
*
* Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled
* on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.
*
* @note Stops at the first matching circular ref.
*/
function containsCircularRef(node, { circularSchemas, excludeName }) {
if (!node || circularSchemas.size === 0) return false;
for (const _ of require_visitor.collectLazy(node, { schema(child) {
if (child.type !== "ref") return null;
const name = require_visitor.resolveRefName(child);
return name && name !== excludeName && circularSchemas.has(name) ? true : null;
} })) return true;
return false;
}
//#endregion
//#region src/utils/schemaTraversal.ts
/**
* Maps each property of an object schema to its transformed output. Pairs every result with the
* original property so the printer keeps full control over modifiers, getters, and key syntax.
*
* @example
* ```ts
* const entries = mapSchemaProperties(node, (schema) => this.transform(schema))
* // entries: [{ name: 'id', property, output: 'z.number()' }, ...]
* ```
*/
function mapSchemaProperties(node, transform) {
return node.properties.map((property) => ({
name: property.name,
property,
output: transform(property.schema)
}));
}
/**
* Maps each member of a union or intersection schema to its transformed output, pairing every
* result with the original member.
*/
function mapSchemaMembers(node, transform) {
return (node.members ?? []).map((schema) => ({
schema,
output: transform(schema)
}));
}
/**
* Maps each item of an array or tuple schema to its transformed output, pairing every result with
* the original item.
*/
function mapSchemaItems(node, transform) {
return (node.items ?? []).map((schema) => ({
schema,
output: transform(schema)
}));
}
/**
* Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key
* is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation
* of a recursive schema until first access.
*
* @example
* ```ts
* lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })
* // "get parent() { return z.lazy(() => Pet) }"
* ```
*/
function lazyGetter({ name, body }) {
return `get ${objectKey(name)}() { return ${body} }`;
}
//#endregion
//#region src/utils/operationParams.ts
/**
* Applies casing rules to parameter names and returns a new array without mutating the input.
*
* Run it before handing parameters to schema builders so output property keys get the right casing
* while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the
* original array is returned unchanged.
*/
const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => {
const transformed = casing === "camelcase" || !isValidVarName(param.name) ? require_response.camelCase(param.name) : param.name;
return {
...param,
name: transformed
};
})));
function caseParams(params, casing) {
if (!casing) return params;
return caseParamsMemo(params)(casing);
}
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, it falls back to the schema primitive (a plain type-name string). When the
* parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like
* `GroupParams['petId']`, otherwise the resolved individual name.
*/
function resolveParamType({ node, param, resolver }) {
if (!resolver) return param.schema.primitive ?? "unknown";
const individualName = resolver.resolveParamName(node, param);
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
const groupResolvers = {
path: resolver.resolvePathParamsName,
query: resolver.resolveQueryParamsName,
header: resolver.resolveHeaderParamsName
};
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
if (groupName && groupName !== individualName) return require_response.createIndexedAccessType({
target: groupName,
key: param.name
});
return individualName;
}
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
function createOperationParams(node, options) {
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options;
const dataName = paramNames?.data ?? "data";
const paramsName = paramNames?.params ?? "params";
const headersName = paramNames?.headers ?? "headers";
const pathName = paramNames?.path ?? "pathParams";
const wrapType = (type) => typeWrapper ? typeWrapper(type) : type;
const wrapTypeExpression = (type) => typeof type === "string" ? wrapType(type) : type;
const casedParams = caseParams(node.parameters, paramsCasing);
const pathParams = casedParams.filter((p) => p.in === "path");
const queryParams = casedParams.filter((p) => p.in === "query");
const headerParams = casedParams.filter((p) => p.in === "header");
const toProperty = (param) => ({
name: param.name,
type: wrapTypeExpression(resolveParamType({
node,
param,
resolver
})),
optional: !param.required
});
const emptyObjectDefault = (props) => props.every((p) => p.optional) ? "{}" : void 0;
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
const bodyProperty = bodyType ? [{
name: dataName,
type: bodyType,
optional: !(node.requestBody?.required ?? false)
}] : [];
const trailingGroups = [{
name: paramsName,
node,
params: queryParams,
groupType: require_visitor.resolveGroupType({
node,
params: queryParams,
group: "query",
resolver
}),
resolver,
wrapType
}, {
name: headersName,
node,
params: headerParams,
groupType: require_visitor.resolveGroupType({
node,
params: headerParams,
group: "header",
resolver
}),
resolver,
wrapType
}];
const params = [];
if (paramsType === "object") {
const children = [
...pathParams.map(toProperty),
...bodyProperty,
...trailingGroups.flatMap(buildGroupProperty)
];
if (children.length) params.push(require_response.createFunctionParameter({
properties: children,
default: emptyObjectDefault(children)
}));
} else {
if (pathParamsType === "inlineSpread" && pathParams.length) {
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]);
params.push(require_response.createFunctionParameter({
name: pathName,
type: spreadType ? wrapType(spreadType) : void 0,
rest: true
}));
} else if (pathParamsType === "inline") params.push(...pathParams.map((p) => require_response.createFunctionParameter(toProperty(p))));
else if (pathParams.length) {
const pathChildren = pathParams.map(toProperty);
params.push(require_response.createFunctionParameter({
properties: pathChildren,
default: pathParamsDefault ?? emptyObjectDefault(pathChildren)
}));
}
params.push(...bodyProperty.map((p) => require_response.createFunctionParameter(p)));
params.push(...trailingGroups.flatMap(buildGroupParam));
}
params.push(...extraParams);
return require_response.createFunctionParameters({ params });
}
/**
* Builds the property descriptor for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }) {
if (groupType) return [{
name,
type: typeof groupType.type === "string" ? wrapType(groupType.type) : groupType.type,
optional: groupType.optional
}];
if (params.length) return [{
name,
type: buildTypeLiteral({
node,
params,
resolver
}),
optional: params.every((p) => !p.required)
}];
return [];
}
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupParam(args) {
return buildGroupProperty(args).map((p) => require_response.createFunctionParameter(p));
}
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
function buildTypeLiteral({ node, params, resolver }) {
return require_response.createTypeLiteral({ members: params.map((p) => ({
name: p.name,
type: resolveParamType({
node,
param: p,
resolver
}),
optional: !p.required
})) });
}
//#endregion
Object.defineProperty(exports, "buildGroupParam", {
enumerable: true,
get: function() {
return buildGroupParam;
}
});
Object.defineProperty(exports, "buildJSDoc", {
enumerable: true,
get: function() {
return buildJSDoc;
}
});
Object.defineProperty(exports, "buildList", {
enumerable: true,
get: function() {
return buildList;
}
});
Object.defineProperty(exports, "buildObject", {
enumerable: true,
get: function() {
return buildObject;
}
});
Object.defineProperty(exports, "buildTypeLiteral", {
enumerable: true,
get: function() {
return buildTypeLiteral;
}
});
Object.defineProperty(exports, "caseParams", {
enumerable: true,
get: function() {
return caseParams;
}
});
Object.defineProperty(exports, "collectUsedSchemaNames", {
enumerable: true,
get: function() {
return collectUsedSchemaNames;
}
});
Object.defineProperty(exports, "containsCircularRef", {
enumerable: true,
get: function() {
return containsCircularRef;
}
});
Object.defineProperty(exports, "createOperationParams", {
enumerable: true,
get: function() {
return createOperationParams;
}
});
Object.defineProperty(exports, "findCircularSchemas", {
enumerable: true,
get: function() {
return findCircularSchemas;
}
});
Object.defineProperty(exports, "getNestedAccessor", {
enumerable: true,
get: function() {
return getNestedAccessor;
}
});
Object.defineProperty(exports, "isValidVarName", {
enumerable: true,
get: function() {
return isValidVarName;
}
});
Object.defineProperty(exports, "jsStringEscape", {
enumerable: true,
get: function() {
return jsStringEscape;
}
});
Object.defineProperty(exports, "lazyGetter", {
enumerable: true,
get: function() {
return lazyGetter;
}
});
Object.defineProperty(exports, "mapSchemaItems", {
enumerable: true,
get: function() {
return mapSchemaItems;
}
});
Object.defineProperty(exports, "mapSchemaMembers", {
enumerable: true,
get: function() {
return mapSchemaMembers;
}
});
Object.defineProperty(exports, "mapSchemaProperties", {
enumerable: true,
get: function() {
return mapSchemaProperties;
}
});
Object.defineProperty(exports, "mergeAdjacentObjectsLazy", {
enumerable: true,
get: function() {
return mergeAdjacentObjectsLazy;
}
});
Object.defineProperty(exports, "objectKey", {
enumerable: true,
get: function() {
return objectKey;
}
});
Object.defineProperty(exports, "resolveParamType", {
enumerable: true,
get: function() {
return resolveParamType;
}
});
Object.defineProperty(exports, "stringify", {
enumerable: true,
get: function() {
return stringify;
}
});
Object.defineProperty(exports, "stringifyObject", {
enumerable: true,
get: function() {
return stringifyObject;
}
});
Object.defineProperty(exports, "toRegExpString", {
enumerable: true,
get: function() {
return toRegExpString;
}
});
Object.defineProperty(exports, "trimQuotes", {
enumerable: true,
get: function() {
return trimQuotes;
}
});
//# sourceMappingURL=utils-BDI3kXlb.cjs.map
{"version":3,"file":"utils-BDI3kXlb.cjs","names":["INDENT","narrowSchema","createSchema","resolveRefName","collectLazy","camelCase","createIndexedAccessType","resolveGroupType","createFunctionParameter","createFunctionParameters","createTypeLiteral"],"sources":["../../../internals/utils/src/promise.ts","../../../internals/utils/src/reserved.ts","../../../internals/utils/src/string.ts","../src/utils/codegen.ts","../src/utils/schemaMerge.ts","../src/utils/strings.ts","../src/utils/schemaGraph.ts","../src/utils/schemaTraversal.ts","../src/utils/operationParams.ts"],"sourcesContent":["function* chunks<T>(arr: ReadonlyArray<T>, size: number): Generator<Array<T>> {\n for (let i = 0; i < arr.length; i += size) {\n yield arr.slice(i, i + size)\n }\n}\n\nexport type ForBatchesOptions = {\n /**\n * Maximum batch size handed to `process`.\n * Parallel dispatch within a batch is the caller's responsibility\n * (typically via `Promise.all(batch.map(...))`).\n */\n concurrency: number\n /**\n * Called after every batch.\n *\n * Use a cheap, idempotent callback (e.g. one that short-circuits when there\n * is nothing new to do). The helper does not coalesce calls — if you need\n * throttling, do it inside `flush` itself.\n */\n flush?: () => Promise<void>\n}\n\n/**\n * Slices `source` into batches of `concurrency` items and awaits `process` for each batch.\n * Accepts both plain arrays (sync) and `AsyncIterable` (streaming).\n *\n * `process` controls whether items inside a batch run in parallel; this helper only\n * controls batch size and per-batch flushing.\n *\n * @example\n * ```ts\n * // parallel dispatch inside each batch\n * await forBatches(schemas, (batch) => Promise.all(batch.map(process)), { concurrency: 8 })\n *\n * // async iterable with a flush after every batch\n * await forBatches(stream.schemas, (batch) => dispatch(batch), { concurrency: 8, flush })\n * ```\n */\nexport async function forBatches<T>(\n source: ReadonlyArray<T> | AsyncIterable<T>,\n process: (batch: Array<T>) => Promise<unknown>,\n options: ForBatchesOptions,\n): Promise<void> {\n const { concurrency, flush } = options\n\n if (Array.isArray(source)) {\n for (const batch of chunks(source, concurrency)) {\n await process(batch)\n if (flush) await flush()\n }\n return\n }\n\n const batch: Array<T> = []\n for await (const item of source) {\n batch.push(item)\n if (batch.length >= concurrency) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n }\n if (batch.length > 0) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n}\n\n/** A value that may already be resolved or still pending.\n *\n * @example\n * ```ts\n * function load(id: string): PossiblePromise<string> {\n * return cache.get(id) ?? fetchRemote(id)\n * }\n * ```\n */\nexport type PossiblePromise<T> = Promise<T> | T\n\n/** Returns `true` when `result` is a thenable `Promise`.\n *\n * @example\n * ```ts\n * isPromise(Promise.resolve(1)) // true\n * isPromise(42) // false\n * ```\n */\nexport function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {\n return result !== null && result !== undefined && typeof (result as Record<string, unknown>)['then'] === 'function'\n}\n\n/** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)\n * ```\n */\nexport function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & { reason: T } {\n return result.status === 'rejected'\n}\n\ntype Store<TKey, TValue> = {\n has(key: TKey): boolean\n get(key: TKey): TValue | undefined\n set(key: TKey, value: TValue): unknown\n}\n\n/**\n * Wraps `factory` with a keyed cache backed by the provided store.\n *\n * Pass a `WeakMap` for object keys (results are GC-eligible when the key is\n * collected) or a `Map` for primitive keys. For multi-argument functions,\n * nest two `memoize` calls — the outer keyed by the first argument, the\n * inner (created once per outer miss) keyed by the second.\n *\n * Because the cache is owned by the caller, it can be shared, inspected, or\n * cleared independently of the memoized function.\n *\n * @example Single WeakMap key\n * ```ts\n * const cache = new WeakMap<SchemaNode, Set<string>>()\n * const getRefs = memoize(cache, (node) => collectRefs(node))\n * ```\n *\n * @example Single Map key (primitive)\n * ```ts\n * const cache = new Map<string, Resolver>()\n * const getResolver = memoize(cache, (name) => buildResolver(name))\n * ```\n *\n * @example Two-level (object + primitive)\n * ```ts\n * const outer = new WeakMap<Params[], Map<string, Params[]>>()\n * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))\n * fn(params)('camelcase')\n * ```\n */\nexport function memoize<TKey, TValue>(store: Store<TKey, TValue>, factory: (key: TKey) => TValue): (key: TKey) => TValue {\n return (key: TKey): TValue => {\n if (store.has(key)) return store.get(key)!\n const value = factory(key)\n store.set(key, value)\n return value\n }\n}\n\n/**\n * Container that switches between an eager `Array<T>` and a lazy `AsyncIterable<T>`.\n *\n * `Array<T>` by default. With `Stream` set to `true` it becomes `AsyncIterable<T>`, so large\n * collections can be produced lazily without holding every item in memory. Pairs with\n * {@link arrayToAsyncIterable}, which lifts a plain array into the streaming form.\n *\n * @example\n * ```ts\n * type Eager = Streamable<number> // Array<number>\n * type Lazy = Streamable<number, true> // AsyncIterable<number>\n * ```\n */\nexport type Streamable<T, Stream extends boolean = false> = Stream extends true ? AsyncIterable<T> : Array<T>\n\n/**\n * Wraps a plain array in a reusable `AsyncIterable`.\n * Each `[Symbol.asyncIterator]()` call returns a fresh generator so the\n * iterable can be consumed multiple times (e.g. once per plugin pre-scan).\n *\n * @example\n * ```ts\n * const stream = arrayToAsyncIterable([1, 2, 3])\n * for await (const n of stream) console.log(n) // 1, 2, 3\n * ```\n */\nexport function arrayToAsyncIterable<T>(arr: ReadonlyArray<T>): AsyncIterable<T> {\n return {\n [Symbol.asyncIterator]() {\n return (async function* () {\n yield* arr\n })()\n },\n }\n}\n","/**\n * JavaScript and Java reserved words.\n * @link https://github.com/jonschlinkert/reserved/blob/master/index.js\n */\nconst reservedWords = new Set([\n 'abstract',\n 'arguments',\n 'boolean',\n 'break',\n 'byte',\n 'case',\n 'catch',\n 'char',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'double',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'final',\n 'finally',\n 'float',\n 'for',\n 'function',\n 'goto',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'int',\n 'interface',\n 'let',\n 'long',\n 'native',\n 'new',\n 'null',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'short',\n 'static',\n 'super',\n 'switch',\n 'synchronized',\n 'this',\n 'throw',\n 'throws',\n 'transient',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'volatile',\n 'while',\n 'with',\n 'yield',\n 'Array',\n 'Date',\n 'hasOwnProperty',\n 'Infinity',\n 'isFinite',\n 'isNaN',\n 'isPrototypeOf',\n 'length',\n 'Math',\n 'name',\n 'NaN',\n 'Number',\n 'Object',\n 'prototype',\n 'String',\n 'toString',\n 'undefined',\n 'valueOf',\n] as const)\n\n/**\n * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.\n *\n * @example\n * ```ts\n * transformReservedWord('class') // '_class'\n * transformReservedWord('42foo') // '_42foo'\n * transformReservedWord('status') // 'status'\n * ```\n */\nexport function transformReservedWord(word: string): string {\n const firstChar = word.charCodeAt(0)\n if (word && (reservedWords.has(word as 'valueOf') || (firstChar >= 48 && firstChar <= 57))) {\n return `_${word}`\n }\n return word\n}\n\n/**\n * Returns `true` when `name` is a syntactically valid JavaScript variable name.\n *\n * @example\n * ```ts\n * isValidVarName('status') // true\n * isValidVarName('class') // false (reserved word)\n * isValidVarName('42foo') // false (starts with digit)\n * ```\n */\nexport function isValidVarName(name: string): boolean {\n if (!name || reservedWords.has(name as 'valueOf')) {\n return false\n }\n return isIdentifier(name)\n}\n\n/**\n * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.\n *\n * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys\n * even though they are not valid variable names, so use this (not {@link isValidVarName}) when\n * deciding whether an object key needs quoting.\n *\n * @example\n * ```ts\n * isIdentifier('name') // true\n * isIdentifier('x-total')// false\n * ```\n */\nexport function isIdentifier(name: string): boolean {\n return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)\n}\n","/**\n * Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping\n * any backslash or single quote in the content.\n *\n * @example\n * ```ts\n * singleQuote('foo') // \"'foo'\"\n * singleQuote(\"o'clock\") // \"'o\\\\'clock'\"\n * ```\n */\nexport function singleQuote(value: string | number | boolean | undefined | null): string {\n if (value === undefined || value === null) return \"''\"\n const escaped = String(value).replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")\n\n return `'${escaped}'`\n}\n","import { isIdentifier, singleQuote } from '@internals/utils'\nimport { INDENT } from '../constants.ts'\n\n/**\n * Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no\n * comments so callers always get a usable string.\n *\n * @example\n * ```ts\n * buildJSDoc(['@type string', '@example hello'])\n * // '/**\\n * @type string\\n * @example hello\\n *\\/\\n '\n * ```\n */\nexport function buildJSDoc(\n comments: Array<string>,\n options: {\n /**\n * String used to indent each comment line.\n * @default ' * '\n */\n indent?: string\n /**\n * String appended after the closing tag.\n * @default '\\n '\n */\n suffix?: string\n /**\n * Returned as-is when `comments` is empty.\n * @default ' '\n */\n fallback?: string\n } = {},\n): string {\n const { indent = ' * ', suffix = '\\n ', fallback = ' ' } = options\n\n if (comments.length === 0) return fallback\n\n return `/**\\n${comments.map((c) => `${indent}${c}`).join('\\n')}\\n */${suffix}`\n}\n\n/**\n * Indents every non-empty line of `text` by one indent level, leaving blank lines empty.\n */\nfunction indentLines(text: string): string {\n if (!text) return ''\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${INDENT}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Renders an object key, quoting it with single quotes only when it is not a valid identifier.\n * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.\n *\n * @example\n * ```ts\n * objectKey('name') // 'name'\n * objectKey('x-total') // \"'x-total'\"\n * ```\n */\nexport function objectKey(name: string): string {\n return isIdentifier(name) ? name : singleQuote(name)\n}\n\n/**\n * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one\n * level and closing the brace at column zero. Nested objects built the same way indent cumulatively,\n * so callers never re-parse the generated code. A trailing comma is added per entry to match the\n * formatter's multi-line style.\n *\n * @example\n * ```ts\n * buildObject(['id: z.number()', 'name: z.string()'])\n * // '{\\n id: z.number(),\\n name: z.string(),\\n}'\n * ```\n */\nexport function buildObject(entries: Array<string>): string {\n if (entries.length === 0) return '{}'\n const body = entries.map((entry) => `${indentLines(entry)},`).join('\\n')\n\n return `{\\n${body}\\n}`\n}\n\n/**\n * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on\n * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented\n * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,\n * `z.array([…])`, and similar member lists so objects inside them nest correctly.\n *\n * @example\n * ```ts\n * buildList(['z.string()', 'z.number()'])\n * // '[z.string(), z.number()]'\n * ```\n */\nexport function buildList(items: Array<string>, brackets: [open: string, close: string] = ['[', ']']): string {\n const [open, close] = brackets\n if (items.length === 0) return `${open}${close}`\n if (!items.some((item) => item.includes('\\n'))) return `${open}${items.join(', ')}${close}`\n const body = items.map((item) => `${indentLines(item)},`).join('\\n')\n\n return `${open}\\n${body}\\n${close}`\n}\n","import { narrowSchema } from '../guards.ts'\nimport { createSchema, type SchemaNode } from '../nodes/schema.ts'\n\n/**\n * Merges a run of adjacent anonymous object members into one. Named or non-object members break the\n * run and pass through. Stays a construction-time helper, not a macro, so callers keep control of the\n * member boundaries (such as keeping synthetic discriminant objects out of a run).\n *\n * @example\n * ```ts\n * const merged = [...mergeAdjacentObjectsLazy([objectA, objectB])]\n * ```\n */\nexport function* mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined> {\n let acc: SchemaNode | undefined\n\n for (const member of members) {\n const objectMember = narrowSchema(member, 'object')\n if (objectMember && !objectMember.name && acc !== undefined) {\n const accObject = narrowSchema(acc, 'object')\n if (accObject && !accObject.name) {\n acc = createSchema({\n ...accObject,\n properties: [...(accObject.properties ?? []), ...(objectMember.properties ?? [])],\n })\n continue\n }\n }\n if (acc !== undefined) yield acc\n acc = member\n }\n\n if (acc !== undefined) yield acc\n}\n","/**\n * Strips a single matching pair of `\"...\"`, `'...'`, or `` `...` `` from both ends of `text`.\n * Returns the string unchanged when no balanced quote pair is found.\n *\n * @example\n * ```ts\n * trimQuotes('\"hello\"') // 'hello'\n * trimQuotes('hello') // 'hello'\n * ```\n */\nexport function trimQuotes(text: string): string {\n if (text.length >= 2) {\n const first = text[0]\n const last = text[text.length - 1]\n if ((first === '\"' && last === '\"') || (first === \"'\" && last === \"'\") || (first === '`' && last === '`')) {\n return text.slice(1, -1)\n }\n }\n return text\n}\n\n/**\n * Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.\n *\n * Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated\n * code matches the repo style without a formatter.\n *\n * @example\n * ```ts\n * stringify('hello') // \"'hello'\"\n * stringify('\"hello\"') // \"'hello'\"\n * ```\n */\nexport function stringify(value: string | number | boolean | undefined): string {\n if (value === undefined || value === null) return \"''\"\n const json = JSON.stringify(trimQuotes(value.toString()))\n const inner = json.slice(1, -1).replace(/\\\\\"/g, '\"').replace(/'/g, \"\\\\'\")\n return `'${inner}'`\n}\n\n/**\n * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,\n * and the Unicode line terminators U+2028 and U+2029.\n *\n * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4\n *\n * @example\n * ```ts\n * jsStringEscape('say \"hi\"\\nbye') // 'say \\\\\"hi\\\\\"\\\\nbye'\n * ```\n */\nexport function jsStringEscape(input: unknown): string {\n return `${input}`.replace(/[\"'\\\\\\n\\r\\u2028\\u2029]/g, (character) => {\n switch (character) {\n case '\"':\n case \"'\":\n case '\\\\':\n return `\\\\${character}`\n case '\\n':\n return '\\\\n'\n case '\\r':\n return '\\\\r'\n case '\\u2028':\n return '\\\\u2028'\n case '\\u2029':\n return '\\\\u2029'\n default:\n return ''\n }\n })\n}\n\n/**\n * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.\n * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.\n * Pass `null` as the second argument to emit a `/pattern/flags` literal instead.\n *\n * @example\n * ```ts\n * toRegExpString('^(?im)foo') // 'new RegExp(\"^foo\", \"im\")'\n * toRegExpString('^(?im)foo', null) // '/^foo/im'\n * ```\n */\nexport function toRegExpString(text: string, func: string | null = 'RegExp'): string {\n const raw = trimQuotes(text)\n\n const match = raw.match(/^\\^(\\(\\?([igmsuy]+)\\))/i)\n const replacementTarget = match?.[1] ?? ''\n const matchedFlags = match?.[2]\n const cleaned = raw\n .replace(/^\\\\?\\//, '')\n .replace(/\\\\?\\/$/, '')\n .replace(replacementTarget, '')\n\n const { source, flags } = new RegExp(cleaned, matchedFlags)\n\n if (func === null) return `/${source}/${flags}`\n\n return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ''})`\n}\n\n/**\n * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested\n * objects recurse with fixed indentation, so the result drops straight into an object literal\n * without re-parsing.\n *\n * @example\n * ```ts\n * stringifyObject({ foo: 'bar', nested: { a: 1 } })\n * // 'foo: bar,\\nnested: {\\n a: 1\\n }'\n * ```\n */\nexport function stringifyObject(value: Record<string, unknown>): string {\n const items = Object.entries(value)\n .map(([key, val]) => {\n if (val !== null && typeof val === 'object') {\n return `${key}: {\\n ${stringifyObject(val as Record<string, unknown>)}\\n }`\n }\n return `${key}: ${val}`\n })\n .filter(Boolean)\n return items.join(',\\n')\n}\n\n/**\n * Renders a dotted path or string array as an optional-chaining accessor expression rooted at\n * `accessor`. Returns `null` for an empty path.\n *\n * @example\n * ```ts\n * getNestedAccessor('pagination.next.id', 'lastPage')\n * // \"lastPage?.['pagination']?.['next']?.['id']\"\n * ```\n */\nexport function getNestedAccessor(param: string | Array<string>, accessor: string): string | null {\n const parts = Array.isArray(param) ? param : param.split('.')\n if (parts.length === 0 || (parts.length === 1 && parts[0] === '')) return null\n return `${accessor}?.['${`${parts.join(\"']?.['\")}']`}`\n}\n","import { memoize } from '@internals/utils'\nimport type { OperationNode, SchemaNode } from '../nodes/index.ts'\nimport { collect, collectLazy } from '../visitor.ts'\nimport { resolveRefName } from './refs.ts'\n\n/**\n * Collects every named schema referenced transitively from a node through its ref edges.\n *\n * Refs are followed by name only, so the resolved `node.schema` is never traversed inline.\n *\n * @example Collect refs from a single schema\n * ```ts\n * const names = collectReferencedSchemaNames(petSchema)\n * // → Set { 'Category', 'Tag' }\n * ```\n *\n * @example Accumulate refs from multiple schemas into one set\n * ```ts\n * const out = new Set<string>()\n * for (const schema of schemas) {\n * collectReferencedSchemaNames(schema, out)\n * }\n * ```\n */\nconst collectSchemaRefs = memoize(new WeakMap<SchemaNode, ReadonlySet<string>>(), (node: SchemaNode): ReadonlySet<string> => {\n const refs = new Set<string>()\n collect<void>(node, {\n schema(child) {\n if (child.type === 'ref') {\n const name = resolveRefName(child)\n if (name) refs.add(name)\n }\n },\n })\n return refs\n})\n\nexport function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {\n if (!node) return out\n for (const name of collectSchemaRefs(node)) out.add(name)\n return out\n}\n\n/**\n * Collects the names of all top-level schemas transitively used by a set of operations.\n *\n * An operation uses a schema when its parameters, request body, or responses reference it, directly\n * or through other named schemas. The walk is iterative, so reference cycles are safe.\n *\n * Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.\n *\n * @example Only generate schemas referenced by included operations\n * ```ts\n * const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)\n * const allowed = collectUsedSchemaNames(includedOps, schemas)\n *\n * for (const schema of schemas) {\n * if (schema.name && !allowed.has(schema.name)) continue\n * // … generate schema\n * }\n * ```\n */\nconst collectUsedSchemaNamesMemo = memoize(new WeakMap<ReadonlyArray<OperationNode>, (schemas: ReadonlyArray<SchemaNode>) => Set<string>>(), (ops) =>\n memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas) => computeUsedSchemaNames(ops, schemas)),\n)\n\nfunction computeUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n const schemaMap = new Map<string, SchemaNode>()\n for (const schema of schemas) {\n if (schema.name) schemaMap.set(schema.name, schema)\n }\n\n const result = new Set<string>()\n\n function visitSchema(schema: SchemaNode): void {\n const directRefs = collectReferencedSchemaNames(schema)\n for (const name of directRefs) {\n if (!result.has(name)) {\n result.add(name)\n const namedSchema = schemaMap.get(name)\n if (namedSchema) visitSchema(namedSchema)\n }\n }\n }\n\n for (const op of operations) {\n for (const schema of collectLazy<SchemaNode>(op, { depth: 'shallow', schema: (node) => node })) {\n visitSchema(schema)\n }\n }\n\n return result\n}\n\nexport function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n return collectUsedSchemaNamesMemo(operations)(schemas)\n}\n\nconst EMPTY_CIRCULAR_SET = new Set<string>()\n\nconst findCircularSchemasMemo = memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas: ReadonlyArray<SchemaNode>): Set<string> => {\n const graph = new Map<string, Set<string>>()\n\n for (const schema of schemas) {\n if (!schema.name) continue\n graph.set(schema.name, collectReferencedSchemaNames(schema))\n }\n\n const circular = new Set<string>()\n for (const start of graph.keys()) {\n const visited = new Set<string>()\n const stack: Array<string> = [...(graph.get(start) ?? [])]\n while (stack.length > 0) {\n const node = stack.pop()!\n if (node === start) {\n circular.add(start)\n break\n }\n if (visited.has(node)) continue\n visited.add(node)\n\n const next = graph.get(node)\n if (next) for (const r of next) stack.push(r)\n }\n }\n\n return circular\n})\n\n/**\n * Finds every schema that takes part in a circular dependency chain, including direct self-loops.\n *\n * Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so\n * the generated code does not recurse forever. Refs are followed by name only, so the walk stays\n * linear in the size of the schema graph.\n *\n * @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.\n */\nexport function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string> {\n if (schemas.length === 0) return EMPTY_CIRCULAR_SET\n return findCircularSchemasMemo(schemas)\n}\n\n/**\n * Returns `true` when a schema, or anything nested inside it, references a circular schema.\n *\n * Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled\n * on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.\n *\n * @note Stops at the first matching circular ref.\n */\nexport function containsCircularRef(\n node: SchemaNode | undefined,\n { circularSchemas, excludeName }: { circularSchemas: ReadonlySet<string>; excludeName?: string },\n): boolean {\n if (!node || circularSchemas.size === 0) return false\n\n for (const _ of collectLazy<true>(node, {\n schema(child) {\n if (child.type !== 'ref') return null\n const name = resolveRefName(child)\n return name && name !== excludeName && circularSchemas.has(name) ? true : null\n },\n })) {\n return true\n }\n\n return false\n}\n","import type { ArraySchemaNode, IntersectionSchemaNode, ObjectSchemaNode, PropertyNode, SchemaNode, UnionSchemaNode } from '../nodes/index.ts'\nimport { objectKey } from './codegen.ts'\n\n/**\n * Converts a child schema to printer output. Plugins instantiate it with their own output type:\n * `string` for the zod and faker printers, `ts.TypeNode` for the TypeScript printer. A printer's\n * `this.transform` fits directly, so its `null` for an empty result carries through to `output`.\n */\nexport type SchemaTransform<TOutput> = (schema: SchemaNode) => TOutput\n\n/**\n * A union or intersection member, or an array or tuple item, paired with its transformed output.\n */\nexport type MappedSchema<TOutput> = {\n /**\n * The original child schema, kept so the printer can read its metadata for leaf formatting.\n */\n schema: SchemaNode\n /**\n * The child schema after being run through the transform.\n */\n output: TOutput\n}\n\n/**\n * An object property paired with its transformed output.\n */\nexport type MappedProperty<TOutput> = {\n /**\n * The property name as written on the schema, before any identifier quoting.\n */\n name: string\n /**\n * The original property node, kept so the printer can read `required`, `schema`, and metadata.\n */\n property: PropertyNode\n /**\n * The property schema after being run through the transform.\n */\n output: TOutput\n}\n\n/**\n * Maps each property of an object schema to its transformed output. Pairs every result with the\n * original property so the printer keeps full control over modifiers, getters, and key syntax.\n *\n * @example\n * ```ts\n * const entries = mapSchemaProperties(node, (schema) => this.transform(schema))\n * // entries: [{ name: 'id', property, output: 'z.number()' }, ...]\n * ```\n */\nexport function mapSchemaProperties<TOutput>(node: ObjectSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedProperty<TOutput>> {\n return node.properties.map((property) => ({ name: property.name, property, output: transform(property.schema) }))\n}\n\n/**\n * Maps each member of a union or intersection schema to its transformed output, pairing every\n * result with the original member.\n */\nexport function mapSchemaMembers<TOutput>(node: UnionSchemaNode | IntersectionSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> {\n return (node.members ?? []).map((schema) => ({ schema, output: transform(schema) }))\n}\n\n/**\n * Maps each item of an array or tuple schema to its transformed output, pairing every result with\n * the original item.\n */\nexport function mapSchemaItems<TOutput>(node: ArraySchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> {\n return (node.items ?? []).map((schema) => ({ schema, output: transform(schema) }))\n}\n\n/**\n * Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key\n * is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation\n * of a recursive schema until first access.\n *\n * @example\n * ```ts\n * lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })\n * // \"get parent() { return z.lazy(() => Pet) }\"\n * ```\n */\nexport function lazyGetter({ name, body }: { name: string; body: string }): string {\n return `get ${objectKey(name)}() { return ${body} }`\n}\n","import { camelCase, isValidVarName, memoize } from '@internals/utils'\nimport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createTypeLiteral } from '../nodes/function.ts'\nimport type { FunctionParameterNode, FunctionParametersNode, OperationNode, ParameterNode, TypeExpression, TypeLiteralNode } from '../nodes/index.ts'\nimport { resolveGroupType } from './refs.ts'\n\n/**\n * Applies casing rules to parameter names and returns a new array without mutating the input.\n *\n * Run it before handing parameters to schema builders so output property keys get the right casing\n * while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the\n * original array is returned unchanged.\n */\nconst caseParamsMemo = memoize(new WeakMap<Array<ParameterNode>, (casing: string) => Array<ParameterNode>>(), (params) =>\n memoize(new Map<string, Array<ParameterNode>>(), (casing: string) =>\n params.map((param) => {\n const transformed = casing === 'camelcase' || !isValidVarName(param.name) ? camelCase(param.name) : param.name\n return { ...param, name: transformed }\n }),\n ),\n)\n\nexport function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {\n if (!casing) return params\n return caseParamsMemo(params)(casing)\n}\n\n/**\n * Named type for a group of parameters (query or header) emitted as a single typed parameter.\n */\nexport type ParamGroupType = {\n /**\n * Type expression for the group, a plain group-name reference.\n */\n type: TypeExpression\n /**\n * Whether the parameter group is optional.\n */\n optional: boolean\n}\n\n/**\n * A single member of a destructured parameter group, fed to `createFunctionParameter({ properties })`.\n */\ntype GroupProperty = {\n name: string\n type: TypeExpression\n optional?: boolean\n}\n\n/**\n * Resolver interface for {@link createOperationParams}.\n *\n * `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.\n */\nexport type OperationParamsResolver = {\n /**\n * Resolves the type name for an individual parameter.\n *\n * @example Individual path parameter name\n * `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`\n */\n resolveParamName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the request body type name.\n *\n * @example Request body type name\n * `resolver.resolveDataName(node) // → 'CreatePetData'`\n */\n resolveDataName(node: OperationNode): string\n /**\n * Resolves the grouped path parameters type name.\n * When the return value equals `resolveParamName`, no indexed access is emitted.\n *\n * @example Grouped path params type name\n * `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`\n */\n resolvePathParamsName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the grouped query parameters type name.\n * When the return value equals `resolveParamName`, an inline struct type is emitted instead.\n *\n * @example Grouped query params type name\n * `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`\n */\n resolveQueryParamsName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the grouped header parameters type name.\n * When the return value equals `resolveParamName`, an inline struct type is emitted instead.\n *\n * @example Grouped header params type name\n * `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`\n */\n resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string\n}\n\n/**\n * Options for {@link createOperationParams}.\n */\nexport type CreateOperationParamsOptions = {\n /**\n * How all operation parameters are grouped in the function signature.\n * - `'object'` wraps all params into a single destructured object `{ petId, data, params }`\n * - `'inline'` emits each param category as a separate top-level parameter\n */\n paramsType: 'object' | 'inline'\n /**\n * How path parameters are emitted when `paramsType` is `'inline'`.\n * - `'object'` groups them as `{ petId, storeId }: PathParams`\n * - `'inline'` spreads them as individual parameters `petId: string, storeId: string`\n * - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`\n */\n pathParamsType: 'object' | 'inline' | 'inlineSpread'\n /**\n * Converts parameter names to camelCase before output.\n */\n paramsCasing?: 'camelcase'\n /**\n * Resolver for parameter and request body type names.\n * Pass `ResolverTs` from `@kubb/plugin-ts` directly.\n * When omitted, falls back to the schema primitive or `'unknown'`.\n */\n resolver?: OperationParamsResolver\n /**\n * Default value for the path parameters binding when `pathParamsType` is `'object'`.\n * Falls back to `'{}'` when all path params are optional.\n */\n pathParamsDefault?: string\n /**\n * Extra parameters appended after the standard operation parameters.\n *\n * @example Plugin-specific trailing parameter\n * ```ts\n * extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]\n * ```\n */\n extraParams?: Array<FunctionParameterNode>\n /**\n * Override the default parameter names used for body, query, header, and rest-path groups.\n *\n * Useful when targeting languages or frameworks with different naming conventions.\n *\n * @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }\n */\n paramNames?: {\n /**\n * Name for the request body parameter.\n * @default 'data'\n */\n data?: string\n /**\n * Name for the query parameters group parameter.\n * @default 'params'\n */\n params?: string\n /**\n * Name for the header parameters group parameter.\n * @default 'headers'\n */\n headers?: string\n /**\n * Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.\n * @default 'pathParams'\n */\n path?: string\n }\n /**\n * Transforms every resolved type name before it lands in a parameter node, for framework-level\n * type wrappers.\n *\n * @example Vue Query, wrap every parameter type with `MaybeRefOrGetter`\n * `typeWrapper: (t) => \\`MaybeRefOrGetter<${t}>\\``\n */\n typeWrapper?: (type: string) => string\n}\n\n/**\n * Resolves the {@link TypeExpression} for an individual parameter.\n *\n * Without a resolver, it falls back to the schema primitive (a plain type-name string). When the\n * parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like\n * `GroupParams['petId']`, otherwise the resolved individual name.\n */\nexport function resolveParamType({\n node,\n param,\n resolver,\n}: {\n node: OperationNode\n param: ParameterNode\n resolver: OperationParamsResolver | undefined\n}): TypeExpression {\n if (!resolver) {\n return param.schema.primitive ?? 'unknown'\n }\n\n const individualName = resolver.resolveParamName(node, param)\n\n const groupLocation = param.in === 'path' || param.in === 'query' || param.in === 'header' ? param.in : undefined\n\n const groupResolvers = {\n path: resolver.resolvePathParamsName,\n query: resolver.resolveQueryParamsName,\n header: resolver.resolveHeaderParamsName,\n } as const\n\n const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : undefined\n\n if (groupName && groupName !== individualName) {\n return createIndexedAccessType({ target: groupName, key: param.name })\n }\n\n return individualName\n}\n\n/**\n * Converts an `OperationNode` into function parameters for code generation.\n *\n * Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one\n * destructured object parameter (`object`) and separate top-level parameters (`inline`), while\n * `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type\n * name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.\n */\nexport function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode {\n const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options\n\n const dataName = paramNames?.data ?? 'data'\n const paramsName = paramNames?.params ?? 'params'\n const headersName = paramNames?.headers ?? 'headers'\n const pathName = paramNames?.path ?? 'pathParams'\n\n const wrapType = (type: string): string => (typeWrapper ? typeWrapper(type) : type)\n // Only plain type-name references are wrapped, they need casing applied.\n // TypeLiteral and IndexedAccessType expressions are pre-resolved and pass through unchanged.\n const wrapTypeExpression = (type: TypeExpression): TypeExpression => (typeof type === 'string' ? wrapType(type) : type)\n\n const casedParams = caseParams(node.parameters, paramsCasing)\n const pathParams = casedParams.filter((p) => p.in === 'path')\n const queryParams = casedParams.filter((p) => p.in === 'query')\n const headerParams = casedParams.filter((p) => p.in === 'header')\n\n const toProperty = (param: ParameterNode): GroupProperty => ({\n name: param.name,\n type: wrapTypeExpression(resolveParamType({ node, param, resolver })),\n optional: !param.required,\n })\n const emptyObjectDefault = (props: Array<GroupProperty>): string | undefined => (props.every((p) => p.optional) ? '{}' : undefined)\n\n const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? 'unknown') : undefined\n const bodyProperty: Array<GroupProperty> = bodyType ? [{ name: dataName, type: bodyType, optional: !(node.requestBody?.required ?? false) }] : []\n\n const trailingGroups: Array<BuildGroupArgs> = [\n { name: paramsName, node, params: queryParams, groupType: resolveGroupType({ node, params: queryParams, group: 'query', resolver }), resolver, wrapType },\n {\n name: headersName,\n node,\n params: headerParams,\n groupType: resolveGroupType({ node, params: headerParams, group: 'header', resolver }),\n resolver,\n wrapType,\n },\n ]\n\n const params: Array<FunctionParameterNode> = []\n\n if (paramsType === 'object') {\n const children = [...pathParams.map(toProperty), ...bodyProperty, ...trailingGroups.flatMap(buildGroupProperty)]\n if (children.length) {\n params.push(createFunctionParameter({ properties: children, default: emptyObjectDefault(children) }))\n }\n } else {\n if (pathParamsType === 'inlineSpread' && pathParams.length) {\n const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]!)\n params.push(createFunctionParameter({ name: pathName, type: spreadType ? wrapType(spreadType) : undefined, rest: true }))\n } else if (pathParamsType === 'inline') {\n params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))))\n } else if (pathParams.length) {\n const pathChildren = pathParams.map(toProperty)\n params.push(createFunctionParameter({ properties: pathChildren, default: pathParamsDefault ?? emptyObjectDefault(pathChildren) }))\n }\n\n params.push(...bodyProperty.map((p) => createFunctionParameter(p)))\n params.push(...trailingGroups.flatMap(buildGroupParam))\n }\n\n params.push(...extraParams)\n\n return createFunctionParameters({ params })\n}\n\n/**\n * Shared arguments for building a query or header parameter group.\n */\nexport type BuildGroupArgs = {\n name: string\n node: OperationNode\n params: Array<ParameterNode>\n groupType: ParamGroupType | null\n resolver: OperationParamsResolver | undefined\n wrapType: (type: string) => string\n}\n\n/**\n * Builds the property descriptor for a query or header group.\n * Returns an empty array when there are no params to emit.\n *\n * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline\n * {@link TypeLiteralNode} from the individual params.\n */\nfunction buildGroupProperty({ name, node, params, groupType, resolver, wrapType }: BuildGroupArgs): Array<GroupProperty> {\n if (groupType) {\n const type = typeof groupType.type === 'string' ? wrapType(groupType.type) : groupType.type\n return [{ name, type, optional: groupType.optional }]\n }\n if (params.length) {\n return [{ name, type: buildTypeLiteral({ node, params, resolver }), optional: params.every((p) => !p.required) }]\n }\n return []\n}\n\n/**\n * Builds a single {@link FunctionParameterNode} for a query or header group.\n * Returns an empty array when there are no params to emit.\n *\n * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline\n * {@link TypeLiteralNode} from the individual params.\n */\nexport function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode> {\n return buildGroupProperty(args).map((p) => createFunctionParameter(p))\n}\n\n/**\n * Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.\n *\n * Used when query or header parameters have no dedicated group type name.\n * Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).\n */\nexport function buildTypeLiteral({\n node,\n params,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n resolver: OperationParamsResolver | undefined\n}): TypeLiteralNode {\n return createTypeLiteral({\n members: params.map((p) => ({\n name: p.name,\n type: resolveParamType({ node, param: p, resolver }),\n optional: !p.required,\n })),\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6IA,SAAgB,QAAsB,OAA4B,SAAuD;CACvH,QAAQ,QAAsB;EAC5B,IAAI,MAAM,IAAI,GAAG,GAAG,OAAO,MAAM,IAAI,GAAG;EACxC,MAAM,QAAQ,QAAQ,GAAG;EACzB,MAAM,IAAI,KAAK,KAAK;EACpB,OAAO;CACT;AACF;;;;;;;AChJA,MAAM,gBAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAU;;;;;;;;;;;AA8BV,SAAgB,eAAe,MAAuB;CACpD,IAAI,CAAC,QAAQ,cAAc,IAAI,IAAiB,GAC9C,OAAO;CAET,OAAO,aAAa,IAAI;AAC1B;;;;;;;;;;;;;;AAeA,SAAgB,aAAa,MAA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,GAAG,eAAe,QAAQ,eAAe,CAAC;CACxD;CAEA,OAAO,KAAK,GAAG,WAAW;CAE1B,OAAOC,iBAAAA,yBAAyB,EAAE,OAAO,CAAC;AAC5C;;;;;;;;AAqBA,SAAS,mBAAmB,EAAE,MAAM,MAAM,QAAQ,WAAW,UAAU,YAAkD;CACvH,IAAI,WAEF,OAAO,CAAC;EAAE;EAAM,MADH,OAAO,UAAU,SAAS,WAAW,SAAS,UAAU,IAAI,IAAI,UAAU;EACjE,UAAU,UAAU;CAAS,CAAC;CAEtD,IAAI,OAAO,QACT,OAAO,CAAC;EAAE;EAAM,MAAM,iBAAiB;GAAE;GAAM;GAAQ;EAAS,CAAC;EAAG,UAAU,OAAO,OAAO,MAAM,CAAC,EAAE,QAAQ;CAAE,CAAC;CAElH,OAAO,CAAC;AACV;;;;;;;;AASA,SAAgB,gBAAgB,MAAoD;CAClF,OAAO,mBAAmB,IAAI,CAAC,CAAC,KAAK,MAAMD,iBAAAA,wBAAwB,CAAC,CAAC;AACvE;;;;;;;AAQA,SAAgB,iBAAiB,EAC/B,MACA,QACA,YAKkB;CAClB,OAAOE,iBAAAA,kBAAkB,EACvB,SAAS,OAAO,KAAK,OAAO;EAC1B,MAAM,EAAE;EACR,MAAM,iBAAiB;GAAE;GAAM,OAAO;GAAG;EAAS,CAAC;EACnD,UAAU,CAAC,EAAE;CACf,EAAE,EACJ,CAAC;AACH"}
import "./chunk-CNktS9qV.js";
import { d as resolveRefName, h as INDENT, m as narrowSchema, n as collectLazy, t as collect, u as resolveGroupType } from "./visitor-DepQEKyp.js";
import { Q as createSchema, X as camelCase, _ as createIndexedAccessType, g as createFunctionParameters, h as createFunctionParameter, y as createTypeLiteral } from "./response-Rd1uisM1.js";
//#region ../../internals/utils/src/promise.ts
/**
* Wraps `factory` with a keyed cache backed by the provided store.
*
* Pass a `WeakMap` for object keys (results are GC-eligible when the key is
* collected) or a `Map` for primitive keys. For multi-argument functions,
* nest two `memoize` calls — the outer keyed by the first argument, the
* inner (created once per outer miss) keyed by the second.
*
* Because the cache is owned by the caller, it can be shared, inspected, or
* cleared independently of the memoized function.
*
* @example Single WeakMap key
* ```ts
* const cache = new WeakMap<SchemaNode, Set<string>>()
* const getRefs = memoize(cache, (node) => collectRefs(node))
* ```
*
* @example Single Map key (primitive)
* ```ts
* const cache = new Map<string, Resolver>()
* const getResolver = memoize(cache, (name) => buildResolver(name))
* ```
*
* @example Two-level (object + primitive)
* ```ts
* const outer = new WeakMap<Params[], Map<string, Params[]>>()
* const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
* fn(params)('camelcase')
* ```
*/
function memoize(store, factory) {
return (key) => {
if (store.has(key)) return store.get(key);
const value = factory(key);
store.set(key, value);
return value;
};
}
//#endregion
//#region ../../internals/utils/src/reserved.ts
/**
* JavaScript and Java reserved words.
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
*/
const reservedWords = new Set([
"abstract",
"arguments",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"double",
"else",
"enum",
"eval",
"export",
"extends",
"false",
"final",
"finally",
"float",
"for",
"function",
"goto",
"if",
"implements",
"import",
"in",
"instanceof",
"int",
"interface",
"let",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"typeof",
"var",
"void",
"volatile",
"while",
"with",
"yield",
"Array",
"Date",
"hasOwnProperty",
"Infinity",
"isFinite",
"isNaN",
"isPrototypeOf",
"length",
"Math",
"name",
"NaN",
"Number",
"Object",
"prototype",
"String",
"toString",
"undefined",
"valueOf"
]);
/**
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
*
* @example
* ```ts
* isValidVarName('status') // true
* isValidVarName('class') // false (reserved word)
* isValidVarName('42foo') // false (starts with digit)
* ```
*/
function isValidVarName(name) {
if (!name || reservedWords.has(name)) return false;
return isIdentifier(name);
}
/**
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
*
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
* deciding whether an object key needs quoting.
*
* @example
* ```ts
* isIdentifier('name') // true
* isIdentifier('x-total')// false
* ```
*/
function isIdentifier(name) {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
}
//#endregion
//#region ../../internals/utils/src/string.ts
/**
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
* any backslash or single quote in the content.
*
* @example
* ```ts
* singleQuote('foo') // "'foo'"
* singleQuote("o'clock") // "'o\\'clock'"
* ```
*/
function singleQuote(value) {
if (value === void 0 || value === null) return "''";
return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
}
//#endregion
//#region src/utils/codegen.ts
/**
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no
* comments so callers always get a usable string.
*
* @example
* ```ts
* buildJSDoc(['@type string', '@example hello'])
* // '/**\n * @type string\n * @example hello\n *\/\n '
* ```
*/
function buildJSDoc(comments, options = {}) {
const { indent = " * ", suffix = "\n ", fallback = " " } = options;
if (comments.length === 0) return fallback;
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
}
/**
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
*/
function indentLines(text) {
if (!text) return "";
return text.split("\n").map((line) => line.trim() ? `${INDENT}${line}` : "").join("\n");
}
/**
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
*
* @example
* ```ts
* objectKey('name') // 'name'
* objectKey('x-total') // "'x-total'"
* ```
*/
function objectKey(name) {
return isIdentifier(name) ? name : singleQuote(name);
}
/**
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
* level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
* so callers never re-parse the generated code. A trailing comma is added per entry to match the
* formatter's multi-line style.
*
* @example
* ```ts
* buildObject(['id: z.number()', 'name: z.string()'])
* // '{\n id: z.number(),\n name: z.string(),\n}'
* ```
*/
function buildObject(entries) {
if (entries.length === 0) return "{}";
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
}
/**
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
* one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
* `z.array([…])`, and similar member lists so objects inside them nest correctly.
*
* @example
* ```ts
* buildList(['z.string()', 'z.number()'])
* // '[z.string(), z.number()]'
* ```
*/
function buildList(items, brackets = ["[", "]"]) {
const [open, close] = brackets;
if (items.length === 0) return `${open}${close}`;
if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
}
//#endregion
//#region src/utils/schemaMerge.ts
/**
* Merges a run of adjacent anonymous object members into one. Named or non-object members break the
* run and pass through. Stays a construction-time helper, not a macro, so callers keep control of the
* member boundaries (such as keeping synthetic discriminant objects out of a run).
*
* @example
* ```ts
* const merged = [...mergeAdjacentObjectsLazy([objectA, objectB])]
* ```
*/
function* mergeAdjacentObjectsLazy(members) {
let acc;
for (const member of members) {
const objectMember = narrowSchema(member, "object");
if (objectMember && !objectMember.name && acc !== void 0) {
const accObject = narrowSchema(acc, "object");
if (accObject && !accObject.name) {
acc = createSchema({
...accObject,
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
});
continue;
}
}
if (acc !== void 0) yield acc;
acc = member;
}
if (acc !== void 0) yield acc;
}
//#endregion
//#region src/utils/strings.ts
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
function trimQuotes(text) {
if (text.length >= 2) {
const first = text[0];
const last = text[text.length - 1];
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
}
return text;
}
/**
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
*
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
* code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
function stringify(value) {
if (value === void 0 || value === null) return "''";
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
}
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
function jsStringEscape(input) {
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
switch (character) {
case "\"":
case "'":
case "\\": return `\\${character}`;
case "\n": return "\\n";
case "\r": return "\\r";
case "\u2028": return "\\u2028";
case "\u2029": return "\\u2029";
default: return "";
}
});
}
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
function toRegExpString(text, func = "RegExp") {
const raw = trimQuotes(text);
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
const replacementTarget = match?.[1] ?? "";
const matchedFlags = match?.[2];
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
const { source, flags } = new RegExp(cleaned, matchedFlags);
if (func === null) return `/${source}/${flags}`;
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
}
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
function stringifyObject(value) {
return Object.entries(value).map(([key, val]) => {
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
return `${key}: ${val}`;
}).filter(Boolean).join(",\n");
}
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
function getNestedAccessor(param, accessor) {
const parts = Array.isArray(param) ? param : param.split(".");
if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
}
//#endregion
//#region src/utils/schemaGraph.ts
/**
* Collects every named schema referenced transitively from a node through its ref edges.
*
* Refs are followed by name only, so the resolved `node.schema` is never traversed inline.
*
* @example Collect refs from a single schema
* ```ts
* const names = collectReferencedSchemaNames(petSchema)
* // → Set { 'Category', 'Tag' }
* ```
*
* @example Accumulate refs from multiple schemas into one set
* ```ts
* const out = new Set<string>()
* for (const schema of schemas) {
* collectReferencedSchemaNames(schema, out)
* }
* ```
*/
const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
const refs = /* @__PURE__ */ new Set();
collect(node, { schema(child) {
if (child.type === "ref") {
const name = resolveRefName(child);
if (name) refs.add(name);
}
} });
return refs;
});
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
if (!node) return out;
for (const name of collectSchemaRefs(node)) out.add(name);
return out;
}
/**
* Collects the names of all top-level schemas transitively used by a set of operations.
*
* An operation uses a schema when its parameters, request body, or responses reference it, directly
* or through other named schemas. The walk is iterative, so reference cycles are safe.
*
* Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.
*
* @example Only generate schemas referenced by included operations
* ```ts
* const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)
* const allowed = collectUsedSchemaNames(includedOps, schemas)
*
* for (const schema of schemas) {
* if (schema.name && !allowed.has(schema.name)) continue
* // … generate schema
* }
* ```
*/
const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
function computeUsedSchemaNames(operations, schemas) {
const schemaMap = /* @__PURE__ */ new Map();
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
const result = /* @__PURE__ */ new Set();
function visitSchema(schema) {
const directRefs = collectReferencedSchemaNames(schema);
for (const name of directRefs) if (!result.has(name)) {
result.add(name);
const namedSchema = schemaMap.get(name);
if (namedSchema) visitSchema(namedSchema);
}
}
for (const op of operations) for (const schema of collectLazy(op, {
depth: "shallow",
schema: (node) => node
})) visitSchema(schema);
return result;
}
function collectUsedSchemaNames(operations, schemas) {
return collectUsedSchemaNamesMemo(operations)(schemas);
}
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
const graph = /* @__PURE__ */ new Map();
for (const schema of schemas) {
if (!schema.name) continue;
graph.set(schema.name, collectReferencedSchemaNames(schema));
}
const circular = /* @__PURE__ */ new Set();
for (const start of graph.keys()) {
const visited = /* @__PURE__ */ new Set();
const stack = [...graph.get(start) ?? []];
while (stack.length > 0) {
const node = stack.pop();
if (node === start) {
circular.add(start);
break;
}
if (visited.has(node)) continue;
visited.add(node);
const next = graph.get(node);
if (next) for (const r of next) stack.push(r);
}
}
return circular;
});
/**
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
*
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
* the generated code does not recurse forever. Refs are followed by name only, so the walk stays
* linear in the size of the schema graph.
*
* @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.
*/
function findCircularSchemas(schemas) {
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
return findCircularSchemasMemo(schemas);
}
/**
* Returns `true` when a schema, or anything nested inside it, references a circular schema.
*
* Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled
* on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.
*
* @note Stops at the first matching circular ref.
*/
function containsCircularRef(node, { circularSchemas, excludeName }) {
if (!node || circularSchemas.size === 0) return false;
for (const _ of collectLazy(node, { schema(child) {
if (child.type !== "ref") return null;
const name = resolveRefName(child);
return name && name !== excludeName && circularSchemas.has(name) ? true : null;
} })) return true;
return false;
}
//#endregion
//#region src/utils/schemaTraversal.ts
/**
* Maps each property of an object schema to its transformed output. Pairs every result with the
* original property so the printer keeps full control over modifiers, getters, and key syntax.
*
* @example
* ```ts
* const entries = mapSchemaProperties(node, (schema) => this.transform(schema))
* // entries: [{ name: 'id', property, output: 'z.number()' }, ...]
* ```
*/
function mapSchemaProperties(node, transform) {
return node.properties.map((property) => ({
name: property.name,
property,
output: transform(property.schema)
}));
}
/**
* Maps each member of a union or intersection schema to its transformed output, pairing every
* result with the original member.
*/
function mapSchemaMembers(node, transform) {
return (node.members ?? []).map((schema) => ({
schema,
output: transform(schema)
}));
}
/**
* Maps each item of an array or tuple schema to its transformed output, pairing every result with
* the original item.
*/
function mapSchemaItems(node, transform) {
return (node.items ?? []).map((schema) => ({
schema,
output: transform(schema)
}));
}
/**
* Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key
* is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation
* of a recursive schema until first access.
*
* @example
* ```ts
* lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })
* // "get parent() { return z.lazy(() => Pet) }"
* ```
*/
function lazyGetter({ name, body }) {
return `get ${objectKey(name)}() { return ${body} }`;
}
//#endregion
//#region src/utils/operationParams.ts
/**
* Applies casing rules to parameter names and returns a new array without mutating the input.
*
* Run it before handing parameters to schema builders so output property keys get the right casing
* while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the
* original array is returned unchanged.
*/
const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => {
const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name;
return {
...param,
name: transformed
};
})));
function caseParams(params, casing) {
if (!casing) return params;
return caseParamsMemo(params)(casing);
}
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, it falls back to the schema primitive (a plain type-name string). When the
* parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like
* `GroupParams['petId']`, otherwise the resolved individual name.
*/
function resolveParamType({ node, param, resolver }) {
if (!resolver) return param.schema.primitive ?? "unknown";
const individualName = resolver.resolveParamName(node, param);
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
const groupResolvers = {
path: resolver.resolvePathParamsName,
query: resolver.resolveQueryParamsName,
header: resolver.resolveHeaderParamsName
};
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
if (groupName && groupName !== individualName) return createIndexedAccessType({
target: groupName,
key: param.name
});
return individualName;
}
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
function createOperationParams(node, options) {
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options;
const dataName = paramNames?.data ?? "data";
const paramsName = paramNames?.params ?? "params";
const headersName = paramNames?.headers ?? "headers";
const pathName = paramNames?.path ?? "pathParams";
const wrapType = (type) => typeWrapper ? typeWrapper(type) : type;
const wrapTypeExpression = (type) => typeof type === "string" ? wrapType(type) : type;
const casedParams = caseParams(node.parameters, paramsCasing);
const pathParams = casedParams.filter((p) => p.in === "path");
const queryParams = casedParams.filter((p) => p.in === "query");
const headerParams = casedParams.filter((p) => p.in === "header");
const toProperty = (param) => ({
name: param.name,
type: wrapTypeExpression(resolveParamType({
node,
param,
resolver
})),
optional: !param.required
});
const emptyObjectDefault = (props) => props.every((p) => p.optional) ? "{}" : void 0;
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
const bodyProperty = bodyType ? [{
name: dataName,
type: bodyType,
optional: !(node.requestBody?.required ?? false)
}] : [];
const trailingGroups = [{
name: paramsName,
node,
params: queryParams,
groupType: resolveGroupType({
node,
params: queryParams,
group: "query",
resolver
}),
resolver,
wrapType
}, {
name: headersName,
node,
params: headerParams,
groupType: resolveGroupType({
node,
params: headerParams,
group: "header",
resolver
}),
resolver,
wrapType
}];
const params = [];
if (paramsType === "object") {
const children = [
...pathParams.map(toProperty),
...bodyProperty,
...trailingGroups.flatMap(buildGroupProperty)
];
if (children.length) params.push(createFunctionParameter({
properties: children,
default: emptyObjectDefault(children)
}));
} else {
if (pathParamsType === "inlineSpread" && pathParams.length) {
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]);
params.push(createFunctionParameter({
name: pathName,
type: spreadType ? wrapType(spreadType) : void 0,
rest: true
}));
} else if (pathParamsType === "inline") params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))));
else if (pathParams.length) {
const pathChildren = pathParams.map(toProperty);
params.push(createFunctionParameter({
properties: pathChildren,
default: pathParamsDefault ?? emptyObjectDefault(pathChildren)
}));
}
params.push(...bodyProperty.map((p) => createFunctionParameter(p)));
params.push(...trailingGroups.flatMap(buildGroupParam));
}
params.push(...extraParams);
return createFunctionParameters({ params });
}
/**
* Builds the property descriptor for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }) {
if (groupType) return [{
name,
type: typeof groupType.type === "string" ? wrapType(groupType.type) : groupType.type,
optional: groupType.optional
}];
if (params.length) return [{
name,
type: buildTypeLiteral({
node,
params,
resolver
}),
optional: params.every((p) => !p.required)
}];
return [];
}
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupParam(args) {
return buildGroupProperty(args).map((p) => createFunctionParameter(p));
}
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
function buildTypeLiteral({ node, params, resolver }) {
return createTypeLiteral({ members: params.map((p) => ({
name: p.name,
type: resolveParamType({
node,
param: p,
resolver
}),
optional: !p.required
})) });
}
//#endregion
export { objectKey as C, buildObject as S, toRegExpString as _, resolveParamType as a, buildJSDoc as b, mapSchemaMembers as c, containsCircularRef as d, findCircularSchemas as f, stringifyObject as g, stringify as h, createOperationParams as i, mapSchemaProperties as l, jsStringEscape as m, buildTypeLiteral as n, lazyGetter as o, getNestedAccessor as p, caseParams as r, mapSchemaItems as s, buildGroupParam as t, collectUsedSchemaNames as u, trimQuotes as v, isValidVarName as w, buildList as x, mergeAdjacentObjectsLazy as y };
//# sourceMappingURL=utils-DW9rnfsD.js.map
{"version":3,"file":"utils-DW9rnfsD.js","names":[],"sources":["../../../internals/utils/src/promise.ts","../../../internals/utils/src/reserved.ts","../../../internals/utils/src/string.ts","../src/utils/codegen.ts","../src/utils/schemaMerge.ts","../src/utils/strings.ts","../src/utils/schemaGraph.ts","../src/utils/schemaTraversal.ts","../src/utils/operationParams.ts"],"sourcesContent":["function* chunks<T>(arr: ReadonlyArray<T>, size: number): Generator<Array<T>> {\n for (let i = 0; i < arr.length; i += size) {\n yield arr.slice(i, i + size)\n }\n}\n\nexport type ForBatchesOptions = {\n /**\n * Maximum batch size handed to `process`.\n * Parallel dispatch within a batch is the caller's responsibility\n * (typically via `Promise.all(batch.map(...))`).\n */\n concurrency: number\n /**\n * Called after every batch.\n *\n * Use a cheap, idempotent callback (e.g. one that short-circuits when there\n * is nothing new to do). The helper does not coalesce calls — if you need\n * throttling, do it inside `flush` itself.\n */\n flush?: () => Promise<void>\n}\n\n/**\n * Slices `source` into batches of `concurrency` items and awaits `process` for each batch.\n * Accepts both plain arrays (sync) and `AsyncIterable` (streaming).\n *\n * `process` controls whether items inside a batch run in parallel; this helper only\n * controls batch size and per-batch flushing.\n *\n * @example\n * ```ts\n * // parallel dispatch inside each batch\n * await forBatches(schemas, (batch) => Promise.all(batch.map(process)), { concurrency: 8 })\n *\n * // async iterable with a flush after every batch\n * await forBatches(stream.schemas, (batch) => dispatch(batch), { concurrency: 8, flush })\n * ```\n */\nexport async function forBatches<T>(\n source: ReadonlyArray<T> | AsyncIterable<T>,\n process: (batch: Array<T>) => Promise<unknown>,\n options: ForBatchesOptions,\n): Promise<void> {\n const { concurrency, flush } = options\n\n if (Array.isArray(source)) {\n for (const batch of chunks(source, concurrency)) {\n await process(batch)\n if (flush) await flush()\n }\n return\n }\n\n const batch: Array<T> = []\n for await (const item of source) {\n batch.push(item)\n if (batch.length >= concurrency) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n }\n if (batch.length > 0) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n}\n\n/** A value that may already be resolved or still pending.\n *\n * @example\n * ```ts\n * function load(id: string): PossiblePromise<string> {\n * return cache.get(id) ?? fetchRemote(id)\n * }\n * ```\n */\nexport type PossiblePromise<T> = Promise<T> | T\n\n/** Returns `true` when `result` is a thenable `Promise`.\n *\n * @example\n * ```ts\n * isPromise(Promise.resolve(1)) // true\n * isPromise(42) // false\n * ```\n */\nexport function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {\n return result !== null && result !== undefined && typeof (result as Record<string, unknown>)['then'] === 'function'\n}\n\n/** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)\n * ```\n */\nexport function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & { reason: T } {\n return result.status === 'rejected'\n}\n\ntype Store<TKey, TValue> = {\n has(key: TKey): boolean\n get(key: TKey): TValue | undefined\n set(key: TKey, value: TValue): unknown\n}\n\n/**\n * Wraps `factory` with a keyed cache backed by the provided store.\n *\n * Pass a `WeakMap` for object keys (results are GC-eligible when the key is\n * collected) or a `Map` for primitive keys. For multi-argument functions,\n * nest two `memoize` calls — the outer keyed by the first argument, the\n * inner (created once per outer miss) keyed by the second.\n *\n * Because the cache is owned by the caller, it can be shared, inspected, or\n * cleared independently of the memoized function.\n *\n * @example Single WeakMap key\n * ```ts\n * const cache = new WeakMap<SchemaNode, Set<string>>()\n * const getRefs = memoize(cache, (node) => collectRefs(node))\n * ```\n *\n * @example Single Map key (primitive)\n * ```ts\n * const cache = new Map<string, Resolver>()\n * const getResolver = memoize(cache, (name) => buildResolver(name))\n * ```\n *\n * @example Two-level (object + primitive)\n * ```ts\n * const outer = new WeakMap<Params[], Map<string, Params[]>>()\n * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))\n * fn(params)('camelcase')\n * ```\n */\nexport function memoize<TKey, TValue>(store: Store<TKey, TValue>, factory: (key: TKey) => TValue): (key: TKey) => TValue {\n return (key: TKey): TValue => {\n if (store.has(key)) return store.get(key)!\n const value = factory(key)\n store.set(key, value)\n return value\n }\n}\n\n/**\n * Container that switches between an eager `Array<T>` and a lazy `AsyncIterable<T>`.\n *\n * `Array<T>` by default. With `Stream` set to `true` it becomes `AsyncIterable<T>`, so large\n * collections can be produced lazily without holding every item in memory. Pairs with\n * {@link arrayToAsyncIterable}, which lifts a plain array into the streaming form.\n *\n * @example\n * ```ts\n * type Eager = Streamable<number> // Array<number>\n * type Lazy = Streamable<number, true> // AsyncIterable<number>\n * ```\n */\nexport type Streamable<T, Stream extends boolean = false> = Stream extends true ? AsyncIterable<T> : Array<T>\n\n/**\n * Wraps a plain array in a reusable `AsyncIterable`.\n * Each `[Symbol.asyncIterator]()` call returns a fresh generator so the\n * iterable can be consumed multiple times (e.g. once per plugin pre-scan).\n *\n * @example\n * ```ts\n * const stream = arrayToAsyncIterable([1, 2, 3])\n * for await (const n of stream) console.log(n) // 1, 2, 3\n * ```\n */\nexport function arrayToAsyncIterable<T>(arr: ReadonlyArray<T>): AsyncIterable<T> {\n return {\n [Symbol.asyncIterator]() {\n return (async function* () {\n yield* arr\n })()\n },\n }\n}\n","/**\n * JavaScript and Java reserved words.\n * @link https://github.com/jonschlinkert/reserved/blob/master/index.js\n */\nconst reservedWords = new Set([\n 'abstract',\n 'arguments',\n 'boolean',\n 'break',\n 'byte',\n 'case',\n 'catch',\n 'char',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'double',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'final',\n 'finally',\n 'float',\n 'for',\n 'function',\n 'goto',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'int',\n 'interface',\n 'let',\n 'long',\n 'native',\n 'new',\n 'null',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'short',\n 'static',\n 'super',\n 'switch',\n 'synchronized',\n 'this',\n 'throw',\n 'throws',\n 'transient',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'volatile',\n 'while',\n 'with',\n 'yield',\n 'Array',\n 'Date',\n 'hasOwnProperty',\n 'Infinity',\n 'isFinite',\n 'isNaN',\n 'isPrototypeOf',\n 'length',\n 'Math',\n 'name',\n 'NaN',\n 'Number',\n 'Object',\n 'prototype',\n 'String',\n 'toString',\n 'undefined',\n 'valueOf',\n] as const)\n\n/**\n * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.\n *\n * @example\n * ```ts\n * transformReservedWord('class') // '_class'\n * transformReservedWord('42foo') // '_42foo'\n * transformReservedWord('status') // 'status'\n * ```\n */\nexport function transformReservedWord(word: string): string {\n const firstChar = word.charCodeAt(0)\n if (word && (reservedWords.has(word as 'valueOf') || (firstChar >= 48 && firstChar <= 57))) {\n return `_${word}`\n }\n return word\n}\n\n/**\n * Returns `true` when `name` is a syntactically valid JavaScript variable name.\n *\n * @example\n * ```ts\n * isValidVarName('status') // true\n * isValidVarName('class') // false (reserved word)\n * isValidVarName('42foo') // false (starts with digit)\n * ```\n */\nexport function isValidVarName(name: string): boolean {\n if (!name || reservedWords.has(name as 'valueOf')) {\n return false\n }\n return isIdentifier(name)\n}\n\n/**\n * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.\n *\n * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys\n * even though they are not valid variable names, so use this (not {@link isValidVarName}) when\n * deciding whether an object key needs quoting.\n *\n * @example\n * ```ts\n * isIdentifier('name') // true\n * isIdentifier('x-total')// false\n * ```\n */\nexport function isIdentifier(name: string): boolean {\n return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)\n}\n","/**\n * Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping\n * any backslash or single quote in the content.\n *\n * @example\n * ```ts\n * singleQuote('foo') // \"'foo'\"\n * singleQuote(\"o'clock\") // \"'o\\\\'clock'\"\n * ```\n */\nexport function singleQuote(value: string | number | boolean | undefined | null): string {\n if (value === undefined || value === null) return \"''\"\n const escaped = String(value).replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")\n\n return `'${escaped}'`\n}\n","import { isIdentifier, singleQuote } from '@internals/utils'\nimport { INDENT } from '../constants.ts'\n\n/**\n * Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no\n * comments so callers always get a usable string.\n *\n * @example\n * ```ts\n * buildJSDoc(['@type string', '@example hello'])\n * // '/**\\n * @type string\\n * @example hello\\n *\\/\\n '\n * ```\n */\nexport function buildJSDoc(\n comments: Array<string>,\n options: {\n /**\n * String used to indent each comment line.\n * @default ' * '\n */\n indent?: string\n /**\n * String appended after the closing tag.\n * @default '\\n '\n */\n suffix?: string\n /**\n * Returned as-is when `comments` is empty.\n * @default ' '\n */\n fallback?: string\n } = {},\n): string {\n const { indent = ' * ', suffix = '\\n ', fallback = ' ' } = options\n\n if (comments.length === 0) return fallback\n\n return `/**\\n${comments.map((c) => `${indent}${c}`).join('\\n')}\\n */${suffix}`\n}\n\n/**\n * Indents every non-empty line of `text` by one indent level, leaving blank lines empty.\n */\nfunction indentLines(text: string): string {\n if (!text) return ''\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${INDENT}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Renders an object key, quoting it with single quotes only when it is not a valid identifier.\n * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.\n *\n * @example\n * ```ts\n * objectKey('name') // 'name'\n * objectKey('x-total') // \"'x-total'\"\n * ```\n */\nexport function objectKey(name: string): string {\n return isIdentifier(name) ? name : singleQuote(name)\n}\n\n/**\n * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one\n * level and closing the brace at column zero. Nested objects built the same way indent cumulatively,\n * so callers never re-parse the generated code. A trailing comma is added per entry to match the\n * formatter's multi-line style.\n *\n * @example\n * ```ts\n * buildObject(['id: z.number()', 'name: z.string()'])\n * // '{\\n id: z.number(),\\n name: z.string(),\\n}'\n * ```\n */\nexport function buildObject(entries: Array<string>): string {\n if (entries.length === 0) return '{}'\n const body = entries.map((entry) => `${indentLines(entry)},`).join('\\n')\n\n return `{\\n${body}\\n}`\n}\n\n/**\n * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on\n * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented\n * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,\n * `z.array([…])`, and similar member lists so objects inside them nest correctly.\n *\n * @example\n * ```ts\n * buildList(['z.string()', 'z.number()'])\n * // '[z.string(), z.number()]'\n * ```\n */\nexport function buildList(items: Array<string>, brackets: [open: string, close: string] = ['[', ']']): string {\n const [open, close] = brackets\n if (items.length === 0) return `${open}${close}`\n if (!items.some((item) => item.includes('\\n'))) return `${open}${items.join(', ')}${close}`\n const body = items.map((item) => `${indentLines(item)},`).join('\\n')\n\n return `${open}\\n${body}\\n${close}`\n}\n","import { narrowSchema } from '../guards.ts'\nimport { createSchema, type SchemaNode } from '../nodes/schema.ts'\n\n/**\n * Merges a run of adjacent anonymous object members into one. Named or non-object members break the\n * run and pass through. Stays a construction-time helper, not a macro, so callers keep control of the\n * member boundaries (such as keeping synthetic discriminant objects out of a run).\n *\n * @example\n * ```ts\n * const merged = [...mergeAdjacentObjectsLazy([objectA, objectB])]\n * ```\n */\nexport function* mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined> {\n let acc: SchemaNode | undefined\n\n for (const member of members) {\n const objectMember = narrowSchema(member, 'object')\n if (objectMember && !objectMember.name && acc !== undefined) {\n const accObject = narrowSchema(acc, 'object')\n if (accObject && !accObject.name) {\n acc = createSchema({\n ...accObject,\n properties: [...(accObject.properties ?? []), ...(objectMember.properties ?? [])],\n })\n continue\n }\n }\n if (acc !== undefined) yield acc\n acc = member\n }\n\n if (acc !== undefined) yield acc\n}\n","/**\n * Strips a single matching pair of `\"...\"`, `'...'`, or `` `...` `` from both ends of `text`.\n * Returns the string unchanged when no balanced quote pair is found.\n *\n * @example\n * ```ts\n * trimQuotes('\"hello\"') // 'hello'\n * trimQuotes('hello') // 'hello'\n * ```\n */\nexport function trimQuotes(text: string): string {\n if (text.length >= 2) {\n const first = text[0]\n const last = text[text.length - 1]\n if ((first === '\"' && last === '\"') || (first === \"'\" && last === \"'\") || (first === '`' && last === '`')) {\n return text.slice(1, -1)\n }\n }\n return text\n}\n\n/**\n * Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.\n *\n * Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated\n * code matches the repo style without a formatter.\n *\n * @example\n * ```ts\n * stringify('hello') // \"'hello'\"\n * stringify('\"hello\"') // \"'hello'\"\n * ```\n */\nexport function stringify(value: string | number | boolean | undefined): string {\n if (value === undefined || value === null) return \"''\"\n const json = JSON.stringify(trimQuotes(value.toString()))\n const inner = json.slice(1, -1).replace(/\\\\\"/g, '\"').replace(/'/g, \"\\\\'\")\n return `'${inner}'`\n}\n\n/**\n * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,\n * and the Unicode line terminators U+2028 and U+2029.\n *\n * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4\n *\n * @example\n * ```ts\n * jsStringEscape('say \"hi\"\\nbye') // 'say \\\\\"hi\\\\\"\\\\nbye'\n * ```\n */\nexport function jsStringEscape(input: unknown): string {\n return `${input}`.replace(/[\"'\\\\\\n\\r\\u2028\\u2029]/g, (character) => {\n switch (character) {\n case '\"':\n case \"'\":\n case '\\\\':\n return `\\\\${character}`\n case '\\n':\n return '\\\\n'\n case '\\r':\n return '\\\\r'\n case '\\u2028':\n return '\\\\u2028'\n case '\\u2029':\n return '\\\\u2029'\n default:\n return ''\n }\n })\n}\n\n/**\n * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.\n * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.\n * Pass `null` as the second argument to emit a `/pattern/flags` literal instead.\n *\n * @example\n * ```ts\n * toRegExpString('^(?im)foo') // 'new RegExp(\"^foo\", \"im\")'\n * toRegExpString('^(?im)foo', null) // '/^foo/im'\n * ```\n */\nexport function toRegExpString(text: string, func: string | null = 'RegExp'): string {\n const raw = trimQuotes(text)\n\n const match = raw.match(/^\\^(\\(\\?([igmsuy]+)\\))/i)\n const replacementTarget = match?.[1] ?? ''\n const matchedFlags = match?.[2]\n const cleaned = raw\n .replace(/^\\\\?\\//, '')\n .replace(/\\\\?\\/$/, '')\n .replace(replacementTarget, '')\n\n const { source, flags } = new RegExp(cleaned, matchedFlags)\n\n if (func === null) return `/${source}/${flags}`\n\n return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ''})`\n}\n\n/**\n * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested\n * objects recurse with fixed indentation, so the result drops straight into an object literal\n * without re-parsing.\n *\n * @example\n * ```ts\n * stringifyObject({ foo: 'bar', nested: { a: 1 } })\n * // 'foo: bar,\\nnested: {\\n a: 1\\n }'\n * ```\n */\nexport function stringifyObject(value: Record<string, unknown>): string {\n const items = Object.entries(value)\n .map(([key, val]) => {\n if (val !== null && typeof val === 'object') {\n return `${key}: {\\n ${stringifyObject(val as Record<string, unknown>)}\\n }`\n }\n return `${key}: ${val}`\n })\n .filter(Boolean)\n return items.join(',\\n')\n}\n\n/**\n * Renders a dotted path or string array as an optional-chaining accessor expression rooted at\n * `accessor`. Returns `null` for an empty path.\n *\n * @example\n * ```ts\n * getNestedAccessor('pagination.next.id', 'lastPage')\n * // \"lastPage?.['pagination']?.['next']?.['id']\"\n * ```\n */\nexport function getNestedAccessor(param: string | Array<string>, accessor: string): string | null {\n const parts = Array.isArray(param) ? param : param.split('.')\n if (parts.length === 0 || (parts.length === 1 && parts[0] === '')) return null\n return `${accessor}?.['${`${parts.join(\"']?.['\")}']`}`\n}\n","import { memoize } from '@internals/utils'\nimport type { OperationNode, SchemaNode } from '../nodes/index.ts'\nimport { collect, collectLazy } from '../visitor.ts'\nimport { resolveRefName } from './refs.ts'\n\n/**\n * Collects every named schema referenced transitively from a node through its ref edges.\n *\n * Refs are followed by name only, so the resolved `node.schema` is never traversed inline.\n *\n * @example Collect refs from a single schema\n * ```ts\n * const names = collectReferencedSchemaNames(petSchema)\n * // → Set { 'Category', 'Tag' }\n * ```\n *\n * @example Accumulate refs from multiple schemas into one set\n * ```ts\n * const out = new Set<string>()\n * for (const schema of schemas) {\n * collectReferencedSchemaNames(schema, out)\n * }\n * ```\n */\nconst collectSchemaRefs = memoize(new WeakMap<SchemaNode, ReadonlySet<string>>(), (node: SchemaNode): ReadonlySet<string> => {\n const refs = new Set<string>()\n collect<void>(node, {\n schema(child) {\n if (child.type === 'ref') {\n const name = resolveRefName(child)\n if (name) refs.add(name)\n }\n },\n })\n return refs\n})\n\nexport function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {\n if (!node) return out\n for (const name of collectSchemaRefs(node)) out.add(name)\n return out\n}\n\n/**\n * Collects the names of all top-level schemas transitively used by a set of operations.\n *\n * An operation uses a schema when its parameters, request body, or responses reference it, directly\n * or through other named schemas. The walk is iterative, so reference cycles are safe.\n *\n * Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.\n *\n * @example Only generate schemas referenced by included operations\n * ```ts\n * const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)\n * const allowed = collectUsedSchemaNames(includedOps, schemas)\n *\n * for (const schema of schemas) {\n * if (schema.name && !allowed.has(schema.name)) continue\n * // … generate schema\n * }\n * ```\n */\nconst collectUsedSchemaNamesMemo = memoize(new WeakMap<ReadonlyArray<OperationNode>, (schemas: ReadonlyArray<SchemaNode>) => Set<string>>(), (ops) =>\n memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas) => computeUsedSchemaNames(ops, schemas)),\n)\n\nfunction computeUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n const schemaMap = new Map<string, SchemaNode>()\n for (const schema of schemas) {\n if (schema.name) schemaMap.set(schema.name, schema)\n }\n\n const result = new Set<string>()\n\n function visitSchema(schema: SchemaNode): void {\n const directRefs = collectReferencedSchemaNames(schema)\n for (const name of directRefs) {\n if (!result.has(name)) {\n result.add(name)\n const namedSchema = schemaMap.get(name)\n if (namedSchema) visitSchema(namedSchema)\n }\n }\n }\n\n for (const op of operations) {\n for (const schema of collectLazy<SchemaNode>(op, { depth: 'shallow', schema: (node) => node })) {\n visitSchema(schema)\n }\n }\n\n return result\n}\n\nexport function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n return collectUsedSchemaNamesMemo(operations)(schemas)\n}\n\nconst EMPTY_CIRCULAR_SET = new Set<string>()\n\nconst findCircularSchemasMemo = memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas: ReadonlyArray<SchemaNode>): Set<string> => {\n const graph = new Map<string, Set<string>>()\n\n for (const schema of schemas) {\n if (!schema.name) continue\n graph.set(schema.name, collectReferencedSchemaNames(schema))\n }\n\n const circular = new Set<string>()\n for (const start of graph.keys()) {\n const visited = new Set<string>()\n const stack: Array<string> = [...(graph.get(start) ?? [])]\n while (stack.length > 0) {\n const node = stack.pop()!\n if (node === start) {\n circular.add(start)\n break\n }\n if (visited.has(node)) continue\n visited.add(node)\n\n const next = graph.get(node)\n if (next) for (const r of next) stack.push(r)\n }\n }\n\n return circular\n})\n\n/**\n * Finds every schema that takes part in a circular dependency chain, including direct self-loops.\n *\n * Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so\n * the generated code does not recurse forever. Refs are followed by name only, so the walk stays\n * linear in the size of the schema graph.\n *\n * @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.\n */\nexport function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string> {\n if (schemas.length === 0) return EMPTY_CIRCULAR_SET\n return findCircularSchemasMemo(schemas)\n}\n\n/**\n * Returns `true` when a schema, or anything nested inside it, references a circular schema.\n *\n * Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled\n * on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.\n *\n * @note Stops at the first matching circular ref.\n */\nexport function containsCircularRef(\n node: SchemaNode | undefined,\n { circularSchemas, excludeName }: { circularSchemas: ReadonlySet<string>; excludeName?: string },\n): boolean {\n if (!node || circularSchemas.size === 0) return false\n\n for (const _ of collectLazy<true>(node, {\n schema(child) {\n if (child.type !== 'ref') return null\n const name = resolveRefName(child)\n return name && name !== excludeName && circularSchemas.has(name) ? true : null\n },\n })) {\n return true\n }\n\n return false\n}\n","import type { ArraySchemaNode, IntersectionSchemaNode, ObjectSchemaNode, PropertyNode, SchemaNode, UnionSchemaNode } from '../nodes/index.ts'\nimport { objectKey } from './codegen.ts'\n\n/**\n * Converts a child schema to printer output. Plugins instantiate it with their own output type:\n * `string` for the zod and faker printers, `ts.TypeNode` for the TypeScript printer. A printer's\n * `this.transform` fits directly, so its `null` for an empty result carries through to `output`.\n */\nexport type SchemaTransform<TOutput> = (schema: SchemaNode) => TOutput\n\n/**\n * A union or intersection member, or an array or tuple item, paired with its transformed output.\n */\nexport type MappedSchema<TOutput> = {\n /**\n * The original child schema, kept so the printer can read its metadata for leaf formatting.\n */\n schema: SchemaNode\n /**\n * The child schema after being run through the transform.\n */\n output: TOutput\n}\n\n/**\n * An object property paired with its transformed output.\n */\nexport type MappedProperty<TOutput> = {\n /**\n * The property name as written on the schema, before any identifier quoting.\n */\n name: string\n /**\n * The original property node, kept so the printer can read `required`, `schema`, and metadata.\n */\n property: PropertyNode\n /**\n * The property schema after being run through the transform.\n */\n output: TOutput\n}\n\n/**\n * Maps each property of an object schema to its transformed output. Pairs every result with the\n * original property so the printer keeps full control over modifiers, getters, and key syntax.\n *\n * @example\n * ```ts\n * const entries = mapSchemaProperties(node, (schema) => this.transform(schema))\n * // entries: [{ name: 'id', property, output: 'z.number()' }, ...]\n * ```\n */\nexport function mapSchemaProperties<TOutput>(node: ObjectSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedProperty<TOutput>> {\n return node.properties.map((property) => ({ name: property.name, property, output: transform(property.schema) }))\n}\n\n/**\n * Maps each member of a union or intersection schema to its transformed output, pairing every\n * result with the original member.\n */\nexport function mapSchemaMembers<TOutput>(node: UnionSchemaNode | IntersectionSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> {\n return (node.members ?? []).map((schema) => ({ schema, output: transform(schema) }))\n}\n\n/**\n * Maps each item of an array or tuple schema to its transformed output, pairing every result with\n * the original item.\n */\nexport function mapSchemaItems<TOutput>(node: ArraySchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> {\n return (node.items ?? []).map((schema) => ({ schema, output: transform(schema) }))\n}\n\n/**\n * Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key\n * is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation\n * of a recursive schema until first access.\n *\n * @example\n * ```ts\n * lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })\n * // \"get parent() { return z.lazy(() => Pet) }\"\n * ```\n */\nexport function lazyGetter({ name, body }: { name: string; body: string }): string {\n return `get ${objectKey(name)}() { return ${body} }`\n}\n","import { camelCase, isValidVarName, memoize } from '@internals/utils'\nimport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createTypeLiteral } from '../nodes/function.ts'\nimport type { FunctionParameterNode, FunctionParametersNode, OperationNode, ParameterNode, TypeExpression, TypeLiteralNode } from '../nodes/index.ts'\nimport { resolveGroupType } from './refs.ts'\n\n/**\n * Applies casing rules to parameter names and returns a new array without mutating the input.\n *\n * Run it before handing parameters to schema builders so output property keys get the right casing\n * while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the\n * original array is returned unchanged.\n */\nconst caseParamsMemo = memoize(new WeakMap<Array<ParameterNode>, (casing: string) => Array<ParameterNode>>(), (params) =>\n memoize(new Map<string, Array<ParameterNode>>(), (casing: string) =>\n params.map((param) => {\n const transformed = casing === 'camelcase' || !isValidVarName(param.name) ? camelCase(param.name) : param.name\n return { ...param, name: transformed }\n }),\n ),\n)\n\nexport function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {\n if (!casing) return params\n return caseParamsMemo(params)(casing)\n}\n\n/**\n * Named type for a group of parameters (query or header) emitted as a single typed parameter.\n */\nexport type ParamGroupType = {\n /**\n * Type expression for the group, a plain group-name reference.\n */\n type: TypeExpression\n /**\n * Whether the parameter group is optional.\n */\n optional: boolean\n}\n\n/**\n * A single member of a destructured parameter group, fed to `createFunctionParameter({ properties })`.\n */\ntype GroupProperty = {\n name: string\n type: TypeExpression\n optional?: boolean\n}\n\n/**\n * Resolver interface for {@link createOperationParams}.\n *\n * `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.\n */\nexport type OperationParamsResolver = {\n /**\n * Resolves the type name for an individual parameter.\n *\n * @example Individual path parameter name\n * `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`\n */\n resolveParamName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the request body type name.\n *\n * @example Request body type name\n * `resolver.resolveDataName(node) // → 'CreatePetData'`\n */\n resolveDataName(node: OperationNode): string\n /**\n * Resolves the grouped path parameters type name.\n * When the return value equals `resolveParamName`, no indexed access is emitted.\n *\n * @example Grouped path params type name\n * `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`\n */\n resolvePathParamsName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the grouped query parameters type name.\n * When the return value equals `resolveParamName`, an inline struct type is emitted instead.\n *\n * @example Grouped query params type name\n * `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`\n */\n resolveQueryParamsName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the grouped header parameters type name.\n * When the return value equals `resolveParamName`, an inline struct type is emitted instead.\n *\n * @example Grouped header params type name\n * `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`\n */\n resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string\n}\n\n/**\n * Options for {@link createOperationParams}.\n */\nexport type CreateOperationParamsOptions = {\n /**\n * How all operation parameters are grouped in the function signature.\n * - `'object'` wraps all params into a single destructured object `{ petId, data, params }`\n * - `'inline'` emits each param category as a separate top-level parameter\n */\n paramsType: 'object' | 'inline'\n /**\n * How path parameters are emitted when `paramsType` is `'inline'`.\n * - `'object'` groups them as `{ petId, storeId }: PathParams`\n * - `'inline'` spreads them as individual parameters `petId: string, storeId: string`\n * - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`\n */\n pathParamsType: 'object' | 'inline' | 'inlineSpread'\n /**\n * Converts parameter names to camelCase before output.\n */\n paramsCasing?: 'camelcase'\n /**\n * Resolver for parameter and request body type names.\n * Pass `ResolverTs` from `@kubb/plugin-ts` directly.\n * When omitted, falls back to the schema primitive or `'unknown'`.\n */\n resolver?: OperationParamsResolver\n /**\n * Default value for the path parameters binding when `pathParamsType` is `'object'`.\n * Falls back to `'{}'` when all path params are optional.\n */\n pathParamsDefault?: string\n /**\n * Extra parameters appended after the standard operation parameters.\n *\n * @example Plugin-specific trailing parameter\n * ```ts\n * extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]\n * ```\n */\n extraParams?: Array<FunctionParameterNode>\n /**\n * Override the default parameter names used for body, query, header, and rest-path groups.\n *\n * Useful when targeting languages or frameworks with different naming conventions.\n *\n * @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }\n */\n paramNames?: {\n /**\n * Name for the request body parameter.\n * @default 'data'\n */\n data?: string\n /**\n * Name for the query parameters group parameter.\n * @default 'params'\n */\n params?: string\n /**\n * Name for the header parameters group parameter.\n * @default 'headers'\n */\n headers?: string\n /**\n * Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.\n * @default 'pathParams'\n */\n path?: string\n }\n /**\n * Transforms every resolved type name before it lands in a parameter node, for framework-level\n * type wrappers.\n *\n * @example Vue Query, wrap every parameter type with `MaybeRefOrGetter`\n * `typeWrapper: (t) => \\`MaybeRefOrGetter<${t}>\\``\n */\n typeWrapper?: (type: string) => string\n}\n\n/**\n * Resolves the {@link TypeExpression} for an individual parameter.\n *\n * Without a resolver, it falls back to the schema primitive (a plain type-name string). When the\n * parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like\n * `GroupParams['petId']`, otherwise the resolved individual name.\n */\nexport function resolveParamType({\n node,\n param,\n resolver,\n}: {\n node: OperationNode\n param: ParameterNode\n resolver: OperationParamsResolver | undefined\n}): TypeExpression {\n if (!resolver) {\n return param.schema.primitive ?? 'unknown'\n }\n\n const individualName = resolver.resolveParamName(node, param)\n\n const groupLocation = param.in === 'path' || param.in === 'query' || param.in === 'header' ? param.in : undefined\n\n const groupResolvers = {\n path: resolver.resolvePathParamsName,\n query: resolver.resolveQueryParamsName,\n header: resolver.resolveHeaderParamsName,\n } as const\n\n const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : undefined\n\n if (groupName && groupName !== individualName) {\n return createIndexedAccessType({ target: groupName, key: param.name })\n }\n\n return individualName\n}\n\n/**\n * Converts an `OperationNode` into function parameters for code generation.\n *\n * Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one\n * destructured object parameter (`object`) and separate top-level parameters (`inline`), while\n * `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type\n * name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.\n */\nexport function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode {\n const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options\n\n const dataName = paramNames?.data ?? 'data'\n const paramsName = paramNames?.params ?? 'params'\n const headersName = paramNames?.headers ?? 'headers'\n const pathName = paramNames?.path ?? 'pathParams'\n\n const wrapType = (type: string): string => (typeWrapper ? typeWrapper(type) : type)\n // Only plain type-name references are wrapped, they need casing applied.\n // TypeLiteral and IndexedAccessType expressions are pre-resolved and pass through unchanged.\n const wrapTypeExpression = (type: TypeExpression): TypeExpression => (typeof type === 'string' ? wrapType(type) : type)\n\n const casedParams = caseParams(node.parameters, paramsCasing)\n const pathParams = casedParams.filter((p) => p.in === 'path')\n const queryParams = casedParams.filter((p) => p.in === 'query')\n const headerParams = casedParams.filter((p) => p.in === 'header')\n\n const toProperty = (param: ParameterNode): GroupProperty => ({\n name: param.name,\n type: wrapTypeExpression(resolveParamType({ node, param, resolver })),\n optional: !param.required,\n })\n const emptyObjectDefault = (props: Array<GroupProperty>): string | undefined => (props.every((p) => p.optional) ? '{}' : undefined)\n\n const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? 'unknown') : undefined\n const bodyProperty: Array<GroupProperty> = bodyType ? [{ name: dataName, type: bodyType, optional: !(node.requestBody?.required ?? false) }] : []\n\n const trailingGroups: Array<BuildGroupArgs> = [\n { name: paramsName, node, params: queryParams, groupType: resolveGroupType({ node, params: queryParams, group: 'query', resolver }), resolver, wrapType },\n {\n name: headersName,\n node,\n params: headerParams,\n groupType: resolveGroupType({ node, params: headerParams, group: 'header', resolver }),\n resolver,\n wrapType,\n },\n ]\n\n const params: Array<FunctionParameterNode> = []\n\n if (paramsType === 'object') {\n const children = [...pathParams.map(toProperty), ...bodyProperty, ...trailingGroups.flatMap(buildGroupProperty)]\n if (children.length) {\n params.push(createFunctionParameter({ properties: children, default: emptyObjectDefault(children) }))\n }\n } else {\n if (pathParamsType === 'inlineSpread' && pathParams.length) {\n const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]!)\n params.push(createFunctionParameter({ name: pathName, type: spreadType ? wrapType(spreadType) : undefined, rest: true }))\n } else if (pathParamsType === 'inline') {\n params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))))\n } else if (pathParams.length) {\n const pathChildren = pathParams.map(toProperty)\n params.push(createFunctionParameter({ properties: pathChildren, default: pathParamsDefault ?? emptyObjectDefault(pathChildren) }))\n }\n\n params.push(...bodyProperty.map((p) => createFunctionParameter(p)))\n params.push(...trailingGroups.flatMap(buildGroupParam))\n }\n\n params.push(...extraParams)\n\n return createFunctionParameters({ params })\n}\n\n/**\n * Shared arguments for building a query or header parameter group.\n */\nexport type BuildGroupArgs = {\n name: string\n node: OperationNode\n params: Array<ParameterNode>\n groupType: ParamGroupType | null\n resolver: OperationParamsResolver | undefined\n wrapType: (type: string) => string\n}\n\n/**\n * Builds the property descriptor for a query or header group.\n * Returns an empty array when there are no params to emit.\n *\n * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline\n * {@link TypeLiteralNode} from the individual params.\n */\nfunction buildGroupProperty({ name, node, params, groupType, resolver, wrapType }: BuildGroupArgs): Array<GroupProperty> {\n if (groupType) {\n const type = typeof groupType.type === 'string' ? wrapType(groupType.type) : groupType.type\n return [{ name, type, optional: groupType.optional }]\n }\n if (params.length) {\n return [{ name, type: buildTypeLiteral({ node, params, resolver }), optional: params.every((p) => !p.required) }]\n }\n return []\n}\n\n/**\n * Builds a single {@link FunctionParameterNode} for a query or header group.\n * Returns an empty array when there are no params to emit.\n *\n * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline\n * {@link TypeLiteralNode} from the individual params.\n */\nexport function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode> {\n return buildGroupProperty(args).map((p) => createFunctionParameter(p))\n}\n\n/**\n * Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.\n *\n * Used when query or header parameters have no dedicated group type name.\n * Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).\n */\nexport function buildTypeLiteral({\n node,\n params,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n resolver: OperationParamsResolver | undefined\n}): TypeLiteralNode {\n return createTypeLiteral({\n members: params.map((p) => ({\n name: p.name,\n type: resolveParamType({ node, param: p, resolver }),\n optional: !p.required,\n })),\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6IA,SAAgB,QAAsB,OAA4B,SAAuD;CACvH,QAAQ,QAAsB;EAC5B,IAAI,MAAM,IAAI,GAAG,GAAG,OAAO,MAAM,IAAI,GAAG;EACxC,MAAM,QAAQ,QAAQ,GAAG;EACzB,MAAM,IAAI,KAAK,KAAK;EACpB,OAAO;C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AAC;EACnD,UAAU,CAAC,EAAE;CACf,EAAE,EACJ,CAAC;AACH"}
const require_response = require("./response-BeG6hq2D.cjs");
//#region src/constants.ts
const visitorDepths = {
shallow: "shallow",
deep: "deep"
};
/**
* Schema type discriminators used by all AST schema nodes.
*
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/
const schemaTypes = {
/**
* Text value.
*/
string: "string",
/**
* Floating-point number (`float`, `double`).
*/
number: "number",
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
integer: "integer",
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
bigint: "bigint",
/**
* Boolean value.
*/
boolean: "boolean",
/**
* Explicit null value.
*/
null: "null",
/**
* Any value (no type restriction).
*/
any: "any",
/**
* Unknown value (must be narrowed before usage).
*/
unknown: "unknown",
/**
* No return value (`void`).
*/
void: "void",
/**
* Object with named properties.
*/
object: "object",
/**
* Sequential list of items.
*/
array: "array",
/**
* Fixed-length list with position-specific items.
*/
tuple: "tuple",
/**
* "One of" multiple schema members.
*/
union: "union",
/**
* "All of" multiple schema members.
*/
intersection: "intersection",
/**
* Enum schema.
*/
enum: "enum",
/**
* Reference to another schema.
*/
ref: "ref",
/**
* Calendar date (for example `2026-03-24`).
*/
date: "date",
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
datetime: "datetime",
/**
* Time-only value (for example `09:00:00`).
*/
time: "time",
/**
* UUID value.
*/
uuid: "uuid",
/**
* Email address value.
*/
email: "email",
/**
* URL value.
*/
url: "url",
/**
* IPv4 address value.
*/
ipv4: "ipv4",
/**
* IPv6 address value.
*/
ipv6: "ipv6",
/**
* Binary/blob value.
*/
blob: "blob",
/**
* Impossible value (`never`).
*/
never: "never"
};
/**
* One indentation level, derived from {@link INDENT_SIZE}.
*/
const INDENT = Array.from({ length: 2 }, () => " ").join("");
//#endregion
//#region src/guards.ts
/**
* Narrows a `SchemaNode` to the variant that matches `type`.
*
* @example
* ```ts
* const schema = createSchema({ type: 'string' })
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
* ```
*/
function narrowSchema(node, type) {
return node?.type === type ? node : null;
}
/**
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
*
* @example
* ```ts
* if (isHttpOperationNode(node)) {
* console.log(node.method, node.path)
* }
* ```
*/
function isHttpOperationNode(node) {
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
}
//#endregion
//#region src/utils/refs.ts
const plainStringTypes = new Set([
"string",
"uuid",
"email",
"url",
"datetime"
]);
/**
* Returns the last path segment of a reference string.
*
* @example
* ```ts
* extractRefName('#/components/schemas/Pet') // 'Pet'
* ```
*/
function extractRefName(ref) {
return ref.split("/").at(-1) ?? ref;
}
/**
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
*
* Returns `null` for non-ref nodes or when no name resolves.
*
* @example
* ```ts
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
* // => 'Pet'
* ```
*/
function resolveRefName(node) {
if (!node || node.type !== "ref") return null;
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
return node.name ?? node.schema?.name ?? null;
}
/**
* Builds a PascalCase child schema name by joining a parent name and property name.
* Returns `null` when there is no parent to nest under.
*
* @example
* ```ts
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
* childName(undefined, 'params') // null
* ```
*/
function childName(parentName, propName) {
return parentName ? require_response.pascalCase([parentName, propName].join(" ")) : null;
}
/**
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
* empty parts.
*
* @example
* ```ts
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
* ```
*/
function enumPropName(parentName, propName, enumSuffix) {
return require_response.pascalCase([
parentName,
propName,
enumSuffix
].filter(Boolean).join(" "));
}
/**
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
*
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override
* the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
*
* @example
* ```ts
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
* ```
*/
function syncSchemaRef(node) {
const ref = narrowSchema(node, "ref");
if (!ref) return node;
if (!ref.schema) return node;
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
return require_response.createSchema({
...ref.schema,
...definedOverrides
});
}
/**
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
function isStringType(node) {
if (plainStringTypes.has(node.type)) return true;
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
if (temporal) return temporal.representation !== "date";
return false;
}
/**
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
*
* Returns `null` when there is no resolver, no params, or the group name equals the
* individual param name (so there is no real group to emit).
*/
function resolveGroupType({ node, params, group, resolver }) {
if (!resolver || !params.length) return null;
const firstParam = params[0];
const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam);
if (groupName === resolver.resolveParamName(node, firstParam)) return null;
return {
type: groupName,
optional: params.every((p) => !p.required)
};
}
//#endregion
//#region src/registry.ts
/**
* Every node definition. Adding a node means adding its `defineNode` to one
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
*/
const nodeDefs = [
require_response.inputDef,
require_response.outputDef,
require_response.operationDef,
require_response.requestBodyDef,
require_response.contentDef,
require_response.responseDef,
require_response.schemaDef,
require_response.propertyDef,
require_response.parameterDef,
require_response.functionParameterDef,
require_response.functionParametersDef,
require_response.typeLiteralDef,
require_response.indexedAccessTypeDef,
require_response.objectBindingPatternDef,
require_response.constDef,
require_response.typeDef,
require_response.functionDef,
require_response.arrowFunctionDef,
require_response.textDef,
require_response.breakDef,
require_response.jsxDef,
require_response.importDef,
require_response.exportDef,
require_response.sourceDef,
require_response.fileDef
];
//#endregion
//#region src/visitor.ts
/**
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
* Derived from each definition's `children`.
*/
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
/**
* Maps a node kind to the matching visitor callback name. Derived from each
* definition's `visitorKey`.
*/
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
/**
* Creates a small async concurrency limiter.
*
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
*
* @example
* ```ts
* const limit = createLimit(2)
* for (const task of [taskA, taskB, taskC]) {
* await limit(() => task())
* }
* // only 2 tasks run at the same time
* ```
*/
function createLimit(concurrency) {
let active = 0;
const queue = [];
function next() {
if (active < concurrency && queue.length > 0) {
active++;
queue.shift()();
}
}
return function limit(fn) {
return new Promise((resolve, reject) => {
queue.push(() => {
Promise.resolve(fn()).then(resolve, reject).finally(() => {
active--;
next();
});
});
next();
});
};
}
const visitorKeysByKind = VISITOR_KEYS;
/**
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
*/
function isNode(value) {
return typeof value === "object" && value !== null && "kind" in value;
}
/**
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
*
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
*
* @example
* ```ts
* const children = getChildren(operationNode, true)
* // returns parameters, the request body, and responses
* ```
*/
function* getChildren(node, recurse) {
if (node.kind === "Schema" && !recurse) return;
const keys = visitorKeysByKind[node.kind];
if (!keys) return;
const record = node;
for (const key of keys) {
const value = record[key];
if (Array.isArray(value)) {
for (const item of value) if (isNode(item)) yield item;
} else if (isNode(value)) yield value;
}
}
/**
* Runs the visitor callback that matches `node.kind` with the traversal
* context. The result is a replacement node, a collected value, or `undefined`
* when no callback is registered for the kind.
*
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
* in one place. `TResult` is the caller's expected return: the same node type
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
*/
function applyVisitor(node, visitor, parent) {
const key = VISITOR_KEY_BY_KIND[node.kind];
if (!key) return void 0;
const fn = visitor[key];
return fn?.(node, { parent });
}
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
async function walk(node, options) {
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
}
async function _walk(node, visitor, recurse, limit, parent) {
await limit(() => applyVisitor(node, visitor, parent));
const children = Array.from(getChildren(node, recurse));
if (children.length === 0) return;
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
}
function transform(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
return transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
}
/**
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
* `Schema` children are skipped in shallow mode.
*/
function transformChildren(node, options, recurse) {
if (node.kind === "Schema" && !recurse) return node;
const keys = visitorKeysByKind[node.kind];
if (!keys) return node;
const record = node;
const childOptions = {
...options,
parent: node
};
let updates;
for (const key of keys) {
if (!(key in record)) continue;
const value = record[key];
if (Array.isArray(value)) {
let changed = false;
const mapped = value.map((item) => {
if (!isNode(item)) return item;
const next = transform(item, childOptions);
if (next !== item) changed = true;
return next;
});
if (changed) (updates ??= {})[key] = mapped;
} else if (isNode(value)) {
const next = transform(value, childOptions);
if (next !== value) (updates ??= {})[key] = next;
}
}
return updates ? {
...node,
...updates
} : node;
}
/**
* Lazy depth-first collection pass. Yields every non-null value returned by
* the visitor callbacks. Use `collect` for the eager array form.
*
* @example Collect every operationId
* ```ts
* const ids: string[] = []
* for (const id of collectLazy<string>(root, {
* operation(node) {
* return node.operationId
* },
* })) {
* ids.push(id)
* }
* ```
*/
function* collectLazy(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const v = applyVisitor(node, visitor, parent);
if (v != null) yield v;
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
...options,
parent: node
});
}
/**
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
function collect(node, options) {
return Array.from(collectLazy(node, options));
}
//#endregion
Object.defineProperty(exports, "INDENT", {
enumerable: true,
get: function() {
return INDENT;
}
});
Object.defineProperty(exports, "childName", {
enumerable: true,
get: function() {
return childName;
}
});
Object.defineProperty(exports, "collect", {
enumerable: true,
get: function() {
return collect;
}
});
Object.defineProperty(exports, "collectLazy", {
enumerable: true,
get: function() {
return collectLazy;
}
});
Object.defineProperty(exports, "enumPropName", {
enumerable: true,
get: function() {
return enumPropName;
}
});
Object.defineProperty(exports, "extractRefName", {
enumerable: true,
get: function() {
return extractRefName;
}
});
Object.defineProperty(exports, "isHttpOperationNode", {
enumerable: true,
get: function() {
return isHttpOperationNode;
}
});
Object.defineProperty(exports, "isStringType", {
enumerable: true,
get: function() {
return isStringType;
}
});
Object.defineProperty(exports, "narrowSchema", {
enumerable: true,
get: function() {
return narrowSchema;
}
});
Object.defineProperty(exports, "nodeDefs", {
enumerable: true,
get: function() {
return nodeDefs;
}
});
Object.defineProperty(exports, "resolveGroupType", {
enumerable: true,
get: function() {
return resolveGroupType;
}
});
Object.defineProperty(exports, "resolveRefName", {
enumerable: true,
get: function() {
return resolveRefName;
}
});
Object.defineProperty(exports, "schemaTypes", {
enumerable: true,
get: function() {
return schemaTypes;
}
});
Object.defineProperty(exports, "syncSchemaRef", {
enumerable: true,
get: function() {
return syncSchemaRef;
}
});
Object.defineProperty(exports, "transform", {
enumerable: true,
get: function() {
return transform;
}
});
Object.defineProperty(exports, "walk", {
enumerable: true,
get: function() {
return walk;
}
});
//# sourceMappingURL=visitor-B7Zlnp4c.cjs.map
{"version":3,"file":"visitor-B7Zlnp4c.cjs","names":["pascalCase","createSchema","inputDef","outputDef","operationDef","requestBodyDef","contentDef","responseDef","schemaDef","propertyDef","parameterDef","functionParameterDef","functionParametersDef","typeLiteralDef","indexedAccessTypeDef","objectBindingPatternDef","constDef","typeDef","functionDef","arrowFunctionDef","textDef","breakDef","jsxDef","importDef","exportDef","sourceDef","fileDef"],"sources":["../src/constants.ts","../src/guards.ts","../src/utils/refs.ts","../src/registry.ts","../src/visitor.ts"],"sourcesContent":["import type { SchemaType } from './nodes/schema.ts'\n\n/**\n * Traversal depth for AST visitor utilities.\n *\n * - `'shallow'` visits only the immediate node, skipping children.\n * - `'deep'` recursively visits all descendant nodes.\n */\nexport type VisitorDepth = 'shallow' | 'deep'\n\nexport const visitorDepths = {\n shallow: 'shallow',\n deep: 'deep',\n} as const satisfies Record<VisitorDepth, VisitorDepth>\n\n/**\n * Schema type discriminators used by all AST schema nodes.\n *\n * Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).\n * Call `isScalarPrimitive()` to check for the scalar types.\n */\nexport const schemaTypes = {\n /**\n * Text value.\n */\n string: 'string',\n /**\n * Floating-point number (`float`, `double`).\n */\n number: 'number',\n /**\n * Whole number (`int32`). Use `bigint` for `int64`.\n */\n integer: 'integer',\n /**\n * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.\n */\n bigint: 'bigint',\n /**\n * Boolean value.\n */\n boolean: 'boolean',\n /**\n * Explicit null value.\n */\n null: 'null',\n /**\n * Any value (no type restriction).\n */\n any: 'any',\n /**\n * Unknown value (must be narrowed before usage).\n */\n unknown: 'unknown',\n /**\n * No return value (`void`).\n */\n void: 'void',\n /**\n * Object with named properties.\n */\n object: 'object',\n /**\n * Sequential list of items.\n */\n array: 'array',\n /**\n * Fixed-length list with position-specific items.\n */\n tuple: 'tuple',\n /**\n * \"One of\" multiple schema members.\n */\n union: 'union',\n /**\n * \"All of\" multiple schema members.\n */\n intersection: 'intersection',\n /**\n * Enum schema.\n */\n enum: 'enum',\n /**\n * Reference to another schema.\n */\n ref: 'ref',\n /**\n * Calendar date (for example `2026-03-24`).\n */\n date: 'date',\n /**\n * Date-time value (for example `2026-03-24T09:00:00Z`).\n */\n datetime: 'datetime',\n /**\n * Time-only value (for example `09:00:00`).\n */\n time: 'time',\n /**\n * UUID value.\n */\n uuid: 'uuid',\n /**\n * Email address value.\n */\n email: 'email',\n /**\n * URL value.\n */\n url: 'url',\n /**\n * IPv4 address value.\n */\n ipv4: 'ipv4',\n /**\n * IPv6 address value.\n */\n ipv6: 'ipv6',\n /**\n * Binary/blob value.\n */\n blob: 'blob',\n /**\n * Impossible value (`never`).\n */\n never: 'never',\n} as const satisfies Record<SchemaType, SchemaType>\n\n/**\n * Default concurrency limit for the `walk()` traversal utility.\n *\n * Set to 30 to balance I/O-bound resolver parallelism against event-loop and memory pressure\n * during large spec traversals. Override it for different hardware constraints.\n *\n * @example\n * ```ts\n * import { walk, WALK_CONCURRENCY } from '@kubb/ast'\n *\n * walk(root, { concurrency: WALK_CONCURRENCY, root: () => {} })\n * ```\n */\nexport const WALK_CONCURRENCY = 30\n\n/**\n * Number of spaces in one indentation level when assembling multi-line code as strings.\n * Set to 2, 3, … to change the indent width used by `buildObject`/`buildList`.\n */\nconst INDENT_SIZE = 2\n\n/**\n * One indentation level, derived from {@link INDENT_SIZE}.\n */\nexport const INDENT = Array.from({ length: INDENT_SIZE }, () => ' ').join('')\n","import type { HttpOperationNode, OperationNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'\n\n/**\n * Narrows a `SchemaNode` to the variant that matches `type`.\n *\n * @example\n * ```ts\n * const schema = createSchema({ type: 'string' })\n * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null\n * ```\n */\nexport function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null {\n return node?.type === type ? (node as SchemaNodeByType[T]) : null\n}\n\n/**\n * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.\n *\n * @example\n * ```ts\n * if (isHttpOperationNode(node)) {\n * console.log(node.method, node.path)\n * }\n * ```\n */\nexport function isHttpOperationNode(node: OperationNode): node is HttpOperationNode {\n return node.protocol === 'http' || (node.method !== undefined && node.path !== undefined)\n}\n","import { pascalCase } from '@internals/utils'\nimport { narrowSchema } from '../guards.ts'\nimport type { OperationNode, ParameterNode, SchemaNode } from '../nodes/index.ts'\nimport { createSchema, type SchemaType } from '../nodes/schema.ts'\nimport type { OperationParamsResolver, ParamGroupType } from './operationParams.ts'\n\nconst plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)\n\n/**\n * Returns the last path segment of a reference string.\n *\n * @example\n * ```ts\n * extractRefName('#/components/schemas/Pet') // 'Pet'\n * ```\n */\nexport function extractRefName(ref: string): string {\n return ref.split('/').at(-1) ?? ref\n}\n\n/**\n * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.\n *\n * Returns `null` for non-ref nodes or when no name resolves.\n *\n * @example\n * ```ts\n * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })\n * // => 'Pet'\n * ```\n */\nexport function resolveRefName(node: SchemaNode | undefined): string | null {\n if (!node || node.type !== 'ref') return null\n if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null\n\n return node.name ?? node.schema?.name ?? null\n}\n\n/**\n * Builds a PascalCase child schema name by joining a parent name and property name.\n * Returns `null` when there is no parent to nest under.\n *\n * @example\n * ```ts\n * childName('Order', 'shipping_address') // 'OrderShippingAddress'\n * childName(undefined, 'params') // null\n * ```\n */\nexport function childName(parentName: string | null | undefined, propName: string): string | null {\n return parentName ? pascalCase([parentName, propName].join(' ')) : null\n}\n\n/**\n * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any\n * empty parts.\n *\n * @example\n * ```ts\n * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'\n * ```\n */\nexport function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {\n return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))\n}\n\n/**\n * Merges a ref node with its resolved schema, giving usage-site fields precedence.\n *\n * Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override\n * the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })\n * const merged = syncSchemaRef(ref) // merges with resolved Pet schema\n * ```\n */\nexport function syncSchemaRef(node: SchemaNode): SchemaNode {\n const ref = narrowSchema(node, 'ref')\n\n if (!ref) return node\n if (!ref.schema) return node\n\n const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref\n\n // Filter out undefined override values so they don't shadow the resolved schema's fields.\n const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))\n\n return createSchema({ ...ref.schema, ...definedOverrides })\n}\n\n/**\n * Type guard that returns `true` when a schema emits as a plain `string` type.\n *\n * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`\n * types, returns `true` only when `representation` is `'string'` rather than `'date'`.\n */\nexport function isStringType(node: SchemaNode): boolean {\n if (plainStringTypes.has(node.type)) {\n return true\n }\n\n const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')\n if (temporal) {\n return temporal.representation !== 'date'\n }\n\n return false\n}\n\n/**\n * Derives a {@link ParamGroupType} for a query or header group from the resolver.\n *\n * Returns `null` when there is no resolver, no params, or the group name equals the\n * individual param name (so there is no real group to emit).\n */\nexport function resolveGroupType({\n node,\n params,\n group,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n group: 'query' | 'header'\n resolver: OperationParamsResolver | undefined\n}): ParamGroupType | null {\n if (!resolver || !params.length) {\n return null\n }\n const firstParam = params[0]!\n const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName\n const groupName = groupMethod.call(resolver, node, firstParam)\n if (groupName === resolver.resolveParamName(node, firstParam)) {\n return null\n }\n return { type: groupName, optional: params.every((p) => !p.required) }\n}\n","import type { NodeDef } from './defineNode.ts'\nimport { arrowFunctionDef, breakDef, constDef, functionDef, jsxDef, textDef, typeDef } from './nodes/code.ts'\nimport { contentDef } from './nodes/content.ts'\nimport { exportDef, fileDef, importDef, sourceDef } from './nodes/file.ts'\nimport { functionParameterDef, functionParametersDef, indexedAccessTypeDef, objectBindingPatternDef, typeLiteralDef } from './nodes/function.ts'\nimport { inputDef } from './nodes/input.ts'\nimport { operationDef } from './nodes/operation.ts'\nimport { outputDef } from './nodes/output.ts'\nimport { parameterDef } from './nodes/parameter.ts'\nimport { propertyDef } from './nodes/property.ts'\nimport { requestBodyDef } from './nodes/requestBody.ts'\nimport { responseDef } from './nodes/response.ts'\nimport { schemaDef } from './nodes/schema.ts'\n\n// Surface every def from one place so the package barrel re-exports them with `export * from './registry.ts'`.\n// Adding a node means adding its `defineNode` to a `nodes/*.ts` file and listing it in `nodeDefs` below, nothing else.\nexport {\n arrowFunctionDef,\n breakDef,\n constDef,\n contentDef,\n exportDef,\n fileDef,\n functionDef,\n functionParameterDef,\n functionParametersDef,\n importDef,\n indexedAccessTypeDef,\n inputDef,\n jsxDef,\n objectBindingPatternDef,\n operationDef,\n outputDef,\n parameterDef,\n propertyDef,\n requestBodyDef,\n responseDef,\n schemaDef,\n sourceDef,\n textDef,\n typeDef,\n typeLiteralDef,\n}\n\n/**\n * Every node definition. Adding a node means adding its `defineNode` to one\n * `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.\n */\nexport const nodeDefs = [\n inputDef,\n outputDef,\n operationDef,\n requestBodyDef,\n contentDef,\n responseDef,\n schemaDef,\n propertyDef,\n parameterDef,\n functionParameterDef,\n functionParametersDef,\n typeLiteralDef,\n indexedAccessTypeDef,\n objectBindingPatternDef,\n constDef,\n typeDef,\n functionDef,\n arrowFunctionDef,\n textDef,\n breakDef,\n jsxDef,\n importDef,\n exportDef,\n sourceDef,\n fileDef,\n] satisfies ReadonlyArray<NodeDef>\n","import type { VisitorDepth } from './constants.ts'\nimport { visitorDepths, WALK_CONCURRENCY } from './constants.ts'\nimport type { NodeDef } from './defineNode.ts'\nimport type {\n ContentNode,\n InputNode,\n Node,\n NodeKind,\n OperationNode,\n OutputNode,\n ParameterNode,\n PropertyNode,\n RequestBodyNode,\n ResponseNode,\n SchemaNode,\n} from './nodes/index.ts'\nimport { nodeDefs } from './registry.ts'\n\n/**\n * Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).\n * Derived from each definition's `children`.\n */\nconst VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => (def.children ? [[def.kind, def.children] as const] : []))) as Partial<\n Record<NodeKind, ReadonlyArray<string>>\n>\n\n/**\n * Maps a node kind to the matching visitor callback name. Derived from each\n * definition's `visitorKey`.\n */\nconst VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => (def.visitorKey ? [[def.kind, def.visitorKey] as const] : []))) as Partial<\n Record<NodeKind, NonNullable<NodeDef['visitorKey']>>\n>\n\n/**\n * Creates a small async concurrency limiter.\n *\n * At most `concurrency` tasks are in flight at once. Extra tasks are queued.\n *\n * @example\n * ```ts\n * const limit = createLimit(2)\n * for (const task of [taskA, taskB, taskC]) {\n * await limit(() => task())\n * }\n * // only 2 tasks run at the same time\n * ```\n */\nfunction createLimit(concurrency: number) {\n let active = 0\n const queue: Array<() => void> = []\n\n function next() {\n if (active < concurrency && queue.length > 0) {\n active++\n queue.shift()!()\n }\n }\n\n return function limit<T>(fn: () => Promise<T> | T): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n queue.push(() => {\n Promise.resolve(fn())\n .then(resolve, reject)\n .finally(() => {\n active--\n next()\n })\n })\n next()\n })\n }\n}\n\ntype LimitFn = ReturnType<typeof createLimit>\n\n/**\n * Ordered mapping of `[NodeType, ParentType]` pairs.\n *\n * `ParentOf` uses this map to find parent types.\n */\ntype ParentNodeMap = [\n [InputNode, undefined],\n [OutputNode, undefined],\n [OperationNode, InputNode],\n [RequestBodyNode, OperationNode],\n [ContentNode, RequestBodyNode | ResponseNode],\n [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode],\n [PropertyNode, SchemaNode],\n [ParameterNode, OperationNode],\n [ResponseNode, OperationNode],\n]\n\n/**\n * Resolves the parent node type for a given AST node type.\n *\n * Visitor context relies on this so `ctx.parent` is typed for each callback.\n *\n * @example\n * ```ts\n * type InputParent = ParentOf<InputNode>\n * // undefined\n * ```\n *\n * @example\n * ```ts\n * type PropertyParent = ParentOf<PropertyNode>\n * // SchemaNode\n * ```\n *\n * @example\n * ```ts\n * type SchemaParent = ParentOf<SchemaNode>\n * // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode\n * ```\n */\nexport type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [\n infer TEntry extends [Node, unknown],\n ...infer TRest extends ReadonlyArray<[Node, unknown]>,\n]\n ? T extends TEntry[0]\n ? TEntry[1]\n : ParentOf<T, TRest>\n : Node\n\n/**\n * Traversal context passed as the second argument to every visitor callback.\n * `parent` is typed from the current node type.\n *\n * @example\n * ```ts\n * const visitor: Visitor = {\n * schema(node, { parent }) {\n * // parent type is narrowed by node kind\n * },\n * }\n * ```\n */\nexport type VisitorContext<T extends Node = Node> = {\n /**\n * Parent node of the currently visited node.\n * For `InputNode`, this is `undefined`.\n */\n parent?: ParentOf<T>\n}\n\n/**\n * Synchronous visitor consumed by `transform`. Each optional callback runs\n * for the matching node type. Return a new node to replace it, or `undefined`\n * to leave it untouched.\n *\n * Plugins typically expose `transformer` so users can supply a `Visitor` that\n * rewrites the AST before printing.\n *\n * @example Prefix every operationId\n * ```ts\n * const visitor: Visitor = {\n * operation(node) {\n * return { ...node, operationId: `api_${node.operationId}` }\n * },\n * }\n * ```\n *\n * @example Strip schema descriptions\n * ```ts\n * const visitor: Visitor = {\n * schema(node) {\n * return { ...node, description: undefined }\n * },\n * }\n * ```\n */\nexport type Visitor = {\n input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode\n output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode\n}\n\n/**\n * A visitor callback result that may be sync or async.\n */\ntype MaybePromise<T> = T | Promise<T>\n\n/**\n * Async visitor for `walk`. Synchronous `Visitor` objects are compatible.\n *\n * @example\n * ```ts\n * const visitor: AsyncVisitor = {\n * async operation(node) {\n * await Promise.resolve(node.operationId)\n * },\n * }\n * ```\n */\ntype AsyncVisitor = {\n input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>\n output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>\n}\n\n/**\n * Visitor used by `collect`.\n *\n * @example\n * ```ts\n * const visitor: CollectVisitor<string> = {\n * operation(node) {\n * return node.operationId\n * },\n * }\n * ```\n */\ntype CollectVisitor<T> = {\n input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined\n output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined\n}\n\n/**\n * Options for `transform`.\n *\n * @example\n * ```ts\n * const options: TransformOptions = { depth: 'deep', schema: (node) => node }\n * ```\n *\n * @example\n * ```ts\n * // Only transform the current node, not nested children\n * const options: TransformOptions = { depth: 'shallow', schema: (node) => node }\n * ```\n */\nexport type TransformOptions = Visitor & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Internal parent override used during recursion.\n */\n parent?: Node\n}\n\n/**\n * Options for `walk`.\n *\n * @example\n * ```ts\n * const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }\n * ```\n */\nexport type WalkOptions = AsyncVisitor & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Maximum number of sibling nodes visited concurrently.\n * @default 30\n */\n concurrency?: number\n}\n\n/**\n * Options for `collect`.\n *\n * @example\n * ```ts\n * const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }\n * ```\n */\nexport type CollectOptions<T> = CollectVisitor<T> & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Internal parent override used during recursion.\n */\n parent?: Node\n}\n\nconst visitorKeysByKind = VISITOR_KEYS as Record<string, ReadonlyArray<string> | undefined>\n\n/**\n * Returns `true` when `value` is an AST node (an object carrying a `kind`).\n */\nfunction isNode(value: unknown): value is Node {\n return typeof value === 'object' && value !== null && 'kind' in value\n}\n\n/**\n * Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.\n *\n * `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.\n *\n * @example\n * ```ts\n * const children = getChildren(operationNode, true)\n * // returns parameters, the request body, and responses\n * ```\n */\nfunction* getChildren(node: Node, recurse: boolean): Generator<Node, void, undefined> {\n if (node.kind === 'Schema' && !recurse) return\n\n const keys = visitorKeysByKind[node.kind]\n if (!keys) return\n\n const record = node as unknown as Record<string, unknown>\n for (const key of keys) {\n const value = record[key]\n if (Array.isArray(value)) {\n for (const item of value) if (isNode(item)) yield item\n } else if (isNode(value)) {\n yield value\n }\n }\n}\n\n/**\n * Runs the visitor callback that matches `node.kind` with the traversal\n * context. The result is a replacement node, a collected value, or `undefined`\n * when no callback is registered for the kind.\n *\n * Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives\n * in one place. `TResult` is the caller's expected return: the same node type\n * for `transform`, the collected value type for `collectLazy`, ignored for `walk`.\n */\nfunction applyVisitor<TResult>(node: Node, visitor: Visitor | AsyncVisitor | CollectVisitor<unknown>, parent: Node | undefined): TResult | null | undefined {\n const key = VISITOR_KEY_BY_KIND[node.kind]\n if (!key) return undefined\n\n const fn = visitor[key] as ((node: Node, context: VisitorContext) => TResult | null | undefined) | undefined\n\n return fn?.(node, { parent })\n}\n\n/**\n * Async depth-first traversal for side effects. Visitor return values are\n * ignored. Use `transform` when you want to rewrite nodes.\n *\n * Sibling nodes at each depth run concurrently up to `options.concurrency`\n * (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor\n * work. Lower values reduce memory pressure.\n *\n * @example Log every operation\n * ```ts\n * await walk(root, {\n * operation(node) {\n * console.log(node.operationId)\n * },\n * })\n * ```\n *\n * @example Only visit the root node\n * ```ts\n * await walk(root, { depth: 'shallow', input: () => {} })\n * ```\n */\nexport async function walk(node: Node, options: WalkOptions): Promise<void> {\n const recurse = (options.depth ?? visitorDepths.deep) === visitorDepths.deep\n const limit = createLimit(options.concurrency ?? WALK_CONCURRENCY)\n\n return _walk(node, options, recurse, limit, undefined)\n}\n\nasync function _walk(node: Node, visitor: AsyncVisitor, recurse: boolean, limit: LimitFn, parent: Node | undefined): Promise<void> {\n await limit(() => applyVisitor(node, visitor, parent))\n\n // Visit siblings concurrently and let the shared `limit` cap how many callbacks\n // run at once. Awaiting each child sequentially here would serialize the whole\n // traversal and make `concurrency` inert. Every visitor callback would run one\n // at a time regardless of the limit.\n const children = Array.from(getChildren(node, recurse))\n if (children.length === 0) return\n\n await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)))\n}\n\n/**\n * Synchronous depth-first transform. Each visitor callback can return a\n * replacement node. Returning `undefined` keeps the original.\n *\n * The original tree is never mutated, a new tree is returned. Pass\n * `depth: 'shallow'` to skip recursion into children.\n *\n * @example Prefix every operationId\n * ```ts\n * const next = transform(root, {\n * operation(node) {\n * return { ...node, operationId: `prefixed_${node.operationId}` }\n * },\n * })\n * ```\n *\n * @example Replace only the root node\n * ```ts\n * const next = transform(root, {\n * depth: 'shallow',\n * input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),\n * })\n * ```\n */\nexport function transform(node: InputNode, options: TransformOptions): InputNode\nexport function transform(node: OutputNode, options: TransformOptions): OutputNode\nexport function transform(node: OperationNode, options: TransformOptions): OperationNode\nexport function transform(node: SchemaNode, options: TransformOptions): SchemaNode\nexport function transform(node: PropertyNode, options: TransformOptions): PropertyNode\nexport function transform(node: ParameterNode, options: TransformOptions): ParameterNode\nexport function transform(node: ResponseNode, options: TransformOptions): ResponseNode\nexport function transform(node: Node, options: TransformOptions): Node\nexport function transform(node: Node, options: TransformOptions): Node {\n const { depth, parent, ...visitor } = options\n const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep\n\n const visited = applyVisitor<Node>(node, visitor, parent) ?? node\n const rebuilt = transformChildren(visited, options, recurse)\n\n // Structural sharing: when the visitor and child rebuild both left this node\n // untouched, return the original reference so callers can detect \"nothing\n // changed\" by identity and ancestors can avoid reallocating.\n return rebuilt\n}\n\n/**\n * Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming\n * each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.\n * `Schema` children are skipped in shallow mode.\n */\nfunction transformChildren(node: Node, options: TransformOptions, recurse: boolean): Node {\n if (node.kind === 'Schema' && !recurse) return node\n\n const keys = visitorKeysByKind[node.kind]\n if (!keys) return node\n\n const record = node as unknown as Record<string, unknown>\n const childOptions = { ...options, parent: node }\n let updates: Record<string, unknown> | undefined\n\n for (const key of keys) {\n if (!(key in record)) continue\n const value = record[key]\n if (Array.isArray(value)) {\n let changed = false\n const mapped = value.map((item) => {\n if (!isNode(item)) return item\n const next = transform(item, childOptions)\n if (next !== item) changed = true\n return next\n })\n if (changed) (updates ??= {})[key] = mapped\n } else if (isNode(value)) {\n const next = transform(value, childOptions)\n if (next !== value) (updates ??= {})[key] = next\n }\n }\n\n return updates ? ({ ...node, ...updates } as Node) : node\n}\n/**\n * Lazy depth-first collection pass. Yields every non-null value returned by\n * the visitor callbacks. Use `collect` for the eager array form.\n *\n * @example Collect every operationId\n * ```ts\n * const ids: string[] = []\n * for (const id of collectLazy<string>(root, {\n * operation(node) {\n * return node.operationId\n * },\n * })) {\n * ids.push(id)\n * }\n * ```\n */\nexport function* collectLazy<T>(node: Node, options: CollectOptions<T>): Generator<T, void, undefined> {\n const { depth, parent, ...visitor } = options\n const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep\n\n const v = applyVisitor<T>(node, visitor, parent)\n if (v != null) yield v\n\n for (const child of getChildren(node, recurse)) {\n yield* collectLazy(child, { ...options, parent: node })\n }\n}\n\n/**\n * Eager depth-first collection pass. Gathers every non-null value the visitor\n * callbacks return into an array.\n *\n * @example Collect every operationId\n * ```ts\n * const ids = collect<string>(root, {\n * operation(node) {\n * return node.operationId\n * },\n * })\n * ```\n */\nexport function collect<T>(node: Node, options: CollectOptions<T>): Array<T> {\n return Array.from(collectLazy(node, options))\n}\n"],"mappings":";;AAUA,MAAa,gBAAgB;CAC3B,SAAS;CACT,MAAM;AACR;;;;;;;AAQA,MAAa,cAAc;;;;CAIzB,QAAQ;;;;CAIR,QAAQ;;;;CAIR,SAAS;;;;CAIT,QAAQ;;;;CAIR,SAAS;;;;CAIT,MAAM;;;;CAIN,KAAK;;;;CAIL,SAAS;;;;CAIT,MAAM;;;;CAIN,QAAQ;;;;CAIR,OAAO;;;;CAIP,OAAO;;;;CAIP,OAAO;;;;CAIP,cAAc;;;;CAId,MAAM;;;;CAIN,KAAK;;;;CAIL,MAAM;;;;CAIN,UAAU;;;;CAIV,MAAM;;;;CAIN,MAAM;;;;CAIN,OAAO;;;;CAIP,KAAK;;;;CAIL,MAAM;;;;CAIN,MAAM;;;;CAIN,MAAM;;;;CAIN,OAAO;AACT;;;;AA0BA,MAAa,SAAS,MAAM,KAAK,EAAE,QAAQ,EAAY,SAAS,GAAG,CAAC,CAAC,KAAK,EAAE;;;;;;;;;;;;AC7I5E,SAAgB,aAA2C,MAA8B,MAAqC;CAC5H,OAAO,MAAM,SAAS,OAAQ,OAA+B;AAC/D;;;;;;;;;;;AAYA,SAAgB,oBAAoB,MAAgD;CAClF,OAAO,KAAK,aAAa,UAAW,KAAK,WAAW,KAAA,KAAa,KAAK,SAAS,KAAA;AACjF;;;ACrBA,MAAM,mBAAmB,IAAI,IAAgB;CAAC;CAAU;CAAQ;CAAS;CAAO;AAAU,CAAU;;;;;;;;;AAUpG,SAAgB,eAAe,KAAqB;CAClD,OAAO,IAAI,MA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EhE,MAAM,IAAI,aAAgB,MAAM,SAAS,MAAM;CAC/C,IAAI,KAAK,MAAM,MAAM;CAErB,KAAK,MAAM,SAAS,YAAY,MAAM,OAAO,GAC3C,OAAO,YAAY,OAAO;EAAE,GAAG;EAAS,QAAQ;CAAK,CAAC;AAE1D;;;;;;;;;;;;;;AAeA,SAAgB,QAAW,MAAY,SAAsC;CAC3E,OAAO,MAAM,KAAK,YAAY,MAAM,OAAO,CAAC;AAC9C"}
import "./chunk-CNktS9qV.js";
import { $ as schemaDef, A as fileDef, C as objectBindingPatternDef, F as arrowFunctionDef, G as functionDef, I as breakDef, J as typeDef, K as jsxDef, L as constDef, M as sourceDef, N as contentDef, Q as createSchema, S as indexedAccessTypeDef, Z as pascalCase, b as functionParameterDef, c as outputDef, f as requestBodyDef, i as propertyDef, j as importDef, k as exportDef, m as inputDef, n as responseDef, o as parameterDef, q as textDef, u as operationDef, w as typeLiteralDef, x as functionParametersDef } from "./response-Rd1uisM1.js";
//#region src/constants.ts
const visitorDepths = {
shallow: "shallow",
deep: "deep"
};
/**
* Schema type discriminators used by all AST schema nodes.
*
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/
const schemaTypes = {
/**
* Text value.
*/
string: "string",
/**
* Floating-point number (`float`, `double`).
*/
number: "number",
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
integer: "integer",
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
bigint: "bigint",
/**
* Boolean value.
*/
boolean: "boolean",
/**
* Explicit null value.
*/
null: "null",
/**
* Any value (no type restriction).
*/
any: "any",
/**
* Unknown value (must be narrowed before usage).
*/
unknown: "unknown",
/**
* No return value (`void`).
*/
void: "void",
/**
* Object with named properties.
*/
object: "object",
/**
* Sequential list of items.
*/
array: "array",
/**
* Fixed-length list with position-specific items.
*/
tuple: "tuple",
/**
* "One of" multiple schema members.
*/
union: "union",
/**
* "All of" multiple schema members.
*/
intersection: "intersection",
/**
* Enum schema.
*/
enum: "enum",
/**
* Reference to another schema.
*/
ref: "ref",
/**
* Calendar date (for example `2026-03-24`).
*/
date: "date",
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
datetime: "datetime",
/**
* Time-only value (for example `09:00:00`).
*/
time: "time",
/**
* UUID value.
*/
uuid: "uuid",
/**
* Email address value.
*/
email: "email",
/**
* URL value.
*/
url: "url",
/**
* IPv4 address value.
*/
ipv4: "ipv4",
/**
* IPv6 address value.
*/
ipv6: "ipv6",
/**
* Binary/blob value.
*/
blob: "blob",
/**
* Impossible value (`never`).
*/
never: "never"
};
/**
* One indentation level, derived from {@link INDENT_SIZE}.
*/
const INDENT = Array.from({ length: 2 }, () => " ").join("");
//#endregion
//#region src/guards.ts
/**
* Narrows a `SchemaNode` to the variant that matches `type`.
*
* @example
* ```ts
* const schema = createSchema({ type: 'string' })
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
* ```
*/
function narrowSchema(node, type) {
return node?.type === type ? node : null;
}
/**
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
*
* @example
* ```ts
* if (isHttpOperationNode(node)) {
* console.log(node.method, node.path)
* }
* ```
*/
function isHttpOperationNode(node) {
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
}
//#endregion
//#region src/utils/refs.ts
const plainStringTypes = new Set([
"string",
"uuid",
"email",
"url",
"datetime"
]);
/**
* Returns the last path segment of a reference string.
*
* @example
* ```ts
* extractRefName('#/components/schemas/Pet') // 'Pet'
* ```
*/
function extractRefName(ref) {
return ref.split("/").at(-1) ?? ref;
}
/**
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
*
* Returns `null` for non-ref nodes or when no name resolves.
*
* @example
* ```ts
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
* // => 'Pet'
* ```
*/
function resolveRefName(node) {
if (!node || node.type !== "ref") return null;
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
return node.name ?? node.schema?.name ?? null;
}
/**
* Builds a PascalCase child schema name by joining a parent name and property name.
* Returns `null` when there is no parent to nest under.
*
* @example
* ```ts
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
* childName(undefined, 'params') // null
* ```
*/
function childName(parentName, propName) {
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
}
/**
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
* empty parts.
*
* @example
* ```ts
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
* ```
*/
function enumPropName(parentName, propName, enumSuffix) {
return pascalCase([
parentName,
propName,
enumSuffix
].filter(Boolean).join(" "));
}
/**
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
*
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override
* the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
*
* @example
* ```ts
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
* ```
*/
function syncSchemaRef(node) {
const ref = narrowSchema(node, "ref");
if (!ref) return node;
if (!ref.schema) return node;
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
return createSchema({
...ref.schema,
...definedOverrides
});
}
/**
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
function isStringType(node) {
if (plainStringTypes.has(node.type)) return true;
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
if (temporal) return temporal.representation !== "date";
return false;
}
/**
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
*
* Returns `null` when there is no resolver, no params, or the group name equals the
* individual param name (so there is no real group to emit).
*/
function resolveGroupType({ node, params, group, resolver }) {
if (!resolver || !params.length) return null;
const firstParam = params[0];
const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam);
if (groupName === resolver.resolveParamName(node, firstParam)) return null;
return {
type: groupName,
optional: params.every((p) => !p.required)
};
}
//#endregion
//#region src/registry.ts
/**
* Every node definition. Adding a node means adding its `defineNode` to one
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
*/
const nodeDefs = [
inputDef,
outputDef,
operationDef,
requestBodyDef,
contentDef,
responseDef,
schemaDef,
propertyDef,
parameterDef,
functionParameterDef,
functionParametersDef,
typeLiteralDef,
indexedAccessTypeDef,
objectBindingPatternDef,
constDef,
typeDef,
functionDef,
arrowFunctionDef,
textDef,
breakDef,
jsxDef,
importDef,
exportDef,
sourceDef,
fileDef
];
//#endregion
//#region src/visitor.ts
/**
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
* Derived from each definition's `children`.
*/
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
/**
* Maps a node kind to the matching visitor callback name. Derived from each
* definition's `visitorKey`.
*/
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
/**
* Creates a small async concurrency limiter.
*
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
*
* @example
* ```ts
* const limit = createLimit(2)
* for (const task of [taskA, taskB, taskC]) {
* await limit(() => task())
* }
* // only 2 tasks run at the same time
* ```
*/
function createLimit(concurrency) {
let active = 0;
const queue = [];
function next() {
if (active < concurrency && queue.length > 0) {
active++;
queue.shift()();
}
}
return function limit(fn) {
return new Promise((resolve, reject) => {
queue.push(() => {
Promise.resolve(fn()).then(resolve, reject).finally(() => {
active--;
next();
});
});
next();
});
};
}
const visitorKeysByKind = VISITOR_KEYS;
/**
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
*/
function isNode(value) {
return typeof value === "object" && value !== null && "kind" in value;
}
/**
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
*
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
*
* @example
* ```ts
* const children = getChildren(operationNode, true)
* // returns parameters, the request body, and responses
* ```
*/
function* getChildren(node, recurse) {
if (node.kind === "Schema" && !recurse) return;
const keys = visitorKeysByKind[node.kind];
if (!keys) return;
const record = node;
for (const key of keys) {
const value = record[key];
if (Array.isArray(value)) {
for (const item of value) if (isNode(item)) yield item;
} else if (isNode(value)) yield value;
}
}
/**
* Runs the visitor callback that matches `node.kind` with the traversal
* context. The result is a replacement node, a collected value, or `undefined`
* when no callback is registered for the kind.
*
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
* in one place. `TResult` is the caller's expected return: the same node type
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
*/
function applyVisitor(node, visitor, parent) {
const key = VISITOR_KEY_BY_KIND[node.kind];
if (!key) return void 0;
const fn = visitor[key];
return fn?.(node, { parent });
}
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
async function walk(node, options) {
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
}
async function _walk(node, visitor, recurse, limit, parent) {
await limit(() => applyVisitor(node, visitor, parent));
const children = Array.from(getChildren(node, recurse));
if (children.length === 0) return;
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
}
function transform(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
return transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
}
/**
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
* `Schema` children are skipped in shallow mode.
*/
function transformChildren(node, options, recurse) {
if (node.kind === "Schema" && !recurse) return node;
const keys = visitorKeysByKind[node.kind];
if (!keys) return node;
const record = node;
const childOptions = {
...options,
parent: node
};
let updates;
for (const key of keys) {
if (!(key in record)) continue;
const value = record[key];
if (Array.isArray(value)) {
let changed = false;
const mapped = value.map((item) => {
if (!isNode(item)) return item;
const next = transform(item, childOptions);
if (next !== item) changed = true;
return next;
});
if (changed) (updates ??= {})[key] = mapped;
} else if (isNode(value)) {
const next = transform(value, childOptions);
if (next !== value) (updates ??= {})[key] = next;
}
}
return updates ? {
...node,
...updates
} : node;
}
/**
* Lazy depth-first collection pass. Yields every non-null value returned by
* the visitor callbacks. Use `collect` for the eager array form.
*
* @example Collect every operationId
* ```ts
* const ids: string[] = []
* for (const id of collectLazy<string>(root, {
* operation(node) {
* return node.operationId
* },
* })) {
* ids.push(id)
* }
* ```
*/
function* collectLazy(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const v = applyVisitor(node, visitor, parent);
if (v != null) yield v;
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
...options,
parent: node
});
}
/**
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
function collect(node, options) {
return Array.from(collectLazy(node, options));
}
//#endregion
export { nodeDefs as a, extractRefName as c, resolveRefName as d, syncSchemaRef as f, schemaTypes as g, INDENT as h, walk as i, isStringType as l, narrowSchema as m, collectLazy as n, childName as o, isHttpOperationNode as p, transform as r, enumPropName as s, collect as t, resolveGroupType as u };
//# sourceMappingURL=visitor-DepQEKyp.js.map
{"version":3,"file":"visitor-DepQEKyp.js","names":[],"sources":["../src/constants.ts","../src/guards.ts","../src/utils/refs.ts","../src/registry.ts","../src/visitor.ts"],"sourcesContent":["import type { SchemaType } from './nodes/schema.ts'\n\n/**\n * Traversal depth for AST visitor utilities.\n *\n * - `'shallow'` visits only the immediate node, skipping children.\n * - `'deep'` recursively visits all descendant nodes.\n */\nexport type VisitorDepth = 'shallow' | 'deep'\n\nexport const visitorDepths = {\n shallow: 'shallow',\n deep: 'deep',\n} as const satisfies Record<VisitorDepth, VisitorDepth>\n\n/**\n * Schema type discriminators used by all AST schema nodes.\n *\n * Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).\n * Call `isScalarPrimitive()` to check for the scalar types.\n */\nexport const schemaTypes = {\n /**\n * Text value.\n */\n string: 'string',\n /**\n * Floating-point number (`float`, `double`).\n */\n number: 'number',\n /**\n * Whole number (`int32`). Use `bigint` for `int64`.\n */\n integer: 'integer',\n /**\n * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.\n */\n bigint: 'bigint',\n /**\n * Boolean value.\n */\n boolean: 'boolean',\n /**\n * Explicit null value.\n */\n null: 'null',\n /**\n * Any value (no type restriction).\n */\n any: 'any',\n /**\n * Unknown value (must be narrowed before usage).\n */\n unknown: 'unknown',\n /**\n * No return value (`void`).\n */\n void: 'void',\n /**\n * Object with named properties.\n */\n object: 'object',\n /**\n * Sequential list of items.\n */\n array: 'array',\n /**\n * Fixed-length list with position-specific items.\n */\n tuple: 'tuple',\n /**\n * \"One of\" multiple schema members.\n */\n union: 'union',\n /**\n * \"All of\" multiple schema members.\n */\n intersection: 'intersection',\n /**\n * Enum schema.\n */\n enum: 'enum',\n /**\n * Reference to another schema.\n */\n ref: 'ref',\n /**\n * Calendar date (for example `2026-03-24`).\n */\n date: 'date',\n /**\n * Date-time value (for example `2026-03-24T09:00:00Z`).\n */\n datetime: 'datetime',\n /**\n * Time-only value (for example `09:00:00`).\n */\n time: 'time',\n /**\n * UUID value.\n */\n uuid: 'uuid',\n /**\n * Email address value.\n */\n email: 'email',\n /**\n * URL value.\n */\n url: 'url',\n /**\n * IPv4 address value.\n */\n ipv4: 'ipv4',\n /**\n * IPv6 address value.\n */\n ipv6: 'ipv6',\n /**\n * Binary/blob value.\n */\n blob: 'blob',\n /**\n * Impossible value (`never`).\n */\n never: 'never',\n} as const satisfies Record<SchemaType, SchemaType>\n\n/**\n * Default concurrency limit for the `walk()` traversal utility.\n *\n * Set to 30 to balance I/O-bound resolver parallelism against event-loop and memory pressure\n * during large spec traversals. Override it for different hardware constraints.\n *\n * @example\n * ```ts\n * import { walk, WALK_CONCURRENCY } from '@kubb/ast'\n *\n * walk(root, { concurrency: WALK_CONCURRENCY, root: () => {} })\n * ```\n */\nexport const WALK_CONCURRENCY = 30\n\n/**\n * Number of spaces in one indentation level when assembling multi-line code as strings.\n * Set to 2, 3, … to change the indent width used by `buildObject`/`buildList`.\n */\nconst INDENT_SIZE = 2\n\n/**\n * One indentation level, derived from {@link INDENT_SIZE}.\n */\nexport const INDENT = Array.from({ length: INDENT_SIZE }, () => ' ').join('')\n","import type { HttpOperationNode, OperationNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'\n\n/**\n * Narrows a `SchemaNode` to the variant that matches `type`.\n *\n * @example\n * ```ts\n * const schema = createSchema({ type: 'string' })\n * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null\n * ```\n */\nexport function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null {\n return node?.type === type ? (node as SchemaNodeByType[T]) : null\n}\n\n/**\n * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.\n *\n * @example\n * ```ts\n * if (isHttpOperationNode(node)) {\n * console.log(node.method, node.path)\n * }\n * ```\n */\nexport function isHttpOperationNode(node: OperationNode): node is HttpOperationNode {\n return node.protocol === 'http' || (node.method !== undefined && node.path !== undefined)\n}\n","import { pascalCase } from '@internals/utils'\nimport { narrowSchema } from '../guards.ts'\nimport type { OperationNode, ParameterNode, SchemaNode } from '../nodes/index.ts'\nimport { createSchema, type SchemaType } from '../nodes/schema.ts'\nimport type { OperationParamsResolver, ParamGroupType } from './operationParams.ts'\n\nconst plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)\n\n/**\n * Returns the last path segment of a reference string.\n *\n * @example\n * ```ts\n * extractRefName('#/components/schemas/Pet') // 'Pet'\n * ```\n */\nexport function extractRefName(ref: string): string {\n return ref.split('/').at(-1) ?? ref\n}\n\n/**\n * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.\n *\n * Returns `null` for non-ref nodes or when no name resolves.\n *\n * @example\n * ```ts\n * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })\n * // => 'Pet'\n * ```\n */\nexport function resolveRefName(node: SchemaNode | undefined): string | null {\n if (!node || node.type !== 'ref') return null\n if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null\n\n return node.name ?? node.schema?.name ?? null\n}\n\n/**\n * Builds a PascalCase child schema name by joining a parent name and property name.\n * Returns `null` when there is no parent to nest under.\n *\n * @example\n * ```ts\n * childName('Order', 'shipping_address') // 'OrderShippingAddress'\n * childName(undefined, 'params') // null\n * ```\n */\nexport function childName(parentName: string | null | undefined, propName: string): string | null {\n return parentName ? pascalCase([parentName, propName].join(' ')) : null\n}\n\n/**\n * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any\n * empty parts.\n *\n * @example\n * ```ts\n * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'\n * ```\n */\nexport function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {\n return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))\n}\n\n/**\n * Merges a ref node with its resolved schema, giving usage-site fields precedence.\n *\n * Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override\n * the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })\n * const merged = syncSchemaRef(ref) // merges with resolved Pet schema\n * ```\n */\nexport function syncSchemaRef(node: SchemaNode): SchemaNode {\n const ref = narrowSchema(node, 'ref')\n\n if (!ref) return node\n if (!ref.schema) return node\n\n const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref\n\n // Filter out undefined override values so they don't shadow the resolved schema's fields.\n const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))\n\n return createSchema({ ...ref.schema, ...definedOverrides })\n}\n\n/**\n * Type guard that returns `true` when a schema emits as a plain `string` type.\n *\n * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`\n * types, returns `true` only when `representation` is `'string'` rather than `'date'`.\n */\nexport function isStringType(node: SchemaNode): boolean {\n if (plainStringTypes.has(node.type)) {\n return true\n }\n\n const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')\n if (temporal) {\n return temporal.representation !== 'date'\n }\n\n return false\n}\n\n/**\n * Derives a {@link ParamGroupType} for a query or header group from the resolver.\n *\n * Returns `null` when there is no resolver, no params, or the group name equals the\n * individual param name (so there is no real group to emit).\n */\nexport function resolveGroupType({\n node,\n params,\n group,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n group: 'query' | 'header'\n resolver: OperationParamsResolver | undefined\n}): ParamGroupType | null {\n if (!resolver || !params.length) {\n return null\n }\n const firstParam = params[0]!\n const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName\n const groupName = groupMethod.call(resolver, node, firstParam)\n if (groupName === resolver.resolveParamName(node, firstParam)) {\n return null\n }\n return { type: groupName, optional: params.every((p) => !p.required) }\n}\n","import type { NodeDef } from './defineNode.ts'\nimport { arrowFunctionDef, breakDef, constDef, functionDef, jsxDef, textDef, typeDef } from './nodes/code.ts'\nimport { contentDef } from './nodes/content.ts'\nimport { exportDef, fileDef, importDef, sourceDef } from './nodes/file.ts'\nimport { functionParameterDef, functionParametersDef, indexedAccessTypeDef, objectBindingPatternDef, typeLiteralDef } from './nodes/function.ts'\nimport { inputDef } from './nodes/input.ts'\nimport { operationDef } from './nodes/operation.ts'\nimport { outputDef } from './nodes/output.ts'\nimport { parameterDef } from './nodes/parameter.ts'\nimport { propertyDef } from './nodes/property.ts'\nimport { requestBodyDef } from './nodes/requestBody.ts'\nimport { responseDef } from './nodes/response.ts'\nimport { schemaDef } from './nodes/schema.ts'\n\n// Surface every def from one place so the package barrel re-exports them with `export * from './registry.ts'`.\n// Adding a node means adding its `defineNode` to a `nodes/*.ts` file and listing it in `nodeDefs` below, nothing else.\nexport {\n arrowFunctionDef,\n breakDef,\n constDef,\n contentDef,\n exportDef,\n fileDef,\n functionDef,\n functionParameterDef,\n functionParametersDef,\n importDef,\n indexedAccessTypeDef,\n inputDef,\n jsxDef,\n objectBindingPatternDef,\n operationDef,\n outputDef,\n parameterDef,\n propertyDef,\n requestBodyDef,\n responseDef,\n schemaDef,\n sourceDef,\n textDef,\n typeDef,\n typeLiteralDef,\n}\n\n/**\n * Every node definition. Adding a node means adding its `defineNode` to one\n * `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.\n */\nexport const nodeDefs = [\n inputDef,\n outputDef,\n operationDef,\n requestBodyDef,\n contentDef,\n responseDef,\n schemaDef,\n propertyDef,\n parameterDef,\n functionParameterDef,\n functionParametersDef,\n typeLiteralDef,\n indexedAccessTypeDef,\n objectBindingPatternDef,\n constDef,\n typeDef,\n functionDef,\n arrowFunctionDef,\n textDef,\n breakDef,\n jsxDef,\n importDef,\n exportDef,\n sourceDef,\n fileDef,\n] satisfies ReadonlyArray<NodeDef>\n","import type { VisitorDepth } from './constants.ts'\nimport { visitorDepths, WALK_CONCURRENCY } from './constants.ts'\nimport type { NodeDef } from './defineNode.ts'\nimport type {\n ContentNode,\n InputNode,\n Node,\n NodeKind,\n OperationNode,\n OutputNode,\n ParameterNode,\n PropertyNode,\n RequestBodyNode,\n ResponseNode,\n SchemaNode,\n} from './nodes/index.ts'\nimport { nodeDefs } from './registry.ts'\n\n/**\n * Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).\n * Derived from each definition's `children`.\n */\nconst VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => (def.children ? [[def.kind, def.children] as const] : []))) as Partial<\n Record<NodeKind, ReadonlyArray<string>>\n>\n\n/**\n * Maps a node kind to the matching visitor callback name. Derived from each\n * definition's `visitorKey`.\n */\nconst VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => (def.visitorKey ? [[def.kind, def.visitorKey] as const] : []))) as Partial<\n Record<NodeKind, NonNullable<NodeDef['visitorKey']>>\n>\n\n/**\n * Creates a small async concurrency limiter.\n *\n * At most `concurrency` tasks are in flight at once. Extra tasks are queued.\n *\n * @example\n * ```ts\n * const limit = createLimit(2)\n * for (const task of [taskA, taskB, taskC]) {\n * await limit(() => task())\n * }\n * // only 2 tasks run at the same time\n * ```\n */\nfunction createLimit(concurrency: number) {\n let active = 0\n const queue: Array<() => void> = []\n\n function next() {\n if (active < concurrency && queue.length > 0) {\n active++\n queue.shift()!()\n }\n }\n\n return function limit<T>(fn: () => Promise<T> | T): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n queue.push(() => {\n Promise.resolve(fn())\n .then(resolve, reject)\n .finally(() => {\n active--\n next()\n })\n })\n next()\n })\n }\n}\n\ntype LimitFn = ReturnType<typeof createLimit>\n\n/**\n * Ordered mapping of `[NodeType, ParentType]` pairs.\n *\n * `ParentOf` uses this map to find parent types.\n */\ntype ParentNodeMap = [\n [InputNode, undefined],\n [OutputNode, undefined],\n [OperationNode, InputNode],\n [RequestBodyNode, OperationNode],\n [ContentNode, RequestBodyNode | ResponseNode],\n [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode],\n [PropertyNode, SchemaNode],\n [ParameterNode, OperationNode],\n [ResponseNode, OperationNode],\n]\n\n/**\n * Resolves the parent node type for a given AST node type.\n *\n * Visitor context relies on this so `ctx.parent` is typed for each callback.\n *\n * @example\n * ```ts\n * type InputParent = ParentOf<InputNode>\n * // undefined\n * ```\n *\n * @example\n * ```ts\n * type PropertyParent = ParentOf<PropertyNode>\n * // SchemaNode\n * ```\n *\n * @example\n * ```ts\n * type SchemaParent = ParentOf<SchemaNode>\n * // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode\n * ```\n */\nexport type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [\n infer TEntry extends [Node, unknown],\n ...infer TRest extends ReadonlyArray<[Node, unknown]>,\n]\n ? T extends TEntry[0]\n ? TEntry[1]\n : ParentOf<T, TRest>\n : Node\n\n/**\n * Traversal context passed as the second argument to every visitor callback.\n * `parent` is typed from the current node type.\n *\n * @example\n * ```ts\n * const visitor: Visitor = {\n * schema(node, { parent }) {\n * // parent type is narrowed by node kind\n * },\n * }\n * ```\n */\nexport type VisitorContext<T extends Node = Node> = {\n /**\n * Parent node of the currently visited node.\n * For `InputNode`, this is `undefined`.\n */\n parent?: ParentOf<T>\n}\n\n/**\n * Synchronous visitor consumed by `transform`. Each optional callback runs\n * for the matching node type. Return a new node to replace it, or `undefined`\n * to leave it untouched.\n *\n * Plugins typically expose `transformer` so users can supply a `Visitor` that\n * rewrites the AST before printing.\n *\n * @example Prefix every operationId\n * ```ts\n * const visitor: Visitor = {\n * operation(node) {\n * return { ...node, operationId: `api_${node.operationId}` }\n * },\n * }\n * ```\n *\n * @example Strip schema descriptions\n * ```ts\n * const visitor: Visitor = {\n * schema(node) {\n * return { ...node, description: undefined }\n * },\n * }\n * ```\n */\nexport type Visitor = {\n input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode\n output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode\n}\n\n/**\n * A visitor callback result that may be sync or async.\n */\ntype MaybePromise<T> = T | Promise<T>\n\n/**\n * Async visitor for `walk`. Synchronous `Visitor` objects are compatible.\n *\n * @example\n * ```ts\n * const visitor: AsyncVisitor = {\n * async operation(node) {\n * await Promise.resolve(node.operationId)\n * },\n * }\n * ```\n */\ntype AsyncVisitor = {\n input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>\n output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>\n}\n\n/**\n * Visitor used by `collect`.\n *\n * @example\n * ```ts\n * const visitor: CollectVisitor<string> = {\n * operation(node) {\n * return node.operationId\n * },\n * }\n * ```\n */\ntype CollectVisitor<T> = {\n input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined\n output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined\n}\n\n/**\n * Options for `transform`.\n *\n * @example\n * ```ts\n * const options: TransformOptions = { depth: 'deep', schema: (node) => node }\n * ```\n *\n * @example\n * ```ts\n * // Only transform the current node, not nested children\n * const options: TransformOptions = { depth: 'shallow', schema: (node) => node }\n * ```\n */\nexport type TransformOptions = Visitor & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Internal parent override used during recursion.\n */\n parent?: Node\n}\n\n/**\n * Options for `walk`.\n *\n * @example\n * ```ts\n * const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }\n * ```\n */\nexport type WalkOptions = AsyncVisitor & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Maximum number of sibling nodes visited concurrently.\n * @default 30\n */\n concurrency?: number\n}\n\n/**\n * Options for `collect`.\n *\n * @example\n * ```ts\n * const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }\n * ```\n */\nexport type CollectOptions<T> = CollectVisitor<T> & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Internal parent override used during recursion.\n */\n parent?: Node\n}\n\nconst visitorKeysByKind = VISITOR_KEYS as Record<string, ReadonlyArray<string> | undefined>\n\n/**\n * Returns `true` when `value` is an AST node (an object carrying a `kind`).\n */\nfunction isNode(value: unknown): value is Node {\n return typeof value === 'object' && value !== null && 'kind' in value\n}\n\n/**\n * Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.\n *\n * `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.\n *\n * @example\n * ```ts\n * const children = getChildren(operationNode, true)\n * // returns parameters, the request body, and responses\n * ```\n */\nfunction* getChildren(node: Node, recurse: boolean): Generator<Node, void, undefined> {\n if (node.kind === 'Schema' && !recurse) return\n\n const keys = visitorKeysByKind[node.kind]\n if (!keys) return\n\n const record = node as unknown as Record<string, unknown>\n for (const key of keys) {\n const value = record[key]\n if (Array.isArray(value)) {\n for (const item of value) if (isNode(item)) yield item\n } else if (isNode(value)) {\n yield value\n }\n }\n}\n\n/**\n * Runs the visitor callback that matches `node.kind` with the traversal\n * context. The result is a replacement node, a collected value, or `undefined`\n * when no callback is registered for the kind.\n *\n * Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives\n * in one place. `TResult` is the caller's expected return: the same node type\n * for `transform`, the collected value type for `collectLazy`, ignored for `walk`.\n */\nfunction applyVisitor<TResult>(node: Node, visitor: Visitor | AsyncVisitor | CollectVisitor<unknown>, parent: Node | undefined): TResult | null | undefined {\n const key = VISITOR_KEY_BY_KIND[node.kind]\n if (!key) return undefined\n\n const fn = visitor[key] as ((node: Node, context: VisitorContext) => TResult | null | undefined) | undefined\n\n return fn?.(node, { parent })\n}\n\n/**\n * Async depth-first traversal for side effects. Visitor return values are\n * ignored. Use `transform` when you want to rewrite nodes.\n *\n * Sibling nodes at each depth run concurrently up to `options.concurrency`\n * (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor\n * work. Lower values reduce memory pressure.\n *\n * @example Log every operation\n * ```ts\n * await walk(root, {\n * operation(node) {\n * console.log(node.operationId)\n * },\n * })\n * ```\n *\n * @example Only visit the root node\n * ```ts\n * await walk(root, { depth: 'shallow', input: () => {} })\n * ```\n */\nexport async function walk(node: Node, options: WalkOptions): Promise<void> {\n const recurse = (options.depth ?? visitorDepths.deep) === visitorDepths.deep\n const limit = createLimit(options.concurrency ?? WALK_CONCURRENCY)\n\n return _walk(node, options, recurse, limit, undefined)\n}\n\nasync function _walk(node: Node, visitor: AsyncVisitor, recurse: boolean, limit: LimitFn, parent: Node | undefined): Promise<void> {\n await limit(() => applyVisitor(node, visitor, parent))\n\n // Visit siblings concurrently and let the shared `limit` cap how many callbacks\n // run at once. Awaiting each child sequentially here would serialize the whole\n // traversal and make `concurrency` inert. Every visitor callback would run one\n // at a time regardless of the limit.\n const children = Array.from(getChildren(node, recurse))\n if (children.length === 0) return\n\n await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)))\n}\n\n/**\n * Synchronous depth-first transform. Each visitor callback can return a\n * replacement node. Returning `undefined` keeps the original.\n *\n * The original tree is never mutated, a new tree is returned. Pass\n * `depth: 'shallow'` to skip recursion into children.\n *\n * @example Prefix every operationId\n * ```ts\n * const next = transform(root, {\n * operation(node) {\n * return { ...node, operationId: `prefixed_${node.operationId}` }\n * },\n * })\n * ```\n *\n * @example Replace only the root node\n * ```ts\n * const next = transform(root, {\n * depth: 'shallow',\n * input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),\n * })\n * ```\n */\nexport function transform(node: InputNode, options: TransformOptions): InputNode\nexport function transform(node: OutputNode, options: TransformOptions): OutputNode\nexport function transform(node: OperationNode, options: TransformOptions): OperationNode\nexport function transform(node: SchemaNode, options: TransformOptions): SchemaNode\nexport function transform(node: PropertyNode, options: TransformOptions): PropertyNode\nexport function transform(node: ParameterNode, options: TransformOptions): ParameterNode\nexport function transform(node: ResponseNode, options: TransformOptions): ResponseNode\nexport function transform(node: Node, options: TransformOptions): Node\nexport function transform(node: Node, options: TransformOptions): Node {\n const { depth, parent, ...visitor } = options\n const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep\n\n const visited = applyVisitor<Node>(node, visitor, parent) ?? node\n const rebuilt = transformChildren(visited, options, recurse)\n\n // Structural sharing: when the visitor and child rebuild both left this node\n // untouched, return the original reference so callers can detect \"nothing\n // changed\" by identity and ancestors can avoid reallocating.\n return rebuilt\n}\n\n/**\n * Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming\n * each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.\n * `Schema` children are skipped in shallow mode.\n */\nfunction transformChildren(node: Node, options: TransformOptions, recurse: boolean): Node {\n if (node.kind === 'Schema' && !recurse) return node\n\n const keys = visitorKeysByKind[node.kind]\n if (!keys) return node\n\n const record = node as unknown as Record<string, unknown>\n const childOptions = { ...options, parent: node }\n let updates: Record<string, unknown> | undefined\n\n for (const key of keys) {\n if (!(key in record)) continue\n const value = record[key]\n if (Array.isArray(value)) {\n let changed = false\n const mapped = value.map((item) => {\n if (!isNode(item)) return item\n const next = transform(item, childOptions)\n if (next !== item) changed = true\n return next\n })\n if (changed) (updates ??= {})[key] = mapped\n } else if (isNode(value)) {\n const next = transform(value, childOptions)\n if (next !== value) (updates ??= {})[key] = next\n }\n }\n\n return updates ? ({ ...node, ...updates } as Node) : node\n}\n/**\n * Lazy depth-first collection pass. Yields every non-null value returned by\n * the visitor callbacks. Use `collect` for the eager array form.\n *\n * @example Collect every operationId\n * ```ts\n * const ids: string[] = []\n * for (const id of collectLazy<string>(root, {\n * operation(node) {\n * return node.operationId\n * },\n * })) {\n * ids.push(id)\n * }\n * ```\n */\nexport function* collectLazy<T>(node: Node, options: CollectOptions<T>): Generator<T, void, undefined> {\n const { depth, parent, ...visitor } = options\n const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep\n\n const v = applyVisitor<T>(node, visitor, parent)\n if (v != null) yield v\n\n for (const child of getChildren(node, recurse)) {\n yield* collectLazy(child, { ...options, parent: node })\n }\n}\n\n/**\n * Eager depth-first collection pass. Gathers every non-null value the visitor\n * callbacks return into an array.\n *\n * @example Collect every operationId\n * ```ts\n * const ids = collect<string>(root, {\n * operation(node) {\n * return node.operationId\n * },\n * })\n * ```\n */\nexport function collect<T>(node: Node, options: CollectOptions<T>): Array<T> {\n return Array.from(collectLazy(node, options))\n}\n"],"mappings":";;;AAUA,MAAa,gBAAgB;CAC3B,SAAS;CACT,MAAM;AACR;;;;;;;AAQA,MAAa,cAAc;;;;CAIzB,QAAQ;;;;CAIR,QAAQ;;;;CAIR,SAAS;;;;CAIT,QAAQ;;;;CAIR,SAAS;;;;CAIT,MAAM;;;;CAIN,KAAK;;;;CAIL,SAAS;;;;CAIT,MAAM;;;;CAIN,QAAQ;;;;CAIR,OAAO;;;;CAIP,OAAO;;;;CAI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,SAAS,CAAC,YAAY,CAAC,EAAA,CAAG,OAAO;EACvC,OAAO,IAAI,OAAO,KAAK,GAAG;GACxB,MAAM,OAAO,UAAU,OAAO,YAAY;GAC1C,IAAI,SAAS,OAAO,CAAC,YAAY,CAAC,EAAA,CAAG,OAAO;EAC9C;CACF;CAEA,OAAO,UAAW;EAAE,GAAG;EAAM,GAAG;CAAQ,IAAa;AACvD;;;;;;;;;;;;;;;;;AAiBA,UAAiB,YAAe,MAAY,SAA2D;CACrG,MAAM,EAAE,OAAO,QAAQ,GAAG,YAAY;CACtC,MAAM,WAAW,SAAS,cAAc,UAAU,cAAc;CAEhE,MAAM,IAAI,aAAgB,MAAM,SAAS,MAAM;CAC/C,IAAI,KAAK,MAAM,MAAM;CAErB,KAAK,MAAM,SAAS,YAAY,MAAM,OAAO,GAC3C,OAAO,YAAY,OAAO;EAAE,GAAG;EAAS,QAAQ;CAAK,CAAC;AAE1D;;;;;;;;;;;;;;AAeA,SAAgB,QAAW,MAAY,SAAsC;CAC3E,OAAO,MAAM,KAAK,YAAY,MAAM,OAAO,CAAC;AAC9C"}
import type { Dialect } from './dialect.ts'
import type { BaseNode, NodeKind } from './nodes/base.ts'
/**
* Visitor callback names, one per traversable node kind, in traversal order.
* Kept in sync with the keys of `Visitor` in `visitor.ts`.
*/
export const visitorKeys = ['input', 'output', 'operation', 'schema', 'property', 'parameter', 'response'] as const
/**
* One of the {@link visitorKeys} callback names.
*/
export type VisitorKey = (typeof visitorKeys)[number]
/**
* Distributive `Omit` that preserves each member of a union.
*
* @example
* ```ts
* type A = { kind: 'a'; keep: string; drop: number }
* type B = { kind: 'b'; keep: boolean; drop: number }
* type Result = DistributiveOmit<A | B, 'drop'>
* // -> { kind: 'a'; keep: string } | { kind: 'b'; keep: boolean }
* ```
*/
export type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never
/**
* Builds a type guard that matches nodes of the given `kind`.
*/
function isKind<T extends BaseNode>(kind: NodeKind) {
return (node: unknown): node is T => (node as BaseNode).kind === kind
}
/**
* The single definition derived from one {@link defineNode} call: the node's
* `create` builder, its `is` guard, and the traversal metadata the registry
* collects into the visitor tables.
*/
export type NodeDef<TNode extends BaseNode = BaseNode, TInput = never> = {
/**
* Node discriminator this definition owns.
*/
kind: NodeKind
/**
* Builds a node from its input, applying `defaults` and the optional `build` hook. An
* optional `dialect` is forwarded to `build` so nodes can derive spec-specific fields
* (e.g. a schema's `optional`/`nullish`) through `dialect.schema.optionality` when one is given.
*/
create: (input: TInput, dialect?: Dialect) => TNode
/**
* Type guard matching this node kind.
*/
is: (node: unknown) => node is TNode
/**
* Child node fields in traversal order. Feeds `VISITOR_KEYS`.
*/
children?: ReadonlyArray<string>
/**
* Visitor callback name. Feeds `VISITOR_KEY_BY_KIND`.
*/
visitorKey?: VisitorKey
}
type DefineNodeConfig<TNode extends BaseNode, TInput, TBuilt extends object> = {
kind: TNode['kind']
defaults?: Partial<TNode>
build?: (input: TInput, dialect?: Dialect) => TBuilt
children?: ReadonlyArray<string>
visitorKey?: VisitorKey
}
/**
* Defines a node once and derives its `create` builder, `is` guard, and traversal
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
* `kind`, so node construction lives in one place without scattered `as` casts.
*
* @example Simple node
* ```ts
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
* const createImport = importDef.create
* ```
*
* @example Node with a build hook
* ```ts
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
* kind: 'Property',
* build: (props) => ({ ...props, required: props.required ?? false }),
* children: ['schema'],
* visitorKey: 'property',
* })
* ```
*/
export function defineNode<TNode extends BaseNode, TInput = Omit<TNode, 'kind'>, TBuilt extends object = Omit<TNode, 'kind'>>(
config: DefineNodeConfig<TNode, TInput, TBuilt>,
): NodeDef<TNode, TInput> {
const { kind, defaults, build, children, visitorKey } = config
function create(input: TInput, dialect?: Dialect): TNode {
const base = build ? build(input, dialect) : input
return { ...defaults, ...(base as object), kind } as TNode
}
return { kind, create, is: isKind<TNode>(kind), children, visitorKey }
}
import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'
/**
* Runtime context passed as `this` to printer handlers.
*
* `this.transform` dispatches to node-level handlers from `nodes`.
*
* @example
* ```ts
* const context: PrinterHandlerContext<string, {}> = {
* options: {},
* transform: () => 'value',
* }
* ```
*/
type PrinterHandlerContext<TOutput, TOptions extends object> = {
/**
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
*/
transform: (node: SchemaNode) => TOutput | null
/**
* Options for this printer instance.
*/
options: TOptions
}
/**
* Handler for one schema node type.
*
* Use a regular function (not an arrow function) if you need `this`.
*
* @example
* ```ts
* const handler: PrinterHandler<string, {}, 'string'> = function () {
* return 'string'
* }
* ```
*/
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
this: PrinterHandlerContext<TOutput, TOptions>,
node: SchemaNodeByType[T],
) => TOutput | null
/**
* Partial map of per-node-type handler overrides for a printer.
*
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
* Supply only the handlers you want to replace. The printer's built-in
* defaults fill in the rest.
*
* @example
* ```ts
* pluginZod({
* printer: {
* nodes: {
* date(): string {
* return 'z.string().date()'
* },
* } satisfies PrinterPartial<string, PrinterZodOptions>,
* },
* })
* ```
*/
export type PrinterPartial<TOutput, TOptions extends object> = Partial<{
[K in SchemaType]: PrinterHandler<TOutput, TOptions, K>
}>
/**
* Generic shape used by `definePrinter`.
*
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
* - `TOptions` options passed to and stored on the printer instance
* - `TOutput` the type emitted by node handlers
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
*
* @example
* ```ts
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
* ```
*/
export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
name: TName
options: TOptions
output: TOutput
printOutput: TPrintOutput
}
/**
* Printer instance returned by a printer factory.
*
* @example
* ```ts
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
* ```
*/
export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
/**
* Unique identifier supplied at creation time.
*/
name: T['name']
/**
* Options for this printer instance.
*/
options: T['options']
/**
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
* Always dispatches through the `nodes` map. Never calls the `print` override.
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
*/
transform: (node: SchemaNode) => T['output'] | null
/**
* Public printer. If the builder provides a root-level `print`, this calls that
* higher-level function (which may produce full declarations).
* Otherwise, falls back to the node-level dispatcher.
*/
print: (node: SchemaNode) => T['printOutput'] | null
}
/**
* Builder function passed to `definePrinter`.
*
* It receives resolved options and returns:
* - `name`
* - `options`
* - `nodes` handlers
* - optional top-level `print` override
*
* @example
* ```ts
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
* ```
*/
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
name: T['name']
/**
* Options to store on the printer.
*/
options: T['options']
nodes: Partial<{
[K in SchemaType]: PrinterHandler<T['output'], T['options'], K>
}>
/**
* Optional root-level print override. When provided, becomes the public `printer.print`.
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
* not the override itself, so recursion is safe.
*/
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
}
/**
* Defines a schema printer: a function that takes a `SchemaNode` and emits
* code in your target language. Each plugin that produces code from schemas
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
* with this helper.
*
* The builder receives resolved options and returns:
*
* - `name` unique identifier for the printer.
* - `options` stored on the returned printer instance.
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
* output (a string, a TypeScript AST node, ...) for that schema type.
* - `print` (optional), top-level override exposed as `printer.print`.
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
*
* Without a `print` override, `printer.print` falls back to `printer.transform`
* (the node-level dispatcher).
*
* @example Tiny Zod printer
* ```ts
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
*
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
*
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
* name: 'zod',
* options: { strict: options.strict ?? true },
* nodes: {
* string: () => 'z.string()',
* object(node) {
* const props = node.properties
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
* .join(', ')
* return `z.object({ ${props} })`
* },
* },
* }))
* ```
*/
export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {
return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>
}
/**
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
*
* @example
* ```ts
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
* (node) => node.kind,
* )
* ```
*/
export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {
return function <T extends PrinterFactoryOptions>(
build: (options: T['options']) => {
name: T['name']
options: T['options']
nodes: Partial<{
[K in TKey]: (
this: {
transform: (node: TNode) => T['output'] | null
options: T['options']
},
node: TNodeByKey[K],
) => T['output'] | null
}>
print?: (
this: {
transform: (node: TNode) => T['output'] | null
options: T['options']
},
node: TNode,
) => T['printOutput'] | null
},
): (options?: T['options']) => {
name: T['name']
options: T['options']
transform: (node: TNode) => T['output'] | null
print: (node: TNode) => T['printOutput'] | null
} {
return (options) => {
const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))
const context = {
options: resolvedOptions,
transform: (node: TNode): T['output'] | null => {
const key = getKey(node)
if (key === null) return null
const handler = nodes[key]
if (!handler) return null
return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)
},
}
return {
name,
options: resolvedOptions,
transform: context.transform,
print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,
}
}
}
}
+64
-4
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_response = require("./response-hnSw2NKE.cjs");
const require_factory = require("./factory-DG1CVkEb.cjs");
const require_response = require("./response-BeG6hq2D.cjs");
//#region src/factory.ts
var factory_exports = /* @__PURE__ */ require_response.__exportAll({
createArrowFunction: () => require_response.createArrowFunction,
createBreak: () => require_response.createBreak,
createConst: () => require_response.createConst,
createContent: () => require_response.createContent,
createExport: () => require_response.createExport,
createFile: () => require_response.createFile,
createFunction: () => require_response.createFunction,
createFunctionParameter: () => require_response.createFunctionParameter,
createFunctionParameters: () => require_response.createFunctionParameters,
createImport: () => require_response.createImport,
createIndexedAccessType: () => require_response.createIndexedAccessType,
createInput: () => require_response.createInput,
createJsx: () => require_response.createJsx,
createObjectBindingPattern: () => require_response.createObjectBindingPattern,
createOperation: () => require_response.createOperation,
createOutput: () => require_response.createOutput,
createParameter: () => require_response.createParameter,
createProperty: () => require_response.createProperty,
createRequestBody: () => require_response.createRequestBody,
createResponse: () => require_response.createResponse,
createSchema: () => require_response.createSchema,
createSource: () => require_response.createSource,
createText: () => require_response.createText,
createType: () => require_response.createType,
createTypeLiteral: () => require_response.createTypeLiteral,
update: () => update
});
/**
* Identity-preserving node update: returns `node` unchanged when every field in
* `changes` already equals (by reference) the current value, otherwise a new node
* with the changes applied.
*
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
* downstream passes can detect "nothing changed" by identity. Comparison is
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
*
* @example
* ```ts
* update(node, { name: node.name }) // -> same `node` reference
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
* ```
*/
function update(node, changes) {
for (const key in changes) if (changes[key] !== node[key]) return {
...node,
...changes
};
return node;
}
//#endregion
exports.createArrowFunction = require_response.createArrowFunction;

@@ -9,3 +61,3 @@ exports.createBreak = require_response.createBreak;

exports.createExport = require_response.createExport;
exports.createFile = require_factory.createFile;
exports.createFile = require_response.createFile;
exports.createFunction = require_response.createFunction;

@@ -30,2 +82,10 @@ exports.createFunctionParameter = require_response.createFunctionParameter;

exports.createTypeLiteral = require_response.createTypeLiteral;
exports.update = require_factory.update;
Object.defineProperty(exports, "factory_exports", {
enumerable: true,
get: function() {
return factory_exports;
}
});
exports.update = update;
//# sourceMappingURL=factory.cjs.map
+2
-37
import { n as __name, t as __exportAll } from "./chunk-CNktS9qV.js";
import { F as createFunctionParameter, Gt as createBreak, I as createFunctionParameters, Jt as createJsx, K as FileNode, Kt as createConst, L as createIndexedAccessType, Ot as createProperty, R as createObjectBindingPattern, T as createParameter, Wt as createArrowFunction, X as createImport, Xt as createType, Y as createExport, Yt as createText, Z as createSource, at as createContent, g as createResponse, p as createOperation, qt as createFunction, r as createOutput, s as createInput, t as Node, wt as createSchema, x as createRequestBody, z as createTypeLiteral } from "./index-BzjwdK2M.js";
import { F as createFunctionParameters, Gt as createBreak, I as createIndexedAccessType, J as UserFileNode, Jt as createJsx, Kt as createConst, L as createObjectBindingPattern, Ot as createProperty, P as createFunctionParameter, Q as createSource, R as createTypeLiteral, Wt as createArrowFunction, X as createFile, Xt as createType, Y as createExport, Yt as createText, Z as createImport, _ as createResponse, at as createContent, b as createRequestBody, p as createOperation, qt as createFunction, r as createOutput, s as createInput, t as Node, w as createParameter, wt as createSchema } from "./index-CTaN_ee_.js";

@@ -25,39 +25,4 @@ //#region src/factory.d.ts

declare function update<T extends Node>(node: T, changes: Partial<T>): T;
/**
* Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed
* and `imports`/`exports`/`sources` are deduplicated.
*/
type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> & Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>;
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
declare function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta>;
//#endregion
export { UserFileNode, createArrowFunction, createBreak, createConst, createContent, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, factory_d_exports as t, update };
export { type UserFileNode, createArrowFunction, createBreak, createConst, createContent, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, factory_d_exports as t, update };
//# sourceMappingURL=factory.d.ts.map

@@ -1,3 +0,58 @@

import { B as createFunction, D as createSource, E as createImport, H as createText, L as createArrowFunction, N as createContent, R as createBreak, T as createExport, U as createType, V as createJsx, X as createSchema, _ as createIndexedAccessType, a as createParameter, d as createRequestBody, g as createFunctionParameters, h as createFunctionParameter, l as createOperation, p as createInput, r as createProperty, s as createOutput, t as createResponse, v as createObjectBindingPattern, y as createTypeLiteral, z as createConst } from "./response-KUdWiDWw.js";
import { r as update, t as createFile } from "./factory-CZNOGI-N.js";
export { createArrowFunction, createBreak, createConst, createContent, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, update };
import { t as __exportAll } from "./chunk-CNktS9qV.js";
import { B as createConst, D as createImport, E as createFile, H as createJsx, O as createSource, P as createContent, Q as createSchema, R as createArrowFunction, T as createExport, U as createText, V as createFunction, W as createType, _ as createIndexedAccessType, a as createParameter, d as createRequestBody, g as createFunctionParameters, h as createFunctionParameter, l as createOperation, p as createInput, r as createProperty, s as createOutput, t as createResponse, v as createObjectBindingPattern, y as createTypeLiteral, z as createBreak } from "./response-Rd1uisM1.js";
//#region src/factory.ts
var factory_exports = /* @__PURE__ */ __exportAll({
createArrowFunction: () => createArrowFunction,
createBreak: () => createBreak,
createConst: () => createConst,
createContent: () => createContent,
createExport: () => createExport,
createFile: () => createFile,
createFunction: () => createFunction,
createFunctionParameter: () => createFunctionParameter,
createFunctionParameters: () => createFunctionParameters,
createImport: () => createImport,
createIndexedAccessType: () => createIndexedAccessType,
createInput: () => createInput,
createJsx: () => createJsx,
createObjectBindingPattern: () => createObjectBindingPattern,
createOperation: () => createOperation,
createOutput: () => createOutput,
createParameter: () => createParameter,
createProperty: () => createProperty,
createRequestBody: () => createRequestBody,
createResponse: () => createResponse,
createSchema: () => createSchema,
createSource: () => createSource,
createText: () => createText,
createType: () => createType,
createTypeLiteral: () => createTypeLiteral,
update: () => update
});
/**
* Identity-preserving node update: returns `node` unchanged when every field in
* `changes` already equals (by reference) the current value, otherwise a new node
* with the changes applied.
*
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
* downstream passes can detect "nothing changed" by identity. Comparison is
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
*
* @example
* ```ts
* update(node, { name: node.name }) // -> same `node` reference
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
* ```
*/
function update(node, changes) {
for (const key in changes) if (changes[key] !== node[key]) return {
...node,
...changes
};
return node;
}
//#endregion
export { createArrowFunction, createBreak, createConst, createContent, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, factory_exports as t, update };
//# sourceMappingURL=factory.js.map
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_response = require("./response-hnSw2NKE.cjs");
const require_visitor = require("./visitor-DpKZ9Tk0.cjs");
const require_factory = require("./factory-DG1CVkEb.cjs");
require("./utils-BLJwyza-.cjs");
const require_extractStringsFromNodes = require("./extractStringsFromNodes-DKgDjFO0.cjs");
const require_defineMacro = require("./defineMacro-Bv9R_9a2.cjs");
const require_response = require("./response-BeG6hq2D.cjs");
const require_visitor = require("./visitor-B7Zlnp4c.cjs");
require("./utils-BDI3kXlb.cjs");
const require_defineMacro = require("./defineMacro-BryDiuQl.cjs");
const require_factory = require("./factory.cjs");
let node_crypto = require("node:crypto");

@@ -338,4 +337,4 @@ //#region src/signature.ts

function record(schemaNode) {
if (!isCandidate(schemaNode)) return;
const signature = signatureOf(schemaNode);
if (!isCandidate(schemaNode)) return;
const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name;

@@ -394,3 +393,3 @@ const group = groups.get(signature);

/**
* Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the
* Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the
* dialect's type for inference.

@@ -400,17 +399,20 @@ *

* ```ts
* export const oasDialect = defineSchemaDialect({
* export const oasDialect = defineDialect({
* name: 'oas',
* isNullable,
* isReference,
* isDiscriminator,
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
* resolveRef,
* schema: {
* isNullable,
* isReference,
* isDiscriminator,
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
* resolveRef,
* optionality,
* },
* })
* ```
*/
function defineSchemaDialect(dialect) {
function defineDialect(dialect) {
return dialect;
}
//#endregion
//#region src/printer.ts
//#region src/definePrinter.ts
/**

@@ -502,8 +504,7 @@ * Defines a schema printer: a function that takes a `SchemaNode` and emits

exports.createPrinterFactory = createPrinterFactory;
exports.defineDialect = defineDialect;
exports.defineMacro = require_defineMacro.defineMacro;
exports.defineNode = require_response.defineNode;
exports.definePrinter = definePrinter;
exports.defineSchemaDialect = defineSchemaDialect;
exports.exportDef = require_response.exportDef;
exports.extractStringsFromNodes = require_extractStringsFromNodes.extractStringsFromNodes;
Object.defineProperty(exports, "factory", {

@@ -519,3 +520,2 @@ enumerable: true,

exports.functionParametersDef = require_response.functionParametersDef;
exports.httpMethods = require_visitor.httpMethods;
exports.importDef = require_response.importDef;

@@ -539,3 +539,2 @@ exports.indexedAccessTypeDef = require_response.indexedAccessTypeDef;

exports.sourceDef = require_response.sourceDef;
exports.syncOptionality = require_response.syncOptionality;
exports.textDef = require_response.textDef;

@@ -542,0 +541,0 @@ exports.transform = require_visitor.transform;

@@ -1,1 +0,1 @@

{"version":3,"file":"index.cjs","names":["extractRefName","obj","createSchema","transform","extractRefName","collectLazy"],"sources":["../src/signature.ts","../src/dedupe.ts","../src/dialect.ts","../src/printer.ts"],"sourcesContent":["import { hash } from 'node:crypto'\nimport type { SchemaNode } from './nodes/index.ts'\nimport { extractRefName } from './utils/index.ts'\n\n/**\n * The flags shared by every node kind that affect its type: `primitive`, `format`, `nullable`.\n */\nfunction flagsDescriptor(node: SchemaNode): string {\n return `${node.primitive ?? ''};${node.format ?? ''};${node.nullable ? 1 : 0}`\n}\n\nfunction refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {\n if (node.ref) return extractRefName(node.ref)\n return node.name ?? ''\n}\n\ntype ScalarField = { kind: 'scalar'; key: string; prefix: string }\ntype BoolField = { kind: 'bool'; key: string; prefix: string }\ntype ChildField = { kind: 'child'; key: string; prefix: string }\ntype ChildrenField = { kind: 'children'; key: string; prefix: string }\ntype ObjectPropsField = { kind: 'objectProps' }\ntype AdditionalPropsField = { kind: 'additionalProps' }\ntype PatternPropsField = { kind: 'patternProps' }\ntype EnumValuesField = { kind: 'enumValues' }\ntype RefTargetField = { kind: 'refTarget' }\n\ntype ShapeField =\n | ScalarField\n | BoolField\n | ChildField\n | ChildrenField\n | ObjectPropsField\n | AdditionalPropsField\n | PatternPropsField\n | EnumValuesField\n | RefTargetField\n\nconst arrayTupleFields: ReadonlyArray<ShapeField> = [\n { kind: 'children', key: 'items', prefix: 'i' },\n { kind: 'child', key: 'rest', prefix: 'r' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'bool', key: 'unique', prefix: 'u' },\n]\n\nconst numericFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'exclusiveMinimum', prefix: 'emn' },\n { kind: 'scalar', key: 'exclusiveMaximum', prefix: 'emx' },\n { kind: 'scalar', key: 'multipleOf', prefix: 'mo' },\n]\n\nconst rangeFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n]\n\n/**\n * Maps each node `type` to its ordered shape-contributing fields. Types absent from the map\n * (boolean, null, any, and other scalars) fall back to `${type}|${flags}`.\n */\nconst SHAPE_KEYS: Partial<Record<SchemaNode['type'], ReadonlyArray<ShapeField>>> = {\n object: [\n { kind: 'objectProps' },\n { kind: 'additionalProps' },\n { kind: 'patternProps' },\n { kind: 'scalar', key: 'minProperties', prefix: 'mn' },\n { kind: 'scalar', key: 'maxProperties', prefix: 'mx' },\n ],\n array: arrayTupleFields,\n tuple: arrayTupleFields,\n union: [\n { kind: 'scalar', key: 'strategy', prefix: 's' },\n { kind: 'scalar', key: 'discriminatorPropertyName', prefix: 'd' },\n { kind: 'children', key: 'members', prefix: 'm' },\n ],\n intersection: [{ kind: 'children', key: 'members', prefix: 'm' }],\n enum: [{ kind: 'enumValues' }],\n ref: [{ kind: 'refTarget' }],\n string: [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'pattern', prefix: 'pt' },\n ],\n number: numericFields,\n integer: numericFields,\n bigint: numericFields,\n url: [\n { kind: 'scalar', key: 'path', prefix: 'path' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n ],\n uuid: rangeFields,\n email: rangeFields,\n datetime: [\n { kind: 'bool', key: 'offset', prefix: 'o' },\n { kind: 'bool', key: 'local', prefix: 'l' },\n ],\n date: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n time: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n}\n\nfunction serializeShapeField(field: ShapeField, node: SchemaNode, record: Record<string, unknown>): string {\n switch (field.kind) {\n case 'scalar':\n return `${field.prefix}:${record[field.key] ?? ''}`\n case 'bool':\n return `${field.prefix}:${record[field.key] ? 1 : 0}`\n case 'child': {\n const child = record[field.key] as SchemaNode | undefined\n return `${field.prefix}:${child ? signatureOf(child) : ''}`\n }\n case 'children': {\n const children = (record[field.key] as Array<SchemaNode> | undefined) ?? []\n return `${field.prefix}[${children.map((c) => signatureOf(c)).join(',')}]`\n }\n case 'objectProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const props = (obj.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema)}`).join(',')\n return `p[${props}]`\n }\n case 'additionalProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n if (typeof obj.additionalProperties === 'boolean') return `ab:${obj.additionalProperties}`\n if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`\n return ''\n }\n case 'patternProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const pattern = obj.patternProperties\n ? Object.keys(obj.patternProperties)\n .sort()\n .map((key) => `${key}=${signatureOf(obj.patternProperties![key]!)}`)\n .join(',')\n : ''\n return `pp[${pattern}]`\n }\n case 'enumValues': {\n const en = node as Extract<SchemaNode, { type: 'enum' }>\n let values = ''\n if (en.namedEnumValues?.length) {\n values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')\n } else if (en.enumValues?.length) {\n values = en.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')\n }\n return `v[${values}]`\n }\n case 'refTarget': {\n return `->${refTargetName(node as Extract<SchemaNode, { type: 'ref' }>)}`\n }\n }\n}\n\n/**\n * Builds the local shape descriptor that {@link signatureOf} hashes: the node's kind, flags,\n * constraints, and its children's signatures.\n */\nfunction describeShape(node: SchemaNode): string {\n const flags = flagsDescriptor(node)\n const fields = SHAPE_KEYS[node.type]\n if (!fields) return `${node.type}|${flags}`\n\n const record = node as unknown as Record<string, unknown>\n const parts: Array<string> = [`${node.type}|${flags}`]\n for (const field of fields) {\n parts.push(serializeShapeField(field, node, record))\n }\n return parts.join('|')\n}\n\n/**\n * Caches the digest per node, keyed by identity. A `WeakMap` so entries die with the node, and so\n * a tree hashed during dedupe planning is not walked again when it is rewritten during streaming.\n * Reuse is safe because a signature depends only on content, and nodes are immutable once created.\n */\nconst signatureCache = new WeakMap<SchemaNode, string>()\n\n/**\n * Computes a deterministic, shape-only signature (a content hash) for a schema node. Two schemas\n * share a signature when they are structurally identical, ignoring documentation (`name`, `title`,\n * `description`, `example`, `default`, `deprecated`) and usage-slot flags (`optional`, `nullish`,\n * `readOnly`, `writeOnly`). `nullable` is kept because it changes the produced type, and `ref`\n * nodes compare by target name, which also terminates on circular shapes.\n *\n * @example Two enums with different descriptions share a signature\n * ```ts\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))\n * ```\n */\nexport function signatureOf(node: SchemaNode): string {\n const cached = signatureCache.get(node)\n if (cached !== undefined) return cached\n const signature = hash('sha256', describeShape(node), 'hex')\n signatureCache.set(node, signature)\n\n return signature\n}\n\n/**\n * Returns `true` when two schema nodes are structurally identical under shape-only equality,\n * meaning they produce the same TypeScript type.\n */\nexport function isSchemaEqual(a: SchemaNode, b: SchemaNode): boolean {\n return signatureOf(a) === signatureOf(b)\n}\n","/**\n * Schema-shape deduplication. `buildDedupePlan` finds top-level and inline schemas that share a\n * structural signature, picks one canonical definition, and `applyDedupe` repoints every duplicate\n * at it. This works on `SchemaNode` content, not on files.\n *\n * For merging a file's imports, exports, and source nodes, see `utils/fileMerge.ts`. Same idea of\n * collapsing duplicates, but a different domain.\n */\nimport type { Node, OperationNode, SchemaNode } from './nodes/index.ts'\nimport { createSchema } from './nodes/schema.ts'\nimport { signatureOf } from './signature.ts'\nimport { extractRefName } from './utils/index.ts'\nimport { collectLazy, transform } from './visitor.ts'\n\n/**\n * A canonical destination for a deduplicated shape: the shared schema name and\n * the synthetic `$ref` path that points at it.\n */\nexport type DedupeCanonical = {\n /**\n * Canonical schema name every duplicate occurrence refers to.\n */\n name: string\n /**\n * `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).\n */\n ref: string\n}\n\n/**\n * The result of {@link buildDedupePlan}: a lookup from structural signature to its\n * canonical target, plus the freshly hoisted definitions that must be added to\n * the schema list.\n */\nexport type DedupePlan = {\n /**\n * Maps a structural signature to the canonical schema that represents it.\n */\n canonicalBySignature: Map<string, DedupeCanonical>\n /**\n * Maps the name of a top-level schema that duplicates a canonical one to that canonical, so\n * references to the duplicate can be repointed at the first schema with the same content.\n */\n aliasNames: Map<string, DedupeCanonical>\n /**\n * New top-level schema definitions created for inline shapes that had no existing\n * named component. Nested duplicates inside each definition are already collapsed.\n */\n hoisted: Array<SchemaNode>\n}\n\n/**\n * The lookups {@link applyDedupe} needs from a {@link DedupePlan}.\n */\nexport type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>\n\n/**\n * Options that inject the naming and candidate policy into {@link buildDedupePlan}.\n * The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.\n */\nexport type BuildDedupePlanOptions = {\n /**\n * Returns `true` when a node should be deduplicated. This is the only gate, so it must\n * reject both ineligible kinds (return `false` for anything other than, say, enums and\n * objects) and unsafe shapes (e.g. nodes that reference a circular schema).\n */\n isCandidate: (node: SchemaNode) => boolean\n /**\n * Produces the canonical name for an inline shape with no existing named component.\n * Return `null` to leave the shape inline (for example when no contextual name exists).\n */\n nameFor: (node: SchemaNode, signature: string) => string | null\n /**\n * Builds the `$ref` path for a canonical name.\n */\n refFor: (name: string) => string\n /**\n * Minimum number of occurrences before a shape is deduplicated.\n *\n * @default 2\n */\n minOccurrences?: number\n}\n\n/**\n * Builds the shared `ref` replacement for a duplicate occurrence, carrying the\n * usage-slot and documentation fields that are not part of the canonical type.\n */\nfunction createRefNode(node: SchemaNode, canonical: DedupeCanonical): SchemaNode {\n return createSchema({\n type: 'ref',\n name: canonical.name,\n ref: canonical.ref,\n optional: node.optional,\n nullish: node.nullish,\n readOnly: node.readOnly,\n writeOnly: node.writeOnly,\n deprecated: node.deprecated,\n description: node.description,\n default: node.default,\n example: node.example,\n })\n}\n\n/**\n * Rewrites a node, replacing every candidate sub-schema whose signature has a canonical\n * target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,\n * so nested duplicates inside a replaced shape are not visited again. A `ref` that points\n * at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with\n * the same content.\n *\n * Pass `skipRootMatch` when rewriting a canonical definition so its own root is not\n * turned into a reference to itself. Nested duplicates are still collapsed.\n *\n * @example\n * ```ts\n * const next = applyDedupe(operationNode, plan)\n * ```\n */\nexport function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode\nexport function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode\nexport function applyDedupe(node: Node, plan: DedupeLookups, skipRootMatch = false): Node {\n const { canonicalBySignature, aliasNames } = plan\n if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node\n\n const root = node\n\n return transform(node, {\n schema(schemaNode) {\n if (schemaNode.type === 'ref') {\n const target = schemaNode.ref ? extractRefName(schemaNode.ref) : schemaNode.name\n const canonical = target ? aliasNames.get(target) : undefined\n\n return canonical ? { ...schemaNode, name: canonical.name, ref: canonical.ref } : undefined\n }\n\n const signature = signatureOf(schemaNode)\n if (skipRootMatch && schemaNode === root) return undefined\n\n const canonical = canonicalBySignature.get(signature)\n if (!canonical) return undefined\n\n return createRefNode(schemaNode, canonical)\n },\n })\n}\n\n/**\n * Strips usage-slot flags from a hoisted definition and applies its canonical name.\n * A standalone definition is never optional, so `optional`/`nullish` are cleared.\n */\nfunction cleanDefinition(node: SchemaNode, name: string): SchemaNode {\n return { ...node, name, optional: undefined, nullish: undefined }\n}\n\n/**\n * Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.\n *\n * A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence\n * is a named top-level schema, the first one becomes the canonical (so other top-level\n * duplicates and inline copies turn into references to it). Every other top-level name with\n * the same content is recorded in `aliasNames`, so refs to it can be repointed at the\n * canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied\n * per node with {@link applyDedupe}.\n *\n * @example\n * ```ts\n * const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {\n * isCandidate: (node) => node.type === 'enum' || node.type === 'object',\n * nameFor: (node) => node.name ?? null,\n * refFor: (name) => `#/components/schemas/${name}`,\n * })\n * ```\n */\nexport function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {\n const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options\n\n const topLevelNodes = new Set<SchemaNode>()\n\n type Group = {\n count: number\n representative: SchemaNode\n topLevelNames: Array<string>\n }\n const groups = new Map<string, Group>()\n\n function record(schemaNode: SchemaNode): void {\n const signature = signatureOf(schemaNode)\n if (!isCandidate(schemaNode)) return\n\n const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name\n const group = groups.get(signature)\n if (group) {\n group.count++\n if (isTopLevel) group.topLevelNames.push(schemaNode.name!)\n } else {\n groups.set(signature, { count: 1, representative: schemaNode, topLevelNames: isTopLevel ? [schemaNode.name!] : [] })\n }\n }\n\n for (const root of roots) {\n if (root.kind === 'Schema') topLevelNodes.add(root)\n for (const schemaNode of collectLazy<SchemaNode>(root, { schema: (node) => node })) {\n record(schemaNode)\n }\n }\n\n const canonicalBySignature = new Map<string, DedupeCanonical>()\n const aliasNames = new Map<string, DedupeCanonical>()\n const pendingHoists: Array<{ name: string; representative: SchemaNode }> = []\n\n for (const [signature, group] of groups) {\n if (group.count < minOccurrences) continue\n\n const [firstName, ...duplicateNames] = group.topLevelNames\n if (firstName) {\n const canonical: DedupeCanonical = { name: firstName, ref: refFor(firstName) }\n canonicalBySignature.set(signature, canonical)\n for (const duplicate of duplicateNames) {\n aliasNames.set(duplicate, canonical)\n }\n continue\n }\n\n const name = nameFor(group.representative, signature)\n if (!name) continue\n\n canonicalBySignature.set(signature, { name, ref: refFor(name) })\n pendingHoists.push({ name, representative: group.representative })\n }\n\n // Build hoisted definitions only after every canonical name is known, so nested\n // duplicates inside a definition also resolve to refs.\n const hoisted = pendingHoists.map(({ name, representative }) =>\n cleanDefinition(applyDedupe(representative, { canonicalBySignature, aliasNames }, true), name),\n )\n\n return { canonicalBySignature, aliasNames, hoisted }\n}\n","/**\n * The spec-specific questions a schema parser answers while turning a source document into Kubb\n * AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where\n * OpenAPI, AsyncAPI, and plain JSON Schema differ.\n */\nexport type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {\n /**\n * Identifies the dialect in logs and diagnostics.\n */\n name: string\n /**\n * Whether the schema is nullable.\n */\n isNullable: (schema?: TSchema) => boolean\n /**\n * Whether the value is a `$ref` pointer.\n */\n isReference: (value?: unknown) => value is TRef\n /**\n * Whether the schema carries a discriminator for polymorphism.\n */\n isDiscriminator: (value?: unknown) => value is TDiscriminated\n /**\n * Whether the schema is binary data, converted to a `blob` node.\n */\n isBinary: (schema: TSchema) => boolean\n /**\n * Resolves a local `$ref` against the document, or nullish when it cannot.\n */\n resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined\n}\n\n/**\n * Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the\n * dialect's type for inference.\n *\n * @example\n * ```ts\n * export const oasDialect = defineSchemaDialect({\n * name: 'oas',\n * isNullable,\n * isReference,\n * isDiscriminator,\n * isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',\n * resolveRef,\n * })\n * ```\n */\nexport function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(\n dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>,\n): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument> {\n return dialect\n}\n","import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'\n\n/**\n * Runtime context passed as `this` to printer handlers.\n *\n * `this.transform` dispatches to node-level handlers from `nodes`.\n *\n * @example\n * ```ts\n * const context: PrinterHandlerContext<string, {}> = {\n * options: {},\n * transform: () => 'value',\n * }\n * ```\n */\ntype PrinterHandlerContext<TOutput, TOptions extends object> = {\n /**\n * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.\n * Use `this.transform` inside `nodes` handlers and inside the `print` override.\n */\n transform: (node: SchemaNode) => TOutput | null\n /**\n * Options for this printer instance.\n */\n options: TOptions\n}\n\n/**\n * Handler for one schema node type.\n *\n * Use a regular function (not an arrow function) if you need `this`.\n *\n * @example\n * ```ts\n * const handler: PrinterHandler<string, {}, 'string'> = function () {\n * return 'string'\n * }\n * ```\n */\ntype PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (\n this: PrinterHandlerContext<TOutput, TOptions>,\n node: SchemaNodeByType[T],\n) => TOutput | null\n\n/**\n * Partial map of per-node-type handler overrides for a printer.\n *\n * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).\n * Supply only the handlers you want to replace. The printer's built-in\n * defaults fill in the rest.\n *\n * @example\n * ```ts\n * pluginZod({\n * printer: {\n * nodes: {\n * date(): string {\n * return 'z.string().date()'\n * },\n * } satisfies PrinterPartial<string, PrinterZodOptions>,\n * },\n * })\n * ```\n */\nexport type PrinterPartial<TOutput, TOptions extends object> = Partial<{\n [K in SchemaType]: PrinterHandler<TOutput, TOptions, K>\n}>\n\n/**\n * Generic shape used by `definePrinter`.\n *\n * - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)\n * - `TOptions` options passed to and stored on the printer instance\n * - `TOutput` the type emitted by node handlers\n * - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)\n *\n * @example\n * ```ts\n * type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>\n * ```\n */\nexport type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {\n name: TName\n options: TOptions\n output: TOutput\n printOutput: TPrintOutput\n}\n\n/**\n * Printer instance returned by a printer factory.\n *\n * @example\n * ```ts\n * const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})\n * ```\n */\nexport type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {\n /**\n * Unique identifier supplied at creation time.\n */\n name: T['name']\n /**\n * Options for this printer instance.\n */\n options: T['options']\n /**\n * Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.\n * Always dispatches through the `nodes` map. Never calls the `print` override.\n * Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.\n */\n transform: (node: SchemaNode) => T['output'] | null\n /**\n * Public printer. If the builder provides a root-level `print`, this calls that\n * higher-level function (which may produce full declarations).\n * Otherwise, falls back to the node-level dispatcher.\n */\n print: (node: SchemaNode) => T['printOutput'] | null\n}\n\n/**\n * Builder function passed to `definePrinter`.\n *\n * It receives resolved options and returns:\n * - `name`\n * - `options`\n * - `nodes` handlers\n * - optional top-level `print` override\n *\n * @example\n * ```ts\n * const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })\n * ```\n */\ntype PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {\n name: T['name']\n /**\n * Options to store on the printer.\n */\n options: T['options']\n nodes: Partial<{\n [K in SchemaType]: PrinterHandler<T['output'], T['options'], K>\n }>\n /**\n * Optional root-level print override. When provided, becomes the public `printer.print`.\n * Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),\n * not the override itself, so recursion is safe.\n */\n print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null\n}\n/**\n * Defines a schema printer: a function that takes a `SchemaNode` and emits\n * code in your target language. Each plugin that produces code from schemas\n * (TypeScript types, Zod schemas, Faker factories) ships a printer built\n * with this helper.\n *\n * The builder receives resolved options and returns:\n *\n * - `name` unique identifier for the printer.\n * - `options` stored on the returned printer instance.\n * - `nodes` map of `SchemaType` → handler. Handlers return the rendered\n * output (a string, a TypeScript AST node, ...) for that schema type.\n * - `print` (optional), top-level override exposed as `printer.print`.\n * Use `this.transform(node)` inside it to dispatch to `nodes` recursively.\n *\n * Without a `print` override, `printer.print` falls back to `printer.transform`\n * (the node-level dispatcher).\n *\n * @example Tiny Zod printer\n * ```ts\n * import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'\n *\n * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>\n *\n * export const zodPrinter = definePrinter<PrinterZod>((options) => ({\n * name: 'zod',\n * options: { strict: options.strict ?? true },\n * nodes: {\n * string: () => 'z.string()',\n * object(node) {\n * const props = node.properties\n * .map((p) => `${p.name}: ${this.transform(p.schema)}`)\n * .join(', ')\n * return `z.object({ ${props} })`\n * },\n * },\n * }))\n * ```\n */\nexport function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {\n return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>\n}\n\n/**\n * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.\n *\n * @example\n * ```ts\n * export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(\n * (node) => node.kind,\n * )\n * ```\n */\nexport function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {\n return function <T extends PrinterFactoryOptions>(\n build: (options: T['options']) => {\n name: T['name']\n options: T['options']\n nodes: Partial<{\n [K in TKey]: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNodeByKey[K],\n ) => T['output'] | null\n }>\n print?: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNode,\n ) => T['printOutput'] | null\n },\n ): (options?: T['options']) => {\n name: T['name']\n options: T['options']\n transform: (node: TNode) => T['output'] | null\n print: (node: TNode) => T['printOutput'] | null\n } {\n return (options) => {\n const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))\n\n const context = {\n options: resolvedOptions,\n transform: (node: TNode): T['output'] | null => {\n const key = getKey(node)\n if (key === null) return null\n\n const handler = nodes[key]\n\n if (!handler) return null\n\n return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)\n },\n }\n\n return {\n name,\n options: resolvedOptions,\n transform: context.transform,\n print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;AAOA,SAAS,gBAA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O;IACL;IACA,SAAS;IACT,WAAW,QAAQ;IACnB,OAAQ,gBAAgB,cAAc,KAAK,OAAO,IAAI,QAAQ;GAChE;EACF;CACF;AACF"}
{"version":3,"file":"index.cjs","names":["extractRefName","obj","createSchema","transform","extractRefName","collectLazy"],"sources":["../src/signature.ts","../src/dedupe.ts","../src/dialect.ts","../src/definePrinter.ts"],"sourcesContent":["import { hash } from 'node:crypto'\nimport type { SchemaNode } from './nodes/index.ts'\nimport { extractRefName } from './utils/index.ts'\n\n/**\n * The flags shared by every node kind that affect its type: `primitive`, `format`, `nullable`.\n */\nfunction flagsDescriptor(node: SchemaNode): string {\n return `${node.primitive ?? ''};${node.format ?? ''};${node.nullable ? 1 : 0}`\n}\n\nfunction refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {\n if (node.ref) return extractRefName(node.ref)\n return node.name ?? ''\n}\n\ntype ScalarField = { kind: 'scalar'; key: string; prefix: string }\ntype BoolField = { kind: 'bool'; key: string; prefix: string }\ntype ChildField = { kind: 'child'; key: string; prefix: string }\ntype ChildrenField = { kind: 'children'; key: string; prefix: string }\ntype ObjectPropsField = { kind: 'objectProps' }\ntype AdditionalPropsField = { kind: 'additionalProps' }\ntype PatternPropsField = { kind: 'patternProps' }\ntype EnumValuesField = { kind: 'enumValues' }\ntype RefTargetField = { kind: 'refTarget' }\n\ntype ShapeField =\n | ScalarField\n | BoolField\n | ChildField\n | ChildrenField\n | ObjectPropsField\n | AdditionalPropsField\n | PatternPropsField\n | EnumValuesField\n | RefTargetField\n\nconst arrayTupleFields: ReadonlyArray<ShapeField> = [\n { kind: 'children', key: 'items', prefix: 'i' },\n { kind: 'child', key: 'rest', prefix: 'r' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'bool', key: 'unique', prefix: 'u' },\n]\n\nconst numericFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'exclusiveMinimum', prefix: 'emn' },\n { kind: 'scalar', key: 'exclusiveMaximum', prefix: 'emx' },\n { kind: 'scalar', key: 'multipleOf', prefix: 'mo' },\n]\n\nconst rangeFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n]\n\n/**\n * Maps each node `type` to its ordered shape-contributing fields. Types absent from the map\n * (boolean, null, any, and other scalars) fall back to `${type}|${flags}`.\n */\nconst SHAPE_KEYS: Partial<Record<SchemaNode['type'], ReadonlyArray<ShapeField>>> = {\n object: [\n { kind: 'objectProps' },\n { kind: 'additionalProps' },\n { kind: 'patternProps' },\n { kind: 'scalar', key: 'minProperties', prefix: 'mn' },\n { kind: 'scalar', key: 'maxProperties', prefix: 'mx' },\n ],\n array: arrayTupleFields,\n tuple: arrayTupleFields,\n union: [\n { kind: 'scalar', key: 'strategy', prefix: 's' },\n { kind: 'scalar', key: 'discriminatorPropertyName', prefix: 'd' },\n { kind: 'children', key: 'members', prefix: 'm' },\n ],\n intersection: [{ kind: 'children', key: 'members', prefix: 'm' }],\n enum: [{ kind: 'enumValues' }],\n ref: [{ kind: 'refTarget' }],\n string: [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'pattern', prefix: 'pt' },\n ],\n number: numericFields,\n integer: numericFields,\n bigint: numericFields,\n url: [\n { kind: 'scalar', key: 'path', prefix: 'path' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n ],\n uuid: rangeFields,\n email: rangeFields,\n datetime: [\n { kind: 'bool', key: 'offset', prefix: 'o' },\n { kind: 'bool', key: 'local', prefix: 'l' },\n ],\n date: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n time: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n}\n\nfunction serializeShapeField(field: ShapeField, node: SchemaNode, record: Record<string, unknown>): string {\n switch (field.kind) {\n case 'scalar':\n return `${field.prefix}:${record[field.key] ?? ''}`\n case 'bool':\n return `${field.prefix}:${record[field.key] ? 1 : 0}`\n case 'child': {\n const child = record[field.key] as SchemaNode | undefined\n return `${field.prefix}:${child ? signatureOf(child) : ''}`\n }\n case 'children': {\n const children = (record[field.key] as Array<SchemaNode> | undefined) ?? []\n return `${field.prefix}[${children.map((c) => signatureOf(c)).join(',')}]`\n }\n case 'objectProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const props = (obj.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema)}`).join(',')\n return `p[${props}]`\n }\n case 'additionalProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n if (typeof obj.additionalProperties === 'boolean') return `ab:${obj.additionalProperties}`\n if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`\n return ''\n }\n case 'patternProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const pattern = obj.patternProperties\n ? Object.keys(obj.patternProperties)\n .sort()\n .map((key) => `${key}=${signatureOf(obj.patternProperties![key]!)}`)\n .join(',')\n : ''\n return `pp[${pattern}]`\n }\n case 'enumValues': {\n const en = node as Extract<SchemaNode, { type: 'enum' }>\n let values = ''\n if (en.namedEnumValues?.length) {\n values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')\n } else if (en.enumValues?.length) {\n values = en.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')\n }\n return `v[${values}]`\n }\n case 'refTarget': {\n return `->${refTargetName(node as Extract<SchemaNode, { type: 'ref' }>)}`\n }\n }\n}\n\n/**\n * Builds the local shape descriptor that {@link signatureOf} hashes: the node's kind, flags,\n * constraints, and its children's signatures.\n */\nfunction describeShape(node: SchemaNode): string {\n const flags = flagsDescriptor(node)\n const fields = SHAPE_KEYS[node.type]\n if (!fields) return `${node.type}|${flags}`\n\n const record = node as unknown as Record<string, unknown>\n const parts: Array<string> = [`${node.type}|${flags}`]\n for (const field of fields) {\n parts.push(serializeShapeField(field, node, record))\n }\n return parts.join('|')\n}\n\n/**\n * Caches the digest per node, keyed by identity. A `WeakMap` so entries die with the node, and so\n * a tree hashed during dedupe planning is not walked again when it is rewritten during streaming.\n * Reuse is safe because a signature depends only on content, and nodes are immutable once created.\n */\nconst signatureCache = new WeakMap<SchemaNode, string>()\n\n/**\n * Computes a deterministic, shape-only signature (a content hash) for a schema node. Two schemas\n * share a signature when they are structurally identical, ignoring documentation (`name`, `title`,\n * `description`, `example`, `default`, `deprecated`) and usage-slot flags (`optional`, `nullish`,\n * `readOnly`, `writeOnly`). `nullable` is kept because it changes the produced type, and `ref`\n * nodes compare by target name, which also terminates on circular shapes.\n *\n * @example Two enums with different descriptions share a signature\n * ```ts\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))\n * ```\n */\nexport function signatureOf(node: SchemaNode): string {\n const cached = signatureCache.get(node)\n if (cached !== undefined) return cached\n const signature = hash('sha256', describeShape(node), 'hex')\n signatureCache.set(node, signature)\n\n return signature\n}\n\n/**\n * Returns `true` when two schema nodes are structurally identical under shape-only equality,\n * meaning they produce the same TypeScript type.\n */\nexport function isSchemaEqual(a: SchemaNode, b: SchemaNode): boolean {\n return signatureOf(a) === signatureOf(b)\n}\n","import type { Node, OperationNode, SchemaNode } from './nodes/index.ts'\nimport { createSchema } from './nodes/schema.ts'\nimport { signatureOf } from './signature.ts'\nimport { extractRefName } from './utils/index.ts'\nimport { collectLazy, transform } from './visitor.ts'\n\n/**\n * A canonical destination for a deduplicated shape: the shared schema name and\n * the synthetic `$ref` path that points at it.\n */\nexport type DedupeCanonical = {\n /**\n * Canonical schema name every duplicate occurrence refers to.\n */\n name: string\n /**\n * `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).\n */\n ref: string\n}\n\n/**\n * The result of {@link buildDedupePlan}: a lookup from structural signature to its\n * canonical target, plus the freshly hoisted definitions that must be added to\n * the schema list.\n */\nexport type DedupePlan = {\n /**\n * Maps a structural signature to the canonical schema that represents it.\n */\n canonicalBySignature: Map<string, DedupeCanonical>\n /**\n * Maps the name of a top-level schema that duplicates a canonical one to that canonical, so\n * references to the duplicate can be repointed at the first schema with the same content.\n */\n aliasNames: Map<string, DedupeCanonical>\n /**\n * New top-level schema definitions created for inline shapes that had no existing\n * named component. Nested duplicates inside each definition are already collapsed.\n */\n hoisted: Array<SchemaNode>\n}\n\n/**\n * The lookups {@link applyDedupe} needs from a {@link DedupePlan}.\n */\nexport type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>\n\n/**\n * Options that inject the naming and candidate policy into {@link buildDedupePlan}.\n * The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.\n */\nexport type BuildDedupePlanOptions = {\n /**\n * Returns `true` when a node should be deduplicated. This is the only gate, so it must\n * reject both ineligible kinds (return `false` for anything other than, say, enums and\n * objects) and unsafe shapes (e.g. nodes that reference a circular schema).\n */\n isCandidate: (node: SchemaNode) => boolean\n /**\n * Produces the canonical name for an inline shape with no existing named component.\n * Return `null` to leave the shape inline (for example when no contextual name exists).\n */\n nameFor: (node: SchemaNode, signature: string) => string | null\n /**\n * Builds the `$ref` path for a canonical name.\n */\n refFor: (name: string) => string\n /**\n * Minimum number of occurrences before a shape is deduplicated.\n *\n * @default 2\n */\n minOccurrences?: number\n}\n\n/**\n * Builds the shared `ref` replacement for a duplicate occurrence, carrying the\n * usage-slot and documentation fields that are not part of the canonical type.\n */\nfunction createRefNode(node: SchemaNode, canonical: DedupeCanonical): SchemaNode {\n return createSchema({\n type: 'ref',\n name: canonical.name,\n ref: canonical.ref,\n optional: node.optional,\n nullish: node.nullish,\n readOnly: node.readOnly,\n writeOnly: node.writeOnly,\n deprecated: node.deprecated,\n description: node.description,\n default: node.default,\n example: node.example,\n })\n}\n\n/**\n * Rewrites a node, replacing every candidate sub-schema whose signature has a canonical\n * target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,\n * so nested duplicates inside a replaced shape are not visited again. A `ref` that points\n * at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with\n * the same content.\n *\n * Pass `skipRootMatch` when rewriting a canonical definition so its own root is not\n * turned into a reference to itself. Nested duplicates are still collapsed.\n *\n * @example\n * ```ts\n * const next = applyDedupe(operationNode, plan)\n * ```\n */\nexport function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode\nexport function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode\nexport function applyDedupe(node: Node, plan: DedupeLookups, skipRootMatch = false): Node {\n const { canonicalBySignature, aliasNames } = plan\n if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node\n\n const root = node\n\n return transform(node, {\n schema(schemaNode) {\n if (schemaNode.type === 'ref') {\n const target = schemaNode.ref ? extractRefName(schemaNode.ref) : schemaNode.name\n const canonical = target ? aliasNames.get(target) : undefined\n\n return canonical ? { ...schemaNode, name: canonical.name, ref: canonical.ref } : undefined\n }\n\n const signature = signatureOf(schemaNode)\n if (skipRootMatch && schemaNode === root) return undefined\n\n const canonical = canonicalBySignature.get(signature)\n if (!canonical) return undefined\n\n return createRefNode(schemaNode, canonical)\n },\n })\n}\n\n/**\n * Strips usage-slot flags from a hoisted definition and applies its canonical name.\n * A standalone definition is never optional, so `optional`/`nullish` are cleared.\n */\nfunction cleanDefinition(node: SchemaNode, name: string): SchemaNode {\n return { ...node, name, optional: undefined, nullish: undefined }\n}\n\n/**\n * Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.\n *\n * A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence\n * is a named top-level schema, the first one becomes the canonical (so other top-level\n * duplicates and inline copies turn into references to it). Every other top-level name with\n * the same content is recorded in `aliasNames`, so refs to it can be repointed at the\n * canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied\n * per node with {@link applyDedupe}.\n *\n * @example\n * ```ts\n * const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {\n * isCandidate: (node) => node.type === 'enum' || node.type === 'object',\n * nameFor: (node) => node.name ?? null,\n * refFor: (name) => `#/components/schemas/${name}`,\n * })\n * ```\n */\nexport function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {\n const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options\n\n const topLevelNodes = new Set<SchemaNode>()\n\n type Group = {\n count: number\n representative: SchemaNode\n topLevelNames: Array<string>\n }\n const groups = new Map<string, Group>()\n\n function record(schemaNode: SchemaNode): void {\n if (!isCandidate(schemaNode)) return\n const signature = signatureOf(schemaNode)\n\n const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name\n const group = groups.get(signature)\n if (group) {\n group.count++\n if (isTopLevel) group.topLevelNames.push(schemaNode.name!)\n } else {\n groups.set(signature, { count: 1, representative: schemaNode, topLevelNames: isTopLevel ? [schemaNode.name!] : [] })\n }\n }\n\n for (const root of roots) {\n if (root.kind === 'Schema') topLevelNodes.add(root)\n for (const schemaNode of collectLazy<SchemaNode>(root, { schema: (node) => node })) {\n record(schemaNode)\n }\n }\n\n const canonicalBySignature = new Map<string, DedupeCanonical>()\n const aliasNames = new Map<string, DedupeCanonical>()\n const pendingHoists: Array<{ name: string; representative: SchemaNode }> = []\n\n for (const [signature, group] of groups) {\n if (group.count < minOccurrences) continue\n\n const [firstName, ...duplicateNames] = group.topLevelNames\n if (firstName) {\n const canonical: DedupeCanonical = { name: firstName, ref: refFor(firstName) }\n canonicalBySignature.set(signature, canonical)\n for (const duplicate of duplicateNames) {\n aliasNames.set(duplicate, canonical)\n }\n continue\n }\n\n const name = nameFor(group.representative, signature)\n if (!name) continue\n\n canonicalBySignature.set(signature, { name, ref: refFor(name) })\n pendingHoists.push({ name, representative: group.representative })\n }\n\n // Build hoisted definitions only after every canonical name is known, so nested\n // duplicates inside a definition also resolve to refs.\n const hoisted = pendingHoists.map(({ name, representative }) =>\n cleanDefinition(applyDedupe(representative, { canonicalBySignature, aliasNames }, true), name),\n )\n\n return { canonicalBySignature, aliasNames, hoisted }\n}\n","import type { SchemaNode } from './nodes/index.ts'\n\n/**\n * The spec-specific questions a schema parser answers while turning a source document into Kubb\n * AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where\n * OpenAPI, AsyncAPI, and plain JSON Schema differ.\n */\nexport type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {\n /**\n * Whether the schema is nullable.\n */\n isNullable(schema?: TSchema): boolean\n /**\n * Whether the value is a `$ref` pointer.\n */\n isReference(value?: unknown): value is TRef\n /**\n * Whether the schema carries a discriminator for polymorphism.\n */\n isDiscriminator(value?: unknown): value is TDiscriminated\n /**\n * Whether the schema is binary data, converted to a `blob` node.\n */\n isBinary(schema: TSchema): boolean\n /**\n * Resolves a local `$ref` against the document, or nullish when it cannot.\n */\n resolveRef<TResolved>(document: TDocument, ref: string): TResolved | null | undefined\n /**\n * Derives a schema's `optional`/`nullish` flags from a parent's `required` value and the\n * schema's own `nullable`. How \"required\" and \"nullable\" combine is spec-specific, so the\n * dialect owns it. Method syntax keeps a concrete dialect assignable to the base\n * `SchemaDialect` (bivariant parameters).\n */\n optionality(schema: SchemaNode, required: boolean): SchemaNode\n}\n\n/**\n * A spec adapter's dialect. `name` identifies it in logs and diagnostics; `schema` holds the\n * spec-specific schema questions the parser answers.\n */\nexport type Dialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {\n /**\n * Identifies the dialect in logs and diagnostics.\n */\n name: string\n /**\n * The spec-specific schema behavior. See {@link SchemaDialect}.\n */\n schema: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>\n}\n\n/**\n * Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the\n * dialect's type for inference.\n *\n * @example\n * ```ts\n * export const oasDialect = defineDialect({\n * name: 'oas',\n * schema: {\n * isNullable,\n * isReference,\n * isDiscriminator,\n * isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',\n * resolveRef,\n * optionality,\n * },\n * })\n * ```\n */\nexport function defineDialect<TSchema, TRef, TDiscriminated, TDocument>(\n dialect: Dialect<TSchema, TRef, TDiscriminated, TDocument>,\n): Dialect<TSchema, TRef, TDiscriminated, TDocument> {\n return dialect\n}\n","import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'\n\n/**\n * Runtime context passed as `this` to printer handlers.\n *\n * `this.transform` dispatches to node-level handlers from `nodes`.\n *\n * @example\n * ```ts\n * const context: PrinterHandlerContext<string, {}> = {\n * options: {},\n * transform: () => 'value',\n * }\n * ```\n */\ntype PrinterHandlerContext<TOutput, TOptions extends object> = {\n /**\n * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.\n * Use `this.transform` inside `nodes` handlers and inside the `print` override.\n */\n transform: (node: SchemaNode) => TOutput | null\n /**\n * Options for this printer instance.\n */\n options: TOptions\n}\n\n/**\n * Handler for one schema node type.\n *\n * Use a regular function (not an arrow function) if you need `this`.\n *\n * @example\n * ```ts\n * const handler: PrinterHandler<string, {}, 'string'> = function () {\n * return 'string'\n * }\n * ```\n */\ntype PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (\n this: PrinterHandlerContext<TOutput, TOptions>,\n node: SchemaNodeByType[T],\n) => TOutput | null\n\n/**\n * Partial map of per-node-type handler overrides for a printer.\n *\n * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).\n * Supply only the handlers you want to replace. The printer's built-in\n * defaults fill in the rest.\n *\n * @example\n * ```ts\n * pluginZod({\n * printer: {\n * nodes: {\n * date(): string {\n * return 'z.string().date()'\n * },\n * } satisfies PrinterPartial<string, PrinterZodOptions>,\n * },\n * })\n * ```\n */\nexport type PrinterPartial<TOutput, TOptions extends object> = Partial<{\n [K in SchemaType]: PrinterHandler<TOutput, TOptions, K>\n}>\n\n/**\n * Generic shape used by `definePrinter`.\n *\n * - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)\n * - `TOptions` options passed to and stored on the printer instance\n * - `TOutput` the type emitted by node handlers\n * - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)\n *\n * @example\n * ```ts\n * type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>\n * ```\n */\nexport type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {\n name: TName\n options: TOptions\n output: TOutput\n printOutput: TPrintOutput\n}\n\n/**\n * Printer instance returned by a printer factory.\n *\n * @example\n * ```ts\n * const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})\n * ```\n */\nexport type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {\n /**\n * Unique identifier supplied at creation time.\n */\n name: T['name']\n /**\n * Options for this printer instance.\n */\n options: T['options']\n /**\n * Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.\n * Always dispatches through the `nodes` map. Never calls the `print` override.\n * Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.\n */\n transform: (node: SchemaNode) => T['output'] | null\n /**\n * Public printer. If the builder provides a root-level `print`, this calls that\n * higher-level function (which may produce full declarations).\n * Otherwise, falls back to the node-level dispatcher.\n */\n print: (node: SchemaNode) => T['printOutput'] | null\n}\n\n/**\n * Builder function passed to `definePrinter`.\n *\n * It receives resolved options and returns:\n * - `name`\n * - `options`\n * - `nodes` handlers\n * - optional top-level `print` override\n *\n * @example\n * ```ts\n * const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })\n * ```\n */\ntype PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {\n name: T['name']\n /**\n * Options to store on the printer.\n */\n options: T['options']\n nodes: Partial<{\n [K in SchemaType]: PrinterHandler<T['output'], T['options'], K>\n }>\n /**\n * Optional root-level print override. When provided, becomes the public `printer.print`.\n * Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),\n * not the override itself, so recursion is safe.\n */\n print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null\n}\n/**\n * Defines a schema printer: a function that takes a `SchemaNode` and emits\n * code in your target language. Each plugin that produces code from schemas\n * (TypeScript types, Zod schemas, Faker factories) ships a printer built\n * with this helper.\n *\n * The builder receives resolved options and returns:\n *\n * - `name` unique identifier for the printer.\n * - `options` stored on the returned printer instance.\n * - `nodes` map of `SchemaType` → handler. Handlers return the rendered\n * output (a string, a TypeScript AST node, ...) for that schema type.\n * - `print` (optional), top-level override exposed as `printer.print`.\n * Use `this.transform(node)` inside it to dispatch to `nodes` recursively.\n *\n * Without a `print` override, `printer.print` falls back to `printer.transform`\n * (the node-level dispatcher).\n *\n * @example Tiny Zod printer\n * ```ts\n * import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'\n *\n * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>\n *\n * export const zodPrinter = definePrinter<PrinterZod>((options) => ({\n * name: 'zod',\n * options: { strict: options.strict ?? true },\n * nodes: {\n * string: () => 'z.string()',\n * object(node) {\n * const props = node.properties\n * .map((p) => `${p.name}: ${this.transform(p.schema)}`)\n * .join(', ')\n * return `z.object({ ${props} })`\n * },\n * },\n * }))\n * ```\n */\nexport function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {\n return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>\n}\n\n/**\n * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.\n *\n * @example\n * ```ts\n * export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(\n * (node) => node.kind,\n * )\n * ```\n */\nexport function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {\n return function <T extends PrinterFactoryOptions>(\n build: (options: T['options']) => {\n name: T['name']\n options: T['options']\n nodes: Partial<{\n [K in TKey]: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNodeByKey[K],\n ) => T['output'] | null\n }>\n print?: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNode,\n ) => T['printOutput'] | null\n },\n ): (options?: T['options']) => {\n name: T['name']\n options: T['options']\n transform: (node: TNode) => T['output'] | null\n print: (node: TNode) => T['printOutput'] | null\n } {\n return (options) => {\n const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))\n\n const context = {\n options: resolvedOptions,\n transform: (node: TNode): T['output'] | null => {\n const key = getKey(node)\n if (key === null) return null\n\n const handler = nodes[key]\n\n if (!handler) return null\n\n return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)\n },\n }\n\n return {\n name,\n options: resolvedOptions,\n transform: context.transform,\n print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;AAOA,SAAS,gBAAgB,MAA0B;CACjD,OAAO,GAAG,KAAK,aAAa,GAAG,GAAG,KAAK,UAAU,GAAG,GAAG,KAAK,WAAW,IAAI;AAC7E;AAEA,SAAS,cAAc,MAAoD;CACzE,IAAI,KAAK,KAAK,OAAOA,gBAAAA,eAAe,KAAK,GAAG;CAC5C,OAAO,KAAK,QAAQ;AACtB;AAuBA,MAAM,mBAA8C;CAClD;EAAE,MAAM;EAAY,KAAK;EAAS,QAAQ;CAAI;CAC9C;EAAE,MAAM;EAAS,KAAK;EAAQ,QAAQ;CAAI;CAC1C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAO,QA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AAE;GAAM,gBAAgB,MAAM;EAAe,CAAC;CACnE;CAQA,OAAO;EAAE;EAAsB;EAAY,SAJ3B,cAAc,KAAK,EAAE,MAAM,qBACzC,gBAAgB,YAAY,gBAAgB;GAAE;GAAsB;EAAW,GAAG,IAAI,GAAG,IAAI,CAG9C;CAAE;AACrD;;;;;;;;;;;;;;;;;;;;;;AC/JA,SAAgB,cACd,SACmD;CACnD,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACiHA,SAAgB,cAAuE,OAAkE;CACvJ,OAAO,sBAAgE,SAAS,KAAK,IAAI,CAAC,CAAC,KAAK;AAClG;;;;;;;;;;;AAYA,SAAgB,qBAAkG,QAAsC;CACtJ,OAAO,SACL,OAyBA;EACA,QAAQ,YAAY;GAClB,MAAM,EAAE,MAAM,SAAS,iBAAiB,OAAO,OAAO,kBAAkB,MAAM,WAAY,CAAC,CAAkB;GAE7G,MAAM,UAAU;IACd,SAAS;IACT,YAAY,SAAoC;KAC9C,MAAM,MAAM,OAAO,IAAI;KACvB,IAAI,QAAQ,MAAM,OAAO;KAEzB,MAAM,UAAU,MAAM;KAEtB,IAAI,CAAC,SAAS,OAAO;KAErB,OAAQ,QAAsE,KAAK,SAAS,IAAI;IAClG;GACF;GAEA,OAAO;IACL;IACA,SAAS;IACT,WAAW,QAAQ;IACnB,OAAQ,gBAAgB,cAAc,KAAK,OAAO,IAAI,QAAQ;GAChE;EACF;CACF;AACF"}
import { n as __name } from "./chunk-CNktS9qV.js";
import { $ as fileDef, $t as textDef, A as FunctionParametersNode, At as InferSchemaNode, B as functionParameterDef, Bt as TypeNode, C as ParameterLocation, Ct as UrlSchemaNode, D as FunctionParamKind, Dt as UserPropertyNode, E as parameterDef, Et as PropertyNode, Ft as ConstNode, G as ExportNode, H as indexedAccessTypeDef, Ht as breakDef, It as FunctionNode, J as SourceNode, K as FileNode, Lt as JSDocNode, M as ObjectBindingPatternNode, Mt as ArrowFunctionNode, N as TypeExpression, Nt as BreakNode, O as FunctionParamNode, P as TypeLiteralNode, Pt as CodeNode, Q as exportDef, Qt as jsxDef, Rt as JsxNode, S as requestBodyDef, St as UnionSchemaNode, Tt as schemaDef, U as objectBindingPatternDef, Ut as constDef, V as functionParametersDef, Vt as arrowFunctionDef, W as typeLiteralDef, Zt as functionDef, _ as responseDef, _t as SchemaNode, a as InputMeta, an as BaseNode, b as UserRequestBody, bt as StringSchemaNode, c as inputDef, ct as DatetimeSchemaNode, d as HttpOperationNode, dt as NumberSchemaNode, en as typeDef, et as importDef, f as OperationNode, ft as ObjectSchemaNode, gt as ScalarSchemaType, h as ResponseNode, ht as ScalarSchemaNode, i as outputDef, in as syncOptionality, it as contentDef, j as IndexedAccessTypeNode, jt as ParserOptions, k as FunctionParameterNode, kt as propertyDef, l as GenericOperationNode, lt as EnumSchemaNode, m as operationDef, mt as RefSchemaNode, n as OutputNode, nn as NodeDef, nt as ContentNode, o as InputNode, on as NodeKind, ot as ArraySchemaNode, pt as PrimitiveSchemaType, q as ImportNode, rn as defineNode, rt as UserContent, st as DateSchemaNode, t as Node, tn as DistributiveOmit, tt as sourceDef, u as HttpMethod, ut as IntersectionSchemaNode, v as StatusCode, vt as SchemaNodeByType, w as ParameterNode, xt as TimeSchemaNode, y as RequestBodyNode, yt as SchemaType, zt as TextNode } from "./index-BzjwdK2M.js";
import { UserFileNode, t as factory_d_exports } from "./factory.js";
import { a as ParentOf, c as collect, d as httpMethods, f as schemaTypes, i as defineMacro, l as transform, n as applyMacros, o as Visitor, r as composeMacros, s as VisitorContext, t as Macro, u as walk } from "./defineMacro-BLIR6k-j.js";
import { a as definePrinter, c as DedupeCanonical, d as applyDedupe, f as buildDedupePlan, i as createPrinterFactory, l as DedupeLookups, n as PrinterFactoryOptions, o as SchemaDialect, r as PrinterPartial, s as defineSchemaDialect, t as Printer, u as DedupePlan } from "./types-DyDzizSf.js";
import { n as OperationParamsResolver } from "./operationParams-BZ07xDm0.js";
import { t as extractStringsFromNodes } from "./extractStringsFromNodes-p4mX1TQD.js";
import { $ as exportDef, $t as textDef, A as IndexedAccessTypeNode, At as InferSchemaNode, B as functionParametersDef, Bt as TypeNode, C as ParameterNode, Ct as UrlSchemaNode, D as FunctionParamNode, Dt as UserPropertyNode, E as FunctionParamKind, Et as PropertyNode, Ft as ConstNode, G as FileNode, H as objectBindingPatternDef, Ht as breakDef, It as FunctionNode, J as UserFileNode, K as ImportNode, Lt as JSDocNode, M as TypeExpression, Mt as ArrowFunctionNode, N as TypeLiteralNode, Nt as BreakNode, O as FunctionParameterNode, Pt as CodeNode, Qt as jsxDef, Rt as JsxNode, S as ParameterLocation, St as UnionSchemaNode, T as parameterDef, Tt as schemaDef, U as typeLiteralDef, Ut as constDef, V as indexedAccessTypeDef, Vt as arrowFunctionDef, W as ExportNode, Zt as functionDef, _t as SchemaNode, a as InputMeta, an as NodeKind, bt as StringSchemaNode, c as inputDef, cn as defineDialect, ct as DatetimeSchemaNode, d as HttpOperationNode, dt as NumberSchemaNode, en as typeDef, et as fileDef, f as OperationNode, ft as ObjectSchemaNode, g as StatusCode, gt as ScalarSchemaType, h as ResponseNode, ht as ScalarSchemaNode, i as outputDef, in as BaseNode, it as contentDef, j as ObjectBindingPatternNode, jt as ParserOptions, k as FunctionParametersNode, kt as propertyDef, l as GenericOperationNode, lt as EnumSchemaNode, m as operationDef, mt as RefSchemaNode, n as OutputNode, nn as NodeDef, nt as sourceDef, o as InputNode, on as Dialect, ot as ArraySchemaNode, pt as PrimitiveSchemaType, q as SourceNode, rn as defineNode, rt as ContentNode, sn as SchemaDialect, st as DateSchemaNode, t as Node, tn as DistributiveOmit, tt as importDef, u as HttpMethod, ut as IntersectionSchemaNode, v as responseDef, vt as SchemaNodeByType, x as requestBodyDef, xt as TimeSchemaNode, y as RequestBodyNode, yt as SchemaType, z as functionParameterDef, zt as TextNode } from "./index-CTaN_ee_.js";
import { t as factory_d_exports } from "./factory.js";
import { a as defineMacro, c as VisitorContext, d as walk, f as schemaTypes, i as composeMacros, l as collect, n as Macro, o as ParentOf, r as applyMacros, s as Visitor, t as Enforce, u as transform } from "./defineMacro-Vw-xQvq7.js";
import { a as definePrinter, c as DedupePlan, i as createPrinterFactory, l as applyDedupe, n as PrinterFactoryOptions, o as DedupeCanonical, r as PrinterPartial, s as DedupeLookups, t as Printer, u as buildDedupePlan } from "./types-DQTgVSwE.js";
import { n as OperationParamsResolver } from "./operationParams-B44p6EQy.js";

@@ -32,2 +31,18 @@ //#region src/guards.d.ts

//#endregion
//#region src/signature.d.ts
/**
* Computes a deterministic, shape-only signature (a content hash) for a schema node. Two schemas
* share a signature when they are structurally identical, ignoring documentation (`name`, `title`,
* `description`, `example`, `default`, `deprecated`) and usage-slot flags (`optional`, `nullish`,
* `readOnly`, `writeOnly`). `nullable` is kept because it changes the produced type, and `ref`
* nodes compare by target name, which also terminates on circular shapes.
*
* @example Two enums with different descriptions share a signature
* ```ts
* signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===
* signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))
* ```
*/
declare function signatureOf(node: SchemaNode): string;
//#endregion
//#region src/registry.d.ts

@@ -38,3 +53,14 @@ /**

*/
declare const nodeDefs: (NodeDef<PropertyNode, UserPropertyNode> | NodeDef<SchemaNode, (Omit<ObjectSchemaNode, "kind" | "primitive" | "properties"> & {
declare const nodeDefs: (NodeDef<PropertyNode, UserPropertyNode> | NodeDef<InputNode<false>, Partial<Omit<InputNode<false>, "kind">>> | NodeDef<OutputNode, Partial<Omit<OutputNode, "kind">>> | NodeDef<RequestBodyNode, Omit<RequestBodyNode, "kind">> | NodeDef<OperationNode, {
[key: string]: unknown;
operationId: string;
method?: HttpOperationNode["method"];
path?: HttpOperationNode["path"];
requestBody?: Omit<RequestBodyNode, "kind">;
}> | NodeDef<ContentNode, Omit<ContentNode, "kind">> | NodeDef<ResponseNode, Pick<ResponseNode, "statusCode"> & Partial<Omit<ResponseNode, "kind" | "content" | "statusCode">> & {
content?: Array<ContentNode>;
schema?: SchemaNode;
mediaType?: string | null;
keysToOmit?: Array<string> | null;
}> | NodeDef<SchemaNode, (Omit<ObjectSchemaNode, "kind" | "primitive" | "properties"> & {
properties?: Array<PropertyNode>;

@@ -95,14 +121,3 @@ primitive?: "object";

type: "ipv6";
}) | ScalarSchemaNode, "kind">> | NodeDef<ContentNode, UserContent> | NodeDef<RequestBodyNode, UserRequestBody> | NodeDef<OperationNode, {
[key: string]: unknown;
operationId: string;
method?: HttpOperationNode["method"];
path?: HttpOperationNode["path"];
requestBody?: UserRequestBody;
}> | NodeDef<OutputNode, Partial<Omit<OutputNode, "kind">>> | NodeDef<ParameterNode, Pick<ParameterNode, "name" | "schema" | "in"> & Partial<Omit<ParameterNode, "kind" | "name" | "schema" | "in">>> | NodeDef<ConstNode, Omit<ConstNode, "kind">> | NodeDef<TypeNode, Omit<TypeNode, "kind">> | NodeDef<FunctionNode, Omit<FunctionNode, "kind">> | NodeDef<ArrowFunctionNode, Omit<ArrowFunctionNode, "kind">> | NodeDef<TextNode, string> | NodeDef<BreakNode, void> | NodeDef<JsxNode, string> | NodeDef<InputNode<false>, Partial<Omit<InputNode<false>, "kind">>> | NodeDef<ImportNode, Omit<ImportNode, "kind">> | NodeDef<ExportNode, Omit<ExportNode, "kind">> | NodeDef<SourceNode, Omit<SourceNode, "kind">> | NodeDef<FileNode<object>, Omit<FileNode<object>, "kind">> | NodeDef<ResponseNode, Pick<ResponseNode, "statusCode"> & Partial<Omit<ResponseNode, "kind" | "content" | "statusCode">> & {
content?: Array<UserContent>;
schema?: SchemaNode;
mediaType?: string | null;
keysToOmit?: Array<string> | null;
}> | NodeDef<TypeLiteralNode, Pick<TypeLiteralNode, "members">> | NodeDef<IndexedAccessTypeNode, Omit<IndexedAccessTypeNode, "kind">> | NodeDef<ObjectBindingPatternNode, Pick<ObjectBindingPatternNode, "elements">> | NodeDef<FunctionParameterNode, {
}) | ScalarSchemaNode, "kind">> | NodeDef<ParameterNode, Pick<ParameterNode, "name" | "schema" | "in"> & Partial<Omit<ParameterNode, "kind" | "name" | "schema" | "in">>> | NodeDef<ObjectBindingPatternNode, Pick<ObjectBindingPatternNode, "elements">> | NodeDef<TypeLiteralNode, Pick<TypeLiteralNode, "members">> | NodeDef<FunctionParameterNode, {
name: string;

@@ -121,21 +136,5 @@ type?: TypeExpression;

default?: string;
}> | NodeDef<FunctionParametersNode, Partial<Omit<FunctionParametersNode, "kind">>>)[];
}> | NodeDef<FunctionParametersNode, Partial<Omit<FunctionParametersNode, "kind">>> | NodeDef<IndexedAccessTypeNode, Omit<IndexedAccessTypeNode, "kind">> | NodeDef<ConstNode, Omit<ConstNode, "kind">> | NodeDef<TypeNode, Omit<TypeNode, "kind">> | NodeDef<FunctionNode, Omit<FunctionNode, "kind">> | NodeDef<ArrowFunctionNode, Omit<ArrowFunctionNode, "kind">> | NodeDef<TextNode, string> | NodeDef<BreakNode, void> | NodeDef<JsxNode, string> | NodeDef<ImportNode, Omit<ImportNode, "kind">> | NodeDef<ExportNode, Omit<ExportNode, "kind">> | NodeDef<SourceNode, Omit<SourceNode, "kind">> | NodeDef<FileNode<object>, Omit<FileNode<object>, "kind">>)[];
//#endregion
//#region src/signature.d.ts
/**
* Computes a deterministic, shape-only signature (a content hash) for a schema node. Two schemas
* share a signature when they are structurally identical, ignoring documentation (`name`, `title`,
* `description`, `example`, `default`, `deprecated`) and usage-slot flags (`optional`, `nullish`,
* `readOnly`, `writeOnly`). `nullable` is kept because it changes the produced type, and `ref`
* nodes compare by target name, which also terminates on circular shapes.
*
* @example Two enums with different descriptions share a signature
* ```ts
* signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===
* signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))
* ```
*/
declare function signatureOf(node: SchemaNode): string;
//#endregion
export { type ArraySchemaNode, type ArrowFunctionNode, type BreakNode, type CodeNode, type ConstNode, type ContentNode, type DateSchemaNode, type DatetimeSchemaNode, type DedupeCanonical, type DedupeLookups, type DedupePlan, type DistributiveOmit, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type FunctionParamKind, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type ImportNode, type IndexedAccessTypeNode, type InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Macro, type Node, type NodeDef, type NodeKind, type NumberSchemaNode, type ObjectBindingPatternNode, type ObjectSchemaNode, type OperationNode, type OperationParamsResolver, type OutputNode, type ParameterLocation, type ParameterNode, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefSchemaNode, type RequestBodyNode, type ResponseNode, type ScalarSchemaNode, type ScalarSchemaType, type SchemaDialect, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeExpression, type TypeLiteralNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, applyDedupe, applyMacros, arrowFunctionDef, breakDef, buildDedupePlan, collect, composeMacros, constDef, contentDef, createPrinterFactory, defineMacro, defineNode, definePrinter, defineSchemaDialect, exportDef, extractStringsFromNodes, factory_d_exports as factory, fileDef, functionDef, functionParameterDef, functionParametersDef, httpMethods, importDef, indexedAccessTypeDef, inputDef, isHttpOperationNode, jsxDef, narrowSchema, nodeDefs, objectBindingPatternDef, operationDef, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, signatureOf, sourceDef, syncOptionality, textDef, transform, typeDef, typeLiteralDef, walk };
export { type ArraySchemaNode, type ArrowFunctionNode, type BreakNode, type CodeNode, type ConstNode, type ContentNode, type DateSchemaNode, type DatetimeSchemaNode, type DedupeCanonical, type DedupeLookups, type DedupePlan, type Dialect, type DistributiveOmit, type Enforce, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type FunctionParamKind, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type ImportNode, type IndexedAccessTypeNode, type InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Macro, type Node, type NodeDef, type NodeKind, type NumberSchemaNode, type ObjectBindingPatternNode, type ObjectSchemaNode, type OperationNode, type OperationParamsResolver, type OutputNode, type ParameterLocation, type ParameterNode, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefSchemaNode, type RequestBodyNode, type ResponseNode, type ScalarSchemaNode, type ScalarSchemaType, type SchemaDialect, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeExpression, type TypeLiteralNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, applyDedupe, applyMacros, arrowFunctionDef, breakDef, buildDedupePlan, collect, composeMacros, constDef, contentDef, createPrinterFactory, defineDialect, defineMacro, defineNode, definePrinter, exportDef, factory_d_exports as factory, fileDef, functionDef, functionParameterDef, functionParametersDef, importDef, indexedAccessTypeDef, inputDef, isHttpOperationNode, jsxDef, narrowSchema, nodeDefs, objectBindingPatternDef, operationDef, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, signatureOf, sourceDef, textDef, transform, typeDef, typeLiteralDef, walk };
//# sourceMappingURL=index.d.ts.map
import "./chunk-CNktS9qV.js";
import { a as nodeDefs, c as extractRefName, g as httpMethods, i as walk, m as narrowSchema, n as collectLazy, p as isHttpOperationNode, r as transform, t as collect, v as schemaTypes } from "./visitor-DJ6ZEJvq.js";
import { $ as syncOptionality, A as importDef, C as objectBindingPatternDef, F as breakDef, G as jsxDef, I as constDef, K as textDef, M as contentDef, O as exportDef, P as arrowFunctionDef, Q as defineNode, S as indexedAccessTypeDef, W as functionDef, X as createSchema, Z as schemaDef, b as functionParameterDef, c as outputDef, f as requestBodyDef, i as propertyDef, j as sourceDef, k as fileDef, m as inputDef, n as responseDef, o as parameterDef, q as typeDef, u as operationDef, w as typeLiteralDef, x as functionParametersDef } from "./response-KUdWiDWw.js";
import { n as factory_exports } from "./factory-CZNOGI-N.js";
import "./utils-CF_-Pn_c.js";
import { t as extractStringsFromNodes } from "./extractStringsFromNodes-Cja-xxx5.js";
import { n as composeMacros, r as defineMacro, t as applyMacros } from "./defineMacro-BTXvS8nI.js";
import { a as nodeDefs, c as extractRefName, g as schemaTypes, i as walk, m as narrowSchema, n as collectLazy, p as isHttpOperationNode, r as transform, t as collect } from "./visitor-DepQEKyp.js";
import { $ as schemaDef, A as fileDef, C as objectBindingPatternDef, F as arrowFunctionDef, G as functionDef, I as breakDef, J as typeDef, K as jsxDef, L as constDef, M as sourceDef, N as contentDef, Q as createSchema, S as indexedAccessTypeDef, b as functionParameterDef, c as outputDef, et as defineNode, f as requestBodyDef, i as propertyDef, j as importDef, k as exportDef, m as inputDef, n as responseDef, o as parameterDef, q as textDef, u as operationDef, w as typeLiteralDef, x as functionParametersDef } from "./response-Rd1uisM1.js";
import "./utils-DW9rnfsD.js";
import { n as composeMacros, r as defineMacro, t as applyMacros } from "./defineMacro-CNkUpxia.js";
import { t as factory_exports } from "./factory.js";
import { hash } from "node:crypto";

@@ -338,4 +337,4 @@ //#region src/signature.ts

function record(schemaNode) {
if (!isCandidate(schemaNode)) return;
const signature = signatureOf(schemaNode);
if (!isCandidate(schemaNode)) return;
const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name;

@@ -394,3 +393,3 @@ const group = groups.get(signature);

/**
* Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the
* Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the
* dialect's type for inference.

@@ -400,17 +399,20 @@ *

* ```ts
* export const oasDialect = defineSchemaDialect({
* export const oasDialect = defineDialect({
* name: 'oas',
* isNullable,
* isReference,
* isDiscriminator,
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
* resolveRef,
* schema: {
* isNullable,
* isReference,
* isDiscriminator,
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
* resolveRef,
* optionality,
* },
* })
* ```
*/
function defineSchemaDialect(dialect) {
function defineDialect(dialect) {
return dialect;
}
//#endregion
//#region src/printer.ts
//#region src/definePrinter.ts
/**

@@ -492,4 +494,4 @@ * Defines a schema printer: a function that takes a `SchemaNode` and emits

//#endregion
export { applyDedupe, applyMacros, arrowFunctionDef, breakDef, buildDedupePlan, collect, composeMacros, constDef, contentDef, createPrinterFactory, defineMacro, defineNode, definePrinter, defineSchemaDialect, exportDef, extractStringsFromNodes, factory_exports as factory, fileDef, functionDef, functionParameterDef, functionParametersDef, httpMethods, importDef, indexedAccessTypeDef, inputDef, isHttpOperationNode, jsxDef, narrowSchema, nodeDefs, objectBindingPatternDef, operationDef, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, signatureOf, sourceDef, syncOptionality, textDef, transform, typeDef, typeLiteralDef, walk };
export { applyDedupe, applyMacros, arrowFunctionDef, breakDef, buildDedupePlan, collect, composeMacros, constDef, contentDef, createPrinterFactory, defineDialect, defineMacro, defineNode, definePrinter, exportDef, factory_exports as factory, fileDef, functionDef, functionParameterDef, functionParametersDef, importDef, indexedAccessTypeDef, inputDef, isHttpOperationNode, jsxDef, narrowSchema, nodeDefs, objectBindingPatternDef, operationDef, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, signatureOf, sourceDef, textDef, transform, typeDef, typeLiteralDef, walk };
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","names":["obj"],"sources":["../src/signature.ts","../src/dedupe.ts","../src/dialect.ts","../src/printer.ts"],"sourcesContent":["import { hash } from 'node:crypto'\nimport type { SchemaNode } from './nodes/index.ts'\nimport { extractRefName } from './utils/index.ts'\n\n/**\n * The flags shared by every node kind that affect its type: `primitive`, `format`, `nullable`.\n */\nfunction flagsDescriptor(node: SchemaNode): string {\n return `${node.primitive ?? ''};${node.format ?? ''};${node.nullable ? 1 : 0}`\n}\n\nfunction refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {\n if (node.ref) return extractRefName(node.ref)\n return node.name ?? ''\n}\n\ntype ScalarField = { kind: 'scalar'; key: string; prefix: string }\ntype BoolField = { kind: 'bool'; key: string; prefix: string }\ntype ChildField = { kind: 'child'; key: string; prefix: string }\ntype ChildrenField = { kind: 'children'; key: string; prefix: string }\ntype ObjectPropsField = { kind: 'objectProps' }\ntype AdditionalPropsField = { kind: 'additionalProps' }\ntype PatternPropsField = { kind: 'patternProps' }\ntype EnumValuesField = { kind: 'enumValues' }\ntype RefTargetField = { kind: 'refTarget' }\n\ntype ShapeField =\n | ScalarField\n | BoolField\n | ChildField\n | ChildrenField\n | ObjectPropsField\n | AdditionalPropsField\n | PatternPropsField\n | EnumValuesField\n | RefTargetField\n\nconst arrayTupleFields: ReadonlyArray<ShapeField> = [\n { kind: 'children', key: 'items', prefix: 'i' },\n { kind: 'child', key: 'rest', prefix: 'r' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'bool', key: 'unique', prefix: 'u' },\n]\n\nconst numericFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'exclusiveMinimum', prefix: 'emn' },\n { kind: 'scalar', key: 'exclusiveMaximum', prefix: 'emx' },\n { kind: 'scalar', key: 'multipleOf', prefix: 'mo' },\n]\n\nconst rangeFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n]\n\n/**\n * Maps each node `type` to its ordered shape-contributing fields. Types absent from the map\n * (boolean, null, any, and other scalars) fall back to `${type}|${flags}`.\n */\nconst SHAPE_KEYS: Partial<Record<SchemaNode['type'], ReadonlyArray<ShapeField>>> = {\n object: [\n { kind: 'objectProps' },\n { kind: 'additionalProps' },\n { kind: 'patternProps' },\n { kind: 'scalar', key: 'minProperties', prefix: 'mn' },\n { kind: 'scalar', key: 'maxProperties', prefix: 'mx' },\n ],\n array: arrayTupleFields,\n tuple: arrayTupleFields,\n union: [\n { kind: 'scalar', key: 'strategy', prefix: 's' },\n { kind: 'scalar', key: 'discriminatorPropertyName', prefix: 'd' },\n { kind: 'children', key: 'members', prefix: 'm' },\n ],\n intersection: [{ kind: 'children', key: 'members', prefix: 'm' }],\n enum: [{ kind: 'enumValues' }],\n ref: [{ kind: 'refTarget' }],\n string: [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'pattern', prefix: 'pt' },\n ],\n number: numericFields,\n integer: numericFields,\n bigint: numericFields,\n url: [\n { kind: 'scalar', key: 'path', prefix: 'path' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n ],\n uuid: rangeFields,\n email: rangeFields,\n datetime: [\n { kind: 'bool', key: 'offset', prefix: 'o' },\n { kind: 'bool', key: 'local', prefix: 'l' },\n ],\n date: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n time: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n}\n\nfunction serializeShapeField(field: ShapeField, node: SchemaNode, record: Record<string, unknown>): string {\n switch (field.kind) {\n case 'scalar':\n return `${field.prefix}:${record[field.key] ?? ''}`\n case 'bool':\n return `${field.prefix}:${record[field.key] ? 1 : 0}`\n case 'child': {\n const child = record[field.key] as SchemaNode | undefined\n return `${field.prefix}:${child ? signatureOf(child) : ''}`\n }\n case 'children': {\n const children = (record[field.key] as Array<SchemaNode> | undefined) ?? []\n return `${field.prefix}[${children.map((c) => signatureOf(c)).join(',')}]`\n }\n case 'objectProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const props = (obj.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema)}`).join(',')\n return `p[${props}]`\n }\n case 'additionalProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n if (typeof obj.additionalProperties === 'boolean') return `ab:${obj.additionalProperties}`\n if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`\n return ''\n }\n case 'patternProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const pattern = obj.patternProperties\n ? Object.keys(obj.patternProperties)\n .sort()\n .map((key) => `${key}=${signatureOf(obj.patternProperties![key]!)}`)\n .join(',')\n : ''\n return `pp[${pattern}]`\n }\n case 'enumValues': {\n const en = node as Extract<SchemaNode, { type: 'enum' }>\n let values = ''\n if (en.namedEnumValues?.length) {\n values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')\n } else if (en.enumValues?.length) {\n values = en.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')\n }\n return `v[${values}]`\n }\n case 'refTarget': {\n return `->${refTargetName(node as Extract<SchemaNode, { type: 'ref' }>)}`\n }\n }\n}\n\n/**\n * Builds the local shape descriptor that {@link signatureOf} hashes: the node's kind, flags,\n * constraints, and its children's signatures.\n */\nfunction describeShape(node: SchemaNode): string {\n const flags = flagsDescriptor(node)\n const fields = SHAPE_KEYS[node.type]\n if (!fields) return `${node.type}|${flags}`\n\n const record = node as unknown as Record<string, unknown>\n const parts: Array<string> = [`${node.type}|${flags}`]\n for (const field of fields) {\n parts.push(serializeShapeField(field, node, record))\n }\n return parts.join('|')\n}\n\n/**\n * Caches the digest per node, keyed by identity. A `WeakMap` so entries die with the node, and so\n * a tree hashed during dedupe planning is not walked again when it is rewritten during streaming.\n * Reuse is safe because a signature depends only on content, and nodes are immutable once created.\n */\nconst signatureCache = new WeakMap<SchemaNode, string>()\n\n/**\n * Computes a deterministic, shape-only signature (a content hash) for a schema node. Two schemas\n * share a signature when they are structurally identical, ignoring documentation (`name`, `title`,\n * `description`, `example`, `default`, `deprecated`) and usage-slot flags (`optional`, `nullish`,\n * `readOnly`, `writeOnly`). `nullable` is kept because it changes the produced type, and `ref`\n * nodes compare by target name, which also terminates on circular shapes.\n *\n * @example Two enums with different descriptions share a signature\n * ```ts\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))\n * ```\n */\nexport function signatureOf(node: SchemaNode): string {\n const cached = signatureCache.get(node)\n if (cached !== undefined) return cached\n const signature = hash('sha256', describeShape(node), 'hex')\n signatureCache.set(node, signature)\n\n return signature\n}\n\n/**\n * Returns `true` when two schema nodes are structurally identical under shape-only equality,\n * meaning they produce the same TypeScript type.\n */\nexport function isSchemaEqual(a: SchemaNode, b: SchemaNode): boolean {\n return signatureOf(a) === signatureOf(b)\n}\n","/**\n * Schema-shape deduplication. `buildDedupePlan` finds top-level and inline schemas that share a\n * structural signature, picks one canonical definition, and `applyDedupe` repoints every duplicate\n * at it. This works on `SchemaNode` content, not on files.\n *\n * For merging a file's imports, exports, and source nodes, see `utils/fileMerge.ts`. Same idea of\n * collapsing duplicates, but a different domain.\n */\nimport type { Node, OperationNode, SchemaNode } from './nodes/index.ts'\nimport { createSchema } from './nodes/schema.ts'\nimport { signatureOf } from './signature.ts'\nimport { extractRefName } from './utils/index.ts'\nimport { collectLazy, transform } from './visitor.ts'\n\n/**\n * A canonical destination for a deduplicated shape: the shared schema name and\n * the synthetic `$ref` path that points at it.\n */\nexport type DedupeCanonical = {\n /**\n * Canonical schema name every duplicate occurrence refers to.\n */\n name: string\n /**\n * `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).\n */\n ref: string\n}\n\n/**\n * The result of {@link buildDedupePlan}: a lookup from structural signature to its\n * canonical target, plus the freshly hoisted definitions that must be added to\n * the schema list.\n */\nexport type DedupePlan = {\n /**\n * Maps a structural signature to the canonical schema that represents it.\n */\n canonicalBySignature: Map<string, DedupeCanonical>\n /**\n * Maps the name of a top-level schema that duplicates a canonical one to that canonical, so\n * references to the duplicate can be repointed at the first schema with the same content.\n */\n aliasNames: Map<string, DedupeCanonical>\n /**\n * New top-level schema definitions created for inline shapes that had no existing\n * named component. Nested duplicates inside each definition are already collapsed.\n */\n hoisted: Array<SchemaNode>\n}\n\n/**\n * The lookups {@link applyDedupe} needs from a {@link DedupePlan}.\n */\nexport type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>\n\n/**\n * Options that inject the naming and candidate policy into {@link buildDedupePlan}.\n * The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.\n */\nexport type BuildDedupePlanOptions = {\n /**\n * Returns `true` when a node should be deduplicated. This is the only gate, so it must\n * reject both ineligible kinds (return `false` for anything other than, say, enums and\n * objects) and unsafe shapes (e.g. nodes that reference a circular schema).\n */\n isCandidate: (node: SchemaNode) => boolean\n /**\n * Produces the canonical name for an inline shape with no existing named component.\n * Return `null` to leave the shape inline (for example when no contextual name exists).\n */\n nameFor: (node: SchemaNode, signature: string) => string | null\n /**\n * Builds the `$ref` path for a canonical name.\n */\n refFor: (name: string) => string\n /**\n * Minimum number of occurrences before a shape is deduplicated.\n *\n * @default 2\n */\n minOccurrences?: number\n}\n\n/**\n * Builds the shared `ref` replacement for a duplicate occurrence, carrying the\n * usage-slot and documentation fields that are not part of the canonical type.\n */\nfunction createRefNode(node: SchemaNode, canonical: DedupeCanonical): SchemaNode {\n return createSchema({\n type: 'ref',\n name: canonical.name,\n ref: canonical.ref,\n optional: node.optional,\n nullish: node.nullish,\n readOnly: node.readOnly,\n writeOnly: node.writeOnly,\n deprecated: node.deprecated,\n description: node.description,\n default: node.default,\n example: node.example,\n })\n}\n\n/**\n * Rewrites a node, replacing every candidate sub-schema whose signature has a canonical\n * target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,\n * so nested duplicates inside a replaced shape are not visited again. A `ref` that points\n * at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with\n * the same content.\n *\n * Pass `skipRootMatch` when rewriting a canonical definition so its own root is not\n * turned into a reference to itself. Nested duplicates are still collapsed.\n *\n * @example\n * ```ts\n * const next = applyDedupe(operationNode, plan)\n * ```\n */\nexport function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode\nexport function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode\nexport function applyDedupe(node: Node, plan: DedupeLookups, skipRootMatch = false): Node {\n const { canonicalBySignature, aliasNames } = plan\n if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node\n\n const root = node\n\n return transform(node, {\n schema(schemaNode) {\n if (schemaNode.type === 'ref') {\n const target = schemaNode.ref ? extractRefName(schemaNode.ref) : schemaNode.name\n const canonical = target ? aliasNames.get(target) : undefined\n\n return canonical ? { ...schemaNode, name: canonical.name, ref: canonical.ref } : undefined\n }\n\n const signature = signatureOf(schemaNode)\n if (skipRootMatch && schemaNode === root) return undefined\n\n const canonical = canonicalBySignature.get(signature)\n if (!canonical) return undefined\n\n return createRefNode(schemaNode, canonical)\n },\n })\n}\n\n/**\n * Strips usage-slot flags from a hoisted definition and applies its canonical name.\n * A standalone definition is never optional, so `optional`/`nullish` are cleared.\n */\nfunction cleanDefinition(node: SchemaNode, name: string): SchemaNode {\n return { ...node, name, optional: undefined, nullish: undefined }\n}\n\n/**\n * Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.\n *\n * A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence\n * is a named top-level schema, the first one becomes the canonical (so other top-level\n * duplicates and inline copies turn into references to it). Every other top-level name with\n * the same content is recorded in `aliasNames`, so refs to it can be repointed at the\n * canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied\n * per node with {@link applyDedupe}.\n *\n * @example\n * ```ts\n * const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {\n * isCandidate: (node) => node.type === 'enum' || node.type === 'object',\n * nameFor: (node) => node.name ?? null,\n * refFor: (name) => `#/components/schemas/${name}`,\n * })\n * ```\n */\nexport function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {\n const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options\n\n const topLevelNodes = new Set<SchemaNode>()\n\n type Group = {\n count: number\n representative: SchemaNode\n topLevelNames: Array<string>\n }\n const groups = new Map<string, Group>()\n\n function record(schemaNode: SchemaNode): void {\n const signature = signatureOf(schemaNode)\n if (!isCandidate(schemaNode)) return\n\n const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name\n const group = groups.get(signature)\n if (group) {\n group.count++\n if (isTopLevel) group.topLevelNames.push(schemaNode.name!)\n } else {\n groups.set(signature, { count: 1, representative: schemaNode, topLevelNames: isTopLevel ? [schemaNode.name!] : [] })\n }\n }\n\n for (const root of roots) {\n if (root.kind === 'Schema') topLevelNodes.add(root)\n for (const schemaNode of collectLazy<SchemaNode>(root, { schema: (node) => node })) {\n record(schemaNode)\n }\n }\n\n const canonicalBySignature = new Map<string, DedupeCanonical>()\n const aliasNames = new Map<string, DedupeCanonical>()\n const pendingHoists: Array<{ name: string; representative: SchemaNode }> = []\n\n for (const [signature, group] of groups) {\n if (group.count < minOccurrences) continue\n\n const [firstName, ...duplicateNames] = group.topLevelNames\n if (firstName) {\n const canonical: DedupeCanonical = { name: firstName, ref: refFor(firstName) }\n canonicalBySignature.set(signature, canonical)\n for (const duplicate of duplicateNames) {\n aliasNames.set(duplicate, canonical)\n }\n continue\n }\n\n const name = nameFor(group.representative, signature)\n if (!name) continue\n\n canonicalBySignature.set(signature, { name, ref: refFor(name) })\n pendingHoists.push({ name, representative: group.representative })\n }\n\n // Build hoisted definitions only after every canonical name is known, so nested\n // duplicates inside a definition also resolve to refs.\n const hoisted = pendingHoists.map(({ name, representative }) =>\n cleanDefinition(applyDedupe(representative, { canonicalBySignature, aliasNames }, true), name),\n )\n\n return { canonicalBySignature, aliasNames, hoisted }\n}\n","/**\n * The spec-specific questions a schema parser answers while turning a source document into Kubb\n * AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where\n * OpenAPI, AsyncAPI, and plain JSON Schema differ.\n */\nexport type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {\n /**\n * Identifies the dialect in logs and diagnostics.\n */\n name: string\n /**\n * Whether the schema is nullable.\n */\n isNullable: (schema?: TSchema) => boolean\n /**\n * Whether the value is a `$ref` pointer.\n */\n isReference: (value?: unknown) => value is TRef\n /**\n * Whether the schema carries a discriminator for polymorphism.\n */\n isDiscriminator: (value?: unknown) => value is TDiscriminated\n /**\n * Whether the schema is binary data, converted to a `blob` node.\n */\n isBinary: (schema: TSchema) => boolean\n /**\n * Resolves a local `$ref` against the document, or nullish when it cannot.\n */\n resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined\n}\n\n/**\n * Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the\n * dialect's type for inference.\n *\n * @example\n * ```ts\n * export const oasDialect = defineSchemaDialect({\n * name: 'oas',\n * isNullable,\n * isReference,\n * isDiscriminator,\n * isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',\n * resolveRef,\n * })\n * ```\n */\nexport function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(\n dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>,\n): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument> {\n return dialect\n}\n","import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'\n\n/**\n * Runtime context passed as `this` to printer handlers.\n *\n * `this.transform` dispatches to node-level handlers from `nodes`.\n *\n * @example\n * ```ts\n * const context: PrinterHandlerContext<string, {}> = {\n * options: {},\n * transform: () => 'value',\n * }\n * ```\n */\ntype PrinterHandlerContext<TOutput, TOptions extends object> = {\n /**\n * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.\n * Use `this.transform` inside `nodes` handlers and inside the `print` override.\n */\n transform: (node: SchemaNode) => TOutput | null\n /**\n * Options for this printer instance.\n */\n options: TOptions\n}\n\n/**\n * Handler for one schema node type.\n *\n * Use a regular function (not an arrow function) if you need `this`.\n *\n * @example\n * ```ts\n * const handler: PrinterHandler<string, {}, 'string'> = function () {\n * return 'string'\n * }\n * ```\n */\ntype PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (\n this: PrinterHandlerContext<TOutput, TOptions>,\n node: SchemaNodeByType[T],\n) => TOutput | null\n\n/**\n * Partial map of per-node-type handler overrides for a printer.\n *\n * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).\n * Supply only the handlers you want to replace. The printer's built-in\n * defaults fill in the rest.\n *\n * @example\n * ```ts\n * pluginZod({\n * printer: {\n * nodes: {\n * date(): string {\n * return 'z.string().date()'\n * },\n * } satisfies PrinterPartial<string, PrinterZodOptions>,\n * },\n * })\n * ```\n */\nexport type PrinterPartial<TOutput, TOptions extends object> = Partial<{\n [K in SchemaType]: PrinterHandler<TOutput, TOptions, K>\n}>\n\n/**\n * Generic shape used by `definePrinter`.\n *\n * - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)\n * - `TOptions` options passed to and stored on the printer instance\n * - `TOutput` the type emitted by node handlers\n * - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)\n *\n * @example\n * ```ts\n * type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>\n * ```\n */\nexport type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {\n name: TName\n options: TOptions\n output: TOutput\n printOutput: TPrintOutput\n}\n\n/**\n * Printer instance returned by a printer factory.\n *\n * @example\n * ```ts\n * const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})\n * ```\n */\nexport type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {\n /**\n * Unique identifier supplied at creation time.\n */\n name: T['name']\n /**\n * Options for this printer instance.\n */\n options: T['options']\n /**\n * Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.\n * Always dispatches through the `nodes` map. Never calls the `print` override.\n * Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.\n */\n transform: (node: SchemaNode) => T['output'] | null\n /**\n * Public printer. If the builder provides a root-level `print`, this calls that\n * higher-level function (which may produce full declarations).\n * Otherwise, falls back to the node-level dispatcher.\n */\n print: (node: SchemaNode) => T['printOutput'] | null\n}\n\n/**\n * Builder function passed to `definePrinter`.\n *\n * It receives resolved options and returns:\n * - `name`\n * - `options`\n * - `nodes` handlers\n * - optional top-level `print` override\n *\n * @example\n * ```ts\n * const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })\n * ```\n */\ntype PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {\n name: T['name']\n /**\n * Options to store on the printer.\n */\n options: T['options']\n nodes: Partial<{\n [K in SchemaType]: PrinterHandler<T['output'], T['options'], K>\n }>\n /**\n * Optional root-level print override. When provided, becomes the public `printer.print`.\n * Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),\n * not the override itself, so recursion is safe.\n */\n print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null\n}\n/**\n * Defines a schema printer: a function that takes a `SchemaNode` and emits\n * code in your target language. Each plugin that produces code from schemas\n * (TypeScript types, Zod schemas, Faker factories) ships a printer built\n * with this helper.\n *\n * The builder receives resolved options and returns:\n *\n * - `name` unique identifier for the printer.\n * - `options` stored on the returned printer instance.\n * - `nodes` map of `SchemaType` → handler. Handlers return the rendered\n * output (a string, a TypeScript AST node, ...) for that schema type.\n * - `print` (optional), top-level override exposed as `printer.print`.\n * Use `this.transform(node)` inside it to dispatch to `nodes` recursively.\n *\n * Without a `print` override, `printer.print` falls back to `printer.transform`\n * (the node-level dispatcher).\n *\n * @example Tiny Zod printer\n * ```ts\n * import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'\n *\n * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>\n *\n * export const zodPrinter = definePrinter<PrinterZod>((options) => ({\n * name: 'zod',\n * options: { strict: options.strict ?? true },\n * nodes: {\n * string: () => 'z.string()',\n * object(node) {\n * const props = node.properties\n * .map((p) => `${p.name}: ${this.transform(p.schema)}`)\n * .join(', ')\n * return `z.object({ ${props} })`\n * },\n * },\n * }))\n * ```\n */\nexport function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {\n return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>\n}\n\n/**\n * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.\n *\n * @example\n * ```ts\n * export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(\n * (node) => node.kind,\n * )\n * ```\n */\nexport function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {\n return function <T extends PrinterFactoryOptions>(\n build: (options: T['options']) => {\n name: T['name']\n options: T['options']\n nodes: Partial<{\n [K in TKey]: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNodeByKey[K],\n ) => T['output'] | null\n }>\n print?: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNode,\n ) => T['printOutput'] | null\n },\n ): (options?: T['options']) => {\n name: T['name']\n options: T['options']\n transform: (node: TNode) => T['output'] | null\n print: (node: TNode) => T['printOutput'] | null\n } {\n return (options) => {\n const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))\n\n const context = {\n options: resolvedOptions,\n transform: (node: TNode): T['output'] | null => {\n const key = getKey(node)\n if (key === null) return null\n\n const handler = nodes[key]\n\n if (!handler) return null\n\n return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)\n },\n }\n\n return {\n name,\n options: resolvedOptions,\n transform: context.transform,\n print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;AAOA,SAAS,gBAAgB,MAA0B;CACjD,OAAO,GAAG,KAAK,aAAa,GAAG,GAAG,KAAK,UAAU,GAAG,GAAG,KAAK,WAAW,IAAI;AAC7E;AAEA,SAAS,cAAc,MAAoD;CACzE,IAAI,KAAK,KAAK,OAAO,eAAe,KAAK,GAAG;CAC5C,OAAO,KAAK,QAAQ;AACtB;AAuBA,MAAM,mBAA8C;CAClD;EAAE,MAAM;EAAY,KAAK;EAAS,QAAQ;CAAI;CAC9C;EAAE,MAAM;EAAS,KAAK;EAAQ,QAAQ;CAAI;CAC1C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAQ,KAAK;EAAU,QAAQ;CAAI;AAC7C;AAEA,MAAM,gBAA2C;CAC/C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAoB,QAAQ;CAAM;CACzD;EAAE,MAAM;EAAU,KAAK;EAAoB,QAAQ;CAAM;CACzD;EAAE,MAAM;EAAU,KAAK;EAAc,QAAQ;CAAK;AACpD;AAEA,MAAM,cAAyC,CAC7C;CAAE,MAAM;CAAU,KAAK;CAAO,QAAQ;AAAK,GAC3C;CAAE,MAAM;CAAU,KAAK;CAAO,QAAQ;AAAK,CAC7C;;;;;AAMA,MAAM,a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AM,WAAY,CAAC,CAAkB;GAE7G,MAAM,UAAU;IACd,SAAS;IACT,YAAY,SAAoC;KAC9C,MAAM,MAAM,OAAO,IAAI;KACvB,IAAI,QAAQ,MAAM,OAAO;KAEzB,MAAM,UAAU,MAAM;KAEtB,IAAI,CAAC,SAAS,OAAO;KAErB,OAAQ,QAAsE,KAAK,SAAS,IAAI;IAClG;GACF;GAEA,OAAO;IACL;IACA,SAAS;IACT,WAAW,QAAQ;IACnB,OAAQ,gBAAgB,cAAc,KAAK,OAAO,IAAI,QAAQ;GAChE;EACF;CACF;AACF"}
{"version":3,"file":"index.js","names":["obj"],"sources":["../src/signature.ts","../src/dedupe.ts","../src/dialect.ts","../src/definePrinter.ts"],"sourcesContent":["import { hash } from 'node:crypto'\nimport type { SchemaNode } from './nodes/index.ts'\nimport { extractRefName } from './utils/index.ts'\n\n/**\n * The flags shared by every node kind that affect its type: `primitive`, `format`, `nullable`.\n */\nfunction flagsDescriptor(node: SchemaNode): string {\n return `${node.primitive ?? ''};${node.format ?? ''};${node.nullable ? 1 : 0}`\n}\n\nfunction refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {\n if (node.ref) return extractRefName(node.ref)\n return node.name ?? ''\n}\n\ntype ScalarField = { kind: 'scalar'; key: string; prefix: string }\ntype BoolField = { kind: 'bool'; key: string; prefix: string }\ntype ChildField = { kind: 'child'; key: string; prefix: string }\ntype ChildrenField = { kind: 'children'; key: string; prefix: string }\ntype ObjectPropsField = { kind: 'objectProps' }\ntype AdditionalPropsField = { kind: 'additionalProps' }\ntype PatternPropsField = { kind: 'patternProps' }\ntype EnumValuesField = { kind: 'enumValues' }\ntype RefTargetField = { kind: 'refTarget' }\n\ntype ShapeField =\n | ScalarField\n | BoolField\n | ChildField\n | ChildrenField\n | ObjectPropsField\n | AdditionalPropsField\n | PatternPropsField\n | EnumValuesField\n | RefTargetField\n\nconst arrayTupleFields: ReadonlyArray<ShapeField> = [\n { kind: 'children', key: 'items', prefix: 'i' },\n { kind: 'child', key: 'rest', prefix: 'r' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'bool', key: 'unique', prefix: 'u' },\n]\n\nconst numericFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'exclusiveMinimum', prefix: 'emn' },\n { kind: 'scalar', key: 'exclusiveMaximum', prefix: 'emx' },\n { kind: 'scalar', key: 'multipleOf', prefix: 'mo' },\n]\n\nconst rangeFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n]\n\n/**\n * Maps each node `type` to its ordered shape-contributing fields. Types absent from the map\n * (boolean, null, any, and other scalars) fall back to `${type}|${flags}`.\n */\nconst SHAPE_KEYS: Partial<Record<SchemaNode['type'], ReadonlyArray<ShapeField>>> = {\n object: [\n { kind: 'objectProps' },\n { kind: 'additionalProps' },\n { kind: 'patternProps' },\n { kind: 'scalar', key: 'minProperties', prefix: 'mn' },\n { kind: 'scalar', key: 'maxProperties', prefix: 'mx' },\n ],\n array: arrayTupleFields,\n tuple: arrayTupleFields,\n union: [\n { kind: 'scalar', key: 'strategy', prefix: 's' },\n { kind: 'scalar', key: 'discriminatorPropertyName', prefix: 'd' },\n { kind: 'children', key: 'members', prefix: 'm' },\n ],\n intersection: [{ kind: 'children', key: 'members', prefix: 'm' }],\n enum: [{ kind: 'enumValues' }],\n ref: [{ kind: 'refTarget' }],\n string: [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'pattern', prefix: 'pt' },\n ],\n number: numericFields,\n integer: numericFields,\n bigint: numericFields,\n url: [\n { kind: 'scalar', key: 'path', prefix: 'path' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n ],\n uuid: rangeFields,\n email: rangeFields,\n datetime: [\n { kind: 'bool', key: 'offset', prefix: 'o' },\n { kind: 'bool', key: 'local', prefix: 'l' },\n ],\n date: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n time: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n}\n\nfunction serializeShapeField(field: ShapeField, node: SchemaNode, record: Record<string, unknown>): string {\n switch (field.kind) {\n case 'scalar':\n return `${field.prefix}:${record[field.key] ?? ''}`\n case 'bool':\n return `${field.prefix}:${record[field.key] ? 1 : 0}`\n case 'child': {\n const child = record[field.key] as SchemaNode | undefined\n return `${field.prefix}:${child ? signatureOf(child) : ''}`\n }\n case 'children': {\n const children = (record[field.key] as Array<SchemaNode> | undefined) ?? []\n return `${field.prefix}[${children.map((c) => signatureOf(c)).join(',')}]`\n }\n case 'objectProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const props = (obj.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema)}`).join(',')\n return `p[${props}]`\n }\n case 'additionalProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n if (typeof obj.additionalProperties === 'boolean') return `ab:${obj.additionalProperties}`\n if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`\n return ''\n }\n case 'patternProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const pattern = obj.patternProperties\n ? Object.keys(obj.patternProperties)\n .sort()\n .map((key) => `${key}=${signatureOf(obj.patternProperties![key]!)}`)\n .join(',')\n : ''\n return `pp[${pattern}]`\n }\n case 'enumValues': {\n const en = node as Extract<SchemaNode, { type: 'enum' }>\n let values = ''\n if (en.namedEnumValues?.length) {\n values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')\n } else if (en.enumValues?.length) {\n values = en.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')\n }\n return `v[${values}]`\n }\n case 'refTarget': {\n return `->${refTargetName(node as Extract<SchemaNode, { type: 'ref' }>)}`\n }\n }\n}\n\n/**\n * Builds the local shape descriptor that {@link signatureOf} hashes: the node's kind, flags,\n * constraints, and its children's signatures.\n */\nfunction describeShape(node: SchemaNode): string {\n const flags = flagsDescriptor(node)\n const fields = SHAPE_KEYS[node.type]\n if (!fields) return `${node.type}|${flags}`\n\n const record = node as unknown as Record<string, unknown>\n const parts: Array<string> = [`${node.type}|${flags}`]\n for (const field of fields) {\n parts.push(serializeShapeField(field, node, record))\n }\n return parts.join('|')\n}\n\n/**\n * Caches the digest per node, keyed by identity. A `WeakMap` so entries die with the node, and so\n * a tree hashed during dedupe planning is not walked again when it is rewritten during streaming.\n * Reuse is safe because a signature depends only on content, and nodes are immutable once created.\n */\nconst signatureCache = new WeakMap<SchemaNode, string>()\n\n/**\n * Computes a deterministic, shape-only signature (a content hash) for a schema node. Two schemas\n * share a signature when they are structurally identical, ignoring documentation (`name`, `title`,\n * `description`, `example`, `default`, `deprecated`) and usage-slot flags (`optional`, `nullish`,\n * `readOnly`, `writeOnly`). `nullable` is kept because it changes the produced type, and `ref`\n * nodes compare by target name, which also terminates on circular shapes.\n *\n * @example Two enums with different descriptions share a signature\n * ```ts\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))\n * ```\n */\nexport function signatureOf(node: SchemaNode): string {\n const cached = signatureCache.get(node)\n if (cached !== undefined) return cached\n const signature = hash('sha256', describeShape(node), 'hex')\n signatureCache.set(node, signature)\n\n return signature\n}\n\n/**\n * Returns `true` when two schema nodes are structurally identical under shape-only equality,\n * meaning they produce the same TypeScript type.\n */\nexport function isSchemaEqual(a: SchemaNode, b: SchemaNode): boolean {\n return signatureOf(a) === signatureOf(b)\n}\n","import type { Node, OperationNode, SchemaNode } from './nodes/index.ts'\nimport { createSchema } from './nodes/schema.ts'\nimport { signatureOf } from './signature.ts'\nimport { extractRefName } from './utils/index.ts'\nimport { collectLazy, transform } from './visitor.ts'\n\n/**\n * A canonical destination for a deduplicated shape: the shared schema name and\n * the synthetic `$ref` path that points at it.\n */\nexport type DedupeCanonical = {\n /**\n * Canonical schema name every duplicate occurrence refers to.\n */\n name: string\n /**\n * `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).\n */\n ref: string\n}\n\n/**\n * The result of {@link buildDedupePlan}: a lookup from structural signature to its\n * canonical target, plus the freshly hoisted definitions that must be added to\n * the schema list.\n */\nexport type DedupePlan = {\n /**\n * Maps a structural signature to the canonical schema that represents it.\n */\n canonicalBySignature: Map<string, DedupeCanonical>\n /**\n * Maps the name of a top-level schema that duplicates a canonical one to that canonical, so\n * references to the duplicate can be repointed at the first schema with the same content.\n */\n aliasNames: Map<string, DedupeCanonical>\n /**\n * New top-level schema definitions created for inline shapes that had no existing\n * named component. Nested duplicates inside each definition are already collapsed.\n */\n hoisted: Array<SchemaNode>\n}\n\n/**\n * The lookups {@link applyDedupe} needs from a {@link DedupePlan}.\n */\nexport type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>\n\n/**\n * Options that inject the naming and candidate policy into {@link buildDedupePlan}.\n * The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.\n */\nexport type BuildDedupePlanOptions = {\n /**\n * Returns `true` when a node should be deduplicated. This is the only gate, so it must\n * reject both ineligible kinds (return `false` for anything other than, say, enums and\n * objects) and unsafe shapes (e.g. nodes that reference a circular schema).\n */\n isCandidate: (node: SchemaNode) => boolean\n /**\n * Produces the canonical name for an inline shape with no existing named component.\n * Return `null` to leave the shape inline (for example when no contextual name exists).\n */\n nameFor: (node: SchemaNode, signature: string) => string | null\n /**\n * Builds the `$ref` path for a canonical name.\n */\n refFor: (name: string) => string\n /**\n * Minimum number of occurrences before a shape is deduplicated.\n *\n * @default 2\n */\n minOccurrences?: number\n}\n\n/**\n * Builds the shared `ref` replacement for a duplicate occurrence, carrying the\n * usage-slot and documentation fields that are not part of the canonical type.\n */\nfunction createRefNode(node: SchemaNode, canonical: DedupeCanonical): SchemaNode {\n return createSchema({\n type: 'ref',\n name: canonical.name,\n ref: canonical.ref,\n optional: node.optional,\n nullish: node.nullish,\n readOnly: node.readOnly,\n writeOnly: node.writeOnly,\n deprecated: node.deprecated,\n description: node.description,\n default: node.default,\n example: node.example,\n })\n}\n\n/**\n * Rewrites a node, replacing every candidate sub-schema whose signature has a canonical\n * target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,\n * so nested duplicates inside a replaced shape are not visited again. A `ref` that points\n * at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with\n * the same content.\n *\n * Pass `skipRootMatch` when rewriting a canonical definition so its own root is not\n * turned into a reference to itself. Nested duplicates are still collapsed.\n *\n * @example\n * ```ts\n * const next = applyDedupe(operationNode, plan)\n * ```\n */\nexport function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode\nexport function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode\nexport function applyDedupe(node: Node, plan: DedupeLookups, skipRootMatch = false): Node {\n const { canonicalBySignature, aliasNames } = plan\n if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node\n\n const root = node\n\n return transform(node, {\n schema(schemaNode) {\n if (schemaNode.type === 'ref') {\n const target = schemaNode.ref ? extractRefName(schemaNode.ref) : schemaNode.name\n const canonical = target ? aliasNames.get(target) : undefined\n\n return canonical ? { ...schemaNode, name: canonical.name, ref: canonical.ref } : undefined\n }\n\n const signature = signatureOf(schemaNode)\n if (skipRootMatch && schemaNode === root) return undefined\n\n const canonical = canonicalBySignature.get(signature)\n if (!canonical) return undefined\n\n return createRefNode(schemaNode, canonical)\n },\n })\n}\n\n/**\n * Strips usage-slot flags from a hoisted definition and applies its canonical name.\n * A standalone definition is never optional, so `optional`/`nullish` are cleared.\n */\nfunction cleanDefinition(node: SchemaNode, name: string): SchemaNode {\n return { ...node, name, optional: undefined, nullish: undefined }\n}\n\n/**\n * Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.\n *\n * A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence\n * is a named top-level schema, the first one becomes the canonical (so other top-level\n * duplicates and inline copies turn into references to it). Every other top-level name with\n * the same content is recorded in `aliasNames`, so refs to it can be repointed at the\n * canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied\n * per node with {@link applyDedupe}.\n *\n * @example\n * ```ts\n * const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {\n * isCandidate: (node) => node.type === 'enum' || node.type === 'object',\n * nameFor: (node) => node.name ?? null,\n * refFor: (name) => `#/components/schemas/${name}`,\n * })\n * ```\n */\nexport function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {\n const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options\n\n const topLevelNodes = new Set<SchemaNode>()\n\n type Group = {\n count: number\n representative: SchemaNode\n topLevelNames: Array<string>\n }\n const groups = new Map<string, Group>()\n\n function record(schemaNode: SchemaNode): void {\n if (!isCandidate(schemaNode)) return\n const signature = signatureOf(schemaNode)\n\n const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name\n const group = groups.get(signature)\n if (group) {\n group.count++\n if (isTopLevel) group.topLevelNames.push(schemaNode.name!)\n } else {\n groups.set(signature, { count: 1, representative: schemaNode, topLevelNames: isTopLevel ? [schemaNode.name!] : [] })\n }\n }\n\n for (const root of roots) {\n if (root.kind === 'Schema') topLevelNodes.add(root)\n for (const schemaNode of collectLazy<SchemaNode>(root, { schema: (node) => node })) {\n record(schemaNode)\n }\n }\n\n const canonicalBySignature = new Map<string, DedupeCanonical>()\n const aliasNames = new Map<string, DedupeCanonical>()\n const pendingHoists: Array<{ name: string; representative: SchemaNode }> = []\n\n for (const [signature, group] of groups) {\n if (group.count < minOccurrences) continue\n\n const [firstName, ...duplicateNames] = group.topLevelNames\n if (firstName) {\n const canonical: DedupeCanonical = { name: firstName, ref: refFor(firstName) }\n canonicalBySignature.set(signature, canonical)\n for (const duplicate of duplicateNames) {\n aliasNames.set(duplicate, canonical)\n }\n continue\n }\n\n const name = nameFor(group.representative, signature)\n if (!name) continue\n\n canonicalBySignature.set(signature, { name, ref: refFor(name) })\n pendingHoists.push({ name, representative: group.representative })\n }\n\n // Build hoisted definitions only after every canonical name is known, so nested\n // duplicates inside a definition also resolve to refs.\n const hoisted = pendingHoists.map(({ name, representative }) =>\n cleanDefinition(applyDedupe(representative, { canonicalBySignature, aliasNames }, true), name),\n )\n\n return { canonicalBySignature, aliasNames, hoisted }\n}\n","import type { SchemaNode } from './nodes/index.ts'\n\n/**\n * The spec-specific questions a schema parser answers while turning a source document into Kubb\n * AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where\n * OpenAPI, AsyncAPI, and plain JSON Schema differ.\n */\nexport type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {\n /**\n * Whether the schema is nullable.\n */\n isNullable(schema?: TSchema): boolean\n /**\n * Whether the value is a `$ref` pointer.\n */\n isReference(value?: unknown): value is TRef\n /**\n * Whether the schema carries a discriminator for polymorphism.\n */\n isDiscriminator(value?: unknown): value is TDiscriminated\n /**\n * Whether the schema is binary data, converted to a `blob` node.\n */\n isBinary(schema: TSchema): boolean\n /**\n * Resolves a local `$ref` against the document, or nullish when it cannot.\n */\n resolveRef<TResolved>(document: TDocument, ref: string): TResolved | null | undefined\n /**\n * Derives a schema's `optional`/`nullish` flags from a parent's `required` value and the\n * schema's own `nullable`. How \"required\" and \"nullable\" combine is spec-specific, so the\n * dialect owns it. Method syntax keeps a concrete dialect assignable to the base\n * `SchemaDialect` (bivariant parameters).\n */\n optionality(schema: SchemaNode, required: boolean): SchemaNode\n}\n\n/**\n * A spec adapter's dialect. `name` identifies it in logs and diagnostics; `schema` holds the\n * spec-specific schema questions the parser answers.\n */\nexport type Dialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {\n /**\n * Identifies the dialect in logs and diagnostics.\n */\n name: string\n /**\n * The spec-specific schema behavior. See {@link SchemaDialect}.\n */\n schema: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>\n}\n\n/**\n * Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the\n * dialect's type for inference.\n *\n * @example\n * ```ts\n * export const oasDialect = defineDialect({\n * name: 'oas',\n * schema: {\n * isNullable,\n * isReference,\n * isDiscriminator,\n * isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',\n * resolveRef,\n * optionality,\n * },\n * })\n * ```\n */\nexport function defineDialect<TSchema, TRef, TDiscriminated, TDocument>(\n dialect: Dialect<TSchema, TRef, TDiscriminated, TDocument>,\n): Dialect<TSchema, TRef, TDiscriminated, TDocument> {\n return dialect\n}\n","import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'\n\n/**\n * Runtime context passed as `this` to printer handlers.\n *\n * `this.transform` dispatches to node-level handlers from `nodes`.\n *\n * @example\n * ```ts\n * const context: PrinterHandlerContext<string, {}> = {\n * options: {},\n * transform: () => 'value',\n * }\n * ```\n */\ntype PrinterHandlerContext<TOutput, TOptions extends object> = {\n /**\n * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.\n * Use `this.transform` inside `nodes` handlers and inside the `print` override.\n */\n transform: (node: SchemaNode) => TOutput | null\n /**\n * Options for this printer instance.\n */\n options: TOptions\n}\n\n/**\n * Handler for one schema node type.\n *\n * Use a regular function (not an arrow function) if you need `this`.\n *\n * @example\n * ```ts\n * const handler: PrinterHandler<string, {}, 'string'> = function () {\n * return 'string'\n * }\n * ```\n */\ntype PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (\n this: PrinterHandlerContext<TOutput, TOptions>,\n node: SchemaNodeByType[T],\n) => TOutput | null\n\n/**\n * Partial map of per-node-type handler overrides for a printer.\n *\n * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).\n * Supply only the handlers you want to replace. The printer's built-in\n * defaults fill in the rest.\n *\n * @example\n * ```ts\n * pluginZod({\n * printer: {\n * nodes: {\n * date(): string {\n * return 'z.string().date()'\n * },\n * } satisfies PrinterPartial<string, PrinterZodOptions>,\n * },\n * })\n * ```\n */\nexport type PrinterPartial<TOutput, TOptions extends object> = Partial<{\n [K in SchemaType]: PrinterHandler<TOutput, TOptions, K>\n}>\n\n/**\n * Generic shape used by `definePrinter`.\n *\n * - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)\n * - `TOptions` options passed to and stored on the printer instance\n * - `TOutput` the type emitted by node handlers\n * - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)\n *\n * @example\n * ```ts\n * type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>\n * ```\n */\nexport type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {\n name: TName\n options: TOptions\n output: TOutput\n printOutput: TPrintOutput\n}\n\n/**\n * Printer instance returned by a printer factory.\n *\n * @example\n * ```ts\n * const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})\n * ```\n */\nexport type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {\n /**\n * Unique identifier supplied at creation time.\n */\n name: T['name']\n /**\n * Options for this printer instance.\n */\n options: T['options']\n /**\n * Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.\n * Always dispatches through the `nodes` map. Never calls the `print` override.\n * Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.\n */\n transform: (node: SchemaNode) => T['output'] | null\n /**\n * Public printer. If the builder provides a root-level `print`, this calls that\n * higher-level function (which may produce full declarations).\n * Otherwise, falls back to the node-level dispatcher.\n */\n print: (node: SchemaNode) => T['printOutput'] | null\n}\n\n/**\n * Builder function passed to `definePrinter`.\n *\n * It receives resolved options and returns:\n * - `name`\n * - `options`\n * - `nodes` handlers\n * - optional top-level `print` override\n *\n * @example\n * ```ts\n * const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })\n * ```\n */\ntype PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {\n name: T['name']\n /**\n * Options to store on the printer.\n */\n options: T['options']\n nodes: Partial<{\n [K in SchemaType]: PrinterHandler<T['output'], T['options'], K>\n }>\n /**\n * Optional root-level print override. When provided, becomes the public `printer.print`.\n * Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),\n * not the override itself, so recursion is safe.\n */\n print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null\n}\n/**\n * Defines a schema printer: a function that takes a `SchemaNode` and emits\n * code in your target language. Each plugin that produces code from schemas\n * (TypeScript types, Zod schemas, Faker factories) ships a printer built\n * with this helper.\n *\n * The builder receives resolved options and returns:\n *\n * - `name` unique identifier for the printer.\n * - `options` stored on the returned printer instance.\n * - `nodes` map of `SchemaType` → handler. Handlers return the rendered\n * output (a string, a TypeScript AST node, ...) for that schema type.\n * - `print` (optional), top-level override exposed as `printer.print`.\n * Use `this.transform(node)` inside it to dispatch to `nodes` recursively.\n *\n * Without a `print` override, `printer.print` falls back to `printer.transform`\n * (the node-level dispatcher).\n *\n * @example Tiny Zod printer\n * ```ts\n * import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'\n *\n * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>\n *\n * export const zodPrinter = definePrinter<PrinterZod>((options) => ({\n * name: 'zod',\n * options: { strict: options.strict ?? true },\n * nodes: {\n * string: () => 'z.string()',\n * object(node) {\n * const props = node.properties\n * .map((p) => `${p.name}: ${this.transform(p.schema)}`)\n * .join(', ')\n * return `z.object({ ${props} })`\n * },\n * },\n * }))\n * ```\n */\nexport function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {\n return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>\n}\n\n/**\n * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.\n *\n * @example\n * ```ts\n * export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(\n * (node) => node.kind,\n * )\n * ```\n */\nexport function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {\n return function <T extends PrinterFactoryOptions>(\n build: (options: T['options']) => {\n name: T['name']\n options: T['options']\n nodes: Partial<{\n [K in TKey]: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNodeByKey[K],\n ) => T['output'] | null\n }>\n print?: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNode,\n ) => T['printOutput'] | null\n },\n ): (options?: T['options']) => {\n name: T['name']\n options: T['options']\n transform: (node: TNode) => T['output'] | null\n print: (node: TNode) => T['printOutput'] | null\n } {\n return (options) => {\n const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))\n\n const context = {\n options: resolvedOptions,\n transform: (node: TNode): T['output'] | null => {\n const key = getKey(node)\n if (key === null) return null\n\n const handler = nodes[key]\n\n if (!handler) return null\n\n return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)\n },\n }\n\n return {\n name,\n options: resolvedOptions,\n transform: context.transform,\n print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;AAOA,SAAS,gBAAgB,MAA0B;CACjD,OAAO,GAAG,KAAK,aAAa,GAAG,GAAG,KAAK,UAAU,GAAG,GAAG,KAAK,WAAW,IAAI;AAC7E;AAEA,SAAS,cAAc,MAAoD;CACzE,IAAI,KAAK,KAAK,OAAO,eAAe,KAAK,GAAG;CAC5C,OAAO,KAAK,QAAQ;AACtB;AAuBA,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,OAAO,QAAQ,MAAM,gBAAgB,SAAS;EACpD,IAAI,CAAC,MAAM;EAEX,qBAAqB,IAAI,WAAW;GAAE;GAAM,KAAK,OAAO,IAAI;EAAE,CAAC;EAC/D,cAAc,KAAK;GAAE;GAAM,gBAAgB,MAAM;EAAe,CAAC;CACnE;CAQA,OAAO;EAAE;EAAsB;EAAY,SAJ3B,cAAc,KAAK,EAAE,MAAM,qBACzC,gBAAgB,YAAY,gBAAgB;GAAE;GAAsB;EAAW,GAAG,IAAI,GAAG,IAAI,CAG9C;CAAE;AACrD;;;;;;;;;;;;;;;;;;;;;;AC/JA,SAAgB,cACd,SACmD;CACnD,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACiHA,SAAgB,cAAuE,OAAkE;CACvJ,OAAO,sBAAgE,SAAS,KAAK,IAAI,CAAC,CAAC,KAAK;AAClG;;;;;;;;;;;AAYA,SAAgB,qBAAkG,QAAsC;CACtJ,OAAO,SACL,OAyBA;EACA,QAAQ,YAAY;GAClB,MAAM,EAAE,MAAM,SAAS,iBAAiB,OAAO,OAAO,kBAAkB,MAAM,WAAY,CAAC,CAAkB;GAE7G,MAAM,UAAU;IACd,SAAS;IACT,YAAY,SAAoC;KAC9C,MAAM,MAAM,OAAO,IAAI;KACvB,IAAI,QAAQ,MAAM,OAAO;KAEzB,MAAM,UAAU,MAAM;KAEtB,IAAI,CAAC,SAAS,OAAO;KAErB,OAAQ,QAAsE,KAAK,SAAS,IAAI;IAClG;GACF;GAEA,OAAO;IACL;IACA,SAAS;IACT,WAAW,QAAQ;IACnB,OAAQ,gBAAgB,cAAc,KAAK,OAAO,IAAI,QAAQ;GAChE;EACF;CACF;AACF"}
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_response = require("./response-hnSw2NKE.cjs");
const require_visitor = require("./visitor-DpKZ9Tk0.cjs");
const require_defineMacro = require("./defineMacro-Bv9R_9a2.cjs");
const require_response = require("./response-BeG6hq2D.cjs");
const require_visitor = require("./visitor-B7Zlnp4c.cjs");
const require_defineMacro = require("./defineMacro-BryDiuQl.cjs");
//#region src/macros/macroDiscriminatorEnum.ts

@@ -74,2 +74,32 @@ /**

/**
* Scalar primitive schema types used for union simplification and type narrowing.
*/
const SCALAR_PRIMITIVE_TYPES = new Set([
"string",
"number",
"integer",
"bigint",
"boolean"
]);
function isScalarPrimitive(type) {
return SCALAR_PRIMITIVE_TYPES.has(type);
}
/**
* Filters union members, dropping enum members that a broader scalar primitive already covers.
*/
function simplifyUnionMembers(members) {
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
if (!scalarPrimitives.size) return members;
return members.filter((member) => {
const enumNode = require_visitor.narrowSchema(member, "enum");
if (!enumNode) return true;
const primitive = enumNode.primitive;
if (!primitive) return true;
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
if (scalarPrimitives.has(primitive)) return false;
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
return true;
});
}
/**
* Removes union members a broader scalar primitive already covers, such as a multi-value string enum

@@ -96,19 +126,2 @@ * sitting next to a plain `string`. Single-value enums are kept.

});
/**
* Filters union members, dropping enum members that a broader scalar primitive already covers.
*/
function simplifyUnionMembers(members) {
const scalarPrimitives = new Set(members.filter((member) => require_visitor.isScalarPrimitive(member.type)).map((m) => m.type));
if (!scalarPrimitives.size) return members;
return members.filter((member) => {
const enumNode = require_visitor.narrowSchema(member, "enum");
if (!enumNode) return true;
const primitive = enumNode.primitive;
if (!primitive) return true;
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
if (scalarPrimitives.has(primitive)) return false;
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
return true;
});
}
//#endregion

@@ -115,0 +128,0 @@ exports.macroDiscriminatorEnum = macroDiscriminatorEnum;

@@ -1,1 +0,1 @@

{"version":3,"file":"macros.cjs","names":["defineMacro","narrowSchema","createSchema","createProperty","defineMacro","narrowSchema","enumPropName","defineMacro","narrowSchema","isScalarPrimitive"],"sources":["../src/macros/macroDiscriminatorEnum.ts","../src/macros/macroEnumName.ts","../src/macros/macroSimplifyUnion.ts"],"sourcesContent":["import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { createProperty } from '../nodes/property.ts'\nimport { createSchema } from '../nodes/schema.ts'\n\n/**\n * Builds a macro that replaces a discriminator property's schema with a string enum of the given\n * values. Object schemas that lack the property are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })\n * const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroDiscriminatorEnum({ propertyName, values, enumName }: { propertyName: string; values: Array<string>; enumName?: string }) {\n return defineMacro({\n name: 'discriminator-enum',\n schema(node) {\n const objectNode = narrowSchema(node, 'object')\n if (!objectNode?.properties?.length) return undefined\n if (!objectNode.properties.some((prop) => prop.name === propertyName)) return undefined\n\n return createSchema({\n ...objectNode,\n properties: objectNode.properties.map((prop) => {\n if (prop.name !== propertyName) return prop\n\n return createProperty({\n ...prop,\n schema: createSchema({\n type: 'enum',\n primitive: 'string',\n enumValues: values,\n name: enumName,\n readOnly: prop.schema.readOnly,\n writeOnly: prop.schema.writeOnly,\n }),\n })\n }),\n })\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { enumPropName } from '../utils/refs.ts'\n\n/**\n * Builds a macro that names an inline enum schema from its parent and property name. Boolean enums\n * are left anonymous; non-enum nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })\n * const named = applyMacros(propSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroEnumName({ parentName, propName, enumSuffix }: { parentName: string | null | undefined; propName: string; enumSuffix: string }) {\n return defineMacro({\n name: 'enum-name',\n schema(node) {\n const enumNode = narrowSchema(node, 'enum')\n if (enumNode?.primitive === 'boolean') return { ...node, name: null }\n if (enumNode) return { ...node, name: enumPropName(parentName, propName, enumSuffix) }\n return undefined\n },\n })\n}\n","import { isScalarPrimitive } from '../constants.ts'\nimport { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport type { SchemaNode } from '../nodes/schema.ts'\n\n/**\n * Removes union members a broader scalar primitive already covers, such as a multi-value string enum\n * sitting next to a plain `string`. Single-value enums are kept.\n *\n * @example\n * ```ts\n * const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })\n * ```\n */\nexport const macroSimplifyUnion = defineMacro({\n name: 'simplify-union',\n schema(node) {\n const unionNode = narrowSchema(node, 'union')\n if (!unionNode?.members?.length) return undefined\n\n const simplified = simplifyUnionMembers(unionNode.members)\n if (simplified.length === unionNode.members.length) return undefined\n\n return { ...unionNode, members: simplified }\n },\n})\n\n/**\n * Filters union members, dropping enum members that a broader scalar primitive already covers.\n */\nfunction simplifyUnionMembers(members: Array<SchemaNode>): Array<SchemaNode> {\n const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))\n if (!scalarPrimitives.size) return members\n\n return members.filter((member) => {\n const enumNode = narrowSchema(member, 'enum')\n if (!enumNode) return true\n\n const primitive = enumNode.primitive\n if (!primitive) return true\n\n const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0\n if (enumValueCount <= 1) return true\n\n if (scalarPrimitives.has(primitive)) return false\n if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) return false\n\n return true\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;AAeA,SAAgB,uBAAuB,EAAE,cAAc,QAAQ,YAAgF;CAC7I,OAAOA,oBAAAA,YAAY;EACjB,MAAM;EACN,OAAO,MAAM;GACX,MAAM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I,IAAI,QAAQ,QAAQ,WAAWC,gBAAAA,kBAAkB,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,IAAI,CAAC;CAC9G,IAAI,CAAC,iBAAiB,MAAM,OAAO;CAEnC,OAAO,QAAQ,QAAQ,WAAW;EAChC,MAAM,WAAWD,gBAAAA,aAAa,QAAQ,MAAM;EAC5C,IAAI,CAAC,UAAU,OAAO;EAEtB,MAAM,YAAY,SAAS;EAC3B,IAAI,CAAC,WAAW,OAAO;EAGvB,KADuB,SAAS,iBAAiB,UAAU,SAAS,YAAY,UAAU,MACpE,GAAG,OAAO;EAEhC,IAAI,iBAAiB,IAAI,SAAS,GAAG,OAAO;EAC5C,KAAK,cAAc,aAAa,cAAc,cAAc,iBAAiB,IAAI,SAAS,KAAK,iBAAiB,IAAI,QAAQ,IAAI,OAAO;EAEvI,OAAO;CACT,CAAC;AACH"}
{"version":3,"file":"macros.cjs","names":["defineMacro","narrowSchema","createSchema","createProperty","defineMacro","narrowSchema","enumPropName","narrowSchema","defineMacro"],"sources":["../src/macros/macroDiscriminatorEnum.ts","../src/macros/macroEnumName.ts","../src/macros/macroSimplifyUnion.ts"],"sourcesContent":["import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { createProperty } from '../nodes/property.ts'\nimport { createSchema } from '../nodes/schema.ts'\n\ntype Props = {\n propertyName: string\n values: Array<string>\n enumName?: string\n}\n\n/**\n * Builds a macro that replaces a discriminator property's schema with a string enum of the given\n * values. Object schemas that lack the property are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })\n * const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroDiscriminatorEnum({ propertyName, values, enumName }: Props) {\n return defineMacro({\n name: 'discriminator-enum',\n schema(node) {\n const objectNode = narrowSchema(node, 'object')\n if (!objectNode?.properties?.length) return undefined\n if (!objectNode.properties.some((prop) => prop.name === propertyName)) return undefined\n\n return createSchema({\n ...objectNode,\n properties: objectNode.properties.map((prop) => {\n if (prop.name !== propertyName) return prop\n\n return createProperty({\n ...prop,\n schema: createSchema({\n type: 'enum',\n primitive: 'string',\n enumValues: values,\n name: enumName,\n readOnly: prop.schema.readOnly,\n writeOnly: prop.schema.writeOnly,\n }),\n })\n }),\n })\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { enumPropName } from '../utils/refs.ts'\n\ntype Props = {\n parentName: string | null | undefined\n propName: string\n enumSuffix: string\n}\n\n/**\n * Builds a macro that names an inline enum schema from its parent and property name. Boolean enums\n * are left anonymous; non-enum nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })\n * const named = applyMacros(propSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroEnumName({ parentName, propName, enumSuffix }: Props) {\n return defineMacro({\n name: 'enum-name',\n schema(node) {\n const enumNode = narrowSchema(node, 'enum')\n\n if (enumNode?.primitive === 'boolean') return { ...node, name: null }\n if (enumNode) return { ...node, name: enumPropName(parentName, propName, enumSuffix) }\n\n return undefined\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport type { SchemaNode } from '../nodes/schema.ts'\n\ntype ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'\n\n/**\n * Scalar primitive schema types used for union simplification and type narrowing.\n */\nconst SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])\n\nfunction isScalarPrimitive(type: string): type is ScalarPrimitive {\n return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)\n}\n\n/**\n * Filters union members, dropping enum members that a broader scalar primitive already covers.\n */\nfunction simplifyUnionMembers(members: Array<SchemaNode>): Array<SchemaNode> {\n const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))\n if (!scalarPrimitives.size) return members\n\n return members.filter((member) => {\n const enumNode = narrowSchema(member, 'enum')\n if (!enumNode) return true\n\n const primitive = enumNode.primitive\n if (!primitive) return true\n\n const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0\n if (enumValueCount <= 1) return true\n\n if (scalarPrimitives.has(primitive)) return false\n if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) return false\n\n return true\n })\n}\n\n/**\n * Removes union members a broader scalar primitive already covers, such as a multi-value string enum\n * sitting next to a plain `string`. Single-value enums are kept.\n *\n * @example\n * ```ts\n * const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })\n * ```\n */\nexport const macroSimplifyUnion = defineMacro({\n name: 'simplify-union',\n schema(node) {\n const unionNode = narrowSchema(node, 'union')\n if (!unionNode?.members?.length) return undefined\n\n const simplified = simplifyUnionMembers(unionNode.members)\n if (simplified.length === unionNode.members.length) return undefined\n\n return { ...unionNode, members: simplified }\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;AAqBA,SAAgB,uBAAuB,EAAE,cAAc,QAAQ,YAAmB;CAChF,OAAOA,oBAAAA,YAAY;EACjB,MAAM;EACN,OAAO,MAAM;GACX,MAAM,aAAaC,gBAAAA,aAAa,MAAM,QAAQ;GAC9C,IAAI,CAAC,YAAY,YAAY,QAAQ,OAAO,KAAA;GAC5C,IAAI,CAAC,WAAW,WAAW,MAAM,SAAS,KAAK,SAAS,YAAY,GAAG,OAAO,KAAA;GAE9E,OAAOC,iBAAAA,aAAa;IAClB,GAAG;IACH,YAAY,WAAW,WAAW,KAAK,SAAS;KAC9C,IAAI,KAAK,SAAS,cAAc,OAAO;KAEvC,OAAOC,iBAAAA,eAAe;MACpB,GAAG;MACH,QAAQD,iBAAAA,aAAa;OACnB,MAAM;OACN,WAAW;OACX,YAAY;OACZ,MAAM;OACN,UAAU,KAAK,OAA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Y;CAC5C,MAAM;CACN,OAAO,MAAM;EACX,MAAM,YAAYD,gBAAAA,aAAa,MAAM,OAAO;EAC5C,IAAI,CAAC,WAAW,SAAS,QAAQ,OAAO,KAAA;EAExC,MAAM,aAAa,qBAAqB,UAAU,OAAO;EACzD,IAAI,WAAW,WAAW,UAAU,QAAQ,QAAQ,OAAO,KAAA;EAE3D,OAAO;GAAE,GAAG;GAAW,SAAS;EAAW;CAC7C;AACF,CAAC"}
import { n as __name } from "./chunk-CNktS9qV.js";
import { t as Macro } from "./defineMacro-BLIR6k-j.js";
import { n as Macro } from "./defineMacro-Vw-xQvq7.js";
//#region src/macros/macroDiscriminatorEnum.d.ts
type Props$1 = {
propertyName: string;
values: Array<string>;
enumName?: string;
};
/**

@@ -19,9 +24,10 @@ * Builds a macro that replaces a discriminator property's schema with a string enum of the given

enumName
}: {
propertyName: string;
values: Array<string>;
enumName?: string;
}): Macro;
}: Props$1): Macro;
//#endregion
//#region src/macros/macroEnumName.d.ts
type Props = {
parentName: string | null | undefined;
propName: string;
enumSuffix: string;
};
/**

@@ -41,7 +47,3 @@ * Builds a macro that names an inline enum schema from its parent and property name. Boolean enums

enumSuffix
}: {
parentName: string | null | undefined;
propName: string;
enumSuffix: string;
}): Macro;
}: Props): Macro;
//#endregion

@@ -48,0 +50,0 @@ //#region src/macros/macroSimplifyUnion.d.ts

import "./chunk-CNktS9qV.js";
import { _ as isScalarPrimitive, m as narrowSchema, s as enumPropName } from "./visitor-DJ6ZEJvq.js";
import { X as createSchema, r as createProperty } from "./response-KUdWiDWw.js";
import { r as defineMacro } from "./defineMacro-BTXvS8nI.js";
import { m as narrowSchema, s as enumPropName } from "./visitor-DepQEKyp.js";
import { Q as createSchema, r as createProperty } from "./response-Rd1uisM1.js";
import { r as defineMacro } from "./defineMacro-CNkUpxia.js";
//#region src/macros/macroDiscriminatorEnum.ts

@@ -74,2 +74,32 @@ /**

/**
* Scalar primitive schema types used for union simplification and type narrowing.
*/
const SCALAR_PRIMITIVE_TYPES = new Set([
"string",
"number",
"integer",
"bigint",
"boolean"
]);
function isScalarPrimitive(type) {
return SCALAR_PRIMITIVE_TYPES.has(type);
}
/**
* Filters union members, dropping enum members that a broader scalar primitive already covers.
*/
function simplifyUnionMembers(members) {
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
if (!scalarPrimitives.size) return members;
return members.filter((member) => {
const enumNode = narrowSchema(member, "enum");
if (!enumNode) return true;
const primitive = enumNode.primitive;
if (!primitive) return true;
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
if (scalarPrimitives.has(primitive)) return false;
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
return true;
});
}
/**
* Removes union members a broader scalar primitive already covers, such as a multi-value string enum

@@ -96,19 +126,2 @@ * sitting next to a plain `string`. Single-value enums are kept.

});
/**
* Filters union members, dropping enum members that a broader scalar primitive already covers.
*/
function simplifyUnionMembers(members) {
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
if (!scalarPrimitives.size) return members;
return members.filter((member) => {
const enumNode = narrowSchema(member, "enum");
if (!enumNode) return true;
const primitive = enumNode.primitive;
if (!primitive) return true;
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
if (scalarPrimitives.has(primitive)) return false;
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
return true;
});
}
//#endregion

@@ -115,0 +128,0 @@ export { macroDiscriminatorEnum, macroEnumName, macroSimplifyUnion };

@@ -1,1 +0,1 @@

{"version":3,"file":"macros.js","names":[],"sources":["../src/macros/macroDiscriminatorEnum.ts","../src/macros/macroEnumName.ts","../src/macros/macroSimplifyUnion.ts"],"sourcesContent":["import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { createProperty } from '../nodes/property.ts'\nimport { createSchema } from '../nodes/schema.ts'\n\n/**\n * Builds a macro that replaces a discriminator property's schema with a string enum of the given\n * values. Object schemas that lack the property are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })\n * const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroDiscriminatorEnum({ propertyName, values, enumName }: { propertyName: string; values: Array<string>; enumName?: string }) {\n return defineMacro({\n name: 'discriminator-enum',\n schema(node) {\n const objectNode = narrowSchema(node, 'object')\n if (!objectNode?.properties?.length) return undefined\n if (!objectNode.properties.some((prop) => prop.name === propertyName)) return undefined\n\n return createSchema({\n ...objectNode,\n properties: objectNode.properties.map((prop) => {\n if (prop.name !== propertyName) return prop\n\n return createProperty({\n ...prop,\n schema: createSchema({\n type: 'enum',\n primitive: 'string',\n enumValues: values,\n name: enumName,\n readOnly: prop.schema.readOnly,\n writeOnly: prop.schema.writeOnly,\n }),\n })\n }),\n })\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { enumPropName } from '../utils/refs.ts'\n\n/**\n * Builds a macro that names an inline enum schema from its parent and property name. Boolean enums\n * are left anonymous; non-enum nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })\n * const named = applyMacros(propSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroEnumName({ parentName, propName, enumSuffix }: { parentName: string | null | undefined; propName: string; enumSuffix: string }) {\n return defineMacro({\n name: 'enum-name',\n schema(node) {\n const enumNode = narrowSchema(node, 'enum')\n if (enumNode?.primitive === 'boolean') return { ...node, name: null }\n if (enumNode) return { ...node, name: enumPropName(parentName, propName, enumSuffix) }\n return undefined\n },\n })\n}\n","import { isScalarPrimitive } from '../constants.ts'\nimport { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport type { SchemaNode } from '../nodes/schema.ts'\n\n/**\n * Removes union members a broader scalar primitive already covers, such as a multi-value string enum\n * sitting next to a plain `string`. Single-value enums are kept.\n *\n * @example\n * ```ts\n * const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })\n * ```\n */\nexport const macroSimplifyUnion = defineMacro({\n name: 'simplify-union',\n schema(node) {\n const unionNode = narrowSchema(node, 'union')\n if (!unionNode?.members?.length) return undefined\n\n const simplified = simplifyUnionMembers(unionNode.members)\n if (simplified.length === unionNode.members.length) return undefined\n\n return { ...unionNode, members: simplified }\n },\n})\n\n/**\n * Filters union members, dropping enum members that a broader scalar primitive already covers.\n */\nfunction simplifyUnionMembers(members: Array<SchemaNode>): Array<SchemaNode> {\n const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))\n if (!scalarPrimitives.size) return members\n\n return members.filter((member) => {\n const enumNode = narrowSchema(member, 'enum')\n if (!enumNode) return true\n\n const primitive = enumNode.primitive\n if (!primitive) return true\n\n const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0\n if (enumValueCount <= 1) return true\n\n if (scalarPrimitives.has(primitive)) return false\n if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) return false\n\n return true\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;AAeA,SAAgB,uBAAuB,EAAE,cAAc,QAAQ,YAAgF;CAC7I,OAAO,YAAY;EACjB,MAAM;EACN,OAAO,MAAM;GACX,MAAM,aAAa,aAAa,MAAM,QAAQ;GAC9C,IAAI,CAAC,YAAY,YAAY,QAAQ,OAAO,KAAA;GAC5C,IAAI,CAAC,WAAW,WAAW,MAAM,SAAS,KAAK,SAAS,YAAY,GAAG,OAAO,KAAA;GAE9E,OAAO,aAAa;IAClB,GAAG;IACH,YAAY,WAAW,WAAW,KAAK,SAAS;KAC9C,IAAI,KAAK,SAAS,cAAc,OAAO;KAEvC,OAAO,eAAe;MACpB,GAA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AAI,iBAAiB,IAAI,SAAS,GAAG,OAAO;EAC5C,KAAK,cAAc,aAAa,cAAc,cAAc,iBAAiB,IAAI,SAAS,KAAK,iBAAiB,IAAI,QAAQ,IAAI,OAAO;EAEvI,OAAO;CACT,CAAC;AACH"}
{"version":3,"file":"macros.js","names":[],"sources":["../src/macros/macroDiscriminatorEnum.ts","../src/macros/macroEnumName.ts","../src/macros/macroSimplifyUnion.ts"],"sourcesContent":["import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { createProperty } from '../nodes/property.ts'\nimport { createSchema } from '../nodes/schema.ts'\n\ntype Props = {\n propertyName: string\n values: Array<string>\n enumName?: string\n}\n\n/**\n * Builds a macro that replaces a discriminator property's schema with a string enum of the given\n * values. Object schemas that lack the property are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })\n * const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroDiscriminatorEnum({ propertyName, values, enumName }: Props) {\n return defineMacro({\n name: 'discriminator-enum',\n schema(node) {\n const objectNode = narrowSchema(node, 'object')\n if (!objectNode?.properties?.length) return undefined\n if (!objectNode.properties.some((prop) => prop.name === propertyName)) return undefined\n\n return createSchema({\n ...objectNode,\n properties: objectNode.properties.map((prop) => {\n if (prop.name !== propertyName) return prop\n\n return createProperty({\n ...prop,\n schema: createSchema({\n type: 'enum',\n primitive: 'string',\n enumValues: values,\n name: enumName,\n readOnly: prop.schema.readOnly,\n writeOnly: prop.schema.writeOnly,\n }),\n })\n }),\n })\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { enumPropName } from '../utils/refs.ts'\n\ntype Props = {\n parentName: string | null | undefined\n propName: string\n enumSuffix: string\n}\n\n/**\n * Builds a macro that names an inline enum schema from its parent and property name. Boolean enums\n * are left anonymous; non-enum nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })\n * const named = applyMacros(propSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroEnumName({ parentName, propName, enumSuffix }: Props) {\n return defineMacro({\n name: 'enum-name',\n schema(node) {\n const enumNode = narrowSchema(node, 'enum')\n\n if (enumNode?.primitive === 'boolean') return { ...node, name: null }\n if (enumNode) return { ...node, name: enumPropName(parentName, propName, enumSuffix) }\n\n return undefined\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport type { SchemaNode } from '../nodes/schema.ts'\n\ntype ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'\n\n/**\n * Scalar primitive schema types used for union simplification and type narrowing.\n */\nconst SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])\n\nfunction isScalarPrimitive(type: string): type is ScalarPrimitive {\n return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)\n}\n\n/**\n * Filters union members, dropping enum members that a broader scalar primitive already covers.\n */\nfunction simplifyUnionMembers(members: Array<SchemaNode>): Array<SchemaNode> {\n const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))\n if (!scalarPrimitives.size) return members\n\n return members.filter((member) => {\n const enumNode = narrowSchema(member, 'enum')\n if (!enumNode) return true\n\n const primitive = enumNode.primitive\n if (!primitive) return true\n\n const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0\n if (enumValueCount <= 1) return true\n\n if (scalarPrimitives.has(primitive)) return false\n if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) return false\n\n return true\n })\n}\n\n/**\n * Removes union members a broader scalar primitive already covers, such as a multi-value string enum\n * sitting next to a plain `string`. Single-value enums are kept.\n *\n * @example\n * ```ts\n * const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })\n * ```\n */\nexport const macroSimplifyUnion = defineMacro({\n name: 'simplify-union',\n schema(node) {\n const unionNode = narrowSchema(node, 'union')\n if (!unionNode?.members?.length) return undefined\n\n const simplified = simplifyUnionMembers(unionNode.members)\n if (simplified.length === unionNode.members.length) return undefined\n\n return { ...unionNode, members: simplified }\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;AAqBA,SAAgB,uBAAuB,EAAE,cAAc,QAAQ,YAAmB;CAChF,OAAO,YAAY;EACjB,MAAM;EACN,OAAO,MAAM;GACX,MAAM,aAAa,aAAa,MAAM,QAAQ;GAC9C,IAAI,CAAC,YAAY,YAAY,QAAQ,OAAO,KAAA;GAC5C,IAAI,CAAC,WAAW,WAAW,MAAM,SAAS,KAAK,SAAS,YAAY,GAAG,OAAO,KAAA;GAE9E,OAAO,aAAa;IAClB,GAAG;IACH,YAAY,WAAW,WAAW,KAAK,SAAS;KAC9C,IAAI,KAAK,SAAS,cAAc,OAAO;KAEvC,OAAO,eAAe;MACpB,GAAG;MACH,QAAQ,aAAa;OACnB,MAAM;OACN,WAAW;OACX,YAAY;OACZ,MAAM;OACN,UAAU,KAAK,OAAO;OACtB,WAAW,KAAK,OAAO;MACzB,CAAC;KACH,CAAC;IACH,CAAC;GACH,CAAC;EACH;CACF,CAAC;AACH;;;;;;;;;;;;;AC7BA,SAAgB,cAAc,EAAE,YAAY,UAAU,cAAqB;CACzE,OAAO,YAAY;EACjB,MAAM;EACN,OAAO,MAAM;GACX,MAAM,WAAW,aAAa,MAAM,MAAM;GAE1C,IAAI,UAAU,cAAc,WAAW,OAAO;IAAE,GAAG;IAAM,MAAM;GAAK;GACpE,IAAI,UAAU,OAAO;IAAE,GAAG;IAAM,MAAM,aAAa,YAAY,UAAU,UAAU;GAAE;EAGvF;CACF,CAAC;AACH;;;;;;ACvBA,MAAM,yBAAyB,IAAI,IAAqB;CAAC;CAAU;CAAU;CAAW;CAAU;AAAS,CAAC;AAE5G,SAAS,kBAAkB,MAAuC;CAChE,OAAO,uBAAuB,IAAI,IAAuB;AAC3D;;;;AAKA,SAAS,qBAAqB,SAA+C;CAC3E,MAAM,mBAAmB,IAAI,IAAI,QAAQ,QAAQ,WAAW,kBAAkB,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,IAAI,CAAC;CAC9G,IAAI,CAAC,iBAAiB,MAAM,OAAO;CAEnC,OAAO,QAAQ,QAAQ,WAAW;EAChC,MAAM,WAAW,aAAa,QAAQ,MAAM;EAC5C,IAAI,CAAC,UAAU,OAAO;EAEtB,MAAM,YAAY,SAAS;EAC3B,IAAI,CAAC,WAAW,OAAO;EAGvB,KADuB,SAAS,iBAAiB,UAAU,SAAS,YAAY,UAAU,MACpE,GAAG,OAAO;EAEhC,IAAI,iBAAiB,IAAI,SAAS,GAAG,OAAO;EAC5C,KAAK,cAAc,aAAa,cAAc,cAAc,iBAAiB,IAAI,SAAS,KAAK,iBAAiB,IAAI,QAAQ,IAAI,OAAO;EAEvI,OAAO;CACT,CAAC;AACH;;;;;;;;;;AAWA,MAAa,qBAAqB,YAAY;CAC5C,MAAM;CACN,OAAO,MAAM;EACX,MAAM,YAAY,aAAa,MAAM,OAAO;EAC5C,IAAI,CAAC,WAAW,SAAS,QAAQ,OAAO,KAAA;EAExC,MAAM,aAAa,qBAAqB,UAAU,OAAO;EACzD,IAAI,WAAW,WAAW,UAAU,QAAQ,QAAQ,OAAO,KAAA;EAE3D,OAAO;GAAE,GAAG;GAAW,SAAS;EAAW;CAC7C;AACF,CAAC"}

@@ -1,6 +0,5 @@

import { A as FunctionParametersNode, At as InferSchemaNode, Bt as TypeNode, C as ParameterLocation, Ct as UrlSchemaNode, D as FunctionParamKind, Et as PropertyNode, Ft as ConstNode, G as ExportNode, It as FunctionNode, J as SourceNode, K as FileNode, Lt as JSDocNode, M as ObjectBindingPatternNode, Mt as ArrowFunctionNode, N as TypeExpression, Nt as BreakNode, O as FunctionParamNode, P as TypeLiteralNode, Pt as CodeNode, Rt as JsxNode, St as UnionSchemaNode, _t as SchemaNode, a as InputMeta, bt as StringSchemaNode, ct as DatetimeSchemaNode, d as HttpOperationNode, dt as NumberSchemaNode, f as OperationNode, ft as ObjectSchemaNode, gt as ScalarSchemaType, h as ResponseNode, ht as ScalarSchemaNode, j as IndexedAccessTypeNode, jt as ParserOptions, k as FunctionParameterNode, l as GenericOperationNode, lt as EnumSchemaNode, mt as RefSchemaNode, n as OutputNode, nn as NodeDef, nt as ContentNode, o as InputNode, on as NodeKind, ot as ArraySchemaNode, pt as PrimitiveSchemaType, q as ImportNode, st as DateSchemaNode, t as Node, tn as DistributiveOmit, u as HttpMethod, ut as IntersectionSchemaNode, v as StatusCode, vt as SchemaNodeByType, w as ParameterNode, xt as TimeSchemaNode, y as RequestBodyNode, yt as SchemaType, zt as TextNode } from "./index-BzjwdK2M.js";
import { UserFileNode } from "./factory.js";
import { a as ParentOf, o as Visitor, s as VisitorContext, t as Macro } from "./defineMacro-BLIR6k-j.js";
import { c as DedupeCanonical, l as DedupeLookups, n as PrinterFactoryOptions, o as SchemaDialect, r as PrinterPartial, t as Printer, u as DedupePlan } from "./types-DyDzizSf.js";
import { n as OperationParamsResolver } from "./operationParams-BZ07xDm0.js";
export type { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateSchemaNode, DatetimeSchemaNode, DedupeCanonical, DedupeLookups, DedupePlan, DistributiveOmit, EnumSchemaNode, ExportNode, FileNode, FunctionNode, FunctionParamKind, FunctionParamNode, FunctionParameterNode, FunctionParametersNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, IndexedAccessTypeNode, InferSchemaNode, InputMeta, InputNode, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeDef, NodeKind, NumberSchemaNode, ObjectBindingPatternNode, ObjectSchemaNode, OperationNode, OperationParamsResolver, OutputNode, ParameterLocation, ParameterNode, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaDialect, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeExpression, TypeLiteralNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext };
import { A as IndexedAccessTypeNode, At as InferSchemaNode, Bt as TypeNode, C as ParameterNode, Ct as UrlSchemaNode, D as FunctionParamNode, E as FunctionParamKind, Et as PropertyNode, Ft as ConstNode, G as FileNode, It as FunctionNode, J as UserFileNode, K as ImportNode, Lt as JSDocNode, M as TypeExpression, Mt as ArrowFunctionNode, N as TypeLiteralNode, Nt as BreakNode, O as FunctionParameterNode, Pt as CodeNode, Rt as JsxNode, S as ParameterLocation, St as UnionSchemaNode, W as ExportNode, _t as SchemaNode, a as InputMeta, an as NodeKind, bt as StringSchemaNode, ct as DatetimeSchemaNode, d as HttpOperationNode, dt as NumberSchemaNode, f as OperationNode, ft as ObjectSchemaNode, g as StatusCode, gt as ScalarSchemaType, h as ResponseNode, ht as ScalarSchemaNode, j as ObjectBindingPatternNode, jt as ParserOptions, k as FunctionParametersNode, l as GenericOperationNode, lt as EnumSchemaNode, mt as RefSchemaNode, n as OutputNode, nn as NodeDef, o as InputNode, on as Dialect, ot as ArraySchemaNode, pt as PrimitiveSchemaType, q as SourceNode, rt as ContentNode, sn as SchemaDialect, st as DateSchemaNode, t as Node, tn as DistributiveOmit, u as HttpMethod, ut as IntersectionSchemaNode, vt as SchemaNodeByType, xt as TimeSchemaNode, y as RequestBodyNode, yt as SchemaType, zt as TextNode } from "./index-CTaN_ee_.js";
import { c as VisitorContext, n as Macro, o as ParentOf, s as Visitor, t as Enforce } from "./defineMacro-Vw-xQvq7.js";
import { c as DedupePlan, n as PrinterFactoryOptions, o as DedupeCanonical, r as PrinterPartial, s as DedupeLookups, t as Printer } from "./types-DQTgVSwE.js";
import { n as OperationParamsResolver } from "./operationParams-B44p6EQy.js";
export type { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateSchemaNode, DatetimeSchemaNode, DedupeCanonical, DedupeLookups, DedupePlan, Dialect, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, FunctionParamKind, FunctionParamNode, FunctionParameterNode, FunctionParametersNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, IndexedAccessTypeNode, InferSchemaNode, InputMeta, InputNode, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeDef, NodeKind, NumberSchemaNode, ObjectBindingPatternNode, ObjectSchemaNode, OperationNode, OperationParamsResolver, OutputNode, ParameterLocation, ParameterNode, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaDialect, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeExpression, TypeLiteralNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext };
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_visitor = require("./visitor-DpKZ9Tk0.cjs");
const require_utils = require("./utils-BLJwyza-.cjs");
const require_extractStringsFromNodes = require("./extractStringsFromNodes-DKgDjFO0.cjs");
const require_response = require("./response-BeG6hq2D.cjs");
const require_visitor = require("./visitor-B7Zlnp4c.cjs");
const require_utils = require("./utils-BDI3kXlb.cjs");
exports.buildGroupParam = require_utils.buildGroupParam;

@@ -17,3 +17,3 @@ exports.buildJSDoc = require_utils.buildJSDoc;

exports.extractRefName = require_visitor.extractRefName;
exports.extractStringsFromNodes = require_extractStringsFromNodes.extractStringsFromNodes;
exports.extractStringsFromNodes = require_response.extractStringsFromNodes;
exports.findCircularSchemas = require_utils.findCircularSchemas;

@@ -20,0 +20,0 @@ exports.getNestedAccessor = require_utils.getNestedAccessor;

import { n as __name } from "./chunk-CNktS9qV.js";
import { Et as PropertyNode, St as UnionSchemaNode, _t as SchemaNode, f as OperationNode, ft as ObjectSchemaNode, ot as ArraySchemaNode, ut as IntersectionSchemaNode, w as ParameterNode } from "./index-BzjwdK2M.js";
import { a as buildTypeLiteral, c as resolveParamType, i as buildGroupParam, n as OperationParamsResolver, o as caseParams, r as ParamGroupType, s as createOperationParams, t as BuildGroupArgs } from "./operationParams-BZ07xDm0.js";
import { t as extractStringsFromNodes } from "./extractStringsFromNodes-p4mX1TQD.js";
import { C as ParameterNode, Et as PropertyNode, Pt as CodeNode, St as UnionSchemaNode, _t as SchemaNode, f as OperationNode, ft as ObjectSchemaNode, ot as ArraySchemaNode, ut as IntersectionSchemaNode } from "./index-CTaN_ee_.js";
import { a as buildTypeLiteral, c as resolveParamType, i as buildGroupParam, n as OperationParamsResolver, o as caseParams, r as ParamGroupType, s as createOperationParams, t as BuildGroupArgs } from "./operationParams-B44p6EQy.js";

@@ -85,2 +84,11 @@ //#region ../../internals/utils/src/reserved.d.ts

//#endregion
//#region src/utils/extractStringsFromNodes.d.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string;
//#endregion
//#region src/utils/refs.d.ts

@@ -87,0 +95,0 @@ /**

@@ -1,4 +0,4 @@

import { c as extractRefName, d as resolveRefName, f as syncSchemaRef, l as isStringType, o as childName, s as enumPropName, u as resolveGroupType } from "./visitor-DJ6ZEJvq.js";
import { C as objectKey, S as buildObject, _ as toRegExpString, a as resolveParamType, b as buildJSDoc, c as mapSchemaMembers, d as containsCircularRef, f as findCircularSchemas, g as stringifyObject, h as stringify, i as createOperationParams, l as mapSchemaProperties, m as jsStringEscape, n as buildTypeLiteral, o as lazyGetter, p as getNestedAccessor, r as caseParams, s as mapSchemaItems, t as buildGroupParam, u as collectUsedSchemaNames, v as trimQuotes, w as isValidVarName, x as buildList, y as mergeAdjacentObjectsLazy } from "./utils-CF_-Pn_c.js";
import { t as extractStringsFromNodes } from "./extractStringsFromNodes-Cja-xxx5.js";
import { c as extractRefName, d as resolveRefName, f as syncSchemaRef, l as isStringType, o as childName, s as enumPropName, u as resolveGroupType } from "./visitor-DepQEKyp.js";
import { Y as extractStringsFromNodes } from "./response-Rd1uisM1.js";
import { C as objectKey, S as buildObject, _ as toRegExpString, a as resolveParamType, b as buildJSDoc, c as mapSchemaMembers, d as containsCircularRef, f as findCircularSchemas, g as stringifyObject, h as stringify, i as createOperationParams, l as mapSchemaProperties, m as jsStringEscape, n as buildTypeLiteral, o as lazyGetter, p as getNestedAccessor, r as caseParams, s as mapSchemaItems, t as buildGroupParam, u as collectUsedSchemaNames, v as trimQuotes, w as isValidVarName, x as buildList, y as mergeAdjacentObjectsLazy } from "./utils-DW9rnfsD.js";
export { buildGroupParam, buildJSDoc, buildList, buildObject, buildTypeLiteral, caseParams, childName, collectUsedSchemaNames, containsCircularRef, createOperationParams, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, getNestedAccessor, isStringType, isValidVarName, jsStringEscape, lazyGetter, mapSchemaItems, mapSchemaMembers, mapSchemaProperties, mergeAdjacentObjectsLazy, objectKey, resolveGroupType, resolveParamType, resolveRefName, stringify, stringifyObject, syncSchemaRef, toRegExpString, trimQuotes };
{
"name": "@kubb/ast",
"version": "5.0.0-beta.61",
"version": "5.0.0-beta.62",
"description": "Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -1,2 +0,1 @@

import type { HttpMethod } from './nodes/operation.ts'
import type { SchemaType } from './nodes/schema.ts'

@@ -130,32 +129,3 @@

export type ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'
/**
* Scalar primitive schema types used for union simplification and type narrowing.
*/
const SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])
/**
* Returns `true` when `type` is a scalar primitive that can be assigned without wrapping
* (for example `string | number | boolean`).
*/
export function isScalarPrimitive(type: string): type is ScalarPrimitive {
return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)
}
/**
* HTTP method identifiers used by operation nodes.
*/
export const httpMethods = {
get: 'GET',
post: 'POST',
put: 'PUT',
patch: 'PATCH',
delete: 'DELETE',
head: 'HEAD',
options: 'OPTIONS',
trace: 'TRACE',
} as const satisfies Record<Lowercase<HttpMethod>, HttpMethod>
/**
* Default concurrency limit for the `walk()` traversal utility.

@@ -162,0 +132,0 @@ *

@@ -1,9 +0,1 @@

/**
* Schema-shape deduplication. `buildDedupePlan` finds top-level and inline schemas that share a
* structural signature, picks one canonical definition, and `applyDedupe` repoints every duplicate
* at it. This works on `SchemaNode` content, not on files.
*
* For merging a file's imports, exports, and source nodes, see `utils/fileMerge.ts`. Same idea of
* collapsing duplicates, but a different domain.
*/
import type { Node, OperationNode, SchemaNode } from './nodes/index.ts'

@@ -188,4 +180,4 @@ import { createSchema } from './nodes/schema.ts'

function record(schemaNode: SchemaNode): void {
if (!isCandidate(schemaNode)) return
const signature = signatureOf(schemaNode)
if (!isCandidate(schemaNode)) return

@@ -192,0 +184,0 @@ const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name

import type { VisitorDepth } from './constants.ts'
import type { VisitorKey } from './defineNode.ts'
import { visitorKeys } from './defineNode.ts'
import type { Node } from './nodes/index.ts'

@@ -7,9 +9,7 @@ import type { Visitor, VisitorContext } from './visitor.ts'

/**
* Visitor callback names a macro can implement, one per traversable node kind.
* Mirrors the keys of {@link Visitor}.
* Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,
* and `undefined` keeps declaration order.
*/
const macroKeys = ['input', 'output', 'operation', 'schema', 'property', 'parameter', 'response'] as const
export type Enforce = 'pre' | 'post'
type MacroKey = (typeof macroKeys)[number]
/**

@@ -19,3 +19,3 @@ * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain

*/
function enforceWeight(enforce?: 'pre' | 'post'): number {
function enforceWeight(enforce?: Enforce): number {
if (enforce === 'pre') return 0

@@ -41,3 +41,3 @@ if (enforce === 'post') return 2

*/
enforce?: 'pre' | 'post'
enforce?: Enforce
/**

@@ -70,2 +70,9 @@ * Gate checked against the current node before any callback runs. When it returns `false`

type ChainProps = {
macros: ReadonlyArray<Macro>
key: VisitorKey
node: Node
context: VisitorContext
}
/**

@@ -76,3 +83,3 @@ * Runs every macro's callback for one node kind in order, chaining the result so each macro sees

*/
function chain(macros: ReadonlyArray<Macro>, key: MacroKey, node: Node, context: VisitorContext): Node | undefined {
function chain({ macros, key, node, context }: ChainProps): Node | undefined {
let current = node

@@ -108,7 +115,7 @@

const visitor: Visitor = {}
for (const key of macroKeys) {
for (const key of visitorKeys) {
if (!ordered.some((macro) => typeof macro[key] === 'function')) continue
const callback = (node: Node, context: VisitorContext) => chain(ordered, key, node, context)
;(visitor as Record<MacroKey, MacroCallback>)[key] = callback
const callback = (node: Node, context: VisitorContext) => chain({ macros: ordered, key, node, context })
;(visitor as Record<VisitorKey, MacroCallback>)[key] = callback
}

@@ -137,3 +144,3 @@

return transform(root, { ...composeMacros(macros), depth: options?.depth }) as TNode
return transform(root, { ...composeMacros(macros), ...options }) as TNode
}

@@ -0,1 +1,3 @@

import type { SchemaNode } from './nodes/index.ts'
/**

@@ -8,29 +10,47 @@ * The spec-specific questions a schema parser answers while turning a source document into Kubb

/**
* Identifies the dialect in logs and diagnostics.
*/
name: string
/**
* Whether the schema is nullable.
*/
isNullable: (schema?: TSchema) => boolean
isNullable(schema?: TSchema): boolean
/**
* Whether the value is a `$ref` pointer.
*/
isReference: (value?: unknown) => value is TRef
isReference(value?: unknown): value is TRef
/**
* Whether the schema carries a discriminator for polymorphism.
*/
isDiscriminator: (value?: unknown) => value is TDiscriminated
isDiscriminator(value?: unknown): value is TDiscriminated
/**
* Whether the schema is binary data, converted to a `blob` node.
*/
isBinary: (schema: TSchema) => boolean
isBinary(schema: TSchema): boolean
/**
* Resolves a local `$ref` against the document, or nullish when it cannot.
*/
resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined
resolveRef<TResolved>(document: TDocument, ref: string): TResolved | null | undefined
/**
* Derives a schema's `optional`/`nullish` flags from a parent's `required` value and the
* schema's own `nullable`. How "required" and "nullable" combine is spec-specific, so the
* dialect owns it. Method syntax keeps a concrete dialect assignable to the base
* `SchemaDialect` (bivariant parameters).
*/
optionality(schema: SchemaNode, required: boolean): SchemaNode
}
/**
* Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the
* A spec adapter's dialect. `name` identifies it in logs and diagnostics; `schema` holds the
* spec-specific schema questions the parser answers.
*/
export type Dialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {
/**
* Identifies the dialect in logs and diagnostics.
*/
name: string
/**
* The spec-specific schema behavior. See {@link SchemaDialect}.
*/
schema: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>
}
/**
* Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the
* dialect's type for inference.

@@ -40,16 +60,19 @@ *

* ```ts
* export const oasDialect = defineSchemaDialect({
* export const oasDialect = defineDialect({
* name: 'oas',
* isNullable,
* isReference,
* isDiscriminator,
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
* resolveRef,
* schema: {
* isNullable,
* isReference,
* isDiscriminator,
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
* resolveRef,
* optionality,
* },
* })
* ```
*/
export function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(
dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>,
): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument> {
export function defineDialect<TSchema, TRef, TDiscriminated, TDocument>(
dialect: Dialect<TSchema, TRef, TDiscriminated, TDocument>,
): Dialect<TSchema, TRef, TDiscriminated, TDocument> {
return dialect
}

@@ -1,7 +0,2 @@

import { hash } from 'node:crypto'
import path from 'node:path'
import { trimExtName } from '@internals/utils'
import type { FileNode, Node } from './nodes/index.ts'
import { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'
import { combineExports, combineImports, combineSources } from './utils/fileMerge.ts'
import type { Node } from './nodes/index.ts'

@@ -13,3 +8,4 @@ // Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes

export { createContent } from './nodes/content.ts'
export { createExport, createImport, createSource } from './nodes/file.ts'
export { createExport, createFile, createImport, createSource } from './nodes/file.ts'
export type { UserFileNode } from './nodes/file.ts'
export { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts'

@@ -50,84 +46,1 @@ export { createInput } from './nodes/input.ts'

}
/**
* Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed
* and `imports`/`exports`/`sources` are deduplicated.
*/
export type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &
Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
export function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {
const rawExtname = path.extname(input.baseName)
// Handle dotfile basename like '.ts' where path.extname returns ''
const extname = (rawExtname || (input.baseName.startsWith('.') ? input.baseName : '')) as `.${string}`
if (!extname) {
throw new Error(`No extname found for ${input.baseName}`)
}
const source = (input.sources ?? [])
.flatMap((item) => item.nodes ?? [])
.map((node) => extractStringsFromNodes([node]))
.filter(Boolean)
.join('\n\n')
const resolvedExports = input.exports?.length ? combineExports(input.exports) : []
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || undefined) : []
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name): name is string => Boolean(name)))
const nameOf = (item: string | { propertyName: string; name?: string }): string => (typeof item === 'string' ? item : (item.name ?? item.propertyName))
// Drop self-imports. Consolidating output (`mode: 'file'`) can place a symbol's
// definition and a cross-file import of it in the same file. The first pass catches imports that
// resolve to this file's own path. The second drops imports of names the file already defines,
// the case consolidation produces when the import path no longer matches `input.path`. Sources
// stay intact, so the local definition remains. Bare specifiers like `'zod'` never match a path.
const resolvedImports = combinedImports
.filter((imp) => imp.path !== input.path)
.flatMap((imp) => {
if (!Array.isArray(imp.name)) {
return typeof imp.name === 'string' && localNames.has(imp.name) ? [] : [imp]
}
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)))
if (!kept.length) return []
return [kept.length === imp.name.length ? imp : { ...imp, name: kept }]
})
const resolvedSources = input.sources?.length ? combineSources(input.sources) : []
return {
kind: 'File',
...input,
id: hash('sha256', input.path, 'hex'),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? ({} as TMeta),
}
}

@@ -1,15 +0,13 @@

export { httpMethods, schemaTypes } from './constants.ts'
export { schemaTypes } from './constants.ts'
export { applyDedupe, buildDedupePlan } from './dedupe.ts'
export { defineSchemaDialect } from './dialect.ts'
export * as factory from './factory.ts'
export { defineDialect } from './dialect.ts'
export { isHttpOperationNode, narrowSchema } from './guards.ts'
export { applyMacros, composeMacros, defineMacro } from './defineMacro.ts'
export { defineNode } from './node.ts'
export { syncOptionality } from './node.ts'
export * from './registry.ts'
export { createPrinterFactory, definePrinter } from './printer.ts'
export { defineNode } from './defineNode.ts'
export { createPrinterFactory, definePrinter } from './definePrinter.ts'
export { signatureOf } from './signature.ts'
export { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'
export { collect, transform, walk } from './visitor.ts'
export * as factory from './factory.ts'
export * from './registry.ts'
export type * from './types.ts'

@@ -6,2 +6,8 @@ import { defineMacro } from '../defineMacro.ts'

type Props = {
propertyName: string
values: Array<string>
enumName?: string
}
/**

@@ -17,3 +23,3 @@ * Builds a macro that replaces a discriminator property's schema with a string enum of the given

*/
export function macroDiscriminatorEnum({ propertyName, values, enumName }: { propertyName: string; values: Array<string>; enumName?: string }) {
export function macroDiscriminatorEnum({ propertyName, values, enumName }: Props) {
return defineMacro({

@@ -20,0 +26,0 @@ name: 'discriminator-enum',

@@ -5,2 +5,8 @@ import { defineMacro } from '../defineMacro.ts'

type Props = {
parentName: string | null | undefined
propName: string
enumSuffix: string
}
/**

@@ -16,3 +22,3 @@ * Builds a macro that names an inline enum schema from its parent and property name. Boolean enums

*/
export function macroEnumName({ parentName, propName, enumSuffix }: { parentName: string | null | undefined; propName: string; enumSuffix: string }) {
export function macroEnumName({ parentName, propName, enumSuffix }: Props) {
return defineMacro({

@@ -22,4 +28,6 @@ name: 'enum-name',

const enumNode = narrowSchema(node, 'enum')
if (enumNode?.primitive === 'boolean') return { ...node, name: null }
if (enumNode) return { ...node, name: enumPropName(parentName, propName, enumSuffix) }
return undefined

@@ -26,0 +34,0 @@ },

@@ -1,2 +0,1 @@

import { isScalarPrimitive } from '../constants.ts'
import { defineMacro } from '../defineMacro.ts'

@@ -6,24 +5,13 @@ import { narrowSchema } from '../guards.ts'

type ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'
/**
* Removes union members a broader scalar primitive already covers, such as a multi-value string enum
* sitting next to a plain `string`. Single-value enums are kept.
*
* @example
* ```ts
* const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })
* ```
* Scalar primitive schema types used for union simplification and type narrowing.
*/
export const macroSimplifyUnion = defineMacro({
name: 'simplify-union',
schema(node) {
const unionNode = narrowSchema(node, 'union')
if (!unionNode?.members?.length) return undefined
const SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])
const simplified = simplifyUnionMembers(unionNode.members)
if (simplified.length === unionNode.members.length) return undefined
function isScalarPrimitive(type: string): type is ScalarPrimitive {
return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)
}
return { ...unionNode, members: simplified }
},
})
/**

@@ -52,1 +40,23 @@ * Filters union members, dropping enum members that a broader scalar primitive already covers.

}
/**
* Removes union members a broader scalar primitive already covers, such as a multi-value string enum
* sitting next to a plain `string`. Single-value enums are kept.
*
* @example
* ```ts
* const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })
* ```
*/
export const macroSimplifyUnion = defineMacro({
name: 'simplify-union',
schema(node) {
const unionNode = narrowSchema(node, 'union')
if (!unionNode?.members?.length) return undefined
const simplified = simplifyUnionMembers(unionNode.members)
if (simplified.length === unionNode.members.length) return undefined
return { ...unionNode, members: simplified }
},
})

@@ -1,2 +0,2 @@

import { defineNode } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'

@@ -167,51 +167,8 @@

*/
export type ArrowFunctionNode = BaseNode & {
export type ArrowFunctionNode = Omit<FunctionNode, 'kind'> & {
kind: 'ArrowFunction'
/**
* Name of the arrow function (used as the `const` variable name).
*/
name: string
/**
* Whether the function is a default export.
*/
default?: boolean | null
/**
* Function parameter list rendered as a string (e.g. from `FunctionParams.toConstructor()`).
*/
params?: string | null
/**
* Whether the arrow function should be exported.
*/
export?: boolean | null
/**
* Whether the arrow function is async. When `true`, the return type is wrapped in `Promise<>`.
*/
async?: boolean | null
/**
* TypeScript generic type parameters.
*
* @example Constrained generics
* `['T', 'U extends string']`
*/
generics?: string | Array<string> | null
/**
* Return type annotation.
*
* @example Type reference
* `'Pet'`
*/
returnType?: string | null
/**
* JSDoc documentation metadata.
*/
JSDoc?: JSDocNode | null
/**
* Render the arrow function body as a single-line expression.
*/
singleLine?: boolean | null
/**
* Child nodes representing the function body (children of the `Function.Arrow` component).
* Each entry is a {@link CodeNode}. Use {@link TextNode} for raw string content.
*/
nodes?: Array<CodeNode>
}

@@ -290,2 +247,32 @@

/**
* Definition for the {@link TypeNode}.
*/
export const typeDef = defineNode<TypeNode>({ kind: 'Type' })
/**
* Definition for the {@link FunctionNode}.
*/
export const functionDef = defineNode<FunctionNode>({ kind: 'Function' })
/**
* Definition for the {@link ArrowFunctionNode}.
*/
export const arrowFunctionDef = defineNode<ArrowFunctionNode>({ kind: 'ArrowFunction' })
/**
* Definition for the {@link TextNode}.
*/
export const textDef = defineNode<TextNode, string>({ kind: 'Text', build: (value) => ({ value }) })
/**
* Definition for the {@link BreakNode}.
*/
export const breakDef = defineNode<BreakNode, void>({ kind: 'Break', build: () => ({}) })
/**
* Definition for the {@link JsxNode}.
*/
export const jsxDef = defineNode<JsxNode, string>({ kind: 'Jsx', build: (value) => ({ value }) })
/**
* Creates a `ConstNode` representing a TypeScript `const` declaration.

@@ -302,7 +289,2 @@ *

/**
* Definition for the {@link TypeNode}.
*/
export const typeDef = defineNode<TypeNode>({ kind: 'Type' })
/**
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.

@@ -319,7 +301,2 @@ *

/**
* Definition for the {@link FunctionNode}.
*/
export const functionDef = defineNode<FunctionNode>({ kind: 'Function' })
/**
* Creates a `FunctionNode` representing a TypeScript `function` declaration.

@@ -336,7 +313,2 @@ *

/**
* Definition for the {@link ArrowFunctionNode}.
*/
export const arrowFunctionDef = defineNode<ArrowFunctionNode>({ kind: 'ArrowFunction' })
/**
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.

@@ -353,7 +325,2 @@ *

/**
* Definition for the {@link TextNode}.
*/
export const textDef = defineNode<TextNode, string>({ kind: 'Text', build: (value) => ({ value }) })
/**
* Creates a {@link TextNode} representing a raw string fragment in the source output.

@@ -370,7 +337,2 @@ *

/**
* Definition for the {@link BreakNode}.
*/
export const breakDef = defineNode<BreakNode, void>({ kind: 'Break', build: () => ({}) })
/**
* Creates a {@link BreakNode} representing a line break in the source output.

@@ -389,7 +351,2 @@ *

/**
* Definition for the {@link JsxNode}.
*/
export const jsxDef = defineNode<JsxNode, string>({ kind: 'Jsx', build: (value) => ({ value }) })
/**
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.

@@ -396,0 +353,0 @@ *

@@ -1,2 +0,2 @@

import { defineNode } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'

@@ -41,10 +41,5 @@ import type { SchemaNode } from './schema.ts'

/**
* Loosely-typed content entry accepted by the builders, normalized into a {@link ContentNode}.
*/
export type UserContent = Omit<ContentNode, 'kind'>
/**
* Definition for the {@link ContentNode}.
*/
export const contentDef = defineNode<ContentNode, UserContent>({
export const contentDef = defineNode<ContentNode>({
kind: 'Content',

@@ -51,0 +46,0 @@ children: ['schema'],

@@ -1,2 +0,7 @@

import { defineNode } from '../node.ts'
import { hash } from 'node:crypto'
import path from 'node:path'
import { trimExtName } from '@internals/utils'
import { defineNode } from '../defineNode.ts'
import { extractStringsFromNodes } from '../utils/extractStringsFromNodes.ts'
import { combineExports, combineImports, combineSources } from '../utils/fileMerge.ts'
import type { BaseNode } from './base.ts'

@@ -246,2 +251,18 @@ import type { CodeNode } from './code.ts'

/**
* Definition for the {@link ExportNode}.
*/
export const exportDef = defineNode<ExportNode>({ kind: 'Export' })
/**
* Definition for the {@link SourceNode}.
*/
export const sourceDef = defineNode<SourceNode>({ kind: 'Source' })
/**
* Definition for the {@link FileNode}. The fully resolved builder lives in
* `createFile`, so this definition only supplies the guard.
*/
export const fileDef = defineNode<FileNode>({ kind: 'File' })
/**
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.

@@ -258,7 +279,2 @@ *

/**
* Definition for the {@link ExportNode}.
*/
export const exportDef = defineNode<ExportNode>({ kind: 'Export' })
/**
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.

@@ -275,7 +291,2 @@ *

/**
* Definition for the {@link SourceNode}.
*/
export const sourceDef = defineNode<SourceNode>({ kind: 'Source' })
/**
* Creates a `SourceNode` representing a fragment of source code within a file.

@@ -291,5 +302,82 @@ *

/**
* Definition for the {@link FileNode}. The fully resolved builder lives in
* `createFile`, so this definition only supplies the guard.
* Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed
* and `imports`/`exports`/`sources` are deduplicated.
*/
export const fileDef = defineNode<FileNode>({ kind: 'File' })
export type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &
Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
export function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {
const rawExtname = path.extname(input.baseName)
// Handle dotfile basename like '.ts' where path.extname returns ''
const extname = (rawExtname || (input.baseName.startsWith('.') ? input.baseName : '')) as `.${string}`
if (!extname) {
throw new Error(`No extname found for ${input.baseName}`)
}
const source = (input.sources ?? [])
.flatMap((item) => item.nodes ?? [])
.map((node) => extractStringsFromNodes([node]))
.filter(Boolean)
.join('\n\n')
const resolvedExports = input.exports?.length ? combineExports(input.exports) : []
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || undefined) : []
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name): name is string => Boolean(name)))
const nameOf = (item: string | { propertyName: string; name?: string }): string => (typeof item === 'string' ? item : (item.name ?? item.propertyName))
// Drop self-imports. Consolidating output (`mode: 'file'`) can place a symbol's
// definition and a cross-file import of it in the same file. The first pass catches imports that
// resolve to this file's own path. The second drops imports of names the file already defines,
// the case consolidation produces when the import path no longer matches `input.path`. Sources
// stay intact, so the local definition remains. Bare specifiers like `'zod'` never match a path.
const resolvedImports = combinedImports
.filter((imp) => imp.path !== input.path)
.flatMap((imp) => {
if (!Array.isArray(imp.name)) {
return typeof imp.name === 'string' && localNames.has(imp.name) ? [] : [imp]
}
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)))
if (!kept.length) return []
return [kept.length === imp.name.length ? imp : { ...imp, name: kept }]
})
const resolvedSources = input.sources?.length ? combineSources(input.sources) : []
return {
kind: 'File',
...input,
id: hash('sha256', input.path, 'hex'),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? ({} as TMeta),
}
}

@@ -1,2 +0,2 @@

import { defineNode } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'

@@ -46,7 +46,7 @@

* AST node for a single field accessed from a named group type.
* TypeScript renders as `objectType['indexType']`.
* TypeScript renders as `target['key']`.
*
* @example
* ```ts
* createIndexedAccessType({ objectType: 'GetPetPathParams', indexType: 'petId' })
* createIndexedAccessType({ target: 'GetPetPathParams', key: 'petId' })
* // GetPetPathParams['petId']

@@ -60,7 +60,7 @@ * ```

*/
objectType: string
target: string
/**
* Field key to access, e.g. `'petId'`.
*/
indexType: string
key: string
}

@@ -174,13 +174,2 @@

/**
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
*
* @example
* ```ts
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
* // { petId: string }
* ```
*/
export const createTypeLiteral = typeLiteralDef.create
/**
* Definition for the {@link IndexedAccessTypeNode}.

@@ -191,13 +180,2 @@ */

/**
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
*
* @example
* ```ts
* createIndexedAccessType({ objectType: 'DeletePetPathParams', indexType: 'petId' })
* // DeletePetPathParams['petId']
* ```
*/
export const createIndexedAccessType = indexedAccessTypeDef.create
/**
* Definition for the {@link ObjectBindingPatternNode}.

@@ -208,13 +186,2 @@ */

/**
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
*
* @example
* ```ts
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
* // { id, name }
* ```
*/
export const createObjectBindingPattern = objectBindingPatternDef.create
/**
* Plain property descriptor for a destructured group built by {@link createFunctionParameter}.

@@ -253,2 +220,43 @@ */

/**
* Definition for the {@link FunctionParametersNode}.
*/
export const functionParametersDef = defineNode<FunctionParametersNode, Partial<Omit<FunctionParametersNode, 'kind'>>>({
kind: 'FunctionParameters',
defaults: { params: [] },
})
/**
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
*
* @example
* ```ts
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
* // { petId: string }
* ```
*/
export const createTypeLiteral = typeLiteralDef.create
/**
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
*
* @example
* ```ts
* createIndexedAccessType({ target: 'DeletePetPathParams', key: 'petId' })
* // DeletePetPathParams['petId']
* ```
*/
export const createIndexedAccessType = indexedAccessTypeDef.create
/**
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
*
* @example
* ```ts
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
* // { id, name }
* ```
*/
export const createObjectBindingPattern = objectBindingPatternDef.create
/**
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.

@@ -271,10 +279,2 @@ *

/**
* Definition for the {@link FunctionParametersNode}.
*/
export const functionParametersDef = defineNode<FunctionParametersNode, Partial<Omit<FunctionParametersNode, 'kind'>>>({
kind: 'FunctionParameters',
defaults: { params: [] },
})
/**
* Creates a `FunctionParametersNode` from an ordered list of parameters.

@@ -281,0 +281,0 @@ *

@@ -17,3 +17,3 @@ import type { ArrowFunctionNode, ConstNode, FunctionNode, TypeNode } from './code.ts'

export type { ContentNode } from './content.ts'
export type { ExportNode, FileNode, ImportNode, SourceNode } from './file.ts'
export type { ExportNode, FileNode, ImportNode, SourceNode, UserFileNode } from './file.ts'
export type {

@@ -29,3 +29,2 @@ FunctionParamKind,

} from './function.ts'
export type { StatusCode } from './http.ts'
export type { InputMeta, InputNode } from './input.ts'

@@ -37,3 +36,3 @@ export type { GenericOperationNode, HttpMethod, HttpOperationNode, OperationNode } from './operation.ts'

export type { RequestBodyNode } from './requestBody.ts'
export type { ResponseNode } from './response.ts'
export type { ResponseNode, StatusCode } from './response.ts'
export type {

@@ -40,0 +39,0 @@ ArraySchemaNode,

import type { Streamable } from '@internals/utils'
import { defineNode } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'

@@ -71,4 +71,3 @@ import type { OperationNode } from './operation.ts'

* `Stream` switches `schemas` and `operations` between eager `Array`s (the default) and lazy
* `AsyncIterable`s. The streaming variant `InputNode<true>` yields nodes one at a time and makes
* `meta` optional, since the adapter can emit metadata before the first node is parsed.
* `AsyncIterable`s. The streaming variant `InputNode<true>` yields nodes one at a time.
*

@@ -105,3 +104,7 @@ * @example

operations: Streamable<OperationNode, Stream>
} & (Stream extends true ? { meta?: InputMeta } : { meta: InputMeta })
/**
* Document metadata populated by the adapter.
*/
meta: InputMeta
}

@@ -120,4 +123,4 @@ /**

* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
* `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the
* eager variant with array `schemas`/`operations` and the defaulted `meta`.
* `operations` are `AsyncIterable` sources. Otherwise it builds the eager variant with array
* `schemas`/`operations`. Both variants get the defaulted `meta`.
*

@@ -137,8 +140,9 @@ * @example Eager

const { stream, ...overrides } = options
// Streaming inputs carry AsyncIterable sources, so skip the array/meta defaults that
// inputDef.create applies for the eager variant.
// Streaming inputs carry AsyncIterable sources, so skip the array defaults that
// inputDef.create applies for the eager variant. Keep the meta default.
if (stream) {
return { kind: 'Input', ...overrides } as InputNode<Stream>
return { kind: 'Input', meta: { circularNames: [], enumNames: [] }, ...overrides } as InputNode<Stream>
}
return inputDef.create(overrides as Partial<Omit<InputNode, 'kind'>>) as InputNode<Stream>
}

@@ -1,5 +0,5 @@

import { defineNode } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'
import type { ParameterNode } from './parameter.ts'
import { createRequestBody, type RequestBodyNode, type UserRequestBody } from './requestBody.ts'
import { createRequestBody, type RequestBodyNode } from './requestBody.ts'
import type { ResponseNode } from './response.ts'

@@ -114,3 +114,3 @@

path?: HttpOperationNode['path']
requestBody?: UserRequestBody
requestBody?: Omit<RequestBodyNode, 'kind'>
[key: string]: unknown

@@ -155,3 +155,3 @@ }

Partial<Omit<HttpOperationNode, 'kind' | 'operationId' | 'method' | 'path' | 'requestBody'>> & {
requestBody?: UserRequestBody
requestBody?: Omit<RequestBodyNode, 'kind'>
},

@@ -162,3 +162,3 @@ ): HttpOperationNode

Partial<Omit<GenericOperationNode, 'kind' | 'operationId' | 'requestBody'>> & {
requestBody?: UserRequestBody
requestBody?: Omit<RequestBodyNode, 'kind'>
},

@@ -165,0 +165,0 @@ ): GenericOperationNode

@@ -1,2 +0,2 @@

import { defineNode } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'

@@ -3,0 +3,0 @@ import type { FileNode } from './file.ts'

@@ -1,2 +0,2 @@

import { defineNode, syncOptionality } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'

@@ -44,14 +44,14 @@ import type { SchemaNode } from './schema.ts'

/**
* Definition for the {@link ParameterNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
* Definition for the {@link ParameterNode}. `required` defaults to `false`. When a `dialect` is
* passed to `create`, the schema's `optional`/`nullish` flags are derived through its
* `optionality`; without one, the schema is left as-is.
*/
export const parameterDef = defineNode<ParameterNode, UserParameterNode>({
kind: 'Parameter',
build: (props) => {
build: (props, dialect) => {
const required = props.required ?? false
return { ...props, required, schema: syncOptionality(props.schema, required) }
return { ...props, required, schema: dialect ? dialect.schema.optionality(props.schema, required) : props.schema }
},
children: ['schema'],
visitorKey: 'parameter',
rebuild: true,
})

@@ -58,0 +58,0 @@

@@ -1,2 +0,2 @@

import { defineNode, syncOptionality } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'

@@ -40,14 +40,14 @@ import type { SchemaNode } from './schema.ts'

/**
* Definition for the {@link PropertyNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
* Definition for the {@link PropertyNode}. `required` defaults to `false`. When a `dialect` is
* passed to `create`, the schema's `optional`/`nullish` flags are derived through its
* `optionality`; without one, the schema is left as-is.
*/
export const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
kind: 'Property',
build: (props) => {
build: (props, dialect) => {
const required = props.required ?? false
return { ...props, required, schema: syncOptionality(props.schema, required) }
return { ...props, required, schema: dialect ? dialect.schema.optionality(props.schema, required) : props.schema }
},
children: ['schema'],
visitorKey: 'property',
rebuild: true,
})

@@ -54,0 +54,0 @@

@@ -1,4 +0,4 @@

import { defineNode } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'
import { type ContentNode, createContent, type UserContent } from './content.ts'
import type { ContentNode } from './content.ts'

@@ -43,14 +43,7 @@ /**

/**
* Loosely-typed request body accepted by `createOperation`, normalized into a {@link RequestBodyNode}.
* Definition for the {@link RequestBodyNode}. Content entries are built upfront with
* {@link createContent}, mirroring how `parameters` and `responses` take prebuilt nodes.
*/
export type UserRequestBody = Omit<RequestBodyNode, 'kind' | 'content'> & {
content?: Array<UserContent>
}
/**
* Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`.
*/
export const requestBodyDef = defineNode<RequestBodyNode, UserRequestBody>({
export const requestBodyDef = defineNode<RequestBodyNode>({
kind: 'RequestBody',
build: (props) => ({ ...props, content: props.content?.map(createContent) }),
children: ['content'],

@@ -60,4 +53,4 @@ })

/**
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
* Creates a `RequestBodyNode`.
*/
export const createRequestBody = requestBodyDef.create

@@ -1,8 +0,93 @@

import { defineNode } from '../node.ts'
import { defineNode } from '../defineNode.ts'
import type { BaseNode } from './base.ts'
import { type ContentNode, createContent, type UserContent } from './content.ts'
import type { StatusCode } from './http.ts'
import { type ContentNode, createContent } from './content.ts'
import type { SchemaNode } from './schema.ts'
/**
* All supported HTTP status code literals as strings, as used in API specs
* (for example, `"200"` and `"404"`).
*/
type HttpStatusCode =
// 1xx Informational
| '100'
| '101'
| '102'
| '103'
// 2xx Success
| '200'
| '201'
| '202'
| '203'
| '204'
| '205'
| '206'
| '207'
| '208'
| '226'
// 3xx Redirection
| '300'
| '301'
| '302'
| '303'
| '304'
| '305'
| '307'
| '308'
// 4xx Client Error
| '400'
| '401'
| '402'
| '403'
| '404'
| '405'
| '406'
| '407'
| '408'
| '409'
| '410'
| '411'
| '412'
| '413'
| '414'
| '415'
| '416'
| '417'
| '418'
| '421'
| '422'
| '423'
| '424'
| '425'
| '426'
| '428'
| '429'
| '431'
| '451'
// 5xx Server Error
| '500'
| '501'
| '502'
| '503'
| '504'
| '505'
| '506'
| '507'
| '508'
| '510'
| '511'
/**
* Response status code literal used by operations.
*
* Includes specific HTTP status code strings and `"default"` for catch-all responses.
*
* @example
* ```ts
* const status: StatusCode = '200'
* const fallback: StatusCode = 'default'
* ```
*/
export type StatusCode = HttpStatusCode | 'default'
/**
* AST node representing one operation response variant.

@@ -19,3 +104,3 @@ *

* statusCode: '200',
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'string' }) }],
* content: [{ kind: 'Content', contentType: 'application/json', schema: createSchema({ type: 'string' }) }],
* }

@@ -57,3 +142,3 @@ * ```

Partial<Omit<ResponseNode, 'kind' | 'statusCode' | 'content'>> & {
content?: Array<UserContent>
content?: Array<ContentNode>
schema?: SchemaNode

@@ -72,4 +157,4 @@ mediaType?: string | null

const { schema, mediaType, keysToOmit, content, ...rest } = props
const entries = content ?? (schema ? [{ contentType: mediaType ?? 'application/json', schema, keysToOmit: keysToOmit ?? null }] : undefined)
return { ...rest, content: entries?.map(createContent) }
const entries = content ?? (schema ? [createContent({ contentType: mediaType ?? 'application/json', schema, keysToOmit: keysToOmit ?? null })] : undefined)
return { ...rest, content: entries }
},

@@ -87,3 +172,3 @@ children: ['content'],

* statusCode: '200',
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }],
* content: [createContent({ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) })],
* })

@@ -90,0 +175,0 @@ * ```

import type { InferSchemaNode } from '../infer.ts'
import { defineNode, type DistributiveOmit } from '../node.ts'
import { defineNode, type DistributiveOmit } from '../defineNode.ts'
import type { BaseNode } from './base.ts'

@@ -4,0 +4,0 @@ import type { PropertyNode } from './property.ts'

@@ -1,2 +0,2 @@

import type { NodeDef } from './node.ts'
import type { NodeDef } from './defineNode.ts'
import { arrowFunctionDef, breakDef, constDef, functionDef, jsxDef, textDef, typeDef } from './nodes/code.ts'

@@ -3,0 +3,0 @@ import { contentDef } from './nodes/content.ts'

export type { DedupeCanonical, DedupeLookups, DedupePlan } from './dedupe.ts'
export type { SchemaDialect } from './dialect.ts'
export type { DistributiveOmit, NodeDef } from './node.ts'
export type { Dialect, SchemaDialect } from './dialect.ts'
export type { DistributiveOmit, NodeDef } from './defineNode.ts'
export type { InferSchemaNode, ParserOptions } from './infer.ts'
export type * from './nodes/index.ts'
export type { ParentOf, Visitor, VisitorContext } from './visitor.ts'
export type { Printer, PrinterFactoryOptions, PrinterPartial } from './printer.ts'
export type { Printer, PrinterFactoryOptions, PrinterPartial } from './definePrinter.ts'
export type { OperationParamsResolver } from './utils/operationParams.ts'
export type { UserFileNode } from './factory.ts'
export type { Macro } from './defineMacro.ts'
export type { Enforce, Macro } from './defineMacro.ts'

@@ -11,2 +11,3 @@ import type { CodeNode } from '../nodes/code.ts'

if (!nodes?.length) return ''
return nodes

@@ -13,0 +14,0 @@ .map((node) => {

@@ -209,3 +209,3 @@ import { camelCase, isValidVarName, memoize } from '@internals/utils'

if (groupName && groupName !== individualName) {
return createIndexedAccessType({ objectType: groupName, indexType: param.name })
return createIndexedAccessType({ target: groupName, key: param.name })
}

@@ -212,0 +212,0 @@

import type { VisitorDepth } from './constants.ts'
import { visitorDepths, WALK_CONCURRENCY } from './constants.ts'
import type { NodeDef } from './node.ts'
import type { NodeDef } from './defineNode.ts'
import type {

@@ -36,10 +36,2 @@ ContentNode,

/**
* Per-kind builders rerun after children are rebuilt. Derived from each
* definition's `rebuild` flag.
*/
const nodeRebuilders = Object.fromEntries(
nodeDefs.flatMap((def) => (def.rebuild ? [[def.kind, def.create as unknown as (node: Node) => Node] as const] : [])),
) as Partial<Record<NodeKind, (node: Node) => Node>>
/**
* Creates a small async concurrency limiter.

@@ -447,6 +439,3 @@ *

// changed" by identity and ancestors can avoid reallocating.
if (rebuilt === node) return node
const rebuild = nodeRebuilders[rebuilt.kind]
return rebuild ? rebuild(rebuilt) : rebuilt
return rebuilt
}

@@ -453,0 +442,0 @@

import { n as __name } from "./chunk-CNktS9qV.js";
import { Et as PropertyNode, _t as SchemaNode, f as OperationNode, h as ResponseNode, n as OutputNode, nt as ContentNode, o as InputNode, t as Node, w as ParameterNode, y as RequestBodyNode } from "./index-BzjwdK2M.js";
//#region src/constants.d.ts
/**
* Traversal depth for AST visitor utilities.
*
* - `'shallow'` visits only the immediate node, skipping children.
* - `'deep'` recursively visits all descendant nodes.
*/
type VisitorDepth = 'shallow' | 'deep';
/**
* Schema type discriminators used by all AST schema nodes.
*
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/
declare const schemaTypes: {
/**
* Text value.
*/
readonly string: "string";
/**
* Floating-point number (`float`, `double`).
*/
readonly number: "number";
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
readonly integer: "integer";
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
readonly bigint: "bigint";
/**
* Boolean value.
*/
readonly boolean: "boolean";
/**
* Explicit null value.
*/
readonly null: "null";
/**
* Any value (no type restriction).
*/
readonly any: "any";
/**
* Unknown value (must be narrowed before usage).
*/
readonly unknown: "unknown";
/**
* No return value (`void`).
*/
readonly void: "void";
/**
* Object with named properties.
*/
readonly object: "object";
/**
* Sequential list of items.
*/
readonly array: "array";
/**
* Fixed-length list with position-specific items.
*/
readonly tuple: "tuple";
/**
* "One of" multiple schema members.
*/
readonly union: "union";
/**
* "All of" multiple schema members.
*/
readonly intersection: "intersection";
/**
* Enum schema.
*/
readonly enum: "enum";
/**
* Reference to another schema.
*/
readonly ref: "ref";
/**
* Calendar date (for example `2026-03-24`).
*/
readonly date: "date";
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
readonly datetime: "datetime";
/**
* Time-only value (for example `09:00:00`).
*/
readonly time: "time";
/**
* UUID value.
*/
readonly uuid: "uuid";
/**
* Email address value.
*/
readonly email: "email";
/**
* URL value.
*/
readonly url: "url";
/**
* IPv4 address value.
*/
readonly ipv4: "ipv4";
/**
* IPv6 address value.
*/
readonly ipv6: "ipv6";
/**
* Binary/blob value.
*/
readonly blob: "blob";
/**
* Impossible value (`never`).
*/
readonly never: "never";
};
/**
* HTTP method identifiers used by operation nodes.
*/
declare const httpMethods: {
readonly get: "GET";
readonly post: "POST";
readonly put: "PUT";
readonly patch: "PATCH";
readonly delete: "DELETE";
readonly head: "HEAD";
readonly options: "OPTIONS";
readonly trace: "TRACE";
};
//#endregion
//#region src/visitor.d.ts
/**
* Ordered mapping of `[NodeType, ParentType]` pairs.
*
* `ParentOf` uses this map to find parent types.
*/
type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [OperationNode, InputNode], [RequestBodyNode, OperationNode], [ContentNode, RequestBodyNode | ResponseNode], [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
/**
* Resolves the parent node type for a given AST node type.
*
* Visitor context relies on this so `ctx.parent` is typed for each callback.
*
* @example
* ```ts
* type InputParent = ParentOf<InputNode>
* // undefined
* ```
*
* @example
* ```ts
* type PropertyParent = ParentOf<PropertyNode>
* // SchemaNode
* ```
*
* @example
* ```ts
* type SchemaParent = ParentOf<SchemaNode>
* // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
* ```
*/
type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [infer TEntry extends [Node, unknown], ...infer TRest extends ReadonlyArray<[Node, unknown]>] ? T extends TEntry[0] ? TEntry[1] : ParentOf<T, TRest> : Node;
/**
* Traversal context passed as the second argument to every visitor callback.
* `parent` is typed from the current node type.
*
* @example
* ```ts
* const visitor: Visitor = {
* schema(node, { parent }) {
* // parent type is narrowed by node kind
* },
* }
* ```
*/
type VisitorContext<T extends Node = Node> = {
/**
* Parent node of the currently visited node.
* For `InputNode`, this is `undefined`.
*/
parent?: ParentOf<T>;
};
/**
* Synchronous visitor consumed by `transform`. Each optional callback runs
* for the matching node type. Return a new node to replace it, or `undefined`
* to leave it untouched.
*
* Plugins typically expose `transformer` so users can supply a `Visitor` that
* rewrites the AST before printing.
*
* @example Prefix every operationId
* ```ts
* const visitor: Visitor = {
* operation(node) {
* return { ...node, operationId: `api_${node.operationId}` }
* },
* }
* ```
*
* @example Strip schema descriptions
* ```ts
* const visitor: Visitor = {
* schema(node) {
* return { ...node, description: undefined }
* },
* }
* ```
*/
type Visitor = {
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode;
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
};
/**
* A visitor callback result that may be sync or async.
*/
type MaybePromise<T> = T | Promise<T>;
/**
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
*
* @example
* ```ts
* const visitor: AsyncVisitor = {
* async operation(node) {
* await Promise.resolve(node.operationId)
* },
* }
* ```
*/
type AsyncVisitor = {
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>;
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>;
};
/**
* Visitor used by `collect`.
*
* @example
* ```ts
* const visitor: CollectVisitor<string> = {
* operation(node) {
* return node.operationId
* },
* }
* ```
*/
type CollectVisitor<T> = {
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
};
/**
* Options for `transform`.
*
* @example
* ```ts
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
* ```
*
* @example
* ```ts
* // Only transform the current node, not nested children
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
* ```
*/
type TransformOptions = Visitor & {
/**
* Traversal depth.
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Internal parent override used during recursion.
*/
parent?: Node;
};
/**
* Options for `walk`.
*
* @example
* ```ts
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
* ```
*/
type WalkOptions = AsyncVisitor & {
/**
* Traversal depth.
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Maximum number of sibling nodes visited concurrently.
* @default 30
*/
concurrency?: number;
};
/**
* Options for `collect`.
*
* @example
* ```ts
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
* ```
*/
type CollectOptions<T> = CollectVisitor<T> & {
/**
* Traversal depth.
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Internal parent override used during recursion.
*/
parent?: Node;
};
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
declare function walk(node: Node, options: WalkOptions): Promise<void>;
/**
* Synchronous depth-first transform. Each visitor callback can return a
* replacement node. Returning `undefined` keeps the original.
*
* The original tree is never mutated, a new tree is returned. Pass
* `depth: 'shallow'` to skip recursion into children.
*
* @example Prefix every operationId
* ```ts
* const next = transform(root, {
* operation(node) {
* return { ...node, operationId: `prefixed_${node.operationId}` }
* },
* })
* ```
*
* @example Replace only the root node
* ```ts
* const next = transform(root, {
* depth: 'shallow',
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
* })
* ```
*/
declare function transform(node: InputNode, options: TransformOptions): InputNode;
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
declare function transform(node: Node, options: TransformOptions): Node;
/**
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
//#endregion
//#region src/defineMacro.d.ts
/**
* A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a
* {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an
* optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter
* and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).
*/
type Macro = Visitor & {
/**
* Macro identifier, surfaced in diagnostics. Follows the `macro<Name>` convention.
*/
name: string;
/**
* Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.
* Ordering within a bucket follows list order.
*/
enforce?: 'pre' | 'post';
/**
* Gate checked against the current node before any callback runs. When it returns `false`
* the macro is skipped for that node.
*/
when?: (node: Node) => boolean;
};
/**
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
* Adds no runtime behavior.
*
* @example
* ```ts
* const macroUntagged = defineMacro({
* name: 'untagged',
* operation(node) {
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
* },
* })
* ```
*/
declare function defineMacro(macro: Macro): Macro;
/**
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
* has no names, ordering, or composition.
*
* @example
* ```ts
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
* const next = transform(root, visitor)
* ```
*/
declare function composeMacros(macros: ReadonlyArray<Macro>): Visitor;
/**
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
* `depth: 'shallow'` to rewrite the root node only.
*
* @example
* ```ts
* const next = applyMacros(root, [macroIntegerToString])
* ```
*
* @example Apply to the root node only
* ```ts
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
* ```
*/
declare function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: {
depth?: VisitorDepth;
}): TNode;
//#endregion
export { ParentOf as a, collect as c, httpMethods as d, schemaTypes as f, defineMacro as i, transform as l, applyMacros as n, Visitor as o, composeMacros as r, VisitorContext as s, Macro as t, walk as u };
//# sourceMappingURL=defineMacro-BLIR6k-j.d.ts.map
import "./chunk-CNktS9qV.js";
import { r as transform } from "./visitor-DJ6ZEJvq.js";
//#region src/defineMacro.ts
/**
* Visitor callback names a macro can implement, one per traversable node kind.
* Mirrors the keys of {@link Visitor}.
*/
const macroKeys = [
"input",
"output",
"operation",
"schema",
"property",
"parameter",
"response"
];
/**
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
* list keeps its authored order.
*/
function enforceWeight(enforce) {
if (enforce === "pre") return 0;
if (enforce === "post") return 2;
return 1;
}
/**
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
* Adds no runtime behavior.
*
* @example
* ```ts
* const macroUntagged = defineMacro({
* name: 'untagged',
* operation(node) {
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
* },
* })
* ```
*/
function defineMacro(macro) {
return macro;
}
/**
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
* original reference (structural sharing).
*/
function chain(macros, key, node, context) {
let current = node;
for (const macro of macros) {
const callback = macro[key];
if (!callback) continue;
if (macro.when && !macro.when(current)) continue;
const next = callback(current, context);
if (next != null) current = next;
}
return current === node ? void 0 : current;
}
/**
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
* has no names, ordering, or composition.
*
* @example
* ```ts
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
* const next = transform(root, visitor)
* ```
*/
function composeMacros(macros) {
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
const visitor = {};
for (const key of macroKeys) {
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
const callback = (node, context) => chain(ordered, key, node, context);
visitor[key] = callback;
}
return visitor;
}
/**
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
* `depth: 'shallow'` to rewrite the root node only.
*
* @example
* ```ts
* const next = applyMacros(root, [macroIntegerToString])
* ```
*
* @example Apply to the root node only
* ```ts
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
* ```
*/
function applyMacros(root, macros, options) {
if (macros.length === 0) return root;
return transform(root, {
...composeMacros(macros),
depth: options?.depth
});
}
//#endregion
export { composeMacros as n, defineMacro as r, applyMacros as t };
//# sourceMappingURL=defineMacro-BTXvS8nI.js.map
{"version":3,"file":"defineMacro-BTXvS8nI.js","names":[],"sources":["../src/defineMacro.ts"],"sourcesContent":["import type { VisitorDepth } from './constants.ts'\nimport type { Node } from './nodes/index.ts'\nimport type { Visitor, VisitorContext } from './visitor.ts'\nimport { transform } from './visitor.ts'\n\n/**\n * Visitor callback names a macro can implement, one per traversable node kind.\n * Mirrors the keys of {@link Visitor}.\n */\nconst macroKeys = ['input', 'output', 'operation', 'schema', 'property', 'parameter', 'response'] as const\n\ntype MacroKey = (typeof macroKeys)[number]\n\n/**\n * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain\n * list keeps its authored order.\n */\nfunction enforceWeight(enforce?: 'pre' | 'post'): number {\n if (enforce === 'pre') return 0\n if (enforce === 'post') return 2\n return 1\n}\n\n/**\n * A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a\n * {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an\n * optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter\n * and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).\n */\nexport type Macro = Visitor & {\n /**\n * Macro identifier, surfaced in diagnostics. Follows the `macro<Name>` convention.\n */\n name: string\n /**\n * Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.\n * Ordering within a bucket follows list order.\n */\n enforce?: 'pre' | 'post'\n /**\n * Gate checked against the current node before any callback runs. When it returns `false`\n * the macro is skipped for that node.\n */\n when?: (node: Node) => boolean\n}\n\n/**\n * Types a macro for inference and a single construction site, mirroring `definePlugin`.\n * Adds no runtime behavior.\n *\n * @example\n * ```ts\n * const macroUntagged = defineMacro({\n * name: 'untagged',\n * operation(node) {\n * return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }\n * },\n * })\n * ```\n */\nexport function defineMacro(macro: Macro): Macro {\n return macro\n}\n\ntype MacroCallback = (node: Node, context: VisitorContext) => Node | null | undefined\n\n/**\n * Runs every macro's callback for one node kind in order, chaining the result so each macro sees\n * the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the\n * original reference (structural sharing).\n */\nfunction chain(macros: ReadonlyArray<Macro>, key: MacroKey, node: Node, context: VisitorContext): Node | undefined {\n let current = node\n\n for (const macro of macros) {\n const callback = macro[key] as MacroCallback | undefined\n if (!callback) continue\n if (macro.when && !macro.when(current)) continue\n\n const next = callback(current, context)\n if (next != null) current = next\n }\n\n return current === node ? undefined : current\n}\n\n/**\n * Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin\n * transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied\n * sequentially per node so later macros see earlier output. This differs from a plain visitor, which\n * has no names, ordering, or composition.\n *\n * @example\n * ```ts\n * const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])\n * const next = transform(root, visitor)\n * ```\n */\nexport function composeMacros(macros: ReadonlyArray<Macro>): Visitor {\n const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce))\n\n const visitor: Visitor = {}\n for (const key of macroKeys) {\n if (!ordered.some((macro) => typeof macro[key] === 'function')) continue\n\n const callback = (node: Node, context: VisitorContext) => chain(ordered, key, node, context)\n ;(visitor as Record<MacroKey, MacroCallback>)[key] = callback\n }\n\n return visitor\n}\n\n/**\n * Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s\n * structural sharing, so an empty or no-op macro list returns the same reference. Pass\n * `depth: 'shallow'` to rewrite the root node only.\n *\n * @example\n * ```ts\n * const next = applyMacros(root, [macroIntegerToString])\n * ```\n *\n * @example Apply to the root node only\n * ```ts\n * const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })\n * ```\n */\nexport function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: { depth?: VisitorDepth }): TNode {\n if (macros.length === 0) return root\n\n return transform(root, { ...compose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cjs");
//#region src/defineMacro.ts
/**
* Visitor callback names a macro can implement, one per traversable node kind.
* Mirrors the keys of {@link Visitor}.
*/
const macroKeys = [
"input",
"output",
"operation",
"schema",
"property",
"parameter",
"response"
];
/**
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
* list keeps its authored order.
*/
function enforceWeight(enforce) {
if (enforce === "pre") return 0;
if (enforce === "post") return 2;
return 1;
}
/**
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
* Adds no runtime behavior.
*
* @example
* ```ts
* const macroUntagged = defineMacro({
* name: 'untagged',
* operation(node) {
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
* },
* })
* ```
*/
function defineMacro(macro) {
return macro;
}
/**
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
* original reference (structural sharing).
*/
function chain(macros, key, node, context) {
let current = node;
for (const macro of macros) {
const callback = macro[key];
if (!callback) continue;
if (macro.when && !macro.when(current)) continue;
const next = callback(current, context);
if (next != null) current = next;
}
return current === node ? void 0 : current;
}
/**
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
* has no names, ordering, or composition.
*
* @example
* ```ts
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
* const next = transform(root, visitor)
* ```
*/
function composeMacros(macros) {
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
const visitor = {};
for (const key of macroKeys) {
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
const callback = (node, context) => chain(ordered, key, node, context);
visitor[key] = callback;
}
return visitor;
}
/**
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
* `depth: 'shallow'` to rewrite the root node only.
*
* @example
* ```ts
* const next = applyMacros(root, [macroIntegerToString])
* ```
*
* @example Apply to the root node only
* ```ts
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
* ```
*/
function applyMacros(root, macros, options) {
if (macros.length === 0) return root;
return require_visitor.transform(root, {
...composeMacros(macros),
depth: options?.depth
});
}
//#endregion
Object.defineProperty(exports, "applyMacros", {
enumerable: true,
get: function() {
return applyMacros;
}
});
Object.defineProperty(exports, "composeMacros", {
enumerable: true,
get: function() {
return composeMacros;
}
});
Object.defineProperty(exports, "defineMacro", {
enumerable: true,
get: function() {
return defineMacro;
}
});
//# sourceMappingURL=defineMacro-Bv9R_9a2.cjs.map
{"version":3,"file":"defineMacro-Bv9R_9a2.cjs","names":["transform"],"sources":["../src/defineMacro.ts"],"sourcesContent":["import type { VisitorDepth } from './constants.ts'\nimport type { Node } from './nodes/index.ts'\nimport type { Visitor, VisitorContext } from './visitor.ts'\nimport { transform } from './visitor.ts'\n\n/**\n * Visitor callback names a macro can implement, one per traversable node kind.\n * Mirrors the keys of {@link Visitor}.\n */\nconst macroKeys = ['input', 'output', 'operation', 'schema', 'property', 'parameter', 'response'] as const\n\ntype MacroKey = (typeof macroKeys)[number]\n\n/**\n * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain\n * list keeps its authored order.\n */\nfunction enforceWeight(enforce?: 'pre' | 'post'): number {\n if (enforce === 'pre') return 0\n if (enforce === 'post') return 2\n return 1\n}\n\n/**\n * A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a\n * {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an\n * optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter\n * and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).\n */\nexport type Macro = Visitor & {\n /**\n * Macro identifier, surfaced in diagnostics. Follows the `macro<Name>` convention.\n */\n name: string\n /**\n * Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.\n * Ordering within a bucket follows list order.\n */\n enforce?: 'pre' | 'post'\n /**\n * Gate checked against the current node before any callback runs. When it returns `false`\n * the macro is skipped for that node.\n */\n when?: (node: Node) => boolean\n}\n\n/**\n * Types a macro for inference and a single construction site, mirroring `definePlugin`.\n * Adds no runtime behavior.\n *\n * @example\n * ```ts\n * const macroUntagged = defineMacro({\n * name: 'untagged',\n * operation(node) {\n * return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }\n * },\n * })\n * ```\n */\nexport function defineMacro(macro: Macro): Macro {\n return macro\n}\n\ntype MacroCallback = (node: Node, context: VisitorContext) => Node | null | undefined\n\n/**\n * Runs every macro's callback for one node kind in order, chaining the result so each macro sees\n * the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the\n * original reference (structural sharing).\n */\nfunction chain(macros: ReadonlyArray<Macro>, key: MacroKey, node: Node, context: VisitorContext): Node | undefined {\n let current = node\n\n for (const macro of macros) {\n const callback = macro[key] as MacroCallback | undefined\n if (!callback) continue\n if (macro.when && !macro.when(current)) continue\n\n const next = callback(current, context)\n if (next != null) current = next\n }\n\n return current === node ? undefined : current\n}\n\n/**\n * Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin\n * transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied\n * sequentially per node so later macros see earlier output. This differs from a plain visitor, which\n * has no names, ordering, or composition.\n *\n * @example\n * ```ts\n * const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])\n * const next = transform(root, visitor)\n * ```\n */\nexport function composeMacros(macros: ReadonlyArray<Macro>): Visitor {\n const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce))\n\n const visitor: Visitor = {}\n for (const key of macroKeys) {\n if (!ordered.some((macro) => typeof macro[key] === 'function')) continue\n\n const callback = (node: Node, context: VisitorContext) => chain(ordered, key, node, context)\n ;(visitor as Record<MacroKey, MacroCallback>)[key] = callback\n }\n\n return visitor\n}\n\n/**\n * Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s\n * structural sharing, so an empty or no-op macro list returns the same reference. Pass\n * `depth: 'shallow'` to rewrite the root node only.\n *\n * @example\n * ```ts\n * const next = applyMacros(root, [macroIntegerToString])\n * ```\n *\n * @example Apply to the root node only\n * ```ts\n * const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })\n * ```\n */\nexport function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: { depth?: VisitorDepth }): TNode {\n if (macros.length === 0) return root\n\n return transform(root, { ...composeMacros(macros), depth: options?.depth }) as TNode\n}\n"],"mappings":";;;;;;;AASA,MAAM,YAAY;CAAC;CAAS;CAAU;CAAa;CAAU;CAAY;CAAa;AAAU;;;;;AAQhG,SAAS,cAAc,SAAkC;CACvD,IAAI,YAAY,OAAO,OAAO;CAC9B,IAAI,YAAY,QAAQ,OAAO;CAC/B,OAAO;AACT;;;;;;;;;;;;;;;AAuCA,SAAgB,YAAY,OAAqB;CAC/C,OAAO;AACT;;;;;;AASA,SAAS,MAAM,QAA8B,KAAe,MAAY,SAA2C;CACjH,IAAI,UAAU;CAEd,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,WAAW,MAAM;EACvB,IAAI,CAAC,UAAU;EACf,IAAI,MAAM,QAAQ,CAAC,MAAM,KAAK,OAAO,GAAG;EAExC,MAAM,OAAO,SAAS,SAAS,OAAO;EACtC,IAAI,QAAQ,MAAM,UAAU;CAC9B;CAEA,OAAO,YAAY,OAAO,KAAA,IAAY;AACxC;;;;;;;;;;;;;AAcA,SAAgB,cAAc,QAAuC;CACnE,MAAM,UAAU,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM,cAAc,EAAE,OAAO,IAAI,cAAc,EAAE,OAAO,CAAC;CAE9F,MAAM,UAAmB,CAAC;CAC1B,KAAK,MAAM,OAAO,WAAW;EAC3B,IAAI,CAAC,QAAQ,MAAM,UAAU,OAAO,MAAM,SAAS,UAAU,GAAG;EAEhE,MAAM,YAAY,MAAY,YAA4B,MAAM,SAAS,KAAK,MAAM,OAAO;EAC1F,QAA6C,OAAO;CACvD;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,YAAgC,MAAa,QAA8B,SAA2C;CACpI,IAAI,OAAO,WAAW,GAAG,OAAO;CAEhC,OAAOA,gBAAAA,UAAU,MAAM;EAAE,GAAG,cAAc,MAAM;EAAG,OAAO,SAAS;CAAM,CAAC;AAC5E"}
import "./chunk-CNktS9qV.js";
//#region src/utils/extractStringsFromNodes.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
function extractStringsFromNodes(nodes) {
if (!nodes?.length) return "";
return nodes.map((node) => {
if (typeof node === "string") return node;
if (node.kind === "Text") return node.value;
if (node.kind === "Break") return "";
if (node.kind === "Jsx") return node.value;
const parts = [];
if ("params" in node && node.params) parts.push(node.params);
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
if ("returnType" in node && node.returnType) parts.push(node.returnType);
if ("type" in node && typeof node.type === "string") parts.push(node.type);
const nested = extractStringsFromNodes(node.nodes);
if (nested) parts.push(nested);
return parts.join("\n");
}).filter(Boolean).join("\n");
}
//#endregion
export { extractStringsFromNodes as t };
//# sourceMappingURL=extractStringsFromNodes-Cja-xxx5.js.map
{"version":3,"file":"extractStringsFromNodes-Cja-xxx5.js","names":[],"sources":["../src/utils/extractStringsFromNodes.ts"],"sourcesContent":["import type { CodeNode } from '../nodes/code.ts'\n\n/**\n * Extracts all string content from a `CodeNode` tree recursively.\n *\n * Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),\n * and nested node content. Used to build the full source string for import filtering.\n */\nexport function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes?.length) return ''\n return nodes\n .map((node) => {\n // Backward-compat: compiled plugins may still pass bare strings at runtime\n if (typeof node === 'string') return node as string\n if (node.kind === 'Text') return node.value\n if (node.kind === 'Break') return ''\n if (node.kind === 'Jsx') return node.value\n\n const parts: Array<string> = []\n\n if ('params' in node && node.params) parts.push(node.params)\n if ('generics' in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(', ') : node.generics)\n if ('returnType' in node && node.returnType) parts.push(node.returnType)\n if ('type' in node && typeof node.type === 'string') parts.push(node.type)\n\n const nested = extractStringsFromNodes(node.nodes)\n\n if (nested) parts.push(nested)\n\n return parts.join('\\n')\n })\n .filter(Boolean)\n .join('\\n')\n}\n"],"mappings":";;;;;;;;AAQA,SAAgB,wBAAwB,OAA4C;CAClF,IAAI,CAAC,OAAO,QAAQ,OAAO;CAC3B,OAAO,MACJ,KAAK,SAAS;EAEb,IAAI,OAAO,SAAS,UAAU,OAAO;EACrC,IAAI,KAAK,SAAS,QAAQ,OAAO,KAAK;EACtC,IAAI,KAAK,SAAS,SAAS,OAAO;EAClC,IAAI,KAAK,SAAS,OAAO,OAAO,KAAK;EAErC,MAAM,QAAuB,CAAC;EAE9B,IAAI,YAAY,QAAQ,KAAK,QAAQ,MAAM,KAAK,KAAK,MAAM;EAC3D,IAAI,cAAc,QAAQ,KAAK,UAAU,MAAM,KAAK,MAAM,QAAQ,KAAK,QAAQ,IAAI,KAAK,SAAS,KAAK,IAAI,IAAI,KAAK,QAAQ;EAC3H,IAAI,gBAAgB,QAAQ,KAAK,YAAY,MAAM,KAAK,KAAK,UAAU;EACvE,IAAI,UAAU,QAAQ,OAAO,KAAK,SAAS,UAAU,MAAM,KAAK,KAAK,IAAI;EAEzE,MAAM,SAAS,wBAAwB,KAAK,KAAK;EAEjD,IAAI,QAAQ,MAAM,KAAK,MAAM;EAE7B,OAAO,MAAM,KAAK,IAAI;CACxB,CAAC,CAAC,CACD,OAAO,OAAO,CAAC,CACf,KAAK,IAAI;AACd"}
require("./response-hnSw2NKE.cjs");
//#region src/utils/extractStringsFromNodes.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
function extractStringsFromNodes(nodes) {
if (!nodes?.length) return "";
return nodes.map((node) => {
if (typeof node === "string") return node;
if (node.kind === "Text") return node.value;
if (node.kind === "Break") return "";
if (node.kind === "Jsx") return node.value;
const parts = [];
if ("params" in node && node.params) parts.push(node.params);
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
if ("returnType" in node && node.returnType) parts.push(node.returnType);
if ("type" in node && typeof node.type === "string") parts.push(node.type);
const nested = extractStringsFromNodes(node.nodes);
if (nested) parts.push(nested);
return parts.join("\n");
}).filter(Boolean).join("\n");
}
//#endregion
Object.defineProperty(exports, "extractStringsFromNodes", {
enumerable: true,
get: function() {
return extractStringsFromNodes;
}
});
//# sourceMappingURL=extractStringsFromNodes-DKgDjFO0.cjs.map
{"version":3,"file":"extractStringsFromNodes-DKgDjFO0.cjs","names":[],"sources":["../src/utils/extractStringsFromNodes.ts"],"sourcesContent":["import type { CodeNode } from '../nodes/code.ts'\n\n/**\n * Extracts all string content from a `CodeNode` tree recursively.\n *\n * Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),\n * and nested node content. Used to build the full source string for import filtering.\n */\nexport function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes?.length) return ''\n return nodes\n .map((node) => {\n // Backward-compat: compiled plugins may still pass bare strings at runtime\n if (typeof node === 'string') return node as string\n if (node.kind === 'Text') return node.value\n if (node.kind === 'Break') return ''\n if (node.kind === 'Jsx') return node.value\n\n const parts: Array<string> = []\n\n if ('params' in node && node.params) parts.push(node.params)\n if ('generics' in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(', ') : node.generics)\n if ('returnType' in node && node.returnType) parts.push(node.returnType)\n if ('type' in node && typeof node.type === 'string') parts.push(node.type)\n\n const nested = extractStringsFromNodes(node.nodes)\n\n if (nested) parts.push(nested)\n\n return parts.join('\\n')\n })\n .filter(Boolean)\n .join('\\n')\n}\n"],"mappings":";;;;;;;;AAQA,SAAgB,wBAAwB,OAA4C;CAClF,IAAI,CAAC,OAAO,QAAQ,OAAO;CAC3B,OAAO,MACJ,KAAK,SAAS;EAEb,IAAI,OAAO,SAAS,UAAU,OAAO;EACrC,IAAI,KAAK,SAAS,QAAQ,OAAO,KAAK;EACtC,IAAI,KAAK,SAAS,SAAS,OAAO;EAClC,IAAI,KAAK,SAAS,OAAO,OAAO,KAAK;EAErC,MAAM,QAAuB,CAAC;EAE9B,IAAI,YAAY,QAAQ,KAAK,QAAQ,MAAM,KAAK,KAAK,MAAM;EAC3D,IAAI,cAAc,QAAQ,KAAK,UAAU,MAAM,KAAK,MAAM,QAAQ,KAAK,QAAQ,IAAI,KAAK,SAAS,KAAK,IAAI,IAAI,KAAK,QAAQ;EAC3H,IAAI,gBAAgB,QAAQ,KAAK,YAAY,MAAM,KAAK,KAAK,UAAU;EACvE,IAAI,UAAU,QAAQ,OAAO,KAAK,SAAS,UAAU,MAAM,KAAK,KAAK,IAAI;EAEzE,MAAM,SAAS,wBAAwB,KAAK,KAAK;EAEjD,IAAI,QAAQ,MAAM,KAAK,MAAM;EAE7B,OAAO,MAAM,KAAK,IAAI;CACxB,CAAC,CAAC,CACD,OAAO,OAAO,CAAC,CACf,KAAK,IAAI;AACd"}
import { n as __name } from "./chunk-CNktS9qV.js";
import { Pt as CodeNode } from "./index-BzjwdK2M.js";
//#region src/utils/extractStringsFromNodes.d.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string;
//#endregion
export { extractStringsFromNodes as t };
//# sourceMappingURL=extractStringsFromNodes-p4mX1TQD.d.ts.map
import { t as __exportAll } from "./chunk-CNktS9qV.js";
import { B as createFunction, D as createSource, E as createImport, H as createText, L as createArrowFunction, N as createContent, R as createBreak, T as createExport, U as createType, V as createJsx, X as createSchema, _ as createIndexedAccessType, a as createParameter, d as createRequestBody, g as createFunctionParameters, h as createFunctionParameter, l as createOperation, p as createInput, r as createProperty, s as createOutput, t as createResponse, v as createObjectBindingPattern, y as createTypeLiteral, z as createConst } from "./response-KUdWiDWw.js";
import { t as extractStringsFromNodes } from "./extractStringsFromNodes-Cja-xxx5.js";
import { hash } from "node:crypto";
import path from "node:path";
//#region ../../internals/utils/src/fs.ts
/**
* Strips the file extension from a path or file name.
* Only removes the last `.ext` segment when the dot is not part of a directory name.
*
* @example
* trimExtName('petStore.ts') // 'petStore'
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
* trimExtName('noExtension') // 'noExtension'
*/
function trimExtName(text) {
const dotIndex = text.lastIndexOf(".");
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
return text;
}
//#endregion
//#region src/utils/fileMerge.ts
function sourceKey(source) {
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
}
function pathTypeKey(path, isTypeOnly) {
return `${path}:${isTypeOnly ?? false}`;
}
function exportKey(path, name, isTypeOnly, asAlias) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
}
function importKey(path, name, isTypeOnly) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
}
/**
* Computes a multi-level sort key for exports and imports:
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
*/
function sortKey(node) {
const isArray = Array.isArray(node.name) ? "1" : "0";
const typeOnly = node.isTypeOnly ? "0" : "1";
const hasName = node.name != null ? "1" : "0";
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
}
/**
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
*
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
*/
function combineSources(sources) {
const seen = /* @__PURE__ */ new Map();
for (const source of sources) {
const key = sourceKey(source);
if (!seen.has(key)) seen.set(key, source);
}
return [...seen.values()];
}
/**
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
*
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
*/
function mergeNameArrays(existing, incoming) {
const merged = new Set(existing);
for (const name of incoming) merged.add(name);
return [...merged];
}
/**
* Deduplicates and merges `ExportNode` objects by path and type.
*
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
*/
function combineExports(exports) {
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = exports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
const { name, path, isTypeOnly, asAlias } = curr;
if (Array.isArray(name)) {
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name: [...new Set(name)]
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
const key = exportKey(path, name, isTypeOnly, asAlias);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
/**
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
*
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
*/
function combineImports(imports, exports, source) {
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
const importNameMemo = /* @__PURE__ */ new Map();
const canonicalizeName = (n) => {
if (typeof n === "string") return n;
const key = `${n.propertyName}:${n.name ?? ""}`;
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
return importNameMemo.get(key);
};
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
for (const node of imports) {
if (!Array.isArray(node.name)) continue;
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
}
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = imports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
if (curr.path === curr.root) continue;
const { path, isTypeOnly } = curr;
let { name } = curr;
if (Array.isArray(name)) {
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
const key = importKey(path, name, isTypeOnly);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
//#endregion
//#region src/factory.ts
var factory_exports = /* @__PURE__ */ __exportAll({
createArrowFunction: () => createArrowFunction,
createBreak: () => createBreak,
createConst: () => createConst,
createContent: () => createContent,
createExport: () => createExport,
createFile: () => createFile,
createFunction: () => createFunction,
createFunctionParameter: () => createFunctionParameter,
createFunctionParameters: () => createFunctionParameters,
createImport: () => createImport,
createIndexedAccessType: () => createIndexedAccessType,
createInput: () => createInput,
createJsx: () => createJsx,
createObjectBindingPattern: () => createObjectBindingPattern,
createOperation: () => createOperation,
createOutput: () => createOutput,
createParameter: () => createParameter,
createProperty: () => createProperty,
createRequestBody: () => createRequestBody,
createResponse: () => createResponse,
createSchema: () => createSchema,
createSource: () => createSource,
createText: () => createText,
createType: () => createType,
createTypeLiteral: () => createTypeLiteral,
update: () => update
});
/**
* Identity-preserving node update: returns `node` unchanged when every field in
* `changes` already equals (by reference) the current value, otherwise a new node
* with the changes applied.
*
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
* downstream passes can detect "nothing changed" by identity. Comparison is
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
*
* @example
* ```ts
* update(node, { name: node.name }) // -> same `node` reference
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
* ```
*/
function update(node, changes) {
for (const key in changes) if (changes[key] !== node[key]) return {
...node,
...changes
};
return node;
}
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
function createFile(input) {
const extname = path.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
if (!kept.length) return [];
return [kept.length === imp.name.length ? imp : {
...imp,
name: kept
}];
});
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
return {
kind: "File",
...input,
id: hash("sha256", input.path, "hex"),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? {}
};
}
//#endregion
export { factory_exports as n, update as r, createFile as t };
//# sourceMappingURL=factory-CZNOGI-N.js.map
{"version":3,"file":"factory-CZNOGI-N.js","names":[],"sources":["../../../internals/utils/src/fs.ts","../src/utils/fileMerge.ts","../src/factory.ts"],"sourcesContent":["import { existsSync } from 'node:fs'\nimport { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises'\nimport { dirname, join, posix, resolve } from 'node:path'\nimport { camelCase } from './casing.ts'\nimport { runtime } from './runtime.ts'\n\n/**\n * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and\n * returns the absolute path of the nearest `package.json`, or `null` when none\n * is found before reaching the filesystem root.\n *\n * @example\n * ```ts\n * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'\n * ```\n */\nexport function findPackageJSON(cwd?: string): string | null {\n let dir = cwd ? resolve(cwd) : process.cwd()\n while (true) {\n const pkgPath = join(dir, 'package.json')\n if (existsSync(pkgPath)) return pkgPath\n const parent = dirname(dir)\n if (parent === dir) return null\n dir = parent\n }\n}\n\n/**\n * Converts all backslashes to forward slashes.\n * Extended-length Windows paths (`\\\\?\\...`) are left unchanged.\n */\nfunction toSlash(p: string): string {\n if (p.startsWith('\\\\\\\\?\\\\')) return p\n\n return p.replaceAll('\\\\', '/')\n}\n\n/**\n * Returns the relative path from `rootDir` to `filePath`, always using forward slashes\n * and prefixed with `./` when not already traversing upward.\n *\n * @example\n * ```ts\n * getRelativePath('/src/components', '/src/components/Button.tsx') // './Button.tsx'\n * getRelativePath('/src/components', '/src/utils/helpers.ts') // '../utils/helpers.ts'\n * ```\n */\nexport function getRelativePath(rootDir?: string | null, filePath?: string | null): string {\n if (!rootDir || !filePath) {\n throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)\n }\n\n const relativePath = posix.relative(toSlash(rootDir), toSlash(filePath))\n\n return relativePath.startsWith('../') ? relativePath : `./${relativePath}`\n}\n\n/**\n * Resolves to `true` when the file or directory at `path` exists.\n * Uses `Bun.file().exists()` when running under Bun, `fs.access` otherwise.\n *\n * @example\n * ```ts\n * if (await exists('./kubb.config.ts')) {\n * const content = await read('./kubb.config.ts')\n * }\n * ```\n */\nexport async function exists(path: string): Promise<boolean> {\n if (runtime.isBun) {\n return Bun.file(path).exists()\n }\n return access(path).then(\n () => true,\n () => false,\n )\n}\n\n/**\n * Reads the file at `path` as a UTF-8 string.\n * Uses `Bun.file().text()` when running under Bun, `fs.readFile` otherwise.\n *\n * @example\n * ```ts\n * const source = await read('./src/Pet.ts')\n * ```\n */\nexport async function read(path: string): Promise<string> {\n if (runtime.isBun) {\n return Bun.file(path).text()\n }\n return readFile(path, { encoding: 'utf8' })\n}\n\ntype WriteOptions = {\n /**\n * When `true`, re-reads the file immediately after writing and throws if the\n * content does not match — useful for catching write failures on unreliable file systems.\n */\n sanity?: boolean\n}\n\n/**\n * Writes `data` to `path`, trimming leading/trailing whitespace before saving.\n * Skips the write when the trimmed content is empty or identical to what is already on disk.\n * Creates any missing parent directories automatically.\n * When `sanity` is `true`, re-reads the file after writing and throws if the content does not match.\n *\n * @example\n * ```ts\n * await write('./src/Pet.ts', source) // writes and returns trimmed content\n * await write('./src/Pet.ts', source) // null — file unchanged\n * await write('./src/Pet.ts', ' ') // null — empty content skipped\n * ```\n */\nexport async function write(path: string, data: string, options: WriteOptions = {}): Promise<string | null> {\n const trimmed = data.trim()\n if (trimmed === '') return null\n\n const resolved = resolve(path)\n\n if (runtime.isBun) {\n const file = Bun.file(resolved)\n const oldContent = (await file.exists()) ? await file.text() : null\n if (oldContent === trimmed) return null\n await Bun.write(resolved, trimmed)\n return trimmed\n }\n\n try {\n const oldContent = await readFile(resolved, { encoding: 'utf-8' })\n if (oldContent === trimmed) return null\n } catch {\n /* file doesn't exist yet */\n }\n\n await mkdir(dirname(resolved), { recursive: true })\n await writeFile(resolved, trimmed, { encoding: 'utf-8' })\n\n if (options.sanity) {\n const savedData = await readFile(resolved, { encoding: 'utf-8' })\n if (savedData !== trimmed) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n return savedData\n }\n\n return trimmed\n}\n\n/**\n * Recursively removes `path`. Silently succeeds when `path` does not exist.\n *\n * @example\n * ```ts\n * await clean('./dist')\n * ```\n */\nexport async function clean(path: string): Promise<void> {\n return rm(path, { recursive: true, force: true })\n}\n\n/**\n * Converts a filesystem path to use POSIX (`/`) separators.\n *\n * Most of the codebase compares and composes paths as strings (prefix matching, joining for\n * import specifiers, splitting on `/`). On POSIX `path.resolve` already returns `/`-separated\n * paths, but on Windows it returns `\\`-separated paths, which breaks every such comparison.\n *\n * Routing every path that crosses a module boundary through `toPosixPath` keeps the rest of the\n * code platform-agnostic. The conversion runs unconditionally so Windows-specific behavior is\n * exercisable from POSIX CI.\n *\n * @example\n * toPosixPath('C:\\\\repo\\\\src\\\\pet.ts') // 'C:/repo/src/pet.ts'\n */\nexport function toPosixPath(filePath: string): string {\n return filePath.replaceAll('\\\\', '/')\n}\n\n/**\n * Strips the file extension from a path or file name.\n * Only removes the last `.ext` segment when the dot is not part of a directory name.\n *\n * @example\n * trimExtName('petStore.ts') // 'petStore'\n * trimExtName('/src/models/pet.ts') // '/src/models/pet'\n * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'\n * trimExtName('noExtension') // 'noExtension'\n */\nexport function trimExtName(text: string): string {\n const dotIndex = text.lastIndexOf('.')\n if (dotIndex > 0 && !text.includes('/', dotIndex)) {\n return text.slice(0, dotIndex)\n }\n return text\n}\n\n/**\n * Builds a nested file path from a dotted name. Splits on dots that precede a letter\n * (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases\n * every earlier segment, applies `caseLast` to the final segment, and joins with `/`.\n *\n * Empty segments are dropped before joining. They arise when the name starts with a dot\n * followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to\n * an empty string). Without this a leading `/` would form, which `path.resolve` reads as an\n * absolute path, letting generated files escape the configured output directory.\n *\n * @example Nested path from a dotted name\n * `toFilePath('pet.petId') // 'pet/petId'`\n *\n * @example PascalCase the final segment\n * `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`\n *\n * @example Suffix applied to the final segment only\n * `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`\n */\nexport function toFilePath(name: string, caseLast: (part: string) => string = camelCase): string {\n const parts = name.split(/\\.(?=[a-zA-Z])/)\n return parts\n .map((part, i) => (i === parts.length - 1 ? caseLast(part) : camelCase(part)))\n .filter(Boolean)\n .join('/')\n}\n","/**\n * File-member merging. `combineImports`, `combineExports`, and `combineSources` deduplicate and sort\n * the import, export, and source entries of one file, and drop imports nothing references. This works\n * on a file's members, not on schema content.\n *\n * For collapsing duplicate schema shapes by structural signature, see `dedupe.ts`.\n */\nimport type { ExportNode, ImportNode, SourceNode } from '../nodes/index.ts'\nimport { extractStringsFromNodes } from './extractStringsFromNodes.ts'\n\nfunction sourceKey(source: SourceNode): string {\n const nameKey = source.name ?? extractStringsFromNodes(source.nodes)\n return `${nameKey}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`\n}\n\nfunction pathTypeKey(path: string, isTypeOnly: boolean | null | undefined): string {\n return `${path}:${isTypeOnly ?? false}`\n}\n\nfunction exportKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined, asAlias: boolean | null | undefined): string {\n return `${path}:${name ?? ''}:${isTypeOnly ?? false}:${asAlias ?? ''}`\n}\n\nfunction importKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined): string {\n return `${path}:${name ?? ''}:${isTypeOnly ?? false}`\n}\n\n/**\n * Computes a multi-level sort key for exports and imports:\n * non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.\n */\nfunction sortKey(node: { name?: string | Array<unknown> | null; isTypeOnly?: boolean | null; path: string }): string {\n const isArray = Array.isArray(node.name) ? '1' : '0'\n const typeOnly = node.isTypeOnly ? '0' : '1'\n const hasName = node.name != null ? '1' : '0'\n const name = Array.isArray(node.name) ? node.name.toSorted().join('\\0') : (node.name ?? '')\n return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`\n}\n\n/**\n * Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.\n *\n * Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.\n */\nexport function combineSources(sources: Array<SourceNode>): Array<SourceNode> {\n const seen = new Map<string, SourceNode>()\n for (const source of sources) {\n const key = sourceKey(source)\n if (!seen.has(key)) seen.set(key, source)\n }\n return [...seen.values()]\n}\n\n/**\n * Merges `incoming` names into `existing`, preserving order and dropping duplicates.\n *\n * Shared by `combineExports` and `combineImports` for the same-path name-merge case.\n */\nfunction mergeNameArrays<TName>(existing: Array<TName>, incoming: Array<TName>): Array<TName> {\n const merged = new Set(existing)\n for (const name of incoming) merged.add(name)\n return [...merged]\n}\n\n/**\n * Deduplicates and merges `ExportNode` objects by path and type.\n *\n * Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.\n * Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.\n */\nexport function combineExports(exports: Array<ExportNode>): Array<ExportNode> {\n const result: Array<ExportNode> = []\n // Accumulates array-named exports keyed by `path:isTypeOnly` for name-merging\n const namedByPath = new Map<string, ExportNode>()\n // Deduplicates non-array exports by their exact identity\n const seen = new Set<string>()\n\n // Precompute sort keys once, avoids recomputing per comparison.\n const keyed = exports.map((node) => ({ node, key: sortKey(node) }))\n keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n\n for (const { node: curr } of keyed) {\n const { name, path, isTypeOnly, asAlias } = curr\n\n if (Array.isArray(name)) {\n if (!name.length) continue\n\n const key = pathTypeKey(path, isTypeOnly)\n const existing = namedByPath.get(key)\n\n if (existing && Array.isArray(existing.name)) {\n existing.name = mergeNameArrays(existing.name, name)\n } else {\n const newItem: ExportNode = { ...curr, name: [...new Set(name)] }\n result.push(newItem)\n namedByPath.set(key, newItem)\n }\n } else {\n const key = exportKey(path, name, isTypeOnly, asAlias)\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n }\n\n return result\n}\n\n/**\n * Deduplicates and merges `ImportNode` objects, filtering out unused imports.\n *\n * Retains imports that are referenced in `source` or re-exported. Imports with the same path and\n * `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.\n */\nexport function combineImports(imports: Array<ImportNode>, exports: Array<ExportNode>, source?: string): Array<ImportNode> {\n // Build a lookup of all exported names to retain imports that are re-exported\n const exportedNames = new Set(exports.flatMap((e) => (Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])))\n const isUsed = (importName: string): boolean => !source || source.includes(importName) || exportedNames.has(importName)\n\n // Memoize object import names so the same logical (propertyName, name) pair always\n // reuses the same object reference. Set-based deduplication then works correctly.\n const importNameMemo = new Map<string, { propertyName: string; name?: string }>()\n const canonicalizeName = (n: string | { propertyName: string; name?: string }): string | { propertyName: string; name?: string } => {\n if (typeof n === 'string') return n\n const key = `${n.propertyName}:${n.name ?? ''}`\n if (!importNameMemo.has(key)) importNameMemo.set(key, n)\n return importNameMemo.get(key)!\n }\n\n // Paths that keep at least one used named import. A default import from such a path is retained\n // even when its binding can't be found in `source` e.g. a generated `client` default import\n // alongside `import type { Client } from <same path>`, where merged grouped output omits the body.\n const pathsWithUsedNamedImport = new Set<string>()\n for (const node of imports) {\n if (!Array.isArray(node.name)) continue\n if (node.name.some((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))) {\n pathsWithUsedNamedImport.add(node.path)\n }\n }\n\n const result: Array<ImportNode> = []\n // Accumulates array-named imports keyed by `path:isTypeOnly` for name-merging\n const namedByPath = new Map<string, ImportNode>()\n // Deduplicates non-array imports by their exact identity\n const seen = new Set<string>()\n\n // Precompute sort keys once, avoids recomputing per comparison.\n const keyed = imports.map((node) => ({ node, key: sortKey(node) }))\n keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n\n for (const { node: curr } of keyed) {\n if (curr.path === curr.root) continue\n\n const { path, isTypeOnly } = curr\n let { name } = curr\n\n if (Array.isArray(name)) {\n name = [...new Set(name.map(canonicalizeName))].filter((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))\n if (!name.length) continue\n\n const key = pathTypeKey(path, isTypeOnly)\n const existing = namedByPath.get(key)\n\n if (existing && Array.isArray(existing.name)) {\n existing.name = mergeNameArrays(existing.name, name)\n } else {\n const newItem: ImportNode = { ...curr, name }\n result.push(newItem)\n namedByPath.set(key, newItem)\n }\n } else {\n if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue\n\n const key = importKey(path, name, isTypeOnly)\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n }\n\n return result\n}\n","import { hash } from 'node:crypto'\nimport path from 'node:path'\nimport { trimExtName } from '@internals/utils'\nimport type { FileNode, Node } from './nodes/index.ts'\nimport { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'\nimport { combineExports, combineImports, combineSources } from './utils/fileMerge.ts'\n\n// Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes\n// `ts.factory.createX`. Aggregating them here lets `export * as factory from './factory.ts'` in the\n// barrel surface every `createX` alongside the `createFile`/`update` helpers from a single module.\nexport { createArrowFunction, createBreak, createConst, createFunction, createJsx, createText, createType } from './nodes/code.ts'\nexport { createContent } from './nodes/content.ts'\nexport { createExport, createImport, createSource } from './nodes/file.ts'\nexport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts'\nexport { createInput } from './nodes/input.ts'\nexport { createOperation } from './nodes/operation.ts'\nexport { createOutput } from './nodes/output.ts'\nexport { createParameter } from './nodes/parameter.ts'\nexport { createProperty } from './nodes/property.ts'\nexport { createRequestBody } from './nodes/requestBody.ts'\nexport { createResponse } from './nodes/response.ts'\nexport { createSchema } from './nodes/schema.ts'\n\n/**\n * Identity-preserving node update: returns `node` unchanged when every field in\n * `changes` already equals (by reference) the current value, otherwise a new node\n * with the changes applied.\n *\n * Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the\n * structural sharing in {@link transform} so a no-op rewrite doesn't allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is\n * shallow: a structurally-equal but newly-allocated array/object counts as a change.\n *\n * @example\n * ```ts\n * update(node, { name: node.name }) // -> same `node` reference\n * update(node, { name: 'renamed' }) // -> new node, `name` replaced\n * ```\n */\nexport function update<T extends Node>(node: T, changes: Partial<T>): T {\n for (const key in changes) {\n if (changes[key] !== node[key as keyof T]) {\n return { ...node, ...changes }\n }\n }\n\n return node\n}\n\n/**\n * Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed\n * and `imports`/`exports`/`sources` are deduplicated.\n */\nexport type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &\n Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>\n\n/**\n * Creates a fully resolved `FileNode` from a file input descriptor.\n *\n * Computes:\n * - `id` SHA256 hash of the file path\n * - `name` `baseName` without extension\n * - `extname` extension extracted from `baseName`\n *\n * Deduplicates:\n * - `sources` via `combineSources`\n * - `exports` via `combineExports`\n * - `imports` via `combineImports` (also filters unused imports)\n *\n * @throws {Error} when `baseName` has no extension.\n *\n * @example\n * ```ts\n * const file = createFile({\n * baseName: 'petStore.ts',\n * path: 'src/models/petStore.ts',\n * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],\n * imports: [createImport({ name: ['z'], path: 'zod' })],\n * exports: [createExport({ name: ['Pet'], path: './petStore' })],\n * })\n * // file.id = SHA256 hash of 'src/models/petStore.ts'\n * // file.name = 'petStore'\n * // file.extname = '.ts'\n * ```\n */\nexport function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {\n const rawExtname = path.extname(input.baseName)\n // Handle dotfile basename like '.ts' where path.extname returns ''\n const extname = (rawExtname || (input.baseName.startsWith('.') ? input.baseName : '')) as `.${string}`\n if (!extname) {\n throw new Error(`No extname found for ${input.baseName}`)\n }\n\n const source = (input.sources ?? [])\n .flatMap((item) => item.nodes ?? [])\n .map((node) => extractStringsFromNodes([node]))\n .filter(Boolean)\n .join('\\n\\n')\n const resolvedExports = input.exports?.length ? combineExports(input.exports) : []\n const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || undefined) : []\n const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name): name is string => Boolean(name)))\n const nameOf = (item: string | { propertyName: string; name?: string }): string => (typeof item === 'string' ? item : (item.name ?? item.propertyName))\n // Drop self-imports. Consolidating output (`mode: 'file'`) can place a symbol's\n // definition and a cross-file import of it in the same file. The first pass catches imports that\n // resolve to this file's own path. The second drops imports of names the file already defines,\n // the case consolidation produces when the import path no longer matches `input.path`. Sources\n // stay intact, so the local definition remains. Bare specifiers like `'zod'` never match a path.\n const resolvedImports = combinedImports\n .filter((imp) => imp.path !== input.path)\n .flatMap((imp) => {\n if (!Array.isArray(imp.name)) {\n return typeof imp.name === 'string' && localNames.has(imp.name) ? [] : [imp]\n }\n const kept = imp.name.filter((item) => !localNames.has(nameOf(item)))\n if (!kept.length) return []\n return [kept.length === imp.name.length ? imp : { ...imp, name: kept }]\n })\n const resolvedSources = input.sources?.length ? combineSources(input.sources) : []\n\n return {\n kind: 'File',\n ...input,\n id: hash('sha256', input.path, 'hex'),\n name: trimExtName(input.baseName),\n extname,\n imports: resolvedImports,\n exports: resolvedExports,\n sources: resolvedSources,\n meta: input.meta ?? ({} as TMeta),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA8LA,SAAgB,YAAY,MAAsB;CAChD,MAAM,WAAW,KAAK,YAAY,GAAG;CACrC,IAAI,WAAW,KAAK,CAAC,KAAK,SAAS,KAAK,QAAQ,GAC9C,OAAO,KAAK,MAAM,GAAG,QAAQ;CAE/B,OAAO;AACT;;;AC1LA,SAAS,UAAU,QAA4B;CAE7C,OAAO,GADS,OAAO,QAAQ,wBAAwB,OAAO,KAAK,EACjD,GAAG,OAAO,gBAAgB,MAAM,GAAG,OAAO,cAAc;AAC5E;AAEA,SAAS,YAAY,MAAc,YAAgD;CACjF,OAAO,GAAG,KAAK,GAAG,cAAc;AAClC;AAEA,SAAS,UAAU,MAAc,MAAiC,YAAwC,SAA6C;CACrJ,OAAO,GAAG,KAAK,GAAG,QAAQ,GAAG,GAAG,cAAc,MAAM,GAAG,WAAW;AACpE;AAEA,SAAS,UAAU,MAAc,MAAiC,YAAgD;CAChH,OAAO,GAAG,KAAK,GAAG,QAAQ,GAAG,GAAG,cAAc;AAChD;;;;;AAMA,SAAS,QAAQ,MAAoG;CACnH,MAAM,UAAU,MAAM,QAAQ,KAAK,IAAI,IAAI,MAAM;CACjD,MAAM,WAAW,KAAK,aAAa,MAAM;CACzC,MAAM,UAAU,KAAK,QAAQ,OAAO,MAAM;CAC1C,MAAM,OAAO,MAAM,QAAQ,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC,KAAK,IAAI,IAAK,KAAK,QAAQ;CACxF,OAAO,GAAG,QAAQ,GAAG,SAAS,GAAG,KA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IAAI,KAAK,UAAU,MAAM,MAAM,KAAK;EACpC,MAAM,YAAY,MAAM,QAAQ;EAChC;EACA,SAAS;EACT,SAAS;EACT,SAAS;EACT,MAAM,MAAM,QAAS,CAAC;CACxB;AACF"}
const require_response = require("./response-hnSw2NKE.cjs");
const require_extractStringsFromNodes = require("./extractStringsFromNodes-DKgDjFO0.cjs");
let node_crypto = require("node:crypto");
let node_path = require("node:path");
node_path = require_response.__toESM(node_path, 1);
//#region ../../internals/utils/src/fs.ts
/**
* Strips the file extension from a path or file name.
* Only removes the last `.ext` segment when the dot is not part of a directory name.
*
* @example
* trimExtName('petStore.ts') // 'petStore'
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
* trimExtName('noExtension') // 'noExtension'
*/
function trimExtName(text) {
const dotIndex = text.lastIndexOf(".");
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
return text;
}
//#endregion
//#region src/utils/fileMerge.ts
function sourceKey(source) {
return `${source.name ?? require_extractStringsFromNodes.extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
}
function pathTypeKey(path, isTypeOnly) {
return `${path}:${isTypeOnly ?? false}`;
}
function exportKey(path, name, isTypeOnly, asAlias) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
}
function importKey(path, name, isTypeOnly) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
}
/**
* Computes a multi-level sort key for exports and imports:
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
*/
function sortKey(node) {
const isArray = Array.isArray(node.name) ? "1" : "0";
const typeOnly = node.isTypeOnly ? "0" : "1";
const hasName = node.name != null ? "1" : "0";
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
}
/**
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
*
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
*/
function combineSources(sources) {
const seen = /* @__PURE__ */ new Map();
for (const source of sources) {
const key = sourceKey(source);
if (!seen.has(key)) seen.set(key, source);
}
return [...seen.values()];
}
/**
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
*
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
*/
function mergeNameArrays(existing, incoming) {
const merged = new Set(existing);
for (const name of incoming) merged.add(name);
return [...merged];
}
/**
* Deduplicates and merges `ExportNode` objects by path and type.
*
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
*/
function combineExports(exports) {
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = exports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
const { name, path, isTypeOnly, asAlias } = curr;
if (Array.isArray(name)) {
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name: [...new Set(name)]
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
const key = exportKey(path, name, isTypeOnly, asAlias);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
/**
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
*
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
*/
function combineImports(imports, exports, source) {
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
const importNameMemo = /* @__PURE__ */ new Map();
const canonicalizeName = (n) => {
if (typeof n === "string") return n;
const key = `${n.propertyName}:${n.name ?? ""}`;
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
return importNameMemo.get(key);
};
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
for (const node of imports) {
if (!Array.isArray(node.name)) continue;
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
}
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = imports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
if (curr.path === curr.root) continue;
const { path, isTypeOnly } = curr;
let { name } = curr;
if (Array.isArray(name)) {
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
const key = importKey(path, name, isTypeOnly);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
//#endregion
//#region src/factory.ts
var factory_exports = /* @__PURE__ */ require_response.__exportAll({
createArrowFunction: () => require_response.createArrowFunction,
createBreak: () => require_response.createBreak,
createConst: () => require_response.createConst,
createContent: () => require_response.createContent,
createExport: () => require_response.createExport,
createFile: () => createFile,
createFunction: () => require_response.createFunction,
createFunctionParameter: () => require_response.createFunctionParameter,
createFunctionParameters: () => require_response.createFunctionParameters,
createImport: () => require_response.createImport,
createIndexedAccessType: () => require_response.createIndexedAccessType,
createInput: () => require_response.createInput,
createJsx: () => require_response.createJsx,
createObjectBindingPattern: () => require_response.createObjectBindingPattern,
createOperation: () => require_response.createOperation,
createOutput: () => require_response.createOutput,
createParameter: () => require_response.createParameter,
createProperty: () => require_response.createProperty,
createRequestBody: () => require_response.createRequestBody,
createResponse: () => require_response.createResponse,
createSchema: () => require_response.createSchema,
createSource: () => require_response.createSource,
createText: () => require_response.createText,
createType: () => require_response.createType,
createTypeLiteral: () => require_response.createTypeLiteral,
update: () => update
});
/**
* Identity-preserving node update: returns `node` unchanged when every field in
* `changes` already equals (by reference) the current value, otherwise a new node
* with the changes applied.
*
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
* downstream passes can detect "nothing changed" by identity. Comparison is
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
*
* @example
* ```ts
* update(node, { name: node.name }) // -> same `node` reference
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
* ```
*/
function update(node, changes) {
for (const key in changes) if (changes[key] !== node[key]) return {
...node,
...changes
};
return node;
}
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
function createFile(input) {
const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => require_extractStringsFromNodes.extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
if (!kept.length) return [];
return [kept.length === imp.name.length ? imp : {
...imp,
name: kept
}];
});
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
return {
kind: "File",
...input,
id: (0, node_crypto.hash)("sha256", input.path, "hex"),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? {}
};
}
//#endregion
Object.defineProperty(exports, "createFile", {
enumerable: true,
get: function() {
return createFile;
}
});
Object.defineProperty(exports, "factory_exports", {
enumerable: true,
get: function() {
return factory_exports;
}
});
Object.defineProperty(exports, "update", {
enumerable: true,
get: function() {
return update;
}
});
//# sourceMappingURL=factory-DG1CVkEb.cjs.map
{"version":3,"file":"factory-DG1CVkEb.cjs","names":["extractStringsFromNodes","path","extractStringsFromNodes"],"sources":["../../../internals/utils/src/fs.ts","../src/utils/fileMerge.ts","../src/factory.ts"],"sourcesContent":["import { existsSync } from 'node:fs'\nimport { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises'\nimport { dirname, join, posix, resolve } from 'node:path'\nimport { camelCase } from './casing.ts'\nimport { runtime } from './runtime.ts'\n\n/**\n * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and\n * returns the absolute path of the nearest `package.json`, or `null` when none\n * is found before reaching the filesystem root.\n *\n * @example\n * ```ts\n * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'\n * ```\n */\nexport function findPackageJSON(cwd?: string): string | null {\n let dir = cwd ? resolve(cwd) : process.cwd()\n while (true) {\n const pkgPath = join(dir, 'package.json')\n if (existsSync(pkgPath)) return pkgPath\n const parent = dirname(dir)\n if (parent === dir) return null\n dir = parent\n }\n}\n\n/**\n * Converts all backslashes to forward slashes.\n * Extended-length Windows paths (`\\\\?\\...`) are left unchanged.\n */\nfunction toSlash(p: string): string {\n if (p.startsWith('\\\\\\\\?\\\\')) return p\n\n return p.replaceAll('\\\\', '/')\n}\n\n/**\n * Returns the relative path from `rootDir` to `filePath`, always using forward slashes\n * and prefixed with `./` when not already traversing upward.\n *\n * @example\n * ```ts\n * getRelativePath('/src/components', '/src/components/Button.tsx') // './Button.tsx'\n * getRelativePath('/src/components', '/src/utils/helpers.ts') // '../utils/helpers.ts'\n * ```\n */\nexport function getRelativePath(rootDir?: string | null, filePath?: string | null): string {\n if (!rootDir || !filePath) {\n throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)\n }\n\n const relativePath = posix.relative(toSlash(rootDir), toSlash(filePath))\n\n return relativePath.startsWith('../') ? relativePath : `./${relativePath}`\n}\n\n/**\n * Resolves to `true` when the file or directory at `path` exists.\n * Uses `Bun.file().exists()` when running under Bun, `fs.access` otherwise.\n *\n * @example\n * ```ts\n * if (await exists('./kubb.config.ts')) {\n * const content = await read('./kubb.config.ts')\n * }\n * ```\n */\nexport async function exists(path: string): Promise<boolean> {\n if (runtime.isBun) {\n return Bun.file(path).exists()\n }\n return access(path).then(\n () => true,\n () => false,\n )\n}\n\n/**\n * Reads the file at `path` as a UTF-8 string.\n * Uses `Bun.file().text()` when running under Bun, `fs.readFile` otherwise.\n *\n * @example\n * ```ts\n * const source = await read('./src/Pet.ts')\n * ```\n */\nexport async function read(path: string): Promise<string> {\n if (runtime.isBun) {\n return Bun.file(path).text()\n }\n return readFile(path, { encoding: 'utf8' })\n}\n\ntype WriteOptions = {\n /**\n * When `true`, re-reads the file immediately after writing and throws if the\n * content does not match — useful for catching write failures on unreliable file systems.\n */\n sanity?: boolean\n}\n\n/**\n * Writes `data` to `path`, trimming leading/trailing whitespace before saving.\n * Skips the write when the trimmed content is empty or identical to what is already on disk.\n * Creates any missing parent directories automatically.\n * When `sanity` is `true`, re-reads the file after writing and throws if the content does not match.\n *\n * @example\n * ```ts\n * await write('./src/Pet.ts', source) // writes and returns trimmed content\n * await write('./src/Pet.ts', source) // null — file unchanged\n * await write('./src/Pet.ts', ' ') // null — empty content skipped\n * ```\n */\nexport async function write(path: string, data: string, options: WriteOptions = {}): Promise<string | null> {\n const trimmed = data.trim()\n if (trimmed === '') return null\n\n const resolved = resolve(path)\n\n if (runtime.isBun) {\n const file = Bun.file(resolved)\n const oldContent = (await file.exists()) ? await file.text() : null\n if (oldContent === trimmed) return null\n await Bun.write(resolved, trimmed)\n return trimmed\n }\n\n try {\n const oldContent = await readFile(resolved, { encoding: 'utf-8' })\n if (oldContent === trimmed) return null\n } catch {\n /* file doesn't exist yet */\n }\n\n await mkdir(dirname(resolved), { recursive: true })\n await writeFile(resolved, trimmed, { encoding: 'utf-8' })\n\n if (options.sanity) {\n const savedData = await readFile(resolved, { encoding: 'utf-8' })\n if (savedData !== trimmed) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n return savedData\n }\n\n return trimmed\n}\n\n/**\n * Recursively removes `path`. Silently succeeds when `path` does not exist.\n *\n * @example\n * ```ts\n * await clean('./dist')\n * ```\n */\nexport async function clean(path: string): Promise<void> {\n return rm(path, { recursive: true, force: true })\n}\n\n/**\n * Converts a filesystem path to use POSIX (`/`) separators.\n *\n * Most of the codebase compares and composes paths as strings (prefix matching, joining for\n * import specifiers, splitting on `/`). On POSIX `path.resolve` already returns `/`-separated\n * paths, but on Windows it returns `\\`-separated paths, which breaks every such comparison.\n *\n * Routing every path that crosses a module boundary through `toPosixPath` keeps the rest of the\n * code platform-agnostic. The conversion runs unconditionally so Windows-specific behavior is\n * exercisable from POSIX CI.\n *\n * @example\n * toPosixPath('C:\\\\repo\\\\src\\\\pet.ts') // 'C:/repo/src/pet.ts'\n */\nexport function toPosixPath(filePath: string): string {\n return filePath.replaceAll('\\\\', '/')\n}\n\n/**\n * Strips the file extension from a path or file name.\n * Only removes the last `.ext` segment when the dot is not part of a directory name.\n *\n * @example\n * trimExtName('petStore.ts') // 'petStore'\n * trimExtName('/src/models/pet.ts') // '/src/models/pet'\n * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'\n * trimExtName('noExtension') // 'noExtension'\n */\nexport function trimExtName(text: string): string {\n const dotIndex = text.lastIndexOf('.')\n if (dotIndex > 0 && !text.includes('/', dotIndex)) {\n return text.slice(0, dotIndex)\n }\n return text\n}\n\n/**\n * Builds a nested file path from a dotted name. Splits on dots that precede a letter\n * (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases\n * every earlier segment, applies `caseLast` to the final segment, and joins with `/`.\n *\n * Empty segments are dropped before joining. They arise when the name starts with a dot\n * followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to\n * an empty string). Without this a leading `/` would form, which `path.resolve` reads as an\n * absolute path, letting generated files escape the configured output directory.\n *\n * @example Nested path from a dotted name\n * `toFilePath('pet.petId') // 'pet/petId'`\n *\n * @example PascalCase the final segment\n * `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`\n *\n * @example Suffix applied to the final segment only\n * `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`\n */\nexport function toFilePath(name: string, caseLast: (part: string) => string = camelCase): string {\n const parts = name.split(/\\.(?=[a-zA-Z])/)\n return parts\n .map((part, i) => (i === parts.length - 1 ? caseLast(part) : camelCase(part)))\n .filter(Boolean)\n .join('/')\n}\n","/**\n * File-member merging. `combineImports`, `combineExports`, and `combineSources` deduplicate and sort\n * the import, export, and source entries of one file, and drop imports nothing references. This works\n * on a file's members, not on schema content.\n *\n * For collapsing duplicate schema shapes by structural signature, see `dedupe.ts`.\n */\nimport type { ExportNode, ImportNode, SourceNode } from '../nodes/index.ts'\nimport { extractStringsFromNodes } from './extractStringsFromNodes.ts'\n\nfunction sourceKey(source: SourceNode): string {\n const nameKey = source.name ?? extractStringsFromNodes(source.nodes)\n return `${nameKey}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`\n}\n\nfunction pathTypeKey(path: string, isTypeOnly: boolean | null | undefined): string {\n return `${path}:${isTypeOnly ?? false}`\n}\n\nfunction exportKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined, asAlias: boolean | null | undefined): string {\n return `${path}:${name ?? ''}:${isTypeOnly ?? false}:${asAlias ?? ''}`\n}\n\nfunction importKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined): string {\n return `${path}:${name ?? ''}:${isTypeOnly ?? false}`\n}\n\n/**\n * Computes a multi-level sort key for exports and imports:\n * non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.\n */\nfunction sortKey(node: { name?: string | Array<unknown> | null; isTypeOnly?: boolean | null; path: string }): string {\n const isArray = Array.isArray(node.name) ? '1' : '0'\n const typeOnly = node.isTypeOnly ? '0' : '1'\n const hasName = node.name != null ? '1' : '0'\n const name = Array.isArray(node.name) ? node.name.toSorted().join('\\0') : (node.name ?? '')\n return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`\n}\n\n/**\n * Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.\n *\n * Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.\n */\nexport function combineSources(sources: Array<SourceNode>): Array<SourceNode> {\n const seen = new Map<string, SourceNode>()\n for (const source of sources) {\n const key = sourceKey(source)\n if (!seen.has(key)) seen.set(key, source)\n }\n return [...seen.values()]\n}\n\n/**\n * Merges `incoming` names into `existing`, preserving order and dropping duplicates.\n *\n * Shared by `combineExports` and `combineImports` for the same-path name-merge case.\n */\nfunction mergeNameArrays<TName>(existing: Array<TName>, incoming: Array<TName>): Array<TName> {\n const merged = new Set(existing)\n for (const name of incoming) merged.add(name)\n return [...merged]\n}\n\n/**\n * Deduplicates and merges `ExportNode` objects by path and type.\n *\n * Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.\n * Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.\n */\nexport function combineExports(exports: Array<ExportNode>): Array<ExportNode> {\n const result: Array<ExportNode> = []\n // Accumulates array-named exports keyed by `path:isTypeOnly` for name-merging\n const namedByPath = new Map<string, ExportNode>()\n // Deduplicates non-array exports by their exact identity\n const seen = new Set<string>()\n\n // Precompute sort keys once, avoids recomputing per comparison.\n const keyed = exports.map((node) => ({ node, key: sortKey(node) }))\n keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n\n for (const { node: curr } of keyed) {\n const { name, path, isTypeOnly, asAlias } = curr\n\n if (Array.isArray(name)) {\n if (!name.length) continue\n\n const key = pathTypeKey(path, isTypeOnly)\n const existing = namedByPath.get(key)\n\n if (existing && Array.isArray(existing.name)) {\n existing.name = mergeNameArrays(existing.name, name)\n } else {\n const newItem: ExportNode = { ...curr, name: [...new Set(name)] }\n result.push(newItem)\n namedByPath.set(key, newItem)\n }\n } else {\n const key = exportKey(path, name, isTypeOnly, asAlias)\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n }\n\n return result\n}\n\n/**\n * Deduplicates and merges `ImportNode` objects, filtering out unused imports.\n *\n * Retains imports that are referenced in `source` or re-exported. Imports with the same path and\n * `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.\n */\nexport function combineImports(imports: Array<ImportNode>, exports: Array<ExportNode>, source?: string): Array<ImportNode> {\n // Build a lookup of all exported names to retain imports that are re-exported\n const exportedNames = new Set(exports.flatMap((e) => (Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])))\n const isUsed = (importName: string): boolean => !source || source.includes(importName) || exportedNames.has(importName)\n\n // Memoize object import names so the same logical (propertyName, name) pair always\n // reuses the same object reference. Set-based deduplication then works correctly.\n const importNameMemo = new Map<string, { propertyName: string; name?: string }>()\n const canonicalizeName = (n: string | { propertyName: string; name?: string }): string | { propertyName: string; name?: string } => {\n if (typeof n === 'string') return n\n const key = `${n.propertyName}:${n.name ?? ''}`\n if (!importNameMemo.has(key)) importNameMemo.set(key, n)\n return importNameMemo.get(key)!\n }\n\n // Paths that keep at least one used named import. A default import from such a path is retained\n // even when its binding can't be found in `source` e.g. a generated `client` default import\n // alongside `import type { Client } from <same path>`, where merged grouped output omits the body.\n const pathsWithUsedNamedImport = new Set<string>()\n for (const node of imports) {\n if (!Array.isArray(node.name)) continue\n if (node.name.some((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))) {\n pathsWithUsedNamedImport.add(node.path)\n }\n }\n\n const result: Array<ImportNode> = []\n // Accumulates array-named imports keyed by `path:isTypeOnly` for name-merging\n const namedByPath = new Map<string, ImportNode>()\n // Deduplicates non-array imports by their exact identity\n const seen = new Set<string>()\n\n // Precompute sort keys once, avoids recomputing per comparison.\n const keyed = imports.map((node) => ({ node, key: sortKey(node) }))\n keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n\n for (const { node: curr } of keyed) {\n if (curr.path === curr.root) continue\n\n const { path, isTypeOnly } = curr\n let { name } = curr\n\n if (Array.isArray(name)) {\n name = [...new Set(name.map(canonicalizeName))].filter((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))\n if (!name.length) continue\n\n const key = pathTypeKey(path, isTypeOnly)\n const existing = namedByPath.get(key)\n\n if (existing && Array.isArray(existing.name)) {\n existing.name = mergeNameArrays(existing.name, name)\n } else {\n const newItem: ImportNode = { ...curr, name }\n result.push(newItem)\n namedByPath.set(key, newItem)\n }\n } else {\n if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue\n\n const key = importKey(path, name, isTypeOnly)\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n }\n\n return result\n}\n","import { hash } from 'node:crypto'\nimport path from 'node:path'\nimport { trimExtName } from '@internals/utils'\nimport type { FileNode, Node } from './nodes/index.ts'\nimport { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'\nimport { combineExports, combineImports, combineSources } from './utils/fileMerge.ts'\n\n// Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes\n// `ts.factory.createX`. Aggregating them here lets `export * as factory from './factory.ts'` in the\n// barrel surface every `createX` alongside the `createFile`/`update` helpers from a single module.\nexport { createArrowFunction, createBreak, createConst, createFunction, createJsx, createText, createType } from './nodes/code.ts'\nexport { createContent } from './nodes/content.ts'\nexport { createExport, createImport, createSource } from './nodes/file.ts'\nexport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts'\nexport { createInput } from './nodes/input.ts'\nexport { createOperation } from './nodes/operation.ts'\nexport { createOutput } from './nodes/output.ts'\nexport { createParameter } from './nodes/parameter.ts'\nexport { createProperty } from './nodes/property.ts'\nexport { createRequestBody } from './nodes/requestBody.ts'\nexport { createResponse } from './nodes/response.ts'\nexport { createSchema } from './nodes/schema.ts'\n\n/**\n * Identity-preserving node update: returns `node` unchanged when every field in\n * `changes` already equals (by reference) the current value, otherwise a new node\n * with the changes applied.\n *\n * Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the\n * structural sharing in {@link transform} so a no-op rewrite doesn't allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is\n * shallow: a structurally-equal but newly-allocated array/object counts as a change.\n *\n * @example\n * ```ts\n * update(node, { name: node.name }) // -> same `node` reference\n * update(node, { name: 'renamed' }) // -> new node, `name` replaced\n * ```\n */\nexport function update<T extends Node>(node: T, changes: Partial<T>): T {\n for (const key in changes) {\n if (changes[key] !== node[key as keyof T]) {\n return { ...node, ...changes }\n }\n }\n\n return node\n}\n\n/**\n * Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed\n * and `imports`/`exports`/`sources` are deduplicated.\n */\nexport type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &\n Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>\n\n/**\n * Creates a fully resolved `FileNode` from a file input descriptor.\n *\n * Computes:\n * - `id` SHA256 hash of the file path\n * - `name` `baseName` without extension\n * - `extname` extension extracted from `baseName`\n *\n * Deduplicates:\n * - `sources` via `combineSources`\n * - `exports` via `combineExports`\n * - `imports` via `combineImports` (also filters unused imports)\n *\n * @throws {Error} when `baseName` has no extension.\n *\n * @example\n * ```ts\n * const file = createFile({\n * baseName: 'petStore.ts',\n * path: 'src/models/petStore.ts',\n * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],\n * imports: [createImport({ name: ['z'], path: 'zod' })],\n * exports: [createExport({ name: ['Pet'], path: './petStore' })],\n * })\n * // file.id = SHA256 hash of 'src/models/petStore.ts'\n * // file.name = 'petStore'\n * // file.extname = '.ts'\n * ```\n */\nexport function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {\n const rawExtname = path.extname(input.baseName)\n // Handle dotfile basename like '.ts' where path.extname returns ''\n const extname = (rawExtname || (input.baseName.startsWith('.') ? input.baseName : '')) as `.${string}`\n if (!extname) {\n throw new Error(`No extname found for ${input.baseName}`)\n }\n\n const source = (input.sources ?? [])\n .flatMap((item) => item.nodes ?? [])\n .map((node) => extractStringsFromNodes([node]))\n .filter(Boolean)\n .join('\\n\\n')\n const resolvedExports = input.exports?.length ? combineExports(input.exports) : []\n const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || undefined) : []\n const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name): name is string => Boolean(name)))\n const nameOf = (item: string | { propertyName: string; name?: string }): string => (typeof item === 'string' ? item : (item.name ?? item.propertyName))\n // Drop self-imports. Consolidating output (`mode: 'file'`) can place a symbol's\n // definition and a cross-file import of it in the same file. The first pass catches imports that\n // resolve to this file's own path. The second drops imports of names the file already defines,\n // the case consolidation produces when the import path no longer matches `input.path`. Sources\n // stay intact, so the local definition remains. Bare specifiers like `'zod'` never match a path.\n const resolvedImports = combinedImports\n .filter((imp) => imp.path !== input.path)\n .flatMap((imp) => {\n if (!Array.isArray(imp.name)) {\n return typeof imp.name === 'string' && localNames.has(imp.name) ? [] : [imp]\n }\n const kept = imp.name.filter((item) => !localNames.has(nameOf(item)))\n if (!kept.length) return []\n return [kept.length === imp.name.length ? imp : { ...imp, name: kept }]\n })\n const resolvedSources = input.sources?.length ? combineSources(input.sources) : []\n\n return {\n kind: 'File',\n ...input,\n id: hash('sha256', input.path, 'hex'),\n name: trimExtName(input.baseName),\n extname,\n imports: resolvedImports,\n exports: resolvedExports,\n sources: resolvedSources,\n meta: input.meta ?? ({} as TMeta),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA8LA,SAAgB,YAAY,MAAsB;CAChD,MAAM,WAAW,KAAK,YAAY,GAAG;CACrC,IAAI,WAAW,KAAK,CAAC,KAAK,SAAS,KAAK,QAAQ,GAC9C,OAAO,KAAK,MAAM,GAAG,QAAQ;CAE/B,OAAO;AACT;;;AC1LA,SAAS,UAAU,QAA4B;CAE7C,OAAO,GADS,OAAO,QAAQA,gCAAAA,wBAAwB,OAAO,KAAK,EACjD,GAAG,OAAO,gBAAgB,MAAM,GAAG,OAAO,cAAc;AAC5E;AAEA,SAAS,YAAY,MAAc,YAAgD;CACjF,OAAO,GAAG,KAAK,GAAG,cAAc;AAClC;AAEA,SAAS,UAAU,MAAc,MAAiC,YAAwC,SAA6C;CACrJ,OAAO,GAAG,KAAK,GAAG,QAAQ,GAAG,GAAG,cAAc,MAAM,GAAG,WAAW;AACpE;AAEA,SAAS,UAAU,MAAc,MAAiC,YAAgD;CAChH,OAAO,GAAG,KAAK,GAAG,QAAQ,GAAG,GAAG,cAAc;AAChD;;;;;AAMA,SAA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AAO,IAAI,KAAK,QAAQ,SAAS,CAAC,WAAW,IAAI,OAAO,IAAI,CAAC,CAAC;EACpE,IAAI,CAAC,KAAK,QAAQ,OAAO,CAAC;EAC1B,OAAO,CAAC,KAAK,WAAW,IAAI,KAAK,SAAS,MAAM;GAAE,GAAG;GAAK,MAAM;EAAK,CAAC;CACxE,CAAC;CACH,MAAM,kBAAkB,MAAM,SAAS,SAAS,eAAe,MAAM,OAAO,IAAI,CAAC;CAEjF,OAAO;EACL,MAAM;EACN,GAAG;EACH,KAAA,GAAA,YAAA,KAAA,CAAS,UAAU,MAAM,MAAM,KAAK;EACpC,MAAM,YAAY,MAAM,QAAQ;EAChC;EACA,SAAS;EACT,SAAS;EACT,SAAS;EACT,MAAM,MAAM,QAAS,CAAC;CACxB;AACF"}

Sorry, the diff of this file is too big to display

import { n as __name } from "./chunk-CNktS9qV.js";
import { A as FunctionParametersNode, N as TypeExpression, P as TypeLiteralNode, f as OperationNode, k as FunctionParameterNode, w as ParameterNode } from "./index-BzjwdK2M.js";
//#region src/utils/operationParams.d.ts
declare function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
/**
* Named type for a group of parameters (query or header) emitted as a single typed parameter.
*/
type ParamGroupType = {
/**
* Type expression for the group, a plain group-name reference.
*/
type: TypeExpression;
/**
* Whether the parameter group is optional.
*/
optional: boolean;
};
/**
* Resolver interface for {@link createOperationParams}.
*
* `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.
*/
type OperationParamsResolver = {
/**
* Resolves the type name for an individual parameter.
*
* @example Individual path parameter name
* `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
*/
resolveParamName(node: OperationNode, param: ParameterNode): string;
/**
* Resolves the request body type name.
*
* @example Request body type name
* `resolver.resolveDataName(node) // → 'CreatePetData'`
*/
resolveDataName(node: OperationNode): string;
/**
* Resolves the grouped path parameters type name.
* When the return value equals `resolveParamName`, no indexed access is emitted.
*
* @example Grouped path params type name
* `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
*/
resolvePathParamsName(node: OperationNode, param: ParameterNode): string;
/**
* Resolves the grouped query parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped query params type name
* `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
*/
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string;
/**
* Resolves the grouped header parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped header params type name
* `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
*/
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string;
};
/**
* Options for {@link createOperationParams}.
*/
type CreateOperationParamsOptions = {
/**
* How all operation parameters are grouped in the function signature.
* - `'object'` wraps all params into a single destructured object `{ petId, data, params }`
* - `'inline'` emits each param category as a separate top-level parameter
*/
paramsType: 'object' | 'inline';
/**
* How path parameters are emitted when `paramsType` is `'inline'`.
* - `'object'` groups them as `{ petId, storeId }: PathParams`
* - `'inline'` spreads them as individual parameters `petId: string, storeId: string`
* - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`
*/
pathParamsType: 'object' | 'inline' | 'inlineSpread';
/**
* Converts parameter names to camelCase before output.
*/
paramsCasing?: 'camelcase';
/**
* Resolver for parameter and request body type names.
* Pass `ResolverTs` from `@kubb/plugin-ts` directly.
* When omitted, falls back to the schema primitive or `'unknown'`.
*/
resolver?: OperationParamsResolver;
/**
* Default value for the path parameters binding when `pathParamsType` is `'object'`.
* Falls back to `'{}'` when all path params are optional.
*/
pathParamsDefault?: string;
/**
* Extra parameters appended after the standard operation parameters.
*
* @example Plugin-specific trailing parameter
* ```ts
* extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]
* ```
*/
extraParams?: Array<FunctionParameterNode>;
/**
* Override the default parameter names used for body, query, header, and rest-path groups.
*
* Useful when targeting languages or frameworks with different naming conventions.
*
* @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }
*/
paramNames?: {
/**
* Name for the request body parameter.
* @default 'data'
*/
data?: string;
/**
* Name for the query parameters group parameter.
* @default 'params'
*/
params?: string;
/**
* Name for the header parameters group parameter.
* @default 'headers'
*/
headers?: string;
/**
* Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.
* @default 'pathParams'
*/
path?: string;
};
/**
* Transforms every resolved type name before it lands in a parameter node, for framework-level
* type wrappers.
*
* @example Vue Query, wrap every parameter type with `MaybeRefOrGetter`
* `typeWrapper: (t) => \`MaybeRefOrGetter<${t}>\``
*/
typeWrapper?: (type: string) => string;
};
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, it falls back to the schema primitive (a plain type-name string). When the
* parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like
* `GroupParams['petId']`, otherwise the resolved individual name.
*/
declare function resolveParamType({
node,
param,
resolver
}: {
node: OperationNode;
param: ParameterNode;
resolver: OperationParamsResolver | undefined;
}): TypeExpression;
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
declare function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode;
/**
* Shared arguments for building a query or header parameter group.
*/
type BuildGroupArgs = {
name: string;
node: OperationNode;
params: Array<ParameterNode>;
groupType: ParamGroupType | null;
resolver: OperationParamsResolver | undefined;
wrapType: (type: string) => string;
};
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
declare function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode>;
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
declare function buildTypeLiteral({
node,
params,
resolver
}: {
node: OperationNode;
params: Array<ParameterNode>;
resolver: OperationParamsResolver | undefined;
}): TypeLiteralNode;
//#endregion
export { buildTypeLiteral as a, resolveParamType as c, buildGroupParam as i, OperationParamsResolver as n, caseParams as o, ParamGroupType as r, createOperationParams as s, BuildGroupArgs as t };
//# sourceMappingURL=operationParams-BZ07xDm0.d.ts.map
//#region \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
value,
configurable: true
});
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __exportAll = (all, no_symbols) => {
let target = {};
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
return target;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
//#region src/node.ts
/**
* Builds a type guard that matches nodes of the given `kind`.
*/
function isKind(kind) {
return (node) => node.kind === kind;
}
/**
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
* object properties combine "required" and "nullable" into a single AST.
*
* - Non-required + non-nullable → `optional: true`.
* - Non-required + nullable → `nullish: true`.
* - Required → both flags cleared.
*/
function syncOptionality(schema, required) {
const nullable = schema.nullable ?? false;
return {
...schema,
optional: !required && !nullable ? true : void 0,
nullish: !required && nullable ? true : void 0
};
}
/**
* Defines a node once and derives its `create` builder, `is` guard, and traversal
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
* `kind`, so node construction lives in one place without scattered `as` casts.
*
* Set `rebuild: true` when the `build` hook derives fields from children. After a
* transform rewrites those children, the registry reruns `create` so the derived
* fields stay correct.
*
* @example Simple node
* ```ts
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
* const createImport = importDef.create
* ```
*
* @example Node with a build hook that is rerun on transform
* ```ts
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
* kind: 'Property',
* build: (props) => ({ ...props, required: props.required ?? false }),
* children: ['schema'],
* visitorKey: 'property',
* rebuild: true,
* })
* ```
*/
function defineNode(config) {
const { kind, defaults, build, children, visitorKey, rebuild } = config;
function create(input) {
const base = build ? build(input) : input;
return {
...defaults,
...base,
kind
};
}
return {
kind,
create,
is: isKind(kind),
children,
visitorKey,
rebuild
};
}
//#endregion
//#region src/nodes/schema.ts
/**
* Maps schema `type` to its underlying `primitive`.
* Primitive types map to themselves. Special string formats map to `'string'`.
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
*/
const TYPE_TO_PRIMITIVE = {
string: "string",
number: "number",
integer: "integer",
bigint: "bigint",
boolean: "boolean",
null: "null",
any: "any",
unknown: "unknown",
void: "void",
never: "never",
object: "object",
array: "array",
date: "date",
uuid: "string",
email: "string",
url: "string",
datetime: "string",
time: "string"
};
/**
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
*/
const schemaDef = defineNode({
kind: "Schema",
build: (props) => {
if (props.type === "object") return {
properties: [],
primitive: "object",
...props
};
return {
primitive: TYPE_TO_PRIMITIVE[props.type],
...props
};
},
children: [
"properties",
"items",
"members",
"additionalProperties"
],
visitorKey: "schema"
});
function createSchema(props) {
return schemaDef.create(props);
}
//#endregion
//#region ../../internals/utils/src/casing.ts
/**
* Shared implementation for camelCase and PascalCase conversion.
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
* and capitalizes each word according to `pascal`.
*
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
*/
function toCamelOrPascal(text, pascal) {
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
if (word.length > 1 && word === word.toUpperCase()) return word;
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
}).join("").replace(/[^a-zA-Z0-9]/g, "");
}
/**
* Converts `text` to camelCase.
*
* @example Word boundaries
* `camelCase('hello-world') // 'helloWorld'`
*
* @example With a prefix
* `camelCase('tag', { prefix: 'create' }) // 'createTag'`
*/
function camelCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
}
/**
* Converts `text` to PascalCase.
*
* @example Word boundaries
* `pascalCase('hello-world') // 'HelloWorld'`
*
* @example With a suffix
* `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
*/
function pascalCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
}
//#endregion
//#region src/nodes/code.ts
/**
* Definition for the {@link ConstNode}.
*/
const constDef = defineNode({ kind: "Const" });
/**
* Creates a `ConstNode` representing a TypeScript `const` declaration.
*
* @example Exported constant with type and `as const`
* ```ts
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
* // export const pets: Pet[] = ... as const
* ```
*/
const createConst = constDef.create;
/**
* Definition for the {@link TypeNode}.
*/
const typeDef = defineNode({ kind: "Type" });
/**
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
*
* @example
* ```ts
* createType({ name: 'Pet', export: true })
* // export type Pet = ...
* ```
*/
const createType = typeDef.create;
/**
* Definition for the {@link FunctionNode}.
*/
const functionDef = defineNode({ kind: "Function" });
/**
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
*
* @example
* ```ts
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
* // export async function fetchPet(): Promise<Pet> { ... }
* ```
*/
const createFunction = functionDef.create;
/**
* Definition for the {@link ArrowFunctionNode}.
*/
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
/**
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
*
* @example
* ```ts
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
* // export const double = (n: number) => ...
* ```
*/
const createArrowFunction = arrowFunctionDef.create;
/**
* Definition for the {@link TextNode}.
*/
const textDef = defineNode({
kind: "Text",
build: (value) => ({ value })
});
/**
* Creates a {@link TextNode} representing a raw string fragment in the source output.
*
* @example
* ```ts
* createText('return fetch(id)')
* // { kind: 'Text', value: 'return fetch(id)' }
* ```
*/
const createText = textDef.create;
/**
* Definition for the {@link BreakNode}.
*/
const breakDef = defineNode({
kind: "Break",
build: () => ({})
});
/**
* Creates a {@link BreakNode} representing a line break in the source output.
*
* @example
* ```ts
* createBreak()
* // { kind: 'Break' }
* ```
*/
function createBreak() {
return breakDef.create();
}
/**
* Definition for the {@link JsxNode}.
*/
const jsxDef = defineNode({
kind: "Jsx",
build: (value) => ({ value })
});
/**
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
*
* @example
* ```ts
* createJsx('<>\n <a href={href}>Open</a>\n</>')
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
* ```
*/
const createJsx = jsxDef.create;
//#endregion
//#region src/nodes/content.ts
/**
* Definition for the {@link ContentNode}.
*/
const contentDef = defineNode({
kind: "Content",
children: ["schema"]
});
/**
* Creates a `ContentNode` for a single request-body or response content type.
*/
const createContent = contentDef.create;
//#endregion
//#region src/nodes/file.ts
/**
* Definition for the {@link ImportNode}.
*/
const importDef = defineNode({ kind: "Import" });
/**
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
*
* @example Named import
* ```ts
* createImport({ name: ['useState'], path: 'react' })
* // import { useState } from 'react'
* ```
*/
const createImport = importDef.create;
/**
* Definition for the {@link ExportNode}.
*/
const exportDef = defineNode({ kind: "Export" });
/**
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
*
* @example Named export
* ```ts
* createExport({ name: ['Pet'], path: './Pet' })
* // export { Pet } from './Pet'
* ```
*/
const createExport = exportDef.create;
/**
* Definition for the {@link SourceNode}.
*/
const sourceDef = defineNode({ kind: "Source" });
/**
* Creates a `SourceNode` representing a fragment of source code within a file.
*
* @example
* ```ts
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
* ```
*/
const createSource = sourceDef.create;
/**
* Definition for the {@link FileNode}. The fully resolved builder lives in
* `createFile`, so this definition only supplies the guard.
*/
const fileDef = defineNode({ kind: "File" });
//#endregion
//#region src/nodes/function.ts
/**
* Definition for the {@link TypeLiteralNode}.
*/
const typeLiteralDef = defineNode({ kind: "TypeLiteral" });
/**
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
*
* @example
* ```ts
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
* // { petId: string }
* ```
*/
const createTypeLiteral = typeLiteralDef.create;
/**
* Definition for the {@link IndexedAccessTypeNode}.
*/
const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" });
/**
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
*
* @example
* ```ts
* createIndexedAccessType({ objectType: 'DeletePetPathParams', indexType: 'petId' })
* // DeletePetPathParams['petId']
* ```
*/
const createIndexedAccessType = indexedAccessTypeDef.create;
/**
* Definition for the {@link ObjectBindingPatternNode}.
*/
const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" });
/**
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
*
* @example
* ```ts
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
* // { id, name }
* ```
*/
const createObjectBindingPattern = objectBindingPatternDef.create;
/**
* Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`.
* Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name
* paired with a {@link TypeLiteralNode} type.
*/
const functionParameterDef = defineNode({
kind: "FunctionParameter",
build: (input) => {
if ("properties" in input) return {
name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }),
type: createTypeLiteral({ members: input.properties.map((p) => ({
name: p.name,
type: p.type,
optional: p.optional ?? false
})) }),
optional: input.optional ?? false,
...input.default !== void 0 ? { default: input.default } : {}
};
return {
optional: false,
...input
};
}
});
/**
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.
*
* @example Optional param
* ```ts
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
* // → params?: QueryParams
* ```
*
* @example Destructured group
* ```ts
* createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' })
* // → { id, name }: { id: string; name?: string } = {}
* ```
*/
const createFunctionParameter = functionParameterDef.create;
/**
* Definition for the {@link FunctionParametersNode}.
*/
const functionParametersDef = defineNode({
kind: "FunctionParameters",
defaults: { params: [] }
});
/**
* Creates a `FunctionParametersNode` from an ordered list of parameters.
*
* @example
* ```ts
* const empty = createFunctionParameters()
* // { kind: 'FunctionParameters', params: [] }
* ```
*/
function createFunctionParameters(props = {}) {
return functionParametersDef.create(props);
}
//#endregion
//#region src/nodes/input.ts
/**
* Definition for the {@link InputNode}.
*/
const inputDef = defineNode({
kind: "Input",
defaults: {
schemas: [],
operations: [],
meta: {
circularNames: [],
enumNames: []
}
},
children: ["schemas", "operations"],
visitorKey: "input"
});
/**
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
* `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the
* eager variant with array `schemas`/`operations` and the defaulted `meta`.
*
* @example Eager
* ```ts
* const input = createInput()
* // { kind: 'Input', schemas: [], operations: [] }
* ```
*
* @example Streaming
* ```ts
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
* ```
*/
function createInput(options = {}) {
const { stream, ...overrides } = options;
if (stream) return {
kind: "Input",
...overrides
};
return inputDef.create(overrides);
}
//#endregion
//#region src/nodes/requestBody.ts
/**
* Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`.
*/
const requestBodyDef = defineNode({
kind: "RequestBody",
build: (props) => ({
...props,
content: props.content?.map(createContent)
}),
children: ["content"]
});
/**
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
*/
const createRequestBody = requestBodyDef.create;
//#endregion
//#region src/nodes/operation.ts
/**
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
* normalized into a `RequestBodyNode`.
*/
const operationDef = defineNode({
kind: "Operation",
build: (props) => {
const { requestBody, ...rest } = props;
const isHttp = rest.method !== void 0 && rest.path !== void 0;
return {
tags: [],
parameters: [],
responses: [],
...rest,
...isHttp ? { protocol: "http" } : {},
requestBody: requestBody ? createRequestBody(requestBody) : void 0
};
},
children: [
"parameters",
"requestBody",
"responses"
],
visitorKey: "operation"
});
function createOperation(props) {
return operationDef.create(props);
}
//#endregion
//#region src/nodes/output.ts
/**
* Definition for the {@link OutputNode}.
*/
const outputDef = defineNode({
kind: "Output",
defaults: { files: [] },
visitorKey: "output"
});
/**
* Creates an `OutputNode` with a stable default for `files`.
*
* @example
* ```ts
* const output = createOutput()
* // { kind: 'Output', files: [] }
* ```
*/
function createOutput(overrides = {}) {
return outputDef.create(overrides);
}
//#endregion
//#region src/nodes/parameter.ts
/**
* Definition for the {@link ParameterNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const parameterDef = defineNode({
kind: "Parameter",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "parameter",
rebuild: true
});
/**
* Creates a `ParameterNode`.
*
* @example
* ```ts
* const param = createParameter({
* name: 'petId',
* in: 'path',
* required: true,
* schema: createSchema({ type: 'string' }),
* })
* ```
*/
const createParameter = parameterDef.create;
//#endregion
//#region src/nodes/property.ts
/**
* Definition for the {@link PropertyNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const propertyDef = defineNode({
kind: "Property",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "property",
rebuild: true
});
/**
* Creates a `PropertyNode`.
*
* @example
* ```ts
* const property = createProperty({
* name: 'status',
* required: true,
* schema: createSchema({ type: 'string', nullable: true }),
* })
* // required=true, no optional/nullish
* ```
*/
const createProperty = propertyDef.create;
//#endregion
//#region src/nodes/response.ts
/**
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
*/
const responseDef = defineNode({
kind: "Response",
build: (props) => {
const { schema, mediaType, keysToOmit, content, ...rest } = props;
const entries = content ?? (schema ? [{
contentType: mediaType ?? "application/json",
schema,
keysToOmit: keysToOmit ?? null
}] : void 0);
return {
...rest,
content: entries?.map(createContent)
};
},
children: ["content"],
visitorKey: "response"
});
/**
* Creates a `ResponseNode`.
*
* @example
* ```ts
* const response = createResponse({
* statusCode: '200',
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }],
* })
* ```
*/
const createResponse = responseDef.create;
//#endregion
Object.defineProperty(exports, "__exportAll", {
enumerable: true,
get: function() {
return __exportAll;
}
});
Object.defineProperty(exports, "__name", {
enumerable: true,
get: function() {
return __name;
}
});
Object.defineProperty(exports, "__toESM", {
enumerable: true,
get: function() {
return __toESM;
}
});
Object.defineProperty(exports, "arrowFunctionDef", {
enumerable: true,
get: function() {
return arrowFunctionDef;
}
});
Object.defineProperty(exports, "breakDef", {
enumerable: true,
get: function() {
return breakDef;
}
});
Object.defineProperty(exports, "camelCase", {
enumerable: true,
get: function() {
return camelCase;
}
});
Object.defineProperty(exports, "constDef", {
enumerable: true,
get: function() {
return constDef;
}
});
Object.defineProperty(exports, "contentDef", {
enumerable: true,
get: function() {
return contentDef;
}
});
Object.defineProperty(exports, "createArrowFunction", {
enumerable: true,
get: function() {
return createArrowFunction;
}
});
Object.defineProperty(exports, "createBreak", {
enumerable: true,
get: function() {
return createBreak;
}
});
Object.defineProperty(exports, "createConst", {
enumerable: true,
get: function() {
return createConst;
}
});
Object.defineProperty(exports, "createContent", {
enumerable: true,
get: function() {
return createContent;
}
});
Object.defineProperty(exports, "createExport", {
enumerable: true,
get: function() {
return createExport;
}
});
Object.defineProperty(exports, "createFunction", {
enumerable: true,
get: function() {
return createFunction;
}
});
Object.defineProperty(exports, "createFunctionParameter", {
enumerable: true,
get: function() {
return createFunctionParameter;
}
});
Object.defineProperty(exports, "createFunctionParameters", {
enumerable: true,
get: function() {
return createFunctionParameters;
}
});
Object.defineProperty(exports, "createImport", {
enumerable: true,
get: function() {
return createImport;
}
});
Object.defineProperty(exports, "createIndexedAccessType", {
enumerable: true,
get: function() {
return createIndexedAccessType;
}
});
Object.defineProperty(exports, "createInput", {
enumerable: true,
get: function() {
return createInput;
}
});
Object.defineProperty(exports, "createJsx", {
enumerable: true,
get: function() {
return createJsx;
}
});
Object.defineProperty(exports, "createObjectBindingPattern", {
enumerable: true,
get: function() {
return createObjectBindingPattern;
}
});
Object.defineProperty(exports, "createOperation", {
enumerable: true,
get: function() {
return createOperation;
}
});
Object.defineProperty(exports, "createOutput", {
enumerable: true,
get: function() {
return createOutput;
}
});
Object.defineProperty(exports, "createParameter", {
enumerable: true,
get: function() {
return createParameter;
}
});
Object.defineProperty(exports, "createProperty", {
enumerable: true,
get: function() {
return createProperty;
}
});
Object.defineProperty(exports, "createRequestBody", {
enumerable: true,
get: function() {
return createRequestBody;
}
});
Object.defineProperty(exports, "createResponse", {
enumerable: true,
get: function() {
return createResponse;
}
});
Object.defineProperty(exports, "createSchema", {
enumerable: true,
get: function() {
return createSchema;
}
});
Object.defineProperty(exports, "createSource", {
enumerable: true,
get: function() {
return createSource;
}
});
Object.defineProperty(exports, "createText", {
enumerable: true,
get: function() {
return createText;
}
});
Object.defineProperty(exports, "createType", {
enumerable: true,
get: function() {
return createType;
}
});
Object.defineProperty(exports, "createTypeLiteral", {
enumerable: true,
get: function() {
return createTypeLiteral;
}
});
Object.defineProperty(exports, "defineNode", {
enumerable: true,
get: function() {
return defineNode;
}
});
Object.defineProperty(exports, "exportDef", {
enumerable: true,
get: function() {
return exportDef;
}
});
Object.defineProperty(exports, "fileDef", {
enumerable: true,
get: function() {
return fileDef;
}
});
Object.defineProperty(exports, "functionDef", {
enumerable: true,
get: function() {
return functionDef;
}
});
Object.defineProperty(exports, "functionParameterDef", {
enumerable: true,
get: function() {
return functionParameterDef;
}
});
Object.defineProperty(exports, "functionParametersDef", {
enumerable: true,
get: function() {
return functionParametersDef;
}
});
Object.defineProperty(exports, "importDef", {
enumerable: true,
get: function() {
return importDef;
}
});
Object.defineProperty(exports, "indexedAccessTypeDef", {
enumerable: true,
get: function() {
return indexedAccessTypeDef;
}
});
Object.defineProperty(exports, "inputDef", {
enumerable: true,
get: function() {
return inputDef;
}
});
Object.defineProperty(exports, "jsxDef", {
enumerable: true,
get: function() {
return jsxDef;
}
});
Object.defineProperty(exports, "objectBindingPatternDef", {
enumerable: true,
get: function() {
return objectBindingPatternDef;
}
});
Object.defineProperty(exports, "operationDef", {
enumerable: true,
get: function() {
return operationDef;
}
});
Object.defineProperty(exports, "outputDef", {
enumerable: true,
get: function() {
return outputDef;
}
});
Object.defineProperty(exports, "parameterDef", {
enumerable: true,
get: function() {
return parameterDef;
}
});
Object.defineProperty(exports, "pascalCase", {
enumerable: true,
get: function() {
return pascalCase;
}
});
Object.defineProperty(exports, "propertyDef", {
enumerable: true,
get: function() {
return propertyDef;
}
});
Object.defineProperty(exports, "requestBodyDef", {
enumerable: true,
get: function() {
return requestBodyDef;
}
});
Object.defineProperty(exports, "responseDef", {
enumerable: true,
get: function() {
return responseDef;
}
});
Object.defineProperty(exports, "schemaDef", {
enumerable: true,
get: function() {
return schemaDef;
}
});
Object.defineProperty(exports, "sourceDef", {
enumerable: true,
get: function() {
return sourceDef;
}
});
Object.defineProperty(exports, "syncOptionality", {
enumerable: true,
get: function() {
return syncOptionality;
}
});
Object.defineProperty(exports, "textDef", {
enumerable: true,
get: function() {
return textDef;
}
});
Object.defineProperty(exports, "typeDef", {
enumerable: true,
get: function() {
return typeDef;
}
});
Object.defineProperty(exports, "typeLiteralDef", {
enumerable: true,
get: function() {
return typeLiteralDef;
}
});
//# sourceMappingURL=response-hnSw2NKE.cjs.map

Sorry, the diff of this file is too big to display

import "./chunk-CNktS9qV.js";
//#region src/node.ts
/**
* Builds a type guard that matches nodes of the given `kind`.
*/
function isKind(kind) {
return (node) => node.kind === kind;
}
/**
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
* object properties combine "required" and "nullable" into a single AST.
*
* - Non-required + non-nullable → `optional: true`.
* - Non-required + nullable → `nullish: true`.
* - Required → both flags cleared.
*/
function syncOptionality(schema, required) {
const nullable = schema.nullable ?? false;
return {
...schema,
optional: !required && !nullable ? true : void 0,
nullish: !required && nullable ? true : void 0
};
}
/**
* Defines a node once and derives its `create` builder, `is` guard, and traversal
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
* `kind`, so node construction lives in one place without scattered `as` casts.
*
* Set `rebuild: true` when the `build` hook derives fields from children. After a
* transform rewrites those children, the registry reruns `create` so the derived
* fields stay correct.
*
* @example Simple node
* ```ts
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
* const createImport = importDef.create
* ```
*
* @example Node with a build hook that is rerun on transform
* ```ts
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
* kind: 'Property',
* build: (props) => ({ ...props, required: props.required ?? false }),
* children: ['schema'],
* visitorKey: 'property',
* rebuild: true,
* })
* ```
*/
function defineNode(config) {
const { kind, defaults, build, children, visitorKey, rebuild } = config;
function create(input) {
const base = build ? build(input) : input;
return {
...defaults,
...base,
kind
};
}
return {
kind,
create,
is: isKind(kind),
children,
visitorKey,
rebuild
};
}
//#endregion
//#region src/nodes/schema.ts
/**
* Maps schema `type` to its underlying `primitive`.
* Primitive types map to themselves. Special string formats map to `'string'`.
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
*/
const TYPE_TO_PRIMITIVE = {
string: "string",
number: "number",
integer: "integer",
bigint: "bigint",
boolean: "boolean",
null: "null",
any: "any",
unknown: "unknown",
void: "void",
never: "never",
object: "object",
array: "array",
date: "date",
uuid: "string",
email: "string",
url: "string",
datetime: "string",
time: "string"
};
/**
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
*/
const schemaDef = defineNode({
kind: "Schema",
build: (props) => {
if (props.type === "object") return {
properties: [],
primitive: "object",
...props
};
return {
primitive: TYPE_TO_PRIMITIVE[props.type],
...props
};
},
children: [
"properties",
"items",
"members",
"additionalProperties"
],
visitorKey: "schema"
});
function createSchema(props) {
return schemaDef.create(props);
}
//#endregion
//#region ../../internals/utils/src/casing.ts
/**
* Shared implementation for camelCase and PascalCase conversion.
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
* and capitalizes each word according to `pascal`.
*
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
*/
function toCamelOrPascal(text, pascal) {
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
if (word.length > 1 && word === word.toUpperCase()) return word;
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
}).join("").replace(/[^a-zA-Z0-9]/g, "");
}
/**
* Converts `text` to camelCase.
*
* @example Word boundaries
* `camelCase('hello-world') // 'helloWorld'`
*
* @example With a prefix
* `camelCase('tag', { prefix: 'create' }) // 'createTag'`
*/
function camelCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
}
/**
* Converts `text` to PascalCase.
*
* @example Word boundaries
* `pascalCase('hello-world') // 'HelloWorld'`
*
* @example With a suffix
* `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
*/
function pascalCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
}
//#endregion
//#region src/nodes/code.ts
/**
* Definition for the {@link ConstNode}.
*/
const constDef = defineNode({ kind: "Const" });
/**
* Creates a `ConstNode` representing a TypeScript `const` declaration.
*
* @example Exported constant with type and `as const`
* ```ts
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
* // export const pets: Pet[] = ... as const
* ```
*/
const createConst = constDef.create;
/**
* Definition for the {@link TypeNode}.
*/
const typeDef = defineNode({ kind: "Type" });
/**
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
*
* @example
* ```ts
* createType({ name: 'Pet', export: true })
* // export type Pet = ...
* ```
*/
const createType = typeDef.create;
/**
* Definition for the {@link FunctionNode}.
*/
const functionDef = defineNode({ kind: "Function" });
/**
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
*
* @example
* ```ts
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
* // export async function fetchPet(): Promise<Pet> { ... }
* ```
*/
const createFunction = functionDef.create;
/**
* Definition for the {@link ArrowFunctionNode}.
*/
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
/**
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
*
* @example
* ```ts
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
* // export const double = (n: number) => ...
* ```
*/
const createArrowFunction = arrowFunctionDef.create;
/**
* Definition for the {@link TextNode}.
*/
const textDef = defineNode({
kind: "Text",
build: (value) => ({ value })
});
/**
* Creates a {@link TextNode} representing a raw string fragment in the source output.
*
* @example
* ```ts
* createText('return fetch(id)')
* // { kind: 'Text', value: 'return fetch(id)' }
* ```
*/
const createText = textDef.create;
/**
* Definition for the {@link BreakNode}.
*/
const breakDef = defineNode({
kind: "Break",
build: () => ({})
});
/**
* Creates a {@link BreakNode} representing a line break in the source output.
*
* @example
* ```ts
* createBreak()
* // { kind: 'Break' }
* ```
*/
function createBreak() {
return breakDef.create();
}
/**
* Definition for the {@link JsxNode}.
*/
const jsxDef = defineNode({
kind: "Jsx",
build: (value) => ({ value })
});
/**
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
*
* @example
* ```ts
* createJsx('<>\n <a href={href}>Open</a>\n</>')
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
* ```
*/
const createJsx = jsxDef.create;
//#endregion
//#region src/nodes/content.ts
/**
* Definition for the {@link ContentNode}.
*/
const contentDef = defineNode({
kind: "Content",
children: ["schema"]
});
/**
* Creates a `ContentNode` for a single request-body or response content type.
*/
const createContent = contentDef.create;
//#endregion
//#region src/nodes/file.ts
/**
* Definition for the {@link ImportNode}.
*/
const importDef = defineNode({ kind: "Import" });
/**
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
*
* @example Named import
* ```ts
* createImport({ name: ['useState'], path: 'react' })
* // import { useState } from 'react'
* ```
*/
const createImport = importDef.create;
/**
* Definition for the {@link ExportNode}.
*/
const exportDef = defineNode({ kind: "Export" });
/**
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
*
* @example Named export
* ```ts
* createExport({ name: ['Pet'], path: './Pet' })
* // export { Pet } from './Pet'
* ```
*/
const createExport = exportDef.create;
/**
* Definition for the {@link SourceNode}.
*/
const sourceDef = defineNode({ kind: "Source" });
/**
* Creates a `SourceNode` representing a fragment of source code within a file.
*
* @example
* ```ts
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
* ```
*/
const createSource = sourceDef.create;
/**
* Definition for the {@link FileNode}. The fully resolved builder lives in
* `createFile`, so this definition only supplies the guard.
*/
const fileDef = defineNode({ kind: "File" });
//#endregion
//#region src/nodes/function.ts
/**
* Definition for the {@link TypeLiteralNode}.
*/
const typeLiteralDef = defineNode({ kind: "TypeLiteral" });
/**
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
*
* @example
* ```ts
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
* // { petId: string }
* ```
*/
const createTypeLiteral = typeLiteralDef.create;
/**
* Definition for the {@link IndexedAccessTypeNode}.
*/
const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" });
/**
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
*
* @example
* ```ts
* createIndexedAccessType({ objectType: 'DeletePetPathParams', indexType: 'petId' })
* // DeletePetPathParams['petId']
* ```
*/
const createIndexedAccessType = indexedAccessTypeDef.create;
/**
* Definition for the {@link ObjectBindingPatternNode}.
*/
const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" });
/**
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
*
* @example
* ```ts
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
* // { id, name }
* ```
*/
const createObjectBindingPattern = objectBindingPatternDef.create;
/**
* Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`.
* Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name
* paired with a {@link TypeLiteralNode} type.
*/
const functionParameterDef = defineNode({
kind: "FunctionParameter",
build: (input) => {
if ("properties" in input) return {
name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }),
type: createTypeLiteral({ members: input.properties.map((p) => ({
name: p.name,
type: p.type,
optional: p.optional ?? false
})) }),
optional: input.optional ?? false,
...input.default !== void 0 ? { default: input.default } : {}
};
return {
optional: false,
...input
};
}
});
/**
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.
*
* @example Optional param
* ```ts
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
* // → params?: QueryParams
* ```
*
* @example Destructured group
* ```ts
* createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' })
* // → { id, name }: { id: string; name?: string } = {}
* ```
*/
const createFunctionParameter = functionParameterDef.create;
/**
* Definition for the {@link FunctionParametersNode}.
*/
const functionParametersDef = defineNode({
kind: "FunctionParameters",
defaults: { params: [] }
});
/**
* Creates a `FunctionParametersNode` from an ordered list of parameters.
*
* @example
* ```ts
* const empty = createFunctionParameters()
* // { kind: 'FunctionParameters', params: [] }
* ```
*/
function createFunctionParameters(props = {}) {
return functionParametersDef.create(props);
}
//#endregion
//#region src/nodes/input.ts
/**
* Definition for the {@link InputNode}.
*/
const inputDef = defineNode({
kind: "Input",
defaults: {
schemas: [],
operations: [],
meta: {
circularNames: [],
enumNames: []
}
},
children: ["schemas", "operations"],
visitorKey: "input"
});
/**
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
* `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the
* eager variant with array `schemas`/`operations` and the defaulted `meta`.
*
* @example Eager
* ```ts
* const input = createInput()
* // { kind: 'Input', schemas: [], operations: [] }
* ```
*
* @example Streaming
* ```ts
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
* ```
*/
function createInput(options = {}) {
const { stream, ...overrides } = options;
if (stream) return {
kind: "Input",
...overrides
};
return inputDef.create(overrides);
}
//#endregion
//#region src/nodes/requestBody.ts
/**
* Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`.
*/
const requestBodyDef = defineNode({
kind: "RequestBody",
build: (props) => ({
...props,
content: props.content?.map(createContent)
}),
children: ["content"]
});
/**
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
*/
const createRequestBody = requestBodyDef.create;
//#endregion
//#region src/nodes/operation.ts
/**
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
* normalized into a `RequestBodyNode`.
*/
const operationDef = defineNode({
kind: "Operation",
build: (props) => {
const { requestBody, ...rest } = props;
const isHttp = rest.method !== void 0 && rest.path !== void 0;
return {
tags: [],
parameters: [],
responses: [],
...rest,
...isHttp ? { protocol: "http" } : {},
requestBody: requestBody ? createRequestBody(requestBody) : void 0
};
},
children: [
"parameters",
"requestBody",
"responses"
],
visitorKey: "operation"
});
function createOperation(props) {
return operationDef.create(props);
}
//#endregion
//#region src/nodes/output.ts
/**
* Definition for the {@link OutputNode}.
*/
const outputDef = defineNode({
kind: "Output",
defaults: { files: [] },
visitorKey: "output"
});
/**
* Creates an `OutputNode` with a stable default for `files`.
*
* @example
* ```ts
* const output = createOutput()
* // { kind: 'Output', files: [] }
* ```
*/
function createOutput(overrides = {}) {
return outputDef.create(overrides);
}
//#endregion
//#region src/nodes/parameter.ts
/**
* Definition for the {@link ParameterNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const parameterDef = defineNode({
kind: "Parameter",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "parameter",
rebuild: true
});
/**
* Creates a `ParameterNode`.
*
* @example
* ```ts
* const param = createParameter({
* name: 'petId',
* in: 'path',
* required: true,
* schema: createSchema({ type: 'string' }),
* })
* ```
*/
const createParameter = parameterDef.create;
//#endregion
//#region src/nodes/property.ts
/**
* Definition for the {@link PropertyNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const propertyDef = defineNode({
kind: "Property",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "property",
rebuild: true
});
/**
* Creates a `PropertyNode`.
*
* @example
* ```ts
* const property = createProperty({
* name: 'status',
* required: true,
* schema: createSchema({ type: 'string', nullable: true }),
* })
* // required=true, no optional/nullish
* ```
*/
const createProperty = propertyDef.create;
//#endregion
//#region src/nodes/response.ts
/**
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
*/
const responseDef = defineNode({
kind: "Response",
build: (props) => {
const { schema, mediaType, keysToOmit, content, ...rest } = props;
const entries = content ?? (schema ? [{
contentType: mediaType ?? "application/json",
schema,
keysToOmit: keysToOmit ?? null
}] : void 0);
return {
...rest,
content: entries?.map(createContent)
};
},
children: ["content"],
visitorKey: "response"
});
/**
* Creates a `ResponseNode`.
*
* @example
* ```ts
* const response = createResponse({
* statusCode: '200',
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }],
* })
* ```
*/
const createResponse = responseDef.create;
//#endregion
export { syncOptionality as $, importDef as A, createFunction as B, objectBindingPatternDef as C, createSource as D, createImport as E, breakDef as F, jsxDef as G, createText as H, constDef as I, camelCase as J, textDef as K, createArrowFunction as L, contentDef as M, createContent as N, exportDef as O, arrowFunctionDef as P, defineNode as Q, createBreak as R, indexedAccessTypeDef as S, createExport as T, createType as U, createJsx as V, functionDef as W, createSchema as X, pascalCase as Y, schemaDef as Z, createIndexedAccessType as _, createParameter as a, functionParameterDef as b, outputDef as c, createRequestBody as d, requestBodyDef as f, createFunctionParameters as g, createFunctionParameter as h, propertyDef as i, sourceDef as j, fileDef as k, createOperation as l, inputDef as m, responseDef as n, parameterDef as o, createInput as p, typeDef as q, createProperty as r, createOutput as s, createResponse as t, operationDef as u, createObjectBindingPattern as v, typeLiteralDef as w, functionParametersDef as x, createTypeLiteral as y, createConst as z };
//# sourceMappingURL=response-KUdWiDWw.js.map

Sorry, the diff of this file is too big to display

import { n as __name } from "./chunk-CNktS9qV.js";
import { _t as SchemaNode, f as OperationNode, t as Node, vt as SchemaNodeByType, yt as SchemaType } from "./index-BzjwdK2M.js";
//#region src/dedupe.d.ts
/**
* A canonical destination for a deduplicated shape: the shared schema name and
* the synthetic `$ref` path that points at it.
*/
type DedupeCanonical = {
/**
* Canonical schema name every duplicate occurrence refers to.
*/
name: string;
/**
* `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).
*/
ref: string;
};
/**
* The result of {@link buildDedupePlan}: a lookup from structural signature to its
* canonical target, plus the freshly hoisted definitions that must be added to
* the schema list.
*/
type DedupePlan = {
/**
* Maps a structural signature to the canonical schema that represents it.
*/
canonicalBySignature: Map<string, DedupeCanonical>;
/**
* Maps the name of a top-level schema that duplicates a canonical one to that canonical, so
* references to the duplicate can be repointed at the first schema with the same content.
*/
aliasNames: Map<string, DedupeCanonical>;
/**
* New top-level schema definitions created for inline shapes that had no existing
* named component. Nested duplicates inside each definition are already collapsed.
*/
hoisted: Array<SchemaNode>;
};
/**
* The lookups {@link applyDedupe} needs from a {@link DedupePlan}.
*/
type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>;
/**
* Options that inject the naming and candidate policy into {@link buildDedupePlan}.
* The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.
*/
type BuildDedupePlanOptions = {
/**
* Returns `true` when a node should be deduplicated. This is the only gate, so it must
* reject both ineligible kinds (return `false` for anything other than, say, enums and
* objects) and unsafe shapes (e.g. nodes that reference a circular schema).
*/
isCandidate: (node: SchemaNode) => boolean;
/**
* Produces the canonical name for an inline shape with no existing named component.
* Return `null` to leave the shape inline (for example when no contextual name exists).
*/
nameFor: (node: SchemaNode, signature: string) => string | null;
/**
* Builds the `$ref` path for a canonical name.
*/
refFor: (name: string) => string;
/**
* Minimum number of occurrences before a shape is deduplicated.
*
* @default 2
*/
minOccurrences?: number;
};
/**
* Rewrites a node, replacing every candidate sub-schema whose signature has a canonical
* target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,
* so nested duplicates inside a replaced shape are not visited again. A `ref` that points
* at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with
* the same content.
*
* Pass `skipRootMatch` when rewriting a canonical definition so its own root is not
* turned into a reference to itself. Nested duplicates are still collapsed.
*
* @example
* ```ts
* const next = applyDedupe(operationNode, plan)
* ```
*/
declare function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode;
declare function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode;
/**
* Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
*
* A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
* is a named top-level schema, the first one becomes the canonical (so other top-level
* duplicates and inline copies turn into references to it). Every other top-level name with
* the same content is recorded in `aliasNames`, so refs to it can be repointed at the
* canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied
* per node with {@link applyDedupe}.
*
* @example
* ```ts
* const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
* isCandidate: (node) => node.type === 'enum' || node.type === 'object',
* nameFor: (node) => node.name ?? null,
* refFor: (name) => `#/components/schemas/${name}`,
* })
* ```
*/
declare function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan;
//#endregion
//#region src/dialect.d.ts
/**
* The spec-specific questions a schema parser answers while turning a source document into Kubb
* AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where
* OpenAPI, AsyncAPI, and plain JSON Schema differ.
*/
type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {
/**
* Identifies the dialect in logs and diagnostics.
*/
name: string;
/**
* Whether the schema is nullable.
*/
isNullable: (schema?: TSchema) => boolean;
/**
* Whether the value is a `$ref` pointer.
*/
isReference: (value?: unknown) => value is TRef;
/**
* Whether the schema carries a discriminator for polymorphism.
*/
isDiscriminator: (value?: unknown) => value is TDiscriminated;
/**
* Whether the schema is binary data, converted to a `blob` node.
*/
isBinary: (schema: TSchema) => boolean;
/**
* Resolves a local `$ref` against the document, or nullish when it cannot.
*/
resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined;
};
/**
* Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the
* dialect's type for inference.
*
* @example
* ```ts
* export const oasDialect = defineSchemaDialect({
* name: 'oas',
* isNullable,
* isReference,
* isDiscriminator,
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
* resolveRef,
* })
* ```
*/
declare function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>;
//#endregion
//#region src/printer.d.ts
/**
* Runtime context passed as `this` to printer handlers.
*
* `this.transform` dispatches to node-level handlers from `nodes`.
*
* @example
* ```ts
* const context: PrinterHandlerContext<string, {}> = {
* options: {},
* transform: () => 'value',
* }
* ```
*/
type PrinterHandlerContext<TOutput, TOptions extends object> = {
/**
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
*/
transform: (node: SchemaNode) => TOutput | null;
/**
* Options for this printer instance.
*/
options: TOptions;
};
/**
* Handler for one schema node type.
*
* Use a regular function (not an arrow function) if you need `this`.
*
* @example
* ```ts
* const handler: PrinterHandler<string, {}, 'string'> = function () {
* return 'string'
* }
* ```
*/
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
/**
* Partial map of per-node-type handler overrides for a printer.
*
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
* Supply only the handlers you want to replace. The printer's built-in
* defaults fill in the rest.
*
* @example
* ```ts
* pluginZod({
* printer: {
* nodes: {
* date(): string {
* return 'z.string().date()'
* },
* } satisfies PrinterPartial<string, PrinterZodOptions>,
* },
* })
* ```
*/
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
/**
* Generic shape used by `definePrinter`.
*
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
* - `TOptions` options passed to and stored on the printer instance
* - `TOutput` the type emitted by node handlers
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
*
* @example
* ```ts
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
* ```
*/
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
name: TName;
options: TOptions;
output: TOutput;
printOutput: TPrintOutput;
};
/**
* Printer instance returned by a printer factory.
*
* @example
* ```ts
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
* ```
*/
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
/**
* Unique identifier supplied at creation time.
*/
name: T['name'];
/**
* Options for this printer instance.
*/
options: T['options'];
/**
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
* Always dispatches through the `nodes` map. Never calls the `print` override.
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
*/
transform: (node: SchemaNode) => T['output'] | null;
/**
* Public printer. If the builder provides a root-level `print`, this calls that
* higher-level function (which may produce full declarations).
* Otherwise, falls back to the node-level dispatcher.
*/
print: (node: SchemaNode) => T['printOutput'] | null;
};
/**
* Builder function passed to `definePrinter`.
*
* It receives resolved options and returns:
* - `name`
* - `options`
* - `nodes` handlers
* - optional top-level `print` override
*
* @example
* ```ts
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
* ```
*/
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
name: T['name'];
/**
* Options to store on the printer.
*/
options: T['options'];
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
/**
* Optional root-level print override. When provided, becomes the public `printer.print`.
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
* not the override itself, so recursion is safe.
*/
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
};
/**
* Defines a schema printer: a function that takes a `SchemaNode` and emits
* code in your target language. Each plugin that produces code from schemas
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
* with this helper.
*
* The builder receives resolved options and returns:
*
* - `name` unique identifier for the printer.
* - `options` stored on the returned printer instance.
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
* output (a string, a TypeScript AST node, ...) for that schema type.
* - `print` (optional), top-level override exposed as `printer.print`.
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
*
* Without a `print` override, `printer.print` falls back to `printer.transform`
* (the node-level dispatcher).
*
* @example Tiny Zod printer
* ```ts
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
*
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
*
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
* name: 'zod',
* options: { strict: options.strict ?? true },
* nodes: {
* string: () => 'z.string()',
* object(node) {
* const props = node.properties
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
* .join(', ')
* return `z.object({ ${props} })`
* },
* },
* }))
* ```
*/
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
/**
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
*
* @example
* ```ts
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
* (node) => node.kind,
* )
* ```
*/
declare function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null): <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
name: T["name"];
options: T["options"];
nodes: Partial<{ [K in TKey]: (this: {
transform: (node: TNode) => T["output"] | null;
options: T["options"];
}, node: TNodeByKey[K]) => T["output"] | null }>;
print?: (this: {
transform: (node: TNode) => T["output"] | null;
options: T["options"];
}, node: TNode) => T["printOutput"] | null;
}) => (options?: T["options"]) => {
name: T["name"];
options: T["options"];
transform: (node: TNode) => T["output"] | null;
print: (node: TNode) => T["printOutput"] | null;
};
//#endregion
export { definePrinter as a, DedupeCanonical as c, applyDedupe as d, buildDedupePlan as f, createPrinterFactory as i, DedupeLookups as l, PrinterFactoryOptions as n, SchemaDialect as o, PrinterPartial as r, defineSchemaDialect as s, Printer as t, DedupePlan as u };
//# sourceMappingURL=types-DyDzizSf.d.ts.map
const require_response = require("./response-hnSw2NKE.cjs");
const require_visitor = require("./visitor-DpKZ9Tk0.cjs");
//#region ../../internals/utils/src/promise.ts
/**
* Wraps `factory` with a keyed cache backed by the provided store.
*
* Pass a `WeakMap` for object keys (results are GC-eligible when the key is
* collected) or a `Map` for primitive keys. For multi-argument functions,
* nest two `memoize` calls — the outer keyed by the first argument, the
* inner (created once per outer miss) keyed by the second.
*
* Because the cache is owned by the caller, it can be shared, inspected, or
* cleared independently of the memoized function.
*
* @example Single WeakMap key
* ```ts
* const cache = new WeakMap<SchemaNode, Set<string>>()
* const getRefs = memoize(cache, (node) => collectRefs(node))
* ```
*
* @example Single Map key (primitive)
* ```ts
* const cache = new Map<string, Resolver>()
* const getResolver = memoize(cache, (name) => buildResolver(name))
* ```
*
* @example Two-level (object + primitive)
* ```ts
* const outer = new WeakMap<Params[], Map<string, Params[]>>()
* const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
* fn(params)('camelcase')
* ```
*/
function memoize(store, factory) {
return (key) => {
if (store.has(key)) return store.get(key);
const value = factory(key);
store.set(key, value);
return value;
};
}
//#endregion
//#region ../../internals/utils/src/reserved.ts
/**
* JavaScript and Java reserved words.
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
*/
const reservedWords = new Set([
"abstract",
"arguments",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"double",
"else",
"enum",
"eval",
"export",
"extends",
"false",
"final",
"finally",
"float",
"for",
"function",
"goto",
"if",
"implements",
"import",
"in",
"instanceof",
"int",
"interface",
"let",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"typeof",
"var",
"void",
"volatile",
"while",
"with",
"yield",
"Array",
"Date",
"hasOwnProperty",
"Infinity",
"isFinite",
"isNaN",
"isPrototypeOf",
"length",
"Math",
"name",
"NaN",
"Number",
"Object",
"prototype",
"String",
"toString",
"undefined",
"valueOf"
]);
/**
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
*
* @example
* ```ts
* isValidVarName('status') // true
* isValidVarName('class') // false (reserved word)
* isValidVarName('42foo') // false (starts with digit)
* ```
*/
function isValidVarName(name) {
if (!name || reservedWords.has(name)) return false;
return isIdentifier(name);
}
/**
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
*
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
* deciding whether an object key needs quoting.
*
* @example
* ```ts
* isIdentifier('name') // true
* isIdentifier('x-total')// false
* ```
*/
function isIdentifier(name) {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
}
//#endregion
//#region ../../internals/utils/src/string.ts
/**
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
* any backslash or single quote in the content.
*
* @example
* ```ts
* singleQuote('foo') // "'foo'"
* singleQuote("o'clock") // "'o\\'clock'"
* ```
*/
function singleQuote(value) {
if (value === void 0 || value === null) return "''";
return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
}
//#endregion
//#region src/utils/codegen.ts
/**
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no
* comments so callers always get a usable string.
*
* @example
* ```ts
* buildJSDoc(['@type string', '@example hello'])
* // '/**\n * @type string\n * @example hello\n *\/\n '
* ```
*/
function buildJSDoc(comments, options = {}) {
const { indent = " * ", suffix = "\n ", fallback = " " } = options;
if (comments.length === 0) return fallback;
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
}
/**
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
*/
function indentLines(text) {
if (!text) return "";
return text.split("\n").map((line) => line.trim() ? `${require_visitor.INDENT}${line}` : "").join("\n");
}
/**
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
*
* @example
* ```ts
* objectKey('name') // 'name'
* objectKey('x-total') // "'x-total'"
* ```
*/
function objectKey(name) {
return isIdentifier(name) ? name : singleQuote(name);
}
/**
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
* level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
* so callers never re-parse the generated code. A trailing comma is added per entry to match the
* formatter's multi-line style.
*
* @example
* ```ts
* buildObject(['id: z.number()', 'name: z.string()'])
* // '{\n id: z.number(),\n name: z.string(),\n}'
* ```
*/
function buildObject(entries) {
if (entries.length === 0) return "{}";
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
}
/**
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
* one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
* `z.array([…])`, and similar member lists so objects inside them nest correctly.
*
* @example
* ```ts
* buildList(['z.string()', 'z.number()'])
* // '[z.string(), z.number()]'
* ```
*/
function buildList(items, brackets = ["[", "]"]) {
const [open, close] = brackets;
if (items.length === 0) return `${open}${close}`;
if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
}
//#endregion
//#region src/utils/schemaMerge.ts
/**
* Merges a run of adjacent anonymous object members into one. Named or non-object members break the
* run and pass through. Stays a construction-time helper, not a macro, so callers keep control of the
* member boundaries (such as keeping synthetic discriminant objects out of a run).
*
* @example
* ```ts
* const merged = [...mergeAdjacentObjectsLazy([objectA, objectB])]
* ```
*/
function* mergeAdjacentObjectsLazy(members) {
let acc;
for (const member of members) {
const objectMember = require_visitor.narrowSchema(member, "object");
if (objectMember && !objectMember.name && acc !== void 0) {
const accObject = require_visitor.narrowSchema(acc, "object");
if (accObject && !accObject.name) {
acc = require_response.createSchema({
...accObject,
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
});
continue;
}
}
if (acc !== void 0) yield acc;
acc = member;
}
if (acc !== void 0) yield acc;
}
//#endregion
//#region src/utils/strings.ts
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
function trimQuotes(text) {
if (text.length >= 2) {
const first = text[0];
const last = text[text.length - 1];
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
}
return text;
}
/**
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
*
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
* code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
function stringify(value) {
if (value === void 0 || value === null) return "''";
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
}
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
function jsStringEscape(input) {
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
switch (character) {
case "\"":
case "'":
case "\\": return `\\${character}`;
case "\n": return "\\n";
case "\r": return "\\r";
case "\u2028": return "\\u2028";
case "\u2029": return "\\u2029";
default: return "";
}
});
}
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
function toRegExpString(text, func = "RegExp") {
const raw = trimQuotes(text);
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
const replacementTarget = match?.[1] ?? "";
const matchedFlags = match?.[2];
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
const { source, flags } = new RegExp(cleaned, matchedFlags);
if (func === null) return `/${source}/${flags}`;
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
}
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
function stringifyObject(value) {
return Object.entries(value).map(([key, val]) => {
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
return `${key}: ${val}`;
}).filter(Boolean).join(",\n");
}
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
function getNestedAccessor(param, accessor) {
const parts = Array.isArray(param) ? param : param.split(".");
if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
}
//#endregion
//#region src/utils/schemaGraph.ts
/**
* Collects every named schema referenced transitively from a node through its ref edges.
*
* Refs are followed by name only, so the resolved `node.schema` is never traversed inline.
*
* @example Collect refs from a single schema
* ```ts
* const names = collectReferencedSchemaNames(petSchema)
* // → Set { 'Category', 'Tag' }
* ```
*
* @example Accumulate refs from multiple schemas into one set
* ```ts
* const out = new Set<string>()
* for (const schema of schemas) {
* collectReferencedSchemaNames(schema, out)
* }
* ```
*/
const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
const refs = /* @__PURE__ */ new Set();
require_visitor.collect(node, { schema(child) {
if (child.type === "ref") {
const name = require_visitor.resolveRefName(child);
if (name) refs.add(name);
}
} });
return refs;
});
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
if (!node) return out;
for (const name of collectSchemaRefs(node)) out.add(name);
return out;
}
/**
* Collects the names of all top-level schemas transitively used by a set of operations.
*
* An operation uses a schema when its parameters, request body, or responses reference it, directly
* or through other named schemas. The walk is iterative, so reference cycles are safe.
*
* Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.
*
* @example Only generate schemas referenced by included operations
* ```ts
* const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)
* const allowed = collectUsedSchemaNames(includedOps, schemas)
*
* for (const schema of schemas) {
* if (schema.name && !allowed.has(schema.name)) continue
* // … generate schema
* }
* ```
*/
const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
function computeUsedSchemaNames(operations, schemas) {
const schemaMap = /* @__PURE__ */ new Map();
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
const result = /* @__PURE__ */ new Set();
function visitSchema(schema) {
const directRefs = collectReferencedSchemaNames(schema);
for (const name of directRefs) if (!result.has(name)) {
result.add(name);
const namedSchema = schemaMap.get(name);
if (namedSchema) visitSchema(namedSchema);
}
}
for (const op of operations) for (const schema of require_visitor.collectLazy(op, {
depth: "shallow",
schema: (node) => node
})) visitSchema(schema);
return result;
}
function collectUsedSchemaNames(operations, schemas) {
return collectUsedSchemaNamesMemo(operations)(schemas);
}
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
const graph = /* @__PURE__ */ new Map();
for (const schema of schemas) {
if (!schema.name) continue;
graph.set(schema.name, collectReferencedSchemaNames(schema));
}
const circular = /* @__PURE__ */ new Set();
for (const start of graph.keys()) {
const visited = /* @__PURE__ */ new Set();
const stack = [...graph.get(start) ?? []];
while (stack.length > 0) {
const node = stack.pop();
if (node === start) {
circular.add(start);
break;
}
if (visited.has(node)) continue;
visited.add(node);
const next = graph.get(node);
if (next) for (const r of next) stack.push(r);
}
}
return circular;
});
/**
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
*
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
* the generated code does not recurse forever. Refs are followed by name only, so the walk stays
* linear in the size of the schema graph.
*
* @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.
*/
function findCircularSchemas(schemas) {
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
return findCircularSchemasMemo(schemas);
}
/**
* Returns `true` when a schema, or anything nested inside it, references a circular schema.
*
* Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled
* on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.
*
* @note Stops at the first matching circular ref.
*/
function containsCircularRef(node, { circularSchemas, excludeName }) {
if (!node || circularSchemas.size === 0) return false;
for (const _ of require_visitor.collectLazy(node, { schema(child) {
if (child.type !== "ref") return null;
const name = require_visitor.resolveRefName(child);
return name && name !== excludeName && circularSchemas.has(name) ? true : null;
} })) return true;
return false;
}
//#endregion
//#region src/utils/schemaTraversal.ts
/**
* Maps each property of an object schema to its transformed output. Pairs every result with the
* original property so the printer keeps full control over modifiers, getters, and key syntax.
*
* @example
* ```ts
* const entries = mapSchemaProperties(node, (schema) => this.transform(schema))
* // entries: [{ name: 'id', property, output: 'z.number()' }, ...]
* ```
*/
function mapSchemaProperties(node, transform) {
return node.properties.map((property) => ({
name: property.name,
property,
output: transform(property.schema)
}));
}
/**
* Maps each member of a union or intersection schema to its transformed output, pairing every
* result with the original member.
*/
function mapSchemaMembers(node, transform) {
return (node.members ?? []).map((schema) => ({
schema,
output: transform(schema)
}));
}
/**
* Maps each item of an array or tuple schema to its transformed output, pairing every result with
* the original item.
*/
function mapSchemaItems(node, transform) {
return (node.items ?? []).map((schema) => ({
schema,
output: transform(schema)
}));
}
/**
* Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key
* is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation
* of a recursive schema until first access.
*
* @example
* ```ts
* lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })
* // "get parent() { return z.lazy(() => Pet) }"
* ```
*/
function lazyGetter({ name, body }) {
return `get ${objectKey(name)}() { return ${body} }`;
}
//#endregion
//#region src/utils/operationParams.ts
/**
* Applies casing rules to parameter names and returns a new array without mutating the input.
*
* Run it before handing parameters to schema builders so output property keys get the right casing
* while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the
* original array is returned unchanged.
*/
const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => {
const transformed = casing === "camelcase" || !isValidVarName(param.name) ? require_response.camelCase(param.name) : param.name;
return {
...param,
name: transformed
};
})));
function caseParams(params, casing) {
if (!casing) return params;
return caseParamsMemo(params)(casing);
}
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, it falls back to the schema primitive (a plain type-name string). When the
* parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like
* `GroupParams['petId']`, otherwise the resolved individual name.
*/
function resolveParamType({ node, param, resolver }) {
if (!resolver) return param.schema.primitive ?? "unknown";
const individualName = resolver.resolveParamName(node, param);
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
const groupResolvers = {
path: resolver.resolvePathParamsName,
query: resolver.resolveQueryParamsName,
header: resolver.resolveHeaderParamsName
};
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
if (groupName && groupName !== individualName) return require_response.createIndexedAccessType({
objectType: groupName,
indexType: param.name
});
return individualName;
}
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
function createOperationParams(node, options) {
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options;
const dataName = paramNames?.data ?? "data";
const paramsName = paramNames?.params ?? "params";
const headersName = paramNames?.headers ?? "headers";
const pathName = paramNames?.path ?? "pathParams";
const wrapType = (type) => typeWrapper ? typeWrapper(type) : type;
const wrapTypeExpression = (type) => typeof type === "string" ? wrapType(type) : type;
const casedParams = caseParams(node.parameters, paramsCasing);
const pathParams = casedParams.filter((p) => p.in === "path");
const queryParams = casedParams.filter((p) => p.in === "query");
const headerParams = casedParams.filter((p) => p.in === "header");
const toProperty = (param) => ({
name: param.name,
type: wrapTypeExpression(resolveParamType({
node,
param,
resolver
})),
optional: !param.required
});
const emptyObjectDefault = (props) => props.every((p) => p.optional) ? "{}" : void 0;
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
const bodyProperty = bodyType ? [{
name: dataName,
type: bodyType,
optional: !(node.requestBody?.required ?? false)
}] : [];
const trailingGroups = [{
name: paramsName,
node,
params: queryParams,
groupType: require_visitor.resolveGroupType({
node,
params: queryParams,
group: "query",
resolver
}),
resolver,
wrapType
}, {
name: headersName,
node,
params: headerParams,
groupType: require_visitor.resolveGroupType({
node,
params: headerParams,
group: "header",
resolver
}),
resolver,
wrapType
}];
const params = [];
if (paramsType === "object") {
const children = [
...pathParams.map(toProperty),
...bodyProperty,
...trailingGroups.flatMap(buildGroupProperty)
];
if (children.length) params.push(require_response.createFunctionParameter({
properties: children,
default: emptyObjectDefault(children)
}));
} else {
if (pathParamsType === "inlineSpread" && pathParams.length) {
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]);
params.push(require_response.createFunctionParameter({
name: pathName,
type: spreadType ? wrapType(spreadType) : void 0,
rest: true
}));
} else if (pathParamsType === "inline") params.push(...pathParams.map((p) => require_response.createFunctionParameter(toProperty(p))));
else if (pathParams.length) {
const pathChildren = pathParams.map(toProperty);
params.push(require_response.createFunctionParameter({
properties: pathChildren,
default: pathParamsDefault ?? emptyObjectDefault(pathChildren)
}));
}
params.push(...bodyProperty.map((p) => require_response.createFunctionParameter(p)));
params.push(...trailingGroups.flatMap(buildGroupParam));
}
params.push(...extraParams);
return require_response.createFunctionParameters({ params });
}
/**
* Builds the property descriptor for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }) {
if (groupType) return [{
name,
type: typeof groupType.type === "string" ? wrapType(groupType.type) : groupType.type,
optional: groupType.optional
}];
if (params.length) return [{
name,
type: buildTypeLiteral({
node,
params,
resolver
}),
optional: params.every((p) => !p.required)
}];
return [];
}
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupParam(args) {
return buildGroupProperty(args).map((p) => require_response.createFunctionParameter(p));
}
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
function buildTypeLiteral({ node, params, resolver }) {
return require_response.createTypeLiteral({ members: params.map((p) => ({
name: p.name,
type: resolveParamType({
node,
param: p,
resolver
}),
optional: !p.required
})) });
}
//#endregion
Object.defineProperty(exports, "buildGroupParam", {
enumerable: true,
get: function() {
return buildGroupParam;
}
});
Object.defineProperty(exports, "buildJSDoc", {
enumerable: true,
get: function() {
return buildJSDoc;
}
});
Object.defineProperty(exports, "buildList", {
enumerable: true,
get: function() {
return buildList;
}
});
Object.defineProperty(exports, "buildObject", {
enumerable: true,
get: function() {
return buildObject;
}
});
Object.defineProperty(exports, "buildTypeLiteral", {
enumerable: true,
get: function() {
return buildTypeLiteral;
}
});
Object.defineProperty(exports, "caseParams", {
enumerable: true,
get: function() {
return caseParams;
}
});
Object.defineProperty(exports, "collectUsedSchemaNames", {
enumerable: true,
get: function() {
return collectUsedSchemaNames;
}
});
Object.defineProperty(exports, "containsCircularRef", {
enumerable: true,
get: function() {
return containsCircularRef;
}
});
Object.defineProperty(exports, "createOperationParams", {
enumerable: true,
get: function() {
return createOperationParams;
}
});
Object.defineProperty(exports, "findCircularSchemas", {
enumerable: true,
get: function() {
return findCircularSchemas;
}
});
Object.defineProperty(exports, "getNestedAccessor", {
enumerable: true,
get: function() {
return getNestedAccessor;
}
});
Object.defineProperty(exports, "isValidVarName", {
enumerable: true,
get: function() {
return isValidVarName;
}
});
Object.defineProperty(exports, "jsStringEscape", {
enumerable: true,
get: function() {
return jsStringEscape;
}
});
Object.defineProperty(exports, "lazyGetter", {
enumerable: true,
get: function() {
return lazyGetter;
}
});
Object.defineProperty(exports, "mapSchemaItems", {
enumerable: true,
get: function() {
return mapSchemaItems;
}
});
Object.defineProperty(exports, "mapSchemaMembers", {
enumerable: true,
get: function() {
return mapSchemaMembers;
}
});
Object.defineProperty(exports, "mapSchemaProperties", {
enumerable: true,
get: function() {
return mapSchemaProperties;
}
});
Object.defineProperty(exports, "mergeAdjacentObjectsLazy", {
enumerable: true,
get: function() {
return mergeAdjacentObjectsLazy;
}
});
Object.defineProperty(exports, "objectKey", {
enumerable: true,
get: function() {
return objectKey;
}
});
Object.defineProperty(exports, "resolveParamType", {
enumerable: true,
get: function() {
return resolveParamType;
}
});
Object.defineProperty(exports, "stringify", {
enumerable: true,
get: function() {
return stringify;
}
});
Object.defineProperty(exports, "stringifyObject", {
enumerable: true,
get: function() {
return stringifyObject;
}
});
Object.defineProperty(exports, "toRegExpString", {
enumerable: true,
get: function() {
return toRegExpString;
}
});
Object.defineProperty(exports, "trimQuotes", {
enumerable: true,
get: function() {
return trimQuotes;
}
});
//# sourceMappingURL=utils-BLJwyza-.cjs.map
{"version":3,"file":"utils-BLJwyza-.cjs","names":["INDENT","narrowSchema","createSchema","resolveRefName","collectLazy","camelCase","createIndexedAccessType","resolveGroupType","createFunctionParameter","createFunctionParameters","createTypeLiteral"],"sources":["../../../internals/utils/src/promise.ts","../../../internals/utils/src/reserved.ts","../../../internals/utils/src/string.ts","../src/utils/codegen.ts","../src/utils/schemaMerge.ts","../src/utils/strings.ts","../src/utils/schemaGraph.ts","../src/utils/schemaTraversal.ts","../src/utils/operationParams.ts"],"sourcesContent":["function* chunks<T>(arr: ReadonlyArray<T>, size: number): Generator<Array<T>> {\n for (let i = 0; i < arr.length; i += size) {\n yield arr.slice(i, i + size)\n }\n}\n\nexport type ForBatchesOptions = {\n /**\n * Maximum batch size handed to `process`.\n * Parallel dispatch within a batch is the caller's responsibility\n * (typically via `Promise.all(batch.map(...))`).\n */\n concurrency: number\n /**\n * Called after every batch.\n *\n * Use a cheap, idempotent callback (e.g. one that short-circuits when there\n * is nothing new to do). The helper does not coalesce calls — if you need\n * throttling, do it inside `flush` itself.\n */\n flush?: () => Promise<void>\n}\n\n/**\n * Slices `source` into batches of `concurrency` items and awaits `process` for each batch.\n * Accepts both plain arrays (sync) and `AsyncIterable` (streaming).\n *\n * `process` controls whether items inside a batch run in parallel; this helper only\n * controls batch size and per-batch flushing.\n *\n * @example\n * ```ts\n * // parallel dispatch inside each batch\n * await forBatches(schemas, (batch) => Promise.all(batch.map(process)), { concurrency: 8 })\n *\n * // async iterable with a flush after every batch\n * await forBatches(stream.schemas, (batch) => dispatch(batch), { concurrency: 8, flush })\n * ```\n */\nexport async function forBatches<T>(\n source: ReadonlyArray<T> | AsyncIterable<T>,\n process: (batch: Array<T>) => Promise<unknown>,\n options: ForBatchesOptions,\n): Promise<void> {\n const { concurrency, flush } = options\n\n if (Array.isArray(source)) {\n for (const batch of chunks(source, concurrency)) {\n await process(batch)\n if (flush) await flush()\n }\n return\n }\n\n const batch: Array<T> = []\n for await (const item of source) {\n batch.push(item)\n if (batch.length >= concurrency) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n }\n if (batch.length > 0) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n}\n\n/** A value that may already be resolved or still pending.\n *\n * @example\n * ```ts\n * function load(id: string): PossiblePromise<string> {\n * return cache.get(id) ?? fetchRemote(id)\n * }\n * ```\n */\nexport type PossiblePromise<T> = Promise<T> | T\n\n/** Returns `true` when `result` is a thenable `Promise`.\n *\n * @example\n * ```ts\n * isPromise(Promise.resolve(1)) // true\n * isPromise(42) // false\n * ```\n */\nexport function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {\n return result !== null && result !== undefined && typeof (result as Record<string, unknown>)['then'] === 'function'\n}\n\n/** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)\n * ```\n */\nexport function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & { reason: T } {\n return result.status === 'rejected'\n}\n\ntype Store<TKey, TValue> = {\n has(key: TKey): boolean\n get(key: TKey): TValue | undefined\n set(key: TKey, value: TValue): unknown\n}\n\n/**\n * Wraps `factory` with a keyed cache backed by the provided store.\n *\n * Pass a `WeakMap` for object keys (results are GC-eligible when the key is\n * collected) or a `Map` for primitive keys. For multi-argument functions,\n * nest two `memoize` calls — the outer keyed by the first argument, the\n * inner (created once per outer miss) keyed by the second.\n *\n * Because the cache is owned by the caller, it can be shared, inspected, or\n * cleared independently of the memoized function.\n *\n * @example Single WeakMap key\n * ```ts\n * const cache = new WeakMap<SchemaNode, Set<string>>()\n * const getRefs = memoize(cache, (node) => collectRefs(node))\n * ```\n *\n * @example Single Map key (primitive)\n * ```ts\n * const cache = new Map<string, Resolver>()\n * const getResolver = memoize(cache, (name) => buildResolver(name))\n * ```\n *\n * @example Two-level (object + primitive)\n * ```ts\n * const outer = new WeakMap<Params[], Map<string, Params[]>>()\n * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))\n * fn(params)('camelcase')\n * ```\n */\nexport function memoize<TKey, TValue>(store: Store<TKey, TValue>, factory: (key: TKey) => TValue): (key: TKey) => TValue {\n return (key: TKey): TValue => {\n if (store.has(key)) return store.get(key)!\n const value = factory(key)\n store.set(key, value)\n return value\n }\n}\n\n/**\n * Container that switches between an eager `Array<T>` and a lazy `AsyncIterable<T>`.\n *\n * `Array<T>` by default. With `Stream` set to `true` it becomes `AsyncIterable<T>`, so large\n * collections can be produced lazily without holding every item in memory. Pairs with\n * {@link arrayToAsyncIterable}, which lifts a plain array into the streaming form.\n *\n * @example\n * ```ts\n * type Eager = Streamable<number> // Array<number>\n * type Lazy = Streamable<number, true> // AsyncIterable<number>\n * ```\n */\nexport type Streamable<T, Stream extends boolean = false> = Stream extends true ? AsyncIterable<T> : Array<T>\n\n/**\n * Wraps a plain array in a reusable `AsyncIterable`.\n * Each `[Symbol.asyncIterator]()` call returns a fresh generator so the\n * iterable can be consumed multiple times (e.g. once per plugin pre-scan).\n *\n * @example\n * ```ts\n * const stream = arrayToAsyncIterable([1, 2, 3])\n * for await (const n of stream) console.log(n) // 1, 2, 3\n * ```\n */\nexport function arrayToAsyncIterable<T>(arr: ReadonlyArray<T>): AsyncIterable<T> {\n return {\n [Symbol.asyncIterator]() {\n return (async function* () {\n yield* arr\n })()\n },\n }\n}\n","/**\n * JavaScript and Java reserved words.\n * @link https://github.com/jonschlinkert/reserved/blob/master/index.js\n */\nconst reservedWords = new Set([\n 'abstract',\n 'arguments',\n 'boolean',\n 'break',\n 'byte',\n 'case',\n 'catch',\n 'char',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'double',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'final',\n 'finally',\n 'float',\n 'for',\n 'function',\n 'goto',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'int',\n 'interface',\n 'let',\n 'long',\n 'native',\n 'new',\n 'null',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'short',\n 'static',\n 'super',\n 'switch',\n 'synchronized',\n 'this',\n 'throw',\n 'throws',\n 'transient',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'volatile',\n 'while',\n 'with',\n 'yield',\n 'Array',\n 'Date',\n 'hasOwnProperty',\n 'Infinity',\n 'isFinite',\n 'isNaN',\n 'isPrototypeOf',\n 'length',\n 'Math',\n 'name',\n 'NaN',\n 'Number',\n 'Object',\n 'prototype',\n 'String',\n 'toString',\n 'undefined',\n 'valueOf',\n] as const)\n\n/**\n * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.\n *\n * @example\n * ```ts\n * transformReservedWord('class') // '_class'\n * transformReservedWord('42foo') // '_42foo'\n * transformReservedWord('status') // 'status'\n * ```\n */\nexport function transformReservedWord(word: string): string {\n const firstChar = word.charCodeAt(0)\n if (word && (reservedWords.has(word as 'valueOf') || (firstChar >= 48 && firstChar <= 57))) {\n return `_${word}`\n }\n return word\n}\n\n/**\n * Returns `true` when `name` is a syntactically valid JavaScript variable name.\n *\n * @example\n * ```ts\n * isValidVarName('status') // true\n * isValidVarName('class') // false (reserved word)\n * isValidVarName('42foo') // false (starts with digit)\n * ```\n */\nexport function isValidVarName(name: string): boolean {\n if (!name || reservedWords.has(name as 'valueOf')) {\n return false\n }\n return isIdentifier(name)\n}\n\n/**\n * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.\n *\n * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys\n * even though they are not valid variable names, so use this (not {@link isValidVarName}) when\n * deciding whether an object key needs quoting.\n *\n * @example\n * ```ts\n * isIdentifier('name') // true\n * isIdentifier('x-total')// false\n * ```\n */\nexport function isIdentifier(name: string): boolean {\n return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)\n}\n","/**\n * Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping\n * any backslash or single quote in the content.\n *\n * @example\n * ```ts\n * singleQuote('foo') // \"'foo'\"\n * singleQuote(\"o'clock\") // \"'o\\\\'clock'\"\n * ```\n */\nexport function singleQuote(value: string | number | boolean | undefined | null): string {\n if (value === undefined || value === null) return \"''\"\n const escaped = String(value).replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")\n\n return `'${escaped}'`\n}\n","import { isIdentifier, singleQuote } from '@internals/utils'\nimport { INDENT } from '../constants.ts'\n\n/**\n * Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no\n * comments so callers always get a usable string.\n *\n * @example\n * ```ts\n * buildJSDoc(['@type string', '@example hello'])\n * // '/**\\n * @type string\\n * @example hello\\n *\\/\\n '\n * ```\n */\nexport function buildJSDoc(\n comments: Array<string>,\n options: {\n /**\n * String used to indent each comment line.\n * @default ' * '\n */\n indent?: string\n /**\n * String appended after the closing tag.\n * @default '\\n '\n */\n suffix?: string\n /**\n * Returned as-is when `comments` is empty.\n * @default ' '\n */\n fallback?: string\n } = {},\n): string {\n const { indent = ' * ', suffix = '\\n ', fallback = ' ' } = options\n\n if (comments.length === 0) return fallback\n\n return `/**\\n${comments.map((c) => `${indent}${c}`).join('\\n')}\\n */${suffix}`\n}\n\n/**\n * Indents every non-empty line of `text` by one indent level, leaving blank lines empty.\n */\nfunction indentLines(text: string): string {\n if (!text) return ''\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${INDENT}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Renders an object key, quoting it with single quotes only when it is not a valid identifier.\n * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.\n *\n * @example\n * ```ts\n * objectKey('name') // 'name'\n * objectKey('x-total') // \"'x-total'\"\n * ```\n */\nexport function objectKey(name: string): string {\n return isIdentifier(name) ? name : singleQuote(name)\n}\n\n/**\n * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one\n * level and closing the brace at column zero. Nested objects built the same way indent cumulatively,\n * so callers never re-parse the generated code. A trailing comma is added per entry to match the\n * formatter's multi-line style.\n *\n * @example\n * ```ts\n * buildObject(['id: z.number()', 'name: z.string()'])\n * // '{\\n id: z.number(),\\n name: z.string(),\\n}'\n * ```\n */\nexport function buildObject(entries: Array<string>): string {\n if (entries.length === 0) return '{}'\n const body = entries.map((entry) => `${indentLines(entry)},`).join('\\n')\n\n return `{\\n${body}\\n}`\n}\n\n/**\n * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on\n * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented\n * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,\n * `z.array([…])`, and similar member lists so objects inside them nest correctly.\n *\n * @example\n * ```ts\n * buildList(['z.string()', 'z.number()'])\n * // '[z.string(), z.number()]'\n * ```\n */\nexport function buildList(items: Array<string>, brackets: [open: string, close: string] = ['[', ']']): string {\n const [open, close] = brackets\n if (items.length === 0) return `${open}${close}`\n if (!items.some((item) => item.includes('\\n'))) return `${open}${items.join(', ')}${close}`\n const body = items.map((item) => `${indentLines(item)},`).join('\\n')\n\n return `${open}\\n${body}\\n${close}`\n}\n","import { narrowSchema } from '../guards.ts'\nimport { createSchema, type SchemaNode } from '../nodes/schema.ts'\n\n/**\n * Merges a run of adjacent anonymous object members into one. Named or non-object members break the\n * run and pass through. Stays a construction-time helper, not a macro, so callers keep control of the\n * member boundaries (such as keeping synthetic discriminant objects out of a run).\n *\n * @example\n * ```ts\n * const merged = [...mergeAdjacentObjectsLazy([objectA, objectB])]\n * ```\n */\nexport function* mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined> {\n let acc: SchemaNode | undefined\n\n for (const member of members) {\n const objectMember = narrowSchema(member, 'object')\n if (objectMember && !objectMember.name && acc !== undefined) {\n const accObject = narrowSchema(acc, 'object')\n if (accObject && !accObject.name) {\n acc = createSchema({\n ...accObject,\n properties: [...(accObject.properties ?? []), ...(objectMember.properties ?? [])],\n })\n continue\n }\n }\n if (acc !== undefined) yield acc\n acc = member\n }\n\n if (acc !== undefined) yield acc\n}\n","/**\n * Strips a single matching pair of `\"...\"`, `'...'`, or `` `...` `` from both ends of `text`.\n * Returns the string unchanged when no balanced quote pair is found.\n *\n * @example\n * ```ts\n * trimQuotes('\"hello\"') // 'hello'\n * trimQuotes('hello') // 'hello'\n * ```\n */\nexport function trimQuotes(text: string): string {\n if (text.length >= 2) {\n const first = text[0]\n const last = text[text.length - 1]\n if ((first === '\"' && last === '\"') || (first === \"'\" && last === \"'\") || (first === '`' && last === '`')) {\n return text.slice(1, -1)\n }\n }\n return text\n}\n\n/**\n * Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.\n *\n * Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated\n * code matches the repo style without a formatter.\n *\n * @example\n * ```ts\n * stringify('hello') // \"'hello'\"\n * stringify('\"hello\"') // \"'hello'\"\n * ```\n */\nexport function stringify(value: string | number | boolean | undefined): string {\n if (value === undefined || value === null) return \"''\"\n const json = JSON.stringify(trimQuotes(value.toString()))\n const inner = json.slice(1, -1).replace(/\\\\\"/g, '\"').replace(/'/g, \"\\\\'\")\n return `'${inner}'`\n}\n\n/**\n * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,\n * and the Unicode line terminators U+2028 and U+2029.\n *\n * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4\n *\n * @example\n * ```ts\n * jsStringEscape('say \"hi\"\\nbye') // 'say \\\\\"hi\\\\\"\\\\nbye'\n * ```\n */\nexport function jsStringEscape(input: unknown): string {\n return `${input}`.replace(/[\"'\\\\\\n\\r\\u2028\\u2029]/g, (character) => {\n switch (character) {\n case '\"':\n case \"'\":\n case '\\\\':\n return `\\\\${character}`\n case '\\n':\n return '\\\\n'\n case '\\r':\n return '\\\\r'\n case '\\u2028':\n return '\\\\u2028'\n case '\\u2029':\n return '\\\\u2029'\n default:\n return ''\n }\n })\n}\n\n/**\n * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.\n * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.\n * Pass `null` as the second argument to emit a `/pattern/flags` literal instead.\n *\n * @example\n * ```ts\n * toRegExpString('^(?im)foo') // 'new RegExp(\"^foo\", \"im\")'\n * toRegExpString('^(?im)foo', null) // '/^foo/im'\n * ```\n */\nexport function toRegExpString(text: string, func: string | null = 'RegExp'): string {\n const raw = trimQuotes(text)\n\n const match = raw.match(/^\\^(\\(\\?([igmsuy]+)\\))/i)\n const replacementTarget = match?.[1] ?? ''\n const matchedFlags = match?.[2]\n const cleaned = raw\n .replace(/^\\\\?\\//, '')\n .replace(/\\\\?\\/$/, '')\n .replace(replacementTarget, '')\n\n const { source, flags } = new RegExp(cleaned, matchedFlags)\n\n if (func === null) return `/${source}/${flags}`\n\n return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ''})`\n}\n\n/**\n * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested\n * objects recurse with fixed indentation, so the result drops straight into an object literal\n * without re-parsing.\n *\n * @example\n * ```ts\n * stringifyObject({ foo: 'bar', nested: { a: 1 } })\n * // 'foo: bar,\\nnested: {\\n a: 1\\n }'\n * ```\n */\nexport function stringifyObject(value: Record<string, unknown>): string {\n const items = Object.entries(value)\n .map(([key, val]) => {\n if (val !== null && typeof val === 'object') {\n return `${key}: {\\n ${stringifyObject(val as Record<string, unknown>)}\\n }`\n }\n return `${key}: ${val}`\n })\n .filter(Boolean)\n return items.join(',\\n')\n}\n\n/**\n * Renders a dotted path or string array as an optional-chaining accessor expression rooted at\n * `accessor`. Returns `null` for an empty path.\n *\n * @example\n * ```ts\n * getNestedAccessor('pagination.next.id', 'lastPage')\n * // \"lastPage?.['pagination']?.['next']?.['id']\"\n * ```\n */\nexport function getNestedAccessor(param: string | Array<string>, accessor: string): string | null {\n const parts = Array.isArray(param) ? param : param.split('.')\n if (parts.length === 0 || (parts.length === 1 && parts[0] === '')) return null\n return `${accessor}?.['${`${parts.join(\"']?.['\")}']`}`\n}\n","import { memoize } from '@internals/utils'\nimport type { OperationNode, SchemaNode } from '../nodes/index.ts'\nimport { collect, collectLazy } from '../visitor.ts'\nimport { resolveRefName } from './refs.ts'\n\n/**\n * Collects every named schema referenced transitively from a node through its ref edges.\n *\n * Refs are followed by name only, so the resolved `node.schema` is never traversed inline.\n *\n * @example Collect refs from a single schema\n * ```ts\n * const names = collectReferencedSchemaNames(petSchema)\n * // → Set { 'Category', 'Tag' }\n * ```\n *\n * @example Accumulate refs from multiple schemas into one set\n * ```ts\n * const out = new Set<string>()\n * for (const schema of schemas) {\n * collectReferencedSchemaNames(schema, out)\n * }\n * ```\n */\nconst collectSchemaRefs = memoize(new WeakMap<SchemaNode, ReadonlySet<string>>(), (node: SchemaNode): ReadonlySet<string> => {\n const refs = new Set<string>()\n collect<void>(node, {\n schema(child) {\n if (child.type === 'ref') {\n const name = resolveRefName(child)\n if (name) refs.add(name)\n }\n },\n })\n return refs\n})\n\nexport function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {\n if (!node) return out\n for (const name of collectSchemaRefs(node)) out.add(name)\n return out\n}\n\n/**\n * Collects the names of all top-level schemas transitively used by a set of operations.\n *\n * An operation uses a schema when its parameters, request body, or responses reference it, directly\n * or through other named schemas. The walk is iterative, so reference cycles are safe.\n *\n * Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.\n *\n * @example Only generate schemas referenced by included operations\n * ```ts\n * const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)\n * const allowed = collectUsedSchemaNames(includedOps, schemas)\n *\n * for (const schema of schemas) {\n * if (schema.name && !allowed.has(schema.name)) continue\n * // … generate schema\n * }\n * ```\n */\nconst collectUsedSchemaNamesMemo = memoize(new WeakMap<ReadonlyArray<OperationNode>, (schemas: ReadonlyArray<SchemaNode>) => Set<string>>(), (ops) =>\n memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas) => computeUsedSchemaNames(ops, schemas)),\n)\n\nfunction computeUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n const schemaMap = new Map<string, SchemaNode>()\n for (const schema of schemas) {\n if (schema.name) schemaMap.set(schema.name, schema)\n }\n\n const result = new Set<string>()\n\n function visitSchema(schema: SchemaNode): void {\n const directRefs = collectReferencedSchemaNames(schema)\n for (const name of directRefs) {\n if (!result.has(name)) {\n result.add(name)\n const namedSchema = schemaMap.get(name)\n if (namedSchema) visitSchema(namedSchema)\n }\n }\n }\n\n for (const op of operations) {\n for (const schema of collectLazy<SchemaNode>(op, { depth: 'shallow', schema: (node) => node })) {\n visitSchema(schema)\n }\n }\n\n return result\n}\n\nexport function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n return collectUsedSchemaNamesMemo(operations)(schemas)\n}\n\nconst EMPTY_CIRCULAR_SET = new Set<string>()\n\nconst findCircularSchemasMemo = memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas: ReadonlyArray<SchemaNode>): Set<string> => {\n const graph = new Map<string, Set<string>>()\n\n for (const schema of schemas) {\n if (!schema.name) continue\n graph.set(schema.name, collectReferencedSchemaNames(schema))\n }\n\n const circular = new Set<string>()\n for (const start of graph.keys()) {\n const visited = new Set<string>()\n const stack: Array<string> = [...(graph.get(start) ?? [])]\n while (stack.length > 0) {\n const node = stack.pop()!\n if (node === start) {\n circular.add(start)\n break\n }\n if (visited.has(node)) continue\n visited.add(node)\n\n const next = graph.get(node)\n if (next) for (const r of next) stack.push(r)\n }\n }\n\n return circular\n})\n\n/**\n * Finds every schema that takes part in a circular dependency chain, including direct self-loops.\n *\n * Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so\n * the generated code does not recurse forever. Refs are followed by name only, so the walk stays\n * linear in the size of the schema graph.\n *\n * @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.\n */\nexport function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string> {\n if (schemas.length === 0) return EMPTY_CIRCULAR_SET\n return findCircularSchemasMemo(schemas)\n}\n\n/**\n * Returns `true` when a schema, or anything nested inside it, references a circular schema.\n *\n * Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled\n * on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.\n *\n * @note Stops at the first matching circular ref.\n */\nexport function containsCircularRef(\n node: SchemaNode | undefined,\n { circularSchemas, excludeName }: { circularSchemas: ReadonlySet<string>; excludeName?: string },\n): boolean {\n if (!node || circularSchemas.size === 0) return false\n\n for (const _ of collectLazy<true>(node, {\n schema(child) {\n if (child.type !== 'ref') return null\n const name = resolveRefName(child)\n return name && name !== excludeName && circularSchemas.has(name) ? true : null\n },\n })) {\n return true\n }\n\n return false\n}\n","import type { ArraySchemaNode, IntersectionSchemaNode, ObjectSchemaNode, PropertyNode, SchemaNode, UnionSchemaNode } from '../nodes/index.ts'\nimport { objectKey } from './codegen.ts'\n\n/**\n * Converts a child schema to printer output. Plugins instantiate it with their own output type:\n * `string` for the zod and faker printers, `ts.TypeNode` for the TypeScript printer. A printer's\n * `this.transform` fits directly, so its `null` for an empty result carries through to `output`.\n */\nexport type SchemaTransform<TOutput> = (schema: SchemaNode) => TOutput\n\n/**\n * A union or intersection member, or an array or tuple item, paired with its transformed output.\n */\nexport type MappedSchema<TOutput> = {\n /**\n * The original child schema, kept so the printer can read its metadata for leaf formatting.\n */\n schema: SchemaNode\n /**\n * The child schema after being run through the transform.\n */\n output: TOutput\n}\n\n/**\n * An object property paired with its transformed output.\n */\nexport type MappedProperty<TOutput> = {\n /**\n * The property name as written on the schema, before any identifier quoting.\n */\n name: string\n /**\n * The original property node, kept so the printer can read `required`, `schema`, and metadata.\n */\n property: PropertyNode\n /**\n * The property schema after being run through the transform.\n */\n output: TOutput\n}\n\n/**\n * Maps each property of an object schema to its transformed output. Pairs every result with the\n * original property so the printer keeps full control over modifiers, getters, and key syntax.\n *\n * @example\n * ```ts\n * const entries = mapSchemaProperties(node, (schema) => this.transform(schema))\n * // entries: [{ name: 'id', property, output: 'z.number()' }, ...]\n * ```\n */\nexport function mapSchemaProperties<TOutput>(node: ObjectSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedProperty<TOutput>> {\n return node.properties.map((property) => ({ name: property.name, property, output: transform(property.schema) }))\n}\n\n/**\n * Maps each member of a union or intersection schema to its transformed output, pairing every\n * result with the original member.\n */\nexport function mapSchemaMembers<TOutput>(node: UnionSchemaNode | IntersectionSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> {\n return (node.members ?? []).map((schema) => ({ schema, output: transform(schema) }))\n}\n\n/**\n * Maps each item of an array or tuple schema to its transformed output, pairing every result with\n * the original item.\n */\nexport function mapSchemaItems<TOutput>(node: ArraySchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> {\n return (node.items ?? []).map((schema) => ({ schema, output: transform(schema) }))\n}\n\n/**\n * Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key\n * is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation\n * of a recursive schema until first access.\n *\n * @example\n * ```ts\n * lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })\n * // \"get parent() { return z.lazy(() => Pet) }\"\n * ```\n */\nexport function lazyGetter({ name, body }: { name: string; body: string }): string {\n return `get ${objectKey(name)}() { return ${body} }`\n}\n","import { camelCase, isValidVarName, memoize } from '@internals/utils'\nimport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createTypeLiteral } from '../nodes/function.ts'\nimport type { FunctionParameterNode, FunctionParametersNode, OperationNode, ParameterNode, TypeExpression, TypeLiteralNode } from '../nodes/index.ts'\nimport { resolveGroupType } from './refs.ts'\n\n/**\n * Applies casing rules to parameter names and returns a new array without mutating the input.\n *\n * Run it before handing parameters to schema builders so output property keys get the right casing\n * while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the\n * original array is returned unchanged.\n */\nconst caseParamsMemo = memoize(new WeakMap<Array<ParameterNode>, (casing: string) => Array<ParameterNode>>(), (params) =>\n memoize(new Map<string, Array<ParameterNode>>(), (casing: string) =>\n params.map((param) => {\n const transformed = casing === 'camelcase' || !isValidVarName(param.name) ? camelCase(param.name) : param.name\n return { ...param, name: transformed }\n }),\n ),\n)\n\nexport function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {\n if (!casing) return params\n return caseParamsMemo(params)(casing)\n}\n\n/**\n * Named type for a group of parameters (query or header) emitted as a single typed parameter.\n */\nexport type ParamGroupType = {\n /**\n * Type expression for the group, a plain group-name reference.\n */\n type: TypeExpression\n /**\n * Whether the parameter group is optional.\n */\n optional: boolean\n}\n\n/**\n * A single member of a destructured parameter group, fed to `createFunctionParameter({ properties })`.\n */\ntype GroupProperty = {\n name: string\n type: TypeExpression\n optional?: boolean\n}\n\n/**\n * Resolver interface for {@link createOperationParams}.\n *\n * `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.\n */\nexport type OperationParamsResolver = {\n /**\n * Resolves the type name for an individual parameter.\n *\n * @example Individual path parameter name\n * `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`\n */\n resolveParamName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the request body type name.\n *\n * @example Request body type name\n * `resolver.resolveDataName(node) // → 'CreatePetData'`\n */\n resolveDataName(node: OperationNode): string\n /**\n * Resolves the grouped path parameters type name.\n * When the return value equals `resolveParamName`, no indexed access is emitted.\n *\n * @example Grouped path params type name\n * `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`\n */\n resolvePathParamsName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the grouped query parameters type name.\n * When the return value equals `resolveParamName`, an inline struct type is emitted instead.\n *\n * @example Grouped query params type name\n * `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`\n */\n resolveQueryParamsName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the grouped header parameters type name.\n * When the return value equals `resolveParamName`, an inline struct type is emitted instead.\n *\n * @example Grouped header params type name\n * `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`\n */\n resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string\n}\n\n/**\n * Options for {@link createOperationParams}.\n */\nexport type CreateOperationParamsOptions = {\n /**\n * How all operation parameters are grouped in the function signature.\n * - `'object'` wraps all params into a single destructured object `{ petId, data, params }`\n * - `'inline'` emits each param category as a separate top-level parameter\n */\n paramsType: 'object' | 'inline'\n /**\n * How path parameters are emitted when `paramsType` is `'inline'`.\n * - `'object'` groups them as `{ petId, storeId }: PathParams`\n * - `'inline'` spreads them as individual parameters `petId: string, storeId: string`\n * - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`\n */\n pathParamsType: 'object' | 'inline' | 'inlineSpread'\n /**\n * Converts parameter names to camelCase before output.\n */\n paramsCasing?: 'camelcase'\n /**\n * Resolver for parameter and request body type names.\n * Pass `ResolverTs` from `@kubb/plugin-ts` directly.\n * When omitted, falls back to the schema primitive or `'unknown'`.\n */\n resolver?: OperationParamsResolver\n /**\n * Default value for the path parameters binding when `pathParamsType` is `'object'`.\n * Falls back to `'{}'` when all path params are optional.\n */\n pathParamsDefault?: string\n /**\n * Extra parameters appended after the standard operation parameters.\n *\n * @example Plugin-specific trailing parameter\n * ```ts\n * extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]\n * ```\n */\n extraParams?: Array<FunctionParameterNode>\n /**\n * Override the default parameter names used for body, query, header, and rest-path groups.\n *\n * Useful when targeting languages or frameworks with different naming conventions.\n *\n * @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }\n */\n paramNames?: {\n /**\n * Name for the request body parameter.\n * @default 'data'\n */\n data?: string\n /**\n * Name for the query parameters group parameter.\n * @default 'params'\n */\n params?: string\n /**\n * Name for the header parameters group parameter.\n * @default 'headers'\n */\n headers?: string\n /**\n * Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.\n * @default 'pathParams'\n */\n path?: string\n }\n /**\n * Transforms every resolved type name before it lands in a parameter node, for framework-level\n * type wrappers.\n *\n * @example Vue Query, wrap every parameter type with `MaybeRefOrGetter`\n * `typeWrapper: (t) => \\`MaybeRefOrGetter<${t}>\\``\n */\n typeWrapper?: (type: string) => string\n}\n\n/**\n * Resolves the {@link TypeExpression} for an individual parameter.\n *\n * Without a resolver, it falls back to the schema primitive (a plain type-name string). When the\n * parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like\n * `GroupParams['petId']`, otherwise the resolved individual name.\n */\nexport function resolveParamType({\n node,\n param,\n resolver,\n}: {\n node: OperationNode\n param: ParameterNode\n resolver: OperationParamsResolver | undefined\n}): TypeExpression {\n if (!resolver) {\n return param.schema.primitive ?? 'unknown'\n }\n\n const individualName = resolver.resolveParamName(node, param)\n\n const groupLocation = param.in === 'path' || param.in === 'query' || param.in === 'header' ? param.in : undefined\n\n const groupResolvers = {\n path: resolver.resolvePathParamsName,\n query: resolver.resolveQueryParamsName,\n header: resolver.resolveHeaderParamsName,\n } as const\n\n const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : undefined\n\n if (groupName && groupName !== individualName) {\n return createIndexedAccessType({ objectType: groupName, indexType: param.name })\n }\n\n return individualName\n}\n\n/**\n * Converts an `OperationNode` into function parameters for code generation.\n *\n * Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one\n * destructured object parameter (`object`) and separate top-level parameters (`inline`), while\n * `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type\n * name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.\n */\nexport function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode {\n const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options\n\n const dataName = paramNames?.data ?? 'data'\n const paramsName = paramNames?.params ?? 'params'\n const headersName = paramNames?.headers ?? 'headers'\n const pathName = paramNames?.path ?? 'pathParams'\n\n const wrapType = (type: string): string => (typeWrapper ? typeWrapper(type) : type)\n // Only plain type-name references are wrapped, they need casing applied.\n // TypeLiteral and IndexedAccessType expressions are pre-resolved and pass through unchanged.\n const wrapTypeExpression = (type: TypeExpression): TypeExpression => (typeof type === 'string' ? wrapType(type) : type)\n\n const casedParams = caseParams(node.parameters, paramsCasing)\n const pathParams = casedParams.filter((p) => p.in === 'path')\n const queryParams = casedParams.filter((p) => p.in === 'query')\n const headerParams = casedParams.filter((p) => p.in === 'header')\n\n const toProperty = (param: ParameterNode): GroupProperty => ({\n name: param.name,\n type: wrapTypeExpression(resolveParamType({ node, param, resolver })),\n optional: !param.required,\n })\n const emptyObjectDefault = (props: Array<GroupProperty>): string | undefined => (props.every((p) => p.optional) ? '{}' : undefined)\n\n const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? 'unknown') : undefined\n const bodyProperty: Array<GroupProperty> = bodyType ? [{ name: dataName, type: bodyType, optional: !(node.requestBody?.required ?? false) }] : []\n\n const trailingGroups: Array<BuildGroupArgs> = [\n { name: paramsName, node, params: queryParams, groupType: resolveGroupType({ node, params: queryParams, group: 'query', resolver }), resolver, wrapType },\n {\n name: headersName,\n node,\n params: headerParams,\n groupType: resolveGroupType({ node, params: headerParams, group: 'header', resolver }),\n resolver,\n wrapType,\n },\n ]\n\n const params: Array<FunctionParameterNode> = []\n\n if (paramsType === 'object') {\n const children = [...pathParams.map(toProperty), ...bodyProperty, ...trailingGroups.flatMap(buildGroupProperty)]\n if (children.length) {\n params.push(createFunctionParameter({ properties: children, default: emptyObjectDefault(children) }))\n }\n } else {\n if (pathParamsType === 'inlineSpread' && pathParams.length) {\n const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]!)\n params.push(createFunctionParameter({ name: pathName, type: spreadType ? wrapType(spreadType) : undefined, rest: true }))\n } else if (pathParamsType === 'inline') {\n params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))))\n } else if (pathParams.length) {\n const pathChildren = pathParams.map(toProperty)\n params.push(createFunctionParameter({ properties: pathChildren, default: pathParamsDefault ?? emptyObjectDefault(pathChildren) }))\n }\n\n params.push(...bodyProperty.map((p) => createFunctionParameter(p)))\n params.push(...trailingGroups.flatMap(buildGroupParam))\n }\n\n params.push(...extraParams)\n\n return createFunctionParameters({ params })\n}\n\n/**\n * Shared arguments for building a query or header parameter group.\n */\nexport type BuildGroupArgs = {\n name: string\n node: OperationNode\n params: Array<ParameterNode>\n groupType: ParamGroupType | null\n resolver: OperationParamsResolver | undefined\n wrapType: (type: string) => string\n}\n\n/**\n * Builds the property descriptor for a query or header group.\n * Returns an empty array when there are no params to emit.\n *\n * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline\n * {@link TypeLiteralNode} from the individual params.\n */\nfunction buildGroupProperty({ name, node, params, groupType, resolver, wrapType }: BuildGroupArgs): Array<GroupProperty> {\n if (groupType) {\n const type = typeof groupType.type === 'string' ? wrapType(groupType.type) : groupType.type\n return [{ name, type, optional: groupType.optional }]\n }\n if (params.length) {\n return [{ name, type: buildTypeLiteral({ node, params, resolver }), optional: params.every((p) => !p.required) }]\n }\n return []\n}\n\n/**\n * Builds a single {@link FunctionParameterNode} for a query or header group.\n * Returns an empty array when there are no params to emit.\n *\n * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline\n * {@link TypeLiteralNode} from the individual params.\n */\nexport function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode> {\n return buildGroupProperty(args).map((p) => createFunctionParameter(p))\n}\n\n/**\n * Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.\n *\n * Used when query or header parameters have no dedicated group type name.\n * Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).\n */\nexport function buildTypeLiteral({\n node,\n params,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n resolver: OperationParamsResolver | undefined\n}): TypeLiteralNode {\n return createTypeLiteral({\n members: params.map((p) => ({\n name: p.name,\n type: resolveParamType({ node, param: p, resolver }),\n optional: !p.required,\n })),\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6IA,SAAgB,QAAsB,OAA4B,SAAuD;CACvH,QAAQ,QAAsB;EAC5B,IAAI,MAAM,IAAI,GAAG,GAAG,OAAO,MAAM,IAAI,GAAG;EACxC,MAAM,QAAQ,QAAQ,GAAG;EACzB,MAAM,IAAI,KAAK,KAAK;EACpB,OAAO;CACT;AACF;;;;;;;AChJA,MAAM,gBAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAC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AA,wBAAwB;IAAE,YAAY;IAAc,SAAS,qBAAqB,mBAAmB,YAAY;GAAE,CAAC,CAAC;EACnI;EAEA,OAAO,KAAK,GAAG,aAAa,KAAK,MAAMA,iBAAAA,wBAAwB,CAAC,CAAC,CAAC;EAClE,OAAO,KAAK,GAAG,eAAe,QAAQ,eAAe,CAAC;CACxD;CAEA,OAAO,KAAK,GAAG,WAAW;CAE1B,OAAOC,iBAAAA,yBAAyB,EAAE,OAAO,CAAC;AAC5C;;;;;;;;AAqBA,SAAS,mBAAmB,EAAE,MAAM,MAAM,QAAQ,WAAW,UAAU,YAAkD;CACvH,IAAI,WAEF,OAAO,CAAC;EAAE;EAAM,MADH,OAAO,UAAU,SAAS,WAAW,SAAS,UAAU,IAAI,IAAI,UAAU;EACjE,UAAU,UAAU;CAAS,CAAC;CAEtD,IAAI,OAAO,QACT,OAAO,CAAC;EAAE;EAAM,MAAM,iBAAiB;GAAE;GAAM;GAAQ;EAAS,CAAC;EAAG,UAAU,OAAO,OAAO,MAAM,CAAC,EAAE,QAAQ;CAAE,CAAC;CAElH,OAAO,CAAC;AACV;;;;;;;;AASA,SAAgB,gBAAgB,MAAoD;CAClF,OAAO,mBAAmB,IAAI,CAAC,CAAC,KAAK,MAAMD,iBAAAA,wBAAwB,CAAC,CAAC;AACvE;;;;;;;AAQA,SAAgB,iBAAiB,EAC/B,MACA,QACA,YAKkB;CAClB,OAAOE,iBAAAA,kBAAkB,EACvB,SAAS,OAAO,KAAK,OAAO;EAC1B,MAAM,EAAE;EACR,MAAM,iBAAiB;GAAE;GAAM,OAAO;GAAG;EAAS,CAAC;EACnD,UAAU,CAAC,EAAE;CACf,EAAE,EACJ,CAAC;AACH"}
import "./chunk-CNktS9qV.js";
import { d as resolveRefName, h as INDENT, m as narrowSchema, n as collectLazy, t as collect, u as resolveGroupType } from "./visitor-DJ6ZEJvq.js";
import { J as camelCase, X as createSchema, _ as createIndexedAccessType, g as createFunctionParameters, h as createFunctionParameter, y as createTypeLiteral } from "./response-KUdWiDWw.js";
//#region ../../internals/utils/src/promise.ts
/**
* Wraps `factory` with a keyed cache backed by the provided store.
*
* Pass a `WeakMap` for object keys (results are GC-eligible when the key is
* collected) or a `Map` for primitive keys. For multi-argument functions,
* nest two `memoize` calls — the outer keyed by the first argument, the
* inner (created once per outer miss) keyed by the second.
*
* Because the cache is owned by the caller, it can be shared, inspected, or
* cleared independently of the memoized function.
*
* @example Single WeakMap key
* ```ts
* const cache = new WeakMap<SchemaNode, Set<string>>()
* const getRefs = memoize(cache, (node) => collectRefs(node))
* ```
*
* @example Single Map key (primitive)
* ```ts
* const cache = new Map<string, Resolver>()
* const getResolver = memoize(cache, (name) => buildResolver(name))
* ```
*
* @example Two-level (object + primitive)
* ```ts
* const outer = new WeakMap<Params[], Map<string, Params[]>>()
* const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
* fn(params)('camelcase')
* ```
*/
function memoize(store, factory) {
return (key) => {
if (store.has(key)) return store.get(key);
const value = factory(key);
store.set(key, value);
return value;
};
}
//#endregion
//#region ../../internals/utils/src/reserved.ts
/**
* JavaScript and Java reserved words.
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
*/
const reservedWords = new Set([
"abstract",
"arguments",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"double",
"else",
"enum",
"eval",
"export",
"extends",
"false",
"final",
"finally",
"float",
"for",
"function",
"goto",
"if",
"implements",
"import",
"in",
"instanceof",
"int",
"interface",
"let",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"typeof",
"var",
"void",
"volatile",
"while",
"with",
"yield",
"Array",
"Date",
"hasOwnProperty",
"Infinity",
"isFinite",
"isNaN",
"isPrototypeOf",
"length",
"Math",
"name",
"NaN",
"Number",
"Object",
"prototype",
"String",
"toString",
"undefined",
"valueOf"
]);
/**
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
*
* @example
* ```ts
* isValidVarName('status') // true
* isValidVarName('class') // false (reserved word)
* isValidVarName('42foo') // false (starts with digit)
* ```
*/
function isValidVarName(name) {
if (!name || reservedWords.has(name)) return false;
return isIdentifier(name);
}
/**
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
*
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
* deciding whether an object key needs quoting.
*
* @example
* ```ts
* isIdentifier('name') // true
* isIdentifier('x-total')// false
* ```
*/
function isIdentifier(name) {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
}
//#endregion
//#region ../../internals/utils/src/string.ts
/**
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
* any backslash or single quote in the content.
*
* @example
* ```ts
* singleQuote('foo') // "'foo'"
* singleQuote("o'clock") // "'o\\'clock'"
* ```
*/
function singleQuote(value) {
if (value === void 0 || value === null) return "''";
return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
}
//#endregion
//#region src/utils/codegen.ts
/**
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no
* comments so callers always get a usable string.
*
* @example
* ```ts
* buildJSDoc(['@type string', '@example hello'])
* // '/**\n * @type string\n * @example hello\n *\/\n '
* ```
*/
function buildJSDoc(comments, options = {}) {
const { indent = " * ", suffix = "\n ", fallback = " " } = options;
if (comments.length === 0) return fallback;
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
}
/**
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
*/
function indentLines(text) {
if (!text) return "";
return text.split("\n").map((line) => line.trim() ? `${INDENT}${line}` : "").join("\n");
}
/**
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
*
* @example
* ```ts
* objectKey('name') // 'name'
* objectKey('x-total') // "'x-total'"
* ```
*/
function objectKey(name) {
return isIdentifier(name) ? name : singleQuote(name);
}
/**
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
* level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
* so callers never re-parse the generated code. A trailing comma is added per entry to match the
* formatter's multi-line style.
*
* @example
* ```ts
* buildObject(['id: z.number()', 'name: z.string()'])
* // '{\n id: z.number(),\n name: z.string(),\n}'
* ```
*/
function buildObject(entries) {
if (entries.length === 0) return "{}";
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
}
/**
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
* one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
* `z.array([…])`, and similar member lists so objects inside them nest correctly.
*
* @example
* ```ts
* buildList(['z.string()', 'z.number()'])
* // '[z.string(), z.number()]'
* ```
*/
function buildList(items, brackets = ["[", "]"]) {
const [open, close] = brackets;
if (items.length === 0) return `${open}${close}`;
if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
}
//#endregion
//#region src/utils/schemaMerge.ts
/**
* Merges a run of adjacent anonymous object members into one. Named or non-object members break the
* run and pass through. Stays a construction-time helper, not a macro, so callers keep control of the
* member boundaries (such as keeping synthetic discriminant objects out of a run).
*
* @example
* ```ts
* const merged = [...mergeAdjacentObjectsLazy([objectA, objectB])]
* ```
*/
function* mergeAdjacentObjectsLazy(members) {
let acc;
for (const member of members) {
const objectMember = narrowSchema(member, "object");
if (objectMember && !objectMember.name && acc !== void 0) {
const accObject = narrowSchema(acc, "object");
if (accObject && !accObject.name) {
acc = createSchema({
...accObject,
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
});
continue;
}
}
if (acc !== void 0) yield acc;
acc = member;
}
if (acc !== void 0) yield acc;
}
//#endregion
//#region src/utils/strings.ts
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
function trimQuotes(text) {
if (text.length >= 2) {
const first = text[0];
const last = text[text.length - 1];
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
}
return text;
}
/**
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
*
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
* code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
function stringify(value) {
if (value === void 0 || value === null) return "''";
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
}
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
function jsStringEscape(input) {
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
switch (character) {
case "\"":
case "'":
case "\\": return `\\${character}`;
case "\n": return "\\n";
case "\r": return "\\r";
case "\u2028": return "\\u2028";
case "\u2029": return "\\u2029";
default: return "";
}
});
}
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
function toRegExpString(text, func = "RegExp") {
const raw = trimQuotes(text);
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
const replacementTarget = match?.[1] ?? "";
const matchedFlags = match?.[2];
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
const { source, flags } = new RegExp(cleaned, matchedFlags);
if (func === null) return `/${source}/${flags}`;
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
}
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
function stringifyObject(value) {
return Object.entries(value).map(([key, val]) => {
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
return `${key}: ${val}`;
}).filter(Boolean).join(",\n");
}
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
function getNestedAccessor(param, accessor) {
const parts = Array.isArray(param) ? param : param.split(".");
if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
}
//#endregion
//#region src/utils/schemaGraph.ts
/**
* Collects every named schema referenced transitively from a node through its ref edges.
*
* Refs are followed by name only, so the resolved `node.schema` is never traversed inline.
*
* @example Collect refs from a single schema
* ```ts
* const names = collectReferencedSchemaNames(petSchema)
* // → Set { 'Category', 'Tag' }
* ```
*
* @example Accumulate refs from multiple schemas into one set
* ```ts
* const out = new Set<string>()
* for (const schema of schemas) {
* collectReferencedSchemaNames(schema, out)
* }
* ```
*/
const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
const refs = /* @__PURE__ */ new Set();
collect(node, { schema(child) {
if (child.type === "ref") {
const name = resolveRefName(child);
if (name) refs.add(name);
}
} });
return refs;
});
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
if (!node) return out;
for (const name of collectSchemaRefs(node)) out.add(name);
return out;
}
/**
* Collects the names of all top-level schemas transitively used by a set of operations.
*
* An operation uses a schema when its parameters, request body, or responses reference it, directly
* or through other named schemas. The walk is iterative, so reference cycles are safe.
*
* Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.
*
* @example Only generate schemas referenced by included operations
* ```ts
* const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)
* const allowed = collectUsedSchemaNames(includedOps, schemas)
*
* for (const schema of schemas) {
* if (schema.name && !allowed.has(schema.name)) continue
* // … generate schema
* }
* ```
*/
const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
function computeUsedSchemaNames(operations, schemas) {
const schemaMap = /* @__PURE__ */ new Map();
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
const result = /* @__PURE__ */ new Set();
function visitSchema(schema) {
const directRefs = collectReferencedSchemaNames(schema);
for (const name of directRefs) if (!result.has(name)) {
result.add(name);
const namedSchema = schemaMap.get(name);
if (namedSchema) visitSchema(namedSchema);
}
}
for (const op of operations) for (const schema of collectLazy(op, {
depth: "shallow",
schema: (node) => node
})) visitSchema(schema);
return result;
}
function collectUsedSchemaNames(operations, schemas) {
return collectUsedSchemaNamesMemo(operations)(schemas);
}
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
const graph = /* @__PURE__ */ new Map();
for (const schema of schemas) {
if (!schema.name) continue;
graph.set(schema.name, collectReferencedSchemaNames(schema));
}
const circular = /* @__PURE__ */ new Set();
for (const start of graph.keys()) {
const visited = /* @__PURE__ */ new Set();
const stack = [...graph.get(start) ?? []];
while (stack.length > 0) {
const node = stack.pop();
if (node === start) {
circular.add(start);
break;
}
if (visited.has(node)) continue;
visited.add(node);
const next = graph.get(node);
if (next) for (const r of next) stack.push(r);
}
}
return circular;
});
/**
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
*
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
* the generated code does not recurse forever. Refs are followed by name only, so the walk stays
* linear in the size of the schema graph.
*
* @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.
*/
function findCircularSchemas(schemas) {
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
return findCircularSchemasMemo(schemas);
}
/**
* Returns `true` when a schema, or anything nested inside it, references a circular schema.
*
* Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled
* on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.
*
* @note Stops at the first matching circular ref.
*/
function containsCircularRef(node, { circularSchemas, excludeName }) {
if (!node || circularSchemas.size === 0) return false;
for (const _ of collectLazy(node, { schema(child) {
if (child.type !== "ref") return null;
const name = resolveRefName(child);
return name && name !== excludeName && circularSchemas.has(name) ? true : null;
} })) return true;
return false;
}
//#endregion
//#region src/utils/schemaTraversal.ts
/**
* Maps each property of an object schema to its transformed output. Pairs every result with the
* original property so the printer keeps full control over modifiers, getters, and key syntax.
*
* @example
* ```ts
* const entries = mapSchemaProperties(node, (schema) => this.transform(schema))
* // entries: [{ name: 'id', property, output: 'z.number()' }, ...]
* ```
*/
function mapSchemaProperties(node, transform) {
return node.properties.map((property) => ({
name: property.name,
property,
output: transform(property.schema)
}));
}
/**
* Maps each member of a union or intersection schema to its transformed output, pairing every
* result with the original member.
*/
function mapSchemaMembers(node, transform) {
return (node.members ?? []).map((schema) => ({
schema,
output: transform(schema)
}));
}
/**
* Maps each item of an array or tuple schema to its transformed output, pairing every result with
* the original item.
*/
function mapSchemaItems(node, transform) {
return (node.items ?? []).map((schema) => ({
schema,
output: transform(schema)
}));
}
/**
* Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key
* is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation
* of a recursive schema until first access.
*
* @example
* ```ts
* lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })
* // "get parent() { return z.lazy(() => Pet) }"
* ```
*/
function lazyGetter({ name, body }) {
return `get ${objectKey(name)}() { return ${body} }`;
}
//#endregion
//#region src/utils/operationParams.ts
/**
* Applies casing rules to parameter names and returns a new array without mutating the input.
*
* Run it before handing parameters to schema builders so output property keys get the right casing
* while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the
* original array is returned unchanged.
*/
const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => {
const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name;
return {
...param,
name: transformed
};
})));
function caseParams(params, casing) {
if (!casing) return params;
return caseParamsMemo(params)(casing);
}
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, it falls back to the schema primitive (a plain type-name string). When the
* parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like
* `GroupParams['petId']`, otherwise the resolved individual name.
*/
function resolveParamType({ node, param, resolver }) {
if (!resolver) return param.schema.primitive ?? "unknown";
const individualName = resolver.resolveParamName(node, param);
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
const groupResolvers = {
path: resolver.resolvePathParamsName,
query: resolver.resolveQueryParamsName,
header: resolver.resolveHeaderParamsName
};
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
if (groupName && groupName !== individualName) return createIndexedAccessType({
objectType: groupName,
indexType: param.name
});
return individualName;
}
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
function createOperationParams(node, options) {
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options;
const dataName = paramNames?.data ?? "data";
const paramsName = paramNames?.params ?? "params";
const headersName = paramNames?.headers ?? "headers";
const pathName = paramNames?.path ?? "pathParams";
const wrapType = (type) => typeWrapper ? typeWrapper(type) : type;
const wrapTypeExpression = (type) => typeof type === "string" ? wrapType(type) : type;
const casedParams = caseParams(node.parameters, paramsCasing);
const pathParams = casedParams.filter((p) => p.in === "path");
const queryParams = casedParams.filter((p) => p.in === "query");
const headerParams = casedParams.filter((p) => p.in === "header");
const toProperty = (param) => ({
name: param.name,
type: wrapTypeExpression(resolveParamType({
node,
param,
resolver
})),
optional: !param.required
});
const emptyObjectDefault = (props) => props.every((p) => p.optional) ? "{}" : void 0;
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
const bodyProperty = bodyType ? [{
name: dataName,
type: bodyType,
optional: !(node.requestBody?.required ?? false)
}] : [];
const trailingGroups = [{
name: paramsName,
node,
params: queryParams,
groupType: resolveGroupType({
node,
params: queryParams,
group: "query",
resolver
}),
resolver,
wrapType
}, {
name: headersName,
node,
params: headerParams,
groupType: resolveGroupType({
node,
params: headerParams,
group: "header",
resolver
}),
resolver,
wrapType
}];
const params = [];
if (paramsType === "object") {
const children = [
...pathParams.map(toProperty),
...bodyProperty,
...trailingGroups.flatMap(buildGroupProperty)
];
if (children.length) params.push(createFunctionParameter({
properties: children,
default: emptyObjectDefault(children)
}));
} else {
if (pathParamsType === "inlineSpread" && pathParams.length) {
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]);
params.push(createFunctionParameter({
name: pathName,
type: spreadType ? wrapType(spreadType) : void 0,
rest: true
}));
} else if (pathParamsType === "inline") params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))));
else if (pathParams.length) {
const pathChildren = pathParams.map(toProperty);
params.push(createFunctionParameter({
properties: pathChildren,
default: pathParamsDefault ?? emptyObjectDefault(pathChildren)
}));
}
params.push(...bodyProperty.map((p) => createFunctionParameter(p)));
params.push(...trailingGroups.flatMap(buildGroupParam));
}
params.push(...extraParams);
return createFunctionParameters({ params });
}
/**
* Builds the property descriptor for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }) {
if (groupType) return [{
name,
type: typeof groupType.type === "string" ? wrapType(groupType.type) : groupType.type,
optional: groupType.optional
}];
if (params.length) return [{
name,
type: buildTypeLiteral({
node,
params,
resolver
}),
optional: params.every((p) => !p.required)
}];
return [];
}
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupParam(args) {
return buildGroupProperty(args).map((p) => createFunctionParameter(p));
}
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
function buildTypeLiteral({ node, params, resolver }) {
return createTypeLiteral({ members: params.map((p) => ({
name: p.name,
type: resolveParamType({
node,
param: p,
resolver
}),
optional: !p.required
})) });
}
//#endregion
export { objectKey as C, buildObject as S, toRegExpString as _, resolveParamType as a, buildJSDoc as b, mapSchemaMembers as c, containsCircularRef as d, findCircularSchemas as f, stringifyObject as g, stringify as h, createOperationParams as i, mapSchemaProperties as l, jsStringEscape as m, buildTypeLiteral as n, lazyGetter as o, getNestedAccessor as p, caseParams as r, mapSchemaItems as s, buildGroupParam as t, collectUsedSchemaNames as u, trimQuotes as v, isValidVarName as w, buildList as x, mergeAdjacentObjectsLazy as y };
//# sourceMappingURL=utils-CF_-Pn_c.js.map
{"version":3,"file":"utils-CF_-Pn_c.js","names":[],"sources":["../../../internals/utils/src/promise.ts","../../../internals/utils/src/reserved.ts","../../../internals/utils/src/string.ts","../src/utils/codegen.ts","../src/utils/schemaMerge.ts","../src/utils/strings.ts","../src/utils/schemaGraph.ts","../src/utils/schemaTraversal.ts","../src/utils/operationParams.ts"],"sourcesContent":["function* chunks<T>(arr: ReadonlyArray<T>, size: number): Generator<Array<T>> {\n for (let i = 0; i < arr.length; i += size) {\n yield arr.slice(i, i + size)\n }\n}\n\nexport type ForBatchesOptions = {\n /**\n * Maximum batch size handed to `process`.\n * Parallel dispatch within a batch is the caller's responsibility\n * (typically via `Promise.all(batch.map(...))`).\n */\n concurrency: number\n /**\n * Called after every batch.\n *\n * Use a cheap, idempotent callback (e.g. one that short-circuits when there\n * is nothing new to do). The helper does not coalesce calls — if you need\n * throttling, do it inside `flush` itself.\n */\n flush?: () => Promise<void>\n}\n\n/**\n * Slices `source` into batches of `concurrency` items and awaits `process` for each batch.\n * Accepts both plain arrays (sync) and `AsyncIterable` (streaming).\n *\n * `process` controls whether items inside a batch run in parallel; this helper only\n * controls batch size and per-batch flushing.\n *\n * @example\n * ```ts\n * // parallel dispatch inside each batch\n * await forBatches(schemas, (batch) => Promise.all(batch.map(process)), { concurrency: 8 })\n *\n * // async iterable with a flush after every batch\n * await forBatches(stream.schemas, (batch) => dispatch(batch), { concurrency: 8, flush })\n * ```\n */\nexport async function forBatches<T>(\n source: ReadonlyArray<T> | AsyncIterable<T>,\n process: (batch: Array<T>) => Promise<unknown>,\n options: ForBatchesOptions,\n): Promise<void> {\n const { concurrency, flush } = options\n\n if (Array.isArray(source)) {\n for (const batch of chunks(source, concurrency)) {\n await process(batch)\n if (flush) await flush()\n }\n return\n }\n\n const batch: Array<T> = []\n for await (const item of source) {\n batch.push(item)\n if (batch.length >= concurrency) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n }\n if (batch.length > 0) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n}\n\n/** A value that may already be resolved or still pending.\n *\n * @example\n * ```ts\n * function load(id: string): PossiblePromise<string> {\n * return cache.get(id) ?? fetchRemote(id)\n * }\n * ```\n */\nexport type PossiblePromise<T> = Promise<T> | T\n\n/** Returns `true` when `result` is a thenable `Promise`.\n *\n * @example\n * ```ts\n * isPromise(Promise.resolve(1)) // true\n * isPromise(42) // false\n * ```\n */\nexport function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {\n return result !== null && result !== undefined && typeof (result as Record<string, unknown>)['then'] === 'function'\n}\n\n/** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)\n * ```\n */\nexport function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & { reason: T } {\n return result.status === 'rejected'\n}\n\ntype Store<TKey, TValue> = {\n has(key: TKey): boolean\n get(key: TKey): TValue | undefined\n set(key: TKey, value: TValue): unknown\n}\n\n/**\n * Wraps `factory` with a keyed cache backed by the provided store.\n *\n * Pass a `WeakMap` for object keys (results are GC-eligible when the key is\n * collected) or a `Map` for primitive keys. For multi-argument functions,\n * nest two `memoize` calls — the outer keyed by the first argument, the\n * inner (created once per outer miss) keyed by the second.\n *\n * Because the cache is owned by the caller, it can be shared, inspected, or\n * cleared independently of the memoized function.\n *\n * @example Single WeakMap key\n * ```ts\n * const cache = new WeakMap<SchemaNode, Set<string>>()\n * const getRefs = memoize(cache, (node) => collectRefs(node))\n * ```\n *\n * @example Single Map key (primitive)\n * ```ts\n * const cache = new Map<string, Resolver>()\n * const getResolver = memoize(cache, (name) => buildResolver(name))\n * ```\n *\n * @example Two-level (object + primitive)\n * ```ts\n * const outer = new WeakMap<Params[], Map<string, Params[]>>()\n * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))\n * fn(params)('camelcase')\n * ```\n */\nexport function memoize<TKey, TValue>(store: Store<TKey, TValue>, factory: (key: TKey) => TValue): (key: TKey) => TValue {\n return (key: TKey): TValue => {\n if (store.has(key)) return store.get(key)!\n const value = factory(key)\n store.set(key, value)\n return value\n }\n}\n\n/**\n * Container that switches between an eager `Array<T>` and a lazy `AsyncIterable<T>`.\n *\n * `Array<T>` by default. With `Stream` set to `true` it becomes `AsyncIterable<T>`, so large\n * collections can be produced lazily without holding every item in memory. Pairs with\n * {@link arrayToAsyncIterable}, which lifts a plain array into the streaming form.\n *\n * @example\n * ```ts\n * type Eager = Streamable<number> // Array<number>\n * type Lazy = Streamable<number, true> // AsyncIterable<number>\n * ```\n */\nexport type Streamable<T, Stream extends boolean = false> = Stream extends true ? AsyncIterable<T> : Array<T>\n\n/**\n * Wraps a plain array in a reusable `AsyncIterable`.\n * Each `[Symbol.asyncIterator]()` call returns a fresh generator so the\n * iterable can be consumed multiple times (e.g. once per plugin pre-scan).\n *\n * @example\n * ```ts\n * const stream = arrayToAsyncIterable([1, 2, 3])\n * for await (const n of stream) console.log(n) // 1, 2, 3\n * ```\n */\nexport function arrayToAsyncIterable<T>(arr: ReadonlyArray<T>): AsyncIterable<T> {\n return {\n [Symbol.asyncIterator]() {\n return (async function* () {\n yield* arr\n })()\n },\n }\n}\n","/**\n * JavaScript and Java reserved words.\n * @link https://github.com/jonschlinkert/reserved/blob/master/index.js\n */\nconst reservedWords = new Set([\n 'abstract',\n 'arguments',\n 'boolean',\n 'break',\n 'byte',\n 'case',\n 'catch',\n 'char',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'double',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'final',\n 'finally',\n 'float',\n 'for',\n 'function',\n 'goto',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'int',\n 'interface',\n 'let',\n 'long',\n 'native',\n 'new',\n 'null',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'short',\n 'static',\n 'super',\n 'switch',\n 'synchronized',\n 'this',\n 'throw',\n 'throws',\n 'transient',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'volatile',\n 'while',\n 'with',\n 'yield',\n 'Array',\n 'Date',\n 'hasOwnProperty',\n 'Infinity',\n 'isFinite',\n 'isNaN',\n 'isPrototypeOf',\n 'length',\n 'Math',\n 'name',\n 'NaN',\n 'Number',\n 'Object',\n 'prototype',\n 'String',\n 'toString',\n 'undefined',\n 'valueOf',\n] as const)\n\n/**\n * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.\n *\n * @example\n * ```ts\n * transformReservedWord('class') // '_class'\n * transformReservedWord('42foo') // '_42foo'\n * transformReservedWord('status') // 'status'\n * ```\n */\nexport function transformReservedWord(word: string): string {\n const firstChar = word.charCodeAt(0)\n if (word && (reservedWords.has(word as 'valueOf') || (firstChar >= 48 && firstChar <= 57))) {\n return `_${word}`\n }\n return word\n}\n\n/**\n * Returns `true` when `name` is a syntactically valid JavaScript variable name.\n *\n * @example\n * ```ts\n * isValidVarName('status') // true\n * isValidVarName('class') // false (reserved word)\n * isValidVarName('42foo') // false (starts with digit)\n * ```\n */\nexport function isValidVarName(name: string): boolean {\n if (!name || reservedWords.has(name as 'valueOf')) {\n return false\n }\n return isIdentifier(name)\n}\n\n/**\n * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.\n *\n * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys\n * even though they are not valid variable names, so use this (not {@link isValidVarName}) when\n * deciding whether an object key needs quoting.\n *\n * @example\n * ```ts\n * isIdentifier('name') // true\n * isIdentifier('x-total')// false\n * ```\n */\nexport function isIdentifier(name: string): boolean {\n return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)\n}\n","/**\n * Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping\n * any backslash or single quote in the content.\n *\n * @example\n * ```ts\n * singleQuote('foo') // \"'foo'\"\n * singleQuote(\"o'clock\") // \"'o\\\\'clock'\"\n * ```\n */\nexport function singleQuote(value: string | number | boolean | undefined | null): string {\n if (value === undefined || value === null) return \"''\"\n const escaped = String(value).replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")\n\n return `'${escaped}'`\n}\n","import { isIdentifier, singleQuote } from '@internals/utils'\nimport { INDENT } from '../constants.ts'\n\n/**\n * Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no\n * comments so callers always get a usable string.\n *\n * @example\n * ```ts\n * buildJSDoc(['@type string', '@example hello'])\n * // '/**\\n * @type string\\n * @example hello\\n *\\/\\n '\n * ```\n */\nexport function buildJSDoc(\n comments: Array<string>,\n options: {\n /**\n * String used to indent each comment line.\n * @default ' * '\n */\n indent?: string\n /**\n * String appended after the closing tag.\n * @default '\\n '\n */\n suffix?: string\n /**\n * Returned as-is when `comments` is empty.\n * @default ' '\n */\n fallback?: string\n } = {},\n): string {\n const { indent = ' * ', suffix = '\\n ', fallback = ' ' } = options\n\n if (comments.length === 0) return fallback\n\n return `/**\\n${comments.map((c) => `${indent}${c}`).join('\\n')}\\n */${suffix}`\n}\n\n/**\n * Indents every non-empty line of `text` by one indent level, leaving blank lines empty.\n */\nfunction indentLines(text: string): string {\n if (!text) return ''\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${INDENT}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Renders an object key, quoting it with single quotes only when it is not a valid identifier.\n * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.\n *\n * @example\n * ```ts\n * objectKey('name') // 'name'\n * objectKey('x-total') // \"'x-total'\"\n * ```\n */\nexport function objectKey(name: string): string {\n return isIdentifier(name) ? name : singleQuote(name)\n}\n\n/**\n * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one\n * level and closing the brace at column zero. Nested objects built the same way indent cumulatively,\n * so callers never re-parse the generated code. A trailing comma is added per entry to match the\n * formatter's multi-line style.\n *\n * @example\n * ```ts\n * buildObject(['id: z.number()', 'name: z.string()'])\n * // '{\\n id: z.number(),\\n name: z.string(),\\n}'\n * ```\n */\nexport function buildObject(entries: Array<string>): string {\n if (entries.length === 0) return '{}'\n const body = entries.map((entry) => `${indentLines(entry)},`).join('\\n')\n\n return `{\\n${body}\\n}`\n}\n\n/**\n * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on\n * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented\n * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,\n * `z.array([…])`, and similar member lists so objects inside them nest correctly.\n *\n * @example\n * ```ts\n * buildList(['z.string()', 'z.number()'])\n * // '[z.string(), z.number()]'\n * ```\n */\nexport function buildList(items: Array<string>, brackets: [open: string, close: string] = ['[', ']']): string {\n const [open, close] = brackets\n if (items.length === 0) return `${open}${close}`\n if (!items.some((item) => item.includes('\\n'))) return `${open}${items.join(', ')}${close}`\n const body = items.map((item) => `${indentLines(item)},`).join('\\n')\n\n return `${open}\\n${body}\\n${close}`\n}\n","import { narrowSchema } from '../guards.ts'\nimport { createSchema, type SchemaNode } from '../nodes/schema.ts'\n\n/**\n * Merges a run of adjacent anonymous object members into one. Named or non-object members break the\n * run and pass through. Stays a construction-time helper, not a macro, so callers keep control of the\n * member boundaries (such as keeping synthetic discriminant objects out of a run).\n *\n * @example\n * ```ts\n * const merged = [...mergeAdjacentObjectsLazy([objectA, objectB])]\n * ```\n */\nexport function* mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined> {\n let acc: SchemaNode | undefined\n\n for (const member of members) {\n const objectMember = narrowSchema(member, 'object')\n if (objectMember && !objectMember.name && acc !== undefined) {\n const accObject = narrowSchema(acc, 'object')\n if (accObject && !accObject.name) {\n acc = createSchema({\n ...accObject,\n properties: [...(accObject.properties ?? []), ...(objectMember.properties ?? [])],\n })\n continue\n }\n }\n if (acc !== undefined) yield acc\n acc = member\n }\n\n if (acc !== undefined) yield acc\n}\n","/**\n * Strips a single matching pair of `\"...\"`, `'...'`, or `` `...` `` from both ends of `text`.\n * Returns the string unchanged when no balanced quote pair is found.\n *\n * @example\n * ```ts\n * trimQuotes('\"hello\"') // 'hello'\n * trimQuotes('hello') // 'hello'\n * ```\n */\nexport function trimQuotes(text: string): string {\n if (text.length >= 2) {\n const first = text[0]\n const last = text[text.length - 1]\n if ((first === '\"' && last === '\"') || (first === \"'\" && last === \"'\") || (first === '`' && last === '`')) {\n return text.slice(1, -1)\n }\n }\n return text\n}\n\n/**\n * Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.\n *\n * Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated\n * code matches the repo style without a formatter.\n *\n * @example\n * ```ts\n * stringify('hello') // \"'hello'\"\n * stringify('\"hello\"') // \"'hello'\"\n * ```\n */\nexport function stringify(value: string | number | boolean | undefined): string {\n if (value === undefined || value === null) return \"''\"\n const json = JSON.stringify(trimQuotes(value.toString()))\n const inner = json.slice(1, -1).replace(/\\\\\"/g, '\"').replace(/'/g, \"\\\\'\")\n return `'${inner}'`\n}\n\n/**\n * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,\n * and the Unicode line terminators U+2028 and U+2029.\n *\n * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4\n *\n * @example\n * ```ts\n * jsStringEscape('say \"hi\"\\nbye') // 'say \\\\\"hi\\\\\"\\\\nbye'\n * ```\n */\nexport function jsStringEscape(input: unknown): string {\n return `${input}`.replace(/[\"'\\\\\\n\\r\\u2028\\u2029]/g, (character) => {\n switch (character) {\n case '\"':\n case \"'\":\n case '\\\\':\n return `\\\\${character}`\n case '\\n':\n return '\\\\n'\n case '\\r':\n return '\\\\r'\n case '\\u2028':\n return '\\\\u2028'\n case '\\u2029':\n return '\\\\u2029'\n default:\n return ''\n }\n })\n}\n\n/**\n * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.\n * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.\n * Pass `null` as the second argument to emit a `/pattern/flags` literal instead.\n *\n * @example\n * ```ts\n * toRegExpString('^(?im)foo') // 'new RegExp(\"^foo\", \"im\")'\n * toRegExpString('^(?im)foo', null) // '/^foo/im'\n * ```\n */\nexport function toRegExpString(text: string, func: string | null = 'RegExp'): string {\n const raw = trimQuotes(text)\n\n const match = raw.match(/^\\^(\\(\\?([igmsuy]+)\\))/i)\n const replacementTarget = match?.[1] ?? ''\n const matchedFlags = match?.[2]\n const cleaned = raw\n .replace(/^\\\\?\\//, '')\n .replace(/\\\\?\\/$/, '')\n .replace(replacementTarget, '')\n\n const { source, flags } = new RegExp(cleaned, matchedFlags)\n\n if (func === null) return `/${source}/${flags}`\n\n return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ''})`\n}\n\n/**\n * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested\n * objects recurse with fixed indentation, so the result drops straight into an object literal\n * without re-parsing.\n *\n * @example\n * ```ts\n * stringifyObject({ foo: 'bar', nested: { a: 1 } })\n * // 'foo: bar,\\nnested: {\\n a: 1\\n }'\n * ```\n */\nexport function stringifyObject(value: Record<string, unknown>): string {\n const items = Object.entries(value)\n .map(([key, val]) => {\n if (val !== null && typeof val === 'object') {\n return `${key}: {\\n ${stringifyObject(val as Record<string, unknown>)}\\n }`\n }\n return `${key}: ${val}`\n })\n .filter(Boolean)\n return items.join(',\\n')\n}\n\n/**\n * Renders a dotted path or string array as an optional-chaining accessor expression rooted at\n * `accessor`. Returns `null` for an empty path.\n *\n * @example\n * ```ts\n * getNestedAccessor('pagination.next.id', 'lastPage')\n * // \"lastPage?.['pagination']?.['next']?.['id']\"\n * ```\n */\nexport function getNestedAccessor(param: string | Array<string>, accessor: string): string | null {\n const parts = Array.isArray(param) ? param : param.split('.')\n if (parts.length === 0 || (parts.length === 1 && parts[0] === '')) return null\n return `${accessor}?.['${`${parts.join(\"']?.['\")}']`}`\n}\n","import { memoize } from '@internals/utils'\nimport type { OperationNode, SchemaNode } from '../nodes/index.ts'\nimport { collect, collectLazy } from '../visitor.ts'\nimport { resolveRefName } from './refs.ts'\n\n/**\n * Collects every named schema referenced transitively from a node through its ref edges.\n *\n * Refs are followed by name only, so the resolved `node.schema` is never traversed inline.\n *\n * @example Collect refs from a single schema\n * ```ts\n * const names = collectReferencedSchemaNames(petSchema)\n * // → Set { 'Category', 'Tag' }\n * ```\n *\n * @example Accumulate refs from multiple schemas into one set\n * ```ts\n * const out = new Set<string>()\n * for (const schema of schemas) {\n * collectReferencedSchemaNames(schema, out)\n * }\n * ```\n */\nconst collectSchemaRefs = memoize(new WeakMap<SchemaNode, ReadonlySet<string>>(), (node: SchemaNode): ReadonlySet<string> => {\n const refs = new Set<string>()\n collect<void>(node, {\n schema(child) {\n if (child.type === 'ref') {\n const name = resolveRefName(child)\n if (name) refs.add(name)\n }\n },\n })\n return refs\n})\n\nexport function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {\n if (!node) return out\n for (const name of collectSchemaRefs(node)) out.add(name)\n return out\n}\n\n/**\n * Collects the names of all top-level schemas transitively used by a set of operations.\n *\n * An operation uses a schema when its parameters, request body, or responses reference it, directly\n * or through other named schemas. The walk is iterative, so reference cycles are safe.\n *\n * Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.\n *\n * @example Only generate schemas referenced by included operations\n * ```ts\n * const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)\n * const allowed = collectUsedSchemaNames(includedOps, schemas)\n *\n * for (const schema of schemas) {\n * if (schema.name && !allowed.has(schema.name)) continue\n * // … generate schema\n * }\n * ```\n */\nconst collectUsedSchemaNamesMemo = memoize(new WeakMap<ReadonlyArray<OperationNode>, (schemas: ReadonlyArray<SchemaNode>) => Set<string>>(), (ops) =>\n memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas) => computeUsedSchemaNames(ops, schemas)),\n)\n\nfunction computeUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n const schemaMap = new Map<string, SchemaNode>()\n for (const schema of schemas) {\n if (schema.name) schemaMap.set(schema.name, schema)\n }\n\n const result = new Set<string>()\n\n function visitSchema(schema: SchemaNode): void {\n const directRefs = collectReferencedSchemaNames(schema)\n for (const name of directRefs) {\n if (!result.has(name)) {\n result.add(name)\n const namedSchema = schemaMap.get(name)\n if (namedSchema) visitSchema(namedSchema)\n }\n }\n }\n\n for (const op of operations) {\n for (const schema of collectLazy<SchemaNode>(op, { depth: 'shallow', schema: (node) => node })) {\n visitSchema(schema)\n }\n }\n\n return result\n}\n\nexport function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n return collectUsedSchemaNamesMemo(operations)(schemas)\n}\n\nconst EMPTY_CIRCULAR_SET = new Set<string>()\n\nconst findCircularSchemasMemo = memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas: ReadonlyArray<SchemaNode>): Set<string> => {\n const graph = new Map<string, Set<string>>()\n\n for (const schema of schemas) {\n if (!schema.name) continue\n graph.set(schema.name, collectReferencedSchemaNames(schema))\n }\n\n const circular = new Set<string>()\n for (const start of graph.keys()) {\n const visited = new Set<string>()\n const stack: Array<string> = [...(graph.get(start) ?? [])]\n while (stack.length > 0) {\n const node = stack.pop()!\n if (node === start) {\n circular.add(start)\n break\n }\n if (visited.has(node)) continue\n visited.add(node)\n\n const next = graph.get(node)\n if (next) for (const r of next) stack.push(r)\n }\n }\n\n return circular\n})\n\n/**\n * Finds every schema that takes part in a circular dependency chain, including direct self-loops.\n *\n * Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so\n * the generated code does not recurse forever. Refs are followed by name only, so the walk stays\n * linear in the size of the schema graph.\n *\n * @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.\n */\nexport function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string> {\n if (schemas.length === 0) return EMPTY_CIRCULAR_SET\n return findCircularSchemasMemo(schemas)\n}\n\n/**\n * Returns `true` when a schema, or anything nested inside it, references a circular schema.\n *\n * Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled\n * on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.\n *\n * @note Stops at the first matching circular ref.\n */\nexport function containsCircularRef(\n node: SchemaNode | undefined,\n { circularSchemas, excludeName }: { circularSchemas: ReadonlySet<string>; excludeName?: string },\n): boolean {\n if (!node || circularSchemas.size === 0) return false\n\n for (const _ of collectLazy<true>(node, {\n schema(child) {\n if (child.type !== 'ref') return null\n const name = resolveRefName(child)\n return name && name !== excludeName && circularSchemas.has(name) ? true : null\n },\n })) {\n return true\n }\n\n return false\n}\n","import type { ArraySchemaNode, IntersectionSchemaNode, ObjectSchemaNode, PropertyNode, SchemaNode, UnionSchemaNode } from '../nodes/index.ts'\nimport { objectKey } from './codegen.ts'\n\n/**\n * Converts a child schema to printer output. Plugins instantiate it with their own output type:\n * `string` for the zod and faker printers, `ts.TypeNode` for the TypeScript printer. A printer's\n * `this.transform` fits directly, so its `null` for an empty result carries through to `output`.\n */\nexport type SchemaTransform<TOutput> = (schema: SchemaNode) => TOutput\n\n/**\n * A union or intersection member, or an array or tuple item, paired with its transformed output.\n */\nexport type MappedSchema<TOutput> = {\n /**\n * The original child schema, kept so the printer can read its metadata for leaf formatting.\n */\n schema: SchemaNode\n /**\n * The child schema after being run through the transform.\n */\n output: TOutput\n}\n\n/**\n * An object property paired with its transformed output.\n */\nexport type MappedProperty<TOutput> = {\n /**\n * The property name as written on the schema, before any identifier quoting.\n */\n name: string\n /**\n * The original property node, kept so the printer can read `required`, `schema`, and metadata.\n */\n property: PropertyNode\n /**\n * The property schema after being run through the transform.\n */\n output: TOutput\n}\n\n/**\n * Maps each property of an object schema to its transformed output. Pairs every result with the\n * original property so the printer keeps full control over modifiers, getters, and key syntax.\n *\n * @example\n * ```ts\n * const entries = mapSchemaProperties(node, (schema) => this.transform(schema))\n * // entries: [{ name: 'id', property, output: 'z.number()' }, ...]\n * ```\n */\nexport function mapSchemaProperties<TOutput>(node: ObjectSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedProperty<TOutput>> {\n return node.properties.map((property) => ({ name: property.name, property, output: transform(property.schema) }))\n}\n\n/**\n * Maps each member of a union or intersection schema to its transformed output, pairing every\n * result with the original member.\n */\nexport function mapSchemaMembers<TOutput>(node: UnionSchemaNode | IntersectionSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> {\n return (node.members ?? []).map((schema) => ({ schema, output: transform(schema) }))\n}\n\n/**\n * Maps each item of an array or tuple schema to its transformed output, pairing every result with\n * the original item.\n */\nexport function mapSchemaItems<TOutput>(node: ArraySchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> {\n return (node.items ?? []).map((schema) => ({ schema, output: transform(schema) }))\n}\n\n/**\n * Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key\n * is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation\n * of a recursive schema until first access.\n *\n * @example\n * ```ts\n * lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })\n * // \"get parent() { return z.lazy(() => Pet) }\"\n * ```\n */\nexport function lazyGetter({ name, body }: { name: string; body: string }): string {\n return `get ${objectKey(name)}() { return ${body} }`\n}\n","import { camelCase, isValidVarName, memoize } from '@internals/utils'\nimport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createTypeLiteral } from '../nodes/function.ts'\nimport type { FunctionParameterNode, FunctionParametersNode, OperationNode, ParameterNode, TypeExpression, TypeLiteralNode } from '../nodes/index.ts'\nimport { resolveGroupType } from './refs.ts'\n\n/**\n * Applies casing rules to parameter names and returns a new array without mutating the input.\n *\n * Run it before handing parameters to schema builders so output property keys get the right casing\n * while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the\n * original array is returned unchanged.\n */\nconst caseParamsMemo = memoize(new WeakMap<Array<ParameterNode>, (casing: string) => Array<ParameterNode>>(), (params) =>\n memoize(new Map<string, Array<ParameterNode>>(), (casing: string) =>\n params.map((param) => {\n const transformed = casing === 'camelcase' || !isValidVarName(param.name) ? camelCase(param.name) : param.name\n return { ...param, name: transformed }\n }),\n ),\n)\n\nexport function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {\n if (!casing) return params\n return caseParamsMemo(params)(casing)\n}\n\n/**\n * Named type for a group of parameters (query or header) emitted as a single typed parameter.\n */\nexport type ParamGroupType = {\n /**\n * Type expression for the group, a plain group-name reference.\n */\n type: TypeExpression\n /**\n * Whether the parameter group is optional.\n */\n optional: boolean\n}\n\n/**\n * A single member of a destructured parameter group, fed to `createFunctionParameter({ properties })`.\n */\ntype GroupProperty = {\n name: string\n type: TypeExpression\n optional?: boolean\n}\n\n/**\n * Resolver interface for {@link createOperationParams}.\n *\n * `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.\n */\nexport type OperationParamsResolver = {\n /**\n * Resolves the type name for an individual parameter.\n *\n * @example Individual path parameter name\n * `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`\n */\n resolveParamName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the request body type name.\n *\n * @example Request body type name\n * `resolver.resolveDataName(node) // → 'CreatePetData'`\n */\n resolveDataName(node: OperationNode): string\n /**\n * Resolves the grouped path parameters type name.\n * When the return value equals `resolveParamName`, no indexed access is emitted.\n *\n * @example Grouped path params type name\n * `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`\n */\n resolvePathParamsName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the grouped query parameters type name.\n * When the return value equals `resolveParamName`, an inline struct type is emitted instead.\n *\n * @example Grouped query params type name\n * `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`\n */\n resolveQueryParamsName(node: OperationNode, param: ParameterNode): string\n /**\n * Resolves the grouped header parameters type name.\n * When the return value equals `resolveParamName`, an inline struct type is emitted instead.\n *\n * @example Grouped header params type name\n * `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`\n */\n resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string\n}\n\n/**\n * Options for {@link createOperationParams}.\n */\nexport type CreateOperationParamsOptions = {\n /**\n * How all operation parameters are grouped in the function signature.\n * - `'object'` wraps all params into a single destructured object `{ petId, data, params }`\n * - `'inline'` emits each param category as a separate top-level parameter\n */\n paramsType: 'object' | 'inline'\n /**\n * How path parameters are emitted when `paramsType` is `'inline'`.\n * - `'object'` groups them as `{ petId, storeId }: PathParams`\n * - `'inline'` spreads them as individual parameters `petId: string, storeId: string`\n * - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`\n */\n pathParamsType: 'object' | 'inline' | 'inlineSpread'\n /**\n * Converts parameter names to camelCase before output.\n */\n paramsCasing?: 'camelcase'\n /**\n * Resolver for parameter and request body type names.\n * Pass `ResolverTs` from `@kubb/plugin-ts` directly.\n * When omitted, falls back to the schema primitive or `'unknown'`.\n */\n resolver?: OperationParamsResolver\n /**\n * Default value for the path parameters binding when `pathParamsType` is `'object'`.\n * Falls back to `'{}'` when all path params are optional.\n */\n pathParamsDefault?: string\n /**\n * Extra parameters appended after the standard operation parameters.\n *\n * @example Plugin-specific trailing parameter\n * ```ts\n * extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]\n * ```\n */\n extraParams?: Array<FunctionParameterNode>\n /**\n * Override the default parameter names used for body, query, header, and rest-path groups.\n *\n * Useful when targeting languages or frameworks with different naming conventions.\n *\n * @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }\n */\n paramNames?: {\n /**\n * Name for the request body parameter.\n * @default 'data'\n */\n data?: string\n /**\n * Name for the query parameters group parameter.\n * @default 'params'\n */\n params?: string\n /**\n * Name for the header parameters group parameter.\n * @default 'headers'\n */\n headers?: string\n /**\n * Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.\n * @default 'pathParams'\n */\n path?: string\n }\n /**\n * Transforms every resolved type name before it lands in a parameter node, for framework-level\n * type wrappers.\n *\n * @example Vue Query, wrap every parameter type with `MaybeRefOrGetter`\n * `typeWrapper: (t) => \\`MaybeRefOrGetter<${t}>\\``\n */\n typeWrapper?: (type: string) => string\n}\n\n/**\n * Resolves the {@link TypeExpression} for an individual parameter.\n *\n * Without a resolver, it falls back to the schema primitive (a plain type-name string). When the\n * parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like\n * `GroupParams['petId']`, otherwise the resolved individual name.\n */\nexport function resolveParamType({\n node,\n param,\n resolver,\n}: {\n node: OperationNode\n param: ParameterNode\n resolver: OperationParamsResolver | undefined\n}): TypeExpression {\n if (!resolver) {\n return param.schema.primitive ?? 'unknown'\n }\n\n const individualName = resolver.resolveParamName(node, param)\n\n const groupLocation = param.in === 'path' || param.in === 'query' || param.in === 'header' ? param.in : undefined\n\n const groupResolvers = {\n path: resolver.resolvePathParamsName,\n query: resolver.resolveQueryParamsName,\n header: resolver.resolveHeaderParamsName,\n } as const\n\n const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : undefined\n\n if (groupName && groupName !== individualName) {\n return createIndexedAccessType({ objectType: groupName, indexType: param.name })\n }\n\n return individualName\n}\n\n/**\n * Converts an `OperationNode` into function parameters for code generation.\n *\n * Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one\n * destructured object parameter (`object`) and separate top-level parameters (`inline`), while\n * `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type\n * name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.\n */\nexport function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode {\n const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options\n\n const dataName = paramNames?.data ?? 'data'\n const paramsName = paramNames?.params ?? 'params'\n const headersName = paramNames?.headers ?? 'headers'\n const pathName = paramNames?.path ?? 'pathParams'\n\n const wrapType = (type: string): string => (typeWrapper ? typeWrapper(type) : type)\n // Only plain type-name references are wrapped, they need casing applied.\n // TypeLiteral and IndexedAccessType expressions are pre-resolved and pass through unchanged.\n const wrapTypeExpression = (type: TypeExpression): TypeExpression => (typeof type === 'string' ? wrapType(type) : type)\n\n const casedParams = caseParams(node.parameters, paramsCasing)\n const pathParams = casedParams.filter((p) => p.in === 'path')\n const queryParams = casedParams.filter((p) => p.in === 'query')\n const headerParams = casedParams.filter((p) => p.in === 'header')\n\n const toProperty = (param: ParameterNode): GroupProperty => ({\n name: param.name,\n type: wrapTypeExpression(resolveParamType({ node, param, resolver })),\n optional: !param.required,\n })\n const emptyObjectDefault = (props: Array<GroupProperty>): string | undefined => (props.every((p) => p.optional) ? '{}' : undefined)\n\n const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? 'unknown') : undefined\n const bodyProperty: Array<GroupProperty> = bodyType ? [{ name: dataName, type: bodyType, optional: !(node.requestBody?.required ?? false) }] : []\n\n const trailingGroups: Array<BuildGroupArgs> = [\n { name: paramsName, node, params: queryParams, groupType: resolveGroupType({ node, params: queryParams, group: 'query', resolver }), resolver, wrapType },\n {\n name: headersName,\n node,\n params: headerParams,\n groupType: resolveGroupType({ node, params: headerParams, group: 'header', resolver }),\n resolver,\n wrapType,\n },\n ]\n\n const params: Array<FunctionParameterNode> = []\n\n if (paramsType === 'object') {\n const children = [...pathParams.map(toProperty), ...bodyProperty, ...trailingGroups.flatMap(buildGroupProperty)]\n if (children.length) {\n params.push(createFunctionParameter({ properties: children, default: emptyObjectDefault(children) }))\n }\n } else {\n if (pathParamsType === 'inlineSpread' && pathParams.length) {\n const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]!)\n params.push(createFunctionParameter({ name: pathName, type: spreadType ? wrapType(spreadType) : undefined, rest: true }))\n } else if (pathParamsType === 'inline') {\n params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))))\n } else if (pathParams.length) {\n const pathChildren = pathParams.map(toProperty)\n params.push(createFunctionParameter({ properties: pathChildren, default: pathParamsDefault ?? emptyObjectDefault(pathChildren) }))\n }\n\n params.push(...bodyProperty.map((p) => createFunctionParameter(p)))\n params.push(...trailingGroups.flatMap(buildGroupParam))\n }\n\n params.push(...extraParams)\n\n return createFunctionParameters({ params })\n}\n\n/**\n * Shared arguments for building a query or header parameter group.\n */\nexport type BuildGroupArgs = {\n name: string\n node: OperationNode\n params: Array<ParameterNode>\n groupType: ParamGroupType | null\n resolver: OperationParamsResolver | undefined\n wrapType: (type: string) => string\n}\n\n/**\n * Builds the property descriptor for a query or header group.\n * Returns an empty array when there are no params to emit.\n *\n * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline\n * {@link TypeLiteralNode} from the individual params.\n */\nfunction buildGroupProperty({ name, node, params, groupType, resolver, wrapType }: BuildGroupArgs): Array<GroupProperty> {\n if (groupType) {\n const type = typeof groupType.type === 'string' ? wrapType(groupType.type) : groupType.type\n return [{ name, type, optional: groupType.optional }]\n }\n if (params.length) {\n return [{ name, type: buildTypeLiteral({ node, params, resolver }), optional: params.every((p) => !p.required) }]\n }\n return []\n}\n\n/**\n * Builds a single {@link FunctionParameterNode} for a query or header group.\n * Returns an empty array when there are no params to emit.\n *\n * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline\n * {@link TypeLiteralNode} from the individual params.\n */\nexport function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode> {\n return buildGroupProperty(args).map((p) => createFunctionParameter(p))\n}\n\n/**\n * Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.\n *\n * Used when query or header parameters have no dedicated group type name.\n * Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).\n */\nexport function buildTypeLiteral({\n node,\n params,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n resolver: OperationParamsResolver | undefined\n}): TypeLiteralNode {\n return createTypeLiteral({\n members: params.map((p) => ({\n name: p.name,\n type: resolveParamType({ node, param: p, resolver }),\n optional: !p.required,\n })),\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;A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CAAC,CAAC;AACvE;;;;;;;AAQA,SAAgB,iBAAiB,EAC/B,MACA,QACA,YAKkB;CAClB,OAAO,kBAAkB,EACvB,SAAS,OAAO,KAAK,OAAO;EAC1B,MAAM,EAAE;EACR,MAAM,iBAAiB;GAAE;GAAM,OAAO;GAAG;EAAS,CAAC;EACnD,UAAU,CAAC,EAAE;CACf,EAAE,EACJ,CAAC;AACH"}
import "./chunk-CNktS9qV.js";
import { A as importDef, C as objectBindingPatternDef, F as breakDef, G as jsxDef, I as constDef, K as textDef, M as contentDef, O as exportDef, P as arrowFunctionDef, S as indexedAccessTypeDef, W as functionDef, X as createSchema, Y as pascalCase, Z as schemaDef, b as functionParameterDef, c as outputDef, f as requestBodyDef, i as propertyDef, j as sourceDef, k as fileDef, m as inputDef, n as responseDef, o as parameterDef, q as typeDef, u as operationDef, w as typeLiteralDef, x as functionParametersDef } from "./response-KUdWiDWw.js";
//#region src/constants.ts
const visitorDepths = {
shallow: "shallow",
deep: "deep"
};
/**
* Schema type discriminators used by all AST schema nodes.
*
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/
const schemaTypes = {
/**
* Text value.
*/
string: "string",
/**
* Floating-point number (`float`, `double`).
*/
number: "number",
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
integer: "integer",
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
bigint: "bigint",
/**
* Boolean value.
*/
boolean: "boolean",
/**
* Explicit null value.
*/
null: "null",
/**
* Any value (no type restriction).
*/
any: "any",
/**
* Unknown value (must be narrowed before usage).
*/
unknown: "unknown",
/**
* No return value (`void`).
*/
void: "void",
/**
* Object with named properties.
*/
object: "object",
/**
* Sequential list of items.
*/
array: "array",
/**
* Fixed-length list with position-specific items.
*/
tuple: "tuple",
/**
* "One of" multiple schema members.
*/
union: "union",
/**
* "All of" multiple schema members.
*/
intersection: "intersection",
/**
* Enum schema.
*/
enum: "enum",
/**
* Reference to another schema.
*/
ref: "ref",
/**
* Calendar date (for example `2026-03-24`).
*/
date: "date",
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
datetime: "datetime",
/**
* Time-only value (for example `09:00:00`).
*/
time: "time",
/**
* UUID value.
*/
uuid: "uuid",
/**
* Email address value.
*/
email: "email",
/**
* URL value.
*/
url: "url",
/**
* IPv4 address value.
*/
ipv4: "ipv4",
/**
* IPv6 address value.
*/
ipv6: "ipv6",
/**
* Binary/blob value.
*/
blob: "blob",
/**
* Impossible value (`never`).
*/
never: "never"
};
/**
* Scalar primitive schema types used for union simplification and type narrowing.
*/
const SCALAR_PRIMITIVE_TYPES = new Set([
"string",
"number",
"integer",
"bigint",
"boolean"
]);
/**
* Returns `true` when `type` is a scalar primitive that can be assigned without wrapping
* (for example `string | number | boolean`).
*/
function isScalarPrimitive(type) {
return SCALAR_PRIMITIVE_TYPES.has(type);
}
/**
* HTTP method identifiers used by operation nodes.
*/
const httpMethods = {
get: "GET",
post: "POST",
put: "PUT",
patch: "PATCH",
delete: "DELETE",
head: "HEAD",
options: "OPTIONS",
trace: "TRACE"
};
/**
* One indentation level, derived from {@link INDENT_SIZE}.
*/
const INDENT = Array.from({ length: 2 }, () => " ").join("");
//#endregion
//#region src/guards.ts
/**
* Narrows a `SchemaNode` to the variant that matches `type`.
*
* @example
* ```ts
* const schema = createSchema({ type: 'string' })
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
* ```
*/
function narrowSchema(node, type) {
return node?.type === type ? node : null;
}
/**
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
*
* @example
* ```ts
* if (isHttpOperationNode(node)) {
* console.log(node.method, node.path)
* }
* ```
*/
function isHttpOperationNode(node) {
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
}
//#endregion
//#region src/utils/refs.ts
const plainStringTypes = new Set([
"string",
"uuid",
"email",
"url",
"datetime"
]);
/**
* Returns the last path segment of a reference string.
*
* @example
* ```ts
* extractRefName('#/components/schemas/Pet') // 'Pet'
* ```
*/
function extractRefName(ref) {
return ref.split("/").at(-1) ?? ref;
}
/**
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
*
* Returns `null` for non-ref nodes or when no name resolves.
*
* @example
* ```ts
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
* // => 'Pet'
* ```
*/
function resolveRefName(node) {
if (!node || node.type !== "ref") return null;
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
return node.name ?? node.schema?.name ?? null;
}
/**
* Builds a PascalCase child schema name by joining a parent name and property name.
* Returns `null` when there is no parent to nest under.
*
* @example
* ```ts
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
* childName(undefined, 'params') // null
* ```
*/
function childName(parentName, propName) {
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
}
/**
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
* empty parts.
*
* @example
* ```ts
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
* ```
*/
function enumPropName(parentName, propName, enumSuffix) {
return pascalCase([
parentName,
propName,
enumSuffix
].filter(Boolean).join(" "));
}
/**
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
*
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override
* the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
*
* @example
* ```ts
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
* ```
*/
function syncSchemaRef(node) {
const ref = narrowSchema(node, "ref");
if (!ref) return node;
if (!ref.schema) return node;
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
return createSchema({
...ref.schema,
...definedOverrides
});
}
/**
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
function isStringType(node) {
if (plainStringTypes.has(node.type)) return true;
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
if (temporal) return temporal.representation !== "date";
return false;
}
/**
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
*
* Returns `null` when there is no resolver, no params, or the group name equals the
* individual param name (so there is no real group to emit).
*/
function resolveGroupType({ node, params, group, resolver }) {
if (!resolver || !params.length) return null;
const firstParam = params[0];
const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam);
if (groupName === resolver.resolveParamName(node, firstParam)) return null;
return {
type: groupName,
optional: params.every((p) => !p.required)
};
}
//#endregion
//#region src/registry.ts
/**
* Every node definition. Adding a node means adding its `defineNode` to one
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
*/
const nodeDefs = [
inputDef,
outputDef,
operationDef,
requestBodyDef,
contentDef,
responseDef,
schemaDef,
propertyDef,
parameterDef,
functionParameterDef,
functionParametersDef,
typeLiteralDef,
indexedAccessTypeDef,
objectBindingPatternDef,
constDef,
typeDef,
functionDef,
arrowFunctionDef,
textDef,
breakDef,
jsxDef,
importDef,
exportDef,
sourceDef,
fileDef
];
//#endregion
//#region src/visitor.ts
/**
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
* Derived from each definition's `children`.
*/
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
/**
* Maps a node kind to the matching visitor callback name. Derived from each
* definition's `visitorKey`.
*/
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
/**
* Per-kind builders rerun after children are rebuilt. Derived from each
* definition's `rebuild` flag.
*/
const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : []));
/**
* Creates a small async concurrency limiter.
*
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
*
* @example
* ```ts
* const limit = createLimit(2)
* for (const task of [taskA, taskB, taskC]) {
* await limit(() => task())
* }
* // only 2 tasks run at the same time
* ```
*/
function createLimit(concurrency) {
let active = 0;
const queue = [];
function next() {
if (active < concurrency && queue.length > 0) {
active++;
queue.shift()();
}
}
return function limit(fn) {
return new Promise((resolve, reject) => {
queue.push(() => {
Promise.resolve(fn()).then(resolve, reject).finally(() => {
active--;
next();
});
});
next();
});
};
}
const visitorKeysByKind = VISITOR_KEYS;
/**
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
*/
function isNode(value) {
return typeof value === "object" && value !== null && "kind" in value;
}
/**
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
*
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
*
* @example
* ```ts
* const children = getChildren(operationNode, true)
* // returns parameters, the request body, and responses
* ```
*/
function* getChildren(node, recurse) {
if (node.kind === "Schema" && !recurse) return;
const keys = visitorKeysByKind[node.kind];
if (!keys) return;
const record = node;
for (const key of keys) {
const value = record[key];
if (Array.isArray(value)) {
for (const item of value) if (isNode(item)) yield item;
} else if (isNode(value)) yield value;
}
}
/**
* Runs the visitor callback that matches `node.kind` with the traversal
* context. The result is a replacement node, a collected value, or `undefined`
* when no callback is registered for the kind.
*
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
* in one place. `TResult` is the caller's expected return: the same node type
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
*/
function applyVisitor(node, visitor, parent) {
const key = VISITOR_KEY_BY_KIND[node.kind];
if (!key) return void 0;
const fn = visitor[key];
return fn?.(node, { parent });
}
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
async function walk(node, options) {
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
}
async function _walk(node, visitor, recurse, limit, parent) {
await limit(() => applyVisitor(node, visitor, parent));
const children = Array.from(getChildren(node, recurse));
if (children.length === 0) return;
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
}
function transform(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
if (rebuilt === node) return node;
const rebuild = nodeRebuilders[rebuilt.kind];
return rebuild ? rebuild(rebuilt) : rebuilt;
}
/**
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
* `Schema` children are skipped in shallow mode.
*/
function transformChildren(node, options, recurse) {
if (node.kind === "Schema" && !recurse) return node;
const keys = visitorKeysByKind[node.kind];
if (!keys) return node;
const record = node;
const childOptions = {
...options,
parent: node
};
let updates;
for (const key of keys) {
if (!(key in record)) continue;
const value = record[key];
if (Array.isArray(value)) {
let changed = false;
const mapped = value.map((item) => {
if (!isNode(item)) return item;
const next = transform(item, childOptions);
if (next !== item) changed = true;
return next;
});
if (changed) (updates ??= {})[key] = mapped;
} else if (isNode(value)) {
const next = transform(value, childOptions);
if (next !== value) (updates ??= {})[key] = next;
}
}
return updates ? {
...node,
...updates
} : node;
}
/**
* Lazy depth-first collection pass. Yields every non-null value returned by
* the visitor callbacks. Use `collect` for the eager array form.
*
* @example Collect every operationId
* ```ts
* const ids: string[] = []
* for (const id of collectLazy<string>(root, {
* operation(node) {
* return node.operationId
* },
* })) {
* ids.push(id)
* }
* ```
*/
function* collectLazy(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const v = applyVisitor(node, visitor, parent);
if (v != null) yield v;
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
...options,
parent: node
});
}
/**
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
function collect(node, options) {
return Array.from(collectLazy(node, options));
}
//#endregion
export { isScalarPrimitive as _, nodeDefs as a, extractRefName as c, resolveRefName as d, syncSchemaRef as f, httpMethods as g, INDENT as h, walk as i, isStringType as l, narrowSchema as m, collectLazy as n, childName as o, isHttpOperationNode as p, transform as r, enumPropName as s, collect as t, resolveGroupType as u, schemaTypes as v };
//# sourceMappingURL=visitor-DJ6ZEJvq.js.map
{"version":3,"file":"visitor-DJ6ZEJvq.js","names":[],"sources":["../src/constants.ts","../src/guards.ts","../src/utils/refs.ts","../src/registry.ts","../src/visitor.ts"],"sourcesContent":["import type { HttpMethod } from './nodes/operation.ts'\nimport type { SchemaType } from './nodes/schema.ts'\n\n/**\n * Traversal depth for AST visitor utilities.\n *\n * - `'shallow'` visits only the immediate node, skipping children.\n * - `'deep'` recursively visits all descendant nodes.\n */\nexport type VisitorDepth = 'shallow' | 'deep'\n\nexport const visitorDepths = {\n shallow: 'shallow',\n deep: 'deep',\n} as const satisfies Record<VisitorDepth, VisitorDepth>\n\n/**\n * Schema type discriminators used by all AST schema nodes.\n *\n * Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).\n * Call `isScalarPrimitive()` to check for the scalar types.\n */\nexport const schemaTypes = {\n /**\n * Text value.\n */\n string: 'string',\n /**\n * Floating-point number (`float`, `double`).\n */\n number: 'number',\n /**\n * Whole number (`int32`). Use `bigint` for `int64`.\n */\n integer: 'integer',\n /**\n * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.\n */\n bigint: 'bigint',\n /**\n * Boolean value.\n */\n boolean: 'boolean',\n /**\n * Explicit null value.\n */\n null: 'null',\n /**\n * Any value (no type restriction).\n */\n any: 'any',\n /**\n * Unknown value (must be narrowed before usage).\n */\n unknown: 'unknown',\n /**\n * No return value (`void`).\n */\n void: 'void',\n /**\n * Object with named properties.\n */\n object: 'object',\n /**\n * Sequential list of items.\n */\n array: 'array',\n /**\n * Fixed-length list with position-specific items.\n */\n tuple: 'tuple',\n /**\n * \"One of\" multiple schema members.\n */\n union: 'union',\n /**\n * \"All of\" multiple schema members.\n */\n intersection: 'intersection',\n /**\n * Enum schema.\n */\n enum: 'enum',\n /**\n * Reference to another schema.\n */\n ref: 'ref',\n /**\n * Calendar date (for example `2026-03-24`).\n */\n date: 'date',\n /**\n * Date-time value (for example `2026-03-24T09:00:00Z`).\n */\n datetime: 'datetime',\n /**\n * Time-only value (for example `09:00:00`).\n */\n time: 'time',\n /**\n * UUID value.\n */\n uuid: 'uuid',\n /**\n * Email address value.\n */\n email: 'email',\n /**\n * URL value.\n */\n url: 'url',\n /**\n * IPv4 address value.\n */\n ipv4: 'ipv4',\n /**\n * IPv6 address value.\n */\n ipv6: 'ipv6',\n /**\n * Binary/blob value.\n */\n blob: 'blob',\n /**\n * Impossible value (`never`).\n */\n never: 'never',\n} as const satisfies Record<SchemaType, SchemaType>\n\nexport type ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'\n\n/**\n * Scalar primitive schema types used for union simplification and type narrowing.\n */\nconst SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])\n\n/**\n * Returns `true` when `type` is a scalar primitive that can be assigned without wrapping\n * (for example `string | number | boolean`).\n */\nexport function isScalarPrimitive(type: string): type is ScalarPrimitive {\n return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)\n}\n\n/**\n * HTTP method identifiers used by operation nodes.\n */\nexport const httpMethods = {\n get: 'GET',\n post: 'POST',\n put: 'PUT',\n patch: 'PATCH',\n delete: 'DELETE',\n head: 'HEAD',\n options: 'OPTIONS',\n trace: 'TRACE',\n} as const satisfies Record<Lowercase<HttpMethod>, HttpMethod>\n\n/**\n * Default concurrency limit for the `walk()` traversal utility.\n *\n * Set to 30 to balance I/O-bound resolver parallelism against event-loop and memory pressure\n * during large spec traversals. Override it for different hardware constraints.\n *\n * @example\n * ```ts\n * import { walk, WALK_CONCURRENCY } from '@kubb/ast'\n *\n * walk(root, { concurrency: WALK_CONCURRENCY, root: () => {} })\n * ```\n */\nexport const WALK_CONCURRENCY = 30\n\n/**\n * Number of spaces in one indentation level when assembling multi-line code as strings.\n * Set to 2, 3, … to change the indent width used by `buildObject`/`buildList`.\n */\nconst INDENT_SIZE = 2\n\n/**\n * One indentation level, derived from {@link INDENT_SIZE}.\n */\nexport const INDENT = Array.from({ length: INDENT_SIZE }, () => ' ').join('')\n","import type { HttpOperationNode, OperationNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'\n\n/**\n * Narrows a `SchemaNode` to the variant that matches `type`.\n *\n * @example\n * ```ts\n * const schema = createSchema({ type: 'string' })\n * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null\n * ```\n */\nexport function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null {\n return node?.type === type ? (node as SchemaNodeByType[T]) : null\n}\n\n/**\n * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.\n *\n * @example\n * ```ts\n * if (isHttpOperationNode(node)) {\n * console.log(node.method, node.path)\n * }\n * ```\n */\nexport function isHttpOperationNode(node: OperationNode): node is HttpOperationNode {\n return node.protocol === 'http' || (node.method !== undefined && node.path !== undefined)\n}\n","import { pascalCase } from '@internals/utils'\nimport { narrowSchema } from '../guards.ts'\nimport type { OperationNode, ParameterNode, SchemaNode } from '../nodes/index.ts'\nimport { createSchema, type SchemaType } from '../nodes/schema.ts'\nimport type { OperationParamsResolver, ParamGroupType } from './operationParams.ts'\n\nconst plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)\n\n/**\n * Returns the last path segment of a reference string.\n *\n * @example\n * ```ts\n * extractRefName('#/components/schemas/Pet') // 'Pet'\n * ```\n */\nexport function extractRefName(ref: string): string {\n return ref.split('/').at(-1) ?? ref\n}\n\n/**\n * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.\n *\n * Returns `null` for non-ref nodes or when no name resolves.\n *\n * @example\n * ```ts\n * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })\n * // => 'Pet'\n * ```\n */\nexport function resolveRefName(node: SchemaNode | undefined): string | null {\n if (!node || node.type !== 'ref') return null\n if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null\n\n return node.name ?? node.schema?.name ?? null\n}\n\n/**\n * Builds a PascalCase child schema name by joining a parent name and property name.\n * Returns `null` when there is no parent to nest under.\n *\n * @example\n * ```ts\n * childName('Order', 'shipping_address') // 'OrderShippingAddress'\n * childName(undefined, 'params') // null\n * ```\n */\nexport function childName(parentName: string | null | undefined, propName: string): string | null {\n return parentName ? pascalCase([parentName, propName].join(' ')) : null\n}\n\n/**\n * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any\n * empty parts.\n *\n * @example\n * ```ts\n * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'\n * ```\n */\nexport function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {\n return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))\n}\n\n/**\n * Merges a ref node with its resolved schema, giving usage-site fields precedence.\n *\n * Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override\n * the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })\n * const merged = syncSchemaRef(ref) // merges with resolved Pet schema\n * ```\n */\nexport function syncSchemaRef(node: SchemaNode): SchemaNode {\n const ref = narrowSchema(node, 'ref')\n\n if (!ref) return node\n if (!ref.schema) return node\n\n const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref\n\n // Filter out undefined override values so they don't shadow the resolved schema's fields.\n const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))\n\n return createSchema({ ...ref.schema, ...definedOverrides })\n}\n\n/**\n * Type guard that returns `true` when a schema emits as a plain `string` type.\n *\n * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`\n * types, returns `true` only when `representation` is `'string'` rather than `'date'`.\n */\nexport function isStringType(node: SchemaNode): boolean {\n if (plainStringTypes.has(node.type)) {\n return true\n }\n\n const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')\n if (temporal) {\n return temporal.representation !== 'date'\n }\n\n return false\n}\n\n/**\n * Derives a {@link ParamGroupType} for a query or header group from the resolver.\n *\n * Returns `null` when there is no resolver, no params, or the group name equals the\n * individual param name (so there is no real group to emit).\n */\nexport function resolveGroupType({\n node,\n params,\n group,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n group: 'query' | 'header'\n resolver: OperationParamsResolver | undefined\n}): ParamGroupType | null {\n if (!resolver || !params.length) {\n return null\n }\n const firstParam = params[0]!\n const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName\n const groupName = groupMethod.call(resolver, node, firstParam)\n if (groupName === resolver.resolveParamName(node, firstParam)) {\n return null\n }\n return { type: groupName, optional: params.every((p) => !p.required) }\n}\n","import type { NodeDef } from './node.ts'\nimport { arrowFunctionDef, breakDef, constDef, functionDef, jsxDef, textDef, typeDef } from './nodes/code.ts'\nimport { contentDef } from './nodes/content.ts'\nimport { exportDef, fileDef, importDef, sourceDef } from './nodes/file.ts'\nimport { functionParameterDef, functionParametersDef, indexedAccessTypeDef, objectBindingPatternDef, typeLiteralDef } from './nodes/function.ts'\nimport { inputDef } from './nodes/input.ts'\nimport { operationDef } from './nodes/operation.ts'\nimport { outputDef } from './nodes/output.ts'\nimport { parameterDef } from './nodes/parameter.ts'\nimport { propertyDef } from './nodes/property.ts'\nimport { requestBodyDef } from './nodes/requestBody.ts'\nimport { responseDef } from './nodes/response.ts'\nimport { schemaDef } from './nodes/schema.ts'\n\n// Surface every def from one place so the package barrel re-exports them with `export * from './registry.ts'`.\n// Adding a node means adding its `defineNode` to a `nodes/*.ts` file and listing it in `nodeDefs` below, nothing else.\nexport {\n arrowFunctionDef,\n breakDef,\n constDef,\n contentDef,\n exportDef,\n fileDef,\n functionDef,\n functionParameterDef,\n functionParametersDef,\n importDef,\n indexedAccessTypeDef,\n inputDef,\n jsxDef,\n objectBindingPatternDef,\n operationDef,\n outputDef,\n parameterDef,\n propertyDef,\n requestBodyDef,\n responseDef,\n schemaDef,\n sourceDef,\n textDef,\n typeDef,\n typeLiteralDef,\n}\n\n/**\n * Every node definition. Adding a node means adding its `defineNode` to one\n * `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.\n */\nexport const nodeDefs = [\n inputDef,\n outputDef,\n operationDef,\n requestBodyDef,\n contentDef,\n responseDef,\n schemaDef,\n propertyDef,\n parameterDef,\n functionParameterDef,\n functionParametersDef,\n typeLiteralDef,\n indexedAccessTypeDef,\n objectBindingPatternDef,\n constDef,\n typeDef,\n functionDef,\n arrowFunctionDef,\n textDef,\n breakDef,\n jsxDef,\n importDef,\n exportDef,\n sourceDef,\n fileDef,\n] satisfies ReadonlyArray<NodeDef>\n","import type { VisitorDepth } from './constants.ts'\nimport { visitorDepths, WALK_CONCURRENCY } from './constants.ts'\nimport type { NodeDef } from './node.ts'\nimport type {\n ContentNode,\n InputNode,\n Node,\n NodeKind,\n OperationNode,\n OutputNode,\n ParameterNode,\n PropertyNode,\n RequestBodyNode,\n ResponseNode,\n SchemaNode,\n} from './nodes/index.ts'\nimport { nodeDefs } from './registry.ts'\n\n/**\n * Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).\n * Derived from each definition's `children`.\n */\nconst VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => (def.children ? [[def.kind, def.children] as const] : []))) as Partial<\n Record<NodeKind, ReadonlyArray<string>>\n>\n\n/**\n * Maps a node kind to the matching visitor callback name. Derived from each\n * definition's `visitorKey`.\n */\nconst VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => (def.visitorKey ? [[def.kind, def.visitorKey] as const] : []))) as Partial<\n Record<NodeKind, NonNullable<NodeDef['visitorKey']>>\n>\n\n/**\n * Per-kind builders rerun after children are rebuilt. Derived from each\n * definition's `rebuild` flag.\n */\nconst nodeRebuilders = Object.fromEntries(\n nodeDefs.flatMap((def) => (def.rebuild ? [[def.kind, def.create as unknown as (node: Node) => Node] as const] : [])),\n) as Partial<Record<NodeKind, (node: Node) => Node>>\n\n/**\n * Creates a small async concurrency limiter.\n *\n * At most `concurrency` tasks are in flight at once. Extra tasks are queued.\n *\n * @example\n * ```ts\n * const limit = createLimit(2)\n * for (const task of [taskA, taskB, taskC]) {\n * await limit(() => task())\n * }\n * // only 2 tasks run at the same time\n * ```\n */\nfunction createLimit(concurrency: number) {\n let active = 0\n const queue: Array<() => void> = []\n\n function next() {\n if (active < concurrency && queue.length > 0) {\n active++\n queue.shift()!()\n }\n }\n\n return function limit<T>(fn: () => Promise<T> | T): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n queue.push(() => {\n Promise.resolve(fn())\n .then(resolve, reject)\n .finally(() => {\n active--\n next()\n })\n })\n next()\n })\n }\n}\n\ntype LimitFn = ReturnType<typeof createLimit>\n\n/**\n * Ordered mapping of `[NodeType, ParentType]` pairs.\n *\n * `ParentOf` uses this map to find parent types.\n */\ntype ParentNodeMap = [\n [InputNode, undefined],\n [OutputNode, undefined],\n [OperationNode, InputNode],\n [RequestBodyNode, OperationNode],\n [ContentNode, RequestBodyNode | ResponseNode],\n [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode],\n [PropertyNode, SchemaNode],\n [ParameterNode, OperationNode],\n [ResponseNode, OperationNode],\n]\n\n/**\n * Resolves the parent node type for a given AST node type.\n *\n * Visitor context relies on this so `ctx.parent` is typed for each callback.\n *\n * @example\n * ```ts\n * type InputParent = ParentOf<InputNode>\n * // undefined\n * ```\n *\n * @example\n * ```ts\n * type PropertyParent = ParentOf<PropertyNode>\n * // SchemaNode\n * ```\n *\n * @example\n * ```ts\n * type SchemaParent = ParentOf<SchemaNode>\n * // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode\n * ```\n */\nexport type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [\n infer TEntry extends [Node, unknown],\n ...infer TRest extends ReadonlyArray<[Node, unknown]>,\n]\n ? T extends TEntry[0]\n ? TEntry[1]\n : ParentOf<T, TRest>\n : Node\n\n/**\n * Traversal context passed as the second argument to every visitor callback.\n * `parent` is typed from the current node type.\n *\n * @example\n * ```ts\n * const visitor: Visitor = {\n * schema(node, { parent }) {\n * // parent type is narrowed by node kind\n * },\n * }\n * ```\n */\nexport type VisitorContext<T extends Node = Node> = {\n /**\n * Parent node of the currently visited node.\n * For `InputNode`, this is `undefined`.\n */\n parent?: ParentOf<T>\n}\n\n/**\n * Synchronous visitor consumed by `transform`. Each optional callback runs\n * for the matching node type. Return a new node to replace it, or `undefined`\n * to leave it untouched.\n *\n * Plugins typically expose `transformer` so users can supply a `Visitor` that\n * rewrites the AST before printing.\n *\n * @example Prefix every operationId\n * ```ts\n * const visitor: Visitor = {\n * operation(node) {\n * return { ...node, operationId: `api_${node.operationId}` }\n * },\n * }\n * ```\n *\n * @example Strip schema descriptions\n * ```ts\n * const visitor: Visitor = {\n * schema(node) {\n * return { ...node, description: undefined }\n * },\n * }\n * ```\n */\nexport type Visitor = {\n input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode\n output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode\n}\n\n/**\n * A visitor callback result that may be sync or async.\n */\ntype MaybePromise<T> = T | Promise<T>\n\n/**\n * Async visitor for `walk`. Synchronous `Visitor` objects are compatible.\n *\n * @example\n * ```ts\n * const visitor: AsyncVisitor = {\n * async operation(node) {\n * await Promise.resolve(node.operationId)\n * },\n * }\n * ```\n */\ntype AsyncVisitor = {\n input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>\n output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>\n}\n\n/**\n * Visitor used by `collect`.\n *\n * @example\n * ```ts\n * const visitor: CollectVisitor<string> = {\n * operation(node) {\n * return node.operationId\n * },\n * }\n * ```\n */\ntype CollectVisitor<T> = {\n input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined\n output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined\n}\n\n/**\n * Options for `transform`.\n *\n * @example\n * ```ts\n * const options: TransformOptions = { depth: 'deep', schema: (node) => node }\n * ```\n *\n * @example\n * ```ts\n * // Only transform the current node, not nested children\n * const options: TransformOptions = { depth: 'shallow', schema: (node) => node }\n * ```\n */\nexport type TransformOptions = Visitor & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Internal parent override used during recursion.\n */\n parent?: Node\n}\n\n/**\n * Options for `walk`.\n *\n * @example\n * ```ts\n * const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }\n * ```\n */\nexport type WalkOptions = AsyncVisitor & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Maximum number of sibling nodes visited concurrently.\n * @default 30\n */\n concurrency?: number\n}\n\n/**\n * Options for `collect`.\n *\n * @example\n * ```ts\n * const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }\n * ```\n */\nexport type CollectOptions<T> = CollectVisitor<T> & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Internal parent override used during recursion.\n */\n parent?: Node\n}\n\nconst visitorKeysByKind = VISITOR_KEYS as Record<string, ReadonlyArray<string> | undefined>\n\n/**\n * Returns `true` when `value` is an AST node (an object carrying a `kind`).\n */\nfunction isNode(value: unknown): value is Node {\n return typeof value === 'object' && value !== null && 'kind' in value\n}\n\n/**\n * Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.\n *\n * `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.\n *\n * @example\n * ```ts\n * const children = getChildren(operationNode, true)\n * // returns parameters, the request body, and responses\n * ```\n */\nfunction* getChildren(node: Node, recurse: boolean): Generator<Node, void, undefined> {\n if (node.kind === 'Schema' && !recurse) return\n\n const keys = visitorKeysByKind[node.kind]\n if (!keys) return\n\n const record = node as unknown as Record<string, unknown>\n for (const key of keys) {\n const value = record[key]\n if (Array.isArray(value)) {\n for (const item of value) if (isNode(item)) yield item\n } else if (isNode(value)) {\n yield value\n }\n }\n}\n\n/**\n * Runs the visitor callback that matches `node.kind` with the traversal\n * context. The result is a replacement node, a collected value, or `undefined`\n * when no callback is registered for the kind.\n *\n * Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives\n * in one place. `TResult` is the caller's expected return: the same node type\n * for `transform`, the collected value type for `collectLazy`, ignored for `walk`.\n */\nfunction applyVisitor<TResult>(node: Node, visitor: Visitor | AsyncVisitor | CollectVisitor<unknown>, parent: Node | undefined): TResult | null | undefined {\n const key = VISITOR_KEY_BY_KIND[node.kind]\n if (!key) return undefined\n\n const fn = visitor[key] as ((node: Node, context: VisitorContext) => TResult | null | undefined) | undefined\n\n return fn?.(node, { parent })\n}\n\n/**\n * Async depth-first traversal for side effects. Visitor return values are\n * ignored. Use `transform` when you want to rewrite nodes.\n *\n * Sibling nodes at each depth run concurrently up to `options.concurrency`\n * (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor\n * work. Lower values reduce memory pressure.\n *\n * @example Log every operation\n * ```ts\n * await walk(root, {\n * operation(node) {\n * console.log(node.operationId)\n * },\n * })\n * ```\n *\n * @example Only visit the root node\n * ```ts\n * await walk(root, { depth: 'shallow', input: () => {} })\n * ```\n */\nexport async function walk(node: Node, options: WalkOptions): Promise<void> {\n const recurse = (options.depth ?? visitorDepths.deep) === visitorDepths.deep\n const limit = createLimit(options.concurrency ?? WALK_CONCURRENCY)\n\n return _walk(node, options, recurse, limit, undefined)\n}\n\nasync function _walk(node: Node, visitor: AsyncVisitor, recurse: boolean, limit: LimitFn, parent: Node | undefined): Promise<void> {\n await limit(() => applyVisitor(node, visitor, parent))\n\n // Visit siblings concurrently and let the shared `limit` cap how many callbacks\n // run at once. Awaiting each child sequentially here would serialize the whole\n // traversal and make `concurrency` inert. Every visitor callback would run one\n // at a time regardless of the limit.\n const children = Array.from(getChildren(node, recurse))\n if (children.length === 0) return\n\n await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)))\n}\n\n/**\n * Synchronous depth-first transform. Each visitor callback can return a\n * replacement node. Returning `undefined` keeps the original.\n *\n * The original tree is never mutated, a new tree is returned. Pass\n * `depth: 'shallow'` to skip recursion into children.\n *\n * @example Prefix every operationId\n * ```ts\n * const next = transform(root, {\n * operation(node) {\n * return { ...node, operationId: `prefixed_${node.operationId}` }\n * },\n * })\n * ```\n *\n * @example Replace only the root node\n * ```ts\n * const next = transform(root, {\n * depth: 'shallow',\n * input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),\n * })\n * ```\n */\nexport function transform(node: InputNode, options: TransformOptions): InputNode\nexport function transform(node: OutputNode, options: TransformOptions): OutputNode\nexport function transform(node: OperationNode, options: TransformOptions): OperationNode\nexport function transform(node: SchemaNode, options: TransformOptions): SchemaNode\nexport function transform(node: PropertyNode, options: TransformOptions): PropertyNode\nexport function transform(node: ParameterNode, options: TransformOptions): ParameterNode\nexport function transform(node: ResponseNode, options: TransformOptions): ResponseNode\nexport function transform(node: Node, options: TransformOptions): Node\nexport function transform(node: Node, options: TransformOptions): Node {\n const { depth, parent, ...visitor } = options\n const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep\n\n const visited = applyVisitor<Node>(node, visitor, parent) ?? node\n const rebuilt = transformChildren(visited, options, recurse)\n\n // Structural sharing: when the visitor and child rebuild both left this node\n // untouched, return the original reference so callers can detect \"nothing\n // changed\" by identity and ancestors can avoid reallocating.\n if (rebuilt === node) return node\n\n const rebuild = nodeRebuilders[rebuilt.kind]\n return rebuild ? rebuild(rebuilt) : rebuilt\n}\n\n/**\n * Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming\n * each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.\n * `Schema` children are skipped in shallow mode.\n */\nfunction transformChildren(node: Node, options: TransformOptions, recurse: boolean): Node {\n if (node.kind === 'Schema' && !recurse) return node\n\n const keys = visitorKeysByKind[node.kind]\n if (!keys) return node\n\n const record = node as unknown as Record<string, unknown>\n const childOptions = { ...options, parent: node }\n let updates: Record<string, unknown> | undefined\n\n for (const key of keys) {\n if (!(key in record)) continue\n const value = record[key]\n if (Array.isArray(value)) {\n let changed = false\n const mapped = value.map((item) => {\n if (!isNode(item)) return item\n const next = transform(item, childOptions)\n if (next !== item) changed = true\n return next\n })\n if (changed) (updates ??= {})[key] = mapped\n } else if (isNode(value)) {\n const next = transform(value, childOptions)\n if (next !== value) (updates ??= {})[key] = next\n }\n }\n\n return updates ? ({ ...node, ...updates } as Node) : node\n}\n/**\n * Lazy depth-first collection pass. Yields every non-null value returned by\n * the visitor callbacks. Use `collect` for the eager array form.\n *\n * @example Collect every operationId\n * ```ts\n * const ids: string[] = []\n * for (const id of collectLazy<string>(root, {\n * operation(node) {\n * return node.operationId\n * },\n * })) {\n * ids.push(id)\n * }\n * ```\n */\nexport function* collectLazy<T>(node: Node, options: CollectOptions<T>): Generator<T, void, undefined> {\n const { depth, parent, ...visitor } = options\n const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep\n\n const v = applyVisitor<T>(node, visitor, parent)\n if (v != null) yield v\n\n for (const child of getChildren(node, recurse)) {\n yield* collectLazy(child, { ...options, parent: node })\n }\n}\n\n/**\n * Eager depth-first collection pass. Gathers every non-null value the visitor\n * callbacks return into an array.\n *\n * @example Collect every operationId\n * ```ts\n * const ids = collect<string>(root, {\n * operation(node) {\n * return node.operationId\n * },\n * })\n * ```\n */\nexport function collect<T>(node: Node, options: CollectOptions<T>): Array<T> {\n return Array.from(collectLazy(node, options))\n}\n"],"mappings":";;;AAWA,MAAa,gBAAgB;CAC3B,SAAS;CACT,MAAM;AACR;;;;;;;AAQA,MAAa,cAAc;;;;CAIzB,QAAQ;;;;CAIR,QAAQ;;;;CAIR,SAAS;;;;CAIT,QAAQ;;;;CAIR,S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A,SAAgB,QAAW,MAAY,SAAsC;CAC3E,OAAO,MAAM,KAAK,YAAY,MAAM,OAAO,CAAC;AAC9C"}
const require_response = require("./response-hnSw2NKE.cjs");
//#region src/constants.ts
const visitorDepths = {
shallow: "shallow",
deep: "deep"
};
/**
* Schema type discriminators used by all AST schema nodes.
*
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/
const schemaTypes = {
/**
* Text value.
*/
string: "string",
/**
* Floating-point number (`float`, `double`).
*/
number: "number",
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
integer: "integer",
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
bigint: "bigint",
/**
* Boolean value.
*/
boolean: "boolean",
/**
* Explicit null value.
*/
null: "null",
/**
* Any value (no type restriction).
*/
any: "any",
/**
* Unknown value (must be narrowed before usage).
*/
unknown: "unknown",
/**
* No return value (`void`).
*/
void: "void",
/**
* Object with named properties.
*/
object: "object",
/**
* Sequential list of items.
*/
array: "array",
/**
* Fixed-length list with position-specific items.
*/
tuple: "tuple",
/**
* "One of" multiple schema members.
*/
union: "union",
/**
* "All of" multiple schema members.
*/
intersection: "intersection",
/**
* Enum schema.
*/
enum: "enum",
/**
* Reference to another schema.
*/
ref: "ref",
/**
* Calendar date (for example `2026-03-24`).
*/
date: "date",
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
datetime: "datetime",
/**
* Time-only value (for example `09:00:00`).
*/
time: "time",
/**
* UUID value.
*/
uuid: "uuid",
/**
* Email address value.
*/
email: "email",
/**
* URL value.
*/
url: "url",
/**
* IPv4 address value.
*/
ipv4: "ipv4",
/**
* IPv6 address value.
*/
ipv6: "ipv6",
/**
* Binary/blob value.
*/
blob: "blob",
/**
* Impossible value (`never`).
*/
never: "never"
};
/**
* Scalar primitive schema types used for union simplification and type narrowing.
*/
const SCALAR_PRIMITIVE_TYPES = new Set([
"string",
"number",
"integer",
"bigint",
"boolean"
]);
/**
* Returns `true` when `type` is a scalar primitive that can be assigned without wrapping
* (for example `string | number | boolean`).
*/
function isScalarPrimitive(type) {
return SCALAR_PRIMITIVE_TYPES.has(type);
}
/**
* HTTP method identifiers used by operation nodes.
*/
const httpMethods = {
get: "GET",
post: "POST",
put: "PUT",
patch: "PATCH",
delete: "DELETE",
head: "HEAD",
options: "OPTIONS",
trace: "TRACE"
};
/**
* One indentation level, derived from {@link INDENT_SIZE}.
*/
const INDENT = Array.from({ length: 2 }, () => " ").join("");
//#endregion
//#region src/guards.ts
/**
* Narrows a `SchemaNode` to the variant that matches `type`.
*
* @example
* ```ts
* const schema = createSchema({ type: 'string' })
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
* ```
*/
function narrowSchema(node, type) {
return node?.type === type ? node : null;
}
/**
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
*
* @example
* ```ts
* if (isHttpOperationNode(node)) {
* console.log(node.method, node.path)
* }
* ```
*/
function isHttpOperationNode(node) {
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
}
//#endregion
//#region src/utils/refs.ts
const plainStringTypes = new Set([
"string",
"uuid",
"email",
"url",
"datetime"
]);
/**
* Returns the last path segment of a reference string.
*
* @example
* ```ts
* extractRefName('#/components/schemas/Pet') // 'Pet'
* ```
*/
function extractRefName(ref) {
return ref.split("/").at(-1) ?? ref;
}
/**
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
*
* Returns `null` for non-ref nodes or when no name resolves.
*
* @example
* ```ts
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
* // => 'Pet'
* ```
*/
function resolveRefName(node) {
if (!node || node.type !== "ref") return null;
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
return node.name ?? node.schema?.name ?? null;
}
/**
* Builds a PascalCase child schema name by joining a parent name and property name.
* Returns `null` when there is no parent to nest under.
*
* @example
* ```ts
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
* childName(undefined, 'params') // null
* ```
*/
function childName(parentName, propName) {
return parentName ? require_response.pascalCase([parentName, propName].join(" ")) : null;
}
/**
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
* empty parts.
*
* @example
* ```ts
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
* ```
*/
function enumPropName(parentName, propName, enumSuffix) {
return require_response.pascalCase([
parentName,
propName,
enumSuffix
].filter(Boolean).join(" "));
}
/**
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
*
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override
* the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
*
* @example
* ```ts
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
* ```
*/
function syncSchemaRef(node) {
const ref = narrowSchema(node, "ref");
if (!ref) return node;
if (!ref.schema) return node;
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
return require_response.createSchema({
...ref.schema,
...definedOverrides
});
}
/**
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
function isStringType(node) {
if (plainStringTypes.has(node.type)) return true;
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
if (temporal) return temporal.representation !== "date";
return false;
}
/**
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
*
* Returns `null` when there is no resolver, no params, or the group name equals the
* individual param name (so there is no real group to emit).
*/
function resolveGroupType({ node, params, group, resolver }) {
if (!resolver || !params.length) return null;
const firstParam = params[0];
const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam);
if (groupName === resolver.resolveParamName(node, firstParam)) return null;
return {
type: groupName,
optional: params.every((p) => !p.required)
};
}
//#endregion
//#region src/registry.ts
/**
* Every node definition. Adding a node means adding its `defineNode` to one
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
*/
const nodeDefs = [
require_response.inputDef,
require_response.outputDef,
require_response.operationDef,
require_response.requestBodyDef,
require_response.contentDef,
require_response.responseDef,
require_response.schemaDef,
require_response.propertyDef,
require_response.parameterDef,
require_response.functionParameterDef,
require_response.functionParametersDef,
require_response.typeLiteralDef,
require_response.indexedAccessTypeDef,
require_response.objectBindingPatternDef,
require_response.constDef,
require_response.typeDef,
require_response.functionDef,
require_response.arrowFunctionDef,
require_response.textDef,
require_response.breakDef,
require_response.jsxDef,
require_response.importDef,
require_response.exportDef,
require_response.sourceDef,
require_response.fileDef
];
//#endregion
//#region src/visitor.ts
/**
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
* Derived from each definition's `children`.
*/
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
/**
* Maps a node kind to the matching visitor callback name. Derived from each
* definition's `visitorKey`.
*/
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
/**
* Per-kind builders rerun after children are rebuilt. Derived from each
* definition's `rebuild` flag.
*/
const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : []));
/**
* Creates a small async concurrency limiter.
*
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
*
* @example
* ```ts
* const limit = createLimit(2)
* for (const task of [taskA, taskB, taskC]) {
* await limit(() => task())
* }
* // only 2 tasks run at the same time
* ```
*/
function createLimit(concurrency) {
let active = 0;
const queue = [];
function next() {
if (active < concurrency && queue.length > 0) {
active++;
queue.shift()();
}
}
return function limit(fn) {
return new Promise((resolve, reject) => {
queue.push(() => {
Promise.resolve(fn()).then(resolve, reject).finally(() => {
active--;
next();
});
});
next();
});
};
}
const visitorKeysByKind = VISITOR_KEYS;
/**
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
*/
function isNode(value) {
return typeof value === "object" && value !== null && "kind" in value;
}
/**
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
*
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
*
* @example
* ```ts
* const children = getChildren(operationNode, true)
* // returns parameters, the request body, and responses
* ```
*/
function* getChildren(node, recurse) {
if (node.kind === "Schema" && !recurse) return;
const keys = visitorKeysByKind[node.kind];
if (!keys) return;
const record = node;
for (const key of keys) {
const value = record[key];
if (Array.isArray(value)) {
for (const item of value) if (isNode(item)) yield item;
} else if (isNode(value)) yield value;
}
}
/**
* Runs the visitor callback that matches `node.kind` with the traversal
* context. The result is a replacement node, a collected value, or `undefined`
* when no callback is registered for the kind.
*
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
* in one place. `TResult` is the caller's expected return: the same node type
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
*/
function applyVisitor(node, visitor, parent) {
const key = VISITOR_KEY_BY_KIND[node.kind];
if (!key) return void 0;
const fn = visitor[key];
return fn?.(node, { parent });
}
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
async function walk(node, options) {
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
}
async function _walk(node, visitor, recurse, limit, parent) {
await limit(() => applyVisitor(node, visitor, parent));
const children = Array.from(getChildren(node, recurse));
if (children.length === 0) return;
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
}
function transform(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
if (rebuilt === node) return node;
const rebuild = nodeRebuilders[rebuilt.kind];
return rebuild ? rebuild(rebuilt) : rebuilt;
}
/**
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
* `Schema` children are skipped in shallow mode.
*/
function transformChildren(node, options, recurse) {
if (node.kind === "Schema" && !recurse) return node;
const keys = visitorKeysByKind[node.kind];
if (!keys) return node;
const record = node;
const childOptions = {
...options,
parent: node
};
let updates;
for (const key of keys) {
if (!(key in record)) continue;
const value = record[key];
if (Array.isArray(value)) {
let changed = false;
const mapped = value.map((item) => {
if (!isNode(item)) return item;
const next = transform(item, childOptions);
if (next !== item) changed = true;
return next;
});
if (changed) (updates ??= {})[key] = mapped;
} else if (isNode(value)) {
const next = transform(value, childOptions);
if (next !== value) (updates ??= {})[key] = next;
}
}
return updates ? {
...node,
...updates
} : node;
}
/**
* Lazy depth-first collection pass. Yields every non-null value returned by
* the visitor callbacks. Use `collect` for the eager array form.
*
* @example Collect every operationId
* ```ts
* const ids: string[] = []
* for (const id of collectLazy<string>(root, {
* operation(node) {
* return node.operationId
* },
* })) {
* ids.push(id)
* }
* ```
*/
function* collectLazy(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const v = applyVisitor(node, visitor, parent);
if (v != null) yield v;
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
...options,
parent: node
});
}
/**
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
function collect(node, options) {
return Array.from(collectLazy(node, options));
}
//#endregion
Object.defineProperty(exports, "INDENT", {
enumerable: true,
get: function() {
return INDENT;
}
});
Object.defineProperty(exports, "childName", {
enumerable: true,
get: function() {
return childName;
}
});
Object.defineProperty(exports, "collect", {
enumerable: true,
get: function() {
return collect;
}
});
Object.defineProperty(exports, "collectLazy", {
enumerable: true,
get: function() {
return collectLazy;
}
});
Object.defineProperty(exports, "enumPropName", {
enumerable: true,
get: function() {
return enumPropName;
}
});
Object.defineProperty(exports, "extractRefName", {
enumerable: true,
get: function() {
return extractRefName;
}
});
Object.defineProperty(exports, "httpMethods", {
enumerable: true,
get: function() {
return httpMethods;
}
});
Object.defineProperty(exports, "isHttpOperationNode", {
enumerable: true,
get: function() {
return isHttpOperationNode;
}
});
Object.defineProperty(exports, "isScalarPrimitive", {
enumerable: true,
get: function() {
return isScalarPrimitive;
}
});
Object.defineProperty(exports, "isStringType", {
enumerable: true,
get: function() {
return isStringType;
}
});
Object.defineProperty(exports, "narrowSchema", {
enumerable: true,
get: function() {
return narrowSchema;
}
});
Object.defineProperty(exports, "nodeDefs", {
enumerable: true,
get: function() {
return nodeDefs;
}
});
Object.defineProperty(exports, "resolveGroupType", {
enumerable: true,
get: function() {
return resolveGroupType;
}
});
Object.defineProperty(exports, "resolveRefName", {
enumerable: true,
get: function() {
return resolveRefName;
}
});
Object.defineProperty(exports, "schemaTypes", {
enumerable: true,
get: function() {
return schemaTypes;
}
});
Object.defineProperty(exports, "syncSchemaRef", {
enumerable: true,
get: function() {
return syncSchemaRef;
}
});
Object.defineProperty(exports, "transform", {
enumerable: true,
get: function() {
return transform;
}
});
Object.defineProperty(exports, "walk", {
enumerable: true,
get: function() {
return walk;
}
});
//# sourceMappingURL=visitor-DpKZ9Tk0.cjs.map
{"version":3,"file":"visitor-DpKZ9Tk0.cjs","names":["pascalCase","createSchema","inputDef","outputDef","operationDef","requestBodyDef","contentDef","responseDef","schemaDef","propertyDef","parameterDef","functionParameterDef","functionParametersDef","typeLiteralDef","indexedAccessTypeDef","objectBindingPatternDef","constDef","typeDef","functionDef","arrowFunctionDef","textDef","breakDef","jsxDef","importDef","exportDef","sourceDef","fileDef"],"sources":["../src/constants.ts","../src/guards.ts","../src/utils/refs.ts","../src/registry.ts","../src/visitor.ts"],"sourcesContent":["import type { HttpMethod } from './nodes/operation.ts'\nimport type { SchemaType } from './nodes/schema.ts'\n\n/**\n * Traversal depth for AST visitor utilities.\n *\n * - `'shallow'` visits only the immediate node, skipping children.\n * - `'deep'` recursively visits all descendant nodes.\n */\nexport type VisitorDepth = 'shallow' | 'deep'\n\nexport const visitorDepths = {\n shallow: 'shallow',\n deep: 'deep',\n} as const satisfies Record<VisitorDepth, VisitorDepth>\n\n/**\n * Schema type discriminators used by all AST schema nodes.\n *\n * Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).\n * Call `isScalarPrimitive()` to check for the scalar types.\n */\nexport const schemaTypes = {\n /**\n * Text value.\n */\n string: 'string',\n /**\n * Floating-point number (`float`, `double`).\n */\n number: 'number',\n /**\n * Whole number (`int32`). Use `bigint` for `int64`.\n */\n integer: 'integer',\n /**\n * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.\n */\n bigint: 'bigint',\n /**\n * Boolean value.\n */\n boolean: 'boolean',\n /**\n * Explicit null value.\n */\n null: 'null',\n /**\n * Any value (no type restriction).\n */\n any: 'any',\n /**\n * Unknown value (must be narrowed before usage).\n */\n unknown: 'unknown',\n /**\n * No return value (`void`).\n */\n void: 'void',\n /**\n * Object with named properties.\n */\n object: 'object',\n /**\n * Sequential list of items.\n */\n array: 'array',\n /**\n * Fixed-length list with position-specific items.\n */\n tuple: 'tuple',\n /**\n * \"One of\" multiple schema members.\n */\n union: 'union',\n /**\n * \"All of\" multiple schema members.\n */\n intersection: 'intersection',\n /**\n * Enum schema.\n */\n enum: 'enum',\n /**\n * Reference to another schema.\n */\n ref: 'ref',\n /**\n * Calendar date (for example `2026-03-24`).\n */\n date: 'date',\n /**\n * Date-time value (for example `2026-03-24T09:00:00Z`).\n */\n datetime: 'datetime',\n /**\n * Time-only value (for example `09:00:00`).\n */\n time: 'time',\n /**\n * UUID value.\n */\n uuid: 'uuid',\n /**\n * Email address value.\n */\n email: 'email',\n /**\n * URL value.\n */\n url: 'url',\n /**\n * IPv4 address value.\n */\n ipv4: 'ipv4',\n /**\n * IPv6 address value.\n */\n ipv6: 'ipv6',\n /**\n * Binary/blob value.\n */\n blob: 'blob',\n /**\n * Impossible value (`never`).\n */\n never: 'never',\n} as const satisfies Record<SchemaType, SchemaType>\n\nexport type ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'\n\n/**\n * Scalar primitive schema types used for union simplification and type narrowing.\n */\nconst SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])\n\n/**\n * Returns `true` when `type` is a scalar primitive that can be assigned without wrapping\n * (for example `string | number | boolean`).\n */\nexport function isScalarPrimitive(type: string): type is ScalarPrimitive {\n return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)\n}\n\n/**\n * HTTP method identifiers used by operation nodes.\n */\nexport const httpMethods = {\n get: 'GET',\n post: 'POST',\n put: 'PUT',\n patch: 'PATCH',\n delete: 'DELETE',\n head: 'HEAD',\n options: 'OPTIONS',\n trace: 'TRACE',\n} as const satisfies Record<Lowercase<HttpMethod>, HttpMethod>\n\n/**\n * Default concurrency limit for the `walk()` traversal utility.\n *\n * Set to 30 to balance I/O-bound resolver parallelism against event-loop and memory pressure\n * during large spec traversals. Override it for different hardware constraints.\n *\n * @example\n * ```ts\n * import { walk, WALK_CONCURRENCY } from '@kubb/ast'\n *\n * walk(root, { concurrency: WALK_CONCURRENCY, root: () => {} })\n * ```\n */\nexport const WALK_CONCURRENCY = 30\n\n/**\n * Number of spaces in one indentation level when assembling multi-line code as strings.\n * Set to 2, 3, … to change the indent width used by `buildObject`/`buildList`.\n */\nconst INDENT_SIZE = 2\n\n/**\n * One indentation level, derived from {@link INDENT_SIZE}.\n */\nexport const INDENT = Array.from({ length: INDENT_SIZE }, () => ' ').join('')\n","import type { HttpOperationNode, OperationNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'\n\n/**\n * Narrows a `SchemaNode` to the variant that matches `type`.\n *\n * @example\n * ```ts\n * const schema = createSchema({ type: 'string' })\n * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null\n * ```\n */\nexport function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null {\n return node?.type === type ? (node as SchemaNodeByType[T]) : null\n}\n\n/**\n * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.\n *\n * @example\n * ```ts\n * if (isHttpOperationNode(node)) {\n * console.log(node.method, node.path)\n * }\n * ```\n */\nexport function isHttpOperationNode(node: OperationNode): node is HttpOperationNode {\n return node.protocol === 'http' || (node.method !== undefined && node.path !== undefined)\n}\n","import { pascalCase } from '@internals/utils'\nimport { narrowSchema } from '../guards.ts'\nimport type { OperationNode, ParameterNode, SchemaNode } from '../nodes/index.ts'\nimport { createSchema, type SchemaType } from '../nodes/schema.ts'\nimport type { OperationParamsResolver, ParamGroupType } from './operationParams.ts'\n\nconst plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)\n\n/**\n * Returns the last path segment of a reference string.\n *\n * @example\n * ```ts\n * extractRefName('#/components/schemas/Pet') // 'Pet'\n * ```\n */\nexport function extractRefName(ref: string): string {\n return ref.split('/').at(-1) ?? ref\n}\n\n/**\n * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.\n *\n * Returns `null` for non-ref nodes or when no name resolves.\n *\n * @example\n * ```ts\n * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })\n * // => 'Pet'\n * ```\n */\nexport function resolveRefName(node: SchemaNode | undefined): string | null {\n if (!node || node.type !== 'ref') return null\n if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null\n\n return node.name ?? node.schema?.name ?? null\n}\n\n/**\n * Builds a PascalCase child schema name by joining a parent name and property name.\n * Returns `null` when there is no parent to nest under.\n *\n * @example\n * ```ts\n * childName('Order', 'shipping_address') // 'OrderShippingAddress'\n * childName(undefined, 'params') // null\n * ```\n */\nexport function childName(parentName: string | null | undefined, propName: string): string | null {\n return parentName ? pascalCase([parentName, propName].join(' ')) : null\n}\n\n/**\n * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any\n * empty parts.\n *\n * @example\n * ```ts\n * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'\n * ```\n */\nexport function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {\n return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))\n}\n\n/**\n * Merges a ref node with its resolved schema, giving usage-site fields precedence.\n *\n * Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override\n * the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })\n * const merged = syncSchemaRef(ref) // merges with resolved Pet schema\n * ```\n */\nexport function syncSchemaRef(node: SchemaNode): SchemaNode {\n const ref = narrowSchema(node, 'ref')\n\n if (!ref) return node\n if (!ref.schema) return node\n\n const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref\n\n // Filter out undefined override values so they don't shadow the resolved schema's fields.\n const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))\n\n return createSchema({ ...ref.schema, ...definedOverrides })\n}\n\n/**\n * Type guard that returns `true` when a schema emits as a plain `string` type.\n *\n * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`\n * types, returns `true` only when `representation` is `'string'` rather than `'date'`.\n */\nexport function isStringType(node: SchemaNode): boolean {\n if (plainStringTypes.has(node.type)) {\n return true\n }\n\n const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')\n if (temporal) {\n return temporal.representation !== 'date'\n }\n\n return false\n}\n\n/**\n * Derives a {@link ParamGroupType} for a query or header group from the resolver.\n *\n * Returns `null` when there is no resolver, no params, or the group name equals the\n * individual param name (so there is no real group to emit).\n */\nexport function resolveGroupType({\n node,\n params,\n group,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n group: 'query' | 'header'\n resolver: OperationParamsResolver | undefined\n}): ParamGroupType | null {\n if (!resolver || !params.length) {\n return null\n }\n const firstParam = params[0]!\n const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName\n const groupName = groupMethod.call(resolver, node, firstParam)\n if (groupName === resolver.resolveParamName(node, firstParam)) {\n return null\n }\n return { type: groupName, optional: params.every((p) => !p.required) }\n}\n","import type { NodeDef } from './node.ts'\nimport { arrowFunctionDef, breakDef, constDef, functionDef, jsxDef, textDef, typeDef } from './nodes/code.ts'\nimport { contentDef } from './nodes/content.ts'\nimport { exportDef, fileDef, importDef, sourceDef } from './nodes/file.ts'\nimport { functionParameterDef, functionParametersDef, indexedAccessTypeDef, objectBindingPatternDef, typeLiteralDef } from './nodes/function.ts'\nimport { inputDef } from './nodes/input.ts'\nimport { operationDef } from './nodes/operation.ts'\nimport { outputDef } from './nodes/output.ts'\nimport { parameterDef } from './nodes/parameter.ts'\nimport { propertyDef } from './nodes/property.ts'\nimport { requestBodyDef } from './nodes/requestBody.ts'\nimport { responseDef } from './nodes/response.ts'\nimport { schemaDef } from './nodes/schema.ts'\n\n// Surface every def from one place so the package barrel re-exports them with `export * from './registry.ts'`.\n// Adding a node means adding its `defineNode` to a `nodes/*.ts` file and listing it in `nodeDefs` below, nothing else.\nexport {\n arrowFunctionDef,\n breakDef,\n constDef,\n contentDef,\n exportDef,\n fileDef,\n functionDef,\n functionParameterDef,\n functionParametersDef,\n importDef,\n indexedAccessTypeDef,\n inputDef,\n jsxDef,\n objectBindingPatternDef,\n operationDef,\n outputDef,\n parameterDef,\n propertyDef,\n requestBodyDef,\n responseDef,\n schemaDef,\n sourceDef,\n textDef,\n typeDef,\n typeLiteralDef,\n}\n\n/**\n * Every node definition. Adding a node means adding its `defineNode` to one\n * `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.\n */\nexport const nodeDefs = [\n inputDef,\n outputDef,\n operationDef,\n requestBodyDef,\n contentDef,\n responseDef,\n schemaDef,\n propertyDef,\n parameterDef,\n functionParameterDef,\n functionParametersDef,\n typeLiteralDef,\n indexedAccessTypeDef,\n objectBindingPatternDef,\n constDef,\n typeDef,\n functionDef,\n arrowFunctionDef,\n textDef,\n breakDef,\n jsxDef,\n importDef,\n exportDef,\n sourceDef,\n fileDef,\n] satisfies ReadonlyArray<NodeDef>\n","import type { VisitorDepth } from './constants.ts'\nimport { visitorDepths, WALK_CONCURRENCY } from './constants.ts'\nimport type { NodeDef } from './node.ts'\nimport type {\n ContentNode,\n InputNode,\n Node,\n NodeKind,\n OperationNode,\n OutputNode,\n ParameterNode,\n PropertyNode,\n RequestBodyNode,\n ResponseNode,\n SchemaNode,\n} from './nodes/index.ts'\nimport { nodeDefs } from './registry.ts'\n\n/**\n * Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).\n * Derived from each definition's `children`.\n */\nconst VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => (def.children ? [[def.kind, def.children] as const] : []))) as Partial<\n Record<NodeKind, ReadonlyArray<string>>\n>\n\n/**\n * Maps a node kind to the matching visitor callback name. Derived from each\n * definition's `visitorKey`.\n */\nconst VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => (def.visitorKey ? [[def.kind, def.visitorKey] as const] : []))) as Partial<\n Record<NodeKind, NonNullable<NodeDef['visitorKey']>>\n>\n\n/**\n * Per-kind builders rerun after children are rebuilt. Derived from each\n * definition's `rebuild` flag.\n */\nconst nodeRebuilders = Object.fromEntries(\n nodeDefs.flatMap((def) => (def.rebuild ? [[def.kind, def.create as unknown as (node: Node) => Node] as const] : [])),\n) as Partial<Record<NodeKind, (node: Node) => Node>>\n\n/**\n * Creates a small async concurrency limiter.\n *\n * At most `concurrency` tasks are in flight at once. Extra tasks are queued.\n *\n * @example\n * ```ts\n * const limit = createLimit(2)\n * for (const task of [taskA, taskB, taskC]) {\n * await limit(() => task())\n * }\n * // only 2 tasks run at the same time\n * ```\n */\nfunction createLimit(concurrency: number) {\n let active = 0\n const queue: Array<() => void> = []\n\n function next() {\n if (active < concurrency && queue.length > 0) {\n active++\n queue.shift()!()\n }\n }\n\n return function limit<T>(fn: () => Promise<T> | T): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n queue.push(() => {\n Promise.resolve(fn())\n .then(resolve, reject)\n .finally(() => {\n active--\n next()\n })\n })\n next()\n })\n }\n}\n\ntype LimitFn = ReturnType<typeof createLimit>\n\n/**\n * Ordered mapping of `[NodeType, ParentType]` pairs.\n *\n * `ParentOf` uses this map to find parent types.\n */\ntype ParentNodeMap = [\n [InputNode, undefined],\n [OutputNode, undefined],\n [OperationNode, InputNode],\n [RequestBodyNode, OperationNode],\n [ContentNode, RequestBodyNode | ResponseNode],\n [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode],\n [PropertyNode, SchemaNode],\n [ParameterNode, OperationNode],\n [ResponseNode, OperationNode],\n]\n\n/**\n * Resolves the parent node type for a given AST node type.\n *\n * Visitor context relies on this so `ctx.parent` is typed for each callback.\n *\n * @example\n * ```ts\n * type InputParent = ParentOf<InputNode>\n * // undefined\n * ```\n *\n * @example\n * ```ts\n * type PropertyParent = ParentOf<PropertyNode>\n * // SchemaNode\n * ```\n *\n * @example\n * ```ts\n * type SchemaParent = ParentOf<SchemaNode>\n * // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode\n * ```\n */\nexport type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [\n infer TEntry extends [Node, unknown],\n ...infer TRest extends ReadonlyArray<[Node, unknown]>,\n]\n ? T extends TEntry[0]\n ? TEntry[1]\n : ParentOf<T, TRest>\n : Node\n\n/**\n * Traversal context passed as the second argument to every visitor callback.\n * `parent` is typed from the current node type.\n *\n * @example\n * ```ts\n * const visitor: Visitor = {\n * schema(node, { parent }) {\n * // parent type is narrowed by node kind\n * },\n * }\n * ```\n */\nexport type VisitorContext<T extends Node = Node> = {\n /**\n * Parent node of the currently visited node.\n * For `InputNode`, this is `undefined`.\n */\n parent?: ParentOf<T>\n}\n\n/**\n * Synchronous visitor consumed by `transform`. Each optional callback runs\n * for the matching node type. Return a new node to replace it, or `undefined`\n * to leave it untouched.\n *\n * Plugins typically expose `transformer` so users can supply a `Visitor` that\n * rewrites the AST before printing.\n *\n * @example Prefix every operationId\n * ```ts\n * const visitor: Visitor = {\n * operation(node) {\n * return { ...node, operationId: `api_${node.operationId}` }\n * },\n * }\n * ```\n *\n * @example Strip schema descriptions\n * ```ts\n * const visitor: Visitor = {\n * schema(node) {\n * return { ...node, description: undefined }\n * },\n * }\n * ```\n */\nexport type Visitor = {\n input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode\n output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode\n}\n\n/**\n * A visitor callback result that may be sync or async.\n */\ntype MaybePromise<T> = T | Promise<T>\n\n/**\n * Async visitor for `walk`. Synchronous `Visitor` objects are compatible.\n *\n * @example\n * ```ts\n * const visitor: AsyncVisitor = {\n * async operation(node) {\n * await Promise.resolve(node.operationId)\n * },\n * }\n * ```\n */\ntype AsyncVisitor = {\n input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>\n output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>\n}\n\n/**\n * Visitor used by `collect`.\n *\n * @example\n * ```ts\n * const visitor: CollectVisitor<string> = {\n * operation(node) {\n * return node.operationId\n * },\n * }\n * ```\n */\ntype CollectVisitor<T> = {\n input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined\n output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined\n operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined\n schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined\n property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined\n parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined\n response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined\n}\n\n/**\n * Options for `transform`.\n *\n * @example\n * ```ts\n * const options: TransformOptions = { depth: 'deep', schema: (node) => node }\n * ```\n *\n * @example\n * ```ts\n * // Only transform the current node, not nested children\n * const options: TransformOptions = { depth: 'shallow', schema: (node) => node }\n * ```\n */\nexport type TransformOptions = Visitor & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Internal parent override used during recursion.\n */\n parent?: Node\n}\n\n/**\n * Options for `walk`.\n *\n * @example\n * ```ts\n * const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }\n * ```\n */\nexport type WalkOptions = AsyncVisitor & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Maximum number of sibling nodes visited concurrently.\n * @default 30\n */\n concurrency?: number\n}\n\n/**\n * Options for `collect`.\n *\n * @example\n * ```ts\n * const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }\n * ```\n */\nexport type CollectOptions<T> = CollectVisitor<T> & {\n /**\n * Traversal depth.\n * @default 'deep'\n */\n depth?: VisitorDepth\n /**\n * Internal parent override used during recursion.\n */\n parent?: Node\n}\n\nconst visitorKeysByKind = VISITOR_KEYS as Record<string, ReadonlyArray<string> | undefined>\n\n/**\n * Returns `true` when `value` is an AST node (an object carrying a `kind`).\n */\nfunction isNode(value: unknown): value is Node {\n return typeof value === 'object' && value !== null && 'kind' in value\n}\n\n/**\n * Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.\n *\n * `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.\n *\n * @example\n * ```ts\n * const children = getChildren(operationNode, true)\n * // returns parameters, the request body, and responses\n * ```\n */\nfunction* getChildren(node: Node, recurse: boolean): Generator<Node, void, undefined> {\n if (node.kind === 'Schema' && !recurse) return\n\n const keys = visitorKeysByKind[node.kind]\n if (!keys) return\n\n const record = node as unknown as Record<string, unknown>\n for (const key of keys) {\n const value = record[key]\n if (Array.isArray(value)) {\n for (const item of value) if (isNode(item)) yield item\n } else if (isNode(value)) {\n yield value\n }\n }\n}\n\n/**\n * Runs the visitor callback that matches `node.kind` with the traversal\n * context. The result is a replacement node, a collected value, or `undefined`\n * when no callback is registered for the kind.\n *\n * Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives\n * in one place. `TResult` is the caller's expected return: the same node type\n * for `transform`, the collected value type for `collectLazy`, ignored for `walk`.\n */\nfunction applyVisitor<TResult>(node: Node, visitor: Visitor | AsyncVisitor | CollectVisitor<unknown>, parent: Node | undefined): TResult | null | undefined {\n const key = VISITOR_KEY_BY_KIND[node.kind]\n if (!key) return undefined\n\n const fn = visitor[key] as ((node: Node, context: VisitorContext) => TResult | null | undefined) | undefined\n\n return fn?.(node, { parent })\n}\n\n/**\n * Async depth-first traversal for side effects. Visitor return values are\n * ignored. Use `transform` when you want to rewrite nodes.\n *\n * Sibling nodes at each depth run concurrently up to `options.concurrency`\n * (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor\n * work. Lower values reduce memory pressure.\n *\n * @example Log every operation\n * ```ts\n * await walk(root, {\n * operation(node) {\n * console.log(node.operationId)\n * },\n * })\n * ```\n *\n * @example Only visit the root node\n * ```ts\n * await walk(root, { depth: 'shallow', input: () => {} })\n * ```\n */\nexport async function walk(node: Node, options: WalkOptions): Promise<void> {\n const recurse = (options.depth ?? visitorDepths.deep) === visitorDepths.deep\n const limit = createLimit(options.concurrency ?? WALK_CONCURRENCY)\n\n return _walk(node, options, recurse, limit, undefined)\n}\n\nasync function _walk(node: Node, visitor: AsyncVisitor, recurse: boolean, limit: LimitFn, parent: Node | undefined): Promise<void> {\n await limit(() => applyVisitor(node, visitor, parent))\n\n // Visit siblings concurrently and let the shared `limit` cap how many callbacks\n // run at once. Awaiting each child sequentially here would serialize the whole\n // traversal and make `concurrency` inert. Every visitor callback would run one\n // at a time regardless of the limit.\n const children = Array.from(getChildren(node, recurse))\n if (children.length === 0) return\n\n await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)))\n}\n\n/**\n * Synchronous depth-first transform. Each visitor callback can return a\n * replacement node. Returning `undefined` keeps the original.\n *\n * The original tree is never mutated, a new tree is returned. Pass\n * `depth: 'shallow'` to skip recursion into children.\n *\n * @example Prefix every operationId\n * ```ts\n * const next = transform(root, {\n * operation(node) {\n * return { ...node, operationId: `prefixed_${node.operationId}` }\n * },\n * })\n * ```\n *\n * @example Replace only the root node\n * ```ts\n * const next = transform(root, {\n * depth: 'shallow',\n * input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),\n * })\n * ```\n */\nexport function transform(node: InputNode, options: TransformOptions): InputNode\nexport function transform(node: OutputNode, options: TransformOptions): OutputNode\nexport function transform(node: OperationNode, options: TransformOptions): OperationNode\nexport function transform(node: SchemaNode, options: TransformOptions): SchemaNode\nexport function transform(node: PropertyNode, options: TransformOptions): PropertyNode\nexport function transform(node: ParameterNode, options: TransformOptions): ParameterNode\nexport function transform(node: ResponseNode, options: TransformOptions): ResponseNode\nexport function transform(node: Node, options: TransformOptions): Node\nexport function transform(node: Node, options: TransformOptions): Node {\n const { depth, parent, ...visitor } = options\n const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep\n\n const visited = applyVisitor<Node>(node, visitor, parent) ?? node\n const rebuilt = transformChildren(visited, options, recurse)\n\n // Structural sharing: when the visitor and child rebuild both left this node\n // untouched, return the original reference so callers can detect \"nothing\n // changed\" by identity and ancestors can avoid reallocating.\n if (rebuilt === node) return node\n\n const rebuild = nodeRebuilders[rebuilt.kind]\n return rebuild ? rebuild(rebuilt) : rebuilt\n}\n\n/**\n * Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming\n * each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.\n * `Schema` children are skipped in shallow mode.\n */\nfunction transformChildren(node: Node, options: TransformOptions, recurse: boolean): Node {\n if (node.kind === 'Schema' && !recurse) return node\n\n const keys = visitorKeysByKind[node.kind]\n if (!keys) return node\n\n const record = node as unknown as Record<string, unknown>\n const childOptions = { ...options, parent: node }\n let updates: Record<string, unknown> | undefined\n\n for (const key of keys) {\n if (!(key in record)) continue\n const value = record[key]\n if (Array.isArray(value)) {\n let changed = false\n const mapped = value.map((item) => {\n if (!isNode(item)) return item\n const next = transform(item, childOptions)\n if (next !== item) changed = true\n return next\n })\n if (changed) (updates ??= {})[key] = mapped\n } else if (isNode(value)) {\n const next = transform(value, childOptions)\n if (next !== value) (updates ??= {})[key] = next\n }\n }\n\n return updates ? ({ ...node, ...updates } as Node) : node\n}\n/**\n * Lazy depth-first collection pass. Yields every non-null value returned by\n * the visitor callbacks. Use `collect` for the eager array form.\n *\n * @example Collect every operationId\n * ```ts\n * const ids: string[] = []\n * for (const id of collectLazy<string>(root, {\n * operation(node) {\n * return node.operationId\n * },\n * })) {\n * ids.push(id)\n * }\n * ```\n */\nexport function* collectLazy<T>(node: Node, options: CollectOptions<T>): Generator<T, void, undefined> {\n const { depth, parent, ...visitor } = options\n const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep\n\n const v = applyVisitor<T>(node, visitor, parent)\n if (v != null) yield v\n\n for (const child of getChildren(node, recurse)) {\n yield* collectLazy(child, { ...options, parent: node })\n }\n}\n\n/**\n * Eager depth-first collection pass. Gathers every non-null value the visitor\n * callbacks return into an array.\n *\n * @example Collect every operationId\n * ```ts\n * const ids = collect<string>(root, {\n * operation(node) {\n * return node.operationId\n * },\n * })\n * ```\n */\nexport function collect<T>(node: Node, options: CollectOptions<T>): Array<T> {\n return Array.from(collectLazy(node, options))\n}\n"],"mappings":";;AAWA,MAAa,gBAAgB;CAC3B,SAAS;CACT,MAAM;AACR;;;;;;;AAQA,MAAa,cAAc;;;;CAIzB,QAAQ;;;;CAIR,QAAQ;;;;CAIR,SAAS;;;;CAIT,QAAQ;;;;CAIR,SAAS;;;;CAIT,MAAM;;;;CAIN,KAAK;;;;CAIL,SAAS;;;;CAIT,MAAM;;;;CAIN,QAAQ;;;;CAIR,OAAO;;;;CAIP,OAAO;;;;CAIP,OAAO;;;;CAIP,cAAc;;;;CAId,MAAM;;;;CAIN,KAAK;;;;CAIL,MAAM;;;;CAIN,UAAU;;;;CAIV,MAAM;;;;CAIN,MAAM;;;;CAIN,OAAO;;;;CAIP,KAAK;;;;CAIL,MAAM;;;;CAIN,MAAM;;;;CAIN,MAAM;;;;CAIN,OAAO;AACT;;;;AAOA,MAAM,yBAAyB,IAAI,IAAqB;CAAC;CAAU;CAAU;CAAW;CAAU;AAAS,CAAC;;;;;AAM5G,SAAgB,kBAAkB,MAAuC;CACvE,OAAO,uBAAuB,IAAI,IAAuB;AAC3D;;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,MAAM,OAAO;CAElB,MAAM,SAAS;CACf,MAAM,eAAe;EAAE,GAAG;EAAS,QAAQ;CAAK;CAChD,IAAI;CAEJ,KAAK,MAAM,OAAO,MAAM;EACtB,IAAI,EAAE,OAAO,SAAS;EACtB,MAAM,QAAQ,OAAO;EACrB,IAAI,MAAM,QAAQ,KAAK,GAAG;GACxB,IAAI,UAAU;GACd,MAAM,SAAS,MAAM,KAAK,SAAS;IACjC,IAAI,CAAC,OAAO,IAAI,GAAG,OAAO;IAC1B,MAAM,OAAO,UAAU,MAAM,YAAY;IACzC,IAAI,SAAS,MAAM,UAAU;IAC7B,OAAO;GACT,CAAC;GACD,IAAI,SAAS,CAAC,YAAY,CAAC,EAAA,CAAG,OAAO;EACvC,OAAO,IAAI,OAAO,KAAK,GAAG;GACxB,MAAM,OAAO,UAAU,OAAO,YAAY;GAC1C,IAAI,SAAS,OAAO,CAAC,YAAY,CAAC,EAAA,CAAG,OAAO;EAC9C;CACF;CAEA,OAAO,UAAW;EAAE,GAAG;EAAM,GAAG;CAAQ,IAAa;AACvD;;;;;;;;;;;;;;;;;AAiBA,UAAiB,YAAe,MAAY,SAA2D;CACrG,MAAM,EAAE,OAAO,QAAQ,GAAG,YAAY;CACtC,MAAM,WAAW,SAAS,cAAc,UAAU,cAAc;CAEhE,MAAM,IAAI,aAAgB,MAAM,SAAS,MAAM;CAC/C,IAAI,KAAK,MAAM,MAAM;CAErB,KAAK,MAAM,SAAS,YAAY,MAAM,OAAO,GAC3C,OAAO,YAAY,OAAO;EAAE,GAAG;EAAS,QAAQ;CAAK,CAAC;AAE1D;;;;;;;;;;;;;;AAeA,SAAgB,QAAW,MAAY,SAAsC;CAC3E,OAAO,MAAM,KAAK,YAAY,MAAM,OAAO,CAAC;AAC9C"}
import { createInput } from './nodes/input.ts'
import type { InputNode } from './nodes/input.ts'
import { createOperation } from './nodes/operation.ts'
import { createParameter } from './nodes/parameter.ts'
import { createProperty } from './nodes/property.ts'
import { createResponse } from './nodes/response.ts'
import { createSchema } from './nodes/schema.ts'
/**
* Builds a minimal sample AST with one `Pet` schema and one `getPetById` operation.
*/
export function buildSampleTree(): InputNode {
const petSchema = createSchema({
type: 'object',
name: 'Pet',
properties: [
createProperty({
name: 'id',
schema: createSchema({ type: 'integer' }),
required: true,
}),
createProperty({
name: 'name',
schema: createSchema({ type: 'string' }),
required: true,
}),
],
})
const operation = createOperation({
operationId: 'getPetById',
method: 'GET',
path: '/pets/{petId}',
tags: ['pets'],
parameters: [
createParameter({
name: 'petId',
in: 'path',
schema: createSchema({ type: 'integer' }),
required: true,
}),
],
responses: [
createResponse({
statusCode: '200',
schema: createSchema({ type: 'ref', name: 'Pet' }),
}),
createResponse({
statusCode: '404',
schema: createSchema({ type: 'ref', name: 'Error' }),
}),
],
})
return createInput({ schemas: [petSchema], operations: [operation] })
}
import type { BaseNode, NodeKind } from './nodes/base.ts'
import type { SchemaNode } from './nodes/index.ts'
/**
* Visitor callback names, one per traversable node kind. Kept in sync with the
* keys of `Visitor` in `visitor.ts`.
*/
type VisitorKey = 'input' | 'output' | 'operation' | 'schema' | 'property' | 'parameter' | 'response'
/**
* Distributive `Omit` that preserves each member of a union.
*
* @example
* ```ts
* type A = { kind: 'a'; keep: string; drop: number }
* type B = { kind: 'b'; keep: boolean; drop: number }
* type Result = DistributiveOmit<A | B, 'drop'>
* // -> { kind: 'a'; keep: string } | { kind: 'b'; keep: boolean }
* ```
*/
export type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never
/**
* Builds a type guard that matches nodes of the given `kind`.
*/
function isKind<T extends BaseNode>(kind: NodeKind) {
return (node: unknown): node is T => (node as BaseNode).kind === kind
}
/**
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
* object properties combine "required" and "nullable" into a single AST.
*
* - Non-required + non-nullable → `optional: true`.
* - Non-required + nullable → `nullish: true`.
* - Required → both flags cleared.
*/
export function syncOptionality(schema: SchemaNode, required: boolean): SchemaNode {
const nullable = schema.nullable ?? false
return {
...schema,
optional: !required && !nullable ? true : undefined,
nullish: !required && nullable ? true : undefined,
}
}
/**
* The single definition derived from one {@link defineNode} call: the node's
* `create` builder, its `is` guard, and the traversal metadata the registry
* collects into the visitor tables.
*/
export type NodeDef<TNode extends BaseNode = BaseNode, TInput = never> = {
/**
* Node discriminator this definition owns.
*/
kind: NodeKind
/**
* Builds a node from its input, applying `defaults` and the optional `build` hook.
*/
create: (input: TInput) => TNode
/**
* Type guard matching this node kind.
*/
is: (node: unknown) => node is TNode
/**
* Child node fields in traversal order. Feeds `VISITOR_KEYS`.
*/
children?: ReadonlyArray<string>
/**
* Visitor callback name. Feeds `VISITOR_KEY_BY_KIND`.
*/
visitorKey?: VisitorKey
/**
* When `true`, `create` is rerun after children are rebuilt so computed fields
* stay in sync. Feeds `nodeRebuilders`.
*/
rebuild?: boolean
}
type DefineNodeConfig<TNode extends BaseNode, TInput, TBuilt extends object> = {
kind: TNode['kind']
defaults?: Partial<TNode>
build?: (input: TInput) => TBuilt
children?: ReadonlyArray<string>
visitorKey?: VisitorKey
rebuild?: boolean
}
/**
* Defines a node once and derives its `create` builder, `is` guard, and traversal
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
* `kind`, so node construction lives in one place without scattered `as` casts.
*
* Set `rebuild: true` when the `build` hook derives fields from children. After a
* transform rewrites those children, the registry reruns `create` so the derived
* fields stay correct.
*
* @example Simple node
* ```ts
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
* const createImport = importDef.create
* ```
*
* @example Node with a build hook that is rerun on transform
* ```ts
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
* kind: 'Property',
* build: (props) => ({ ...props, required: props.required ?? false }),
* children: ['schema'],
* visitorKey: 'property',
* rebuild: true,
* })
* ```
*/
export function defineNode<TNode extends BaseNode, TInput = Omit<TNode, 'kind'>, TBuilt extends object = Omit<TNode, 'kind'>>(
config: DefineNodeConfig<TNode, TInput, TBuilt>,
): NodeDef<TNode, TInput> {
const { kind, defaults, build, children, visitorKey, rebuild } = config
function create(input: TInput): TNode {
const base = build ? build(input) : input
return { ...defaults, ...(base as object), kind } as TNode
}
return { kind, create, is: isKind<TNode>(kind), children, visitorKey, rebuild }
}
/**
* All supported HTTP status code literals as strings, as used in API specs
* (for example, `"200"` and `"404"`).
*/
type HttpStatusCode =
// 1xx Informational
| '100'
| '101'
| '102'
| '103'
// 2xx Success
| '200'
| '201'
| '202'
| '203'
| '204'
| '205'
| '206'
| '207'
| '208'
| '226'
// 3xx Redirection
| '300'
| '301'
| '302'
| '303'
| '304'
| '305'
| '307'
| '308'
// 4xx Client Error
| '400'
| '401'
| '402'
| '403'
| '404'
| '405'
| '406'
| '407'
| '408'
| '409'
| '410'
| '411'
| '412'
| '413'
| '414'
| '415'
| '416'
| '417'
| '418'
| '421'
| '422'
| '423'
| '424'
| '425'
| '426'
| '428'
| '429'
| '431'
| '451'
// 5xx Server Error
| '500'
| '501'
| '502'
| '503'
| '504'
| '505'
| '506'
| '507'
| '508'
| '510'
| '511'
/**
* Response status code literal used by operations.
*
* Includes specific HTTP status code strings and `"default"` for catch-all responses.
*
* @example
* ```ts
* const status: StatusCode = '200'
* const fallback: StatusCode = 'default'
* ```
*/
export type StatusCode = HttpStatusCode | 'default'
import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'
/**
* Runtime context passed as `this` to printer handlers.
*
* `this.transform` dispatches to node-level handlers from `nodes`.
*
* @example
* ```ts
* const context: PrinterHandlerContext<string, {}> = {
* options: {},
* transform: () => 'value',
* }
* ```
*/
type PrinterHandlerContext<TOutput, TOptions extends object> = {
/**
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
*/
transform: (node: SchemaNode) => TOutput | null
/**
* Options for this printer instance.
*/
options: TOptions
}
/**
* Handler for one schema node type.
*
* Use a regular function (not an arrow function) if you need `this`.
*
* @example
* ```ts
* const handler: PrinterHandler<string, {}, 'string'> = function () {
* return 'string'
* }
* ```
*/
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
this: PrinterHandlerContext<TOutput, TOptions>,
node: SchemaNodeByType[T],
) => TOutput | null
/**
* Partial map of per-node-type handler overrides for a printer.
*
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
* Supply only the handlers you want to replace. The printer's built-in
* defaults fill in the rest.
*
* @example
* ```ts
* pluginZod({
* printer: {
* nodes: {
* date(): string {
* return 'z.string().date()'
* },
* } satisfies PrinterPartial<string, PrinterZodOptions>,
* },
* })
* ```
*/
export type PrinterPartial<TOutput, TOptions extends object> = Partial<{
[K in SchemaType]: PrinterHandler<TOutput, TOptions, K>
}>
/**
* Generic shape used by `definePrinter`.
*
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
* - `TOptions` options passed to and stored on the printer instance
* - `TOutput` the type emitted by node handlers
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
*
* @example
* ```ts
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
* ```
*/
export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
name: TName
options: TOptions
output: TOutput
printOutput: TPrintOutput
}
/**
* Printer instance returned by a printer factory.
*
* @example
* ```ts
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
* ```
*/
export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
/**
* Unique identifier supplied at creation time.
*/
name: T['name']
/**
* Options for this printer instance.
*/
options: T['options']
/**
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
* Always dispatches through the `nodes` map. Never calls the `print` override.
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
*/
transform: (node: SchemaNode) => T['output'] | null
/**
* Public printer. If the builder provides a root-level `print`, this calls that
* higher-level function (which may produce full declarations).
* Otherwise, falls back to the node-level dispatcher.
*/
print: (node: SchemaNode) => T['printOutput'] | null
}
/**
* Builder function passed to `definePrinter`.
*
* It receives resolved options and returns:
* - `name`
* - `options`
* - `nodes` handlers
* - optional top-level `print` override
*
* @example
* ```ts
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
* ```
*/
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
name: T['name']
/**
* Options to store on the printer.
*/
options: T['options']
nodes: Partial<{
[K in SchemaType]: PrinterHandler<T['output'], T['options'], K>
}>
/**
* Optional root-level print override. When provided, becomes the public `printer.print`.
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
* not the override itself, so recursion is safe.
*/
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
}
/**
* Defines a schema printer: a function that takes a `SchemaNode` and emits
* code in your target language. Each plugin that produces code from schemas
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
* with this helper.
*
* The builder receives resolved options and returns:
*
* - `name` unique identifier for the printer.
* - `options` stored on the returned printer instance.
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
* output (a string, a TypeScript AST node, ...) for that schema type.
* - `print` (optional), top-level override exposed as `printer.print`.
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
*
* Without a `print` override, `printer.print` falls back to `printer.transform`
* (the node-level dispatcher).
*
* @example Tiny Zod printer
* ```ts
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
*
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
*
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
* name: 'zod',
* options: { strict: options.strict ?? true },
* nodes: {
* string: () => 'z.string()',
* object(node) {
* const props = node.properties
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
* .join(', ')
* return `z.object({ ${props} })`
* },
* },
* }))
* ```
*/
export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {
return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>
}
/**
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
*
* @example
* ```ts
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
* (node) => node.kind,
* )
* ```
*/
export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {
return function <T extends PrinterFactoryOptions>(
build: (options: T['options']) => {
name: T['name']
options: T['options']
nodes: Partial<{
[K in TKey]: (
this: {
transform: (node: TNode) => T['output'] | null
options: T['options']
},
node: TNodeByKey[K],
) => T['output'] | null
}>
print?: (
this: {
transform: (node: TNode) => T['output'] | null
options: T['options']
},
node: TNode,
) => T['printOutput'] | null
},
): (options?: T['options']) => {
name: T['name']
options: T['options']
transform: (node: TNode) => T['output'] | null
print: (node: TNode) => T['printOutput'] | null
} {
return (options) => {
const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))
const context = {
options: resolvedOptions,
transform: (node: TNode): T['output'] | null => {
const key = getKey(node)
if (key === null) return null
const handler = nodes[key]
if (!handler) return null
return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)
},
}
return {
name,
options: resolvedOptions,
transform: context.transform,
print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,
}
}
}
}