@kubb/ast 5.0.0-alpha.8 → 5.0.0-beta.75

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