@kubb/ast 5.0.0-alpha.2 → 5.0.0-alpha.21

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