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

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