@kubb/ast 5.0.0-alpha.9 → 5.0.0-beta.10

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