@kubb/ast 5.0.0-alpha.6 → 5.0.0-alpha.61

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