@kubb/ast 5.0.0-alpha.16 → 5.0.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +651 -494
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +229 -68
- package/dist/index.js +638 -489
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/visitor-DCQyoFvH.d.ts +1976 -0
- package/package.json +1 -1
- package/src/constants.ts +88 -4
- package/src/factory.ts +174 -37
- package/src/guards.ts +37 -10
- package/src/index.ts +6 -5
- package/src/infer.ts +130 -0
- package/src/mocks.ts +4 -3
- package/src/nodes/base.ts +21 -3
- package/src/nodes/function.ts +34 -22
- package/src/nodes/http.ts +17 -5
- package/src/nodes/index.ts +14 -4
- package/src/nodes/operation.ts +47 -9
- package/src/nodes/parameter.ts +27 -1
- package/src/nodes/property.ts +23 -1
- package/src/nodes/response.ts +24 -4
- package/src/nodes/root.ts +29 -8
- package/src/nodes/schema.ts +301 -37
- package/src/{functionPrinter.ts → printers/functionPrinter.ts} +20 -19
- package/src/printers/index.ts +3 -0
- package/src/{printer.ts → printers/printer.ts} +60 -44
- package/src/refs.ts +30 -6
- package/src/resolvers.ts +45 -0
- package/src/transformers.ts +196 -0
- package/src/types.ts +2 -1
- package/src/utils.ts +37 -8
- package/src/visitor.ts +204 -18
- package/dist/visitor-YMltBj6w.d.ts +0 -970
- package/src/transforms.ts +0 -114
|
@@ -0,0 +1,1976 @@
|
|
|
1
|
+
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
+
|
|
3
|
+
//#region src/constants.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Traversal depth used by AST visitor utilities.
|
|
6
|
+
*/
|
|
7
|
+
type VisitorDepth = 'shallow' | 'deep';
|
|
8
|
+
declare const nodeKinds: {
|
|
9
|
+
readonly root: "Root";
|
|
10
|
+
readonly operation: "Operation";
|
|
11
|
+
readonly schema: "Schema";
|
|
12
|
+
readonly property: "Property";
|
|
13
|
+
readonly parameter: "Parameter";
|
|
14
|
+
readonly response: "Response";
|
|
15
|
+
readonly functionParameter: "FunctionParameter";
|
|
16
|
+
readonly objectBindingParameter: "ObjectBindingParameter";
|
|
17
|
+
readonly functionParameters: "FunctionParameters";
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Canonical schema type strings used by AST schema nodes.
|
|
21
|
+
*
|
|
22
|
+
* These values are used across the AST as stable discriminators
|
|
23
|
+
* (for example `schema.type === schemaTypes.object`).
|
|
24
|
+
*
|
|
25
|
+
* The map is grouped by intent:
|
|
26
|
+
* - primitives (`string`, `number`, `boolean`, ...)
|
|
27
|
+
* - structural/composite (`object`, `array`, `union`, ...)
|
|
28
|
+
* - special OpenAPI-oriented types (`ref`, `datetime`, `uuid`, ...)
|
|
29
|
+
*/
|
|
30
|
+
declare const schemaTypes: {
|
|
31
|
+
/**
|
|
32
|
+
* Text value.
|
|
33
|
+
*/
|
|
34
|
+
readonly string: "string";
|
|
35
|
+
/**
|
|
36
|
+
* Floating-point number (`float`, `double`).
|
|
37
|
+
*/
|
|
38
|
+
readonly number: "number";
|
|
39
|
+
/**
|
|
40
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
41
|
+
*/
|
|
42
|
+
readonly integer: "integer";
|
|
43
|
+
/**
|
|
44
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
45
|
+
*/
|
|
46
|
+
readonly bigint: "bigint";
|
|
47
|
+
/**
|
|
48
|
+
* Boolean value
|
|
49
|
+
*/
|
|
50
|
+
readonly boolean: "boolean";
|
|
51
|
+
/**
|
|
52
|
+
* Explicit null value.
|
|
53
|
+
*/
|
|
54
|
+
readonly null: "null";
|
|
55
|
+
/**
|
|
56
|
+
* Any value (no type restriction).
|
|
57
|
+
*/
|
|
58
|
+
readonly any: "any";
|
|
59
|
+
/**
|
|
60
|
+
* Unknown value (must be narrowed before usage).
|
|
61
|
+
*/
|
|
62
|
+
readonly unknown: "unknown";
|
|
63
|
+
/**
|
|
64
|
+
* No return value (`void`).
|
|
65
|
+
*/
|
|
66
|
+
readonly void: "void";
|
|
67
|
+
/**
|
|
68
|
+
* Object with named properties.
|
|
69
|
+
*/
|
|
70
|
+
readonly object: "object";
|
|
71
|
+
/**
|
|
72
|
+
* Sequential list of items.
|
|
73
|
+
*/
|
|
74
|
+
readonly array: "array";
|
|
75
|
+
/**
|
|
76
|
+
* Fixed-length list with position-specific items.
|
|
77
|
+
*/
|
|
78
|
+
readonly tuple: "tuple";
|
|
79
|
+
/**
|
|
80
|
+
* "One of" multiple schema members.
|
|
81
|
+
*/
|
|
82
|
+
readonly union: "union";
|
|
83
|
+
/**
|
|
84
|
+
* "All of" multiple schema members.
|
|
85
|
+
*/
|
|
86
|
+
readonly intersection: "intersection";
|
|
87
|
+
/**
|
|
88
|
+
* Enum schema.
|
|
89
|
+
*/
|
|
90
|
+
readonly enum: "enum";
|
|
91
|
+
/**
|
|
92
|
+
* Reference to another schema.
|
|
93
|
+
*/
|
|
94
|
+
readonly ref: "ref";
|
|
95
|
+
/**
|
|
96
|
+
* Calendar date (for example `2026-03-24`).
|
|
97
|
+
*/
|
|
98
|
+
readonly date: "date";
|
|
99
|
+
/**
|
|
100
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
101
|
+
*/
|
|
102
|
+
readonly datetime: "datetime";
|
|
103
|
+
/**
|
|
104
|
+
* Time-only value (for example `09:00:00`).
|
|
105
|
+
*/
|
|
106
|
+
readonly time: "time";
|
|
107
|
+
/**
|
|
108
|
+
* UUID value.
|
|
109
|
+
*/
|
|
110
|
+
readonly uuid: "uuid";
|
|
111
|
+
/**
|
|
112
|
+
* Email address value.
|
|
113
|
+
*/
|
|
114
|
+
readonly email: "email";
|
|
115
|
+
/**
|
|
116
|
+
* URL value.
|
|
117
|
+
*/
|
|
118
|
+
readonly url: "url";
|
|
119
|
+
/**
|
|
120
|
+
* Binary/blob value.
|
|
121
|
+
*/
|
|
122
|
+
readonly blob: "blob";
|
|
123
|
+
/**
|
|
124
|
+
* Impossible value (`never`).
|
|
125
|
+
*/
|
|
126
|
+
readonly never: "never";
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Primitive scalar schema types used when simplifying union members.
|
|
130
|
+
*/
|
|
131
|
+
declare const SCALAR_PRIMITIVE_TYPES: Set<"string" | "number" | "bigint" | "boolean" | "integer">;
|
|
132
|
+
declare const httpMethods: {
|
|
133
|
+
readonly get: "GET";
|
|
134
|
+
readonly post: "POST";
|
|
135
|
+
readonly put: "PUT";
|
|
136
|
+
readonly patch: "PATCH";
|
|
137
|
+
readonly delete: "DELETE";
|
|
138
|
+
readonly head: "HEAD";
|
|
139
|
+
readonly options: "OPTIONS";
|
|
140
|
+
readonly trace: "TRACE";
|
|
141
|
+
};
|
|
142
|
+
declare const mediaTypes: {
|
|
143
|
+
readonly applicationJson: "application/json";
|
|
144
|
+
readonly applicationXml: "application/xml";
|
|
145
|
+
readonly applicationFormUrlEncoded: "application/x-www-form-urlencoded";
|
|
146
|
+
readonly applicationOctetStream: "application/octet-stream";
|
|
147
|
+
readonly applicationPdf: "application/pdf";
|
|
148
|
+
readonly applicationZip: "application/zip";
|
|
149
|
+
readonly applicationGraphql: "application/graphql";
|
|
150
|
+
readonly multipartFormData: "multipart/form-data";
|
|
151
|
+
readonly textPlain: "text/plain";
|
|
152
|
+
readonly textHtml: "text/html";
|
|
153
|
+
readonly textCsv: "text/csv";
|
|
154
|
+
readonly textXml: "text/xml";
|
|
155
|
+
readonly imagePng: "image/png";
|
|
156
|
+
readonly imageJpeg: "image/jpeg";
|
|
157
|
+
readonly imageGif: "image/gif";
|
|
158
|
+
readonly imageWebp: "image/webp";
|
|
159
|
+
readonly imageSvgXml: "image/svg+xml";
|
|
160
|
+
readonly audioMpeg: "audio/mpeg";
|
|
161
|
+
readonly videoMp4: "video/mp4";
|
|
162
|
+
};
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region src/nodes/base.d.ts
|
|
165
|
+
/**
|
|
166
|
+
* `kind` values used by AST nodes.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* const kind: NodeKind = 'Schema'
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
type NodeKind = 'Root' | 'Operation' | 'Schema' | 'Property' | 'Parameter' | 'Response' | 'FunctionParameter' | 'ObjectBindingParameter' | 'FunctionParameters';
|
|
174
|
+
/**
|
|
175
|
+
* Base shape shared by all AST nodes.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* const base: BaseNode = { kind: 'Root' }
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
type BaseNode = {
|
|
183
|
+
/**
|
|
184
|
+
* Node discriminator.
|
|
185
|
+
*/
|
|
186
|
+
kind: NodeKind;
|
|
187
|
+
};
|
|
188
|
+
//#endregion
|
|
189
|
+
//#region src/nodes/function.d.ts
|
|
190
|
+
/**
|
|
191
|
+
* AST node for one function parameter.
|
|
192
|
+
*
|
|
193
|
+
* @example Required parameter
|
|
194
|
+
* `name: Type`
|
|
195
|
+
*
|
|
196
|
+
* @example Optional param
|
|
197
|
+
* `name?: Type`
|
|
198
|
+
*
|
|
199
|
+
* @example Parameter with default
|
|
200
|
+
* `name: Type = defaultValue`
|
|
201
|
+
*
|
|
202
|
+
* @example Rest parameter
|
|
203
|
+
* `...name: Type[]`
|
|
204
|
+
*/
|
|
205
|
+
type FunctionParameterNode = BaseNode & {
|
|
206
|
+
/**
|
|
207
|
+
* Node kind.
|
|
208
|
+
*/
|
|
209
|
+
kind: 'FunctionParameter';
|
|
210
|
+
/**
|
|
211
|
+
* Parameter name in the generated signature.
|
|
212
|
+
*/
|
|
213
|
+
name: string;
|
|
214
|
+
/**
|
|
215
|
+
* TypeScript type text, for example, `"string"` or `"Pet[]"`.
|
|
216
|
+
* Omit for untyped JavaScript output.
|
|
217
|
+
*/
|
|
218
|
+
type?: string;
|
|
219
|
+
/**
|
|
220
|
+
* When `true` the parameter is emitted as a rest parameter (`...name`).
|
|
221
|
+
* @default false
|
|
222
|
+
*/
|
|
223
|
+
rest?: boolean;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Optional parameter, rendered with `?` in declarations.
|
|
227
|
+
* Cannot be combined with `default` because defaulted parameters are already optional.
|
|
228
|
+
* @example `name?: Type`
|
|
229
|
+
*/
|
|
230
|
+
& ({
|
|
231
|
+
optional: true;
|
|
232
|
+
default?: never;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Required parameter, or parameter with a default value.
|
|
236
|
+
* @example Required: `name: Type`
|
|
237
|
+
* @example With default: `name: Type = default`
|
|
238
|
+
*/
|
|
239
|
+
| {
|
|
240
|
+
optional?: false;
|
|
241
|
+
default?: string;
|
|
242
|
+
});
|
|
243
|
+
/**
|
|
244
|
+
* AST node for object-destructured function parameters.
|
|
245
|
+
*
|
|
246
|
+
* This node renders as `{ key1, key2 }: { key1: Type1; key2: Type2 } = {}` in declarations,
|
|
247
|
+
* or as individual top-level parameters when `inline` is `true`.
|
|
248
|
+
*
|
|
249
|
+
* This replaces `mode: 'object'` and `mode: 'inlineSpread'` from the old `FunctionParams` API.
|
|
250
|
+
*
|
|
251
|
+
* @example Object destructuring with auto-computed type (declaration)
|
|
252
|
+
* `{ id, name }: { id: string; name: string } = {}`
|
|
253
|
+
*
|
|
254
|
+
* @example Inline (spread) — children emitted as individual top-level params
|
|
255
|
+
* `id: string, name: string`
|
|
256
|
+
*/
|
|
257
|
+
type ObjectBindingParameterNode = BaseNode & {
|
|
258
|
+
/**
|
|
259
|
+
* Node kind.
|
|
260
|
+
*/
|
|
261
|
+
kind: 'ObjectBindingParameter';
|
|
262
|
+
/**
|
|
263
|
+
* The individual parameters that form the destructured object.
|
|
264
|
+
* Rendered as `{ key1, key2 }` in declarations, or spread inline when `inline` is `true`.
|
|
265
|
+
*/
|
|
266
|
+
properties: Array<FunctionParameterNode>;
|
|
267
|
+
/**
|
|
268
|
+
* Optional type text for the full object parameter.
|
|
269
|
+
* When absent, the printer auto-computes `{ key1: Type1; key2: Type2 }` from `properties`.
|
|
270
|
+
*/
|
|
271
|
+
type?: string;
|
|
272
|
+
/**
|
|
273
|
+
* When `true`, `properties` are emitted as individual top-level parameters instead of
|
|
274
|
+
* being wrapped in a destructuring pattern (`{ key1, key2 }`).
|
|
275
|
+
*
|
|
276
|
+
* Equivalent to `mode: 'inlineSpread'` in the legacy `FunctionParams` API.
|
|
277
|
+
* @default false
|
|
278
|
+
*/
|
|
279
|
+
inline?: boolean;
|
|
280
|
+
/**
|
|
281
|
+
* Whether the full object binding is optional.
|
|
282
|
+
* If omitted, printers infer this from child properties.
|
|
283
|
+
*/
|
|
284
|
+
optional?: boolean;
|
|
285
|
+
/**
|
|
286
|
+
* Default value for the object group, written verbatim after `=`.
|
|
287
|
+
* Commonly `'{}'` to allow omitting the argument entirely.
|
|
288
|
+
*/
|
|
289
|
+
default?: string;
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* AST node for a complete function parameter list.
|
|
293
|
+
*
|
|
294
|
+
* Printers are responsible for sorting (`required` → `optional` → `defaulted`).
|
|
295
|
+
* Nodes are plain immutable data.
|
|
296
|
+
*
|
|
297
|
+
* Renders differently depending on the output mode:
|
|
298
|
+
* - `declaration` → `(id: string, config: Config = {})` — function declaration parameters
|
|
299
|
+
* - `call` → `(id, { method, url })` — function call arguments
|
|
300
|
+
* - `keys` → `{ id, config }` — key names only (for destructuring)
|
|
301
|
+
* - `values` → `{ id: id, config: config }` — key → value pairs
|
|
302
|
+
*/
|
|
303
|
+
type FunctionParametersNode = BaseNode & {
|
|
304
|
+
/**
|
|
305
|
+
* Node kind.
|
|
306
|
+
*/
|
|
307
|
+
kind: 'FunctionParameters';
|
|
308
|
+
/**
|
|
309
|
+
* Ordered parameter nodes.
|
|
310
|
+
*/
|
|
311
|
+
params: Array<FunctionParameterNode | ObjectBindingParameterNode>;
|
|
312
|
+
};
|
|
313
|
+
/**
|
|
314
|
+
* The three function-signature AST node variants.
|
|
315
|
+
*/
|
|
316
|
+
type FunctionNode = FunctionParameterNode | ObjectBindingParameterNode | FunctionParametersNode;
|
|
317
|
+
/**
|
|
318
|
+
* Handler map keys — one per `FunctionNode` kind.
|
|
319
|
+
*/
|
|
320
|
+
type FunctionNodeType = 'functionParameter' | 'objectBindingParameter' | 'functionParameters';
|
|
321
|
+
//#endregion
|
|
322
|
+
//#region src/nodes/property.d.ts
|
|
323
|
+
/**
|
|
324
|
+
* AST node representing one named object property.
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* const property: PropertyNode = {
|
|
329
|
+
* kind: 'Property',
|
|
330
|
+
* name: 'id',
|
|
331
|
+
* schema: createSchema({ type: 'integer' }),
|
|
332
|
+
* required: true,
|
|
333
|
+
* }
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
type PropertyNode = BaseNode & {
|
|
337
|
+
/**
|
|
338
|
+
* Node kind.
|
|
339
|
+
*/
|
|
340
|
+
kind: 'Property';
|
|
341
|
+
/**
|
|
342
|
+
* Property key.
|
|
343
|
+
*/
|
|
344
|
+
name: string;
|
|
345
|
+
/**
|
|
346
|
+
* Property schema.
|
|
347
|
+
*/
|
|
348
|
+
schema: SchemaNode;
|
|
349
|
+
/**
|
|
350
|
+
* Whether the property is required.
|
|
351
|
+
*/
|
|
352
|
+
required: boolean;
|
|
353
|
+
};
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region src/nodes/schema.d.ts
|
|
356
|
+
type PrimitiveSchemaType =
|
|
357
|
+
/**
|
|
358
|
+
* Text value.
|
|
359
|
+
*/
|
|
360
|
+
'string'
|
|
361
|
+
/**
|
|
362
|
+
* Floating-point number.
|
|
363
|
+
*/
|
|
364
|
+
| 'number'
|
|
365
|
+
/**
|
|
366
|
+
* Integer number.
|
|
367
|
+
*/
|
|
368
|
+
| 'integer'
|
|
369
|
+
/**
|
|
370
|
+
* Big integer number.
|
|
371
|
+
*/
|
|
372
|
+
| 'bigint'
|
|
373
|
+
/**
|
|
374
|
+
* Boolean value.
|
|
375
|
+
*/
|
|
376
|
+
| 'boolean'
|
|
377
|
+
/**
|
|
378
|
+
* Null value.
|
|
379
|
+
*/
|
|
380
|
+
| 'null'
|
|
381
|
+
/**
|
|
382
|
+
* Any value.
|
|
383
|
+
*/
|
|
384
|
+
| 'any'
|
|
385
|
+
/**
|
|
386
|
+
* Unknown value.
|
|
387
|
+
*/
|
|
388
|
+
| 'unknown'
|
|
389
|
+
/**
|
|
390
|
+
* No value (`void`).
|
|
391
|
+
*/
|
|
392
|
+
| 'void'
|
|
393
|
+
/**
|
|
394
|
+
* Never value.
|
|
395
|
+
*/
|
|
396
|
+
| 'never'
|
|
397
|
+
/**
|
|
398
|
+
* Object value.
|
|
399
|
+
*/
|
|
400
|
+
| 'object'
|
|
401
|
+
/**
|
|
402
|
+
* Array value.
|
|
403
|
+
*/
|
|
404
|
+
| 'array'
|
|
405
|
+
/**
|
|
406
|
+
* Date value.
|
|
407
|
+
*/
|
|
408
|
+
| 'date';
|
|
409
|
+
/**
|
|
410
|
+
* Composite schema types.
|
|
411
|
+
*/
|
|
412
|
+
type ComplexSchemaType = 'tuple' | 'union' | 'intersection' | 'enum';
|
|
413
|
+
/**
|
|
414
|
+
* Schema types that need special handling in generators.
|
|
415
|
+
*/
|
|
416
|
+
type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'blob';
|
|
417
|
+
/**
|
|
418
|
+
* All schema type strings.
|
|
419
|
+
*/
|
|
420
|
+
type SchemaType = PrimitiveSchemaType | ComplexSchemaType | SpecialSchemaType;
|
|
421
|
+
/**
|
|
422
|
+
* Scalar schema types without extra object/array/ref structure.
|
|
423
|
+
*/
|
|
424
|
+
type ScalarSchemaType = Exclude<SchemaType, 'object' | 'array' | 'tuple' | 'union' | 'intersection' | 'enum' | 'ref' | 'datetime' | 'date' | 'time' | 'string' | 'number' | 'integer' | 'bigint' | 'url'>;
|
|
425
|
+
/**
|
|
426
|
+
* Fields shared by all schema nodes.
|
|
427
|
+
*/
|
|
428
|
+
type SchemaNodeBase = BaseNode & {
|
|
429
|
+
/**
|
|
430
|
+
* Node kind.
|
|
431
|
+
*/
|
|
432
|
+
kind: 'Schema';
|
|
433
|
+
/**
|
|
434
|
+
* Schema name for named definitions (for example, `"Pet"`).
|
|
435
|
+
* Inline schemas omit this field.
|
|
436
|
+
* `null` means kubb has processed this and determined there is no applicable name.
|
|
437
|
+
* `undefined` means the name has not been set yet.
|
|
438
|
+
*/
|
|
439
|
+
name?: string | null;
|
|
440
|
+
/**
|
|
441
|
+
* Short schema title.
|
|
442
|
+
*/
|
|
443
|
+
title?: string;
|
|
444
|
+
/**
|
|
445
|
+
* Schema description text.
|
|
446
|
+
*/
|
|
447
|
+
description?: string;
|
|
448
|
+
/**
|
|
449
|
+
* Whether `null` is allowed.
|
|
450
|
+
*/
|
|
451
|
+
nullable?: boolean;
|
|
452
|
+
/**
|
|
453
|
+
* Whether the field is optional.
|
|
454
|
+
*/
|
|
455
|
+
optional?: boolean;
|
|
456
|
+
/**
|
|
457
|
+
* Both optional and nullable (`optional` + `nullable`).
|
|
458
|
+
*/
|
|
459
|
+
nullish?: boolean;
|
|
460
|
+
/**
|
|
461
|
+
* Whether the schema is deprecated.
|
|
462
|
+
*/
|
|
463
|
+
deprecated?: boolean;
|
|
464
|
+
/**
|
|
465
|
+
* Whether the schema is read-only.
|
|
466
|
+
*/
|
|
467
|
+
readOnly?: boolean;
|
|
468
|
+
/**
|
|
469
|
+
* Whether the schema is write-only.
|
|
470
|
+
*/
|
|
471
|
+
writeOnly?: boolean;
|
|
472
|
+
/**
|
|
473
|
+
* Default value.
|
|
474
|
+
*/
|
|
475
|
+
default?: unknown;
|
|
476
|
+
/**
|
|
477
|
+
* Example value.
|
|
478
|
+
*/
|
|
479
|
+
example?: unknown;
|
|
480
|
+
/**
|
|
481
|
+
* Base primitive type.
|
|
482
|
+
* For example, this is `'string'` for a `uuid` schema.
|
|
483
|
+
*/
|
|
484
|
+
primitive?: PrimitiveSchemaType;
|
|
485
|
+
};
|
|
486
|
+
/**
|
|
487
|
+
* Object schema with ordered properties.
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* ```ts
|
|
491
|
+
* const objectSchema: ObjectSchemaNode = {
|
|
492
|
+
* kind: 'Schema',
|
|
493
|
+
* type: 'object',
|
|
494
|
+
* properties: [],
|
|
495
|
+
* }
|
|
496
|
+
* ```
|
|
497
|
+
*/
|
|
498
|
+
type ObjectSchemaNode = SchemaNodeBase & {
|
|
499
|
+
/**
|
|
500
|
+
* Schema type discriminator.
|
|
501
|
+
*/
|
|
502
|
+
type: 'object';
|
|
503
|
+
/**
|
|
504
|
+
* Ordered object properties.
|
|
505
|
+
*/
|
|
506
|
+
properties: Array<PropertyNode>;
|
|
507
|
+
/**
|
|
508
|
+
* Additional object properties behavior:
|
|
509
|
+
* - `true`: allow any value
|
|
510
|
+
* - `SchemaNode`: allow values that match that schema
|
|
511
|
+
* - `undefined`: no additional properties
|
|
512
|
+
*/
|
|
513
|
+
additionalProperties?: SchemaNode | true;
|
|
514
|
+
/**
|
|
515
|
+
* Pattern-based property schemas.
|
|
516
|
+
*/
|
|
517
|
+
patternProperties?: Record<string, SchemaNode>;
|
|
518
|
+
};
|
|
519
|
+
/**
|
|
520
|
+
* Array-like schema (`array` or `tuple`).
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```ts
|
|
524
|
+
* const arraySchema: ArraySchemaNode = {
|
|
525
|
+
* kind: 'Schema',
|
|
526
|
+
* type: 'array',
|
|
527
|
+
* items: [],
|
|
528
|
+
* }
|
|
529
|
+
* ```
|
|
530
|
+
*/
|
|
531
|
+
type ArraySchemaNode = SchemaNodeBase & {
|
|
532
|
+
/**
|
|
533
|
+
* Schema type discriminator (`array` or `tuple`).
|
|
534
|
+
*/
|
|
535
|
+
type: 'array' | 'tuple';
|
|
536
|
+
/**
|
|
537
|
+
* Item schemas.
|
|
538
|
+
*/
|
|
539
|
+
items?: Array<SchemaNode>;
|
|
540
|
+
/**
|
|
541
|
+
* Tuple rest-item schema for elements beyond positional `items`.
|
|
542
|
+
*/
|
|
543
|
+
rest?: SchemaNode;
|
|
544
|
+
/**
|
|
545
|
+
* Minimum item count (or tuple length).
|
|
546
|
+
*/
|
|
547
|
+
min?: number;
|
|
548
|
+
/**
|
|
549
|
+
* Maximum item count (or tuple length).
|
|
550
|
+
*/
|
|
551
|
+
max?: number;
|
|
552
|
+
/**
|
|
553
|
+
* Whether all items must be unique.
|
|
554
|
+
*/
|
|
555
|
+
unique?: boolean;
|
|
556
|
+
};
|
|
557
|
+
/**
|
|
558
|
+
* Shared shape for union and intersection schemas.
|
|
559
|
+
*/
|
|
560
|
+
type CompositeSchemaNodeBase = SchemaNodeBase & {
|
|
561
|
+
/**
|
|
562
|
+
* Member schemas.
|
|
563
|
+
*/
|
|
564
|
+
members?: Array<SchemaNode>;
|
|
565
|
+
};
|
|
566
|
+
/**
|
|
567
|
+
* Union schema, often from `oneOf` or `anyOf`.
|
|
568
|
+
*
|
|
569
|
+
* @example
|
|
570
|
+
* ```ts
|
|
571
|
+
* const unionSchema: UnionSchemaNode = {
|
|
572
|
+
* kind: 'Schema',
|
|
573
|
+
* type: 'union',
|
|
574
|
+
* members: [],
|
|
575
|
+
* }
|
|
576
|
+
* ```
|
|
577
|
+
*/
|
|
578
|
+
type UnionSchemaNode = CompositeSchemaNodeBase & {
|
|
579
|
+
/**
|
|
580
|
+
* Schema type discriminator.
|
|
581
|
+
*/
|
|
582
|
+
type: 'union';
|
|
583
|
+
/**
|
|
584
|
+
* Discriminator property name from OpenAPI `discriminator.propertyName`.
|
|
585
|
+
*/
|
|
586
|
+
discriminatorPropertyName?: string;
|
|
587
|
+
};
|
|
588
|
+
/**
|
|
589
|
+
* Intersection schema, often from `allOf`.
|
|
590
|
+
*
|
|
591
|
+
* @example
|
|
592
|
+
* ```ts
|
|
593
|
+
* const intersectionSchema: IntersectionSchemaNode = {
|
|
594
|
+
* kind: 'Schema',
|
|
595
|
+
* type: 'intersection',
|
|
596
|
+
* members: [],
|
|
597
|
+
* }
|
|
598
|
+
* ```
|
|
599
|
+
*/
|
|
600
|
+
type IntersectionSchemaNode = CompositeSchemaNodeBase & {
|
|
601
|
+
/**
|
|
602
|
+
* Schema type discriminator.
|
|
603
|
+
*/
|
|
604
|
+
type: 'intersection';
|
|
605
|
+
};
|
|
606
|
+
/**
|
|
607
|
+
* One named enum item.
|
|
608
|
+
*/
|
|
609
|
+
type EnumValueNode = {
|
|
610
|
+
/**
|
|
611
|
+
* Enum item name.
|
|
612
|
+
*/
|
|
613
|
+
name: string;
|
|
614
|
+
/**
|
|
615
|
+
* Enum item value.
|
|
616
|
+
*/
|
|
617
|
+
value: string | number | boolean;
|
|
618
|
+
/**
|
|
619
|
+
* Primitive type of the enum value.
|
|
620
|
+
*/
|
|
621
|
+
primitive: Extract<PrimitiveSchemaType, 'string' | 'number' | 'boolean'>;
|
|
622
|
+
};
|
|
623
|
+
/**
|
|
624
|
+
* Enum schema node.
|
|
625
|
+
*
|
|
626
|
+
* @example
|
|
627
|
+
* ```ts
|
|
628
|
+
* const enumSchema: EnumSchemaNode = {
|
|
629
|
+
* kind: 'Schema',
|
|
630
|
+
* type: 'enum',
|
|
631
|
+
* enumValues: ['a', 'b'],
|
|
632
|
+
* }
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
type EnumSchemaNode = SchemaNodeBase & {
|
|
636
|
+
/**
|
|
637
|
+
* Schema type discriminator.
|
|
638
|
+
*/
|
|
639
|
+
type: 'enum';
|
|
640
|
+
/**
|
|
641
|
+
* Enum values in simple form.
|
|
642
|
+
*/
|
|
643
|
+
enumValues?: Array<string | number | boolean | null>;
|
|
644
|
+
/**
|
|
645
|
+
* Enum values in named form.
|
|
646
|
+
* If present, this is used instead of `enumValues`.
|
|
647
|
+
*/
|
|
648
|
+
namedEnumValues?: Array<EnumValueNode>;
|
|
649
|
+
};
|
|
650
|
+
/**
|
|
651
|
+
* Reference schema that points to another schema definition.
|
|
652
|
+
*
|
|
653
|
+
* @example
|
|
654
|
+
* ```ts
|
|
655
|
+
* const refSchema: RefSchemaNode = {
|
|
656
|
+
* kind: 'Schema',
|
|
657
|
+
* type: 'ref',
|
|
658
|
+
* ref: '#/components/schemas/Pet',
|
|
659
|
+
* }
|
|
660
|
+
* ```
|
|
661
|
+
*/
|
|
662
|
+
type RefSchemaNode = SchemaNodeBase & {
|
|
663
|
+
/**
|
|
664
|
+
* Schema type discriminator.
|
|
665
|
+
*/
|
|
666
|
+
type: 'ref';
|
|
667
|
+
/**
|
|
668
|
+
* Referenced schema name.
|
|
669
|
+
*/
|
|
670
|
+
name?: string;
|
|
671
|
+
/**
|
|
672
|
+
* Original `$ref` path, for example, `#/components/schemas/Order`.
|
|
673
|
+
* Used to resolve names later.
|
|
674
|
+
*/
|
|
675
|
+
ref?: string;
|
|
676
|
+
/**
|
|
677
|
+
* Pattern copied from a sibling `pattern` field.
|
|
678
|
+
*/
|
|
679
|
+
pattern?: string;
|
|
680
|
+
};
|
|
681
|
+
/**
|
|
682
|
+
* Datetime schema.
|
|
683
|
+
*
|
|
684
|
+
* @example
|
|
685
|
+
* ```ts
|
|
686
|
+
* const datetimeSchema: DatetimeSchemaNode = { kind: 'Schema', type: 'datetime' }
|
|
687
|
+
* ```
|
|
688
|
+
*/
|
|
689
|
+
type DatetimeSchemaNode = SchemaNodeBase & {
|
|
690
|
+
/**
|
|
691
|
+
* Schema type discriminator.
|
|
692
|
+
*/
|
|
693
|
+
type: 'datetime';
|
|
694
|
+
/**
|
|
695
|
+
* Whether the datetime includes a timezone offset (`dateType: 'stringOffset'`).
|
|
696
|
+
*/
|
|
697
|
+
offset?: boolean;
|
|
698
|
+
/**
|
|
699
|
+
* Whether the datetime is local (no timezone, `dateType: 'stringLocal'`).
|
|
700
|
+
*/
|
|
701
|
+
local?: boolean;
|
|
702
|
+
};
|
|
703
|
+
/**
|
|
704
|
+
* Shared base for `date` and `time` schemas.
|
|
705
|
+
*/
|
|
706
|
+
type TemporalSchemaNodeBase<T extends 'date' | 'time'> = SchemaNodeBase & {
|
|
707
|
+
/**
|
|
708
|
+
* Schema type discriminator.
|
|
709
|
+
*/
|
|
710
|
+
type: T;
|
|
711
|
+
/**
|
|
712
|
+
* Output representation in generated code.
|
|
713
|
+
*/
|
|
714
|
+
representation: 'date' | 'string';
|
|
715
|
+
};
|
|
716
|
+
/**
|
|
717
|
+
* Date schema node.
|
|
718
|
+
*
|
|
719
|
+
* @example
|
|
720
|
+
* ```ts
|
|
721
|
+
* const dateSchema: DateSchemaNode = { kind: 'Schema', type: 'date', representation: 'string' }
|
|
722
|
+
* ```
|
|
723
|
+
*/
|
|
724
|
+
type DateSchemaNode = TemporalSchemaNodeBase<'date'>;
|
|
725
|
+
/**
|
|
726
|
+
* Time schema node.
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```ts
|
|
730
|
+
* const timeSchema: TimeSchemaNode = { kind: 'Schema', type: 'time', representation: 'string' }
|
|
731
|
+
* ```
|
|
732
|
+
*/
|
|
733
|
+
type TimeSchemaNode = TemporalSchemaNodeBase<'time'>;
|
|
734
|
+
/**
|
|
735
|
+
* String schema node.
|
|
736
|
+
*
|
|
737
|
+
* @example
|
|
738
|
+
* ```ts
|
|
739
|
+
* const stringSchema: StringSchemaNode = { kind: 'Schema', type: 'string' }
|
|
740
|
+
* ```
|
|
741
|
+
*/
|
|
742
|
+
type StringSchemaNode = SchemaNodeBase & {
|
|
743
|
+
/**
|
|
744
|
+
* Schema type discriminator.
|
|
745
|
+
*/
|
|
746
|
+
type: 'string';
|
|
747
|
+
/**
|
|
748
|
+
* Minimum string length.
|
|
749
|
+
*/
|
|
750
|
+
min?: number;
|
|
751
|
+
/**
|
|
752
|
+
* Maximum string length.
|
|
753
|
+
*/
|
|
754
|
+
max?: number;
|
|
755
|
+
/**
|
|
756
|
+
* Regex pattern.
|
|
757
|
+
*/
|
|
758
|
+
pattern?: string;
|
|
759
|
+
};
|
|
760
|
+
/**
|
|
761
|
+
* Numeric schema (`number`, `integer`, or `bigint`).
|
|
762
|
+
*
|
|
763
|
+
* @example
|
|
764
|
+
* ```ts
|
|
765
|
+
* const numberSchema: NumberSchemaNode = { kind: 'Schema', type: 'number' }
|
|
766
|
+
* ```
|
|
767
|
+
*/
|
|
768
|
+
type NumberSchemaNode = SchemaNodeBase & {
|
|
769
|
+
/**
|
|
770
|
+
* Schema type discriminator.
|
|
771
|
+
*/
|
|
772
|
+
type: 'number' | 'integer' | 'bigint';
|
|
773
|
+
/**
|
|
774
|
+
* Minimum value.
|
|
775
|
+
*/
|
|
776
|
+
min?: number;
|
|
777
|
+
/**
|
|
778
|
+
* Maximum value.
|
|
779
|
+
*/
|
|
780
|
+
max?: number;
|
|
781
|
+
/**
|
|
782
|
+
* Exclusive minimum value.
|
|
783
|
+
*/
|
|
784
|
+
exclusiveMinimum?: number;
|
|
785
|
+
/**
|
|
786
|
+
* Exclusive maximum value.
|
|
787
|
+
*/
|
|
788
|
+
exclusiveMaximum?: number;
|
|
789
|
+
};
|
|
790
|
+
/**
|
|
791
|
+
* Scalar schema with no extra constraints.
|
|
792
|
+
*
|
|
793
|
+
* @example
|
|
794
|
+
* ```ts
|
|
795
|
+
* const anySchema: ScalarSchemaNode = { kind: 'Schema', type: 'any' }
|
|
796
|
+
* ```
|
|
797
|
+
*/
|
|
798
|
+
type ScalarSchemaNode = SchemaNodeBase & {
|
|
799
|
+
/**
|
|
800
|
+
* Schema type discriminator.
|
|
801
|
+
*/
|
|
802
|
+
type: ScalarSchemaType;
|
|
803
|
+
};
|
|
804
|
+
/**
|
|
805
|
+
* URL schema node.
|
|
806
|
+
* Can include an Express-style path template for template literal types.
|
|
807
|
+
*
|
|
808
|
+
* @example
|
|
809
|
+
* ```ts
|
|
810
|
+
* const urlSchema: UrlSchemaNode = { kind: 'Schema', type: 'url', path: '/pets/:petId' }
|
|
811
|
+
* ```
|
|
812
|
+
*/
|
|
813
|
+
type UrlSchemaNode = SchemaNodeBase & {
|
|
814
|
+
/**
|
|
815
|
+
* Schema type discriminator.
|
|
816
|
+
*/
|
|
817
|
+
type: 'url';
|
|
818
|
+
/**
|
|
819
|
+
* Express-style path template, for example, `'/pets/:petId'`.
|
|
820
|
+
*/
|
|
821
|
+
path?: string;
|
|
822
|
+
};
|
|
823
|
+
/**
|
|
824
|
+
* Mapping from schema type literals to concrete schema node types.
|
|
825
|
+
* Used by `narrowSchema`.
|
|
826
|
+
*/
|
|
827
|
+
type SchemaNodeByType = {
|
|
828
|
+
object: ObjectSchemaNode;
|
|
829
|
+
array: ArraySchemaNode;
|
|
830
|
+
tuple: ArraySchemaNode;
|
|
831
|
+
union: UnionSchemaNode;
|
|
832
|
+
intersection: IntersectionSchemaNode;
|
|
833
|
+
enum: EnumSchemaNode;
|
|
834
|
+
ref: RefSchemaNode;
|
|
835
|
+
datetime: DatetimeSchemaNode;
|
|
836
|
+
date: DateSchemaNode;
|
|
837
|
+
time: TimeSchemaNode;
|
|
838
|
+
string: StringSchemaNode;
|
|
839
|
+
number: NumberSchemaNode;
|
|
840
|
+
integer: NumberSchemaNode;
|
|
841
|
+
bigint: NumberSchemaNode;
|
|
842
|
+
boolean: ScalarSchemaNode;
|
|
843
|
+
null: ScalarSchemaNode;
|
|
844
|
+
any: ScalarSchemaNode;
|
|
845
|
+
unknown: ScalarSchemaNode;
|
|
846
|
+
void: ScalarSchemaNode;
|
|
847
|
+
never: ScalarSchemaNode;
|
|
848
|
+
uuid: ScalarSchemaNode;
|
|
849
|
+
email: ScalarSchemaNode;
|
|
850
|
+
url: UrlSchemaNode;
|
|
851
|
+
blob: ScalarSchemaNode;
|
|
852
|
+
};
|
|
853
|
+
/**
|
|
854
|
+
* Union of all schema node types.
|
|
855
|
+
*/
|
|
856
|
+
type SchemaNode = ObjectSchemaNode | ArraySchemaNode | UnionSchemaNode | IntersectionSchemaNode | EnumSchemaNode | RefSchemaNode | DatetimeSchemaNode | DateSchemaNode | TimeSchemaNode | StringSchemaNode | NumberSchemaNode | UrlSchemaNode | ScalarSchemaNode;
|
|
857
|
+
//#endregion
|
|
858
|
+
//#region src/nodes/parameter.d.ts
|
|
859
|
+
type ParameterLocation = 'path' | 'query' | 'header' | 'cookie';
|
|
860
|
+
/**
|
|
861
|
+
* AST node representing one operation parameter.
|
|
862
|
+
*
|
|
863
|
+
* @example
|
|
864
|
+
* ```ts
|
|
865
|
+
* const param: ParameterNode = {
|
|
866
|
+
* kind: 'Parameter',
|
|
867
|
+
* name: 'petId',
|
|
868
|
+
* in: 'path',
|
|
869
|
+
* schema: createSchema({ type: 'string' }),
|
|
870
|
+
* required: true,
|
|
871
|
+
* }
|
|
872
|
+
* ```
|
|
873
|
+
*/
|
|
874
|
+
type ParameterNode = BaseNode & {
|
|
875
|
+
/**
|
|
876
|
+
* Node kind.
|
|
877
|
+
*/
|
|
878
|
+
kind: 'Parameter';
|
|
879
|
+
/**
|
|
880
|
+
* Parameter name.
|
|
881
|
+
*/
|
|
882
|
+
name: string;
|
|
883
|
+
/**
|
|
884
|
+
* Parameter location (`path`, `query`, `header`, or `cookie`).
|
|
885
|
+
*/
|
|
886
|
+
in: ParameterLocation;
|
|
887
|
+
/**
|
|
888
|
+
* Parameter schema.
|
|
889
|
+
*/
|
|
890
|
+
schema: SchemaNode;
|
|
891
|
+
/**
|
|
892
|
+
* Whether the parameter is required.
|
|
893
|
+
*/
|
|
894
|
+
required: boolean;
|
|
895
|
+
};
|
|
896
|
+
//#endregion
|
|
897
|
+
//#region src/nodes/http.d.ts
|
|
898
|
+
/**
|
|
899
|
+
* All supported HTTP status code literals as strings, as used in API specs
|
|
900
|
+
* (for example, `"200"` and `"404"`).
|
|
901
|
+
*/
|
|
902
|
+
type HttpStatusCode = '100' | '101' | '102' | '103' | '200' | '201' | '202' | '203' | '204' | '205' | '206' | '207' | '208' | '226' | '300' | '301' | '302' | '303' | '304' | '305' | '307' | '308' | '400' | '401' | '402' | '403' | '404' | '405' | '406' | '407' | '408' | '409' | '410' | '411' | '412' | '413' | '414' | '415' | '416' | '417' | '418' | '421' | '422' | '423' | '424' | '425' | '426' | '428' | '429' | '431' | '451' | '500' | '501' | '502' | '503' | '504' | '505' | '506' | '507' | '508' | '510' | '511';
|
|
903
|
+
/**
|
|
904
|
+
* Response status code literal used by operations.
|
|
905
|
+
*
|
|
906
|
+
* Includes specific HTTP status code strings and `"default"` for catch-all responses.
|
|
907
|
+
*
|
|
908
|
+
* @example
|
|
909
|
+
* ```ts
|
|
910
|
+
* const status: StatusCode = '200'
|
|
911
|
+
* const fallback: StatusCode = 'default'
|
|
912
|
+
* ```
|
|
913
|
+
*/
|
|
914
|
+
type StatusCode = HttpStatusCode | 'default';
|
|
915
|
+
/**
|
|
916
|
+
* Supported media type strings used in request and response bodies.
|
|
917
|
+
*
|
|
918
|
+
* @example
|
|
919
|
+
* ```ts
|
|
920
|
+
* const mediaType: MediaType = 'application/json'
|
|
921
|
+
* ```
|
|
922
|
+
*/
|
|
923
|
+
type MediaType = 'application/json' | 'application/xml' | 'application/x-www-form-urlencoded' | 'application/octet-stream' | 'application/pdf' | 'application/zip' | 'application/graphql' | 'multipart/form-data' | 'text/plain' | 'text/html' | 'text/csv' | 'text/xml' | 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp' | 'image/svg+xml' | 'audio/mpeg' | 'video/mp4';
|
|
924
|
+
//#endregion
|
|
925
|
+
//#region src/nodes/response.d.ts
|
|
926
|
+
/**
|
|
927
|
+
* AST node representing one operation response variant.
|
|
928
|
+
*
|
|
929
|
+
* @example
|
|
930
|
+
* ```ts
|
|
931
|
+
* const response: ResponseNode = {
|
|
932
|
+
* kind: 'Response',
|
|
933
|
+
* statusCode: '200',
|
|
934
|
+
* schema: createSchema({ type: 'string' }),
|
|
935
|
+
* }
|
|
936
|
+
* ```
|
|
937
|
+
*/
|
|
938
|
+
type ResponseNode = BaseNode & {
|
|
939
|
+
/**
|
|
940
|
+
* Node kind.
|
|
941
|
+
*/
|
|
942
|
+
kind: 'Response';
|
|
943
|
+
/**
|
|
944
|
+
* HTTP status code or `'default'` for a fallback response.
|
|
945
|
+
*/
|
|
946
|
+
statusCode: StatusCode;
|
|
947
|
+
/**
|
|
948
|
+
* Optional response description.
|
|
949
|
+
*/
|
|
950
|
+
description?: string;
|
|
951
|
+
/**
|
|
952
|
+
* Response body schema.
|
|
953
|
+
*/
|
|
954
|
+
schema: SchemaNode;
|
|
955
|
+
/**
|
|
956
|
+
* Response media type.
|
|
957
|
+
*/
|
|
958
|
+
mediaType?: MediaType | null;
|
|
959
|
+
/**
|
|
960
|
+
* Property keys to exclude from the generated type via `Omit<Type, Keys>`.
|
|
961
|
+
* Set when a referenced schema has `writeOnly` fields that should not appear in response types.
|
|
962
|
+
*/
|
|
963
|
+
keysToOmit?: Array<string>;
|
|
964
|
+
};
|
|
965
|
+
//#endregion
|
|
966
|
+
//#region src/nodes/operation.d.ts
|
|
967
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE';
|
|
968
|
+
/**
|
|
969
|
+
* AST node representing one API operation.
|
|
970
|
+
*
|
|
971
|
+
* @example
|
|
972
|
+
* ```ts
|
|
973
|
+
* const operation: OperationNode = {
|
|
974
|
+
* kind: 'Operation',
|
|
975
|
+
* operationId: 'listPets',
|
|
976
|
+
* method: 'GET',
|
|
977
|
+
* path: '/pets',
|
|
978
|
+
* tags: [],
|
|
979
|
+
* parameters: [],
|
|
980
|
+
* responses: [],
|
|
981
|
+
* }
|
|
982
|
+
* ```
|
|
983
|
+
*/
|
|
984
|
+
type OperationNode = BaseNode & {
|
|
985
|
+
/**
|
|
986
|
+
* Node kind.
|
|
987
|
+
*/
|
|
988
|
+
kind: 'Operation';
|
|
989
|
+
/**
|
|
990
|
+
* Operation identifier, usually from OpenAPI `operationId`.
|
|
991
|
+
*/
|
|
992
|
+
operationId: string;
|
|
993
|
+
/**
|
|
994
|
+
* HTTP Method like 'GET'
|
|
995
|
+
*/
|
|
996
|
+
method: HttpMethod;
|
|
997
|
+
/**
|
|
998
|
+
* Express-style path string, for example `/pets/:petId`.
|
|
999
|
+
* OpenAPI `{param}` parts are converted to `:param`.
|
|
1000
|
+
*/
|
|
1001
|
+
path: string;
|
|
1002
|
+
/**
|
|
1003
|
+
* Group labels for the operation.
|
|
1004
|
+
* Usually copied from OpenAPI `tags`.
|
|
1005
|
+
*/
|
|
1006
|
+
tags: Array<string>;
|
|
1007
|
+
/**
|
|
1008
|
+
* Short one-line operation summary.
|
|
1009
|
+
*/
|
|
1010
|
+
summary?: string;
|
|
1011
|
+
/**
|
|
1012
|
+
* Full operation description.
|
|
1013
|
+
*/
|
|
1014
|
+
description?: string;
|
|
1015
|
+
/**
|
|
1016
|
+
* Marks the operation as deprecated.
|
|
1017
|
+
*/
|
|
1018
|
+
deprecated?: boolean;
|
|
1019
|
+
/**
|
|
1020
|
+
* Parameters that could be used, we have QueryParams, PathParams, HeaderParams and CookieParams
|
|
1021
|
+
*/
|
|
1022
|
+
parameters: Array<ParameterNode>;
|
|
1023
|
+
/**
|
|
1024
|
+
* Request body metadata for the operation.
|
|
1025
|
+
*/
|
|
1026
|
+
requestBody?: {
|
|
1027
|
+
/**
|
|
1028
|
+
* Human-readable request body description.
|
|
1029
|
+
*/
|
|
1030
|
+
description?: string;
|
|
1031
|
+
/**
|
|
1032
|
+
* Request body schema.
|
|
1033
|
+
* For OpenAPI, this is the schema from the first `content` entry.
|
|
1034
|
+
*/
|
|
1035
|
+
schema?: SchemaNode;
|
|
1036
|
+
/**
|
|
1037
|
+
* Property keys to exclude from the generated request body type via `Omit<Type, Keys>`.
|
|
1038
|
+
* Set when a referenced schema has `readOnly` fields that should be omitted in request types.
|
|
1039
|
+
*/
|
|
1040
|
+
keysToOmit?: Array<string>;
|
|
1041
|
+
};
|
|
1042
|
+
/**
|
|
1043
|
+
* Operation responses.
|
|
1044
|
+
*/
|
|
1045
|
+
responses: Array<ResponseNode>;
|
|
1046
|
+
};
|
|
1047
|
+
//#endregion
|
|
1048
|
+
//#region src/nodes/root.d.ts
|
|
1049
|
+
/**
|
|
1050
|
+
* Basic metadata for an API document.
|
|
1051
|
+
* Adapters fill fields that exist in their source format.
|
|
1052
|
+
*
|
|
1053
|
+
* @example
|
|
1054
|
+
* ```ts
|
|
1055
|
+
* const meta: RootMeta = { title: 'Pet API', version: '1.0.0' }
|
|
1056
|
+
* ```
|
|
1057
|
+
*/
|
|
1058
|
+
type RootMeta = {
|
|
1059
|
+
/**
|
|
1060
|
+
* API title (from `info.title` in OAS/AsyncAPI).
|
|
1061
|
+
*/
|
|
1062
|
+
title?: string;
|
|
1063
|
+
/**
|
|
1064
|
+
* API description (from `info.description` in OAS/AsyncAPI).
|
|
1065
|
+
*/
|
|
1066
|
+
description?: string;
|
|
1067
|
+
/**
|
|
1068
|
+
* API version string (from `info.version` in OAS/AsyncAPI).
|
|
1069
|
+
*/
|
|
1070
|
+
version?: string;
|
|
1071
|
+
/**
|
|
1072
|
+
* Resolved API base URL.
|
|
1073
|
+
* For OpenAPI and AsyncAPI, this comes from the selected server URL.
|
|
1074
|
+
*/
|
|
1075
|
+
baseURL?: string;
|
|
1076
|
+
};
|
|
1077
|
+
/**
|
|
1078
|
+
* Root AST node that contains all schemas and operations for one API document.
|
|
1079
|
+
*
|
|
1080
|
+
* @example
|
|
1081
|
+
* ```ts
|
|
1082
|
+
* const root: RootNode = {
|
|
1083
|
+
* kind: 'Root',
|
|
1084
|
+
* schemas: [],
|
|
1085
|
+
* operations: [],
|
|
1086
|
+
* }
|
|
1087
|
+
* ```
|
|
1088
|
+
*/
|
|
1089
|
+
type RootNode = BaseNode & {
|
|
1090
|
+
/**
|
|
1091
|
+
* Node kind.
|
|
1092
|
+
*/
|
|
1093
|
+
kind: 'Root';
|
|
1094
|
+
/**
|
|
1095
|
+
* All schema nodes in the document.
|
|
1096
|
+
*/
|
|
1097
|
+
schemas: Array<SchemaNode>;
|
|
1098
|
+
/**
|
|
1099
|
+
* All operation nodes in the document.
|
|
1100
|
+
*/
|
|
1101
|
+
operations: Array<OperationNode>;
|
|
1102
|
+
/**
|
|
1103
|
+
* Optional document metadata populated by the adapter.
|
|
1104
|
+
*/
|
|
1105
|
+
meta?: RootMeta;
|
|
1106
|
+
};
|
|
1107
|
+
//#endregion
|
|
1108
|
+
//#region src/nodes/index.d.ts
|
|
1109
|
+
/**
|
|
1110
|
+
* Union of all AST node types.
|
|
1111
|
+
*
|
|
1112
|
+
* This lets TypeScript narrow types in `switch (node.kind)` blocks.
|
|
1113
|
+
*
|
|
1114
|
+
* @example
|
|
1115
|
+
* ```ts
|
|
1116
|
+
* function getKind(node: Node): string {
|
|
1117
|
+
* switch (node.kind) {
|
|
1118
|
+
* case 'Root':
|
|
1119
|
+
* return 'root'
|
|
1120
|
+
* default:
|
|
1121
|
+
* return 'other'
|
|
1122
|
+
* }
|
|
1123
|
+
* }
|
|
1124
|
+
* ```
|
|
1125
|
+
*/
|
|
1126
|
+
type Node = RootNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode | FunctionNode;
|
|
1127
|
+
//#endregion
|
|
1128
|
+
//#region src/infer.d.ts
|
|
1129
|
+
/**
|
|
1130
|
+
* Shared parser options used by OAS-to-AST inference and parser flows.
|
|
1131
|
+
*/
|
|
1132
|
+
type ParserOptions = {
|
|
1133
|
+
/**
|
|
1134
|
+
* How `format: 'date-time'` schemas are represented. `false` falls through to a plain string.
|
|
1135
|
+
*/
|
|
1136
|
+
dateType: false | 'string' | 'stringOffset' | 'stringLocal' | 'date';
|
|
1137
|
+
/**
|
|
1138
|
+
* Whether `type: 'integer'` and `format: 'int64'` produce `number` or `bigint` nodes.
|
|
1139
|
+
*/
|
|
1140
|
+
integerType?: 'number' | 'bigint';
|
|
1141
|
+
/**
|
|
1142
|
+
* AST type used when no schema type can be inferred.
|
|
1143
|
+
*/
|
|
1144
|
+
unknownType: 'any' | 'unknown' | 'void';
|
|
1145
|
+
/**
|
|
1146
|
+
* AST type used for completely empty schemas (`{}`).
|
|
1147
|
+
*/
|
|
1148
|
+
emptySchemaType: 'any' | 'unknown' | 'void';
|
|
1149
|
+
/**
|
|
1150
|
+
* Suffix appended to derived enum names when building property schema names.
|
|
1151
|
+
*/
|
|
1152
|
+
enumSuffix: 'enum' | (string & {});
|
|
1153
|
+
};
|
|
1154
|
+
/**
|
|
1155
|
+
* Maps each `dateType` option value to the AST node produced by `format: 'date-time'`.
|
|
1156
|
+
*/
|
|
1157
|
+
type DateTimeNodeByDateType = {
|
|
1158
|
+
date: DateSchemaNode;
|
|
1159
|
+
string: DatetimeSchemaNode;
|
|
1160
|
+
stringOffset: DatetimeSchemaNode;
|
|
1161
|
+
stringLocal: DatetimeSchemaNode;
|
|
1162
|
+
false: StringSchemaNode;
|
|
1163
|
+
};
|
|
1164
|
+
/**
|
|
1165
|
+
* Resolves the AST node produced by `format: 'date-time'` based on the `dateType` option.
|
|
1166
|
+
*/
|
|
1167
|
+
type ResolveDateTimeNode<TDateType extends ParserOptions['dateType']> = DateTimeNodeByDateType[TDateType extends keyof DateTimeNodeByDateType ? TDateType : 'string'];
|
|
1168
|
+
/**
|
|
1169
|
+
* Ordered list of `[schema-shape, SchemaNode]` pairs.
|
|
1170
|
+
* `InferSchemaNode` walks this tuple in order and returns the first matching node type.
|
|
1171
|
+
*/
|
|
1172
|
+
type SchemaNodeMap<TDateType extends ParserOptions['dateType'] = 'string'> = [[{
|
|
1173
|
+
$ref: string;
|
|
1174
|
+
}, RefSchemaNode], [{
|
|
1175
|
+
allOf: ReadonlyArray<unknown>;
|
|
1176
|
+
properties: object;
|
|
1177
|
+
}, IntersectionSchemaNode], [{
|
|
1178
|
+
allOf: readonly [unknown, unknown, ...unknown[]];
|
|
1179
|
+
}, IntersectionSchemaNode], [{
|
|
1180
|
+
allOf: ReadonlyArray<unknown>;
|
|
1181
|
+
}, SchemaNode], [{
|
|
1182
|
+
oneOf: ReadonlyArray<unknown>;
|
|
1183
|
+
}, UnionSchemaNode], [{
|
|
1184
|
+
anyOf: ReadonlyArray<unknown>;
|
|
1185
|
+
}, UnionSchemaNode], [{
|
|
1186
|
+
const: null;
|
|
1187
|
+
}, ScalarSchemaNode], [{
|
|
1188
|
+
const: string | number | boolean;
|
|
1189
|
+
}, EnumSchemaNode], [{
|
|
1190
|
+
type: ReadonlyArray<string>;
|
|
1191
|
+
}, UnionSchemaNode], [{
|
|
1192
|
+
type: 'array';
|
|
1193
|
+
enum: ReadonlyArray<unknown>;
|
|
1194
|
+
}, ArraySchemaNode], [{
|
|
1195
|
+
enum: ReadonlyArray<unknown>;
|
|
1196
|
+
}, EnumSchemaNode], [{
|
|
1197
|
+
type: 'enum';
|
|
1198
|
+
}, EnumSchemaNode], [{
|
|
1199
|
+
type: 'union';
|
|
1200
|
+
}, UnionSchemaNode], [{
|
|
1201
|
+
type: 'intersection';
|
|
1202
|
+
}, IntersectionSchemaNode], [{
|
|
1203
|
+
type: 'tuple';
|
|
1204
|
+
}, ArraySchemaNode], [{
|
|
1205
|
+
type: 'ref';
|
|
1206
|
+
}, RefSchemaNode], [{
|
|
1207
|
+
type: 'datetime';
|
|
1208
|
+
}, DatetimeSchemaNode], [{
|
|
1209
|
+
type: 'date';
|
|
1210
|
+
}, DateSchemaNode], [{
|
|
1211
|
+
type: 'time';
|
|
1212
|
+
}, TimeSchemaNode], [{
|
|
1213
|
+
type: 'url';
|
|
1214
|
+
}, UrlSchemaNode], [{
|
|
1215
|
+
type: 'object';
|
|
1216
|
+
}, ObjectSchemaNode], [{
|
|
1217
|
+
additionalProperties: boolean | {};
|
|
1218
|
+
}, ObjectSchemaNode], [{
|
|
1219
|
+
type: 'array';
|
|
1220
|
+
}, ArraySchemaNode], [{
|
|
1221
|
+
items: object;
|
|
1222
|
+
}, ArraySchemaNode], [{
|
|
1223
|
+
prefixItems: ReadonlyArray<unknown>;
|
|
1224
|
+
}, ArraySchemaNode], [{
|
|
1225
|
+
type: string;
|
|
1226
|
+
format: 'date-time';
|
|
1227
|
+
}, ResolveDateTimeNode<TDateType>], [{
|
|
1228
|
+
type: string;
|
|
1229
|
+
format: 'date';
|
|
1230
|
+
}, DateSchemaNode], [{
|
|
1231
|
+
type: string;
|
|
1232
|
+
format: 'time';
|
|
1233
|
+
}, TimeSchemaNode], [{
|
|
1234
|
+
format: 'date-time';
|
|
1235
|
+
}, ResolveDateTimeNode<TDateType>], [{
|
|
1236
|
+
format: 'date';
|
|
1237
|
+
}, DateSchemaNode], [{
|
|
1238
|
+
format: 'time';
|
|
1239
|
+
}, TimeSchemaNode], [{
|
|
1240
|
+
type: 'string';
|
|
1241
|
+
}, StringSchemaNode], [{
|
|
1242
|
+
type: 'number';
|
|
1243
|
+
}, NumberSchemaNode], [{
|
|
1244
|
+
type: 'integer';
|
|
1245
|
+
}, NumberSchemaNode], [{
|
|
1246
|
+
type: 'bigint';
|
|
1247
|
+
}, NumberSchemaNode], [{
|
|
1248
|
+
type: string;
|
|
1249
|
+
}, ScalarSchemaNode], [{
|
|
1250
|
+
minLength: number;
|
|
1251
|
+
}, StringSchemaNode], [{
|
|
1252
|
+
maxLength: number;
|
|
1253
|
+
}, StringSchemaNode], [{
|
|
1254
|
+
pattern: string;
|
|
1255
|
+
}, StringSchemaNode], [{
|
|
1256
|
+
minimum: number;
|
|
1257
|
+
}, NumberSchemaNode], [{
|
|
1258
|
+
maximum: number;
|
|
1259
|
+
}, NumberSchemaNode]];
|
|
1260
|
+
/**
|
|
1261
|
+
* Infers the matching AST `SchemaNode` type from an input schema shape.
|
|
1262
|
+
*/
|
|
1263
|
+
type InferSchemaNode<TSchema extends object, TDateType extends ParserOptions['dateType'] = 'string', TEntries extends ReadonlyArray<[object, SchemaNode]> = SchemaNodeMap<TDateType>> = TEntries extends [infer TEntry extends [object, SchemaNode], ...infer TRest extends ReadonlyArray<[object, SchemaNode]>] ? TSchema extends TEntry[0] ? TEntry[1] : InferSchemaNode<TSchema, TDateType, TRest> : SchemaNode;
|
|
1264
|
+
/**
|
|
1265
|
+
* Backward-compatible alias for `InferSchemaNode`.
|
|
1266
|
+
*/
|
|
1267
|
+
type InferSchema<TSchema extends object, TDateType extends ParserOptions['dateType'] = 'string', TEntries extends ReadonlyArray<[object, SchemaNode]> = SchemaNodeMap<TDateType>> = InferSchemaNode<TSchema, TDateType, TEntries>;
|
|
1268
|
+
//#endregion
|
|
1269
|
+
//#region src/factory.d.ts
|
|
1270
|
+
/**
|
|
1271
|
+
* Distributive `Omit` that preserves each member of a union.
|
|
1272
|
+
*
|
|
1273
|
+
* @example
|
|
1274
|
+
* ```ts
|
|
1275
|
+
* type A = { kind: 'a'; keep: string; drop: number }
|
|
1276
|
+
* type B = { kind: 'b'; keep: boolean; drop: number }
|
|
1277
|
+
* type Result = DistributiveOmit<A | B, 'drop'>
|
|
1278
|
+
* // -> { kind: 'a'; keep: string } | { kind: 'b'; keep: boolean }
|
|
1279
|
+
* ```
|
|
1280
|
+
*/
|
|
1281
|
+
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
|
1282
|
+
type CreateSchemaObjectInput = Omit<ObjectSchemaNode, 'kind' | 'properties'> & {
|
|
1283
|
+
properties?: Array<PropertyNode>;
|
|
1284
|
+
};
|
|
1285
|
+
type CreateSchemaInput = CreateSchemaObjectInput | DistributiveOmit<Exclude<SchemaNode, ObjectSchemaNode>, 'kind'>;
|
|
1286
|
+
type CreateSchemaOutput<T extends CreateSchemaInput> = InferSchemaNode<T> & {
|
|
1287
|
+
kind: 'Schema';
|
|
1288
|
+
};
|
|
1289
|
+
/**
|
|
1290
|
+
* Creates a `RootNode` with stable defaults for `schemas` and `operations`.
|
|
1291
|
+
*
|
|
1292
|
+
* @example
|
|
1293
|
+
* ```ts
|
|
1294
|
+
* const root = createRoot()
|
|
1295
|
+
* // { kind: 'Root', schemas: [], operations: [] }
|
|
1296
|
+
* ```
|
|
1297
|
+
*
|
|
1298
|
+
* @example
|
|
1299
|
+
* ```ts
|
|
1300
|
+
* const root = createRoot({ schemas: [petSchema] })
|
|
1301
|
+
* // keeps default operations: []
|
|
1302
|
+
* ```
|
|
1303
|
+
*/
|
|
1304
|
+
declare function createRoot(overrides?: Partial<Omit<RootNode, 'kind'>>): RootNode;
|
|
1305
|
+
/**
|
|
1306
|
+
* Creates an `OperationNode` with default empty arrays for `tags`, `parameters`, and `responses`.
|
|
1307
|
+
*
|
|
1308
|
+
* @example
|
|
1309
|
+
* ```ts
|
|
1310
|
+
* const operation = createOperation({
|
|
1311
|
+
* operationId: 'getPetById',
|
|
1312
|
+
* method: 'GET',
|
|
1313
|
+
* path: '/pet/{petId}',
|
|
1314
|
+
* })
|
|
1315
|
+
* // tags, parameters, and responses are []
|
|
1316
|
+
* ```
|
|
1317
|
+
*
|
|
1318
|
+
* @example
|
|
1319
|
+
* ```ts
|
|
1320
|
+
* const operation = createOperation({
|
|
1321
|
+
* operationId: 'findPets',
|
|
1322
|
+
* method: 'GET',
|
|
1323
|
+
* path: '/pet/findByStatus',
|
|
1324
|
+
* tags: ['pet'],
|
|
1325
|
+
* })
|
|
1326
|
+
* ```
|
|
1327
|
+
*/
|
|
1328
|
+
declare function createOperation(props: Pick<OperationNode, 'operationId' | 'method' | 'path'> & Partial<Omit<OperationNode, 'kind' | 'operationId' | 'method' | 'path'>>): OperationNode;
|
|
1329
|
+
/**
|
|
1330
|
+
* Creates a `SchemaNode`, narrowed to the variant of `props.type`.
|
|
1331
|
+
* For object schemas, `properties` defaults to an empty array.
|
|
1332
|
+
*
|
|
1333
|
+
* @example
|
|
1334
|
+
* ```ts
|
|
1335
|
+
* const scalar = createSchema({ type: 'string' })
|
|
1336
|
+
* // { kind: 'Schema', type: 'string' }
|
|
1337
|
+
* ```
|
|
1338
|
+
*
|
|
1339
|
+
* @example
|
|
1340
|
+
* ```ts
|
|
1341
|
+
* const object = createSchema({ type: 'object' })
|
|
1342
|
+
* // { kind: 'Schema', type: 'object', properties: [] }
|
|
1343
|
+
* ```
|
|
1344
|
+
*
|
|
1345
|
+
* @example
|
|
1346
|
+
* ```ts
|
|
1347
|
+
* const enumSchema = createSchema({
|
|
1348
|
+
* type: 'enum',
|
|
1349
|
+
* primitive: 'string',
|
|
1350
|
+
* enumValues: ['available', 'pending'],
|
|
1351
|
+
* })
|
|
1352
|
+
* ```
|
|
1353
|
+
*/
|
|
1354
|
+
declare function createSchema<T extends CreateSchemaInput>(props: T): CreateSchemaOutput<T>;
|
|
1355
|
+
declare function createSchema(props: CreateSchemaInput): SchemaNode;
|
|
1356
|
+
/**
|
|
1357
|
+
* Creates a `PropertyNode`.
|
|
1358
|
+
*
|
|
1359
|
+
* `required` defaults to `false`.
|
|
1360
|
+
* `schema.optional` and `schema.nullish` are derived from `required` and `schema.nullable`.
|
|
1361
|
+
*
|
|
1362
|
+
* @example
|
|
1363
|
+
* ```ts
|
|
1364
|
+
* const property = createProperty({
|
|
1365
|
+
* name: 'status',
|
|
1366
|
+
* schema: createSchema({ type: 'string' }),
|
|
1367
|
+
* })
|
|
1368
|
+
* // required=false, schema.optional=true
|
|
1369
|
+
* ```
|
|
1370
|
+
*
|
|
1371
|
+
* @example
|
|
1372
|
+
* ```ts
|
|
1373
|
+
* const property = createProperty({
|
|
1374
|
+
* name: 'status',
|
|
1375
|
+
* required: true,
|
|
1376
|
+
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1377
|
+
* })
|
|
1378
|
+
* // required=true, no optional/nullish
|
|
1379
|
+
* ```
|
|
1380
|
+
*/
|
|
1381
|
+
declare function createProperty(props: Pick<PropertyNode, 'name' | 'schema'> & Partial<Omit<PropertyNode, 'kind' | 'name' | 'schema'>>): PropertyNode;
|
|
1382
|
+
/**
|
|
1383
|
+
* Creates a `ParameterNode`.
|
|
1384
|
+
*
|
|
1385
|
+
* `required` defaults to `false`.
|
|
1386
|
+
* Nested schema flags are set from `required` and `schema.nullable`.
|
|
1387
|
+
*
|
|
1388
|
+
* @example
|
|
1389
|
+
* ```ts
|
|
1390
|
+
* const param = createParameter({
|
|
1391
|
+
* name: 'petId',
|
|
1392
|
+
* in: 'path',
|
|
1393
|
+
* required: true,
|
|
1394
|
+
* schema: createSchema({ type: 'string' }),
|
|
1395
|
+
* })
|
|
1396
|
+
* ```
|
|
1397
|
+
*
|
|
1398
|
+
* @example
|
|
1399
|
+
* ```ts
|
|
1400
|
+
* const param = createParameter({
|
|
1401
|
+
* name: 'status',
|
|
1402
|
+
* in: 'query',
|
|
1403
|
+
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1404
|
+
* })
|
|
1405
|
+
* // required=false, schema.nullish=true
|
|
1406
|
+
* ```
|
|
1407
|
+
*/
|
|
1408
|
+
declare function createParameter(props: Pick<ParameterNode, 'name' | 'in' | 'schema'> & Partial<Omit<ParameterNode, 'kind' | 'name' | 'in' | 'schema'>>): ParameterNode;
|
|
1409
|
+
/**
|
|
1410
|
+
* Creates a `ResponseNode`.
|
|
1411
|
+
*
|
|
1412
|
+
* @example
|
|
1413
|
+
* ```ts
|
|
1414
|
+
* const response = createResponse({
|
|
1415
|
+
* statusCode: '200',
|
|
1416
|
+
* description: 'Success',
|
|
1417
|
+
* schema: createSchema({ type: 'object', properties: [] }),
|
|
1418
|
+
* })
|
|
1419
|
+
* ```
|
|
1420
|
+
*/
|
|
1421
|
+
declare function createResponse(props: Pick<ResponseNode, 'statusCode' | 'schema'> & Partial<Omit<ResponseNode, 'kind' | 'statusCode' | 'schema'>>): ResponseNode;
|
|
1422
|
+
/**
|
|
1423
|
+
* Creates a single-property object schema used as a discriminator literal.
|
|
1424
|
+
*
|
|
1425
|
+
* @example
|
|
1426
|
+
* ```ts
|
|
1427
|
+
* createDiscriminantNode({ propertyName: 'type', value: 'dog' })
|
|
1428
|
+
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
|
|
1429
|
+
* ```
|
|
1430
|
+
*/
|
|
1431
|
+
declare function createDiscriminantNode({
|
|
1432
|
+
propertyName,
|
|
1433
|
+
value
|
|
1434
|
+
}: {
|
|
1435
|
+
propertyName: string;
|
|
1436
|
+
value: string;
|
|
1437
|
+
}): SchemaNode;
|
|
1438
|
+
/**
|
|
1439
|
+
* Creates a `FunctionParameterNode`.
|
|
1440
|
+
*
|
|
1441
|
+
* `optional` defaults to `false`.
|
|
1442
|
+
*
|
|
1443
|
+
* @example Required typed param
|
|
1444
|
+
* ```ts
|
|
1445
|
+
* createFunctionParameter({ name: 'petId', type: 'string' })
|
|
1446
|
+
* // → petId: string
|
|
1447
|
+
* ```
|
|
1448
|
+
*
|
|
1449
|
+
* @example Optional param
|
|
1450
|
+
* ```ts
|
|
1451
|
+
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
|
|
1452
|
+
* // → params?: QueryParams
|
|
1453
|
+
* ```
|
|
1454
|
+
*
|
|
1455
|
+
* @example Param with default (implicitly optional; cannot combine with `optional: true`)
|
|
1456
|
+
* ```ts
|
|
1457
|
+
* createFunctionParameter({ name: 'config', type: 'RequestConfig', default: '{}' })
|
|
1458
|
+
* // → config: RequestConfig = {}
|
|
1459
|
+
* ```
|
|
1460
|
+
*/
|
|
1461
|
+
declare function createFunctionParameter(props: {
|
|
1462
|
+
name: string;
|
|
1463
|
+
type?: string;
|
|
1464
|
+
rest?: boolean;
|
|
1465
|
+
} & ({
|
|
1466
|
+
optional: true;
|
|
1467
|
+
default?: never;
|
|
1468
|
+
} | {
|
|
1469
|
+
optional?: false;
|
|
1470
|
+
default?: string;
|
|
1471
|
+
})): FunctionParameterNode;
|
|
1472
|
+
/**
|
|
1473
|
+
* Creates an `ObjectBindingParameterNode` for object-destructured parameter groups.
|
|
1474
|
+
*
|
|
1475
|
+
* @example Destructured object param
|
|
1476
|
+
* ```ts
|
|
1477
|
+
* createObjectBindingParameter({
|
|
1478
|
+
* properties: [
|
|
1479
|
+
* createFunctionParameter({ name: 'id', type: 'string', optional: false }),
|
|
1480
|
+
* createFunctionParameter({ name: 'name', type: 'string', optional: true }),
|
|
1481
|
+
* ],
|
|
1482
|
+
* default: '{}',
|
|
1483
|
+
* })
|
|
1484
|
+
* // declaration → { id, name? }: { id: string; name?: string } = {}
|
|
1485
|
+
* // call → { id, name }
|
|
1486
|
+
* ```
|
|
1487
|
+
*
|
|
1488
|
+
* @example Inline mode — children emitted as individual top-level parameters
|
|
1489
|
+
* ```ts
|
|
1490
|
+
* createObjectBindingParameter({
|
|
1491
|
+
* properties: [createFunctionParameter({ name: 'petId', type: 'string', optional: false })],
|
|
1492
|
+
* inline: true,
|
|
1493
|
+
* })
|
|
1494
|
+
* // declaration → petId: string
|
|
1495
|
+
* // call → petId
|
|
1496
|
+
* ```
|
|
1497
|
+
*/
|
|
1498
|
+
declare function createObjectBindingParameter(props: Pick<ObjectBindingParameterNode, 'properties'> & Partial<Omit<ObjectBindingParameterNode, 'kind' | 'properties'>>): ObjectBindingParameterNode;
|
|
1499
|
+
/**
|
|
1500
|
+
* Creates a `FunctionParametersNode` from an ordered list of parameters.
|
|
1501
|
+
*
|
|
1502
|
+
* @example
|
|
1503
|
+
* ```ts
|
|
1504
|
+
* createFunctionParameters({
|
|
1505
|
+
* params: [
|
|
1506
|
+
* createFunctionParameter({ name: 'petId', type: 'string', optional: false }),
|
|
1507
|
+
* createFunctionParameter({ name: 'config', type: 'RequestConfig', optional: false, default: '{}' }),
|
|
1508
|
+
* ],
|
|
1509
|
+
* })
|
|
1510
|
+
* ```
|
|
1511
|
+
*
|
|
1512
|
+
* @example
|
|
1513
|
+
* ```ts
|
|
1514
|
+
* const empty = createFunctionParameters()
|
|
1515
|
+
* // { kind: 'FunctionParameters', params: [] }
|
|
1516
|
+
* ```
|
|
1517
|
+
*/
|
|
1518
|
+
declare function createFunctionParameters(props?: Partial<Omit<FunctionParametersNode, 'kind'>>): FunctionParametersNode;
|
|
1519
|
+
//#endregion
|
|
1520
|
+
//#region src/printers/printer.d.ts
|
|
1521
|
+
/**
|
|
1522
|
+
* Runtime context passed as `this` to printer handlers.
|
|
1523
|
+
*
|
|
1524
|
+
* `this.print` always dispatches to node-level handlers from `nodes`.
|
|
1525
|
+
*
|
|
1526
|
+
* @example
|
|
1527
|
+
* ```ts
|
|
1528
|
+
* const context: PrinterHandlerContext<string, {}> = {
|
|
1529
|
+
* options: {},
|
|
1530
|
+
* print: () => 'value',
|
|
1531
|
+
* }
|
|
1532
|
+
* ```
|
|
1533
|
+
*/
|
|
1534
|
+
type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
1535
|
+
/**
|
|
1536
|
+
* Recursively print a nested `SchemaNode` using the node-level handlers.
|
|
1537
|
+
*/
|
|
1538
|
+
print: (node: SchemaNode) => TOutput | null | undefined;
|
|
1539
|
+
/**
|
|
1540
|
+
* Options for this printer instance.
|
|
1541
|
+
*/
|
|
1542
|
+
options: TOptions;
|
|
1543
|
+
};
|
|
1544
|
+
/**
|
|
1545
|
+
* Handler for one schema node type.
|
|
1546
|
+
*
|
|
1547
|
+
* Use a regular function (not an arrow function) if you need `this`.
|
|
1548
|
+
*
|
|
1549
|
+
* @example
|
|
1550
|
+
* ```ts
|
|
1551
|
+
* const handler: PrinterHandler<string, {}, 'string'> = function () {
|
|
1552
|
+
* return 'string'
|
|
1553
|
+
* }
|
|
1554
|
+
* ```
|
|
1555
|
+
*/
|
|
1556
|
+
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null | undefined;
|
|
1557
|
+
/**
|
|
1558
|
+
* Generic shape used by `definePrinter`.
|
|
1559
|
+
*
|
|
1560
|
+
* - `TName` — unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
1561
|
+
* - `TOptions` — options passed to and stored on the printer instance
|
|
1562
|
+
* - `TOutput` — the type emitted by node handlers
|
|
1563
|
+
* - `TPrintOutput` — type returned by public `print` (defaults to `TOutput`)
|
|
1564
|
+
*
|
|
1565
|
+
* @example
|
|
1566
|
+
* ```ts
|
|
1567
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
1568
|
+
* ```
|
|
1569
|
+
*/
|
|
1570
|
+
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
1571
|
+
name: TName;
|
|
1572
|
+
options: TOptions;
|
|
1573
|
+
output: TOutput;
|
|
1574
|
+
printOutput: TPrintOutput;
|
|
1575
|
+
};
|
|
1576
|
+
/**
|
|
1577
|
+
* Printer instance returned by a printer factory.
|
|
1578
|
+
*
|
|
1579
|
+
* @example
|
|
1580
|
+
* ```ts
|
|
1581
|
+
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
1582
|
+
* ```
|
|
1583
|
+
*/
|
|
1584
|
+
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
1585
|
+
/**
|
|
1586
|
+
* Unique identifier supplied at creation time.
|
|
1587
|
+
*/
|
|
1588
|
+
name: T['name'];
|
|
1589
|
+
/**
|
|
1590
|
+
* Options for this printer instance.
|
|
1591
|
+
*/
|
|
1592
|
+
options: T['options'];
|
|
1593
|
+
/**
|
|
1594
|
+
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
1595
|
+
* higher-level function (which may produce full declarations).
|
|
1596
|
+
* Otherwise, falls back to the node-level dispatcher.
|
|
1597
|
+
*/
|
|
1598
|
+
print: (node: SchemaNode) => T['printOutput'] | null | undefined;
|
|
1599
|
+
};
|
|
1600
|
+
/**
|
|
1601
|
+
* Builder function passed to `definePrinter`.
|
|
1602
|
+
*
|
|
1603
|
+
* It receives resolved options and returns:
|
|
1604
|
+
* - `name`
|
|
1605
|
+
* - `options`
|
|
1606
|
+
* - `nodes` handlers
|
|
1607
|
+
* - optional top-level `print` override
|
|
1608
|
+
*
|
|
1609
|
+
* @example
|
|
1610
|
+
* ```ts
|
|
1611
|
+
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
1612
|
+
* ```
|
|
1613
|
+
*/
|
|
1614
|
+
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
1615
|
+
name: T['name'];
|
|
1616
|
+
/**
|
|
1617
|
+
* Options to store on the printer.
|
|
1618
|
+
*/
|
|
1619
|
+
options: T['options'];
|
|
1620
|
+
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
1621
|
+
/**
|
|
1622
|
+
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
1623
|
+
* `this.print(node)` inside this function calls the node-level dispatcher (`nodes` handlers),
|
|
1624
|
+
* not the override itself — so recursion is safe.
|
|
1625
|
+
*/
|
|
1626
|
+
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null | undefined;
|
|
1627
|
+
};
|
|
1628
|
+
/**
|
|
1629
|
+
* Creates a schema printer factory.
|
|
1630
|
+
*
|
|
1631
|
+
* This function wraps a builder and makes options optional at call sites.
|
|
1632
|
+
*
|
|
1633
|
+
* The builder receives resolved options and returns:
|
|
1634
|
+
* - `name` — a unique identifier for the printer
|
|
1635
|
+
* - `options` — options stored on the returned printer instance
|
|
1636
|
+
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
1637
|
+
* - `print` _(optional)_ — top-level override exposed as `printer.print`
|
|
1638
|
+
* - Inside this function, `this.print(node)` still dispatches to the `nodes` map
|
|
1639
|
+
* - This keeps recursion safe and avoids self-calls
|
|
1640
|
+
*
|
|
1641
|
+
* When no `print` override is provided, `printer.print` is the node-level dispatcher directly.
|
|
1642
|
+
*
|
|
1643
|
+
* @example Basic usage — Zod schema printer
|
|
1644
|
+
* ```ts
|
|
1645
|
+
* type ZodPrinter = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
1646
|
+
*
|
|
1647
|
+
* export const zodPrinter = definePrinter<ZodPrinter>((options) => ({
|
|
1648
|
+
* name: 'zod',
|
|
1649
|
+
* options: { strict: options.strict ?? true },
|
|
1650
|
+
* nodes: {
|
|
1651
|
+
* string: () => 'z.string()',
|
|
1652
|
+
* object(node) {
|
|
1653
|
+
* const props = node.properties.map(p => `${p.name}: ${this.print(p.schema)}`).join(', ')
|
|
1654
|
+
* return `z.object({ ${props} })`
|
|
1655
|
+
* },
|
|
1656
|
+
* },
|
|
1657
|
+
* }))
|
|
1658
|
+
* ```
|
|
1659
|
+
*/
|
|
1660
|
+
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
|
|
1661
|
+
//#endregion
|
|
1662
|
+
//#region src/refs.d.ts
|
|
1663
|
+
/**
|
|
1664
|
+
* Lookup map from schema name to `SchemaNode`.
|
|
1665
|
+
*/
|
|
1666
|
+
type RefMap = Map<string, SchemaNode>;
|
|
1667
|
+
/**
|
|
1668
|
+
* Returns the last path segment of a reference string.
|
|
1669
|
+
*
|
|
1670
|
+
* Example: `#/components/schemas/Pet` becomes `Pet`.
|
|
1671
|
+
*
|
|
1672
|
+
* @example
|
|
1673
|
+
* ```ts
|
|
1674
|
+
* extractRefName('#/components/schemas/Pet') // 'Pet'
|
|
1675
|
+
* ```
|
|
1676
|
+
*/
|
|
1677
|
+
declare function extractRefName(ref: string): string;
|
|
1678
|
+
/**
|
|
1679
|
+
* Builds a `RefMap` from `root.schemas` using each schema's `name`.
|
|
1680
|
+
*
|
|
1681
|
+
* Unnamed schemas are skipped.
|
|
1682
|
+
*
|
|
1683
|
+
* @example
|
|
1684
|
+
* ```ts
|
|
1685
|
+
* const refMap = buildRefMap(root)
|
|
1686
|
+
* const pet = refMap.get('Pet')
|
|
1687
|
+
* ```
|
|
1688
|
+
*/
|
|
1689
|
+
declare function buildRefMap(root: RootNode): RefMap;
|
|
1690
|
+
/**
|
|
1691
|
+
* Resolves a schema by name from a `RefMap`.
|
|
1692
|
+
*
|
|
1693
|
+
* @example
|
|
1694
|
+
* ```ts
|
|
1695
|
+
* const petSchema = resolveRef(refMap, 'Pet')
|
|
1696
|
+
* ```
|
|
1697
|
+
*/
|
|
1698
|
+
declare function resolveRef(refMap: RefMap, ref: string): SchemaNode | undefined;
|
|
1699
|
+
/**
|
|
1700
|
+
* Converts a `RefMap` into a plain object.
|
|
1701
|
+
*
|
|
1702
|
+
* @example
|
|
1703
|
+
* ```ts
|
|
1704
|
+
* const refsObject = refMapToObject(refMap)
|
|
1705
|
+
* ```
|
|
1706
|
+
*/
|
|
1707
|
+
declare function refMapToObject(refMap: RefMap): Record<string, SchemaNode>;
|
|
1708
|
+
//#endregion
|
|
1709
|
+
//#region src/visitor.d.ts
|
|
1710
|
+
/**
|
|
1711
|
+
* Ordered mapping of `[NodeType, ParentType]` pairs.
|
|
1712
|
+
*
|
|
1713
|
+
* `ParentOf` uses this map to find parent types.
|
|
1714
|
+
*/
|
|
1715
|
+
type ParentNodeMap = [[RootNode, undefined], [OperationNode, RootNode], [SchemaNode, RootNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
|
|
1716
|
+
/**
|
|
1717
|
+
* Resolves the parent node type for a given AST node type.
|
|
1718
|
+
*
|
|
1719
|
+
* This is used by visitor context so `ctx.parent` is correctly typed
|
|
1720
|
+
* for each callback.
|
|
1721
|
+
*
|
|
1722
|
+
* @example
|
|
1723
|
+
* ```ts
|
|
1724
|
+
* type RootParent = ParentOf<RootNode>
|
|
1725
|
+
* // undefined
|
|
1726
|
+
* ```
|
|
1727
|
+
*
|
|
1728
|
+
* @example
|
|
1729
|
+
* ```ts
|
|
1730
|
+
* type PropertyParent = ParentOf<PropertyNode>
|
|
1731
|
+
* // SchemaNode
|
|
1732
|
+
* ```
|
|
1733
|
+
*
|
|
1734
|
+
* @example
|
|
1735
|
+
* ```ts
|
|
1736
|
+
* type SchemaParent = ParentOf<SchemaNode>
|
|
1737
|
+
* // RootNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
|
|
1738
|
+
* ```
|
|
1739
|
+
*/
|
|
1740
|
+
type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [infer TEntry extends [Node, unknown], ...infer TRest extends ReadonlyArray<[Node, unknown]>] ? T extends TEntry[0] ? TEntry[1] : ParentOf<T, TRest> : Node;
|
|
1741
|
+
/**
|
|
1742
|
+
* Traversal context passed as the second argument to every visitor callback.
|
|
1743
|
+
* `parent` is typed from the current node type.
|
|
1744
|
+
*
|
|
1745
|
+
* @example
|
|
1746
|
+
* ```ts
|
|
1747
|
+
* const visitor: Visitor = {
|
|
1748
|
+
* schema(node, { parent }) {
|
|
1749
|
+
* // parent type is narrowed by node kind
|
|
1750
|
+
* },
|
|
1751
|
+
* }
|
|
1752
|
+
* ```
|
|
1753
|
+
*/
|
|
1754
|
+
type VisitorContext<T extends Node = Node> = {
|
|
1755
|
+
/**
|
|
1756
|
+
* Parent node of the currently visited node.
|
|
1757
|
+
* For `RootNode`, this is `undefined`.
|
|
1758
|
+
*/
|
|
1759
|
+
parent?: ParentOf<T>;
|
|
1760
|
+
};
|
|
1761
|
+
/**
|
|
1762
|
+
* Synchronous visitor used by `transform`.
|
|
1763
|
+
*
|
|
1764
|
+
* @example
|
|
1765
|
+
* ```ts
|
|
1766
|
+
* const visitor: Visitor = {
|
|
1767
|
+
* operation(node) {
|
|
1768
|
+
* return { ...node, operationId: `x_${node.operationId}` }
|
|
1769
|
+
* },
|
|
1770
|
+
* }
|
|
1771
|
+
* ```
|
|
1772
|
+
*/
|
|
1773
|
+
type Visitor = {
|
|
1774
|
+
root?(node: RootNode, context: VisitorContext<RootNode>): void | RootNode;
|
|
1775
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): void | OperationNode;
|
|
1776
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): void | SchemaNode;
|
|
1777
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): void | PropertyNode;
|
|
1778
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): void | ParameterNode;
|
|
1779
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): void | ResponseNode;
|
|
1780
|
+
};
|
|
1781
|
+
/**
|
|
1782
|
+
* Utility type for values that can be returned directly or asynchronously.
|
|
1783
|
+
*/
|
|
1784
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
1785
|
+
/**
|
|
1786
|
+
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
|
|
1787
|
+
*
|
|
1788
|
+
* @example
|
|
1789
|
+
* ```ts
|
|
1790
|
+
* const visitor: AsyncVisitor = {
|
|
1791
|
+
* async operation(node) {
|
|
1792
|
+
* await Promise.resolve(node.operationId)
|
|
1793
|
+
* },
|
|
1794
|
+
* }
|
|
1795
|
+
* ```
|
|
1796
|
+
*/
|
|
1797
|
+
type AsyncVisitor = {
|
|
1798
|
+
root?(node: RootNode, context: VisitorContext<RootNode>): MaybePromise<void | RootNode>;
|
|
1799
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<void | OperationNode>;
|
|
1800
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<void | SchemaNode>;
|
|
1801
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<void | PropertyNode>;
|
|
1802
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<void | ParameterNode>;
|
|
1803
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<void | ResponseNode>;
|
|
1804
|
+
};
|
|
1805
|
+
/**
|
|
1806
|
+
* Visitor used by `collect`.
|
|
1807
|
+
*
|
|
1808
|
+
* @example
|
|
1809
|
+
* ```ts
|
|
1810
|
+
* const visitor: CollectVisitor<string> = {
|
|
1811
|
+
* operation(node) {
|
|
1812
|
+
* return node.operationId
|
|
1813
|
+
* },
|
|
1814
|
+
* }
|
|
1815
|
+
* ```
|
|
1816
|
+
*/
|
|
1817
|
+
type CollectVisitor<T> = {
|
|
1818
|
+
root?(node: RootNode, context: VisitorContext<RootNode>): T | undefined;
|
|
1819
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | undefined;
|
|
1820
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | undefined;
|
|
1821
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | undefined;
|
|
1822
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | undefined;
|
|
1823
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | undefined;
|
|
1824
|
+
};
|
|
1825
|
+
/**
|
|
1826
|
+
* Options for `transform`.
|
|
1827
|
+
*
|
|
1828
|
+
* @example
|
|
1829
|
+
* ```ts
|
|
1830
|
+
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
|
|
1831
|
+
* ```
|
|
1832
|
+
*
|
|
1833
|
+
* @example
|
|
1834
|
+
* ```ts
|
|
1835
|
+
* // Only transform the current node, not nested children
|
|
1836
|
+
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
|
|
1837
|
+
* ```
|
|
1838
|
+
*/
|
|
1839
|
+
type TransformOptions = Visitor & {
|
|
1840
|
+
/**
|
|
1841
|
+
* Traversal depth (`'deep'` by default).
|
|
1842
|
+
* @default 'deep'
|
|
1843
|
+
*/
|
|
1844
|
+
depth?: VisitorDepth;
|
|
1845
|
+
/**
|
|
1846
|
+
* Internal parent override used during recursion.
|
|
1847
|
+
*/
|
|
1848
|
+
parent?: Node;
|
|
1849
|
+
};
|
|
1850
|
+
/**
|
|
1851
|
+
* Options for `walk`.
|
|
1852
|
+
*
|
|
1853
|
+
* @example
|
|
1854
|
+
* ```ts
|
|
1855
|
+
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
|
|
1856
|
+
* ```
|
|
1857
|
+
*/
|
|
1858
|
+
type WalkOptions = AsyncVisitor & {
|
|
1859
|
+
/**
|
|
1860
|
+
* Traversal depth (`'deep'` by default).
|
|
1861
|
+
* @default 'deep'
|
|
1862
|
+
*/
|
|
1863
|
+
depth?: VisitorDepth;
|
|
1864
|
+
/**
|
|
1865
|
+
* Maximum number of sibling nodes visited concurrently.
|
|
1866
|
+
* @default 30
|
|
1867
|
+
*/
|
|
1868
|
+
concurrency?: number;
|
|
1869
|
+
};
|
|
1870
|
+
/**
|
|
1871
|
+
* Options for `collect`.
|
|
1872
|
+
*
|
|
1873
|
+
* @example
|
|
1874
|
+
* ```ts
|
|
1875
|
+
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
|
|
1876
|
+
* ```
|
|
1877
|
+
*/
|
|
1878
|
+
type CollectOptions<T> = CollectVisitor<T> & {
|
|
1879
|
+
/**
|
|
1880
|
+
* Traversal depth (`'deep'` by default).
|
|
1881
|
+
* @default 'deep'
|
|
1882
|
+
*/
|
|
1883
|
+
depth?: VisitorDepth;
|
|
1884
|
+
/**
|
|
1885
|
+
* Internal parent override used during recursion.
|
|
1886
|
+
*/
|
|
1887
|
+
parent?: Node;
|
|
1888
|
+
};
|
|
1889
|
+
/**
|
|
1890
|
+
* Depth-first traversal for side effects. Visitor return values are ignored.
|
|
1891
|
+
* Sibling nodes at each level are visited concurrently up to `options.concurrency`
|
|
1892
|
+
* (default: `WALK_CONCURRENCY`).
|
|
1893
|
+
*
|
|
1894
|
+
* @example
|
|
1895
|
+
* ```ts
|
|
1896
|
+
* await walk(root, {
|
|
1897
|
+
* operation(node) {
|
|
1898
|
+
* console.log(node.operationId)
|
|
1899
|
+
* },
|
|
1900
|
+
* })
|
|
1901
|
+
* ```
|
|
1902
|
+
*
|
|
1903
|
+
* @example
|
|
1904
|
+
* ```ts
|
|
1905
|
+
* // Visit only the current node
|
|
1906
|
+
* await walk(root, { depth: 'shallow', root: () => {} })
|
|
1907
|
+
* ```
|
|
1908
|
+
*/
|
|
1909
|
+
declare function walk(node: Node, options: WalkOptions): Promise<void>;
|
|
1910
|
+
/**
|
|
1911
|
+
* Runs a depth-first immutable transform.
|
|
1912
|
+
*
|
|
1913
|
+
* If a visitor returns a node, it replaces the current node.
|
|
1914
|
+
* If it returns `undefined`, the current node stays the same.
|
|
1915
|
+
*
|
|
1916
|
+
* @example
|
|
1917
|
+
* ```ts
|
|
1918
|
+
* const next = transform(root, {
|
|
1919
|
+
* operation(node) {
|
|
1920
|
+
* return { ...node, operationId: `prefixed_${node.operationId}` }
|
|
1921
|
+
* },
|
|
1922
|
+
* })
|
|
1923
|
+
* ```
|
|
1924
|
+
*
|
|
1925
|
+
* @example
|
|
1926
|
+
* ```ts
|
|
1927
|
+
* // Shallow mode: only transform the input node
|
|
1928
|
+
* const next = transform(root, { depth: 'shallow', root: (node) => node })
|
|
1929
|
+
* ```
|
|
1930
|
+
*/
|
|
1931
|
+
declare function transform(node: RootNode, options: TransformOptions): RootNode;
|
|
1932
|
+
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
|
|
1933
|
+
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
|
|
1934
|
+
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
|
|
1935
|
+
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
|
|
1936
|
+
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
|
|
1937
|
+
declare function transform(node: Node, options: TransformOptions): Node;
|
|
1938
|
+
/**
|
|
1939
|
+
* Composes multiple visitors into one visitor, applied left to right.
|
|
1940
|
+
*
|
|
1941
|
+
* For each node kind, output from one visitor is input to the next.
|
|
1942
|
+
* If a visitor returns `undefined`, the previous node value is kept.
|
|
1943
|
+
*
|
|
1944
|
+
* @example
|
|
1945
|
+
* ```ts
|
|
1946
|
+
* const visitor = composeTransformers(
|
|
1947
|
+
* { operation: (node) => ({ ...node, operationId: `a_${node.operationId}` }) },
|
|
1948
|
+
* { operation: (node) => ({ ...node, operationId: `b_${node.operationId}` }) },
|
|
1949
|
+
* )
|
|
1950
|
+
* ```
|
|
1951
|
+
*/
|
|
1952
|
+
declare function composeTransformers(...visitors: Array<Visitor>): Visitor;
|
|
1953
|
+
/**
|
|
1954
|
+
* Runs a depth-first synchronous collection pass.
|
|
1955
|
+
*
|
|
1956
|
+
* Non-`undefined` values returned by visitor callbacks are appended to the result.
|
|
1957
|
+
*
|
|
1958
|
+
* @example
|
|
1959
|
+
* ```ts
|
|
1960
|
+
* const ids = collect(root, {
|
|
1961
|
+
* operation(node) {
|
|
1962
|
+
* return node.operationId
|
|
1963
|
+
* },
|
|
1964
|
+
* })
|
|
1965
|
+
* ```
|
|
1966
|
+
*
|
|
1967
|
+
* @example
|
|
1968
|
+
* ```ts
|
|
1969
|
+
* // Collect from only the current node
|
|
1970
|
+
* const values = collect(root, { depth: 'shallow', root: () => 'root' })
|
|
1971
|
+
* ```
|
|
1972
|
+
*/
|
|
1973
|
+
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
1974
|
+
//#endregion
|
|
1975
|
+
export { NumberSchemaNode as $, createRoot as A, ResponseNode as B, createFunctionParameter as C, httpMethods as Ct, createParameter as D, createOperation as E, schemaTypes as Et, Node as F, ParameterNode as G, MediaType as H, RootMeta as I, DateSchemaNode as J, ArraySchemaNode as K, RootNode as L, InferSchema as M, InferSchemaNode as N, createProperty as O, ParserOptions as P, IntersectionSchemaNode as Q, HttpMethod as R, createDiscriminantNode as S, VisitorDepth as St, createObjectBindingParameter as T, nodeKinds as Tt, StatusCode as U, HttpStatusCode as V, ParameterLocation as W, EnumSchemaNode as X, DatetimeSchemaNode as Y, EnumValueNode as Z, resolveRef as _, FunctionParametersNode as _t, TransformOptions as a, SchemaNode as at, definePrinter as b, NodeKind as bt, WalkOptions as c, SpecialSchemaType as ct, transform as d, UnionSchemaNode as dt, ObjectSchemaNode as et, walk as f, UrlSchemaNode as ft, refMapToObject as g, FunctionParameterNode as gt, extractRefName as h, FunctionNodeType as ht, ParentOf as i, ScalarSchemaType as it, createSchema as j, createResponse as k, collect as l, StringSchemaNode as lt, buildRefMap as m, FunctionNode as mt, CollectOptions as n, RefSchemaNode as nt, Visitor as o, SchemaNodeByType as ot, RefMap as p, PropertyNode as pt, ComplexSchemaType as q, CollectVisitor as r, ScalarSchemaNode as rt, VisitorContext as s, SchemaType as st, AsyncVisitor as t, PrimitiveSchemaType as tt, composeTransformers as u, TimeSchemaNode as ut, Printer as v, ObjectBindingParameterNode as vt, createFunctionParameters as w, mediaTypes as wt, DistributiveOmit as x, SCALAR_PRIMITIVE_TYPES as xt, PrinterFactoryOptions as y, BaseNode as yt, OperationNode as z };
|
|
1976
|
+
//# sourceMappingURL=visitor-DCQyoFvH.d.ts.map
|