@kubb/ast 5.0.0-beta.6 → 5.0.0-beta.60
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/LICENSE +17 -10
- package/README.md +50 -27
- package/dist/chunk-CNktS9qV.js +17 -0
- package/dist/extractStringsFromNodes-WMYJ8nQL.d.ts +82 -0
- package/dist/factory-Cl8Z7mcc.cjs +299 -0
- package/dist/factory-Cl8Z7mcc.cjs.map +1 -0
- package/dist/factory-Du7nEP4B.js +282 -0
- package/dist/factory-Du7nEP4B.js.map +1 -0
- package/dist/factory.cjs +29 -0
- package/dist/factory.d.ts +62 -0
- package/dist/factory.js +3 -0
- package/dist/index-BzjwdK2M.d.ts +2433 -0
- package/dist/index.cjs +442 -2180
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +93 -3408
- package/dist/index.js +392 -2101
- package/dist/index.js.map +1 -1
- package/dist/operationParams-BZ07xDm0.d.ts +204 -0
- package/dist/response-DKxTr522.js +683 -0
- package/dist/response-DKxTr522.js.map +1 -0
- package/dist/response-DS5S3IG4.cjs +1058 -0
- package/dist/response-DS5S3IG4.cjs.map +1 -0
- package/dist/types-olVl9v5p.d.ts +764 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +1 -0
- package/dist/utils-D83JA6Xx.cjs +1645 -0
- package/dist/utils-D83JA6Xx.cjs.map +1 -0
- package/dist/utils-Dj_KoXMv.js +1389 -0
- package/dist/utils-Dj_KoXMv.js.map +1 -0
- package/dist/utils.cjs +34 -0
- package/dist/utils.d.ts +332 -0
- package/dist/utils.js +3 -0
- package/package.json +17 -6
- package/src/constants.ts +19 -64
- package/src/dedupe.ts +239 -0
- package/src/dialect.ts +53 -0
- package/src/factory.ts +67 -678
- package/src/guards.ts +10 -92
- package/src/index.ts +16 -43
- package/src/infer.ts +16 -14
- package/src/mocks.ts +7 -127
- package/src/node.ts +128 -0
- package/src/nodes/base.ts +5 -12
- package/src/nodes/code.ts +165 -74
- package/src/nodes/content.ts +56 -0
- package/src/nodes/file.ts +97 -36
- package/src/nodes/function.ts +216 -156
- package/src/nodes/http.ts +1 -35
- package/src/nodes/index.ts +23 -15
- package/src/nodes/input.ts +140 -0
- package/src/nodes/operation.ts +122 -68
- package/src/nodes/output.ts +23 -0
- package/src/nodes/parameter.ts +33 -3
- package/src/nodes/property.ts +36 -3
- package/src/nodes/requestBody.ts +61 -0
- package/src/nodes/response.ts +58 -13
- package/src/nodes/schema.ts +93 -17
- package/src/printer.ts +48 -42
- package/src/registry.ts +75 -0
- package/src/signature.ts +207 -0
- package/src/transformers.ts +50 -18
- package/src/types.ts +7 -68
- package/src/utils/codegen.ts +104 -0
- package/src/utils/extractStringsFromNodes.ts +34 -0
- package/src/utils/fileMerge.ts +184 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/operationParams.ts +353 -0
- package/src/utils/refs.ts +112 -0
- package/src/utils/schemaGraph.ts +169 -0
- package/src/utils/schemaTraversal.ts +86 -0
- package/src/utils/strings.ts +139 -0
- package/src/visitor.ts +227 -289
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/nodes/root.ts +0 -64
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/utils.ts +0 -915
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
import { n as __name } from "./chunk-CNktS9qV.js";
|
|
2
|
+
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, vt as SchemaNodeByType, w as ParameterNode, y as RequestBodyNode, yt as SchemaType } from "./index-BzjwdK2M.js";
|
|
3
|
+
|
|
4
|
+
//#region src/constants.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Traversal depth for AST visitor utilities.
|
|
7
|
+
*
|
|
8
|
+
* - `'shallow'` visits only the immediate node, skipping children.
|
|
9
|
+
* - `'deep'` recursively visits all descendant nodes.
|
|
10
|
+
*/
|
|
11
|
+
type VisitorDepth = 'shallow' | 'deep';
|
|
12
|
+
/**
|
|
13
|
+
* Schema type discriminators used by all AST schema nodes.
|
|
14
|
+
*
|
|
15
|
+
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
|
|
16
|
+
* Call `isScalarPrimitive()` to check for the scalar types.
|
|
17
|
+
*/
|
|
18
|
+
declare const schemaTypes: {
|
|
19
|
+
/**
|
|
20
|
+
* Text value.
|
|
21
|
+
*/
|
|
22
|
+
readonly string: "string";
|
|
23
|
+
/**
|
|
24
|
+
* Floating-point number (`float`, `double`).
|
|
25
|
+
*/
|
|
26
|
+
readonly number: "number";
|
|
27
|
+
/**
|
|
28
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
29
|
+
*/
|
|
30
|
+
readonly integer: "integer";
|
|
31
|
+
/**
|
|
32
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
33
|
+
*/
|
|
34
|
+
readonly bigint: "bigint";
|
|
35
|
+
/**
|
|
36
|
+
* Boolean value.
|
|
37
|
+
*/
|
|
38
|
+
readonly boolean: "boolean";
|
|
39
|
+
/**
|
|
40
|
+
* Explicit null value.
|
|
41
|
+
*/
|
|
42
|
+
readonly null: "null";
|
|
43
|
+
/**
|
|
44
|
+
* Any value (no type restriction).
|
|
45
|
+
*/
|
|
46
|
+
readonly any: "any";
|
|
47
|
+
/**
|
|
48
|
+
* Unknown value (must be narrowed before usage).
|
|
49
|
+
*/
|
|
50
|
+
readonly unknown: "unknown";
|
|
51
|
+
/**
|
|
52
|
+
* No return value (`void`).
|
|
53
|
+
*/
|
|
54
|
+
readonly void: "void";
|
|
55
|
+
/**
|
|
56
|
+
* Object with named properties.
|
|
57
|
+
*/
|
|
58
|
+
readonly object: "object";
|
|
59
|
+
/**
|
|
60
|
+
* Sequential list of items.
|
|
61
|
+
*/
|
|
62
|
+
readonly array: "array";
|
|
63
|
+
/**
|
|
64
|
+
* Fixed-length list with position-specific items.
|
|
65
|
+
*/
|
|
66
|
+
readonly tuple: "tuple";
|
|
67
|
+
/**
|
|
68
|
+
* "One of" multiple schema members.
|
|
69
|
+
*/
|
|
70
|
+
readonly union: "union";
|
|
71
|
+
/**
|
|
72
|
+
* "All of" multiple schema members.
|
|
73
|
+
*/
|
|
74
|
+
readonly intersection: "intersection";
|
|
75
|
+
/**
|
|
76
|
+
* Enum schema.
|
|
77
|
+
*/
|
|
78
|
+
readonly enum: "enum";
|
|
79
|
+
/**
|
|
80
|
+
* Reference to another schema.
|
|
81
|
+
*/
|
|
82
|
+
readonly ref: "ref";
|
|
83
|
+
/**
|
|
84
|
+
* Calendar date (for example `2026-03-24`).
|
|
85
|
+
*/
|
|
86
|
+
readonly date: "date";
|
|
87
|
+
/**
|
|
88
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
89
|
+
*/
|
|
90
|
+
readonly datetime: "datetime";
|
|
91
|
+
/**
|
|
92
|
+
* Time-only value (for example `09:00:00`).
|
|
93
|
+
*/
|
|
94
|
+
readonly time: "time";
|
|
95
|
+
/**
|
|
96
|
+
* UUID value.
|
|
97
|
+
*/
|
|
98
|
+
readonly uuid: "uuid";
|
|
99
|
+
/**
|
|
100
|
+
* Email address value.
|
|
101
|
+
*/
|
|
102
|
+
readonly email: "email";
|
|
103
|
+
/**
|
|
104
|
+
* URL value.
|
|
105
|
+
*/
|
|
106
|
+
readonly url: "url";
|
|
107
|
+
/**
|
|
108
|
+
* IPv4 address value.
|
|
109
|
+
*/
|
|
110
|
+
readonly ipv4: "ipv4";
|
|
111
|
+
/**
|
|
112
|
+
* IPv6 address value.
|
|
113
|
+
*/
|
|
114
|
+
readonly ipv6: "ipv6";
|
|
115
|
+
/**
|
|
116
|
+
* Binary/blob value.
|
|
117
|
+
*/
|
|
118
|
+
readonly blob: "blob";
|
|
119
|
+
/**
|
|
120
|
+
* Impossible value (`never`).
|
|
121
|
+
*/
|
|
122
|
+
readonly never: "never";
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* HTTP method identifiers used by operation nodes.
|
|
126
|
+
*/
|
|
127
|
+
declare const httpMethods: {
|
|
128
|
+
readonly get: "GET";
|
|
129
|
+
readonly post: "POST";
|
|
130
|
+
readonly put: "PUT";
|
|
131
|
+
readonly patch: "PATCH";
|
|
132
|
+
readonly delete: "DELETE";
|
|
133
|
+
readonly head: "HEAD";
|
|
134
|
+
readonly options: "OPTIONS";
|
|
135
|
+
readonly trace: "TRACE";
|
|
136
|
+
};
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/dedupe.d.ts
|
|
139
|
+
/**
|
|
140
|
+
* A canonical destination for a deduplicated shape: the shared schema name and
|
|
141
|
+
* the synthetic `$ref` path that points at it.
|
|
142
|
+
*/
|
|
143
|
+
type DedupeCanonical = {
|
|
144
|
+
/**
|
|
145
|
+
* Canonical schema name every duplicate occurrence refers to.
|
|
146
|
+
*/
|
|
147
|
+
name: string;
|
|
148
|
+
/**
|
|
149
|
+
* `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).
|
|
150
|
+
*/
|
|
151
|
+
ref: string;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* The result of {@link buildDedupePlan}: a lookup from structural signature to its
|
|
155
|
+
* canonical target, plus the freshly hoisted definitions that must be added to
|
|
156
|
+
* the schema list.
|
|
157
|
+
*/
|
|
158
|
+
type DedupePlan = {
|
|
159
|
+
/**
|
|
160
|
+
* Maps a structural signature to the canonical schema that represents it.
|
|
161
|
+
*/
|
|
162
|
+
canonicalBySignature: Map<string, DedupeCanonical>;
|
|
163
|
+
/**
|
|
164
|
+
* Maps the name of a top-level schema that duplicates a canonical one to that canonical, so
|
|
165
|
+
* references to the duplicate can be repointed at the first schema with the same content.
|
|
166
|
+
*/
|
|
167
|
+
aliasNames: Map<string, DedupeCanonical>;
|
|
168
|
+
/**
|
|
169
|
+
* New top-level schema definitions created for inline shapes that had no existing
|
|
170
|
+
* named component. Nested duplicates inside each definition are already collapsed.
|
|
171
|
+
*/
|
|
172
|
+
hoisted: Array<SchemaNode>;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* The lookups {@link applyDedupe} needs from a {@link DedupePlan}.
|
|
176
|
+
*/
|
|
177
|
+
type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>;
|
|
178
|
+
/**
|
|
179
|
+
* Options that inject the naming and candidate policy into {@link buildDedupePlan}.
|
|
180
|
+
* The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.
|
|
181
|
+
*/
|
|
182
|
+
type BuildDedupePlanOptions = {
|
|
183
|
+
/**
|
|
184
|
+
* Returns `true` when a node should be deduplicated. This is the only gate, so it must
|
|
185
|
+
* reject both ineligible kinds (return `false` for anything other than, say, enums and
|
|
186
|
+
* objects) and unsafe shapes (e.g. nodes that reference a circular schema).
|
|
187
|
+
*/
|
|
188
|
+
isCandidate: (node: SchemaNode) => boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Produces the canonical name for an inline shape with no existing named component.
|
|
191
|
+
* Return `null` to leave the shape inline (for example when no contextual name exists).
|
|
192
|
+
*/
|
|
193
|
+
nameFor: (node: SchemaNode, signature: string) => string | null;
|
|
194
|
+
/**
|
|
195
|
+
* Builds the `$ref` path for a canonical name.
|
|
196
|
+
*/
|
|
197
|
+
refFor: (name: string) => string;
|
|
198
|
+
/**
|
|
199
|
+
* Minimum number of occurrences before a shape is deduplicated.
|
|
200
|
+
*
|
|
201
|
+
* @default 2
|
|
202
|
+
*/
|
|
203
|
+
minOccurrences?: number;
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Rewrites a node, replacing every candidate sub-schema whose signature has a canonical
|
|
207
|
+
* target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,
|
|
208
|
+
* so nested duplicates inside a replaced shape are not visited again. A `ref` that points
|
|
209
|
+
* at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with
|
|
210
|
+
* the same content.
|
|
211
|
+
*
|
|
212
|
+
* Pass `skipRootMatch` when rewriting a canonical definition so its own root is not
|
|
213
|
+
* turned into a reference to itself. Nested duplicates are still collapsed.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```ts
|
|
217
|
+
* const next = applyDedupe(operationNode, plan)
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
declare function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode;
|
|
221
|
+
declare function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode;
|
|
222
|
+
/**
|
|
223
|
+
* Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
|
|
224
|
+
*
|
|
225
|
+
* A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
|
|
226
|
+
* is a named top-level schema, the first one becomes the canonical (so other top-level
|
|
227
|
+
* duplicates and inline copies turn into references to it). Every other top-level name with
|
|
228
|
+
* the same content is recorded in `aliasNames`, so refs to it can be repointed at the
|
|
229
|
+
* canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied
|
|
230
|
+
* per node with {@link applyDedupe}.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
|
|
235
|
+
* isCandidate: (node) => node.type === 'enum' || node.type === 'object',
|
|
236
|
+
* nameFor: (node) => node.name ?? null,
|
|
237
|
+
* refFor: (name) => `#/components/schemas/${name}`,
|
|
238
|
+
* })
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
declare function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan;
|
|
242
|
+
//#endregion
|
|
243
|
+
//#region src/dialect.d.ts
|
|
244
|
+
/**
|
|
245
|
+
* The spec-specific questions a schema parser answers while turning a source document into Kubb
|
|
246
|
+
* AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where
|
|
247
|
+
* OpenAPI, AsyncAPI, and plain JSON Schema differ.
|
|
248
|
+
*/
|
|
249
|
+
type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {
|
|
250
|
+
/**
|
|
251
|
+
* Identifies the dialect in logs and diagnostics.
|
|
252
|
+
*/
|
|
253
|
+
name: string;
|
|
254
|
+
/**
|
|
255
|
+
* Whether the schema is nullable.
|
|
256
|
+
*/
|
|
257
|
+
isNullable: (schema?: TSchema) => boolean;
|
|
258
|
+
/**
|
|
259
|
+
* Whether the value is a `$ref` pointer.
|
|
260
|
+
*/
|
|
261
|
+
isReference: (value?: unknown) => value is TRef;
|
|
262
|
+
/**
|
|
263
|
+
* Whether the schema carries a discriminator for polymorphism.
|
|
264
|
+
*/
|
|
265
|
+
isDiscriminator: (value?: unknown) => value is TDiscriminated;
|
|
266
|
+
/**
|
|
267
|
+
* Whether the schema is binary data, converted to a `blob` node.
|
|
268
|
+
*/
|
|
269
|
+
isBinary: (schema: TSchema) => boolean;
|
|
270
|
+
/**
|
|
271
|
+
* Resolves a local `$ref` against the document, or nullish when it cannot.
|
|
272
|
+
*/
|
|
273
|
+
resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined;
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the
|
|
277
|
+
* dialect's type for inference.
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```ts
|
|
281
|
+
* export const oasDialect = defineSchemaDialect({
|
|
282
|
+
* name: 'oas',
|
|
283
|
+
* isNullable,
|
|
284
|
+
* isReference,
|
|
285
|
+
* isDiscriminator,
|
|
286
|
+
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
287
|
+
* resolveRef,
|
|
288
|
+
* })
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
291
|
+
declare function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>;
|
|
292
|
+
//#endregion
|
|
293
|
+
//#region src/visitor.d.ts
|
|
294
|
+
/**
|
|
295
|
+
* Ordered mapping of `[NodeType, ParentType]` pairs.
|
|
296
|
+
*
|
|
297
|
+
* `ParentOf` uses this map to find parent types.
|
|
298
|
+
*/
|
|
299
|
+
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]];
|
|
300
|
+
/**
|
|
301
|
+
* Resolves the parent node type for a given AST node type.
|
|
302
|
+
*
|
|
303
|
+
* Visitor context relies on this so `ctx.parent` is typed for each callback.
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```ts
|
|
307
|
+
* type InputParent = ParentOf<InputNode>
|
|
308
|
+
* // undefined
|
|
309
|
+
* ```
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```ts
|
|
313
|
+
* type PropertyParent = ParentOf<PropertyNode>
|
|
314
|
+
* // SchemaNode
|
|
315
|
+
* ```
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* type SchemaParent = ParentOf<SchemaNode>
|
|
320
|
+
* // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
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;
|
|
324
|
+
/**
|
|
325
|
+
* Traversal context passed as the second argument to every visitor callback.
|
|
326
|
+
* `parent` is typed from the current node type.
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```ts
|
|
330
|
+
* const visitor: Visitor = {
|
|
331
|
+
* schema(node, { parent }) {
|
|
332
|
+
* // parent type is narrowed by node kind
|
|
333
|
+
* },
|
|
334
|
+
* }
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
type VisitorContext<T extends Node = Node> = {
|
|
338
|
+
/**
|
|
339
|
+
* Parent node of the currently visited node.
|
|
340
|
+
* For `InputNode`, this is `undefined`.
|
|
341
|
+
*/
|
|
342
|
+
parent?: ParentOf<T>;
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Synchronous visitor consumed by `transform`. Each optional callback runs
|
|
346
|
+
* for the matching node type. Return a new node to replace it, or `undefined`
|
|
347
|
+
* to leave it untouched.
|
|
348
|
+
*
|
|
349
|
+
* Plugins typically expose `transformer` so users can supply a `Visitor` that
|
|
350
|
+
* rewrites the AST before printing.
|
|
351
|
+
*
|
|
352
|
+
* @example Prefix every operationId
|
|
353
|
+
* ```ts
|
|
354
|
+
* const visitor: Visitor = {
|
|
355
|
+
* operation(node) {
|
|
356
|
+
* return { ...node, operationId: `api_${node.operationId}` }
|
|
357
|
+
* },
|
|
358
|
+
* }
|
|
359
|
+
* ```
|
|
360
|
+
*
|
|
361
|
+
* @example Strip schema descriptions
|
|
362
|
+
* ```ts
|
|
363
|
+
* const visitor: Visitor = {
|
|
364
|
+
* schema(node) {
|
|
365
|
+
* return { ...node, description: undefined }
|
|
366
|
+
* },
|
|
367
|
+
* }
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
type Visitor = {
|
|
371
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode;
|
|
372
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode;
|
|
373
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode;
|
|
374
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode;
|
|
375
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode;
|
|
376
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode;
|
|
377
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
|
|
378
|
+
};
|
|
379
|
+
/**
|
|
380
|
+
* A visitor callback result that may be sync or async.
|
|
381
|
+
*/
|
|
382
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
383
|
+
/**
|
|
384
|
+
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```ts
|
|
388
|
+
* const visitor: AsyncVisitor = {
|
|
389
|
+
* async operation(node) {
|
|
390
|
+
* await Promise.resolve(node.operationId)
|
|
391
|
+
* },
|
|
392
|
+
* }
|
|
393
|
+
* ```
|
|
394
|
+
*/
|
|
395
|
+
type AsyncVisitor = {
|
|
396
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>;
|
|
397
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>;
|
|
398
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>;
|
|
399
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>;
|
|
400
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>;
|
|
401
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>;
|
|
402
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>;
|
|
403
|
+
};
|
|
404
|
+
/**
|
|
405
|
+
* Visitor used by `collect`.
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
* ```ts
|
|
409
|
+
* const visitor: CollectVisitor<string> = {
|
|
410
|
+
* operation(node) {
|
|
411
|
+
* return node.operationId
|
|
412
|
+
* },
|
|
413
|
+
* }
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
type CollectVisitor<T> = {
|
|
417
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
|
|
418
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
|
|
419
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
|
|
420
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
|
|
421
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
|
|
422
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
|
|
423
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
|
|
424
|
+
};
|
|
425
|
+
/**
|
|
426
|
+
* Options for `transform`.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```ts
|
|
430
|
+
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
|
|
431
|
+
* ```
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```ts
|
|
435
|
+
* // Only transform the current node, not nested children
|
|
436
|
+
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
|
|
437
|
+
* ```
|
|
438
|
+
*/
|
|
439
|
+
type TransformOptions = Visitor & {
|
|
440
|
+
/**
|
|
441
|
+
* Traversal depth.
|
|
442
|
+
* @default 'deep'
|
|
443
|
+
*/
|
|
444
|
+
depth?: VisitorDepth;
|
|
445
|
+
/**
|
|
446
|
+
* Internal parent override used during recursion.
|
|
447
|
+
*/
|
|
448
|
+
parent?: Node;
|
|
449
|
+
};
|
|
450
|
+
/**
|
|
451
|
+
* Options for `walk`.
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* ```ts
|
|
455
|
+
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
|
|
456
|
+
* ```
|
|
457
|
+
*/
|
|
458
|
+
type WalkOptions = AsyncVisitor & {
|
|
459
|
+
/**
|
|
460
|
+
* Traversal depth.
|
|
461
|
+
* @default 'deep'
|
|
462
|
+
*/
|
|
463
|
+
depth?: VisitorDepth;
|
|
464
|
+
/**
|
|
465
|
+
* Maximum number of sibling nodes visited concurrently.
|
|
466
|
+
* @default 30
|
|
467
|
+
*/
|
|
468
|
+
concurrency?: number;
|
|
469
|
+
};
|
|
470
|
+
/**
|
|
471
|
+
* Options for `collect`.
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* ```ts
|
|
475
|
+
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
|
|
476
|
+
* ```
|
|
477
|
+
*/
|
|
478
|
+
type CollectOptions<T> = CollectVisitor<T> & {
|
|
479
|
+
/**
|
|
480
|
+
* Traversal depth.
|
|
481
|
+
* @default 'deep'
|
|
482
|
+
*/
|
|
483
|
+
depth?: VisitorDepth;
|
|
484
|
+
/**
|
|
485
|
+
* Internal parent override used during recursion.
|
|
486
|
+
*/
|
|
487
|
+
parent?: Node;
|
|
488
|
+
};
|
|
489
|
+
/**
|
|
490
|
+
* Async depth-first traversal for side effects. Visitor return values are
|
|
491
|
+
* ignored. Use `transform` when you want to rewrite nodes.
|
|
492
|
+
*
|
|
493
|
+
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
494
|
+
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
495
|
+
* work. Lower values reduce memory pressure.
|
|
496
|
+
*
|
|
497
|
+
* @example Log every operation
|
|
498
|
+
* ```ts
|
|
499
|
+
* await walk(root, {
|
|
500
|
+
* operation(node) {
|
|
501
|
+
* console.log(node.operationId)
|
|
502
|
+
* },
|
|
503
|
+
* })
|
|
504
|
+
* ```
|
|
505
|
+
*
|
|
506
|
+
* @example Only visit the root node
|
|
507
|
+
* ```ts
|
|
508
|
+
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
509
|
+
* ```
|
|
510
|
+
*/
|
|
511
|
+
declare function walk(node: Node, options: WalkOptions): Promise<void>;
|
|
512
|
+
/**
|
|
513
|
+
* Synchronous depth-first transform. Each visitor callback can return a
|
|
514
|
+
* replacement node. Returning `undefined` keeps the original.
|
|
515
|
+
*
|
|
516
|
+
* The original tree is never mutated, a new tree is returned. Pass
|
|
517
|
+
* `depth: 'shallow'` to skip recursion into children.
|
|
518
|
+
*
|
|
519
|
+
* @example Prefix every operationId
|
|
520
|
+
* ```ts
|
|
521
|
+
* const next = transform(root, {
|
|
522
|
+
* operation(node) {
|
|
523
|
+
* return { ...node, operationId: `prefixed_${node.operationId}` }
|
|
524
|
+
* },
|
|
525
|
+
* })
|
|
526
|
+
* ```
|
|
527
|
+
*
|
|
528
|
+
* @example Replace only the root node
|
|
529
|
+
* ```ts
|
|
530
|
+
* const next = transform(root, {
|
|
531
|
+
* depth: 'shallow',
|
|
532
|
+
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
|
|
533
|
+
* })
|
|
534
|
+
* ```
|
|
535
|
+
*/
|
|
536
|
+
declare function transform(node: InputNode, options: TransformOptions): InputNode;
|
|
537
|
+
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
|
|
538
|
+
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
|
|
539
|
+
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
|
|
540
|
+
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
|
|
541
|
+
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
|
|
542
|
+
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
|
|
543
|
+
declare function transform(node: Node, options: TransformOptions): Node;
|
|
544
|
+
/**
|
|
545
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
546
|
+
* callbacks return into an array.
|
|
547
|
+
*
|
|
548
|
+
* @example Collect every operationId
|
|
549
|
+
* ```ts
|
|
550
|
+
* const ids = collect<string>(root, {
|
|
551
|
+
* operation(node) {
|
|
552
|
+
* return node.operationId
|
|
553
|
+
* },
|
|
554
|
+
* })
|
|
555
|
+
* ```
|
|
556
|
+
*/
|
|
557
|
+
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
558
|
+
//#endregion
|
|
559
|
+
//#region src/printer.d.ts
|
|
560
|
+
/**
|
|
561
|
+
* Runtime context passed as `this` to printer handlers.
|
|
562
|
+
*
|
|
563
|
+
* `this.transform` dispatches to node-level handlers from `nodes`.
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* ```ts
|
|
567
|
+
* const context: PrinterHandlerContext<string, {}> = {
|
|
568
|
+
* options: {},
|
|
569
|
+
* transform: () => 'value',
|
|
570
|
+
* }
|
|
571
|
+
* ```
|
|
572
|
+
*/
|
|
573
|
+
type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
574
|
+
/**
|
|
575
|
+
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
|
|
576
|
+
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
|
|
577
|
+
*/
|
|
578
|
+
transform: (node: SchemaNode) => TOutput | null;
|
|
579
|
+
/**
|
|
580
|
+
* Options for this printer instance.
|
|
581
|
+
*/
|
|
582
|
+
options: TOptions;
|
|
583
|
+
};
|
|
584
|
+
/**
|
|
585
|
+
* Handler for one schema node type.
|
|
586
|
+
*
|
|
587
|
+
* Use a regular function (not an arrow function) if you need `this`.
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```ts
|
|
591
|
+
* const handler: PrinterHandler<string, {}, 'string'> = function () {
|
|
592
|
+
* return 'string'
|
|
593
|
+
* }
|
|
594
|
+
* ```
|
|
595
|
+
*/
|
|
596
|
+
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
|
|
597
|
+
/**
|
|
598
|
+
* Partial map of per-node-type handler overrides for a printer.
|
|
599
|
+
*
|
|
600
|
+
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
|
|
601
|
+
* Supply only the handlers you want to replace. The printer's built-in
|
|
602
|
+
* defaults fill in the rest.
|
|
603
|
+
*
|
|
604
|
+
* @example
|
|
605
|
+
* ```ts
|
|
606
|
+
* pluginZod({
|
|
607
|
+
* printer: {
|
|
608
|
+
* nodes: {
|
|
609
|
+
* date(): string {
|
|
610
|
+
* return 'z.string().date()'
|
|
611
|
+
* },
|
|
612
|
+
* } satisfies PrinterPartial<string, PrinterZodOptions>,
|
|
613
|
+
* },
|
|
614
|
+
* })
|
|
615
|
+
* ```
|
|
616
|
+
*/
|
|
617
|
+
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
|
|
618
|
+
/**
|
|
619
|
+
* Generic shape used by `definePrinter`.
|
|
620
|
+
*
|
|
621
|
+
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
622
|
+
* - `TOptions` options passed to and stored on the printer instance
|
|
623
|
+
* - `TOutput` the type emitted by node handlers
|
|
624
|
+
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
|
|
625
|
+
*
|
|
626
|
+
* @example
|
|
627
|
+
* ```ts
|
|
628
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
629
|
+
* ```
|
|
630
|
+
*/
|
|
631
|
+
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
632
|
+
name: TName;
|
|
633
|
+
options: TOptions;
|
|
634
|
+
output: TOutput;
|
|
635
|
+
printOutput: TPrintOutput;
|
|
636
|
+
};
|
|
637
|
+
/**
|
|
638
|
+
* Printer instance returned by a printer factory.
|
|
639
|
+
*
|
|
640
|
+
* @example
|
|
641
|
+
* ```ts
|
|
642
|
+
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
643
|
+
* ```
|
|
644
|
+
*/
|
|
645
|
+
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
646
|
+
/**
|
|
647
|
+
* Unique identifier supplied at creation time.
|
|
648
|
+
*/
|
|
649
|
+
name: T['name'];
|
|
650
|
+
/**
|
|
651
|
+
* Options for this printer instance.
|
|
652
|
+
*/
|
|
653
|
+
options: T['options'];
|
|
654
|
+
/**
|
|
655
|
+
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
|
|
656
|
+
* Always dispatches through the `nodes` map. Never calls the `print` override.
|
|
657
|
+
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
658
|
+
*/
|
|
659
|
+
transform: (node: SchemaNode) => T['output'] | null;
|
|
660
|
+
/**
|
|
661
|
+
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
662
|
+
* higher-level function (which may produce full declarations).
|
|
663
|
+
* Otherwise, falls back to the node-level dispatcher.
|
|
664
|
+
*/
|
|
665
|
+
print: (node: SchemaNode) => T['printOutput'] | null;
|
|
666
|
+
};
|
|
667
|
+
/**
|
|
668
|
+
* Builder function passed to `definePrinter`.
|
|
669
|
+
*
|
|
670
|
+
* It receives resolved options and returns:
|
|
671
|
+
* - `name`
|
|
672
|
+
* - `options`
|
|
673
|
+
* - `nodes` handlers
|
|
674
|
+
* - optional top-level `print` override
|
|
675
|
+
*
|
|
676
|
+
* @example
|
|
677
|
+
* ```ts
|
|
678
|
+
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
679
|
+
* ```
|
|
680
|
+
*/
|
|
681
|
+
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
682
|
+
name: T['name'];
|
|
683
|
+
/**
|
|
684
|
+
* Options to store on the printer.
|
|
685
|
+
*/
|
|
686
|
+
options: T['options'];
|
|
687
|
+
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
688
|
+
/**
|
|
689
|
+
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
690
|
+
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
691
|
+
* not the override itself, so recursion is safe.
|
|
692
|
+
*/
|
|
693
|
+
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
|
|
694
|
+
};
|
|
695
|
+
/**
|
|
696
|
+
* Defines a schema printer: a function that takes a `SchemaNode` and emits
|
|
697
|
+
* code in your target language. Each plugin that produces code from schemas
|
|
698
|
+
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
|
|
699
|
+
* with this helper.
|
|
700
|
+
*
|
|
701
|
+
* The builder receives resolved options and returns:
|
|
702
|
+
*
|
|
703
|
+
* - `name` unique identifier for the printer.
|
|
704
|
+
* - `options` stored on the returned printer instance.
|
|
705
|
+
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
|
|
706
|
+
* output (a string, a TypeScript AST node, ...) for that schema type.
|
|
707
|
+
* - `print` (optional), top-level override exposed as `printer.print`.
|
|
708
|
+
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
|
|
709
|
+
*
|
|
710
|
+
* Without a `print` override, `printer.print` falls back to `printer.transform`
|
|
711
|
+
* (the node-level dispatcher).
|
|
712
|
+
*
|
|
713
|
+
* @example Tiny Zod printer
|
|
714
|
+
* ```ts
|
|
715
|
+
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
716
|
+
*
|
|
717
|
+
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
718
|
+
*
|
|
719
|
+
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
720
|
+
* name: 'zod',
|
|
721
|
+
* options: { strict: options.strict ?? true },
|
|
722
|
+
* nodes: {
|
|
723
|
+
* string: () => 'z.string()',
|
|
724
|
+
* object(node) {
|
|
725
|
+
* const props = node.properties
|
|
726
|
+
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
727
|
+
* .join(', ')
|
|
728
|
+
* return `z.object({ ${props} })`
|
|
729
|
+
* },
|
|
730
|
+
* },
|
|
731
|
+
* }))
|
|
732
|
+
* ```
|
|
733
|
+
*/
|
|
734
|
+
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
|
|
735
|
+
/**
|
|
736
|
+
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
737
|
+
*
|
|
738
|
+
* @example
|
|
739
|
+
* ```ts
|
|
740
|
+
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
|
|
741
|
+
* (node) => node.kind,
|
|
742
|
+
* )
|
|
743
|
+
* ```
|
|
744
|
+
*/
|
|
745
|
+
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"]) => {
|
|
746
|
+
name: T["name"];
|
|
747
|
+
options: T["options"];
|
|
748
|
+
nodes: Partial<{ [K in TKey]: (this: {
|
|
749
|
+
transform: (node: TNode) => T["output"] | null;
|
|
750
|
+
options: T["options"];
|
|
751
|
+
}, node: TNodeByKey[K]) => T["output"] | null }>;
|
|
752
|
+
print?: (this: {
|
|
753
|
+
transform: (node: TNode) => T["output"] | null;
|
|
754
|
+
options: T["options"];
|
|
755
|
+
}, node: TNode) => T["printOutput"] | null;
|
|
756
|
+
}) => (options?: T["options"]) => {
|
|
757
|
+
name: T["name"];
|
|
758
|
+
options: T["options"];
|
|
759
|
+
transform: (node: TNode) => T["output"] | null;
|
|
760
|
+
print: (node: TNode) => T["printOutput"] | null;
|
|
761
|
+
};
|
|
762
|
+
//#endregion
|
|
763
|
+
export { applyDedupe as _, definePrinter as a, schemaTypes as b, VisitorContext as c, walk as d, SchemaDialect as f, DedupePlan as g, DedupeLookups as h, createPrinterFactory as i, collect as l, DedupeCanonical as m, PrinterFactoryOptions as n, ParentOf as o, defineSchemaDialect as p, PrinterPartial as r, Visitor as s, Printer as t, transform as u, buildDedupePlan as v, httpMethods as y };
|
|
764
|
+
//# sourceMappingURL=types-olVl9v5p.d.ts.map
|