@kubb/ast 5.0.0-beta.71 → 5.0.0-beta.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{defineMacro-VfsvblGi.js → defineMacro-B5Plh1uq.js} +2 -2
- package/dist/{defineMacro-VfsvblGi.js.map → defineMacro-B5Plh1uq.js.map} +1 -1
- package/dist/{defineMacro-BXpTwp0y.d.ts → defineMacro-Bm07PZIF.d.ts} +2 -2
- package/dist/{defineMacro-5Dvct8k_.cjs → defineMacro-CI0a-SWY.cjs} +2 -2
- package/dist/{defineMacro-5Dvct8k_.cjs.map → defineMacro-CI0a-SWY.cjs.map} +1 -1
- package/dist/{index-DJEyS5y6.d.ts → index-aTdOZp_L.d.ts} +4 -263
- package/dist/index.cjs +2 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +9 -24
- package/dist/index.js +3 -13
- package/dist/index.js.map +1 -1
- package/dist/macros.cjs +3 -3
- package/dist/macros.d.ts +1 -1
- package/dist/macros.js +3 -3
- package/dist/{refs-D18OeCgb.js → refs-1t3qfkLE.js} +3 -19
- package/dist/refs-1t3qfkLE.js.map +1 -0
- package/dist/{refs-DuP3_Leg.cjs → refs-Dga4lfv7.cjs} +2 -24
- package/dist/refs-Dga4lfv7.cjs.map +1 -0
- package/dist/{types-BsP1SK9j.d.ts → types-DqFHKeAc.d.ts} +2 -2
- package/dist/types.d.ts +4 -5
- package/dist/utils.cjs +2 -192
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.ts +2 -3
- package/dist/utils.js +3 -191
- package/dist/utils.js.map +1 -1
- package/dist/{visitor-dcVC5TOW.js → visitor-CeO-upp5.js} +2 -128
- package/dist/visitor-CeO-upp5.js.map +1 -0
- package/dist/{visitor-CQdSiClY.cjs → visitor-ChiWLwlh.cjs} +1 -193
- package/dist/visitor-ChiWLwlh.cjs.map +1 -0
- package/package.json +2 -2
- package/dist/operationParams-BaY12i2I.d.ts +0 -148
- package/dist/refs-D18OeCgb.js.map +0 -1
- package/dist/refs-DuP3_Leg.cjs.map +0 -1
- package/dist/visitor-CQdSiClY.cjs.map +0 -1
- package/dist/visitor-dcVC5TOW.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
-
import {
|
|
2
|
+
import { X as visitorKeys, r as transform } from "./visitor-CeO-upp5.js";
|
|
3
3
|
//#region src/defineMacro.ts
|
|
4
4
|
/**
|
|
5
5
|
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
|
|
@@ -95,4 +95,4 @@ function applyMacros(root, macros, options) {
|
|
|
95
95
|
//#endregion
|
|
96
96
|
export { composeMacros as n, defineMacro as r, applyMacros as t };
|
|
97
97
|
|
|
98
|
-
//# sourceMappingURL=defineMacro-
|
|
98
|
+
//# sourceMappingURL=defineMacro-B5Plh1uq.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defineMacro-
|
|
1
|
+
{"version":3,"file":"defineMacro-B5Plh1uq.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 used to tell macros apart, for example `'simplify-union'`.\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"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { n as __name } from "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
-
import {
|
|
2
|
+
import { $ as SchemaNode, C as ParameterNode, ct as PropertyNode, f as OperationNode, h as ResponseNode, n as OutputNode, o as InputNode, t as Node, y as RequestBodyNode, z as ContentNode } from "./index-aTdOZp_L.js";
|
|
3
3
|
|
|
4
4
|
//#region src/constants.d.ts
|
|
5
5
|
/**
|
|
@@ -463,4 +463,4 @@ declare function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyAr
|
|
|
463
463
|
}): TNode;
|
|
464
464
|
//#endregion
|
|
465
465
|
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 };
|
|
466
|
-
//# sourceMappingURL=defineMacro-
|
|
466
|
+
//# sourceMappingURL=defineMacro-Bm07PZIF.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_visitor = require("./visitor-
|
|
1
|
+
const require_visitor = require("./visitor-ChiWLwlh.cjs");
|
|
2
2
|
//#region src/defineMacro.ts
|
|
3
3
|
/**
|
|
4
4
|
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
|
|
@@ -111,4 +111,4 @@ Object.defineProperty(exports, "defineMacro", {
|
|
|
111
111
|
}
|
|
112
112
|
});
|
|
113
113
|
|
|
114
|
-
//# sourceMappingURL=defineMacro-
|
|
114
|
+
//# sourceMappingURL=defineMacro-CI0a-SWY.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defineMacro-
|
|
1
|
+
{"version":3,"file":"defineMacro-CI0a-SWY.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 used to tell macros apart, for example `'simplify-union'`.\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,gBAAAA,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"}
|
|
@@ -9,7 +9,7 @@ import { n as __name } from "./rolldown-runtime-CNktS9qV.js";
|
|
|
9
9
|
* const kind: NodeKind = 'Schema'
|
|
10
10
|
* ```
|
|
11
11
|
*/
|
|
12
|
-
type NodeKind = 'Input' | 'Output' | 'Operation' | 'Schema' | 'Property' | 'Parameter' | 'Response' | 'RequestBody' | 'Content' | '
|
|
12
|
+
type NodeKind = 'Input' | 'Output' | 'Operation' | 'Schema' | 'Property' | 'Parameter' | 'Response' | 'RequestBody' | 'Content' | 'Type' | 'File' | 'Import' | 'Export' | 'Source' | 'Const' | 'Function' | 'ArrowFunction' | 'Text' | 'Break' | 'Jsx';
|
|
13
13
|
/**
|
|
14
14
|
* Base shape shared by all AST nodes.
|
|
15
15
|
*
|
|
@@ -1642,265 +1642,6 @@ type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind'
|
|
|
1642
1642
|
*/
|
|
1643
1643
|
declare function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta>;
|
|
1644
1644
|
//#endregion
|
|
1645
|
-
//#region src/nodes/function.d.ts
|
|
1646
|
-
/**
|
|
1647
|
-
* A language-agnostic type expression used as a function parameter type annotation.
|
|
1648
|
-
*
|
|
1649
|
-
* - a plain `string` is a type reference rendered as-is, e.g. `'string'`, `'QueryParams'`, `'Partial<Config>'`
|
|
1650
|
-
* - a {@link TypeLiteralNode} is an inline anonymous type, e.g. `{ petId: string; name?: string }`
|
|
1651
|
-
* - an {@link IndexedAccessTypeNode} is a single field accessed from a named type, e.g. `PathParams['petId']`
|
|
1652
|
-
*/
|
|
1653
|
-
type TypeExpression = string | TypeLiteralNode | IndexedAccessTypeNode;
|
|
1654
|
-
/**
|
|
1655
|
-
* AST node for an inline anonymous object type grouping named fields.
|
|
1656
|
-
* TypeScript renders as `{ key: Type; other?: OtherType }`.
|
|
1657
|
-
*
|
|
1658
|
-
* @example
|
|
1659
|
-
* ```ts
|
|
1660
|
-
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
|
|
1661
|
-
* // { petId: string }
|
|
1662
|
-
* ```
|
|
1663
|
-
*/
|
|
1664
|
-
type TypeLiteralNode = BaseNode & {
|
|
1665
|
-
kind: 'TypeLiteral';
|
|
1666
|
-
/**
|
|
1667
|
-
* Members of the object type, rendered in order.
|
|
1668
|
-
*/
|
|
1669
|
-
members: Array<{
|
|
1670
|
-
/**
|
|
1671
|
-
* Member key.
|
|
1672
|
-
*/
|
|
1673
|
-
name: string;
|
|
1674
|
-
/**
|
|
1675
|
-
* Member type expression.
|
|
1676
|
-
*/
|
|
1677
|
-
type: TypeExpression;
|
|
1678
|
-
/**
|
|
1679
|
-
* Whether the member is optional, rendered with `?`.
|
|
1680
|
-
*/
|
|
1681
|
-
optional?: boolean;
|
|
1682
|
-
}>;
|
|
1683
|
-
};
|
|
1684
|
-
/**
|
|
1685
|
-
* AST node for a single field accessed from a named group type.
|
|
1686
|
-
* TypeScript renders as `target['key']`.
|
|
1687
|
-
*
|
|
1688
|
-
* @example
|
|
1689
|
-
* ```ts
|
|
1690
|
-
* createIndexedAccessType({ target: 'GetPetPathParams', key: 'petId' })
|
|
1691
|
-
* // GetPetPathParams['petId']
|
|
1692
|
-
* ```
|
|
1693
|
-
*/
|
|
1694
|
-
type IndexedAccessTypeNode = BaseNode & {
|
|
1695
|
-
kind: 'IndexedAccessType';
|
|
1696
|
-
/**
|
|
1697
|
-
* Name of the type being indexed, e.g. `'GetPetPathParams'`.
|
|
1698
|
-
*/
|
|
1699
|
-
target: string;
|
|
1700
|
-
/**
|
|
1701
|
-
* Field key to access, e.g. `'petId'`.
|
|
1702
|
-
*/
|
|
1703
|
-
key: string;
|
|
1704
|
-
};
|
|
1705
|
-
/**
|
|
1706
|
-
* AST node for an object destructuring binding, used as the name of a grouped function parameter.
|
|
1707
|
-
* TypeScript renders as `{ id, name }` or `{ id: renamed }` when `propertyName` differs.
|
|
1708
|
-
*
|
|
1709
|
-
* @example
|
|
1710
|
-
* ```ts
|
|
1711
|
-
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
|
|
1712
|
-
* // { id, name }
|
|
1713
|
-
* ```
|
|
1714
|
-
*/
|
|
1715
|
-
type ObjectBindingPatternNode = BaseNode & {
|
|
1716
|
-
kind: 'ObjectBindingPattern';
|
|
1717
|
-
/**
|
|
1718
|
-
* Bound elements, rendered in order.
|
|
1719
|
-
*/
|
|
1720
|
-
elements: Array<{
|
|
1721
|
-
/**
|
|
1722
|
-
* Local binding name.
|
|
1723
|
-
*/
|
|
1724
|
-
name: string;
|
|
1725
|
-
/**
|
|
1726
|
-
* Source key when it differs from the binding name, rendered as `propertyName: name`.
|
|
1727
|
-
*/
|
|
1728
|
-
propertyName?: string;
|
|
1729
|
-
}>;
|
|
1730
|
-
};
|
|
1731
|
-
/**
|
|
1732
|
-
* AST node for one function parameter.
|
|
1733
|
-
*
|
|
1734
|
-
* A simple parameter has a `string` name. A destructured group has an
|
|
1735
|
-
* {@link ObjectBindingPatternNode} name paired with a {@link TypeLiteralNode} type.
|
|
1736
|
-
*
|
|
1737
|
-
* @example Required parameter
|
|
1738
|
-
* `name: Type`
|
|
1739
|
-
*
|
|
1740
|
-
* @example Optional parameter
|
|
1741
|
-
* `name?: Type`
|
|
1742
|
-
*
|
|
1743
|
-
* @example Parameter with default value
|
|
1744
|
-
* `name: Type = defaultValue`
|
|
1745
|
-
*
|
|
1746
|
-
* @example Rest parameter
|
|
1747
|
-
* `...name: Type[]`
|
|
1748
|
-
*
|
|
1749
|
-
* @example Destructured group
|
|
1750
|
-
* `{ id, name? }: { id: string; name?: string } = {}`
|
|
1751
|
-
*/
|
|
1752
|
-
type FunctionParameterNode = BaseNode & {
|
|
1753
|
-
kind: 'FunctionParameter';
|
|
1754
|
-
/**
|
|
1755
|
-
* Parameter name, or an {@link ObjectBindingPatternNode} for a destructured group.
|
|
1756
|
-
*/
|
|
1757
|
-
name: string | ObjectBindingPatternNode;
|
|
1758
|
-
/**
|
|
1759
|
-
* Type annotation as a {@link TypeExpression}. Omit for untyped output.
|
|
1760
|
-
*/
|
|
1761
|
-
type?: TypeExpression;
|
|
1762
|
-
/**
|
|
1763
|
-
* Whether the parameter is optional, rendered with `?`.
|
|
1764
|
-
*/
|
|
1765
|
-
optional?: boolean;
|
|
1766
|
-
/**
|
|
1767
|
-
* Default value, written verbatim after `=`. Commonly `'{}'` for a destructured group.
|
|
1768
|
-
*/
|
|
1769
|
-
default?: string;
|
|
1770
|
-
/**
|
|
1771
|
-
* When `true` the parameter is emitted as a rest parameter, e.g. `...name: Type[]`.
|
|
1772
|
-
*/
|
|
1773
|
-
rest?: boolean;
|
|
1774
|
-
};
|
|
1775
|
-
/**
|
|
1776
|
-
* AST node for a complete function parameter list.
|
|
1777
|
-
*
|
|
1778
|
-
* Printers are responsible for sorting (`required` → `optional` → `defaulted`).
|
|
1779
|
-
* Nodes are plain immutable data.
|
|
1780
|
-
*
|
|
1781
|
-
* Renders differently depending on the output mode:
|
|
1782
|
-
* - `declaration` → `(id: string, config: Config = {})` function declaration parameters
|
|
1783
|
-
* - `call` → `(id, { method, url })` function call arguments
|
|
1784
|
-
*/
|
|
1785
|
-
type FunctionParametersNode = BaseNode & {
|
|
1786
|
-
kind: 'FunctionParameters';
|
|
1787
|
-
/**
|
|
1788
|
-
* Ordered parameter nodes.
|
|
1789
|
-
*/
|
|
1790
|
-
params: ReadonlyArray<FunctionParameterNode>;
|
|
1791
|
-
};
|
|
1792
|
-
/**
|
|
1793
|
-
* Union of all function-parameter AST node variants used by the function-parameter printer.
|
|
1794
|
-
*/
|
|
1795
|
-
type FunctionParamNode = FunctionParameterNode | FunctionParametersNode | TypeLiteralNode | IndexedAccessTypeNode | ObjectBindingPatternNode;
|
|
1796
|
-
/**
|
|
1797
|
-
* Handler-map keys for the function-parameter printer, one per {@link FunctionParamNode} kind.
|
|
1798
|
-
*/
|
|
1799
|
-
type FunctionParamKind = FunctionParamNode['kind'];
|
|
1800
|
-
/**
|
|
1801
|
-
* Definition for the {@link TypeLiteralNode}.
|
|
1802
|
-
*/
|
|
1803
|
-
declare const typeLiteralDef: NodeDef<TypeLiteralNode, Pick<TypeLiteralNode, "members">>;
|
|
1804
|
-
/**
|
|
1805
|
-
* Definition for the {@link IndexedAccessTypeNode}.
|
|
1806
|
-
*/
|
|
1807
|
-
declare const indexedAccessTypeDef: NodeDef<IndexedAccessTypeNode, Omit<IndexedAccessTypeNode, "kind">>;
|
|
1808
|
-
/**
|
|
1809
|
-
* Definition for the {@link ObjectBindingPatternNode}.
|
|
1810
|
-
*/
|
|
1811
|
-
declare const objectBindingPatternDef: NodeDef<ObjectBindingPatternNode, Pick<ObjectBindingPatternNode, "elements">>;
|
|
1812
|
-
/**
|
|
1813
|
-
* Plain property descriptor for a destructured group built by {@link createFunctionParameter}.
|
|
1814
|
-
*/
|
|
1815
|
-
type FunctionParameterProperty = {
|
|
1816
|
-
name: string;
|
|
1817
|
-
type: TypeExpression;
|
|
1818
|
-
optional?: boolean;
|
|
1819
|
-
};
|
|
1820
|
-
type FunctionParameterInput = {
|
|
1821
|
-
name: string | ObjectBindingPatternNode;
|
|
1822
|
-
type?: TypeExpression;
|
|
1823
|
-
optional?: boolean;
|
|
1824
|
-
default?: string;
|
|
1825
|
-
rest?: boolean;
|
|
1826
|
-
} | {
|
|
1827
|
-
properties: Array<FunctionParameterProperty>;
|
|
1828
|
-
optional?: boolean;
|
|
1829
|
-
default?: string;
|
|
1830
|
-
};
|
|
1831
|
-
/**
|
|
1832
|
-
* Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`.
|
|
1833
|
-
* Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name
|
|
1834
|
-
* paired with a {@link TypeLiteralNode} type.
|
|
1835
|
-
*/
|
|
1836
|
-
declare const functionParameterDef: NodeDef<FunctionParameterNode, FunctionParameterInput>;
|
|
1837
|
-
/**
|
|
1838
|
-
* Definition for the {@link FunctionParametersNode}.
|
|
1839
|
-
*/
|
|
1840
|
-
declare const functionParametersDef: NodeDef<FunctionParametersNode, Partial<Omit<FunctionParametersNode, "kind">>>;
|
|
1841
|
-
/**
|
|
1842
|
-
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
|
|
1843
|
-
*
|
|
1844
|
-
* @example
|
|
1845
|
-
* ```ts
|
|
1846
|
-
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
|
|
1847
|
-
* // { petId: string }
|
|
1848
|
-
* ```
|
|
1849
|
-
*/
|
|
1850
|
-
declare const createTypeLiteral: (input: Pick<TypeLiteralNode, "members">) => TypeLiteralNode;
|
|
1851
|
-
/**
|
|
1852
|
-
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
|
|
1853
|
-
*
|
|
1854
|
-
* @example
|
|
1855
|
-
* ```ts
|
|
1856
|
-
* createIndexedAccessType({ target: 'DeletePetPathParams', key: 'petId' })
|
|
1857
|
-
* // DeletePetPathParams['petId']
|
|
1858
|
-
* ```
|
|
1859
|
-
*/
|
|
1860
|
-
declare const createIndexedAccessType: (input: Omit<IndexedAccessTypeNode, "kind">) => IndexedAccessTypeNode;
|
|
1861
|
-
/**
|
|
1862
|
-
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
|
|
1863
|
-
*
|
|
1864
|
-
* @example
|
|
1865
|
-
* ```ts
|
|
1866
|
-
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
|
|
1867
|
-
* // { id, name }
|
|
1868
|
-
* ```
|
|
1869
|
-
*/
|
|
1870
|
-
declare const createObjectBindingPattern: (input: Pick<ObjectBindingPatternNode, "elements">) => ObjectBindingPatternNode;
|
|
1871
|
-
/**
|
|
1872
|
-
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.
|
|
1873
|
-
*
|
|
1874
|
-
* @example Optional param
|
|
1875
|
-
* ```ts
|
|
1876
|
-
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
|
|
1877
|
-
* // → params?: QueryParams
|
|
1878
|
-
* ```
|
|
1879
|
-
*
|
|
1880
|
-
* @example Destructured group
|
|
1881
|
-
* ```ts
|
|
1882
|
-
* createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' })
|
|
1883
|
-
* // → { id, name }: { id: string; name?: string } = {}
|
|
1884
|
-
* ```
|
|
1885
|
-
*
|
|
1886
|
-
* @example Destructured group typed from a single reference
|
|
1887
|
-
* ```ts
|
|
1888
|
-
* createFunctionParameter({ name: createObjectBindingPattern({ elements: [{ name: 'path' }] }), type: "Omit<Config, 'url'>", default: '{}' })
|
|
1889
|
-
* // → { path }: Omit<Config, 'url'> = {}
|
|
1890
|
-
* ```
|
|
1891
|
-
*/
|
|
1892
|
-
declare const createFunctionParameter: (input: FunctionParameterInput) => FunctionParameterNode;
|
|
1893
|
-
/**
|
|
1894
|
-
* Creates a `FunctionParametersNode` from an ordered list of parameters.
|
|
1895
|
-
*
|
|
1896
|
-
* @example
|
|
1897
|
-
* ```ts
|
|
1898
|
-
* const empty = createFunctionParameters()
|
|
1899
|
-
* // { kind: 'FunctionParameters', params: [] }
|
|
1900
|
-
* ```
|
|
1901
|
-
*/
|
|
1902
|
-
declare function createFunctionParameters(props?: Partial<Omit<FunctionParametersNode, 'kind'>>): FunctionParametersNode;
|
|
1903
|
-
//#endregion
|
|
1904
1645
|
//#region ../../internals/utils/src/promise.d.ts
|
|
1905
1646
|
/**
|
|
1906
1647
|
* Container that switches between an eager `Array<T>` and a lazy `AsyncIterable<T>`.
|
|
@@ -2422,7 +2163,7 @@ declare function createOutput(overrides?: Partial<Omit<OutputNode, 'kind'>>): Ou
|
|
|
2422
2163
|
* }
|
|
2423
2164
|
* ```
|
|
2424
2165
|
*/
|
|
2425
|
-
type Node = InputNode | OutputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode | RequestBodyNode | ContentNode |
|
|
2166
|
+
type Node = InputNode | OutputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode | RequestBodyNode | ContentNode | FileNode | ImportNode | ExportNode | SourceNode | ConstNode | TypeNode | FunctionNode | ArrowFunctionNode;
|
|
2426
2167
|
//#endregion
|
|
2427
|
-
export {
|
|
2428
|
-
//# sourceMappingURL=index-
|
|
2168
|
+
export { SchemaNode as $, UserFileNode as A, createJsx as At, contentDef as B, BaseNode as Bt, ParameterNode as C, arrowFunctionDef as Ct, FileNode as D, createBreak as Dt, ExportNode as E, createArrowFunction as Et, exportDef as F, textDef as Ft, EnumSchemaNode as G, ArraySchemaNode as H, fileDef as I, typeDef as It, ObjectSchemaNode as J, IntersectionSchemaNode as K, importDef as L, DistributiveOmit as Lt, createFile as M, createType as Mt, createImport as N, functionDef as Nt, ImportNode as O, createConst as Ot, createSource as P, jsxDef as Pt, ScalarSchemaType as Q, sourceDef as R, NodeDef as Rt, ParameterLocation as S, TypeNode as St, parameterDef as T, constDef as Tt, DateSchemaNode as U, createContent as V, NodeKind as Vt, DatetimeSchemaNode as W, RefSchemaNode as X, PrimitiveSchemaType as Y, ScalarSchemaNode as Z, createResponse as _, ConstNode as _t, InputMeta as a, UrlSchemaNode as at, createRequestBody as b, JsxNode as bt, inputDef as c, PropertyNode as ct, HttpOperationNode as d, propertyDef as dt, SchemaNodeByType as et, OperationNode as f, InferSchemaNode as ft, StatusCode as g, CodeNode as gt, ResponseNode as h, BreakNode as ht, outputDef as i, UnionSchemaNode as it, createExport as j, createText as jt, SourceNode as k, createFunction as kt, GenericOperationNode as l, UserPropertyNode as lt, operationDef as m, ArrowFunctionNode as mt, OutputNode as n, StringSchemaNode as nt, InputNode as o, createSchema as ot, createOperation as p, ParserOptions as pt, NumberSchemaNode as q, createOutput as r, TimeSchemaNode as rt, createInput as s, schemaDef as st, Node as t, SchemaType as tt, HttpMethod as u, createProperty as ut, responseDef as v, FunctionNode as vt, createParameter as w, breakDef as wt, requestBodyDef as x, TextNode as xt, RequestBodyNode as y, JSDocNode as yt, ContentNode as z, defineNode as zt };
|
|
2169
|
+
//# sourceMappingURL=index-aTdOZp_L.d.ts.map
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_visitor = require("./visitor-
|
|
3
|
-
const require_defineMacro = require("./defineMacro-
|
|
2
|
+
const require_visitor = require("./visitor-ChiWLwlh.cjs");
|
|
3
|
+
const require_defineMacro = require("./defineMacro-CI0a-SWY.cjs");
|
|
4
4
|
//#region src/defineDialect.ts
|
|
5
5
|
/**
|
|
6
6
|
* Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the
|
|
@@ -93,13 +93,9 @@ var factory_exports = /* @__PURE__ */ require_visitor.__exportAll({
|
|
|
93
93
|
createExport: () => require_visitor.createExport,
|
|
94
94
|
createFile: () => require_visitor.createFile,
|
|
95
95
|
createFunction: () => require_visitor.createFunction,
|
|
96
|
-
createFunctionParameter: () => require_visitor.createFunctionParameter,
|
|
97
|
-
createFunctionParameters: () => require_visitor.createFunctionParameters,
|
|
98
96
|
createImport: () => require_visitor.createImport,
|
|
99
|
-
createIndexedAccessType: () => require_visitor.createIndexedAccessType,
|
|
100
97
|
createInput: () => require_visitor.createInput,
|
|
101
98
|
createJsx: () => require_visitor.createJsx,
|
|
102
|
-
createObjectBindingPattern: () => require_visitor.createObjectBindingPattern,
|
|
103
99
|
createOperation: () => require_visitor.createOperation,
|
|
104
100
|
createOutput: () => require_visitor.createOutput,
|
|
105
101
|
createParameter: () => require_visitor.createParameter,
|
|
@@ -110,7 +106,6 @@ var factory_exports = /* @__PURE__ */ require_visitor.__exportAll({
|
|
|
110
106
|
createSource: () => require_visitor.createSource,
|
|
111
107
|
createText: () => require_visitor.createText,
|
|
112
108
|
createType: () => require_visitor.createType,
|
|
113
|
-
createTypeLiteral: () => require_visitor.createTypeLiteral,
|
|
114
109
|
update: () => update
|
|
115
110
|
});
|
|
116
111
|
/**
|
|
@@ -154,16 +149,12 @@ var exports_exports = /* @__PURE__ */ require_visitor.__exportAll({
|
|
|
154
149
|
factory: () => factory_exports,
|
|
155
150
|
fileDef: () => require_visitor.fileDef,
|
|
156
151
|
functionDef: () => require_visitor.functionDef,
|
|
157
|
-
functionParameterDef: () => require_visitor.functionParameterDef,
|
|
158
|
-
functionParametersDef: () => require_visitor.functionParametersDef,
|
|
159
152
|
importDef: () => require_visitor.importDef,
|
|
160
|
-
indexedAccessTypeDef: () => require_visitor.indexedAccessTypeDef,
|
|
161
153
|
inputDef: () => require_visitor.inputDef,
|
|
162
154
|
isHttpOperationNode: () => require_visitor.isHttpOperationNode,
|
|
163
155
|
jsxDef: () => require_visitor.jsxDef,
|
|
164
156
|
narrowSchema: () => require_visitor.narrowSchema,
|
|
165
157
|
nodeDefs: () => require_visitor.nodeDefs,
|
|
166
|
-
objectBindingPatternDef: () => require_visitor.objectBindingPatternDef,
|
|
167
158
|
operationDef: () => require_visitor.operationDef,
|
|
168
159
|
optionality: () => require_visitor.optionality,
|
|
169
160
|
outputDef: () => require_visitor.outputDef,
|
|
@@ -177,7 +168,6 @@ var exports_exports = /* @__PURE__ */ require_visitor.__exportAll({
|
|
|
177
168
|
textDef: () => require_visitor.textDef,
|
|
178
169
|
transform: () => require_visitor.transform,
|
|
179
170
|
typeDef: () => require_visitor.typeDef,
|
|
180
|
-
typeLiteralDef: () => require_visitor.typeLiteralDef,
|
|
181
171
|
walk: () => require_visitor.walk
|
|
182
172
|
});
|
|
183
173
|
//#endregion
|
|
@@ -207,16 +197,12 @@ Object.defineProperty(exports, "factory", {
|
|
|
207
197
|
});
|
|
208
198
|
exports.fileDef = require_visitor.fileDef;
|
|
209
199
|
exports.functionDef = require_visitor.functionDef;
|
|
210
|
-
exports.functionParameterDef = require_visitor.functionParameterDef;
|
|
211
|
-
exports.functionParametersDef = require_visitor.functionParametersDef;
|
|
212
200
|
exports.importDef = require_visitor.importDef;
|
|
213
|
-
exports.indexedAccessTypeDef = require_visitor.indexedAccessTypeDef;
|
|
214
201
|
exports.inputDef = require_visitor.inputDef;
|
|
215
202
|
exports.isHttpOperationNode = require_visitor.isHttpOperationNode;
|
|
216
203
|
exports.jsxDef = require_visitor.jsxDef;
|
|
217
204
|
exports.narrowSchema = require_visitor.narrowSchema;
|
|
218
205
|
exports.nodeDefs = require_visitor.nodeDefs;
|
|
219
|
-
exports.objectBindingPatternDef = require_visitor.objectBindingPatternDef;
|
|
220
206
|
exports.operationDef = require_visitor.operationDef;
|
|
221
207
|
exports.optionality = require_visitor.optionality;
|
|
222
208
|
exports.outputDef = require_visitor.outputDef;
|
|
@@ -230,7 +216,6 @@ exports.sourceDef = require_visitor.sourceDef;
|
|
|
230
216
|
exports.textDef = require_visitor.textDef;
|
|
231
217
|
exports.transform = require_visitor.transform;
|
|
232
218
|
exports.typeDef = require_visitor.typeDef;
|
|
233
|
-
exports.typeLiteralDef = require_visitor.typeLiteralDef;
|
|
234
219
|
exports.walk = require_visitor.walk;
|
|
235
220
|
|
|
236
221
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"sources":["../src/defineDialect.ts","../src/createPrinter.ts","../src/factory.ts","../src/exports.ts"],"sourcesContent":["/**\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\n/**\n * A spec adapter's dialect. `name` identifies it in logs and diagnostics, and `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 * },\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 * Creates 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 { createPrinter, type PrinterFactoryOptions } from '@kubb/ast'\n *\n * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>\n *\n * export const zodPrinter = createPrinter<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 createPrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {\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: SchemaNode): T['output'] | null => {\n const handler = nodes[node.type]\n if (!handler) return null\n\n return (handler as (this: typeof context, node: SchemaNode) => 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: SchemaNode) => T['printOutput'] | null,\n }\n }\n}\n","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 does not allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is shallow,\n * so a structurally equal but newly allocated array or 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","export { schemaTypes } from './constants.ts'\nexport { defineDialect } from './defineDialect.ts'\nexport { isHttpOperationNode, narrowSchema } from './guards.ts'\nexport { applyMacros, composeMacros, defineMacro } from './defineMacro.ts'\nexport { defineNode } from './defineNode.ts'\nexport { optionality } from './optionality.ts'\nexport { createPrinter } from './createPrinter.ts'\nexport { collect, transform, walk } from './visitor.ts'\n\nexport * as factory from './factory.ts'\nexport * from './registry.ts'\nexport type * from './types.ts'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA6DA,SAAgB,cACd,SACmD;CACnD,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC2HA,SAAgB,cAAuE,OAAkE;CACvJ,QAAQ,YAAY;EAClB,MAAM,EAAE,MAAM,SAAS,iBAAiB,OAAO,OAAO,kBAAkB,MAAM,WAAY,CAAC,CAAkB;EAE7G,MAAM,UAAU;GACd,SAAS;GACT,YAAY,SAAyC;IACnD,MAAM,UAAU,MAAM,KAAK;IAC3B,IAAI,CAAC,SAAS,OAAO;IAErB,OAAQ,QAA2E,KAAK,SAAS,IAAI;GACvG;EACF;EAEA,OAAO;GACL;GACA,SAAS;GACT,WAAW,QAAQ;GACnB,OAAQ,gBAAgB,cAAc,KAAK,OAAO,IAAI,QAAQ;EAChE;CACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC9KA,SAAgB,OAAuB,MAAS,SAAwB;CACtE,KAAK,MAAM,OAAO,SAChB,IAAI,QAAQ,SAAS,KAAK,MACxB,OAAO;EAAE,GAAG;EAAM,GAAG;CAAQ;CAIjC,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/defineDialect.ts","../src/createPrinter.ts","../src/factory.ts","../src/exports.ts"],"sourcesContent":["/**\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\n/**\n * A spec adapter's dialect. `name` identifies it in logs and diagnostics, and `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 * },\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 * Creates 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 { createPrinter, type PrinterFactoryOptions } from '@kubb/ast'\n *\n * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>\n *\n * export const zodPrinter = createPrinter<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 createPrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {\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: SchemaNode): T['output'] | null => {\n const handler = nodes[node.type]\n if (!handler) return null\n\n return (handler as (this: typeof context, node: SchemaNode) => 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: SchemaNode) => T['printOutput'] | null,\n }\n }\n}\n","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 { 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 does not allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is shallow,\n * so a structurally equal but newly allocated array or 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","export { schemaTypes } from './constants.ts'\nexport { defineDialect } from './defineDialect.ts'\nexport { isHttpOperationNode, narrowSchema } from './guards.ts'\nexport { applyMacros, composeMacros, defineMacro } from './defineMacro.ts'\nexport { defineNode } from './defineNode.ts'\nexport { optionality } from './optionality.ts'\nexport { createPrinter } from './createPrinter.ts'\nexport { collect, transform, walk } from './visitor.ts'\n\nexport * as factory from './factory.ts'\nexport * from './registry.ts'\nexport type * from './types.ts'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA6DA,SAAgB,cACd,SACmD;CACnD,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC2HA,SAAgB,cAAuE,OAAkE;CACvJ,QAAQ,YAAY;EAClB,MAAM,EAAE,MAAM,SAAS,iBAAiB,OAAO,OAAO,kBAAkB,MAAM,WAAY,CAAC,CAAkB;EAE7G,MAAM,UAAU;GACd,SAAS;GACT,YAAY,SAAyC;IACnD,MAAM,UAAU,MAAM,KAAK;IAC3B,IAAI,CAAC,SAAS,OAAO;IAErB,OAAQ,QAA2E,KAAK,SAAS,IAAI;GACvG;EACF;EAEA,OAAO;GACL;GACA,SAAS;GACT,WAAW,QAAQ;GACnB,OAAQ,gBAAgB,cAAc,KAAK,OAAO,IAAI,QAAQ;EAChE;CACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC/KA,SAAgB,OAAuB,MAAS,SAAwB;CACtE,KAAK,MAAM,OAAO,SAChB,IAAI,QAAQ,SAAS,KAAK,MACxB,OAAO;EAAE,GAAG;EAAM,GAAG;CAAQ;CAIjC,OAAO;AACT"}
|