@kubb/ast 5.0.0-beta.75 → 5.0.0-beta.76

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.
Files changed (64) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +53 -27
  3. package/dist/defineMacro-C58x6uaa.cjs +114 -0
  4. package/dist/defineMacro-C58x6uaa.cjs.map +1 -0
  5. package/dist/defineMacro-DzsACbFo.d.ts +466 -0
  6. package/dist/defineMacro-Zagno12u.js +98 -0
  7. package/dist/defineMacro-Zagno12u.js.map +1 -0
  8. package/dist/index-Cu2zmNxv.d.ts +2188 -0
  9. package/dist/index.cjs +183 -2179
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.ts +88 -3364
  12. package/dist/index.js +139 -2101
  13. package/dist/index.js.map +1 -1
  14. package/dist/macros.cjs +130 -0
  15. package/dist/macros.cjs.map +1 -0
  16. package/dist/macros.d.ts +61 -0
  17. package/dist/macros.js +128 -0
  18. package/dist/macros.js.map +1 -0
  19. package/dist/refs-DhraOHHv.cjs +135 -0
  20. package/dist/refs-DhraOHHv.cjs.map +1 -0
  21. package/dist/refs-DliAPaUa.js +101 -0
  22. package/dist/refs-DliAPaUa.js.map +1 -0
  23. package/dist/rolldown-runtime-CNktS9qV.js +17 -0
  24. package/dist/types-Ctz5NB1o.d.ts +244 -0
  25. package/dist/types.cjs +0 -0
  26. package/dist/types.d.ts +4 -0
  27. package/dist/types.js +1 -0
  28. package/dist/utils.cjs +613 -0
  29. package/dist/utils.cjs.map +1 -0
  30. package/dist/utils.d.ts +353 -0
  31. package/dist/utils.js +589 -0
  32. package/dist/utils.js.map +1 -0
  33. package/dist/visitor-CDa9Cn6x.cjs +1547 -0
  34. package/dist/visitor-CDa9Cn6x.cjs.map +1 -0
  35. package/dist/visitor-Ns-njjbG.js +1183 -0
  36. package/dist/visitor-Ns-njjbG.js.map +1 -0
  37. package/package.json +18 -7
  38. package/dist/chunk--u3MIqq1.js +0 -8
  39. package/src/constants.ts +0 -228
  40. package/src/factory.ts +0 -742
  41. package/src/guards.ts +0 -110
  42. package/src/index.ts +0 -45
  43. package/src/infer.ts +0 -130
  44. package/src/mocks.ts +0 -176
  45. package/src/nodes/base.ts +0 -56
  46. package/src/nodes/code.ts +0 -304
  47. package/src/nodes/file.ts +0 -230
  48. package/src/nodes/function.ts +0 -223
  49. package/src/nodes/http.ts +0 -119
  50. package/src/nodes/index.ts +0 -86
  51. package/src/nodes/operation.ts +0 -111
  52. package/src/nodes/output.ts +0 -26
  53. package/src/nodes/parameter.ts +0 -41
  54. package/src/nodes/property.ts +0 -34
  55. package/src/nodes/response.ts +0 -43
  56. package/src/nodes/root.ts +0 -64
  57. package/src/nodes/schema.ts +0 -656
  58. package/src/printer.ts +0 -250
  59. package/src/refs.ts +0 -67
  60. package/src/resolvers.ts +0 -45
  61. package/src/transformers.ts +0 -159
  62. package/src/types.ts +0 -70
  63. package/src/utils.ts +0 -833
  64. package/src/visitor.ts +0 -592
package/dist/index.cjs CHANGED
@@ -1,2217 +1,221 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region \0rolldown/runtime.js
3
- var __create = Object.create;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf;
8
- var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
- get: ((k) => from[k]).bind(null, key),
14
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
- });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
- value: mod,
21
- enumerable: true
22
- }) : target, mod));
23
- //#endregion
24
- let node_crypto = require("node:crypto");
25
- let node_path = require("node:path");
26
- node_path = __toESM(node_path, 1);
27
- //#region src/constants.ts
28
- const visitorDepths = {
29
- shallow: "shallow",
30
- deep: "deep"
31
- };
32
- const nodeKinds = {
33
- input: "Input",
34
- output: "Output",
35
- operation: "Operation",
36
- schema: "Schema",
37
- property: "Property",
38
- parameter: "Parameter",
39
- response: "Response",
40
- functionParameter: "FunctionParameter",
41
- parameterGroup: "ParameterGroup",
42
- functionParameters: "FunctionParameters",
43
- type: "Type",
44
- file: "File",
45
- import: "Import",
46
- export: "Export",
47
- source: "Source",
48
- text: "Text",
49
- break: "Break"
50
- };
51
- /**
52
- * Schema type discriminators used by all AST schema nodes.
53
- *
54
- * These values serve as stable discriminators across the AST (e.g., `schema.type === schemaTypes.object`).
55
- * Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),
56
- * and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.
57
- */
58
- const schemaTypes = {
59
- /**
60
- * Text value.
61
- */
62
- string: "string",
63
- /**
64
- * Floating-point number (`float`, `double`).
65
- */
66
- number: "number",
67
- /**
68
- * Whole number (`int32`). Use `bigint` for `int64`.
69
- */
70
- integer: "integer",
71
- /**
72
- * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
73
- */
74
- bigint: "bigint",
75
- /**
76
- * Boolean value
77
- */
78
- boolean: "boolean",
79
- /**
80
- * Explicit null value.
81
- */
82
- null: "null",
83
- /**
84
- * Any value (no type restriction).
85
- */
86
- any: "any",
87
- /**
88
- * Unknown value (must be narrowed before usage).
89
- */
90
- unknown: "unknown",
91
- /**
92
- * No return value (`void`).
93
- */
94
- void: "void",
95
- /**
96
- * Object with named properties.
97
- */
98
- object: "object",
99
- /**
100
- * Sequential list of items.
101
- */
102
- array: "array",
103
- /**
104
- * Fixed-length list with position-specific items.
105
- */
106
- tuple: "tuple",
107
- /**
108
- * "One of" multiple schema members.
109
- */
110
- union: "union",
111
- /**
112
- * "All of" multiple schema members.
113
- */
114
- intersection: "intersection",
115
- /**
116
- * Enum schema.
117
- */
118
- enum: "enum",
119
- /**
120
- * Reference to another schema.
121
- */
122
- ref: "ref",
123
- /**
124
- * Calendar date (for example `2026-03-24`).
125
- */
126
- date: "date",
127
- /**
128
- * Date-time value (for example `2026-03-24T09:00:00Z`).
129
- */
130
- datetime: "datetime",
131
- /**
132
- * Time-only value (for example `09:00:00`).
133
- */
134
- time: "time",
135
- /**
136
- * UUID value.
137
- */
138
- uuid: "uuid",
139
- /**
140
- * Email address value.
141
- */
142
- email: "email",
143
- /**
144
- * URL value.
145
- */
146
- url: "url",
147
- /**
148
- * IPv4 address value.
149
- */
150
- ipv4: "ipv4",
151
- /**
152
- * IPv6 address value.
153
- */
154
- ipv6: "ipv6",
155
- /**
156
- * Binary/blob value.
157
- */
158
- blob: "blob",
159
- /**
160
- * Impossible value (`never`).
161
- */
162
- never: "never"
163
- };
164
- /**
165
- * Scalar primitive schema types used for union simplification and type narrowing.
166
- *
167
- * Use `isScalarPrimitive()` to safely check whether a type is a scalar primitive.
168
- */
169
- const SCALAR_PRIMITIVE_TYPES = new Set([
170
- "string",
171
- "number",
172
- "integer",
173
- "bigint",
174
- "boolean"
175
- ]);
176
- /**
177
- * Type guard that returns `true` when `type` is a scalar primitive schema type.
178
- *
179
- * Use this to check if a schema type can be directly assigned without wrapping (e.g., `string | number | boolean`).
180
- */
181
- function isScalarPrimitive(type) {
182
- return SCALAR_PRIMITIVE_TYPES.has(type);
183
- }
184
- /**
185
- * HTTP method identifiers used by operation nodes.
186
- *
187
- * Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).
188
- */
189
- const httpMethods = {
190
- get: "GET",
191
- post: "POST",
192
- put: "PUT",
193
- patch: "PATCH",
194
- delete: "DELETE",
195
- head: "HEAD",
196
- options: "OPTIONS",
197
- trace: "TRACE"
198
- };
199
- /**
200
- * Common MIME types used in request/response content negotiation.
201
- *
202
- * Covers JSON, XML, form data, PDFs, images, audio, and video formats.
203
- * Use these as keys when serializing request/response bodies.
204
- */
205
- const mediaTypes = {
206
- applicationJson: "application/json",
207
- applicationXml: "application/xml",
208
- applicationFormUrlEncoded: "application/x-www-form-urlencoded",
209
- applicationOctetStream: "application/octet-stream",
210
- applicationPdf: "application/pdf",
211
- applicationZip: "application/zip",
212
- applicationGraphql: "application/graphql",
213
- multipartFormData: "multipart/form-data",
214
- textPlain: "text/plain",
215
- textHtml: "text/html",
216
- textCsv: "text/csv",
217
- textXml: "text/xml",
218
- imagePng: "image/png",
219
- imageJpeg: "image/jpeg",
220
- imageGif: "image/gif",
221
- imageWebp: "image/webp",
222
- imageSvgXml: "image/svg+xml",
223
- audioMpeg: "audio/mpeg",
224
- videoMp4: "video/mp4"
225
- };
226
- //#endregion
227
- //#region ../../internals/utils/src/casing.ts
228
- /**
229
- * Shared implementation for camelCase and PascalCase conversion.
230
- * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
231
- * and capitalizes each word according to `pascal`.
232
- *
233
- * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
234
- */
235
- function toCamelOrPascal(text, pascal) {
236
- return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
237
- if (word.length > 1 && word === word.toUpperCase()) return word;
238
- if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
239
- return word.charAt(0).toUpperCase() + word.slice(1);
240
- }).join("").replace(/[^a-zA-Z0-9]/g, "");
241
- }
242
- /**
243
- * Splits `text` on `.` and applies `transformPart` to each segment.
244
- * The last segment receives `isLast = true`, all earlier segments receive `false`.
245
- * Segments are joined with `/` to form a file path.
246
- *
247
- * Only splits on dots followed by a letter so that version numbers
248
- * embedded in operationIds (e.g. `v2025.0`) are kept intact.
249
- *
250
- * Empty segments are filtered before joining. They arise when the text starts with
251
- * a dot followed immediately by a letter (e.g. `..Schema` splits into `['..', 'Schema']`
252
- * and `'..'` transforms to an empty string). Without this filter the join would produce
253
- * a leading `/`, which `path.resolve` would interpret as an absolute path, allowing
254
- * generated files to escape the configured output directory.
255
- */
256
- function applyToFileParts(text, transformPart) {
257
- const parts = text.split(/\.(?=[a-zA-Z])/);
258
- return parts.map((part, i) => transformPart(part, i === parts.length - 1)).filter(Boolean).join("/");
259
- }
260
- /**
261
- * Converts `text` to camelCase.
262
- * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
263
- *
264
- * @example
265
- * camelCase('hello-world') // 'helloWorld'
266
- * camelCase('pet.petId', { isFile: true }) // 'pet/petId'
267
- */
268
- function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
269
- if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
270
- prefix,
271
- suffix
272
- } : {}));
273
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
274
- }
275
- /**
276
- * Converts `text` to PascalCase.
277
- * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
278
- *
279
- * @example
280
- * pascalCase('hello-world') // 'HelloWorld'
281
- * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
282
- */
283
- function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
284
- if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
285
- prefix,
286
- suffix
287
- }) : camelCase(part));
288
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
289
- }
290
- //#endregion
291
- //#region ../../internals/utils/src/reserved.ts
292
- /**
293
- * JavaScript and Java reserved words.
294
- * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
295
- */
296
- const reservedWords = new Set([
297
- "abstract",
298
- "arguments",
299
- "boolean",
300
- "break",
301
- "byte",
302
- "case",
303
- "catch",
304
- "char",
305
- "class",
306
- "const",
307
- "continue",
308
- "debugger",
309
- "default",
310
- "delete",
311
- "do",
312
- "double",
313
- "else",
314
- "enum",
315
- "eval",
316
- "export",
317
- "extends",
318
- "false",
319
- "final",
320
- "finally",
321
- "float",
322
- "for",
323
- "function",
324
- "goto",
325
- "if",
326
- "implements",
327
- "import",
328
- "in",
329
- "instanceof",
330
- "int",
331
- "interface",
332
- "let",
333
- "long",
334
- "native",
335
- "new",
336
- "null",
337
- "package",
338
- "private",
339
- "protected",
340
- "public",
341
- "return",
342
- "short",
343
- "static",
344
- "super",
345
- "switch",
346
- "synchronized",
347
- "this",
348
- "throw",
349
- "throws",
350
- "transient",
351
- "true",
352
- "try",
353
- "typeof",
354
- "var",
355
- "void",
356
- "volatile",
357
- "while",
358
- "with",
359
- "yield",
360
- "Array",
361
- "Date",
362
- "hasOwnProperty",
363
- "Infinity",
364
- "isFinite",
365
- "isNaN",
366
- "isPrototypeOf",
367
- "length",
368
- "Math",
369
- "name",
370
- "NaN",
371
- "Number",
372
- "Object",
373
- "prototype",
374
- "String",
375
- "toString",
376
- "undefined",
377
- "valueOf"
378
- ]);
379
- /**
380
- * Returns `true` when `name` is a syntactically valid JavaScript variable name.
381
- *
382
- * @example
383
- * ```ts
384
- * isValidVarName('status') // true
385
- * isValidVarName('class') // false (reserved word)
386
- * isValidVarName('42foo') // false (starts with digit)
387
- * ```
388
- */
389
- function isValidVarName(name) {
390
- if (!name || reservedWords.has(name)) return false;
391
- return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
392
- }
393
- //#endregion
394
- //#region ../../internals/utils/src/string.ts
395
- /**
396
- * Strips the file extension from a path or file name.
397
- * Only removes the last `.ext` segment when the dot is not part of a directory name.
398
- *
399
- * @example
400
- * trimExtName('petStore.ts') // 'petStore'
401
- * trimExtName('/src/models/pet.ts') // '/src/models/pet'
402
- * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
403
- * trimExtName('noExtension') // 'noExtension'
404
- */
405
- function trimExtName(text) {
406
- const dotIndex = text.lastIndexOf(".");
407
- if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
408
- return text;
409
- }
410
- //#endregion
411
- //#region src/guards.ts
412
- /**
413
- * Narrows a `SchemaNode` to the variant that matches `type`.
414
- *
415
- * @example
416
- * ```ts
417
- * const schema = createSchema({ type: 'string' })
418
- * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | undefined
419
- * ```
420
- */
421
- function narrowSchema(node, type) {
422
- return node?.type === type ? node : void 0;
423
- }
424
- function isKind(kind) {
425
- return (node) => node.kind === kind;
426
- }
427
- /**
428
- * Returns `true` when the input is an `InputNode`.
429
- *
430
- * @example
431
- * ```ts
432
- * if (isInputNode(node)) {
433
- * console.log(node.schemas.length)
434
- * }
435
- * ```
436
- */
437
- const isInputNode = isKind("Input");
438
- /**
439
- * Returns `true` when the input is an `OutputNode`.
440
- *
441
- * @example
442
- * ```ts
443
- * if (isOutputNode(node)) {
444
- * console.log(node.files.length)
445
- * }
446
- * ```
447
- */
448
- const isOutputNode = isKind("Output");
449
- /**
450
- * Returns `true` when the input is an `OperationNode`.
451
- *
452
- * @example
453
- * ```ts
454
- * if (isOperationNode(node)) {
455
- * console.log(node.operationId)
456
- * }
457
- * ```
458
- */
459
- const isOperationNode = isKind("Operation");
460
- /**
461
- * Returns `true` when the input is a `SchemaNode`.
462
- *
463
- * @example
464
- * ```ts
465
- * if (isSchemaNode(node)) {
466
- * console.log(node.type)
467
- * }
468
- * ```
469
- */
470
- const isSchemaNode = isKind("Schema");
471
- isKind("Property");
472
- isKind("Parameter");
473
- isKind("Response");
474
- isKind("FunctionParameter");
475
- isKind("ParameterGroup");
476
- isKind("FunctionParameters");
477
- //#endregion
478
- //#region src/refs.ts
479
- /**
480
- * Returns the last path segment of a reference string.
481
- *
482
- * Example: `#/components/schemas/Pet` becomes `Pet`.
483
- *
484
- * @example
485
- * ```ts
486
- * extractRefName('#/components/schemas/Pet') // 'Pet'
487
- * ```
488
- */
489
- function extractRefName(ref) {
490
- return ref.split("/").at(-1) ?? ref;
491
- }
492
- //#endregion
493
- //#region src/visitor.ts
494
- /**
495
- * Creates a small async concurrency limiter.
496
- *
497
- * At most `concurrency` tasks are in flight at once. Extra tasks are queued.
498
- *
499
- * @example
500
- * ```ts
501
- * const limit = createLimit(2)
502
- * for (const task of [taskA, taskB, taskC]) {
503
- * await limit(() => task())
504
- * }
505
- * // only 2 tasks run at the same time
506
- * ```
507
- */
508
- function createLimit(concurrency) {
509
- let active = 0;
510
- const queue = [];
511
- function next() {
512
- if (active < concurrency && queue.length > 0) {
513
- active++;
514
- queue.shift()();
515
- }
516
- }
517
- return function limit(fn) {
518
- return new Promise((resolve, reject) => {
519
- queue.push(() => {
520
- Promise.resolve(fn()).then(resolve, reject).finally(() => {
521
- active--;
522
- next();
523
- });
524
- });
525
- next();
526
- });
527
- };
528
- }
529
- /**
530
- * Returns the immediate traversable children of `node`.
531
- *
532
- * For `Schema` nodes, children (`properties`, `items`, `members`, and non-boolean
533
- * `additionalProperties`) are only included
534
- * when `recurse` is `true`; shallow mode skips them.
535
- *
536
- * @example
537
- * ```ts
538
- * const children = getChildren(operationNode, true)
539
- * // returns parameters, requestBody schema (if present), and responses
540
- * ```
541
- */
542
- function getChildren(node, recurse) {
543
- switch (node.kind) {
544
- case "Input": return [...node.schemas, ...node.operations];
545
- case "Output": return [];
546
- case "Operation": return [
547
- ...node.parameters,
548
- ...node.requestBody?.content?.flatMap((c) => c.schema ? [c.schema] : []) ?? [],
549
- ...node.responses
550
- ];
551
- case "Schema": {
552
- const children = [];
553
- if (!recurse) return [];
554
- if ("properties" in node && node.properties.length > 0) children.push(...node.properties);
555
- if ("items" in node && node.items) children.push(...node.items);
556
- if ("members" in node && node.members) children.push(...node.members);
557
- if ("additionalProperties" in node && node.additionalProperties && node.additionalProperties !== true) children.push(node.additionalProperties);
558
- return children;
559
- }
560
- case "Property": return [node.schema];
561
- case "Parameter": return [node.schema];
562
- case "Response": return node.schema ? [node.schema] : [];
563
- case "FunctionParameter":
564
- case "ParameterGroup":
565
- case "FunctionParameters":
566
- case "Type": return [];
567
- default: return [];
568
- }
569
- }
570
- /**
571
- * Depth-first traversal for side effects. Visitor return values are ignored.
572
- * Sibling nodes at each level are visited concurrently up to `options.concurrency`
573
- * (default: `WALK_CONCURRENCY`).
574
- *
575
- * @example
576
- * ```ts
577
- * await walk(root, {
578
- * operation(node) {
579
- * console.log(node.operationId)
580
- * },
581
- * })
582
- * ```
583
- *
584
- * @example
585
- * ```ts
586
- * // Visit only the current node
587
- * await walk(root, { depth: 'shallow', root: () => {} })
588
- * ```
589
- */
590
- async function walk(node, options) {
591
- return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
592
- }
593
- async function _walk(node, visitor, recurse, limit, parent) {
594
- switch (node.kind) {
595
- case "Input":
596
- await limit(() => visitor.input?.(node, { parent }));
597
- break;
598
- case "Output":
599
- await limit(() => visitor.output?.(node, { parent }));
600
- break;
601
- case "Operation":
602
- await limit(() => visitor.operation?.(node, { parent }));
603
- break;
604
- case "Schema":
605
- await limit(() => visitor.schema?.(node, { parent }));
606
- break;
607
- case "Property":
608
- await limit(() => visitor.property?.(node, { parent }));
609
- break;
610
- case "Parameter":
611
- await limit(() => visitor.parameter?.(node, { parent }));
612
- break;
613
- case "Response":
614
- await limit(() => visitor.response?.(node, { parent }));
615
- break;
616
- case "FunctionParameter":
617
- case "ParameterGroup":
618
- case "FunctionParameters": break;
619
- }
620
- const children = getChildren(node, recurse);
621
- for (const child of children) await _walk(child, visitor, recurse, limit, node);
622
- }
623
- function transform(node, options) {
624
- const { depth, parent, ...visitor } = options;
625
- const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
626
- switch (node.kind) {
627
- case "Input": {
628
- let input = node;
629
- const replaced = visitor.input?.(input, { parent });
630
- if (replaced) input = replaced;
631
- return {
632
- ...input,
633
- schemas: input.schemas.map((s) => transform(s, {
634
- ...options,
635
- parent: input
636
- })),
637
- operations: input.operations.map((op) => transform(op, {
638
- ...options,
639
- parent: input
640
- }))
641
- };
642
- }
643
- case "Output": {
644
- let output = node;
645
- const replaced = visitor.output?.(output, { parent });
646
- if (replaced) output = replaced;
647
- return output;
648
- }
649
- case "Operation": {
650
- let op = node;
651
- const replaced = visitor.operation?.(op, { parent });
652
- if (replaced) op = replaced;
653
- return {
654
- ...op,
655
- parameters: op.parameters.map((p) => transform(p, {
656
- ...options,
657
- parent: op
658
- })),
659
- requestBody: op.requestBody ? {
660
- ...op.requestBody,
661
- content: op.requestBody.content?.map((c) => ({
662
- ...c,
663
- schema: c.schema ? transform(c.schema, {
664
- ...options,
665
- parent: op
666
- }) : void 0
667
- }))
668
- } : void 0,
669
- responses: op.responses.map((r) => transform(r, {
670
- ...options,
671
- parent: op
672
- }))
673
- };
674
- }
675
- case "Schema": {
676
- let schema = node;
677
- const replaced = visitor.schema?.(schema, { parent });
678
- if (replaced) schema = replaced;
679
- const childOptions = {
680
- ...options,
681
- parent: schema
682
- };
683
- return {
684
- ...schema,
685
- ..."properties" in schema && recurse ? { properties: schema.properties.map((p) => transform(p, childOptions)) } : {},
686
- ..."items" in schema && recurse ? { items: schema.items?.map((i) => transform(i, childOptions)) } : {},
687
- ..."members" in schema && recurse ? { members: schema.members?.map((m) => transform(m, childOptions)) } : {},
688
- ..."additionalProperties" in schema && recurse && schema.additionalProperties && schema.additionalProperties !== true ? { additionalProperties: transform(schema.additionalProperties, childOptions) } : {}
689
- };
690
- }
691
- case "Property": {
692
- let prop = node;
693
- const replaced = visitor.property?.(prop, { parent });
694
- if (replaced) prop = replaced;
695
- return createProperty({
696
- ...prop,
697
- schema: transform(prop.schema, {
698
- ...options,
699
- parent: prop
700
- })
701
- });
702
- }
703
- case "Parameter": {
704
- let param = node;
705
- const replaced = visitor.parameter?.(param, { parent });
706
- if (replaced) param = replaced;
707
- return createParameter({
708
- ...param,
709
- schema: transform(param.schema, {
710
- ...options,
711
- parent: param
712
- })
713
- });
714
- }
715
- case "Response": {
716
- let response = node;
717
- const replaced = visitor.response?.(response, { parent });
718
- if (replaced) response = replaced;
719
- return {
720
- ...response,
721
- schema: transform(response.schema, {
722
- ...options,
723
- parent: response
724
- })
725
- };
726
- }
727
- case "FunctionParameter":
728
- case "ParameterGroup":
729
- case "FunctionParameters":
730
- case "Type": return node;
731
- default: return node;
732
- }
733
- }
734
- /**
735
- * Runs a depth-first synchronous collection pass.
736
- *
737
- * Non-`undefined` values returned by visitor callbacks are appended to the result.
738
- *
739
- * @example
740
- * ```ts
741
- * const ids = collect(root, {
742
- * operation(node) {
743
- * return node.operationId
744
- * },
745
- * })
746
- * ```
747
- *
748
- * @example
749
- * ```ts
750
- * // Collect from only the current node
751
- * const values = collect(root, { depth: 'shallow', root: () => 'root' })
752
- * ```
753
- */
754
- function collect(node, options) {
755
- const { depth, parent, ...visitor } = options;
756
- const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
757
- const results = [];
758
- let v;
759
- switch (node.kind) {
760
- case "Input":
761
- v = visitor.input?.(node, { parent });
762
- break;
763
- case "Output":
764
- v = visitor.output?.(node, { parent });
765
- break;
766
- case "Operation":
767
- v = visitor.operation?.(node, { parent });
768
- break;
769
- case "Schema":
770
- v = visitor.schema?.(node, { parent });
771
- break;
772
- case "Property":
773
- v = visitor.property?.(node, { parent });
774
- break;
775
- case "Parameter":
776
- v = visitor.parameter?.(node, { parent });
777
- break;
778
- case "Response":
779
- v = visitor.response?.(node, { parent });
780
- break;
781
- case "FunctionParameter":
782
- case "ParameterGroup":
783
- case "FunctionParameters": break;
784
- }
785
- if (v !== void 0) results.push(v);
786
- for (const child of getChildren(node, recurse)) for (const item of collect(child, {
787
- ...options,
788
- parent: node
789
- })) results.push(item);
790
- return results;
791
- }
792
- //#endregion
793
- //#region src/utils.ts
794
- const plainStringTypes = new Set([
795
- "string",
796
- "uuid",
797
- "email",
798
- "url",
799
- "datetime"
800
- ]);
801
- /**
802
- * Merges a ref node with its resolved schema, giving usage-site fields precedence.
803
- *
804
- * Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node
805
- * override the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
806
- *
807
- * @example
808
- * ```ts
809
- * // Ref with description override
810
- * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
811
- * const merged = syncSchemaRef(ref) // merges with resolved Pet schema
812
- * ```
813
- */
814
- function syncSchemaRef(node) {
815
- const ref = narrowSchema(node, "ref");
816
- if (!ref) return node;
817
- if (!ref.schema) return node;
818
- const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
819
- const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
820
- return createSchema({
821
- ...ref.schema,
822
- ...definedOverrides
823
- });
824
- }
825
- /**
826
- * Type guard that returns `true` when a schema emits as a plain `string` type.
827
- *
828
- * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
829
- * types, returns `true` only when `representation` is `'string'` rather than `'date'`.
830
- */
831
- function isStringType(node) {
832
- if (plainStringTypes.has(node.type)) return true;
833
- const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
834
- if (temporal) return temporal.representation !== "date";
835
- return false;
836
- }
837
- /**
838
- * Applies casing rules to parameter names and returns a new parameter array.
839
- *
840
- * Use this before passing parameters to schema builders so output property keys match
841
- * the desired casing while preserving `OperationNode.parameters` for other consumers.
842
- * The input array is not mutated. When `casing` is not set, the original array is returned unchanged.
843
- */
844
- function caseParams(params, casing) {
845
- if (!casing) return params;
846
- return params.map((param) => {
847
- const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name;
848
- return {
849
- ...param,
850
- name: transformed
851
- };
852
- });
853
- }
854
- /**
855
- * Creates a single-property object schema used as a discriminator literal.
856
- *
857
- * @example
858
- * ```ts
859
- * createDiscriminantNode({ propertyName: 'type', value: 'dog' })
860
- * // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
861
- * ```
862
- */
863
- function createDiscriminantNode({ propertyName, value }) {
864
- return createSchema({
865
- type: "object",
866
- primitive: "object",
867
- properties: [createProperty({
868
- name: propertyName,
869
- schema: createSchema({
870
- type: "enum",
871
- primitive: "string",
872
- enumValues: [value]
873
- }),
874
- required: true
875
- })]
876
- });
877
- }
878
- function resolveParamsType({ node, param, resolver }) {
879
- if (!resolver) return createParamsType({
880
- variant: "reference",
881
- name: param.schema.primitive ?? "unknown"
882
- });
883
- const individualName = resolver.resolveParamName(node, param);
884
- const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
885
- const groupResolvers = {
886
- path: resolver.resolvePathParamsName,
887
- query: resolver.resolveQueryParamsName,
888
- header: resolver.resolveHeaderParamsName
889
- };
890
- const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
891
- if (groupName && groupName !== individualName) return createParamsType({
892
- variant: "member",
893
- base: groupName,
894
- key: param.name
895
- });
896
- return createParamsType({
897
- variant: "reference",
898
- name: individualName
899
- });
900
- }
901
- /**
902
- * Converts an `OperationNode` into function parameters for code generation.
903
- *
904
- * Centralizes parameter grouping logic for all plugins. Provide a `resolver` for type name resolution
905
- * and `extraParams` for plugin-specific trailing parameters (e.g., `options` objects).
906
- * Supports three grouping modes: `object` (single destructured param), `inline` (separate params),
907
- * and `inlineSpread` (rest parameter). Use `CreateOperationParamsOptions` to fine-tune output.
908
- */
909
- function createOperationParams(node, options) {
910
- const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options;
911
- const dataName = paramNames?.data ?? "data";
912
- const paramsName = paramNames?.params ?? "params";
913
- const headersName = paramNames?.headers ?? "headers";
914
- const pathName = paramNames?.path ?? "pathParams";
915
- const wrapType = (type) => createParamsType({
916
- variant: "reference",
917
- name: typeWrapper ? typeWrapper(type) : type
918
- });
919
- const wrapTypeNode = (type) => type.kind === "ParamsType" && type.variant === "reference" ? wrapType(type.name) : type;
920
- const casedParams = caseParams(node.parameters, paramsCasing);
921
- const pathParams = casedParams.filter((p) => p.in === "path");
922
- const queryParams = casedParams.filter((p) => p.in === "query");
923
- const headerParams = casedParams.filter((p) => p.in === "header");
924
- const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
925
- const bodyRequired = node.requestBody?.required ?? false;
926
- const queryGroupType = resolver ? resolveGroupType({
927
- node,
928
- params: queryParams,
929
- groupMethod: resolver.resolveQueryParamsName,
930
- resolver
931
- }) : void 0;
932
- const headerGroupType = resolver ? resolveGroupType({
933
- node,
934
- params: headerParams,
935
- groupMethod: resolver.resolveHeaderParamsName,
936
- resolver
937
- }) : void 0;
938
- const params = [];
939
- if (paramsType === "object") {
940
- const children = [
941
- ...pathParams.map((p) => {
942
- const type = resolveParamsType({
943
- node,
944
- param: p,
945
- resolver
946
- });
947
- return createFunctionParameter({
948
- name: p.name,
949
- type: wrapTypeNode(type),
950
- optional: !p.required
951
- });
952
- }),
953
- ...bodyType ? [createFunctionParameter({
954
- name: dataName,
955
- type: bodyType,
956
- optional: !bodyRequired
957
- })] : [],
958
- ...buildGroupParam({
959
- name: paramsName,
960
- node,
961
- params: queryParams,
962
- groupType: queryGroupType,
963
- resolver,
964
- wrapType
965
- }),
966
- ...buildGroupParam({
967
- name: headersName,
968
- node,
969
- params: headerParams,
970
- groupType: headerGroupType,
971
- resolver,
972
- wrapType
973
- })
974
- ];
975
- if (children.length) params.push(createParameterGroup({
976
- properties: children,
977
- default: children.every((c) => c.optional) ? "{}" : void 0
978
- }));
979
- } else {
980
- if (pathParams.length) if (pathParamsType === "inlineSpread") {
981
- const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]) ?? void 0;
982
- params.push(createFunctionParameter({
983
- name: pathName,
984
- type: spreadType ? wrapType(spreadType) : void 0,
985
- rest: true
986
- }));
987
- } else {
988
- const pathChildren = pathParams.map((p) => {
989
- const type = resolveParamsType({
990
- node,
991
- param: p,
992
- resolver
993
- });
994
- return createFunctionParameter({
995
- name: p.name,
996
- type: wrapTypeNode(type),
997
- optional: !p.required
998
- });
999
- });
1000
- params.push(createParameterGroup({
1001
- properties: pathChildren,
1002
- inline: pathParamsType === "inline",
1003
- default: pathParamsDefault ?? (pathChildren.every((c) => c.optional) ? "{}" : void 0)
1004
- }));
1005
- }
1006
- if (bodyType) params.push(createFunctionParameter({
1007
- name: dataName,
1008
- type: bodyType,
1009
- optional: !bodyRequired
1010
- }));
1011
- params.push(...buildGroupParam({
1012
- name: paramsName,
1013
- node,
1014
- params: queryParams,
1015
- groupType: queryGroupType,
1016
- resolver,
1017
- wrapType
1018
- }));
1019
- params.push(...buildGroupParam({
1020
- name: headersName,
1021
- node,
1022
- params: headerParams,
1023
- groupType: headerGroupType,
1024
- resolver,
1025
- wrapType
1026
- }));
1027
- }
1028
- params.push(...extraParams);
1029
- return createFunctionParameters({ params });
1030
- }
1031
- /**
1032
- * Builds a single {@link FunctionParameterNode} for a query or header group.
1033
- * Returns an empty array when there are no params to emit.
1034
- *
1035
- * If a pre-resolved `groupType` is provided it emits `name: GroupType`.
1036
- * Otherwise, it builds an inline struct from the individual params.
1037
- */
1038
- function buildGroupParam({ name, node, params, groupType, resolver, wrapType }) {
1039
- if (groupType) return [createFunctionParameter({
1040
- name,
1041
- type: groupType.type.kind === "ParamsType" && groupType.type.variant === "reference" ? wrapType(groupType.type.name) : groupType.type,
1042
- optional: groupType.optional
1043
- })];
1044
- if (params.length) return [createFunctionParameter({
1045
- name,
1046
- type: toStructType({
1047
- node,
1048
- params,
1049
- resolver
1050
- }),
1051
- optional: params.every((p) => !p.required)
1052
- })];
1053
- return [];
1054
- }
1055
- /**
1056
- * Derives a {@link ParamGroupType} from the resolver's group method.
1057
- * Returns `undefined` when the group name equals the individual param name (no real group).
1058
- */
1059
- function resolveGroupType({ node, params, groupMethod, resolver }) {
1060
- if (!params.length) return;
1061
- const firstParam = params[0];
1062
- const groupName = groupMethod.call(resolver, node, firstParam);
1063
- if (groupName === resolver.resolveParamName(node, firstParam)) return;
1064
- const allOptional = params.every((p) => !p.required);
1065
- return {
1066
- type: createParamsType({
1067
- variant: "reference",
1068
- name: groupName
1069
- }),
1070
- optional: allOptional
1071
- };
1072
- }
1073
- /**
1074
- * Builds a {@link TypeNode} with `variant: 'struct'` for an inline anonymous type grouping named fields.
1075
- *
1076
- * Used when query or header parameters have no dedicated group type name.
1077
- * Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
1078
- */
1079
- function toStructType({ node, params, resolver }) {
1080
- return createParamsType({
1081
- variant: "struct",
1082
- properties: params.map((p) => ({
1083
- name: p.name,
1084
- optional: !p.required,
1085
- type: resolveParamsType({
1086
- node,
1087
- param: p,
1088
- resolver
1089
- })
1090
- }))
1091
- });
1092
- }
1093
- function sourceKey(source) {
1094
- return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
1095
- }
1096
- function pathTypeKey(path, isTypeOnly) {
1097
- return `${path}:${isTypeOnly ?? false}`;
1098
- }
1099
- function exportKey(path, name, isTypeOnly, asAlias) {
1100
- return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
1101
- }
1102
- function importKey(path, name, isTypeOnly) {
1103
- return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
1104
- }
1105
- /**
1106
- * Computes a multi-level sort key for exports and imports:
1107
- * non-array names first (wildcards/namespace aliases); type-only before value; alphabetical path; unnamed before named.
1108
- */
1109
- function sortKey(node) {
1110
- const isArray = Array.isArray(node.name) ? "1" : "0";
1111
- const typeOnly = node.isTypeOnly ? "0" : "1";
1112
- const hasName = node.name != null ? "1" : "0";
1113
- const name = Array.isArray(node.name) ? [...node.name].sort().join("\0") : node.name ?? "";
1114
- return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
1115
- }
1116
- /**
1117
- * Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
1118
- *
1119
- * Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
1120
- */
1121
- function combineSources(sources) {
1122
- const seen = /* @__PURE__ */ new Map();
1123
- for (const source of sources) {
1124
- const key = sourceKey(source);
1125
- if (!seen.has(key)) seen.set(key, source);
1126
- }
1127
- return [...seen.values()];
1128
- }
1129
- /**
1130
- * Deduplicates and merges `ExportNode` objects by path and type.
1131
- *
1132
- * Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
1133
- * Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
1134
- */
1135
- function combineExports(exports) {
1136
- const result = [];
1137
- const namedByPath = /* @__PURE__ */ new Map();
1138
- const seen = /* @__PURE__ */ new Set();
1139
- const keyed = exports.map((node) => ({
1140
- node,
1141
- key: sortKey(node)
1142
- }));
1143
- keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
1144
- for (const { node: curr } of keyed) {
1145
- const { name, path, isTypeOnly, asAlias } = curr;
1146
- if (Array.isArray(name)) {
1147
- if (!name.length) continue;
1148
- const key = pathTypeKey(path, isTypeOnly);
1149
- const existing = namedByPath.get(key);
1150
- if (existing && Array.isArray(existing.name)) {
1151
- const merged = new Set(existing.name);
1152
- for (const n of name) merged.add(n);
1153
- existing.name = [...merged];
1154
- } else {
1155
- const newItem = {
1156
- ...curr,
1157
- name: [...new Set(name)]
1158
- };
1159
- result.push(newItem);
1160
- namedByPath.set(key, newItem);
1161
- }
1162
- } else {
1163
- const key = exportKey(path, name, isTypeOnly, asAlias);
1164
- if (!seen.has(key)) {
1165
- result.push(curr);
1166
- seen.add(key);
1167
- }
1168
- }
1169
- }
1170
- return result;
1171
- }
1172
- /**
1173
- * Deduplicates and merges `ImportNode` objects, filtering out unused imports.
1174
- *
1175
- * Retains imports that are referenced in `source` or re-exported. Imports with the same path and
1176
- * `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
1177
- *
1178
- * @note Use this when combining imports from multiple files to avoid duplicate declarations.
1179
- */
1180
- function combineImports(imports, exports, source) {
1181
- const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
1182
- const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
1183
- const result = [];
1184
- const namedByPath = /* @__PURE__ */ new Map();
1185
- const seen = /* @__PURE__ */ new Set();
1186
- const keyed = imports.map((node) => ({
1187
- node,
1188
- key: sortKey(node)
1189
- }));
1190
- keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
1191
- for (const { node: curr } of keyed) {
1192
- if (curr.path === curr.root) continue;
1193
- const { path, isTypeOnly } = curr;
1194
- let { name } = curr;
1195
- if (Array.isArray(name)) {
1196
- name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.propertyName));
1197
- if (!name.length) continue;
1198
- const key = pathTypeKey(path, isTypeOnly);
1199
- const existing = namedByPath.get(key);
1200
- if (existing && Array.isArray(existing.name)) {
1201
- const merged = new Set(existing.name);
1202
- for (const n of name) merged.add(n);
1203
- existing.name = [...merged];
1204
- } else {
1205
- const newItem = {
1206
- ...curr,
1207
- name
1208
- };
1209
- result.push(newItem);
1210
- namedByPath.set(key, newItem);
1211
- }
1212
- } else {
1213
- if (name && !isUsed(name)) continue;
1214
- const key = importKey(path, name, isTypeOnly);
1215
- if (!seen.has(key)) {
1216
- result.push(curr);
1217
- seen.add(key);
1218
- }
1219
- }
1220
- }
1221
- return result;
1222
- }
1223
- /**
1224
- * Extracts all string content from a `CodeNode` tree recursively.
1225
- *
1226
- * Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
1227
- * and nested node content. Used internally to build the full source string for import filtering.
1228
- */
1229
- function extractStringsFromNodes(nodes) {
1230
- if (!nodes?.length) return "";
1231
- return nodes.map((node) => {
1232
- if (typeof node === "string") return node;
1233
- if (node.kind === "Text") return node.value;
1234
- if (node.kind === "Break") return "";
1235
- if (node.kind === "Jsx") return node.value;
1236
- const parts = [];
1237
- if ("params" in node && node.params) parts.push(node.params);
1238
- if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
1239
- if ("returnType" in node && node.returnType) parts.push(node.returnType);
1240
- if ("type" in node && typeof node.type === "string") parts.push(node.type);
1241
- const nested = extractStringsFromNodes(node.nodes);
1242
- if (nested) parts.push(nested);
1243
- return parts.join("\n");
1244
- }).filter(Boolean).join("\n");
1245
- }
1246
- /**
1247
- * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
1248
- *
1249
- * Returns `undefined` for non-ref nodes or when no name can be resolved. Use this to get a schema's
1250
- * identifier for type definitions or error messages.
1251
- *
1252
- * @example
1253
- * ```ts
1254
- * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
1255
- * // => 'Pet'
1256
- * ```
1257
- */
1258
- function resolveRefName(node) {
1259
- if (!node || node.type !== "ref") return void 0;
1260
- if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? void 0;
1261
- return node.name ?? node.schema?.name ?? void 0;
1262
- }
1263
- /**
1264
- * Collects every named schema referenced (transitively) from a node via ref edges.
1265
- *
1266
- * Refs are followed by name only — the resolved `node.schema` is not traversed inline.
1267
- * Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
1268
- *
1269
- * @note Returns a Set of schema names for efficient membership testing.
1270
- */
1271
- function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1272
- if (!node) return out;
1273
- collect(node, { schema(child) {
1274
- if (child.type === "ref") {
1275
- const name = resolveRefName(child);
1276
- if (name) out.add(name);
1277
- }
1278
- } });
1279
- return out;
1280
- }
1281
- /**
1282
- * Identifies all schemas that participate in circular dependency chains, including direct self-loops.
1283
- *
1284
- * Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
1285
- * in deferred constructs (lazy getter, `z.lazy(() => …)`) to prevent infinite recursion when generated code runs.
1286
- * Refs are followed by name only, keeping the algorithm linear in the schema graph size.
1287
- *
1288
- * @note Call this once on the full schema graph, then use `containsCircularRef()` to check individual schemas.
1289
- */
1290
- function findCircularSchemas(schemas) {
1291
- const graph = /* @__PURE__ */ new Map();
1292
- for (const schema of schemas) {
1293
- if (!schema.name) continue;
1294
- graph.set(schema.name, collectReferencedSchemaNames(schema));
1295
- }
1296
- const circular = /* @__PURE__ */ new Set();
1297
- for (const start of graph.keys()) {
1298
- const visited = /* @__PURE__ */ new Set();
1299
- const stack = [...graph.get(start) ?? []];
1300
- while (stack.length > 0) {
1301
- const node = stack.pop();
1302
- if (node === start) {
1303
- circular.add(start);
1304
- break;
1305
- }
1306
- if (visited.has(node)) continue;
1307
- visited.add(node);
1308
- const next = graph.get(node);
1309
- if (next) for (const r of next) stack.push(r);
1310
- }
1311
- }
1312
- return circular;
1313
- }
1314
- /**
1315
- * Type guard returning `true` when a schema or anything nested within it contains a ref to a circular schema.
1316
- *
1317
- * Use `excludeName` to ignore refs to specific schemas (useful when self-references are handled separately).
1318
- * Commonly used with `findCircularSchemas()` to detect where lazy wrappers are needed in code generation.
1319
- *
1320
- * @note Returns `true` for the first matching circular ref found; use for fast dependency checks.
1321
- */
1322
- function containsCircularRef(node, { circularSchemas, excludeName }) {
1323
- if (!node || circularSchemas.size === 0) return false;
1324
- return collect(node, { schema(child) {
1325
- if (child.type !== "ref") return void 0;
1326
- const name = resolveRefName(child);
1327
- return name && name !== excludeName && circularSchemas.has(name) ? true : void 0;
1328
- } }).length > 0;
1329
- }
1330
- //#endregion
1331
- //#region src/factory.ts
1332
- /**
1333
- * Syncs property/parameter schema optionality flags from `required` and `schema.nullable`.
1334
- *
1335
- * - `optional` is set for non-required, non-nullable schemas.
1336
- * - `nullish` is set for non-required, nullable schemas.
1337
- */
1338
- function syncOptionality(schema, required) {
1339
- const nullable = schema.nullable ?? false;
1340
- return {
1341
- ...schema,
1342
- optional: !required && !nullable ? true : void 0,
1343
- nullish: !required && nullable ? true : void 0
1344
- };
1345
- }
1346
- /**
1347
- * Creates an `InputNode` with stable defaults for `schemas` and `operations`.
1348
- *
1349
- * @example
1350
- * ```ts
1351
- * const input = createInput()
1352
- * // { kind: 'Input', schemas: [], operations: [] }
1353
- * ```
1354
- *
1355
- * @example
1356
- * ```ts
1357
- * const input = createInput({ schemas: [petSchema] })
1358
- * // keeps default operations: []
1359
- * ```
1360
- */
1361
- function createInput(overrides = {}) {
1362
- return {
1363
- schemas: [],
1364
- operations: [],
1365
- ...overrides,
1366
- kind: "Input"
1367
- };
1368
- }
1369
- /**
1370
- * Creates an `OutputNode` with a stable default for `files`.
1371
- *
1372
- * @example
1373
- * ```ts
1374
- * const output = createOutput()
1375
- * // { kind: 'Output', files: [] }
1376
- * ```
1377
- *
1378
- * @example
1379
- * ```ts
1380
- * const output = createOutput({ files: [petFile] })
1381
- * ```
1382
- */
1383
- function createOutput(overrides = {}) {
1384
- return {
1385
- files: [],
1386
- ...overrides,
1387
- kind: "Output"
1388
- };
1389
- }
1390
- /**
1391
- * Creates an `OperationNode` with default empty arrays for `tags`, `parameters`, and `responses`.
1392
- *
1393
- * @example
1394
- * ```ts
1395
- * const operation = createOperation({
1396
- * operationId: 'getPetById',
1397
- * method: 'GET',
1398
- * path: '/pet/{petId}',
1399
- * })
1400
- * // tags, parameters, and responses are []
1401
- * ```
1402
- *
1403
- * @example
1404
- * ```ts
1405
- * const operation = createOperation({
1406
- * operationId: 'findPets',
1407
- * method: 'GET',
1408
- * path: '/pet/findByStatus',
1409
- * tags: ['pet'],
1410
- * })
1411
- * ```
1412
- */
1413
- function createOperation(props) {
1414
- return {
1415
- tags: [],
1416
- parameters: [],
1417
- responses: [],
1418
- ...props,
1419
- kind: "Operation"
1420
- };
1421
- }
1422
- /**
1423
- * Maps schema `type` to its underlying `primitive`.
1424
- * Primitive types map to themselves; special string formats map to `'string'`.
1425
- * Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
1426
- */
1427
- const TYPE_TO_PRIMITIVE = {
1428
- string: "string",
1429
- number: "number",
1430
- integer: "integer",
1431
- bigint: "bigint",
1432
- boolean: "boolean",
1433
- null: "null",
1434
- any: "any",
1435
- unknown: "unknown",
1436
- void: "void",
1437
- never: "never",
1438
- object: "object",
1439
- array: "array",
1440
- date: "date",
1441
- uuid: "string",
1442
- email: "string",
1443
- url: "string",
1444
- datetime: "string",
1445
- time: "string"
1446
- };
1447
- function createSchema(props) {
1448
- const inferredPrimitive = TYPE_TO_PRIMITIVE[props.type];
1449
- if (props["type"] === "object") return {
1450
- properties: [],
1451
- primitive: "object",
1452
- ...props,
1453
- kind: "Schema"
1454
- };
1455
- return {
1456
- primitive: inferredPrimitive,
1457
- ...props,
1458
- kind: "Schema"
1459
- };
1460
- }
1461
- /**
1462
- * Creates a `PropertyNode`.
1463
- *
1464
- * `required` defaults to `false`.
1465
- * `schema.optional` and `schema.nullish` are derived from `required` and `schema.nullable`.
1466
- *
1467
- * @example
1468
- * ```ts
1469
- * const property = createProperty({
1470
- * name: 'status',
1471
- * schema: createSchema({ type: 'string' }),
1472
- * })
1473
- * // required=false, schema.optional=true
1474
- * ```
1475
- *
1476
- * @example
1477
- * ```ts
1478
- * const property = createProperty({
1479
- * name: 'status',
1480
- * required: true,
1481
- * schema: createSchema({ type: 'string', nullable: true }),
1482
- * })
1483
- * // required=true, no optional/nullish
1484
- * ```
1485
- */
1486
- function createProperty(props) {
1487
- const required = props.required ?? false;
1488
- return {
1489
- ...props,
1490
- kind: "Property",
1491
- required,
1492
- schema: syncOptionality(props.schema, required)
1493
- };
1494
- }
1495
- /**
1496
- * Creates a `ParameterNode`.
1497
- *
1498
- * `required` defaults to `false`.
1499
- * Nested schema flags are set from `required` and `schema.nullable`.
1500
- *
1501
- * @example
1502
- * ```ts
1503
- * const param = createParameter({
1504
- * name: 'petId',
1505
- * in: 'path',
1506
- * required: true,
1507
- * schema: createSchema({ type: 'string' }),
1508
- * })
1509
- * ```
1510
- *
1511
- * @example
1512
- * ```ts
1513
- * const param = createParameter({
1514
- * name: 'status',
1515
- * in: 'query',
1516
- * schema: createSchema({ type: 'string', nullable: true }),
1517
- * })
1518
- * // required=false, schema.nullish=true
1519
- * ```
1520
- */
1521
- function createParameter(props) {
1522
- const required = props.required ?? false;
1523
- return {
1524
- ...props,
1525
- kind: "Parameter",
1526
- required,
1527
- schema: syncOptionality(props.schema, required)
1528
- };
1529
- }
1530
- /**
1531
- * Creates a `ResponseNode`.
1532
- *
1533
- * @example
1534
- * ```ts
1535
- * const response = createResponse({
1536
- * statusCode: '200',
1537
- * description: 'Success',
1538
- * schema: createSchema({ type: 'object', properties: [] }),
1539
- * })
1540
- * ```
1541
- */
1542
- function createResponse(props) {
1543
- return {
1544
- ...props,
1545
- kind: "Response"
1546
- };
1547
- }
1548
- /**
1549
- * Creates a `FunctionParameterNode`.
1550
- *
1551
- * `optional` defaults to `false`.
1552
- *
1553
- * @example Required typed param
1554
- * ```ts
1555
- * createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }) })
1556
- * // → petId: string
1557
- * ```
1558
- *
1559
- * @example Optional param
1560
- * ```ts
1561
- * createFunctionParameter({ name: 'params', type: createParamsType({ variant: 'reference', name: 'QueryParams' }), optional: true })
1562
- * // → params?: QueryParams
1563
- * ```
1564
- *
1565
- * @example Param with default (implicitly optional; cannot combine with `optional: true`)
1566
- * ```ts
1567
- * createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), default: '{}' })
1568
- * // → config: RequestConfig = {}
1569
- * ```
1570
- */
1571
- function createFunctionParameter(props) {
1572
- return {
1573
- optional: false,
1574
- ...props,
1575
- kind: "FunctionParameter"
1576
- };
1577
- }
1578
- /**
1579
- * Creates a {@link TypeNode} representing a language-agnostic structured type expression.
1580
- *
1581
- * Use `variant: 'struct'` for inline anonymous types and `variant: 'member'` for a single
1582
- * named field accessed from a group type. Each language's printer renders the variant
1583
- * into its own syntax (TypeScript, Python, C#, Kotlin, …).
1584
- *
1585
- * @example Reference type (TypeScript: `QueryParams`)
1586
- * ```ts
1587
- * createParamsType({ variant: 'reference', name: 'QueryParams' })
1588
- * ```
1589
- *
1590
- * @example Struct type (TypeScript: `{ petId: string }`)
1591
- * ```ts
1592
- * createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
1593
- * ```
1594
- *
1595
- * @example Member type (TypeScript: `DeletePetPathParams['petId']`)
1596
- * ```ts
1597
- * createParamsType({ variant: 'member', base: 'DeletePetPathParams', key: 'petId' })
1598
- * ```
1599
- */
1600
- function createParamsType(props) {
1601
- return {
1602
- ...props,
1603
- kind: "ParamsType"
1604
- };
1605
- }
1606
- /**
1607
- * Creates a `ParameterGroupNode` representing a group of related parameters treated as a unit.
1608
- *
1609
- * @example Grouped param (TypeScript declaration)
1610
- * ```ts
1611
- * createParameterGroup({
1612
- * properties: [
1613
- * createFunctionParameter({ name: 'id', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
1614
- * createFunctionParameter({ name: 'name', type: createParamsType({ variant: 'reference', name: 'string' }), optional: true }),
1615
- * ],
1616
- * default: '{}',
1617
- * })
1618
- * // declaration → { id, name? }: { id: string; name?: string } = {}
1619
- * // call → { id, name }
1620
- * ```
1621
- *
1622
- * @example Inline (spread) — children emitted as individual top-level parameters
1623
- * ```ts
1624
- * createParameterGroup({
1625
- * properties: [createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false })],
1626
- * inline: true,
1627
- * })
1628
- * // declaration → petId: string
1629
- * // call → petId
1630
- * ```
1631
- */
1632
- function createParameterGroup(props) {
1633
- return {
1634
- ...props,
1635
- kind: "ParameterGroup"
1636
- };
1637
- }
1638
- /**
1639
- * Creates a `FunctionParametersNode` from an ordered list of parameters.
1640
- *
1641
- * @example
1642
- * ```ts
1643
- * createFunctionParameters({
1644
- * params: [
1645
- * createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
1646
- * createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), optional: false, default: '{}' }),
1647
- * ],
1648
- * })
1649
- * ```
1650
- *
1651
- * @example
1652
- * ```ts
1653
- * const empty = createFunctionParameters()
1654
- * // { kind: 'FunctionParameters', params: [] }
1655
- * ```
1656
- */
1657
- function createFunctionParameters(props = {}) {
1658
- return {
1659
- params: [],
1660
- ...props,
1661
- kind: "FunctionParameters"
1662
- };
1663
- }
1664
- /**
1665
- * Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
1666
- *
1667
- * @example Named import
1668
- * ```ts
1669
- * createImport({ name: ['useState'], path: 'react' })
1670
- * // import { useState } from 'react'
1671
- * ```
1672
- *
1673
- * @example Type-only import
1674
- * ```ts
1675
- * createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
1676
- * // import type { FC } from 'react'
1677
- * ```
1678
- */
1679
- function createImport(props) {
1680
- return {
1681
- ...props,
1682
- kind: "Import"
1683
- };
1684
- }
1685
- /**
1686
- * Creates an `ExportNode` representing a language-agnostic export/public API declaration.
1687
- *
1688
- * @example Named export
1689
- * ```ts
1690
- * createExport({ name: ['Pet'], path: './Pet' })
1691
- * // export { Pet } from './Pet'
1692
- * ```
1693
- *
1694
- * @example Wildcard export
1695
- * ```ts
1696
- * createExport({ path: './utils' })
1697
- * // export * from './utils'
1698
- * ```
1699
- */
1700
- function createExport(props) {
1701
- return {
1702
- ...props,
1703
- kind: "Export"
1704
- };
1705
- }
1706
- /**
1707
- * Creates a `SourceNode` representing a fragment of source code within a file.
1708
- *
1709
- * @example
1710
- * ```ts
1711
- * createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
1712
- * ```
1713
- */
1714
- function createSource(props) {
1715
- return {
1716
- ...props,
1717
- kind: "Source"
1718
- };
1719
- }
2
+ const require_visitor = require("./visitor-CDa9Cn6x.cjs");
3
+ const require_defineMacro = require("./defineMacro-C58x6uaa.cjs");
4
+ //#region src/defineDialect.ts
1720
5
  /**
1721
- * Creates a fully resolved `FileNode` from a file input descriptor.
1722
- *
1723
- * Computes:
1724
- * - `id` — SHA256 hash of the file path
1725
- * - `name` — `baseName` without extension
1726
- * - `extname` — extension extracted from `baseName`
1727
- *
1728
- * Deduplicates:
1729
- * - `sources` via `combineSources`
1730
- * - `exports` via `combineExports`
1731
- * - `imports` via `combineImports` (also filters unused imports)
1732
- *
1733
- * @throws {Error} when `baseName` has no extension.
6
+ * Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the
7
+ * dialect's type for inference.
1734
8
  *
1735
9
  * @example
1736
10
  * ```ts
1737
- * const file = createFile({
1738
- * baseName: 'petStore.ts',
1739
- * path: 'src/models/petStore.ts',
1740
- * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
1741
- * imports: [createImport({ name: ['z'], path: 'zod' })],
1742
- * exports: [createExport({ name: ['Pet'], path: './petStore' })],
1743
- * })
1744
- * // file.id = SHA256 hash of 'src/models/petStore.ts'
1745
- * // file.name = 'petStore'
1746
- * // file.extname = '.ts'
1747
- * ```
1748
- */
1749
- function createFile(input) {
1750
- const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
1751
- if (!extname) throw new Error(`No extname found for ${input.baseName}`);
1752
- const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
1753
- const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
1754
- const resolvedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
1755
- const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
1756
- return {
1757
- kind: "File",
1758
- ...input,
1759
- id: (0, node_crypto.createHash)("sha256").update(input.path).digest("hex"),
1760
- name: trimExtName(input.baseName),
1761
- extname,
1762
- imports: resolvedImports,
1763
- exports: resolvedExports,
1764
- sources: resolvedSources,
1765
- meta: input.meta ?? {}
1766
- };
1767
- }
1768
- /**
1769
- * Creates a `ConstNode` representing a TypeScript `const` declaration.
1770
- *
1771
- * Mirrors the `Const` component from `@kubb/renderer-jsx`.
1772
- * The component's `children` are represented as `nodes`.
1773
- *
1774
- * @example Simple constant
1775
- * ```ts
1776
- * createConst({ name: 'pet' })
1777
- * // const pet = ...
1778
- * ```
1779
- *
1780
- * @example Exported constant with type and `as const`
1781
- * ```ts
1782
- * createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
1783
- * // export const pets: Pet[] = ... as const
1784
- * ```
1785
- *
1786
- * @example With JSDoc and child nodes
1787
- * ```ts
1788
- * createConst({
1789
- * name: 'config',
1790
- * export: true,
1791
- * JSDoc: { comments: ['@description App configuration'] },
1792
- * nodes: [],
1793
- * })
1794
- * ```
1795
- */
1796
- function createConst(props) {
1797
- return {
1798
- ...props,
1799
- kind: "Const"
1800
- };
1801
- }
1802
- /**
1803
- * Creates a `TypeNode` representing a TypeScript `type` alias declaration.
1804
- *
1805
- * Mirrors the `Type` component from `@kubb/renderer-jsx`.
1806
- * The component's `children` are represented as `nodes`.
1807
- *
1808
- * @example Simple type alias
1809
- * ```ts
1810
- * createType({ name: 'Pet' })
1811
- * // type Pet = ...
1812
- * ```
1813
- *
1814
- * @example Exported type with JSDoc
1815
- * ```ts
1816
- * createType({
1817
- * name: 'PetStatus',
1818
- * export: true,
1819
- * JSDoc: { comments: ['@description Status of a pet'] },
1820
- * })
1821
- * // export type PetStatus = ...
1822
- * ```
1823
- */
1824
- function createType(props) {
1825
- return {
1826
- ...props,
1827
- kind: "Type"
1828
- };
1829
- }
1830
- /**
1831
- * Creates a `FunctionNode` representing a TypeScript `function` declaration.
1832
- *
1833
- * Mirrors the `Function` component from `@kubb/renderer-jsx`.
1834
- * The component's `children` are represented as `nodes`.
1835
- *
1836
- * @example Simple function
1837
- * ```ts
1838
- * createFunction({ name: 'getPet' })
1839
- * // function getPet() { ... }
1840
- * ```
1841
- *
1842
- * @example Exported async function with return type
1843
- * ```ts
1844
- * createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
1845
- * // export async function fetchPet(): Promise<Pet> { ... }
1846
- * ```
1847
- *
1848
- * @example Function with generics and params
1849
- * ```ts
1850
- * createFunction({
1851
- * name: 'identity',
1852
- * export: true,
1853
- * generics: ['T'],
1854
- * params: 'value: T',
1855
- * returnType: 'T',
1856
- * })
1857
- * // export function identity<T>(value: T): T { ... }
1858
- * ```
1859
- */
1860
- function createFunction(props) {
1861
- return {
1862
- ...props,
1863
- kind: "Function"
1864
- };
1865
- }
1866
- /**
1867
- * Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
1868
- *
1869
- * Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.
1870
- * The component's `children` are represented as `nodes`.
1871
- *
1872
- * @example Simple arrow function
1873
- * ```ts
1874
- * createArrowFunction({ name: 'getPet' })
1875
- * // const getPet = () => { ... }
1876
- * ```
1877
- *
1878
- * @example Single-line exported arrow function
1879
- * ```ts
1880
- * createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
1881
- * // export const double = (n: number) => ...
1882
- * ```
1883
- *
1884
- * @example Async arrow function with generics
1885
- * ```ts
1886
- * createArrowFunction({
1887
- * name: 'fetchPet',
1888
- * export: true,
1889
- * async: true,
1890
- * generics: ['T'],
1891
- * params: 'id: string',
1892
- * returnType: 'T',
11
+ * export const oasDialect = defineDialect({
12
+ * name: 'oas',
13
+ * schema: {
14
+ * isNullable,
15
+ * isReference,
16
+ * isDiscriminator,
17
+ * isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
18
+ * resolveRef,
19
+ * },
1893
20
  * })
1894
- * // export const fetchPet = async <T>(id: string): Promise<T> => { ... }
1895
- * ```
1896
- */
1897
- function createArrowFunction(props) {
1898
- return {
1899
- ...props,
1900
- kind: "ArrowFunction"
1901
- };
1902
- }
1903
- /**
1904
- * Creates a {@link TextNode} representing a raw string fragment in the source output.
1905
- *
1906
- * Use this instead of bare strings when building `nodes` arrays so that every
1907
- * entry in the array is a typed {@link CodeNode}.
1908
- *
1909
- * @example
1910
- * ```ts
1911
- * createText('return fetch(id)')
1912
- * // { kind: 'Text', value: 'return fetch(id)' }
1913
- * ```
1914
- */
1915
- function createText(value) {
1916
- return {
1917
- value,
1918
- kind: "Text"
1919
- };
1920
- }
1921
- /**
1922
- * Creates a {@link BreakNode} representing a line break in the source output.
1923
- *
1924
- * Corresponds to `<br/>` in JSX components. Prints as an empty string which,
1925
- * when joined with `\n` by `printNodes`, produces a blank line.
1926
- *
1927
- * @example
1928
- * ```ts
1929
- * createBreak()
1930
- * // { kind: 'Break' }
1931
- * ```
1932
- */
1933
- function createBreak() {
1934
- return { kind: "Break" };
1935
- }
1936
- /**
1937
- * Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
1938
- *
1939
- * Use this to embed JSX markup (including fragments `<>…</>`) directly in generated code.
1940
- *
1941
- * @example
1942
- * ```ts
1943
- * createJsx('<>\n <a href={href}>Open</a>\n</>')
1944
- * // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
1945
21
  * ```
1946
22
  */
1947
- function createJsx(value) {
1948
- return {
1949
- value,
1950
- kind: "Jsx"
1951
- };
23
+ function defineDialect(dialect) {
24
+ return dialect;
1952
25
  }
1953
26
  //#endregion
1954
- //#region src/printer.ts
27
+ //#region src/createPrinter.ts
1955
28
  /**
1956
- * Creates a schema printer factory.
1957
- *
1958
- * This function wraps a builder and makes options optional at call sites.
29
+ * Creates a schema printer: a function that takes a `SchemaNode` and emits
30
+ * code in your target language. Each plugin that produces code from schemas
31
+ * (TypeScript types, Zod schemas, Faker factories) ships a printer built
32
+ * with this helper.
1959
33
  *
1960
34
  * The builder receives resolved options and returns:
1961
- * - `name` — a unique identifier for the printer
1962
- * - `options` — options stored on the returned printer instance
1963
- * - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
1964
- * - `print` _(optional)_ — top-level override exposed as `printer.print`
1965
- * - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
1966
- * - This keeps recursion safe and avoids self-calls
1967
35
  *
1968
- * When no `print` override is provided, `printer.print` falls back to `printer.transform` (the node-level dispatcher).
36
+ * - `name` unique identifier for the printer.
37
+ * - `options` stored on the returned printer instance.
38
+ * - `nodes` map of `SchemaType` → handler. Handlers return the rendered
39
+ * output (a string, a TypeScript AST node, ...) for that schema type.
40
+ * - `print` (optional), top-level override exposed as `printer.print`.
41
+ * Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
1969
42
  *
1970
- * @example Basic usage Zod schema printer
43
+ * Without a `print` override, `printer.print` falls back to `printer.transform`
44
+ * (the node-level dispatcher).
45
+ *
46
+ * @example Tiny Zod printer
1971
47
  * ```ts
48
+ * import { createPrinter, type PrinterFactoryOptions } from '@kubb/ast'
49
+ *
1972
50
  * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
1973
51
  *
1974
- * export const zodPrinter = definePrinter<PrinterZod>((options) => ({
52
+ * export const zodPrinter = createPrinter<PrinterZod>((options) => ({
1975
53
  * name: 'zod',
1976
54
  * options: { strict: options.strict ?? true },
1977
55
  * nodes: {
1978
56
  * string: () => 'z.string()',
1979
57
  * object(node) {
1980
- * const props = node.properties.map(p => `${p.name}: ${this.transform(p.schema)}`).join(', ')
58
+ * const props = node.properties
59
+ * .map((p) => `${p.name}: ${this.transform(p.schema)}`)
60
+ * .join(', ')
1981
61
  * return `z.object({ ${props} })`
1982
62
  * },
1983
63
  * },
1984
64
  * }))
1985
65
  * ```
1986
66
  */
1987
- function definePrinter(build) {
1988
- return createPrinterFactory((node) => node.type)(build);
1989
- }
1990
- /**
1991
- * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
1992
- **
1993
- * @example
1994
- * ```ts
1995
- * export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>(
1996
- * (node) => kindToHandlerKey[node.kind],
1997
- * )
1998
- * ```
1999
- */
2000
- function createPrinterFactory(getKey) {
2001
- return function(build) {
2002
- return (options) => {
2003
- const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? {});
2004
- const context = {
2005
- options: resolvedOptions,
2006
- transform: (node) => {
2007
- const key = getKey(node);
2008
- if (key === void 0) return null;
2009
- const handler = nodes[key];
2010
- if (!handler) return null;
2011
- return handler.call(context, node);
2012
- }
2013
- };
2014
- return {
2015
- name,
2016
- options: resolvedOptions,
2017
- transform: context.transform,
2018
- print: printOverride ? printOverride.bind(context) : context.transform
2019
- };
67
+ function createPrinter(build) {
68
+ return (options) => {
69
+ const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? {});
70
+ const context = {
71
+ options: resolvedOptions,
72
+ transform: (node) => {
73
+ const handler = nodes[node.type];
74
+ if (!handler) return null;
75
+ return handler.call(context, node);
76
+ }
77
+ };
78
+ return {
79
+ name,
80
+ options: resolvedOptions,
81
+ transform: context.transform,
82
+ print: printOverride ? printOverride.bind(context) : context.transform
2020
83
  };
2021
84
  };
2022
85
  }
2023
86
  //#endregion
2024
- //#region src/resolvers.ts
2025
- function findDiscriminator(mapping, ref) {
2026
- if (!mapping || !ref) return null;
2027
- return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null;
2028
- }
2029
- function childName(parentName, propName) {
2030
- return parentName ? pascalCase([parentName, propName].join(" ")) : null;
2031
- }
2032
- function enumPropName(parentName, propName, enumSuffix) {
2033
- return pascalCase([
2034
- parentName,
2035
- propName,
2036
- enumSuffix
2037
- ].filter(Boolean).join(" "));
2038
- }
2039
- /**
2040
- * Collects import entries for all `ref` schema nodes in `node`.
2041
- */
2042
- function collectImports({ node, nameMapping, resolve }) {
2043
- return collect(node, { schema(schemaNode) {
2044
- const schemaRef = narrowSchema(schemaNode, "ref");
2045
- if (!schemaRef?.ref) return;
2046
- const rawName = extractRefName(schemaRef.ref);
2047
- const result = resolve(nameMapping.get(rawName) ?? rawName);
2048
- if (!result) return;
2049
- return result;
2050
- } });
87
+ //#region src/factory.ts
88
+ var factory_exports = /* @__PURE__ */ require_visitor.__exportAll({
89
+ createArrowFunction: () => require_visitor.createArrowFunction,
90
+ createBreak: () => require_visitor.createBreak,
91
+ createConst: () => require_visitor.createConst,
92
+ createContent: () => require_visitor.createContent,
93
+ createExport: () => require_visitor.createExport,
94
+ createFile: () => require_visitor.createFile,
95
+ createFunction: () => require_visitor.createFunction,
96
+ createImport: () => require_visitor.createImport,
97
+ createInput: () => require_visitor.createInput,
98
+ createJsx: () => require_visitor.createJsx,
99
+ createOperation: () => require_visitor.createOperation,
100
+ createOutput: () => require_visitor.createOutput,
101
+ createParameter: () => require_visitor.createParameter,
102
+ createProperty: () => require_visitor.createProperty,
103
+ createRequestBody: () => require_visitor.createRequestBody,
104
+ createResponse: () => require_visitor.createResponse,
105
+ createSchema: () => require_visitor.createSchema,
106
+ createSource: () => require_visitor.createSource,
107
+ createText: () => require_visitor.createText,
108
+ createType: () => require_visitor.createType,
109
+ update: () => update
110
+ });
111
+ /**
112
+ * Identity-preserving node update: returns `node` unchanged when every field in
113
+ * `changes` already equals (by reference) the current value, otherwise a new node
114
+ * with the changes applied.
115
+ *
116
+ * Mirrors the TypeScript compiler's `factory.updateX` contract. Pair it with the
117
+ * structural sharing in {@link transform} so a no-op rewrite does not allocate and
118
+ * downstream passes can detect "nothing changed" by identity. Comparison is shallow,
119
+ * so a structurally equal but newly allocated array or object counts as a change.
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * update(node, { name: node.name }) // -> same `node` reference
124
+ * update(node, { name: 'renamed' }) // -> new node, `name` replaced
125
+ * ```
126
+ */
127
+ function update(node, changes) {
128
+ for (const key in changes) if (changes[key] !== node[key]) return {
129
+ ...node,
130
+ ...changes
131
+ };
132
+ return node;
2051
133
  }
2052
134
  //#endregion
2053
- //#region src/transformers.ts
2054
- /**
2055
- * Replaces a discriminator property's schema with a string enum of allowed values.
2056
- *
2057
- * If `node` is not an object schema, or if the property does not exist, the input
2058
- * node is returned as-is.
2059
- *
2060
- * @example
2061
- * ```ts
2062
- * const schema = createSchema({
2063
- * type: 'object',
2064
- * properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
2065
- * })
2066
- * const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
2067
- * ```
2068
- */
2069
- function setDiscriminatorEnum({ node, propertyName, values, enumName }) {
2070
- const objectNode = narrowSchema(node, "object");
2071
- if (!objectNode?.properties?.length) return node;
2072
- if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node;
2073
- return createSchema({
2074
- ...objectNode,
2075
- properties: objectNode.properties.map((prop) => {
2076
- if (prop.name !== propertyName) return prop;
2077
- return createProperty({
2078
- ...prop,
2079
- schema: createSchema({
2080
- type: "enum",
2081
- primitive: "string",
2082
- enumValues: values,
2083
- name: enumName,
2084
- readOnly: prop.schema.readOnly,
2085
- writeOnly: prop.schema.writeOnly
2086
- })
2087
- });
2088
- })
2089
- });
2090
- }
2091
- /**
2092
- * Merges adjacent anonymous object members into a single anonymous object member.
2093
- *
2094
- * @example
2095
- * ```ts
2096
- * const merged = mergeAdjacentObjects([
2097
- * createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
2098
- * createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
2099
- * ])
2100
- * ```
2101
- */
2102
- function mergeAdjacentObjects(members) {
2103
- return members.reduce((acc, member) => {
2104
- const objectMember = narrowSchema(member, "object");
2105
- if (objectMember && !objectMember.name) {
2106
- const previous = acc.at(-1);
2107
- const previousObject = previous ? narrowSchema(previous, "object") : void 0;
2108
- if (previousObject && !previousObject.name) {
2109
- acc[acc.length - 1] = createSchema({
2110
- ...previousObject,
2111
- properties: [...previousObject.properties ?? [], ...objectMember.properties ?? []]
2112
- });
2113
- return acc;
2114
- }
2115
- }
2116
- acc.push(member);
2117
- return acc;
2118
- }, []);
2119
- }
2120
- /**
2121
- * Removes enum members that are covered by broader scalar primitives in the same union.
2122
- *
2123
- * @example
2124
- * ```ts
2125
- * const simplified = simplifyUnion([
2126
- * createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
2127
- * createSchema({ type: 'string' }),
2128
- * ])
2129
- * // keeps only string member
2130
- * ```
2131
- */
2132
- function simplifyUnion(members) {
2133
- const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
2134
- if (!scalarPrimitives.size) return members;
2135
- return members.filter((member) => {
2136
- const enumNode = narrowSchema(member, "enum");
2137
- if (!enumNode) return true;
2138
- const primitive = enumNode.primitive;
2139
- if (!primitive) return true;
2140
- if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
2141
- if (scalarPrimitives.has(primitive)) return false;
2142
- if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
2143
- return true;
2144
- });
2145
- }
2146
- function setEnumName(propNode, parentName, propName, enumSuffix) {
2147
- const enumNode = narrowSchema(propNode, "enum");
2148
- if (enumNode?.primitive === "boolean") return {
2149
- ...propNode,
2150
- name: void 0
2151
- };
2152
- if (enumNode) return {
2153
- ...propNode,
2154
- name: enumPropName(parentName, propName, enumSuffix)
2155
- };
2156
- return propNode;
2157
- }
135
+ //#region src/exports.ts
136
+ var exports_exports = /* @__PURE__ */ require_visitor.__exportAll({
137
+ applyMacros: () => require_defineMacro.applyMacros,
138
+ arrowFunctionDef: () => require_visitor.arrowFunctionDef,
139
+ breakDef: () => require_visitor.breakDef,
140
+ collect: () => require_visitor.collect,
141
+ composeMacros: () => require_defineMacro.composeMacros,
142
+ constDef: () => require_visitor.constDef,
143
+ contentDef: () => require_visitor.contentDef,
144
+ createPrinter: () => createPrinter,
145
+ defineDialect: () => defineDialect,
146
+ defineMacro: () => require_defineMacro.defineMacro,
147
+ defineNode: () => require_visitor.defineNode,
148
+ exportDef: () => require_visitor.exportDef,
149
+ factory: () => factory_exports,
150
+ fileDef: () => require_visitor.fileDef,
151
+ functionDef: () => require_visitor.functionDef,
152
+ importDef: () => require_visitor.importDef,
153
+ inputDef: () => require_visitor.inputDef,
154
+ isHttpOperationNode: () => require_visitor.isHttpOperationNode,
155
+ jsxDef: () => require_visitor.jsxDef,
156
+ narrowSchema: () => require_visitor.narrowSchema,
157
+ nodeDefs: () => require_visitor.nodeDefs,
158
+ operationDef: () => require_visitor.operationDef,
159
+ optionality: () => require_visitor.optionality,
160
+ outputDef: () => require_visitor.outputDef,
161
+ parameterDef: () => require_visitor.parameterDef,
162
+ propertyDef: () => require_visitor.propertyDef,
163
+ requestBodyDef: () => require_visitor.requestBodyDef,
164
+ responseDef: () => require_visitor.responseDef,
165
+ schemaDef: () => require_visitor.schemaDef,
166
+ schemaTypes: () => require_visitor.schemaTypes,
167
+ sourceDef: () => require_visitor.sourceDef,
168
+ textDef: () => require_visitor.textDef,
169
+ transform: () => require_visitor.transform,
170
+ typeDef: () => require_visitor.typeDef,
171
+ walk: () => require_visitor.walk
172
+ });
2158
173
  //#endregion
2159
- exports.caseParams = caseParams;
2160
- exports.childName = childName;
2161
- exports.collect = collect;
2162
- exports.collectImports = collectImports;
2163
- exports.collectReferencedSchemaNames = collectReferencedSchemaNames;
2164
- exports.containsCircularRef = containsCircularRef;
2165
- exports.createArrowFunction = createArrowFunction;
2166
- exports.createBreak = createBreak;
2167
- exports.createConst = createConst;
2168
- exports.createDiscriminantNode = createDiscriminantNode;
2169
- exports.createExport = createExport;
2170
- exports.createFile = createFile;
2171
- exports.createFunction = createFunction;
2172
- exports.createFunctionParameter = createFunctionParameter;
2173
- exports.createFunctionParameters = createFunctionParameters;
2174
- exports.createImport = createImport;
2175
- exports.createInput = createInput;
2176
- exports.createJsx = createJsx;
2177
- exports.createOperation = createOperation;
2178
- exports.createOperationParams = createOperationParams;
2179
- exports.createOutput = createOutput;
2180
- exports.createParameter = createParameter;
2181
- exports.createParameterGroup = createParameterGroup;
2182
- exports.createParamsType = createParamsType;
2183
- exports.createPrinterFactory = createPrinterFactory;
2184
- exports.createProperty = createProperty;
2185
- exports.createResponse = createResponse;
2186
- exports.createSchema = createSchema;
2187
- exports.createSource = createSource;
2188
- exports.createText = createText;
2189
- exports.createType = createType;
2190
- exports.definePrinter = definePrinter;
2191
- exports.enumPropName = enumPropName;
2192
- exports.extractRefName = extractRefName;
2193
- exports.extractStringsFromNodes = extractStringsFromNodes;
2194
- exports.findCircularSchemas = findCircularSchemas;
2195
- exports.findDiscriminator = findDiscriminator;
2196
- exports.httpMethods = httpMethods;
2197
- exports.isInputNode = isInputNode;
2198
- exports.isOperationNode = isOperationNode;
2199
- exports.isOutputNode = isOutputNode;
2200
- exports.isScalarPrimitive = isScalarPrimitive;
2201
- exports.isSchemaNode = isSchemaNode;
2202
- exports.isStringType = isStringType;
2203
- exports.mediaTypes = mediaTypes;
2204
- exports.mergeAdjacentObjects = mergeAdjacentObjects;
2205
- exports.narrowSchema = narrowSchema;
2206
- exports.nodeKinds = nodeKinds;
2207
- exports.resolveRefName = resolveRefName;
2208
- exports.schemaTypes = schemaTypes;
2209
- exports.setDiscriminatorEnum = setDiscriminatorEnum;
2210
- exports.setEnumName = setEnumName;
2211
- exports.simplifyUnion = simplifyUnion;
2212
- exports.syncOptionality = syncOptionality;
2213
- exports.syncSchemaRef = syncSchemaRef;
2214
- exports.transform = transform;
2215
- exports.walk = walk;
174
+ exports.applyMacros = require_defineMacro.applyMacros;
175
+ exports.arrowFunctionDef = require_visitor.arrowFunctionDef;
176
+ Object.defineProperty(exports, "ast", {
177
+ enumerable: true,
178
+ get: function() {
179
+ return exports_exports;
180
+ }
181
+ });
182
+ exports.breakDef = require_visitor.breakDef;
183
+ exports.collect = require_visitor.collect;
184
+ exports.composeMacros = require_defineMacro.composeMacros;
185
+ exports.constDef = require_visitor.constDef;
186
+ exports.contentDef = require_visitor.contentDef;
187
+ exports.createPrinter = createPrinter;
188
+ exports.defineDialect = defineDialect;
189
+ exports.defineMacro = require_defineMacro.defineMacro;
190
+ exports.defineNode = require_visitor.defineNode;
191
+ exports.exportDef = require_visitor.exportDef;
192
+ Object.defineProperty(exports, "factory", {
193
+ enumerable: true,
194
+ get: function() {
195
+ return factory_exports;
196
+ }
197
+ });
198
+ exports.fileDef = require_visitor.fileDef;
199
+ exports.functionDef = require_visitor.functionDef;
200
+ exports.importDef = require_visitor.importDef;
201
+ exports.inputDef = require_visitor.inputDef;
202
+ exports.isHttpOperationNode = require_visitor.isHttpOperationNode;
203
+ exports.jsxDef = require_visitor.jsxDef;
204
+ exports.narrowSchema = require_visitor.narrowSchema;
205
+ exports.nodeDefs = require_visitor.nodeDefs;
206
+ exports.operationDef = require_visitor.operationDef;
207
+ exports.optionality = require_visitor.optionality;
208
+ exports.outputDef = require_visitor.outputDef;
209
+ exports.parameterDef = require_visitor.parameterDef;
210
+ exports.propertyDef = require_visitor.propertyDef;
211
+ exports.requestBodyDef = require_visitor.requestBodyDef;
212
+ exports.responseDef = require_visitor.responseDef;
213
+ exports.schemaDef = require_visitor.schemaDef;
214
+ exports.schemaTypes = require_visitor.schemaTypes;
215
+ exports.sourceDef = require_visitor.sourceDef;
216
+ exports.textDef = require_visitor.textDef;
217
+ exports.transform = require_visitor.transform;
218
+ exports.typeDef = require_visitor.typeDef;
219
+ exports.walk = require_visitor.walk;
2216
220
 
2217
221
  //# sourceMappingURL=index.cjs.map