@kubb/ast 5.0.0-alpha.15 → 5.0.0-alpha.17

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.
@@ -2,236 +2,498 @@ import type { BaseNode } from './base.ts'
2
2
  import type { PropertyNode } from './property.ts'
3
3
 
4
4
  export type PrimitiveSchemaType =
5
+ /**
6
+ * Text value.
7
+ */
5
8
  | 'string'
9
+ /**
10
+ * Floating-point number.
11
+ */
6
12
  | 'number'
13
+ /**
14
+ * Integer number.
15
+ */
7
16
  | 'integer'
17
+ /**
18
+ * Big integer number.
19
+ */
8
20
  | 'bigint'
21
+ /**
22
+ * Boolean value.
23
+ */
9
24
  | 'boolean'
25
+ /**
26
+ * Null value.
27
+ */
10
28
  | 'null'
29
+ /**
30
+ * Any value.
31
+ */
11
32
  | 'any'
33
+ /**
34
+ * Unknown value.
35
+ */
12
36
  | 'unknown'
37
+ /**
38
+ * No value (`void`).
39
+ */
13
40
  | 'void'
41
+ /**
42
+ * Never value.
43
+ */
14
44
  | 'never'
45
+ /**
46
+ * Object value.
47
+ */
15
48
  | 'object'
49
+ /**
50
+ * Array value.
51
+ */
16
52
  | 'array'
53
+ /**
54
+ * Date value.
55
+ */
17
56
  | 'date'
18
57
 
58
+ /**
59
+ * Composite schema types.
60
+ */
19
61
  export type ComplexSchemaType = 'tuple' | 'union' | 'intersection' | 'enum'
20
62
 
21
63
  /**
22
- * Semantic types requiring special handling in code generation (e.g. generating a `Date` object or a branded type).
64
+ * Schema types that need special handling in generators.
23
65
  */
24
66
  export type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'blob'
25
67
 
68
+ /**
69
+ * All schema type strings.
70
+ */
26
71
  export type SchemaType = PrimitiveSchemaType | ComplexSchemaType | SpecialSchemaType
27
72
 
73
+ /**
74
+ * Scalar schema types without extra object/array/ref structure.
75
+ */
28
76
  export type ScalarSchemaType = Exclude<
29
77
  SchemaType,
30
78
  'object' | 'array' | 'tuple' | 'union' | 'intersection' | 'enum' | 'ref' | 'datetime' | 'date' | 'time' | 'string' | 'number' | 'integer' | 'bigint' | 'url'
31
79
  >
32
80
 
33
81
  /**
34
- * Base fields shared by every schema variant. Does not include spec-specific fields.
82
+ * Fields shared by all schema nodes.
35
83
  */
36
84
  type SchemaNodeBase = BaseNode & {
85
+ /**
86
+ * Node kind.
87
+ */
37
88
  kind: 'Schema'
38
89
  /**
39
- * Named schema identifier (e.g. `"Pet"` from `#/components/schemas/Pet`). `undefined` for inline schemas.
90
+ * Schema name for named definitions (for example, `"Pet"`).
91
+ * Inline schemas omit this field.
40
92
  */
41
93
  name?: string
94
+ /**
95
+ * Short schema title.
96
+ */
42
97
  title?: string
98
+ /**
99
+ * Schema description text.
100
+ */
43
101
  description?: string
102
+ /**
103
+ * Whether `null` is allowed.
104
+ */
44
105
  nullable?: boolean
106
+ /**
107
+ * Whether the field is optional.
108
+ */
45
109
  optional?: boolean
46
110
  /**
47
111
  * Both optional and nullable (`optional` + `nullable`).
48
112
  */
49
113
  nullish?: boolean
114
+ /**
115
+ * Whether the schema is deprecated.
116
+ */
50
117
  deprecated?: boolean
118
+ /**
119
+ * Whether the schema is read-only.
120
+ */
51
121
  readOnly?: boolean
122
+ /**
123
+ * Whether the schema is write-only.
124
+ */
52
125
  writeOnly?: boolean
126
+ /**
127
+ * Default value.
128
+ */
53
129
  default?: unknown
130
+ /**
131
+ * Example value.
132
+ */
54
133
  example?: unknown
55
134
  /**
56
- * Underlying primitive before format/semantic promotion (e.g. `'string'` for a `uuid` node).
135
+ * Base primitive type.
136
+ * For example, this is `'string'` for a `uuid` schema.
57
137
  */
58
138
  primitive?: PrimitiveSchemaType
59
139
  }
60
140
 
61
141
  /**
62
- * Object schema with ordered property definitions.
142
+ * Object schema with ordered properties.
143
+ *
144
+ * @example
145
+ * ```ts
146
+ * const objectSchema: ObjectSchemaNode = {
147
+ * kind: 'Schema',
148
+ * type: 'object',
149
+ * properties: [],
150
+ * }
151
+ * ```
63
152
  */
64
153
  export type ObjectSchemaNode = SchemaNodeBase & {
154
+ /**
155
+ * Schema type discriminator.
156
+ */
65
157
  type: 'object'
158
+ /**
159
+ * Ordered object properties.
160
+ */
66
161
  properties: Array<PropertyNode>
67
162
  /**
68
- * `true` allows any value; a `SchemaNode` constrains it; absent means not permitted.
163
+ * Additional object properties behavior:
164
+ * - `true`: allow any value
165
+ * - `SchemaNode`: allow values that match that schema
166
+ * - `undefined`: no additional properties
69
167
  */
70
168
  additionalProperties?: SchemaNode | true
169
+ /**
170
+ * Pattern-based property schemas.
171
+ */
71
172
  patternProperties?: Record<string, SchemaNode>
72
173
  }
73
174
 
74
175
  /**
75
- * Array or tuple schema.
176
+ * Array-like schema (`array` or `tuple`).
177
+ *
178
+ * @example
179
+ * ```ts
180
+ * const arraySchema: ArraySchemaNode = {
181
+ * kind: 'Schema',
182
+ * type: 'array',
183
+ * items: [],
184
+ * }
185
+ * ```
76
186
  */
77
187
  export type ArraySchemaNode = SchemaNodeBase & {
188
+ /**
189
+ * Schema type discriminator (`array` or `tuple`).
190
+ */
78
191
  type: 'array' | 'tuple'
192
+ /**
193
+ * Item schemas.
194
+ */
79
195
  items?: Array<SchemaNode>
80
196
  /**
81
- * Additional items beyond positional `items` in a tuple.
197
+ * Tuple rest-item schema for elements beyond positional `items`.
82
198
  */
83
199
  rest?: SchemaNode
200
+ /**
201
+ * Minimum item count (or tuple length).
202
+ */
84
203
  min?: number
204
+ /**
205
+ * Maximum item count (or tuple length).
206
+ */
85
207
  max?: number
208
+ /**
209
+ * Whether all items must be unique.
210
+ */
86
211
  unique?: boolean
87
212
  }
88
213
 
89
214
  /**
90
- * Shared base for union and intersection schemas.
215
+ * Shared shape for union and intersection schemas.
91
216
  */
92
217
  type CompositeSchemaNodeBase = SchemaNodeBase & {
218
+ /**
219
+ * Member schemas.
220
+ */
93
221
  members?: Array<SchemaNode>
94
222
  }
95
223
 
96
224
  /**
97
- * Union schema (`oneOf` / `anyOf`).
225
+ * Union schema, often from `oneOf` or `anyOf`.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * const unionSchema: UnionSchemaNode = {
230
+ * kind: 'Schema',
231
+ * type: 'union',
232
+ * members: [],
233
+ * }
234
+ * ```
98
235
  */
99
236
  export type UnionSchemaNode = CompositeSchemaNodeBase & {
237
+ /**
238
+ * Schema type discriminator.
239
+ */
100
240
  type: 'union'
101
241
  /**
102
- * Discriminator property from OAS `discriminator.propertyName`.
242
+ * Discriminator property name from OpenAPI `discriminator.propertyName`.
103
243
  */
104
244
  discriminatorPropertyName?: string
105
245
  }
106
246
 
107
247
  /**
108
- * Intersection schema (`allOf`).
248
+ * Intersection schema, often from `allOf`.
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * const intersectionSchema: IntersectionSchemaNode = {
253
+ * kind: 'Schema',
254
+ * type: 'intersection',
255
+ * members: [],
256
+ * }
257
+ * ```
109
258
  */
110
259
  export type IntersectionSchemaNode = CompositeSchemaNodeBase & {
260
+ /**
261
+ * Schema type discriminator.
262
+ */
111
263
  type: 'intersection'
112
264
  }
113
265
 
114
266
  /**
115
- * A named enum variant.
267
+ * One named enum item.
116
268
  */
117
269
  export type EnumValueNode = {
270
+ /**
271
+ * Enum item name.
272
+ */
118
273
  name: string
274
+ /**
275
+ * Enum item value.
276
+ */
119
277
  value: string | number | boolean
120
- format: 'string' | 'number' | 'boolean'
278
+ /**
279
+ * Primitive type of the enum value.
280
+ */
281
+ primitive: Extract<PrimitiveSchemaType, 'string' | 'number' | 'boolean'>
121
282
  }
122
283
 
123
284
  /**
124
- * Enum schema.
285
+ * Enum schema node.
286
+ *
287
+ * @example
288
+ * ```ts
289
+ * const enumSchema: EnumSchemaNode = {
290
+ * kind: 'Schema',
291
+ * type: 'enum',
292
+ * enumValues: ['a', 'b'],
293
+ * }
294
+ * ```
125
295
  */
126
296
  export type EnumSchemaNode = SchemaNodeBase & {
127
- type: 'enum'
128
297
  /**
129
- * Enum member type. Generators should use const assertions for `'number'` / `'boolean'`.
298
+ * Schema type discriminator.
130
299
  */
131
- enumType?: 'string' | 'number' | 'boolean'
300
+ type: 'enum'
132
301
  /**
133
- * Allowed values (simple form).
302
+ * Enum values in simple form.
134
303
  */
135
304
  enumValues?: Array<string | number | boolean | null>
136
305
  /**
137
- * Named variants (rich form). Takes priority over `enumValues` when present.
306
+ * Enum values in named form.
307
+ * If present, this is used instead of `enumValues`.
138
308
  */
139
309
  namedEnumValues?: Array<EnumValueNode>
140
310
  }
141
311
 
142
312
  /**
143
- * Ref schema pointer to another schema definition.
313
+ * Reference schema that points to another schema definition.
314
+ *
315
+ * @example
316
+ * ```ts
317
+ * const refSchema: RefSchemaNode = {
318
+ * kind: 'Schema',
319
+ * type: 'ref',
320
+ * ref: '#/components/schemas/Pet',
321
+ * }
322
+ * ```
144
323
  */
145
324
  export type RefSchemaNode = SchemaNodeBase & {
325
+ /**
326
+ * Schema type discriminator.
327
+ */
146
328
  type: 'ref'
329
+ /**
330
+ * Referenced schema name.
331
+ */
147
332
  name?: string
148
333
  /**
149
- * Original `$ref` path (e.g. `#/components/schemas/Order`). Used for name resolution.
334
+ * Original `$ref` path, for example, `#/components/schemas/Order`.
335
+ * Used to resolve names later.
150
336
  */
151
337
  ref?: string
152
338
  /**
153
- * Pattern constraint propagated from a sibling `pattern` field next to the `$ref`.
339
+ * Pattern copied from a sibling `pattern` field.
154
340
  */
155
341
  pattern?: string
156
342
  }
157
343
 
158
344
  /**
159
345
  * Datetime schema.
346
+ *
347
+ * @example
348
+ * ```ts
349
+ * const datetimeSchema: DatetimeSchemaNode = { kind: 'Schema', type: 'datetime' }
350
+ * ```
160
351
  */
161
352
  export type DatetimeSchemaNode = SchemaNodeBase & {
353
+ /**
354
+ * Schema type discriminator.
355
+ */
162
356
  type: 'datetime'
163
357
  /**
164
- * Includes timezone offset (`dateType: 'stringOffset'`).
358
+ * Whether the datetime includes a timezone offset (`dateType: 'stringOffset'`).
165
359
  */
166
360
  offset?: boolean
167
361
  /**
168
- * Local datetime without timezone (`dateType: 'stringLocal'`).
362
+ * Whether the datetime is local (no timezone, `dateType: 'stringLocal'`).
169
363
  */
170
364
  local?: boolean
171
365
  }
172
366
 
173
367
  /**
174
- * Base for `date` and `time` schemas.
368
+ * Shared base for `date` and `time` schemas.
175
369
  */
176
370
  type TemporalSchemaNodeBase<T extends 'date' | 'time'> = SchemaNodeBase & {
371
+ /**
372
+ * Schema type discriminator.
373
+ */
177
374
  type: T
178
375
  /**
179
- * Representation in generated code: native `Date` or plain string.
376
+ * Output representation in generated code.
180
377
  */
181
378
  representation: 'date' | 'string'
182
379
  }
183
380
 
184
381
  /**
185
- * Date schema.
382
+ * Date schema node.
383
+ *
384
+ * @example
385
+ * ```ts
386
+ * const dateSchema: DateSchemaNode = { kind: 'Schema', type: 'date', representation: 'string' }
387
+ * ```
186
388
  */
187
389
  export type DateSchemaNode = TemporalSchemaNodeBase<'date'>
188
390
 
189
391
  /**
190
- * Time schema.
392
+ * Time schema node.
393
+ *
394
+ * @example
395
+ * ```ts
396
+ * const timeSchema: TimeSchemaNode = { kind: 'Schema', type: 'time', representation: 'string' }
397
+ * ```
191
398
  */
192
399
  export type TimeSchemaNode = TemporalSchemaNodeBase<'time'>
193
400
 
194
401
  /**
195
- * String schema.
402
+ * String schema node.
403
+ *
404
+ * @example
405
+ * ```ts
406
+ * const stringSchema: StringSchemaNode = { kind: 'Schema', type: 'string' }
407
+ * ```
196
408
  */
197
409
  export type StringSchemaNode = SchemaNodeBase & {
410
+ /**
411
+ * Schema type discriminator.
412
+ */
198
413
  type: 'string'
414
+ /**
415
+ * Minimum string length.
416
+ */
199
417
  min?: number
418
+ /**
419
+ * Maximum string length.
420
+ */
200
421
  max?: number
422
+ /**
423
+ * Regex pattern.
424
+ */
201
425
  pattern?: string
202
426
  }
203
427
 
204
428
  /**
205
- * Number, integer, or bigint schema.
429
+ * Numeric schema (`number`, `integer`, or `bigint`).
430
+ *
431
+ * @example
432
+ * ```ts
433
+ * const numberSchema: NumberSchemaNode = { kind: 'Schema', type: 'number' }
434
+ * ```
206
435
  */
207
436
  export type NumberSchemaNode = SchemaNodeBase & {
437
+ /**
438
+ * Schema type discriminator.
439
+ */
208
440
  type: 'number' | 'integer' | 'bigint'
441
+ /**
442
+ * Minimum value.
443
+ */
209
444
  min?: number
445
+ /**
446
+ * Maximum value.
447
+ */
210
448
  max?: number
449
+ /**
450
+ * Exclusive minimum value.
451
+ */
211
452
  exclusiveMinimum?: number
453
+ /**
454
+ * Exclusive maximum value.
455
+ */
212
456
  exclusiveMaximum?: number
213
457
  }
214
458
 
215
459
  /**
216
- * Schema for scalar types with no additional constraints.
460
+ * Scalar schema with no extra constraints.
461
+ *
462
+ * @example
463
+ * ```ts
464
+ * const anySchema: ScalarSchemaNode = { kind: 'Schema', type: 'any' }
465
+ * ```
217
466
  */
218
467
  export type ScalarSchemaNode = SchemaNodeBase & {
468
+ /**
469
+ * Schema type discriminator.
470
+ */
219
471
  type: ScalarSchemaType
220
472
  }
221
473
 
222
474
  /**
223
- * URL schema, optionally carrying an Express-style path template for template literal generation.
475
+ * URL schema node.
476
+ * Can include an Express-style path template for template literal types.
477
+ *
478
+ * @example
479
+ * ```ts
480
+ * const urlSchema: UrlSchemaNode = { kind: 'Schema', type: 'url', path: '/pets/:petId' }
481
+ * ```
224
482
  */
225
483
  export type UrlSchemaNode = SchemaNodeBase & {
484
+ /**
485
+ * Schema type discriminator.
486
+ */
226
487
  type: 'url'
227
488
  /**
228
- * Express-style path (e.g. `'/pets/:petId'`). When set, printers may emit a template literal type.
489
+ * Express-style path template, for example, `'/pets/:petId'`.
229
490
  */
230
491
  path?: string
231
492
  }
232
493
 
233
494
  /**
234
- * Maps each schema type string to its `SchemaNode` variant. Used by `narrowSchema`.
495
+ * Mapping from schema type literals to concrete schema node types.
496
+ * Used by `narrowSchema`.
235
497
  */
236
498
  export type SchemaNodeByType = {
237
499
  object: ObjectSchemaNode
@@ -261,7 +523,7 @@ export type SchemaNodeByType = {
261
523
  }
262
524
 
263
525
  /**
264
- * Discriminated union of all schema variants.
526
+ * Union of all schema node types.
265
527
  */
266
528
  export type SchemaNode =
267
529
  | ObjectSchemaNode
@@ -1,10 +1,10 @@
1
- import type { FunctionNode, FunctionNodeType } from './nodes/function.ts'
2
- import type { FunctionParameterNode, FunctionParametersNode, ObjectBindingParameterNode } from './nodes/index.ts'
1
+ import type { FunctionNode, FunctionNodeType } from '../nodes/function.ts'
2
+ import type { FunctionParameterNode, FunctionParametersNode, ObjectBindingParameterNode } from '../nodes/index.ts'
3
3
  import type { PrinterFactoryOptions } from './printer.ts'
4
4
  import { createPrinterFactory } from './printer.ts'
5
5
 
6
6
  /**
7
- * Maps each `FunctionNodeType` key to its concrete node type.
7
+ * Maps each function-printer handler key to its concrete node type.
8
8
  */
9
9
  export type FunctionNodeByType = {
10
10
  functionParameter: FunctionParameterNode
@@ -19,8 +19,10 @@ const kindToHandlerKey = {
19
19
  } satisfies Record<string, FunctionNodeType>
20
20
 
21
21
  /**
22
- * Creates a named function-signature printer factory.
23
- * Built on `createPrinterFactory` — dispatches on `node.kind` instead of `node.type`.
22
+ * Creates a function-parameter printer factory.
23
+ *
24
+ * This wrapper uses `createPrinterFactory` and dispatches handlers by `node.kind`
25
+ * (for function nodes) rather than by `node.type` (for schema nodes).
24
26
  *
25
27
  * @example
26
28
  * ```ts
@@ -46,18 +48,17 @@ const kindToHandlerKey = {
46
48
  */
47
49
  export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>((node) => kindToHandlerKey[node.kind])
48
50
 
49
- /**
50
- * The four rendering modes for a `FunctionParametersNode`.
51
- *
52
- * | Mode | Output example | Use case |
53
- * |---------------|---------------------------------------------|---------------------------------|
54
- * | `declaration` | `id: string, config: Config = {}` | Function parameter declarations |
55
- * | `call` | `id, { method, url }` | Function call arguments |
56
- * | `keys` | `{ id, config }` | Key names only (destructuring) |
57
- * | `values` | `{ id: id, config: config }` | Key → value pairs |
58
- */
59
-
60
51
  export type FunctionPrinterOptions = {
52
+ /**
53
+ * Rendering modes supported by `functionPrinter`.
54
+ *
55
+ * | Mode | Output example | Use case |
56
+ * |---------------|---------------------------------------------|---------------------------------|
57
+ * | `declaration` | `id: string, config: Config = {}` | Function parameter declaration |
58
+ * | `call` | `id, { method, url }` | Function call arguments |
59
+ * | `keys` | `{ id, config }` | Key names only (destructuring) |
60
+ * | `values` | `{ id: id, config: config }` | Key/value object entries |
61
+ */
61
62
  mode: 'declaration' | 'call' | 'keys' | 'values'
62
63
  /**
63
64
  * Optional transformation applied to every parameter name before printing.
@@ -91,12 +92,12 @@ function sortChildParams(params: Array<FunctionParameterNode>): Array<FunctionPa
91
92
  }
92
93
 
93
94
  /**
94
- * Default function-signature printer. Covers the four standard output modes
95
- * used throughout Kubb plugins.
95
+ * Default function-signature printer.
96
+ * Covers the four standard output modes used across Kubb plugins.
96
97
  *
97
98
  * @example
98
99
  * ```ts
99
- * const printer = functionSignaturePrinter({ mode: 'declaration' })
100
+ * const printer = functionPrinter({ mode: 'declaration' })
100
101
  *
101
102
  * const sig = createFunctionParameters({
102
103
  * params: [
@@ -0,0 +1,3 @@
1
+ export { defineFunctionPrinter, functionPrinter } from './functionPrinter.ts'
2
+ export type { Printer, PrinterFactoryOptions } from './printer.ts'
3
+ export { createPrinterFactory, definePrinter } from './printer.ts'