@kubb/ast 5.0.0-alpha.9 → 5.0.0-beta.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -10
- package/dist/index.cjs +1975 -531
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3379 -24
- package/dist/index.js +1911 -519
- package/dist/index.js.map +1 -1
- package/package.json +23 -34
- package/src/constants.ts +133 -15
- package/src/factory.ts +677 -21
- package/src/guards.ts +77 -9
- package/src/index.ts +44 -6
- package/src/infer.ts +130 -0
- package/src/mocks.ts +95 -25
- package/src/nodes/base.ts +44 -4
- package/src/nodes/code.ts +304 -0
- package/src/nodes/file.ts +230 -0
- package/src/nodes/function.ts +223 -0
- package/src/nodes/http.ts +17 -5
- package/src/nodes/index.ts +47 -7
- package/src/nodes/operation.ts +84 -6
- package/src/nodes/output.ts +26 -0
- package/src/nodes/parameter.ts +27 -1
- package/src/nodes/property.ts +23 -1
- package/src/nodes/response.ts +28 -2
- package/src/nodes/root.ts +34 -12
- package/src/nodes/schema.ts +419 -42
- package/src/printer.ts +152 -59
- package/src/refs.ts +39 -7
- package/src/resolvers.ts +45 -0
- package/src/transformers.ts +159 -0
- package/src/types.ts +32 -4
- package/src/utils.ts +798 -13
- package/src/visitor.ts +411 -96
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +0 -2
- package/dist/types.js +0 -1
- package/dist/visitor-Ci6rmtT9.d.ts +0 -702
package/dist/index.d.ts
CHANGED
|
@@ -1,55 +1,3410 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { D as OperationNode, J as SchemaNodeByType, N as ParameterNode, O as ResponseNode, S as createSchema, T as RootNode, _ as createOperation, a as transform, at as httpMethods, b as createResponse, c as buildRefMap, ct as schemaTypes, h as definePrinter, i as collect, l as refMapToObject, o as walk, ot as mediaTypes, q as SchemaNode, st as nodeKinds, tt as PropertyNode, u as resolveRef, v as createParameter, x as createRoot, y as createProperty } from "./visitor-Ci6rmtT9.js";
|
|
3
2
|
|
|
3
|
+
//#region src/constants.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Traversal depth for AST visitor utilities.
|
|
6
|
+
*
|
|
7
|
+
* - `'shallow'` — visits only the immediate node, skipping children.
|
|
8
|
+
* - `'deep'` — recursively visits all descendant nodes.
|
|
9
|
+
*/
|
|
10
|
+
type VisitorDepth = 'shallow' | 'deep';
|
|
11
|
+
declare const nodeKinds: {
|
|
12
|
+
readonly input: "Input";
|
|
13
|
+
readonly output: "Output";
|
|
14
|
+
readonly operation: "Operation";
|
|
15
|
+
readonly schema: "Schema";
|
|
16
|
+
readonly property: "Property";
|
|
17
|
+
readonly parameter: "Parameter";
|
|
18
|
+
readonly response: "Response";
|
|
19
|
+
readonly functionParameter: "FunctionParameter";
|
|
20
|
+
readonly parameterGroup: "ParameterGroup";
|
|
21
|
+
readonly functionParameters: "FunctionParameters";
|
|
22
|
+
readonly type: "Type";
|
|
23
|
+
readonly file: "File";
|
|
24
|
+
readonly import: "Import";
|
|
25
|
+
readonly export: "Export";
|
|
26
|
+
readonly source: "Source";
|
|
27
|
+
readonly text: "Text";
|
|
28
|
+
readonly break: "Break";
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Schema type discriminators used by all AST schema nodes.
|
|
32
|
+
*
|
|
33
|
+
* These values serve as stable discriminators across the AST (e.g., `schema.type === schemaTypes.object`).
|
|
34
|
+
* Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),
|
|
35
|
+
* and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.
|
|
36
|
+
*/
|
|
37
|
+
declare const schemaTypes: {
|
|
38
|
+
/**
|
|
39
|
+
* Text value.
|
|
40
|
+
*/
|
|
41
|
+
readonly string: "string";
|
|
42
|
+
/**
|
|
43
|
+
* Floating-point number (`float`, `double`).
|
|
44
|
+
*/
|
|
45
|
+
readonly number: "number";
|
|
46
|
+
/**
|
|
47
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
48
|
+
*/
|
|
49
|
+
readonly integer: "integer";
|
|
50
|
+
/**
|
|
51
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
52
|
+
*/
|
|
53
|
+
readonly bigint: "bigint";
|
|
54
|
+
/**
|
|
55
|
+
* Boolean value
|
|
56
|
+
*/
|
|
57
|
+
readonly boolean: "boolean";
|
|
58
|
+
/**
|
|
59
|
+
* Explicit null value.
|
|
60
|
+
*/
|
|
61
|
+
readonly null: "null";
|
|
62
|
+
/**
|
|
63
|
+
* Any value (no type restriction).
|
|
64
|
+
*/
|
|
65
|
+
readonly any: "any";
|
|
66
|
+
/**
|
|
67
|
+
* Unknown value (must be narrowed before usage).
|
|
68
|
+
*/
|
|
69
|
+
readonly unknown: "unknown";
|
|
70
|
+
/**
|
|
71
|
+
* No return value (`void`).
|
|
72
|
+
*/
|
|
73
|
+
readonly void: "void";
|
|
74
|
+
/**
|
|
75
|
+
* Object with named properties.
|
|
76
|
+
*/
|
|
77
|
+
readonly object: "object";
|
|
78
|
+
/**
|
|
79
|
+
* Sequential list of items.
|
|
80
|
+
*/
|
|
81
|
+
readonly array: "array";
|
|
82
|
+
/**
|
|
83
|
+
* Fixed-length list with position-specific items.
|
|
84
|
+
*/
|
|
85
|
+
readonly tuple: "tuple";
|
|
86
|
+
/**
|
|
87
|
+
* "One of" multiple schema members.
|
|
88
|
+
*/
|
|
89
|
+
readonly union: "union";
|
|
90
|
+
/**
|
|
91
|
+
* "All of" multiple schema members.
|
|
92
|
+
*/
|
|
93
|
+
readonly intersection: "intersection";
|
|
94
|
+
/**
|
|
95
|
+
* Enum schema.
|
|
96
|
+
*/
|
|
97
|
+
readonly enum: "enum";
|
|
98
|
+
/**
|
|
99
|
+
* Reference to another schema.
|
|
100
|
+
*/
|
|
101
|
+
readonly ref: "ref";
|
|
102
|
+
/**
|
|
103
|
+
* Calendar date (for example `2026-03-24`).
|
|
104
|
+
*/
|
|
105
|
+
readonly date: "date";
|
|
106
|
+
/**
|
|
107
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
108
|
+
*/
|
|
109
|
+
readonly datetime: "datetime";
|
|
110
|
+
/**
|
|
111
|
+
* Time-only value (for example `09:00:00`).
|
|
112
|
+
*/
|
|
113
|
+
readonly time: "time";
|
|
114
|
+
/**
|
|
115
|
+
* UUID value.
|
|
116
|
+
*/
|
|
117
|
+
readonly uuid: "uuid";
|
|
118
|
+
/**
|
|
119
|
+
* Email address value.
|
|
120
|
+
*/
|
|
121
|
+
readonly email: "email";
|
|
122
|
+
/**
|
|
123
|
+
* URL value.
|
|
124
|
+
*/
|
|
125
|
+
readonly url: "url";
|
|
126
|
+
/**
|
|
127
|
+
* IPv4 address value.
|
|
128
|
+
*/
|
|
129
|
+
readonly ipv4: "ipv4";
|
|
130
|
+
/**
|
|
131
|
+
* IPv6 address value.
|
|
132
|
+
*/
|
|
133
|
+
readonly ipv6: "ipv6";
|
|
134
|
+
/**
|
|
135
|
+
* Binary/blob value.
|
|
136
|
+
*/
|
|
137
|
+
readonly blob: "blob";
|
|
138
|
+
/**
|
|
139
|
+
* Impossible value (`never`).
|
|
140
|
+
*/
|
|
141
|
+
readonly never: "never";
|
|
142
|
+
};
|
|
143
|
+
type ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean';
|
|
144
|
+
/**
|
|
145
|
+
* Type guard that returns `true` when `type` is a scalar primitive schema type.
|
|
146
|
+
*
|
|
147
|
+
* Use this to check if a schema type can be directly assigned without wrapping (e.g., `string | number | boolean`).
|
|
148
|
+
*/
|
|
149
|
+
declare function isScalarPrimitive(type: string): type is ScalarPrimitive;
|
|
150
|
+
/**
|
|
151
|
+
* HTTP method identifiers used by operation nodes.
|
|
152
|
+
*
|
|
153
|
+
* Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).
|
|
154
|
+
*/
|
|
155
|
+
declare const httpMethods: {
|
|
156
|
+
readonly get: "GET";
|
|
157
|
+
readonly post: "POST";
|
|
158
|
+
readonly put: "PUT";
|
|
159
|
+
readonly patch: "PATCH";
|
|
160
|
+
readonly delete: "DELETE";
|
|
161
|
+
readonly head: "HEAD";
|
|
162
|
+
readonly options: "OPTIONS";
|
|
163
|
+
readonly trace: "TRACE";
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Common MIME types used in request/response content negotiation.
|
|
167
|
+
*
|
|
168
|
+
* Covers JSON, XML, form data, PDFs, images, audio, and video formats.
|
|
169
|
+
* Use these as keys when serializing request/response bodies.
|
|
170
|
+
*/
|
|
171
|
+
declare const mediaTypes: {
|
|
172
|
+
readonly applicationJson: "application/json";
|
|
173
|
+
readonly applicationXml: "application/xml";
|
|
174
|
+
readonly applicationFormUrlEncoded: "application/x-www-form-urlencoded";
|
|
175
|
+
readonly applicationOctetStream: "application/octet-stream";
|
|
176
|
+
readonly applicationPdf: "application/pdf";
|
|
177
|
+
readonly applicationZip: "application/zip";
|
|
178
|
+
readonly applicationGraphql: "application/graphql";
|
|
179
|
+
readonly multipartFormData: "multipart/form-data";
|
|
180
|
+
readonly textPlain: "text/plain";
|
|
181
|
+
readonly textHtml: "text/html";
|
|
182
|
+
readonly textCsv: "text/csv";
|
|
183
|
+
readonly textXml: "text/xml";
|
|
184
|
+
readonly imagePng: "image/png";
|
|
185
|
+
readonly imageJpeg: "image/jpeg";
|
|
186
|
+
readonly imageGif: "image/gif";
|
|
187
|
+
readonly imageWebp: "image/webp";
|
|
188
|
+
readonly imageSvgXml: "image/svg+xml";
|
|
189
|
+
readonly audioMpeg: "audio/mpeg";
|
|
190
|
+
readonly videoMp4: "video/mp4";
|
|
191
|
+
};
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/nodes/base.d.ts
|
|
194
|
+
/**
|
|
195
|
+
* `kind` values used by AST nodes.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* const kind: NodeKind = 'Schema'
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
type NodeKind = 'Input' | 'Output' | 'Operation' | 'Schema' | 'Property' | 'Parameter' | 'Response' | 'FunctionParameter' | 'ParameterGroup' | 'FunctionParameters' | 'Type' | 'ParamsType' | 'File' | 'Import' | 'Export' | 'Source' | 'Const' | 'Function' | 'ArrowFunction' | 'Text' | 'Break' | 'Jsx';
|
|
203
|
+
/**
|
|
204
|
+
* Base shape shared by all AST nodes.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```ts
|
|
208
|
+
* const base: BaseNode = { kind: 'Input' }
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
type BaseNode = {
|
|
212
|
+
/**
|
|
213
|
+
* Node discriminator.
|
|
214
|
+
*/
|
|
215
|
+
kind: NodeKind;
|
|
216
|
+
};
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region src/nodes/code.d.ts
|
|
219
|
+
/**
|
|
220
|
+
* JSDoc documentation metadata attached to code declarations.
|
|
221
|
+
*/
|
|
222
|
+
type JSDocNode = {
|
|
223
|
+
/**
|
|
224
|
+
* JSDoc comment lines. `undefined` entries are filtered out during rendering.
|
|
225
|
+
* @example ['@description A pet resource', '@deprecated']
|
|
226
|
+
*/
|
|
227
|
+
comments?: Array<string | undefined>;
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* AST node representing a TypeScript `const` declaration.
|
|
231
|
+
*
|
|
232
|
+
* Mirrors the props of the `Const` component from `@kubb/renderer-jsx`.
|
|
233
|
+
* The `children` prop of the component is represented as `nodes`.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* createConst({ name: 'pet', export: true, asConst: true })
|
|
238
|
+
* // export const pet = ... as const
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
type ConstNode = BaseNode & {
|
|
242
|
+
/**
|
|
243
|
+
* Node kind.
|
|
244
|
+
*/
|
|
245
|
+
kind: 'Const';
|
|
246
|
+
/**
|
|
247
|
+
* Name of the constant declaration.
|
|
248
|
+
*/
|
|
249
|
+
name: string;
|
|
250
|
+
/**
|
|
251
|
+
* Whether the declaration should be exported.
|
|
252
|
+
* @default false
|
|
253
|
+
*/
|
|
254
|
+
export?: boolean;
|
|
255
|
+
/**
|
|
256
|
+
* Optional explicit type annotation.
|
|
257
|
+
* @example 'Pet'
|
|
258
|
+
*/
|
|
259
|
+
type?: string;
|
|
260
|
+
/**
|
|
261
|
+
* JSDoc documentation metadata.
|
|
262
|
+
*/
|
|
263
|
+
JSDoc?: JSDocNode;
|
|
264
|
+
/**
|
|
265
|
+
* Whether to append `as const` to the declaration.
|
|
266
|
+
* @default false
|
|
267
|
+
*/
|
|
268
|
+
asConst?: boolean;
|
|
269
|
+
/**
|
|
270
|
+
* Child nodes representing the value of the constant (children of the `Const` component).
|
|
271
|
+
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
272
|
+
*/
|
|
273
|
+
nodes?: Array<CodeNode>;
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* AST node representing a TypeScript `type` alias declaration.
|
|
277
|
+
*
|
|
278
|
+
* Mirrors the props of the `Type` component from `@kubb/renderer-jsx`.
|
|
279
|
+
* The `children` prop of the component is represented as `nodes`.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* createType({ name: 'Pet', export: true })
|
|
284
|
+
* // export type Pet = ...
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
type TypeNode = BaseNode & {
|
|
288
|
+
/**
|
|
289
|
+
* Node kind.
|
|
290
|
+
*/
|
|
291
|
+
kind: 'Type';
|
|
292
|
+
/**
|
|
293
|
+
* Name of the type alias.
|
|
294
|
+
*/
|
|
295
|
+
name: string;
|
|
296
|
+
/**
|
|
297
|
+
* Whether the declaration should be exported.
|
|
298
|
+
* @default false
|
|
299
|
+
*/
|
|
300
|
+
export?: boolean;
|
|
301
|
+
/**
|
|
302
|
+
* JSDoc documentation metadata.
|
|
303
|
+
*/
|
|
304
|
+
JSDoc?: JSDocNode;
|
|
305
|
+
/**
|
|
306
|
+
* Child nodes representing the type body (children of the `Type` component).
|
|
307
|
+
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
308
|
+
*/
|
|
309
|
+
nodes?: Array<CodeNode>;
|
|
310
|
+
};
|
|
311
|
+
/**
|
|
312
|
+
* Convenience alias for {@link TypeNode}.
|
|
313
|
+
* @deprecated Use `TypeNode` directly.
|
|
314
|
+
*/
|
|
315
|
+
type TypeDeclarationNode = TypeNode;
|
|
316
|
+
/**
|
|
317
|
+
* AST node representing a TypeScript `function` declaration.
|
|
318
|
+
*
|
|
319
|
+
* Mirrors the props of the `Function` component from `@kubb/renderer-jsx`.
|
|
320
|
+
* The `children` prop of the component is represented as `nodes`.
|
|
321
|
+
*
|
|
322
|
+
* @example
|
|
323
|
+
* ```ts
|
|
324
|
+
* createFunctionDeclaration({ name: 'getPet', export: true, async: true, returnType: 'Pet' })
|
|
325
|
+
* // export async function getPet(): Promise<Pet> { ... }
|
|
326
|
+
* ```
|
|
327
|
+
*/
|
|
328
|
+
type FunctionNode = BaseNode & {
|
|
329
|
+
/**
|
|
330
|
+
* Node kind.
|
|
331
|
+
*/
|
|
332
|
+
kind: 'Function';
|
|
333
|
+
/**
|
|
334
|
+
* Name of the function.
|
|
335
|
+
*/
|
|
336
|
+
name: string;
|
|
337
|
+
/**
|
|
338
|
+
* Whether the function is a default export.
|
|
339
|
+
* @default false
|
|
340
|
+
*/
|
|
341
|
+
default?: boolean;
|
|
342
|
+
/**
|
|
343
|
+
* Function parameter list rendered as a string (e.g. from `FunctionParams.toConstructor()`).
|
|
344
|
+
*/
|
|
345
|
+
params?: string;
|
|
346
|
+
/**
|
|
347
|
+
* Whether the function should be exported.
|
|
348
|
+
* @default false
|
|
349
|
+
*/
|
|
350
|
+
export?: boolean;
|
|
351
|
+
/**
|
|
352
|
+
* Whether the function is async. When `true`, the return type is wrapped in `Promise<>`.
|
|
353
|
+
* @default false
|
|
354
|
+
*/
|
|
355
|
+
async?: boolean;
|
|
356
|
+
/**
|
|
357
|
+
* TypeScript generic type parameters.
|
|
358
|
+
* @example ['T', 'U extends string']
|
|
359
|
+
*/
|
|
360
|
+
generics?: string | string[];
|
|
361
|
+
/**
|
|
362
|
+
* Return type annotation.
|
|
363
|
+
* @example 'Pet'
|
|
364
|
+
*/
|
|
365
|
+
returnType?: string;
|
|
366
|
+
/**
|
|
367
|
+
* JSDoc documentation metadata.
|
|
368
|
+
*/
|
|
369
|
+
JSDoc?: JSDocNode;
|
|
370
|
+
/**
|
|
371
|
+
* Child nodes representing the function body (children of the `Function` component).
|
|
372
|
+
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
373
|
+
*/
|
|
374
|
+
nodes?: Array<CodeNode>;
|
|
375
|
+
};
|
|
376
|
+
/**
|
|
377
|
+
* AST node representing a TypeScript arrow function (`const name = () => { ... }`).
|
|
378
|
+
*
|
|
379
|
+
* Mirrors the props of the `Function.Arrow` component from `@kubb/renderer-jsx`.
|
|
380
|
+
* The `children` prop of the component is represented as `nodes`.
|
|
381
|
+
*
|
|
382
|
+
* @example
|
|
383
|
+
* ```ts
|
|
384
|
+
* createArrowFunctionDeclaration({ name: 'getPet', export: true, singleLine: true })
|
|
385
|
+
* // export const getPet = () => ...
|
|
386
|
+
* ```
|
|
387
|
+
*/
|
|
388
|
+
type ArrowFunctionNode = BaseNode & {
|
|
389
|
+
/**
|
|
390
|
+
* Node kind.
|
|
391
|
+
*/
|
|
392
|
+
kind: 'ArrowFunction';
|
|
393
|
+
/**
|
|
394
|
+
* Name of the arrow function (used as the `const` variable name).
|
|
395
|
+
*/
|
|
396
|
+
name: string;
|
|
397
|
+
/**
|
|
398
|
+
* Whether the function is a default export.
|
|
399
|
+
* @default false
|
|
400
|
+
*/
|
|
401
|
+
default?: boolean;
|
|
402
|
+
/**
|
|
403
|
+
* Function parameter list rendered as a string (e.g. from `FunctionParams.toConstructor()`).
|
|
404
|
+
*/
|
|
405
|
+
params?: string;
|
|
406
|
+
/**
|
|
407
|
+
* Whether the arrow function should be exported.
|
|
408
|
+
* @default false
|
|
409
|
+
*/
|
|
410
|
+
export?: boolean;
|
|
411
|
+
/**
|
|
412
|
+
* Whether the arrow function is async. When `true`, the return type is wrapped in `Promise<>`.
|
|
413
|
+
* @default false
|
|
414
|
+
*/
|
|
415
|
+
async?: boolean;
|
|
416
|
+
/**
|
|
417
|
+
* TypeScript generic type parameters.
|
|
418
|
+
* @example ['T', 'U extends string']
|
|
419
|
+
*/
|
|
420
|
+
generics?: string | string[];
|
|
421
|
+
/**
|
|
422
|
+
* Return type annotation.
|
|
423
|
+
* @example 'Pet'
|
|
424
|
+
*/
|
|
425
|
+
returnType?: string;
|
|
426
|
+
/**
|
|
427
|
+
* JSDoc documentation metadata.
|
|
428
|
+
*/
|
|
429
|
+
JSDoc?: JSDocNode;
|
|
430
|
+
/**
|
|
431
|
+
* Render the arrow function body as a single-line expression.
|
|
432
|
+
* @default false
|
|
433
|
+
*/
|
|
434
|
+
singleLine?: boolean;
|
|
435
|
+
/**
|
|
436
|
+
* Child nodes representing the function body (children of the `Function.Arrow` component).
|
|
437
|
+
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
438
|
+
*/
|
|
439
|
+
nodes?: Array<CodeNode>;
|
|
440
|
+
};
|
|
441
|
+
/**
|
|
442
|
+
* AST node representing a raw text/string fragment in the source output.
|
|
443
|
+
*
|
|
444
|
+
* Used instead of bare `string` values so that all entries in `nodes` arrays
|
|
445
|
+
* are typed `CodeNode` objects rather than a mixed `CodeNode | string` union.
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```ts
|
|
449
|
+
* createText('return fetch(id)')
|
|
450
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
451
|
+
* ```
|
|
452
|
+
*/
|
|
453
|
+
type TextNode = BaseNode & {
|
|
454
|
+
/**
|
|
455
|
+
* Node kind.
|
|
456
|
+
*/
|
|
457
|
+
kind: 'Text';
|
|
458
|
+
/**
|
|
459
|
+
* The raw string content.
|
|
460
|
+
*/
|
|
461
|
+
value: string;
|
|
462
|
+
};
|
|
463
|
+
/**
|
|
464
|
+
* AST node representing a line break in the source output.
|
|
465
|
+
*
|
|
466
|
+
* Corresponds to `<br/>` in JSX components. When printed, produces an empty
|
|
467
|
+
* string that — joined with `\n` by `printNodes` — creates a blank line
|
|
468
|
+
* between surrounding code nodes.
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
471
|
+
* ```ts
|
|
472
|
+
* createBreak()
|
|
473
|
+
* // { kind: 'Break' }
|
|
474
|
+
* // prints as '' → blank line when surrounded by other nodes
|
|
475
|
+
* ```
|
|
476
|
+
*/
|
|
477
|
+
type BreakNode = BaseNode & {
|
|
478
|
+
/**
|
|
479
|
+
* Node kind.
|
|
480
|
+
*/
|
|
481
|
+
kind: 'Break';
|
|
482
|
+
};
|
|
483
|
+
/**
|
|
484
|
+
* AST node representing a raw JSX fragment in the source output.
|
|
485
|
+
*
|
|
486
|
+
* Mirrors the `Jsx` component from `@kubb/renderer-jsx`. Use this to embed raw
|
|
487
|
+
* JSX/TSX markup (including fragments `<>…</>`) directly in generated code.
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* ```ts
|
|
491
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
492
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
493
|
+
* ```
|
|
494
|
+
*/
|
|
495
|
+
type JsxNode = BaseNode & {
|
|
496
|
+
/**
|
|
497
|
+
* Node kind.
|
|
498
|
+
*/
|
|
499
|
+
kind: 'Jsx';
|
|
500
|
+
/**
|
|
501
|
+
* The raw JSX string content.
|
|
502
|
+
*/
|
|
503
|
+
value: string;
|
|
504
|
+
};
|
|
505
|
+
/**
|
|
506
|
+
* Union of all code-generation AST nodes.
|
|
507
|
+
*
|
|
508
|
+
* These nodes mirror the JSX components from `@kubb/renderer-jsx` and are used as
|
|
509
|
+
* structured children in {@link SourceNode.nodes}.
|
|
510
|
+
*/
|
|
511
|
+
type CodeNode = ConstNode | TypeNode | FunctionNode | ArrowFunctionNode | TextNode | BreakNode | JsxNode;
|
|
512
|
+
//#endregion
|
|
513
|
+
//#region src/nodes/file.d.ts
|
|
514
|
+
/**
|
|
515
|
+
* Supported file extensions.
|
|
516
|
+
*/
|
|
517
|
+
type Extname = '.ts' | '.js' | '.tsx' | '.json' | `.${string}`;
|
|
518
|
+
type ImportName = string | Array<string | {
|
|
519
|
+
propertyName: string;
|
|
520
|
+
name?: string;
|
|
521
|
+
}>;
|
|
522
|
+
/**
|
|
523
|
+
* Represents a language-agnostic import/dependency declaration.
|
|
524
|
+
*
|
|
525
|
+
* @example Named import (TypeScript: `import { useState } from 'react'`)
|
|
526
|
+
* ```ts
|
|
527
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
528
|
+
* ```
|
|
529
|
+
*
|
|
530
|
+
* @example Default import (TypeScript: `import React from 'react'`)
|
|
531
|
+
* ```ts
|
|
532
|
+
* createImport({ name: 'React', path: 'react' })
|
|
533
|
+
* ```
|
|
534
|
+
*
|
|
535
|
+
* @example Type-only import (TypeScript: `import type { FC } from 'react'`)
|
|
536
|
+
* ```ts
|
|
537
|
+
* createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
|
|
538
|
+
* ```
|
|
539
|
+
*
|
|
540
|
+
* @example Namespace import (TypeScript: `import * as React from 'react'`)
|
|
541
|
+
* ```ts
|
|
542
|
+
* createImport({ name: 'React', path: 'react', isNameSpace: true })
|
|
543
|
+
* ```
|
|
544
|
+
*/
|
|
545
|
+
type ImportNode = BaseNode & {
|
|
546
|
+
kind: 'Import';
|
|
547
|
+
/**
|
|
548
|
+
* Import name(s) to be used.
|
|
549
|
+
* @example ['useState']
|
|
550
|
+
* @example 'React'
|
|
551
|
+
*/
|
|
552
|
+
name: ImportName;
|
|
553
|
+
/**
|
|
554
|
+
* Path for the import.
|
|
555
|
+
* @example '@kubb/core'
|
|
556
|
+
*/
|
|
557
|
+
path: string;
|
|
558
|
+
/**
|
|
559
|
+
* Add type-only import prefix.
|
|
560
|
+
* - `true` generates `import type { Type } from './path'`
|
|
561
|
+
* - `false` generates `import { Type } from './path'`
|
|
562
|
+
* @default false
|
|
563
|
+
*/
|
|
564
|
+
isTypeOnly?: boolean;
|
|
565
|
+
/**
|
|
566
|
+
* Import entire module as namespace.
|
|
567
|
+
* - `true` generates `import * as Name from './path'`
|
|
568
|
+
* - `false` generates standard import
|
|
569
|
+
* @default false
|
|
570
|
+
*/
|
|
571
|
+
isNameSpace?: boolean;
|
|
572
|
+
/**
|
|
573
|
+
* When set, the import path is resolved relative to this root.
|
|
574
|
+
*/
|
|
575
|
+
root?: string;
|
|
576
|
+
};
|
|
577
|
+
/**
|
|
578
|
+
* Represents a language-agnostic export/public API declaration.
|
|
579
|
+
*
|
|
580
|
+
* @example Named export (TypeScript: `export { Pets } from './Pets'`)
|
|
581
|
+
* ```ts
|
|
582
|
+
* createExport({ name: ['Pets'], path: './Pets' })
|
|
583
|
+
* ```
|
|
584
|
+
*
|
|
585
|
+
* @example Type-only export (TypeScript: `export type { Pet } from './Pet'`)
|
|
586
|
+
* ```ts
|
|
587
|
+
* createExport({ name: ['Pet'], path: './Pet', isTypeOnly: true })
|
|
588
|
+
* ```
|
|
589
|
+
*
|
|
590
|
+
* @example Wildcard export (TypeScript: `export * from './utils'`)
|
|
591
|
+
* ```ts
|
|
592
|
+
* createExport({ path: './utils' })
|
|
593
|
+
* ```
|
|
594
|
+
*
|
|
595
|
+
* @example Namespace alias (TypeScript: `export * as utils from './utils'`)
|
|
596
|
+
* ```ts
|
|
597
|
+
* createExport({ name: 'utils', path: './utils', asAlias: true })
|
|
598
|
+
* ```
|
|
599
|
+
*/
|
|
600
|
+
type ExportNode = BaseNode & {
|
|
601
|
+
kind: 'Export';
|
|
602
|
+
/**
|
|
603
|
+
* Export name(s) to be used. When omitted, generates a wildcard export.
|
|
604
|
+
* @example ['useState']
|
|
605
|
+
* @example 'React'
|
|
606
|
+
*/
|
|
607
|
+
name?: string | Array<string>;
|
|
608
|
+
/**
|
|
609
|
+
* Path for the export.
|
|
610
|
+
* @example '@kubb/core'
|
|
611
|
+
*/
|
|
612
|
+
path: string;
|
|
613
|
+
/**
|
|
614
|
+
* Add type-only export prefix.
|
|
615
|
+
* - `true` generates `export type { Type } from './path'`
|
|
616
|
+
* - `false` generates `export { Type } from './path'`
|
|
617
|
+
* @default false
|
|
618
|
+
*/
|
|
619
|
+
isTypeOnly?: boolean;
|
|
620
|
+
/**
|
|
621
|
+
* Export as an aliased namespace.
|
|
622
|
+
* - `true` generates `export * as aliasName from './path'`
|
|
623
|
+
* - `false` generates a standard export
|
|
624
|
+
* @default false
|
|
625
|
+
*/
|
|
626
|
+
asAlias?: boolean;
|
|
627
|
+
};
|
|
628
|
+
/**
|
|
629
|
+
* Represents a fragment of source code within a file.
|
|
630
|
+
*
|
|
631
|
+
* @example Named exportable source
|
|
632
|
+
* ```ts
|
|
633
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true, isIndexable: true })
|
|
634
|
+
* ```
|
|
635
|
+
*
|
|
636
|
+
* @example Inline unnamed code block
|
|
637
|
+
* ```ts
|
|
638
|
+
* createSource({ nodes: [createText('const x = 1')] })
|
|
639
|
+
* ```
|
|
640
|
+
*/
|
|
641
|
+
type SourceNode = BaseNode & {
|
|
642
|
+
kind: 'Source';
|
|
643
|
+
/**
|
|
644
|
+
* Optional name identifying this source (used for deduplication and barrel generation).
|
|
645
|
+
*/
|
|
646
|
+
name?: string;
|
|
647
|
+
/**
|
|
648
|
+
* Mark this source as a type-only export.
|
|
649
|
+
* @default false
|
|
650
|
+
*/
|
|
651
|
+
isTypeOnly?: boolean;
|
|
652
|
+
/**
|
|
653
|
+
* Include `export` keyword in the generated source.
|
|
654
|
+
* @default false
|
|
655
|
+
*/
|
|
656
|
+
isExportable?: boolean;
|
|
657
|
+
/**
|
|
658
|
+
* Include this source in barrel/index file generation.
|
|
659
|
+
* @default false
|
|
660
|
+
*/
|
|
661
|
+
isIndexable?: boolean;
|
|
662
|
+
/**
|
|
663
|
+
* Structured child nodes representing the content of this source fragment, in DOM order.
|
|
664
|
+
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
665
|
+
*/
|
|
666
|
+
nodes?: Array<CodeNode>;
|
|
667
|
+
};
|
|
668
|
+
/**
|
|
669
|
+
* Represents a fully resolved file in the AST.
|
|
670
|
+
*
|
|
671
|
+
* Created via `createFile()`, which computes the `id`, `name`, and `extname` from the input
|
|
672
|
+
* and deduplicates `imports`, `exports`, and `sources`.
|
|
673
|
+
*
|
|
674
|
+
* @example
|
|
675
|
+
* ```ts
|
|
676
|
+
* const file = createFile({
|
|
677
|
+
* baseName: 'petStore.ts',
|
|
678
|
+
* path: 'src/models/petStore.ts',
|
|
679
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })],
|
|
680
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
681
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
682
|
+
* })
|
|
683
|
+
* // file.id = SHA256 hash of the path
|
|
684
|
+
* // file.name = 'petStore'
|
|
685
|
+
* // file.extname = '.ts'
|
|
686
|
+
* ```
|
|
687
|
+
*/
|
|
688
|
+
type FileNode<TMeta extends object = object> = BaseNode & {
|
|
689
|
+
kind: 'File';
|
|
690
|
+
/**
|
|
691
|
+
* Unique identifier derived from a SHA256 hash of the file path.
|
|
692
|
+
* @default hash
|
|
693
|
+
*/
|
|
694
|
+
id: string;
|
|
695
|
+
/**
|
|
696
|
+
* File name without extension, derived from `baseName`.
|
|
697
|
+
* @link https://nodejs.org/api/path.html#pathformatpathobject
|
|
698
|
+
*/
|
|
699
|
+
name: string;
|
|
700
|
+
/**
|
|
701
|
+
* File base name, including extension.
|
|
702
|
+
* Based on UNIX basename: `${name}${extname}`
|
|
703
|
+
* @link https://nodejs.org/api/path.html#pathbasenamepath-suffix
|
|
704
|
+
*/
|
|
705
|
+
baseName: `${string}.${string}`;
|
|
706
|
+
/**
|
|
707
|
+
* Full qualified path to the file.
|
|
708
|
+
*/
|
|
709
|
+
path: string;
|
|
710
|
+
/**
|
|
711
|
+
* File extension extracted from `baseName`.
|
|
712
|
+
*/
|
|
713
|
+
extname: Extname;
|
|
714
|
+
/**
|
|
715
|
+
* Deduplicated list of source code fragments.
|
|
716
|
+
*/
|
|
717
|
+
sources: Array<SourceNode>;
|
|
718
|
+
/**
|
|
719
|
+
* Deduplicated list of import declarations.
|
|
720
|
+
*/
|
|
721
|
+
imports: Array<ImportNode>;
|
|
722
|
+
/**
|
|
723
|
+
* Deduplicated list of export declarations.
|
|
724
|
+
*/
|
|
725
|
+
exports: Array<ExportNode>;
|
|
726
|
+
/**
|
|
727
|
+
* Optional metadata attached to this file (used by plugins for barrel generation etc.).
|
|
728
|
+
*/
|
|
729
|
+
meta?: TMeta;
|
|
730
|
+
/**
|
|
731
|
+
* Optional banner prepended to the generated file content.
|
|
732
|
+
*/
|
|
733
|
+
banner?: string;
|
|
734
|
+
/**
|
|
735
|
+
* Optional footer appended to the generated file content.
|
|
736
|
+
*/
|
|
737
|
+
footer?: string;
|
|
738
|
+
};
|
|
739
|
+
//#endregion
|
|
740
|
+
//#region src/nodes/function.d.ts
|
|
741
|
+
/**
|
|
742
|
+
* AST node representing a language-agnostic type expression used as a function parameter
|
|
743
|
+
* type annotation. Each language printer renders the variant into its own syntax.
|
|
744
|
+
*
|
|
745
|
+
* - `struct` — an inline anonymous type grouping named fields.
|
|
746
|
+
* TypeScript renders as `{ petId: string; name?: string }`.
|
|
747
|
+
* - `member` — a single named field accessed from a named group type.
|
|
748
|
+
* TypeScript renders as `PathParams['petId']`.
|
|
749
|
+
*
|
|
750
|
+
* @example Reference variant
|
|
751
|
+
* ```ts
|
|
752
|
+
* createParamsType({ variant: 'reference', name: 'QueryParams' })
|
|
753
|
+
* // QueryParams
|
|
754
|
+
* ```
|
|
755
|
+
*
|
|
756
|
+
* @example Struct variant
|
|
757
|
+
* ```ts
|
|
758
|
+
* createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
|
|
759
|
+
* // { petId: string }
|
|
760
|
+
* ```
|
|
761
|
+
*
|
|
762
|
+
* @example Member variant
|
|
763
|
+
* ```ts
|
|
764
|
+
* createParamsType({ variant: 'member', base: 'PathParams', key: 'petId' })
|
|
765
|
+
* // PathParams['petId']
|
|
766
|
+
* ```
|
|
767
|
+
*/
|
|
768
|
+
type ParamsTypeNode = BaseNode & {
|
|
769
|
+
/**
|
|
770
|
+
* Node kind.
|
|
771
|
+
*/
|
|
772
|
+
kind: 'ParamsType';
|
|
773
|
+
} & ({
|
|
774
|
+
/**
|
|
775
|
+
* Reference variant — a plain type name or identifier.
|
|
776
|
+
* TypeScript renders as-is, e.g. `string`, `QueryParams`, `Partial<Config>`.
|
|
777
|
+
*/
|
|
778
|
+
variant: 'reference';
|
|
779
|
+
/**
|
|
780
|
+
* The full type name string, e.g. `'string'`, `'QueryParams'`, `'Partial<Config>'`.
|
|
781
|
+
*/
|
|
782
|
+
name: string;
|
|
783
|
+
} | {
|
|
784
|
+
/**
|
|
785
|
+
* Struct variant — an inline anonymous type grouping named fields.
|
|
786
|
+
* TypeScript renders as `{ key: Type; other?: OtherType }`.
|
|
787
|
+
*/
|
|
788
|
+
variant: 'struct';
|
|
789
|
+
/**
|
|
790
|
+
* Properties of the struct type.
|
|
791
|
+
*/
|
|
792
|
+
properties: Array<{
|
|
793
|
+
name: string;
|
|
794
|
+
optional: boolean;
|
|
795
|
+
type: ParamsTypeNode;
|
|
796
|
+
}>;
|
|
797
|
+
} | {
|
|
798
|
+
/**
|
|
799
|
+
* Member variant — a single named field accessed from a group type.
|
|
800
|
+
* TypeScript renders as `Base['key']`.
|
|
801
|
+
*/
|
|
802
|
+
variant: 'member';
|
|
803
|
+
/**
|
|
804
|
+
* Base type name, e.g. `'DeletePetPathParams'`.
|
|
805
|
+
*/
|
|
806
|
+
base: string;
|
|
807
|
+
/**
|
|
808
|
+
* The field name to access, e.g. `'petId'`.
|
|
809
|
+
*/
|
|
810
|
+
key: string;
|
|
811
|
+
});
|
|
812
|
+
/**
|
|
813
|
+
* AST node for one function parameter.
|
|
814
|
+
*
|
|
815
|
+
* @example Required parameter
|
|
816
|
+
* `name: Type`
|
|
817
|
+
*
|
|
818
|
+
* @example Optional parameter
|
|
819
|
+
* `name?: Type`
|
|
820
|
+
*
|
|
821
|
+
* @example Parameter with default value
|
|
822
|
+
* `name: Type = defaultValue`
|
|
823
|
+
*
|
|
824
|
+
* @example Rest parameter
|
|
825
|
+
* `...name: Type[]`
|
|
826
|
+
*/
|
|
827
|
+
type FunctionParameterNode = BaseNode & {
|
|
828
|
+
/**
|
|
829
|
+
* Node kind.
|
|
830
|
+
*/
|
|
831
|
+
kind: 'FunctionParameter';
|
|
832
|
+
/**
|
|
833
|
+
* Parameter name in the generated signature.
|
|
834
|
+
*/
|
|
835
|
+
name: string;
|
|
836
|
+
/**
|
|
837
|
+
* Type annotation as a structured {@link ParamsTypeNode}.
|
|
838
|
+
* Omit for untyped output.
|
|
839
|
+
*
|
|
840
|
+
* @example Reference type node
|
|
841
|
+
* `{ kind: 'ParamsType', variant: 'reference', name: 'string' }` → `petId: string`
|
|
842
|
+
*
|
|
843
|
+
* @example Struct type node
|
|
844
|
+
* `{ kind: 'ParamsType', variant: 'struct', properties: [...] }` → `{ key: Type; other?: OtherType }`
|
|
845
|
+
*
|
|
846
|
+
* @example Member type node
|
|
847
|
+
* `{ kind: 'ParamsType', variant: 'member', base: 'PathParams', key: 'petId' }` → `PathParams['petId']`
|
|
848
|
+
*/
|
|
849
|
+
type?: ParamsTypeNode;
|
|
850
|
+
/**
|
|
851
|
+
* When `true` the parameter is emitted as a rest parameter.
|
|
852
|
+
*
|
|
853
|
+
* @example Rest parameter
|
|
854
|
+
* `...name: Type[]`
|
|
855
|
+
*/
|
|
856
|
+
rest?: boolean;
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* Optional parameter — rendered with `?` and may be omitted by the caller.
|
|
860
|
+
* Cannot be combined with `default` because a defaulted parameter is already optional.
|
|
861
|
+
*/
|
|
862
|
+
& ({
|
|
863
|
+
optional: true;
|
|
864
|
+
default?: never;
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* Required parameter, or a parameter with a default value.
|
|
868
|
+
*
|
|
869
|
+
* @example Required
|
|
870
|
+
* `name: Type`
|
|
871
|
+
*
|
|
872
|
+
* @example With default
|
|
873
|
+
* `name: Type = default`
|
|
874
|
+
*/
|
|
875
|
+
| {
|
|
876
|
+
optional?: false;
|
|
877
|
+
default?: string;
|
|
878
|
+
});
|
|
879
|
+
/**
|
|
880
|
+
* AST node for a group of related function parameters treated as a single unit.
|
|
881
|
+
*
|
|
882
|
+
* Each language printer decides how to render this group:
|
|
883
|
+
* - TypeScript/JS: destructured object `{ key1, key2 }: { key1: Type1; key2: Type2 } = {}`
|
|
884
|
+
* - Python: keyword-only args or a typed dict parameter
|
|
885
|
+
* - C# / Kotlin: named record / data-class parameter
|
|
886
|
+
*
|
|
887
|
+
* When `inline` is `true`, the group is spread as individual top-level parameters
|
|
888
|
+
* rather than wrapped in a single grouped construct.
|
|
889
|
+
*
|
|
890
|
+
* @example Grouped destructuring
|
|
891
|
+
* `{ id, name }: { id: string; name: string } = {}`
|
|
892
|
+
*
|
|
893
|
+
* @example Inline (spread as individual parameters)
|
|
894
|
+
* `id: string, name: string`
|
|
895
|
+
*/
|
|
896
|
+
type ParameterGroupNode = BaseNode & {
|
|
897
|
+
/**
|
|
898
|
+
* Node kind.
|
|
899
|
+
*/
|
|
900
|
+
kind: 'ParameterGroup';
|
|
901
|
+
/**
|
|
902
|
+
* The individual parameters that form the group.
|
|
903
|
+
* Rendered as a destructured object or spread inline when `inline` is `true`.
|
|
904
|
+
*/
|
|
905
|
+
properties: Array<FunctionParameterNode>;
|
|
906
|
+
/**
|
|
907
|
+
* Optional explicit type annotation for the whole group.
|
|
908
|
+
* When absent, printers auto-compute it from `properties`.
|
|
909
|
+
*/
|
|
910
|
+
type?: ParamsTypeNode;
|
|
911
|
+
/**
|
|
912
|
+
* When `true`, `properties` are emitted as individual top-level parameters instead of
|
|
913
|
+
* being wrapped in a single grouped construct.
|
|
914
|
+
*
|
|
915
|
+
* @default false
|
|
916
|
+
*/
|
|
917
|
+
inline?: boolean;
|
|
918
|
+
/**
|
|
919
|
+
* Whether the group as a whole is optional.
|
|
920
|
+
* If omitted, printers infer this from child properties.
|
|
921
|
+
*/
|
|
922
|
+
optional?: boolean;
|
|
923
|
+
/**
|
|
924
|
+
* Default value for the group, written verbatim after `=`.
|
|
925
|
+
* Commonly `'{}'` to allow omitting the argument entirely.
|
|
926
|
+
*/
|
|
927
|
+
default?: string;
|
|
928
|
+
};
|
|
929
|
+
/**
|
|
930
|
+
* AST node for a complete function parameter list.
|
|
931
|
+
*
|
|
932
|
+
* Printers are responsible for sorting (`required` → `optional` → `defaulted`).
|
|
933
|
+
* Nodes are plain immutable data.
|
|
934
|
+
*
|
|
935
|
+
* Renders differently depending on the output mode:
|
|
936
|
+
* - `declaration` → `(id: string, config: Config = {})` — function declaration parameters
|
|
937
|
+
* - `call` → `(id, { method, url })` — function call arguments
|
|
938
|
+
* - `keys` → `{ id, config }` — key names only (for destructuring)
|
|
939
|
+
* - `values` → `{ id: id, config: config }` — key → value pairs
|
|
940
|
+
*/
|
|
941
|
+
type FunctionParametersNode = BaseNode & {
|
|
942
|
+
/**
|
|
943
|
+
* Node kind.
|
|
944
|
+
*/
|
|
945
|
+
kind: 'FunctionParameters';
|
|
946
|
+
/**
|
|
947
|
+
* Ordered parameter nodes.
|
|
948
|
+
*/
|
|
949
|
+
params: ReadonlyArray<FunctionParameterNode | ParameterGroupNode>;
|
|
950
|
+
};
|
|
951
|
+
/**
|
|
952
|
+
* Union of all function-parameter AST node variants used by the function-parameter printer.
|
|
953
|
+
*/
|
|
954
|
+
type FunctionParamNode = FunctionParameterNode | ParameterGroupNode | FunctionParametersNode | ParamsTypeNode;
|
|
955
|
+
/**
|
|
956
|
+
* Handler map keys — one per `FunctionParamNode` kind.
|
|
957
|
+
*/
|
|
958
|
+
type FunctionNodeType = 'functionParameter' | 'parameterGroup' | 'functionParameters' | 'paramsType';
|
|
959
|
+
//#endregion
|
|
960
|
+
//#region src/nodes/property.d.ts
|
|
961
|
+
/**
|
|
962
|
+
* AST node representing one named object property.
|
|
963
|
+
*
|
|
964
|
+
* @example
|
|
965
|
+
* ```ts
|
|
966
|
+
* const property: PropertyNode = {
|
|
967
|
+
* kind: 'Property',
|
|
968
|
+
* name: 'id',
|
|
969
|
+
* schema: createSchema({ type: 'integer' }),
|
|
970
|
+
* required: true,
|
|
971
|
+
* }
|
|
972
|
+
* ```
|
|
973
|
+
*/
|
|
974
|
+
type PropertyNode = BaseNode & {
|
|
975
|
+
/**
|
|
976
|
+
* Node kind.
|
|
977
|
+
*/
|
|
978
|
+
kind: 'Property';
|
|
979
|
+
/**
|
|
980
|
+
* Property key.
|
|
981
|
+
*/
|
|
982
|
+
name: string;
|
|
983
|
+
/**
|
|
984
|
+
* Property schema.
|
|
985
|
+
*/
|
|
986
|
+
schema: SchemaNode;
|
|
987
|
+
/**
|
|
988
|
+
* Whether the property is required.
|
|
989
|
+
*/
|
|
990
|
+
required: boolean;
|
|
991
|
+
};
|
|
992
|
+
//#endregion
|
|
993
|
+
//#region src/nodes/schema.d.ts
|
|
994
|
+
type PrimitiveSchemaType =
|
|
995
|
+
/**
|
|
996
|
+
* Text value.
|
|
997
|
+
*/
|
|
998
|
+
'string'
|
|
999
|
+
/**
|
|
1000
|
+
* Floating-point number.
|
|
1001
|
+
*/
|
|
1002
|
+
| 'number'
|
|
1003
|
+
/**
|
|
1004
|
+
* Integer number.
|
|
1005
|
+
*/
|
|
1006
|
+
| 'integer'
|
|
1007
|
+
/**
|
|
1008
|
+
* Big integer number.
|
|
1009
|
+
*/
|
|
1010
|
+
| 'bigint'
|
|
1011
|
+
/**
|
|
1012
|
+
* Boolean value.
|
|
1013
|
+
*/
|
|
1014
|
+
| 'boolean'
|
|
1015
|
+
/**
|
|
1016
|
+
* Null value.
|
|
1017
|
+
*/
|
|
1018
|
+
| 'null'
|
|
1019
|
+
/**
|
|
1020
|
+
* Any value.
|
|
1021
|
+
*/
|
|
1022
|
+
| 'any'
|
|
1023
|
+
/**
|
|
1024
|
+
* Unknown value.
|
|
1025
|
+
*/
|
|
1026
|
+
| 'unknown'
|
|
1027
|
+
/**
|
|
1028
|
+
* No value (`void`).
|
|
1029
|
+
*/
|
|
1030
|
+
| 'void'
|
|
1031
|
+
/**
|
|
1032
|
+
* Never value.
|
|
1033
|
+
*/
|
|
1034
|
+
| 'never'
|
|
1035
|
+
/**
|
|
1036
|
+
* Object value.
|
|
1037
|
+
*/
|
|
1038
|
+
| 'object'
|
|
1039
|
+
/**
|
|
1040
|
+
* Array value.
|
|
1041
|
+
*/
|
|
1042
|
+
| 'array'
|
|
1043
|
+
/**
|
|
1044
|
+
* Date value.
|
|
1045
|
+
*/
|
|
1046
|
+
| 'date';
|
|
1047
|
+
/**
|
|
1048
|
+
* Composite schema types.
|
|
1049
|
+
*/
|
|
1050
|
+
type ComplexSchemaType = 'tuple' | 'union' | 'intersection' | 'enum';
|
|
1051
|
+
/**
|
|
1052
|
+
* Schema types that need special handling in generators.
|
|
1053
|
+
*/
|
|
1054
|
+
type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'ipv4' | 'ipv6' | 'blob';
|
|
1055
|
+
/**
|
|
1056
|
+
* All schema type strings.
|
|
1057
|
+
*/
|
|
1058
|
+
type SchemaType = PrimitiveSchemaType | ComplexSchemaType | SpecialSchemaType;
|
|
1059
|
+
/**
|
|
1060
|
+
* Scalar schema types without extra object/array/ref structure.
|
|
1061
|
+
*/
|
|
1062
|
+
type ScalarSchemaType = Exclude<SchemaType, 'object' | 'array' | 'tuple' | 'union' | 'intersection' | 'enum' | 'ref' | 'datetime' | 'date' | 'time' | 'string' | 'number' | 'integer' | 'bigint' | 'url' | 'uuid' | 'email'>;
|
|
1063
|
+
/**
|
|
1064
|
+
* Fields shared by all schema nodes.
|
|
1065
|
+
*/
|
|
1066
|
+
type SchemaNodeBase = BaseNode & {
|
|
1067
|
+
/**
|
|
1068
|
+
* Node kind.
|
|
1069
|
+
*/
|
|
1070
|
+
kind: 'Schema';
|
|
1071
|
+
/**
|
|
1072
|
+
* Schema name for named definitions (for example, `"Pet"`).
|
|
1073
|
+
* Inline schemas omit this field.
|
|
1074
|
+
* `null` means kubb has processed this and determined there is no applicable name.
|
|
1075
|
+
* `undefined` means the name has not been set yet.
|
|
1076
|
+
*/
|
|
1077
|
+
name?: string | null;
|
|
1078
|
+
/**
|
|
1079
|
+
* Short schema title.
|
|
1080
|
+
*/
|
|
1081
|
+
title?: string;
|
|
1082
|
+
/**
|
|
1083
|
+
* Schema description text.
|
|
1084
|
+
*/
|
|
1085
|
+
description?: string;
|
|
1086
|
+
/**
|
|
1087
|
+
* Whether `null` is allowed.
|
|
1088
|
+
*/
|
|
1089
|
+
nullable?: boolean;
|
|
1090
|
+
/**
|
|
1091
|
+
* Whether the field is optional.
|
|
1092
|
+
*/
|
|
1093
|
+
optional?: boolean;
|
|
1094
|
+
/**
|
|
1095
|
+
* Both optional and nullable (`optional` + `nullable`).
|
|
1096
|
+
*/
|
|
1097
|
+
nullish?: boolean;
|
|
1098
|
+
/**
|
|
1099
|
+
* Whether the schema is deprecated.
|
|
1100
|
+
*/
|
|
1101
|
+
deprecated?: boolean;
|
|
1102
|
+
/**
|
|
1103
|
+
* Whether the schema is read-only.
|
|
1104
|
+
*/
|
|
1105
|
+
readOnly?: boolean;
|
|
1106
|
+
/**
|
|
1107
|
+
* Whether the schema is write-only.
|
|
1108
|
+
*/
|
|
1109
|
+
writeOnly?: boolean;
|
|
1110
|
+
/**
|
|
1111
|
+
* Default value.
|
|
1112
|
+
*/
|
|
1113
|
+
default?: unknown;
|
|
1114
|
+
/**
|
|
1115
|
+
* Example value.
|
|
1116
|
+
*/
|
|
1117
|
+
example?: unknown;
|
|
1118
|
+
/**
|
|
1119
|
+
* Base primitive type.
|
|
1120
|
+
* For example, this is `'string'` for a `uuid` schema.
|
|
1121
|
+
*/
|
|
1122
|
+
primitive?: PrimitiveSchemaType;
|
|
1123
|
+
};
|
|
1124
|
+
/**
|
|
1125
|
+
* Object schema with ordered properties.
|
|
1126
|
+
*
|
|
1127
|
+
* @example
|
|
1128
|
+
* ```ts
|
|
1129
|
+
* const objectSchema: ObjectSchemaNode = {
|
|
1130
|
+
* kind: 'Schema',
|
|
1131
|
+
* type: 'object',
|
|
1132
|
+
* properties: [],
|
|
1133
|
+
* }
|
|
1134
|
+
* ```
|
|
1135
|
+
*/
|
|
1136
|
+
type ObjectSchemaNode = SchemaNodeBase & {
|
|
1137
|
+
/**
|
|
1138
|
+
* Schema type discriminator.
|
|
1139
|
+
*/
|
|
1140
|
+
type: 'object';
|
|
1141
|
+
/**
|
|
1142
|
+
* Primitive type — always `'object'` for object schemas.
|
|
1143
|
+
*/
|
|
1144
|
+
primitive: 'object';
|
|
1145
|
+
/**
|
|
1146
|
+
* Ordered object properties.
|
|
1147
|
+
*/
|
|
1148
|
+
properties: Array<PropertyNode>;
|
|
1149
|
+
/**
|
|
1150
|
+
* Additional object properties behavior:
|
|
1151
|
+
* - `true`: allow any value
|
|
1152
|
+
* - `false`: reject unknown properties (maps to `.strict()` in Zod)
|
|
1153
|
+
* - `SchemaNode`: allow values that match that schema
|
|
1154
|
+
* - `undefined`: no additional properties constraint (open object)
|
|
1155
|
+
*/
|
|
1156
|
+
additionalProperties?: SchemaNode | boolean;
|
|
1157
|
+
/**
|
|
1158
|
+
* Pattern-based property schemas.
|
|
1159
|
+
*/
|
|
1160
|
+
patternProperties?: Record<string, SchemaNode>;
|
|
1161
|
+
/**
|
|
1162
|
+
* Minimum number of properties allowed.
|
|
1163
|
+
*/
|
|
1164
|
+
minProperties?: number;
|
|
1165
|
+
/**
|
|
1166
|
+
* Maximum number of properties allowed.
|
|
1167
|
+
*/
|
|
1168
|
+
maxProperties?: number;
|
|
1169
|
+
};
|
|
1170
|
+
/**
|
|
1171
|
+
* Array-like schema (`array` or `tuple`).
|
|
1172
|
+
*
|
|
1173
|
+
* @example
|
|
1174
|
+
* ```ts
|
|
1175
|
+
* const arraySchema: ArraySchemaNode = {
|
|
1176
|
+
* kind: 'Schema',
|
|
1177
|
+
* type: 'array',
|
|
1178
|
+
* items: [],
|
|
1179
|
+
* }
|
|
1180
|
+
* ```
|
|
1181
|
+
*/
|
|
1182
|
+
type ArraySchemaNode = SchemaNodeBase & {
|
|
1183
|
+
/**
|
|
1184
|
+
* Schema type discriminator (`array` or `tuple`).
|
|
1185
|
+
*/
|
|
1186
|
+
type: 'array' | 'tuple';
|
|
1187
|
+
/**
|
|
1188
|
+
* Item schemas.
|
|
1189
|
+
*/
|
|
1190
|
+
items?: Array<SchemaNode>;
|
|
1191
|
+
/**
|
|
1192
|
+
* Tuple rest-item schema for elements beyond positional `items`.
|
|
1193
|
+
*/
|
|
1194
|
+
rest?: SchemaNode;
|
|
1195
|
+
/**
|
|
1196
|
+
* Minimum item count (or tuple length).
|
|
1197
|
+
*/
|
|
1198
|
+
min?: number;
|
|
1199
|
+
/**
|
|
1200
|
+
* Maximum item count (or tuple length).
|
|
1201
|
+
*/
|
|
1202
|
+
max?: number;
|
|
1203
|
+
/**
|
|
1204
|
+
* Whether all items must be unique.
|
|
1205
|
+
*/
|
|
1206
|
+
unique?: boolean;
|
|
1207
|
+
};
|
|
1208
|
+
/**
|
|
1209
|
+
* Shared shape for union and intersection schemas.
|
|
1210
|
+
*/
|
|
1211
|
+
type CompositeSchemaNodeBase = SchemaNodeBase & {
|
|
1212
|
+
/**
|
|
1213
|
+
* Member schemas.
|
|
1214
|
+
*/
|
|
1215
|
+
members?: Array<SchemaNode>;
|
|
1216
|
+
};
|
|
1217
|
+
/**
|
|
1218
|
+
* Union schema, often from `oneOf` or `anyOf`.
|
|
1219
|
+
*
|
|
1220
|
+
* @example
|
|
1221
|
+
* ```ts
|
|
1222
|
+
* const unionSchema: UnionSchemaNode = {
|
|
1223
|
+
* kind: 'Schema',
|
|
1224
|
+
* type: 'union',
|
|
1225
|
+
* members: [],
|
|
1226
|
+
* }
|
|
1227
|
+
* ```
|
|
1228
|
+
*/
|
|
1229
|
+
type UnionSchemaNode = CompositeSchemaNodeBase & {
|
|
1230
|
+
/**
|
|
1231
|
+
* Schema type discriminator.
|
|
1232
|
+
*/
|
|
1233
|
+
type: 'union';
|
|
1234
|
+
/**
|
|
1235
|
+
* Discriminator property name from OpenAPI `discriminator.propertyName`.
|
|
1236
|
+
*/
|
|
1237
|
+
discriminatorPropertyName?: string;
|
|
1238
|
+
/**
|
|
1239
|
+
* Logical strategy applied to union members: 'one' means exactly one member must be valid (from `oneOf`),
|
|
1240
|
+
* 'any' means any number of members can be valid (from `anyOf`).
|
|
1241
|
+
*/
|
|
1242
|
+
strategy?: 'one' | 'any';
|
|
1243
|
+
};
|
|
1244
|
+
/**
|
|
1245
|
+
* Intersection schema, often from `allOf`.
|
|
1246
|
+
*
|
|
1247
|
+
* @example
|
|
1248
|
+
* ```ts
|
|
1249
|
+
* const intersectionSchema: IntersectionSchemaNode = {
|
|
1250
|
+
* kind: 'Schema',
|
|
1251
|
+
* type: 'intersection',
|
|
1252
|
+
* members: [],
|
|
1253
|
+
* }
|
|
1254
|
+
* ```
|
|
1255
|
+
*/
|
|
1256
|
+
type IntersectionSchemaNode = CompositeSchemaNodeBase & {
|
|
1257
|
+
/**
|
|
1258
|
+
* Schema type discriminator.
|
|
1259
|
+
*/
|
|
1260
|
+
type: 'intersection';
|
|
1261
|
+
};
|
|
1262
|
+
/**
|
|
1263
|
+
* One named enum item.
|
|
1264
|
+
*/
|
|
1265
|
+
type EnumValueNode = {
|
|
1266
|
+
/**
|
|
1267
|
+
* Enum item name.
|
|
1268
|
+
*/
|
|
1269
|
+
name: string;
|
|
1270
|
+
/**
|
|
1271
|
+
* Enum item value.
|
|
1272
|
+
*/
|
|
1273
|
+
value: string | number | boolean;
|
|
1274
|
+
/**
|
|
1275
|
+
* Primitive type of the enum value.
|
|
1276
|
+
*/
|
|
1277
|
+
primitive: Extract<PrimitiveSchemaType, 'string' | 'number' | 'boolean'>;
|
|
1278
|
+
};
|
|
1279
|
+
/**
|
|
1280
|
+
* Enum schema node.
|
|
1281
|
+
*
|
|
1282
|
+
* @example
|
|
1283
|
+
* ```ts
|
|
1284
|
+
* const enumSchema: EnumSchemaNode = {
|
|
1285
|
+
* kind: 'Schema',
|
|
1286
|
+
* type: 'enum',
|
|
1287
|
+
* enumValues: ['a', 'b'],
|
|
1288
|
+
* }
|
|
1289
|
+
* ```
|
|
1290
|
+
*/
|
|
1291
|
+
type EnumSchemaNode = SchemaNodeBase & {
|
|
1292
|
+
/**
|
|
1293
|
+
* Schema type discriminator.
|
|
1294
|
+
*/
|
|
1295
|
+
type: 'enum';
|
|
1296
|
+
/**
|
|
1297
|
+
* Enum values in simple form.
|
|
1298
|
+
*/
|
|
1299
|
+
enumValues?: Array<string | number | boolean | null>;
|
|
1300
|
+
/**
|
|
1301
|
+
* Enum values in named form.
|
|
1302
|
+
* If present, this is used instead of `enumValues`.
|
|
1303
|
+
*/
|
|
1304
|
+
namedEnumValues?: Array<EnumValueNode>;
|
|
1305
|
+
};
|
|
1306
|
+
/**
|
|
1307
|
+
* Reference schema that points to another schema definition.
|
|
1308
|
+
*
|
|
1309
|
+
* @example
|
|
1310
|
+
* ```ts
|
|
1311
|
+
* const refSchema: RefSchemaNode = {
|
|
1312
|
+
* kind: 'Schema',
|
|
1313
|
+
* type: 'ref',
|
|
1314
|
+
* ref: '#/components/schemas/Pet',
|
|
1315
|
+
* }
|
|
1316
|
+
* ```
|
|
1317
|
+
*/
|
|
1318
|
+
type RefSchemaNode = SchemaNodeBase & {
|
|
1319
|
+
/**
|
|
1320
|
+
* Schema type discriminator.
|
|
1321
|
+
*/
|
|
1322
|
+
type: 'ref';
|
|
1323
|
+
/**
|
|
1324
|
+
* Referenced schema name.
|
|
1325
|
+
*/
|
|
1326
|
+
name?: string;
|
|
1327
|
+
/**
|
|
1328
|
+
* Original `$ref` path, for example, `#/components/schemas/Order`.
|
|
1329
|
+
* Used to resolve names later.
|
|
1330
|
+
*/
|
|
1331
|
+
ref?: string;
|
|
1332
|
+
/**
|
|
1333
|
+
* Pattern copied from a sibling `pattern` field.
|
|
1334
|
+
*/
|
|
1335
|
+
pattern?: string;
|
|
1336
|
+
/**
|
|
1337
|
+
* The fully-parsed schema that this ref resolves to.
|
|
1338
|
+
* Populated during OAS parsing when the referenced definition can be resolved.
|
|
1339
|
+
* `undefined` when the ref cannot be resolved or is part of a circular chain.
|
|
1340
|
+
*
|
|
1341
|
+
* Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
|
|
1342
|
+
* without following the reference manually.
|
|
1343
|
+
*/
|
|
1344
|
+
schema?: SchemaNode;
|
|
1345
|
+
};
|
|
1346
|
+
/**
|
|
1347
|
+
* Datetime schema.
|
|
1348
|
+
*
|
|
1349
|
+
* @example
|
|
1350
|
+
* ```ts
|
|
1351
|
+
* const datetimeSchema: DatetimeSchemaNode = { kind: 'Schema', type: 'datetime' }
|
|
1352
|
+
* ```
|
|
1353
|
+
*/
|
|
1354
|
+
type DatetimeSchemaNode = SchemaNodeBase & {
|
|
1355
|
+
/**
|
|
1356
|
+
* Schema type discriminator.
|
|
1357
|
+
*/
|
|
1358
|
+
type: 'datetime';
|
|
1359
|
+
/**
|
|
1360
|
+
* Whether the datetime includes a timezone offset (`dateType: 'stringOffset'`).
|
|
1361
|
+
*/
|
|
1362
|
+
offset?: boolean;
|
|
1363
|
+
/**
|
|
1364
|
+
* Whether the datetime is local (no timezone, `dateType: 'stringLocal'`).
|
|
1365
|
+
*/
|
|
1366
|
+
local?: boolean;
|
|
1367
|
+
};
|
|
1368
|
+
/**
|
|
1369
|
+
* Shared base for `date` and `time` schemas.
|
|
1370
|
+
*/
|
|
1371
|
+
type TemporalSchemaNodeBase<T extends 'date' | 'time'> = SchemaNodeBase & {
|
|
1372
|
+
/**
|
|
1373
|
+
* Schema type discriminator.
|
|
1374
|
+
*/
|
|
1375
|
+
type: T;
|
|
1376
|
+
/**
|
|
1377
|
+
* Output representation in generated code.
|
|
1378
|
+
*/
|
|
1379
|
+
representation: 'date' | 'string';
|
|
1380
|
+
};
|
|
1381
|
+
/**
|
|
1382
|
+
* Date schema node.
|
|
1383
|
+
*
|
|
1384
|
+
* @example
|
|
1385
|
+
* ```ts
|
|
1386
|
+
* const dateSchema: DateSchemaNode = { kind: 'Schema', type: 'date', representation: 'string' }
|
|
1387
|
+
* ```
|
|
1388
|
+
*/
|
|
1389
|
+
type DateSchemaNode = TemporalSchemaNodeBase<'date'>;
|
|
1390
|
+
/**
|
|
1391
|
+
* Time schema node.
|
|
1392
|
+
*
|
|
1393
|
+
* @example
|
|
1394
|
+
* ```ts
|
|
1395
|
+
* const timeSchema: TimeSchemaNode = { kind: 'Schema', type: 'time', representation: 'string' }
|
|
1396
|
+
* ```
|
|
1397
|
+
*/
|
|
1398
|
+
type TimeSchemaNode = TemporalSchemaNodeBase<'time'>;
|
|
1399
|
+
/**
|
|
1400
|
+
* String schema node.
|
|
1401
|
+
*
|
|
1402
|
+
* @example
|
|
1403
|
+
* ```ts
|
|
1404
|
+
* const stringSchema: StringSchemaNode = { kind: 'Schema', type: 'string' }
|
|
1405
|
+
* ```
|
|
1406
|
+
*/
|
|
1407
|
+
type StringSchemaNode = SchemaNodeBase & {
|
|
1408
|
+
/**
|
|
1409
|
+
* Schema type discriminator.
|
|
1410
|
+
*/
|
|
1411
|
+
type: 'string';
|
|
1412
|
+
/**
|
|
1413
|
+
* Minimum string length.
|
|
1414
|
+
*/
|
|
1415
|
+
min?: number;
|
|
1416
|
+
/**
|
|
1417
|
+
* Maximum string length.
|
|
1418
|
+
*/
|
|
1419
|
+
max?: number;
|
|
1420
|
+
/**
|
|
1421
|
+
* Regex pattern.
|
|
1422
|
+
*/
|
|
1423
|
+
pattern?: string;
|
|
1424
|
+
};
|
|
1425
|
+
/**
|
|
1426
|
+
* Numeric schema (`number`, `integer`, or `bigint`).
|
|
1427
|
+
*
|
|
1428
|
+
* @example
|
|
1429
|
+
* ```ts
|
|
1430
|
+
* const numberSchema: NumberSchemaNode = { kind: 'Schema', type: 'number' }
|
|
1431
|
+
* ```
|
|
1432
|
+
*/
|
|
1433
|
+
type NumberSchemaNode = SchemaNodeBase & {
|
|
1434
|
+
/**
|
|
1435
|
+
* Schema type discriminator.
|
|
1436
|
+
*/
|
|
1437
|
+
type: 'number' | 'integer' | 'bigint';
|
|
1438
|
+
/**
|
|
1439
|
+
* Minimum value.
|
|
1440
|
+
*/
|
|
1441
|
+
min?: number;
|
|
1442
|
+
/**
|
|
1443
|
+
* Maximum value.
|
|
1444
|
+
*/
|
|
1445
|
+
max?: number;
|
|
1446
|
+
/**
|
|
1447
|
+
* Exclusive minimum value.
|
|
1448
|
+
*/
|
|
1449
|
+
exclusiveMinimum?: number;
|
|
1450
|
+
/**
|
|
1451
|
+
* Exclusive maximum value.
|
|
1452
|
+
*/
|
|
1453
|
+
exclusiveMaximum?: number;
|
|
1454
|
+
/**
|
|
1455
|
+
* The value must be a multiple of this number.
|
|
1456
|
+
*/
|
|
1457
|
+
multipleOf?: number;
|
|
1458
|
+
};
|
|
1459
|
+
/**
|
|
1460
|
+
* Scalar schema with no extra constraints.
|
|
1461
|
+
*
|
|
1462
|
+
* @example
|
|
1463
|
+
* ```ts
|
|
1464
|
+
* const anySchema: ScalarSchemaNode = { kind: 'Schema', type: 'any' }
|
|
1465
|
+
* ```
|
|
1466
|
+
*/
|
|
1467
|
+
type ScalarSchemaNode = SchemaNodeBase & {
|
|
1468
|
+
/**
|
|
1469
|
+
* Schema type discriminator.
|
|
1470
|
+
*/
|
|
1471
|
+
type: ScalarSchemaType;
|
|
1472
|
+
};
|
|
1473
|
+
/**
|
|
1474
|
+
* URL schema node.
|
|
1475
|
+
* Can include an OpenAPI-style path template for template literal types.
|
|
1476
|
+
*
|
|
1477
|
+
* @example
|
|
1478
|
+
* ```ts
|
|
1479
|
+
* const urlSchema: UrlSchemaNode = { kind: 'Schema', type: 'url', path: '/pets/{petId}' }
|
|
1480
|
+
* ```
|
|
1481
|
+
*/
|
|
1482
|
+
type UrlSchemaNode = SchemaNodeBase & {
|
|
1483
|
+
/**
|
|
1484
|
+
* Schema type discriminator.
|
|
1485
|
+
*/
|
|
1486
|
+
type: 'url';
|
|
1487
|
+
/**
|
|
1488
|
+
* OpenAPI-style path template, for example, `'/pets/{petId}'`.
|
|
1489
|
+
*/
|
|
1490
|
+
path?: string;
|
|
1491
|
+
/**
|
|
1492
|
+
* Minimum string length.
|
|
1493
|
+
*/
|
|
1494
|
+
min?: number;
|
|
1495
|
+
/**
|
|
1496
|
+
* Maximum string length.
|
|
1497
|
+
*/
|
|
1498
|
+
max?: number;
|
|
1499
|
+
};
|
|
1500
|
+
/**
|
|
1501
|
+
* Format-string schema for string-based formats that support length constraints.
|
|
1502
|
+
*
|
|
1503
|
+
* @example
|
|
1504
|
+
* ```ts
|
|
1505
|
+
* const uuidSchema: FormatStringSchemaNode = { kind: 'Schema', type: 'uuid', min: 36, max: 36 }
|
|
1506
|
+
* ```
|
|
1507
|
+
*/
|
|
1508
|
+
type FormatStringSchemaNode = SchemaNodeBase & {
|
|
1509
|
+
/**
|
|
1510
|
+
* Schema type discriminator.
|
|
1511
|
+
*/
|
|
1512
|
+
type: 'uuid' | 'email';
|
|
1513
|
+
/**
|
|
1514
|
+
* Minimum string length.
|
|
1515
|
+
*/
|
|
1516
|
+
min?: number;
|
|
1517
|
+
/**
|
|
1518
|
+
* Maximum string length.
|
|
1519
|
+
*/
|
|
1520
|
+
max?: number;
|
|
1521
|
+
};
|
|
1522
|
+
/**
|
|
1523
|
+
* IPv4 address schema node.
|
|
1524
|
+
*
|
|
1525
|
+
* @example
|
|
1526
|
+
* ```ts
|
|
1527
|
+
* const ipv4Schema: Ipv4SchemaNode = { kind: 'Schema', type: 'ipv4' }
|
|
1528
|
+
* ```
|
|
1529
|
+
*/
|
|
1530
|
+
type Ipv4SchemaNode = SchemaNodeBase & {
|
|
1531
|
+
/**
|
|
1532
|
+
* Schema type discriminator.
|
|
1533
|
+
*/
|
|
1534
|
+
type: 'ipv4';
|
|
1535
|
+
};
|
|
1536
|
+
/**
|
|
1537
|
+
* IPv6 address schema node.
|
|
1538
|
+
*
|
|
1539
|
+
* @example
|
|
1540
|
+
* ```ts
|
|
1541
|
+
* const ipv6Schema: Ipv6SchemaNode = { kind: 'Schema', type: 'ipv6' }
|
|
1542
|
+
* ```
|
|
1543
|
+
*/
|
|
1544
|
+
type Ipv6SchemaNode = SchemaNodeBase & {
|
|
1545
|
+
/**
|
|
1546
|
+
* Schema type discriminator.
|
|
1547
|
+
*/
|
|
1548
|
+
type: 'ipv6';
|
|
1549
|
+
};
|
|
1550
|
+
/**
|
|
1551
|
+
* Mapping from schema type literals to concrete schema node types.
|
|
1552
|
+
* Used by `narrowSchema`.
|
|
1553
|
+
*/
|
|
1554
|
+
type SchemaNodeByType = {
|
|
1555
|
+
object: ObjectSchemaNode;
|
|
1556
|
+
array: ArraySchemaNode;
|
|
1557
|
+
tuple: ArraySchemaNode;
|
|
1558
|
+
union: UnionSchemaNode;
|
|
1559
|
+
intersection: IntersectionSchemaNode;
|
|
1560
|
+
enum: EnumSchemaNode;
|
|
1561
|
+
ref: RefSchemaNode;
|
|
1562
|
+
datetime: DatetimeSchemaNode;
|
|
1563
|
+
date: DateSchemaNode;
|
|
1564
|
+
time: TimeSchemaNode;
|
|
1565
|
+
string: StringSchemaNode;
|
|
1566
|
+
number: NumberSchemaNode;
|
|
1567
|
+
integer: NumberSchemaNode;
|
|
1568
|
+
bigint: NumberSchemaNode;
|
|
1569
|
+
boolean: ScalarSchemaNode;
|
|
1570
|
+
null: ScalarSchemaNode;
|
|
1571
|
+
any: ScalarSchemaNode;
|
|
1572
|
+
unknown: ScalarSchemaNode;
|
|
1573
|
+
void: ScalarSchemaNode;
|
|
1574
|
+
never: ScalarSchemaNode;
|
|
1575
|
+
uuid: FormatStringSchemaNode;
|
|
1576
|
+
email: FormatStringSchemaNode;
|
|
1577
|
+
url: UrlSchemaNode;
|
|
1578
|
+
ipv4: Ipv4SchemaNode;
|
|
1579
|
+
ipv6: Ipv6SchemaNode;
|
|
1580
|
+
blob: ScalarSchemaNode;
|
|
1581
|
+
};
|
|
1582
|
+
/**
|
|
1583
|
+
* Union of all schema node types.
|
|
1584
|
+
*/
|
|
1585
|
+
type SchemaNode = ObjectSchemaNode | ArraySchemaNode | UnionSchemaNode | IntersectionSchemaNode | EnumSchemaNode | RefSchemaNode | DatetimeSchemaNode | DateSchemaNode | TimeSchemaNode | StringSchemaNode | NumberSchemaNode | UrlSchemaNode | FormatStringSchemaNode | Ipv4SchemaNode | Ipv6SchemaNode | ScalarSchemaNode;
|
|
1586
|
+
//#endregion
|
|
1587
|
+
//#region src/nodes/parameter.d.ts
|
|
1588
|
+
type ParameterLocation = 'path' | 'query' | 'header' | 'cookie';
|
|
1589
|
+
/**
|
|
1590
|
+
* AST node representing one operation parameter.
|
|
1591
|
+
*
|
|
1592
|
+
* @example
|
|
1593
|
+
* ```ts
|
|
1594
|
+
* const param: ParameterNode = {
|
|
1595
|
+
* kind: 'Parameter',
|
|
1596
|
+
* name: 'petId',
|
|
1597
|
+
* in: 'path',
|
|
1598
|
+
* schema: createSchema({ type: 'string' }),
|
|
1599
|
+
* required: true,
|
|
1600
|
+
* }
|
|
1601
|
+
* ```
|
|
1602
|
+
*/
|
|
1603
|
+
type ParameterNode = BaseNode & {
|
|
1604
|
+
/**
|
|
1605
|
+
* Node kind.
|
|
1606
|
+
*/
|
|
1607
|
+
kind: 'Parameter';
|
|
1608
|
+
/**
|
|
1609
|
+
* Parameter name.
|
|
1610
|
+
*/
|
|
1611
|
+
name: string;
|
|
1612
|
+
/**
|
|
1613
|
+
* Parameter location (`path`, `query`, `header`, or `cookie`).
|
|
1614
|
+
*/
|
|
1615
|
+
in: ParameterLocation;
|
|
1616
|
+
/**
|
|
1617
|
+
* Parameter schema.
|
|
1618
|
+
*/
|
|
1619
|
+
schema: SchemaNode;
|
|
1620
|
+
/**
|
|
1621
|
+
* Whether the parameter is required.
|
|
1622
|
+
*/
|
|
1623
|
+
required: boolean;
|
|
1624
|
+
};
|
|
1625
|
+
//#endregion
|
|
1626
|
+
//#region src/nodes/http.d.ts
|
|
1627
|
+
/**
|
|
1628
|
+
* All supported HTTP status code literals as strings, as used in API specs
|
|
1629
|
+
* (for example, `"200"` and `"404"`).
|
|
1630
|
+
*/
|
|
1631
|
+
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';
|
|
1632
|
+
/**
|
|
1633
|
+
* Response status code literal used by operations.
|
|
1634
|
+
*
|
|
1635
|
+
* Includes specific HTTP status code strings and `"default"` for catch-all responses.
|
|
1636
|
+
*
|
|
1637
|
+
* @example
|
|
1638
|
+
* ```ts
|
|
1639
|
+
* const status: StatusCode = '200'
|
|
1640
|
+
* const fallback: StatusCode = 'default'
|
|
1641
|
+
* ```
|
|
1642
|
+
*/
|
|
1643
|
+
type StatusCode = HttpStatusCode | 'default';
|
|
1644
|
+
/**
|
|
1645
|
+
* Supported media type strings used in request and response bodies.
|
|
1646
|
+
*
|
|
1647
|
+
* @example
|
|
1648
|
+
* ```ts
|
|
1649
|
+
* const mediaType: MediaType = 'application/json'
|
|
1650
|
+
* ```
|
|
1651
|
+
*/
|
|
1652
|
+
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';
|
|
1653
|
+
//#endregion
|
|
1654
|
+
//#region src/nodes/response.d.ts
|
|
1655
|
+
/**
|
|
1656
|
+
* AST node representing one operation response variant.
|
|
1657
|
+
*
|
|
1658
|
+
* @example
|
|
1659
|
+
* ```ts
|
|
1660
|
+
* const response: ResponseNode = {
|
|
1661
|
+
* kind: 'Response',
|
|
1662
|
+
* statusCode: '200',
|
|
1663
|
+
* schema: createSchema({ type: 'string' }),
|
|
1664
|
+
* }
|
|
1665
|
+
* ```
|
|
1666
|
+
*/
|
|
1667
|
+
type ResponseNode = BaseNode & {
|
|
1668
|
+
/**
|
|
1669
|
+
* Node kind.
|
|
1670
|
+
*/
|
|
1671
|
+
kind: 'Response';
|
|
1672
|
+
/**
|
|
1673
|
+
* HTTP status code or `'default'` for a fallback response.
|
|
1674
|
+
*/
|
|
1675
|
+
statusCode: StatusCode;
|
|
1676
|
+
/**
|
|
1677
|
+
* Optional response description.
|
|
1678
|
+
*/
|
|
1679
|
+
description?: string;
|
|
1680
|
+
/**
|
|
1681
|
+
* Response body schema.
|
|
1682
|
+
*/
|
|
1683
|
+
schema: SchemaNode;
|
|
1684
|
+
/**
|
|
1685
|
+
* Response media type.
|
|
1686
|
+
*/
|
|
1687
|
+
mediaType?: MediaType | null;
|
|
1688
|
+
/**
|
|
1689
|
+
* Property keys to exclude from the generated type via `Omit<Type, Keys>`.
|
|
1690
|
+
* Set when a referenced schema has `writeOnly` fields that should not appear in response types.
|
|
1691
|
+
*/
|
|
1692
|
+
keysToOmit?: Array<string>;
|
|
1693
|
+
};
|
|
1694
|
+
//#endregion
|
|
1695
|
+
//#region src/nodes/operation.d.ts
|
|
1696
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE';
|
|
1697
|
+
/**
|
|
1698
|
+
* AST node representing one API operation.
|
|
1699
|
+
*
|
|
1700
|
+
* @example
|
|
1701
|
+
* ```ts
|
|
1702
|
+
* const operation: OperationNode = {
|
|
1703
|
+
* kind: 'Operation',
|
|
1704
|
+
* operationId: 'listPets',
|
|
1705
|
+
* method: 'GET',
|
|
1706
|
+
* path: '/pets',
|
|
1707
|
+
* tags: [],
|
|
1708
|
+
* parameters: [],
|
|
1709
|
+
* responses: [],
|
|
1710
|
+
* }
|
|
1711
|
+
* ```
|
|
1712
|
+
*/
|
|
1713
|
+
type OperationNode = BaseNode & {
|
|
1714
|
+
/**
|
|
1715
|
+
* Node kind.
|
|
1716
|
+
*/
|
|
1717
|
+
kind: 'Operation';
|
|
1718
|
+
/**
|
|
1719
|
+
* Operation identifier, usually from OpenAPI `operationId`.
|
|
1720
|
+
*/
|
|
1721
|
+
operationId: string;
|
|
1722
|
+
/**
|
|
1723
|
+
* HTTP Method like 'GET'
|
|
1724
|
+
*/
|
|
1725
|
+
method: HttpMethod;
|
|
1726
|
+
/**
|
|
1727
|
+
* OpenAPI-style path string, for example `/pets/{petId}`.
|
|
1728
|
+
* Path parameters retain the `{param}` notation from the original spec.
|
|
1729
|
+
*/
|
|
1730
|
+
path: string;
|
|
1731
|
+
/**
|
|
1732
|
+
* Group labels for the operation.
|
|
1733
|
+
* Usually copied from OpenAPI `tags`.
|
|
1734
|
+
*/
|
|
1735
|
+
tags: Array<string>;
|
|
1736
|
+
/**
|
|
1737
|
+
* Short one-line operation summary.
|
|
1738
|
+
*/
|
|
1739
|
+
summary?: string;
|
|
1740
|
+
/**
|
|
1741
|
+
* Full operation description.
|
|
1742
|
+
*/
|
|
1743
|
+
description?: string;
|
|
1744
|
+
/**
|
|
1745
|
+
* Marks the operation as deprecated.
|
|
1746
|
+
*/
|
|
1747
|
+
deprecated?: boolean;
|
|
1748
|
+
/**
|
|
1749
|
+
* Parameters that could be used, we have QueryParams, PathParams, HeaderParams and CookieParams
|
|
1750
|
+
*/
|
|
1751
|
+
parameters: Array<ParameterNode>;
|
|
1752
|
+
/**
|
|
1753
|
+
* Request body metadata for the operation.
|
|
1754
|
+
*/
|
|
1755
|
+
requestBody?: {
|
|
1756
|
+
/**
|
|
1757
|
+
* Human-readable request body description.
|
|
1758
|
+
*/
|
|
1759
|
+
description?: string;
|
|
1760
|
+
/**
|
|
1761
|
+
* Whether the request body is required (`requestBody.required: true` in the spec).
|
|
1762
|
+
* When `false` or absent, the generated `data` parameter should be optional.
|
|
1763
|
+
*/
|
|
1764
|
+
required?: boolean;
|
|
1765
|
+
/**
|
|
1766
|
+
* All available content type entries for this request body.
|
|
1767
|
+
*
|
|
1768
|
+
* When the adapter `contentType` option is set, this array contains exactly one entry for
|
|
1769
|
+
* that content type. Otherwise it contains one entry per content type declared in the spec,
|
|
1770
|
+
* so that plugins can generate code for every variant (e.g. separate hooks for
|
|
1771
|
+
* `application/json` and `multipart/form-data`).
|
|
1772
|
+
*
|
|
1773
|
+
* @example
|
|
1774
|
+
* ```ts
|
|
1775
|
+
* // spec has both application/json and multipart/form-data
|
|
1776
|
+
* requestBody.content[0].contentType // 'application/json'
|
|
1777
|
+
* requestBody.content[1].contentType // 'multipart/form-data'
|
|
1778
|
+
* ```
|
|
1779
|
+
*/
|
|
1780
|
+
content?: Array<{
|
|
1781
|
+
/**
|
|
1782
|
+
* The content type for this entry (e.g. `'application/json'`).
|
|
1783
|
+
*/
|
|
1784
|
+
contentType: string;
|
|
1785
|
+
/**
|
|
1786
|
+
* Request body schema for this content type.
|
|
1787
|
+
*/
|
|
1788
|
+
schema?: SchemaNode;
|
|
1789
|
+
/**
|
|
1790
|
+
* Property keys to exclude from the generated request body type via `Omit<Type, Keys>`.
|
|
1791
|
+
* Set when a referenced schema has `readOnly` fields that should be omitted in request types.
|
|
1792
|
+
*/
|
|
1793
|
+
keysToOmit?: Array<string>;
|
|
1794
|
+
}>;
|
|
1795
|
+
};
|
|
1796
|
+
/**
|
|
1797
|
+
* Operation responses.
|
|
1798
|
+
*/
|
|
1799
|
+
responses: Array<ResponseNode>;
|
|
1800
|
+
};
|
|
1801
|
+
//#endregion
|
|
1802
|
+
//#region src/nodes/output.d.ts
|
|
1803
|
+
/**
|
|
1804
|
+
* Output AST node that groups all generated file output for one API document.
|
|
1805
|
+
*
|
|
1806
|
+
* Produced by generators and consumed by the build pipeline to write files.
|
|
1807
|
+
*
|
|
1808
|
+
* @example
|
|
1809
|
+
* ```ts
|
|
1810
|
+
* const output: OutputNode = {
|
|
1811
|
+
* kind: 'Output',
|
|
1812
|
+
* files: [],
|
|
1813
|
+
* }
|
|
1814
|
+
* ```
|
|
1815
|
+
*/
|
|
1816
|
+
type OutputNode = BaseNode & {
|
|
1817
|
+
/**
|
|
1818
|
+
* Node kind.
|
|
1819
|
+
*/
|
|
1820
|
+
kind: 'Output';
|
|
1821
|
+
/**
|
|
1822
|
+
* Generated file nodes.
|
|
1823
|
+
*/
|
|
1824
|
+
files: Array<FileNode>;
|
|
1825
|
+
};
|
|
1826
|
+
//#endregion
|
|
1827
|
+
//#region src/nodes/root.d.ts
|
|
1828
|
+
/**
|
|
1829
|
+
* Basic metadata for an API document.
|
|
1830
|
+
* Adapters fill fields that exist in their source format.
|
|
1831
|
+
*
|
|
1832
|
+
* @example
|
|
1833
|
+
* ```ts
|
|
1834
|
+
* const meta: InputMeta = { title: 'Pet API', version: '1.0.0' }
|
|
1835
|
+
* ```
|
|
1836
|
+
*/
|
|
1837
|
+
type InputMeta = {
|
|
1838
|
+
/**
|
|
1839
|
+
* API title (from `info.title` in OAS/AsyncAPI).
|
|
1840
|
+
*/
|
|
1841
|
+
title?: string;
|
|
1842
|
+
/**
|
|
1843
|
+
* API description (from `info.description` in OAS/AsyncAPI).
|
|
1844
|
+
*/
|
|
1845
|
+
description?: string;
|
|
1846
|
+
/**
|
|
1847
|
+
* API version string (from `info.version` in OAS/AsyncAPI).
|
|
1848
|
+
*/
|
|
1849
|
+
version?: string;
|
|
1850
|
+
/**
|
|
1851
|
+
* Resolved API base URL.
|
|
1852
|
+
* For OpenAPI and AsyncAPI, this comes from the selected server URL.
|
|
1853
|
+
*/
|
|
1854
|
+
baseURL?: string;
|
|
1855
|
+
};
|
|
1856
|
+
/**
|
|
1857
|
+
* Input AST node that contains all schemas and operations for one API document.
|
|
1858
|
+
* Produced by the adapter and consumed by all Kubb plugins.
|
|
1859
|
+
*
|
|
1860
|
+
* @example
|
|
1861
|
+
* ```ts
|
|
1862
|
+
* const input: InputNode = {
|
|
1863
|
+
* kind: 'Input',
|
|
1864
|
+
* schemas: [],
|
|
1865
|
+
* operations: [],
|
|
1866
|
+
* }
|
|
1867
|
+
* ```
|
|
1868
|
+
*/
|
|
1869
|
+
type InputNode = BaseNode & {
|
|
1870
|
+
/**
|
|
1871
|
+
* Node kind.
|
|
1872
|
+
*/
|
|
1873
|
+
kind: 'Input';
|
|
1874
|
+
/**
|
|
1875
|
+
* All schema nodes in the document.
|
|
1876
|
+
*/
|
|
1877
|
+
schemas: Array<SchemaNode>;
|
|
1878
|
+
/**
|
|
1879
|
+
* All operation nodes in the document.
|
|
1880
|
+
*/
|
|
1881
|
+
operations: Array<OperationNode>;
|
|
1882
|
+
/**
|
|
1883
|
+
* Optional document metadata populated by the adapter.
|
|
1884
|
+
*/
|
|
1885
|
+
meta?: InputMeta;
|
|
1886
|
+
};
|
|
1887
|
+
//#endregion
|
|
1888
|
+
//#region src/nodes/index.d.ts
|
|
1889
|
+
/**
|
|
1890
|
+
* Union of all AST node types.
|
|
1891
|
+
*
|
|
1892
|
+
* This lets TypeScript narrow types in `switch (node.kind)` blocks.
|
|
1893
|
+
*
|
|
1894
|
+
* @example
|
|
1895
|
+
* ```ts
|
|
1896
|
+
* function getKind(node: Node): string {
|
|
1897
|
+
* switch (node.kind) {
|
|
1898
|
+
* case 'Input':
|
|
1899
|
+
* return 'input'
|
|
1900
|
+
* case 'Output':
|
|
1901
|
+
* return 'output'
|
|
1902
|
+
* default:
|
|
1903
|
+
* return 'other'
|
|
1904
|
+
* }
|
|
1905
|
+
* }
|
|
1906
|
+
* ```
|
|
1907
|
+
*/
|
|
1908
|
+
type Node = InputNode | OutputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode | FunctionParamNode | FileNode | ImportNode | ExportNode | SourceNode | ConstNode | TypeNode | ParamsTypeNode | FunctionNode | ArrowFunctionNode;
|
|
1909
|
+
//#endregion
|
|
1910
|
+
//#region src/infer.d.ts
|
|
1911
|
+
/**
|
|
1912
|
+
* Shared parser options used by OAS-to-AST inference and parser flows.
|
|
1913
|
+
*/
|
|
1914
|
+
type ParserOptions = {
|
|
1915
|
+
/**
|
|
1916
|
+
* How `format: 'date-time'` schemas are represented. `false` falls through to a plain string.
|
|
1917
|
+
*/
|
|
1918
|
+
dateType: false | 'string' | 'stringOffset' | 'stringLocal' | 'date';
|
|
1919
|
+
/**
|
|
1920
|
+
* Whether `type: 'integer'` and `format: 'int64'` produce `number` or `bigint` nodes.
|
|
1921
|
+
*/
|
|
1922
|
+
integerType?: 'number' | 'bigint';
|
|
1923
|
+
/**
|
|
1924
|
+
* AST type used when no schema type can be inferred.
|
|
1925
|
+
*/
|
|
1926
|
+
unknownType: 'any' | 'unknown' | 'void';
|
|
1927
|
+
/**
|
|
1928
|
+
* AST type used for completely empty schemas (`{}`).
|
|
1929
|
+
*/
|
|
1930
|
+
emptySchemaType: 'any' | 'unknown' | 'void';
|
|
1931
|
+
/**
|
|
1932
|
+
* Suffix appended to derived enum names when building property schema names.
|
|
1933
|
+
*/
|
|
1934
|
+
enumSuffix: 'enum' | (string & {});
|
|
1935
|
+
};
|
|
1936
|
+
/**
|
|
1937
|
+
* Maps each `dateType` option value to the AST node produced by `format: 'date-time'`.
|
|
1938
|
+
*/
|
|
1939
|
+
type DateTimeNodeByDateType = {
|
|
1940
|
+
date: DateSchemaNode;
|
|
1941
|
+
string: DatetimeSchemaNode;
|
|
1942
|
+
stringOffset: DatetimeSchemaNode;
|
|
1943
|
+
stringLocal: DatetimeSchemaNode;
|
|
1944
|
+
false: StringSchemaNode;
|
|
1945
|
+
};
|
|
1946
|
+
/**
|
|
1947
|
+
* Resolves the AST node produced by `format: 'date-time'` based on the `dateType` option.
|
|
1948
|
+
*/
|
|
1949
|
+
type ResolveDateTimeNode<TDateType extends ParserOptions['dateType']> = DateTimeNodeByDateType[TDateType extends keyof DateTimeNodeByDateType ? TDateType : 'string'];
|
|
1950
|
+
/**
|
|
1951
|
+
* Ordered list of `[schema-shape, SchemaNode]` pairs.
|
|
1952
|
+
* `InferSchemaNode` walks this tuple in order and returns the first matching node type.
|
|
1953
|
+
*/
|
|
1954
|
+
type SchemaNodeMap<TDateType extends ParserOptions['dateType'] = 'string'> = [[{
|
|
1955
|
+
$ref: string;
|
|
1956
|
+
}, RefSchemaNode], [{
|
|
1957
|
+
allOf: ReadonlyArray<unknown>;
|
|
1958
|
+
properties: object;
|
|
1959
|
+
}, IntersectionSchemaNode], [{
|
|
1960
|
+
allOf: readonly [unknown, unknown, ...unknown[]];
|
|
1961
|
+
}, IntersectionSchemaNode], [{
|
|
1962
|
+
allOf: ReadonlyArray<unknown>;
|
|
1963
|
+
}, SchemaNode], [{
|
|
1964
|
+
oneOf: ReadonlyArray<unknown>;
|
|
1965
|
+
}, UnionSchemaNode], [{
|
|
1966
|
+
anyOf: ReadonlyArray<unknown>;
|
|
1967
|
+
}, UnionSchemaNode], [{
|
|
1968
|
+
const: null;
|
|
1969
|
+
}, ScalarSchemaNode], [{
|
|
1970
|
+
const: string | number | boolean;
|
|
1971
|
+
}, EnumSchemaNode], [{
|
|
1972
|
+
type: ReadonlyArray<string>;
|
|
1973
|
+
}, UnionSchemaNode], [{
|
|
1974
|
+
type: 'array';
|
|
1975
|
+
enum: ReadonlyArray<unknown>;
|
|
1976
|
+
}, ArraySchemaNode], [{
|
|
1977
|
+
enum: ReadonlyArray<unknown>;
|
|
1978
|
+
}, EnumSchemaNode], [{
|
|
1979
|
+
type: 'enum';
|
|
1980
|
+
}, EnumSchemaNode], [{
|
|
1981
|
+
type: 'union';
|
|
1982
|
+
}, UnionSchemaNode], [{
|
|
1983
|
+
type: 'intersection';
|
|
1984
|
+
}, IntersectionSchemaNode], [{
|
|
1985
|
+
type: 'tuple';
|
|
1986
|
+
}, ArraySchemaNode], [{
|
|
1987
|
+
type: 'ref';
|
|
1988
|
+
}, RefSchemaNode], [{
|
|
1989
|
+
type: 'datetime';
|
|
1990
|
+
}, DatetimeSchemaNode], [{
|
|
1991
|
+
type: 'date';
|
|
1992
|
+
}, DateSchemaNode], [{
|
|
1993
|
+
type: 'time';
|
|
1994
|
+
}, TimeSchemaNode], [{
|
|
1995
|
+
type: 'url';
|
|
1996
|
+
}, UrlSchemaNode], [{
|
|
1997
|
+
type: 'object';
|
|
1998
|
+
}, ObjectSchemaNode], [{
|
|
1999
|
+
additionalProperties: boolean | {};
|
|
2000
|
+
}, ObjectSchemaNode], [{
|
|
2001
|
+
type: 'array';
|
|
2002
|
+
}, ArraySchemaNode], [{
|
|
2003
|
+
items: object;
|
|
2004
|
+
}, ArraySchemaNode], [{
|
|
2005
|
+
prefixItems: ReadonlyArray<unknown>;
|
|
2006
|
+
}, ArraySchemaNode], [{
|
|
2007
|
+
type: string;
|
|
2008
|
+
format: 'date-time';
|
|
2009
|
+
}, ResolveDateTimeNode<TDateType>], [{
|
|
2010
|
+
type: string;
|
|
2011
|
+
format: 'date';
|
|
2012
|
+
}, DateSchemaNode], [{
|
|
2013
|
+
type: string;
|
|
2014
|
+
format: 'time';
|
|
2015
|
+
}, TimeSchemaNode], [{
|
|
2016
|
+
format: 'date-time';
|
|
2017
|
+
}, ResolveDateTimeNode<TDateType>], [{
|
|
2018
|
+
format: 'date';
|
|
2019
|
+
}, DateSchemaNode], [{
|
|
2020
|
+
format: 'time';
|
|
2021
|
+
}, TimeSchemaNode], [{
|
|
2022
|
+
type: 'string';
|
|
2023
|
+
}, StringSchemaNode], [{
|
|
2024
|
+
type: 'number';
|
|
2025
|
+
}, NumberSchemaNode], [{
|
|
2026
|
+
type: 'integer';
|
|
2027
|
+
}, NumberSchemaNode], [{
|
|
2028
|
+
type: 'bigint';
|
|
2029
|
+
}, NumberSchemaNode], [{
|
|
2030
|
+
type: string;
|
|
2031
|
+
}, ScalarSchemaNode], [{
|
|
2032
|
+
minLength: number;
|
|
2033
|
+
}, StringSchemaNode], [{
|
|
2034
|
+
maxLength: number;
|
|
2035
|
+
}, StringSchemaNode], [{
|
|
2036
|
+
pattern: string;
|
|
2037
|
+
}, StringSchemaNode], [{
|
|
2038
|
+
minimum: number;
|
|
2039
|
+
}, NumberSchemaNode], [{
|
|
2040
|
+
maximum: number;
|
|
2041
|
+
}, NumberSchemaNode]];
|
|
2042
|
+
/**
|
|
2043
|
+
* Infers the matching AST `SchemaNode` type from an input schema shape.
|
|
2044
|
+
*/
|
|
2045
|
+
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;
|
|
2046
|
+
/**
|
|
2047
|
+
* Backward-compatible alias for `InferSchemaNode`.
|
|
2048
|
+
*/
|
|
2049
|
+
type InferSchema<TSchema extends object, TDateType extends ParserOptions['dateType'] = 'string', TEntries extends ReadonlyArray<[object, SchemaNode]> = SchemaNodeMap<TDateType>> = InferSchemaNode<TSchema, TDateType, TEntries>;
|
|
2050
|
+
//#endregion
|
|
2051
|
+
//#region src/factory.d.ts
|
|
2052
|
+
/**
|
|
2053
|
+
* Syncs property/parameter schema optionality flags from `required` and `schema.nullable`.
|
|
2054
|
+
*
|
|
2055
|
+
* - `optional` is set for non-required, non-nullable schemas.
|
|
2056
|
+
* - `nullish` is set for non-required, nullable schemas.
|
|
2057
|
+
*/
|
|
2058
|
+
declare function syncOptionality(schema: SchemaNode, required: boolean): SchemaNode;
|
|
2059
|
+
/**
|
|
2060
|
+
* Distributive `Omit` that preserves each member of a union.
|
|
2061
|
+
*
|
|
2062
|
+
* @example
|
|
2063
|
+
* ```ts
|
|
2064
|
+
* type A = { kind: 'a'; keep: string; drop: number }
|
|
2065
|
+
* type B = { kind: 'b'; keep: boolean; drop: number }
|
|
2066
|
+
* type Result = DistributiveOmit<A | B, 'drop'>
|
|
2067
|
+
* // -> { kind: 'a'; keep: string } | { kind: 'b'; keep: boolean }
|
|
2068
|
+
* ```
|
|
2069
|
+
*/
|
|
2070
|
+
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
|
2071
|
+
type CreateSchemaObjectInput = Omit<ObjectSchemaNode, 'kind' | 'properties' | 'primitive'> & {
|
|
2072
|
+
properties?: Array<PropertyNode>;
|
|
2073
|
+
primitive?: 'object';
|
|
2074
|
+
};
|
|
2075
|
+
type CreateSchemaInput = CreateSchemaObjectInput | DistributiveOmit<Exclude<SchemaNode, ObjectSchemaNode>, 'kind'>;
|
|
2076
|
+
type CreateSchemaOutput<T extends CreateSchemaInput> = InferSchemaNode<T> & {
|
|
2077
|
+
kind: 'Schema';
|
|
2078
|
+
};
|
|
2079
|
+
/**
|
|
2080
|
+
* Creates an `InputNode` with stable defaults for `schemas` and `operations`.
|
|
2081
|
+
*
|
|
2082
|
+
* @example
|
|
2083
|
+
* ```ts
|
|
2084
|
+
* const input = createInput()
|
|
2085
|
+
* // { kind: 'Input', schemas: [], operations: [] }
|
|
2086
|
+
* ```
|
|
2087
|
+
*
|
|
2088
|
+
* @example
|
|
2089
|
+
* ```ts
|
|
2090
|
+
* const input = createInput({ schemas: [petSchema] })
|
|
2091
|
+
* // keeps default operations: []
|
|
2092
|
+
* ```
|
|
2093
|
+
*/
|
|
2094
|
+
declare function createInput(overrides?: Partial<Omit<InputNode, 'kind'>>): InputNode;
|
|
2095
|
+
/**
|
|
2096
|
+
* Creates an `OutputNode` with a stable default for `files`.
|
|
2097
|
+
*
|
|
2098
|
+
* @example
|
|
2099
|
+
* ```ts
|
|
2100
|
+
* const output = createOutput()
|
|
2101
|
+
* // { kind: 'Output', files: [] }
|
|
2102
|
+
* ```
|
|
2103
|
+
*
|
|
2104
|
+
* @example
|
|
2105
|
+
* ```ts
|
|
2106
|
+
* const output = createOutput({ files: [petFile] })
|
|
2107
|
+
* ```
|
|
2108
|
+
*/
|
|
2109
|
+
declare function createOutput(overrides?: Partial<Omit<OutputNode, 'kind'>>): OutputNode;
|
|
2110
|
+
/**
|
|
2111
|
+
* Creates an `OperationNode` with default empty arrays for `tags`, `parameters`, and `responses`.
|
|
2112
|
+
*
|
|
2113
|
+
* @example
|
|
2114
|
+
* ```ts
|
|
2115
|
+
* const operation = createOperation({
|
|
2116
|
+
* operationId: 'getPetById',
|
|
2117
|
+
* method: 'GET',
|
|
2118
|
+
* path: '/pet/{petId}',
|
|
2119
|
+
* })
|
|
2120
|
+
* // tags, parameters, and responses are []
|
|
2121
|
+
* ```
|
|
2122
|
+
*
|
|
2123
|
+
* @example
|
|
2124
|
+
* ```ts
|
|
2125
|
+
* const operation = createOperation({
|
|
2126
|
+
* operationId: 'findPets',
|
|
2127
|
+
* method: 'GET',
|
|
2128
|
+
* path: '/pet/findByStatus',
|
|
2129
|
+
* tags: ['pet'],
|
|
2130
|
+
* })
|
|
2131
|
+
* ```
|
|
2132
|
+
*/
|
|
2133
|
+
declare function createOperation(props: Pick<OperationNode, 'operationId' | 'method' | 'path'> & Partial<Omit<OperationNode, 'kind' | 'operationId' | 'method' | 'path'>>): OperationNode;
|
|
2134
|
+
/**
|
|
2135
|
+
* Creates a `SchemaNode`, narrowed to the variant of `props.type`.
|
|
2136
|
+
* For object schemas, `properties` defaults to an empty array.
|
|
2137
|
+
* `primitive` is automatically inferred from `type` when not explicitly provided.
|
|
2138
|
+
*
|
|
2139
|
+
* @example
|
|
2140
|
+
* ```ts
|
|
2141
|
+
* const scalar = createSchema({ type: 'string' })
|
|
2142
|
+
* // { kind: 'Schema', type: 'string', primitive: 'string' }
|
|
2143
|
+
* ```
|
|
2144
|
+
*
|
|
2145
|
+
* @example
|
|
2146
|
+
* ```ts
|
|
2147
|
+
* const uuid = createSchema({ type: 'uuid' })
|
|
2148
|
+
* // { kind: 'Schema', type: 'uuid', primitive: 'string' }
|
|
2149
|
+
* ```
|
|
2150
|
+
*
|
|
2151
|
+
* @example
|
|
2152
|
+
* ```ts
|
|
2153
|
+
* const object = createSchema({ type: 'object' })
|
|
2154
|
+
* // { kind: 'Schema', type: 'object', primitive: 'object', properties: [] }
|
|
2155
|
+
* ```
|
|
2156
|
+
*
|
|
2157
|
+
* @example
|
|
2158
|
+
* ```ts
|
|
2159
|
+
* const enumSchema = createSchema({
|
|
2160
|
+
* type: 'enum',
|
|
2161
|
+
* primitive: 'string',
|
|
2162
|
+
* enumValues: ['available', 'pending'],
|
|
2163
|
+
* })
|
|
2164
|
+
* ```
|
|
2165
|
+
*/
|
|
2166
|
+
declare function createSchema<T extends CreateSchemaInput>(props: T): CreateSchemaOutput<T>;
|
|
2167
|
+
declare function createSchema(props: CreateSchemaInput): SchemaNode;
|
|
2168
|
+
type UserPropertyNode = Pick<PropertyNode, 'name' | 'schema'> & Partial<Omit<PropertyNode, 'kind' | 'name' | 'schema'>>;
|
|
2169
|
+
/**
|
|
2170
|
+
* Creates a `PropertyNode`.
|
|
2171
|
+
*
|
|
2172
|
+
* `required` defaults to `false`.
|
|
2173
|
+
* `schema.optional` and `schema.nullish` are derived from `required` and `schema.nullable`.
|
|
2174
|
+
*
|
|
2175
|
+
* @example
|
|
2176
|
+
* ```ts
|
|
2177
|
+
* const property = createProperty({
|
|
2178
|
+
* name: 'status',
|
|
2179
|
+
* schema: createSchema({ type: 'string' }),
|
|
2180
|
+
* })
|
|
2181
|
+
* // required=false, schema.optional=true
|
|
2182
|
+
* ```
|
|
2183
|
+
*
|
|
2184
|
+
* @example
|
|
2185
|
+
* ```ts
|
|
2186
|
+
* const property = createProperty({
|
|
2187
|
+
* name: 'status',
|
|
2188
|
+
* required: true,
|
|
2189
|
+
* schema: createSchema({ type: 'string', nullable: true }),
|
|
2190
|
+
* })
|
|
2191
|
+
* // required=true, no optional/nullish
|
|
2192
|
+
* ```
|
|
2193
|
+
*/
|
|
2194
|
+
declare function createProperty(props: UserPropertyNode): PropertyNode;
|
|
2195
|
+
/**
|
|
2196
|
+
* Creates a `ParameterNode`.
|
|
2197
|
+
*
|
|
2198
|
+
* `required` defaults to `false`.
|
|
2199
|
+
* Nested schema flags are set from `required` and `schema.nullable`.
|
|
2200
|
+
*
|
|
2201
|
+
* @example
|
|
2202
|
+
* ```ts
|
|
2203
|
+
* const param = createParameter({
|
|
2204
|
+
* name: 'petId',
|
|
2205
|
+
* in: 'path',
|
|
2206
|
+
* required: true,
|
|
2207
|
+
* schema: createSchema({ type: 'string' }),
|
|
2208
|
+
* })
|
|
2209
|
+
* ```
|
|
2210
|
+
*
|
|
2211
|
+
* @example
|
|
2212
|
+
* ```ts
|
|
2213
|
+
* const param = createParameter({
|
|
2214
|
+
* name: 'status',
|
|
2215
|
+
* in: 'query',
|
|
2216
|
+
* schema: createSchema({ type: 'string', nullable: true }),
|
|
2217
|
+
* })
|
|
2218
|
+
* // required=false, schema.nullish=true
|
|
2219
|
+
* ```
|
|
2220
|
+
*/
|
|
2221
|
+
declare function createParameter(props: Pick<ParameterNode, 'name' | 'in' | 'schema'> & Partial<Omit<ParameterNode, 'kind' | 'name' | 'in' | 'schema'>>): ParameterNode;
|
|
2222
|
+
/**
|
|
2223
|
+
* Creates a `ResponseNode`.
|
|
2224
|
+
*
|
|
2225
|
+
* @example
|
|
2226
|
+
* ```ts
|
|
2227
|
+
* const response = createResponse({
|
|
2228
|
+
* statusCode: '200',
|
|
2229
|
+
* description: 'Success',
|
|
2230
|
+
* schema: createSchema({ type: 'object', properties: [] }),
|
|
2231
|
+
* })
|
|
2232
|
+
* ```
|
|
2233
|
+
*/
|
|
2234
|
+
declare function createResponse(props: Pick<ResponseNode, 'statusCode' | 'schema'> & Partial<Omit<ResponseNode, 'kind' | 'statusCode' | 'schema'>>): ResponseNode;
|
|
2235
|
+
/**
|
|
2236
|
+
* Creates a `FunctionParameterNode`.
|
|
2237
|
+
*
|
|
2238
|
+
* `optional` defaults to `false`.
|
|
2239
|
+
*
|
|
2240
|
+
* @example Required typed param
|
|
2241
|
+
* ```ts
|
|
2242
|
+
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }) })
|
|
2243
|
+
* // → petId: string
|
|
2244
|
+
* ```
|
|
2245
|
+
*
|
|
2246
|
+
* @example Optional param
|
|
2247
|
+
* ```ts
|
|
2248
|
+
* createFunctionParameter({ name: 'params', type: createParamsType({ variant: 'reference', name: 'QueryParams' }), optional: true })
|
|
2249
|
+
* // → params?: QueryParams
|
|
2250
|
+
* ```
|
|
2251
|
+
*
|
|
2252
|
+
* @example Param with default (implicitly optional; cannot combine with `optional: true`)
|
|
2253
|
+
* ```ts
|
|
2254
|
+
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), default: '{}' })
|
|
2255
|
+
* // → config: RequestConfig = {}
|
|
2256
|
+
* ```
|
|
2257
|
+
*/
|
|
2258
|
+
declare function createFunctionParameter(props: {
|
|
2259
|
+
name: string;
|
|
2260
|
+
type?: ParamsTypeNode;
|
|
2261
|
+
rest?: boolean;
|
|
2262
|
+
} & ({
|
|
2263
|
+
optional: true;
|
|
2264
|
+
default?: never;
|
|
2265
|
+
} | {
|
|
2266
|
+
optional?: false;
|
|
2267
|
+
default?: string;
|
|
2268
|
+
})): FunctionParameterNode;
|
|
2269
|
+
/**
|
|
2270
|
+
* Creates a {@link TypeNode} representing a language-agnostic structured type expression.
|
|
2271
|
+
*
|
|
2272
|
+
* Use `variant: 'struct'` for inline anonymous types and `variant: 'member'` for a single
|
|
2273
|
+
* named field accessed from a group type. Each language's printer renders the variant
|
|
2274
|
+
* into its own syntax (TypeScript, Python, C#, Kotlin, …).
|
|
2275
|
+
*
|
|
2276
|
+
* @example Reference type (TypeScript: `QueryParams`)
|
|
2277
|
+
* ```ts
|
|
2278
|
+
* createParamsType({ variant: 'reference', name: 'QueryParams' })
|
|
2279
|
+
* ```
|
|
2280
|
+
*
|
|
2281
|
+
* @example Struct type (TypeScript: `{ petId: string }`)
|
|
2282
|
+
* ```ts
|
|
2283
|
+
* createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
|
|
2284
|
+
* ```
|
|
2285
|
+
*
|
|
2286
|
+
* @example Member type (TypeScript: `DeletePetPathParams['petId']`)
|
|
2287
|
+
* ```ts
|
|
2288
|
+
* createParamsType({ variant: 'member', base: 'DeletePetPathParams', key: 'petId' })
|
|
2289
|
+
* ```
|
|
2290
|
+
*/
|
|
2291
|
+
declare function createParamsType(props: {
|
|
2292
|
+
variant: 'reference';
|
|
2293
|
+
name: string;
|
|
2294
|
+
} | {
|
|
2295
|
+
variant: 'struct';
|
|
2296
|
+
properties: Array<{
|
|
2297
|
+
name: string;
|
|
2298
|
+
optional: boolean;
|
|
2299
|
+
type: ParamsTypeNode;
|
|
2300
|
+
}>;
|
|
2301
|
+
} | {
|
|
2302
|
+
variant: 'member';
|
|
2303
|
+
base: string;
|
|
2304
|
+
key: string;
|
|
2305
|
+
}): ParamsTypeNode;
|
|
2306
|
+
/**
|
|
2307
|
+
* Creates a `ParameterGroupNode` representing a group of related parameters treated as a unit.
|
|
2308
|
+
*
|
|
2309
|
+
* @example Grouped param (TypeScript declaration)
|
|
2310
|
+
* ```ts
|
|
2311
|
+
* createParameterGroup({
|
|
2312
|
+
* properties: [
|
|
2313
|
+
* createFunctionParameter({ name: 'id', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
2314
|
+
* createFunctionParameter({ name: 'name', type: createParamsType({ variant: 'reference', name: 'string' }), optional: true }),
|
|
2315
|
+
* ],
|
|
2316
|
+
* default: '{}',
|
|
2317
|
+
* })
|
|
2318
|
+
* // declaration → { id, name? }: { id: string; name?: string } = {}
|
|
2319
|
+
* // call → { id, name }
|
|
2320
|
+
* ```
|
|
2321
|
+
*
|
|
2322
|
+
* @example Inline (spread) — children emitted as individual top-level parameters
|
|
2323
|
+
* ```ts
|
|
2324
|
+
* createParameterGroup({
|
|
2325
|
+
* properties: [createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false })],
|
|
2326
|
+
* inline: true,
|
|
2327
|
+
* })
|
|
2328
|
+
* // declaration → petId: string
|
|
2329
|
+
* // call → petId
|
|
2330
|
+
* ```
|
|
2331
|
+
*/
|
|
2332
|
+
declare function createParameterGroup(props: Pick<ParameterGroupNode, 'properties'> & Partial<Omit<ParameterGroupNode, 'kind' | 'properties'>>): ParameterGroupNode;
|
|
2333
|
+
/**
|
|
2334
|
+
* Creates a `FunctionParametersNode` from an ordered list of parameters.
|
|
2335
|
+
*
|
|
2336
|
+
* @example
|
|
2337
|
+
* ```ts
|
|
2338
|
+
* createFunctionParameters({
|
|
2339
|
+
* params: [
|
|
2340
|
+
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
2341
|
+
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), optional: false, default: '{}' }),
|
|
2342
|
+
* ],
|
|
2343
|
+
* })
|
|
2344
|
+
* ```
|
|
2345
|
+
*
|
|
2346
|
+
* @example
|
|
2347
|
+
* ```ts
|
|
2348
|
+
* const empty = createFunctionParameters()
|
|
2349
|
+
* // { kind: 'FunctionParameters', params: [] }
|
|
2350
|
+
* ```
|
|
2351
|
+
*/
|
|
2352
|
+
declare function createFunctionParameters(props?: Partial<Omit<FunctionParametersNode, 'kind'>>): FunctionParametersNode;
|
|
2353
|
+
/**
|
|
2354
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
2355
|
+
*
|
|
2356
|
+
* @example Named import
|
|
2357
|
+
* ```ts
|
|
2358
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
2359
|
+
* // import { useState } from 'react'
|
|
2360
|
+
* ```
|
|
2361
|
+
*
|
|
2362
|
+
* @example Type-only import
|
|
2363
|
+
* ```ts
|
|
2364
|
+
* createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
|
|
2365
|
+
* // import type { FC } from 'react'
|
|
2366
|
+
* ```
|
|
2367
|
+
*/
|
|
2368
|
+
declare function createImport(props: Omit<ImportNode, 'kind'>): ImportNode;
|
|
2369
|
+
/**
|
|
2370
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
2371
|
+
*
|
|
2372
|
+
* @example Named export
|
|
2373
|
+
* ```ts
|
|
2374
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
2375
|
+
* // export { Pet } from './Pet'
|
|
2376
|
+
* ```
|
|
2377
|
+
*
|
|
2378
|
+
* @example Wildcard export
|
|
2379
|
+
* ```ts
|
|
2380
|
+
* createExport({ path: './utils' })
|
|
2381
|
+
* // export * from './utils'
|
|
2382
|
+
* ```
|
|
2383
|
+
*/
|
|
2384
|
+
declare function createExport(props: Omit<ExportNode, 'kind'>): ExportNode;
|
|
2385
|
+
/**
|
|
2386
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
2387
|
+
*
|
|
2388
|
+
* @example
|
|
2389
|
+
* ```ts
|
|
2390
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
2391
|
+
* ```
|
|
2392
|
+
*/
|
|
2393
|
+
declare function createSource(props: Omit<SourceNode, 'kind'>): SourceNode;
|
|
2394
|
+
type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> & Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>;
|
|
2395
|
+
/**
|
|
2396
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
2397
|
+
*
|
|
2398
|
+
* Computes:
|
|
2399
|
+
* - `id` — SHA256 hash of the file path
|
|
2400
|
+
* - `name` — `baseName` without extension
|
|
2401
|
+
* - `extname` — extension extracted from `baseName`
|
|
2402
|
+
*
|
|
2403
|
+
* Deduplicates:
|
|
2404
|
+
* - `sources` via `combineSources`
|
|
2405
|
+
* - `exports` via `combineExports`
|
|
2406
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
2407
|
+
*
|
|
2408
|
+
* @throws {Error} when `baseName` has no extension.
|
|
2409
|
+
*
|
|
2410
|
+
* @example
|
|
2411
|
+
* ```ts
|
|
2412
|
+
* const file = createFile({
|
|
2413
|
+
* baseName: 'petStore.ts',
|
|
2414
|
+
* path: 'src/models/petStore.ts',
|
|
2415
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
2416
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
2417
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
2418
|
+
* })
|
|
2419
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
2420
|
+
* // file.name = 'petStore'
|
|
2421
|
+
* // file.extname = '.ts'
|
|
2422
|
+
* ```
|
|
2423
|
+
*/
|
|
2424
|
+
declare function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta>;
|
|
2425
|
+
/**
|
|
2426
|
+
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
2427
|
+
*
|
|
2428
|
+
* Mirrors the `Const` component from `@kubb/renderer-jsx`.
|
|
2429
|
+
* The component's `children` are represented as `nodes`.
|
|
2430
|
+
*
|
|
2431
|
+
* @example Simple constant
|
|
2432
|
+
* ```ts
|
|
2433
|
+
* createConst({ name: 'pet' })
|
|
2434
|
+
* // const pet = ...
|
|
2435
|
+
* ```
|
|
2436
|
+
*
|
|
2437
|
+
* @example Exported constant with type and `as const`
|
|
2438
|
+
* ```ts
|
|
2439
|
+
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
2440
|
+
* // export const pets: Pet[] = ... as const
|
|
2441
|
+
* ```
|
|
2442
|
+
*
|
|
2443
|
+
* @example With JSDoc and child nodes
|
|
2444
|
+
* ```ts
|
|
2445
|
+
* createConst({
|
|
2446
|
+
* name: 'config',
|
|
2447
|
+
* export: true,
|
|
2448
|
+
* JSDoc: { comments: ['@description App configuration'] },
|
|
2449
|
+
* nodes: [],
|
|
2450
|
+
* })
|
|
2451
|
+
* ```
|
|
2452
|
+
*/
|
|
2453
|
+
declare function createConst(props: Omit<ConstNode, 'kind'>): ConstNode;
|
|
2454
|
+
/**
|
|
2455
|
+
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
2456
|
+
*
|
|
2457
|
+
* Mirrors the `Type` component from `@kubb/renderer-jsx`.
|
|
2458
|
+
* The component's `children` are represented as `nodes`.
|
|
2459
|
+
*
|
|
2460
|
+
* @example Simple type alias
|
|
2461
|
+
* ```ts
|
|
2462
|
+
* createType({ name: 'Pet' })
|
|
2463
|
+
* // type Pet = ...
|
|
2464
|
+
* ```
|
|
2465
|
+
*
|
|
2466
|
+
* @example Exported type with JSDoc
|
|
2467
|
+
* ```ts
|
|
2468
|
+
* createType({
|
|
2469
|
+
* name: 'PetStatus',
|
|
2470
|
+
* export: true,
|
|
2471
|
+
* JSDoc: { comments: ['@description Status of a pet'] },
|
|
2472
|
+
* })
|
|
2473
|
+
* // export type PetStatus = ...
|
|
2474
|
+
* ```
|
|
2475
|
+
*/
|
|
2476
|
+
declare function createType(props: Omit<TypeNode, 'kind'>): TypeNode;
|
|
2477
|
+
/**
|
|
2478
|
+
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
2479
|
+
*
|
|
2480
|
+
* Mirrors the `Function` component from `@kubb/renderer-jsx`.
|
|
2481
|
+
* The component's `children` are represented as `nodes`.
|
|
2482
|
+
*
|
|
2483
|
+
* @example Simple function
|
|
2484
|
+
* ```ts
|
|
2485
|
+
* createFunction({ name: 'getPet' })
|
|
2486
|
+
* // function getPet() { ... }
|
|
2487
|
+
* ```
|
|
2488
|
+
*
|
|
2489
|
+
* @example Exported async function with return type
|
|
2490
|
+
* ```ts
|
|
2491
|
+
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
2492
|
+
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
2493
|
+
* ```
|
|
2494
|
+
*
|
|
2495
|
+
* @example Function with generics and params
|
|
2496
|
+
* ```ts
|
|
2497
|
+
* createFunction({
|
|
2498
|
+
* name: 'identity',
|
|
2499
|
+
* export: true,
|
|
2500
|
+
* generics: ['T'],
|
|
2501
|
+
* params: 'value: T',
|
|
2502
|
+
* returnType: 'T',
|
|
2503
|
+
* })
|
|
2504
|
+
* // export function identity<T>(value: T): T { ... }
|
|
2505
|
+
* ```
|
|
2506
|
+
*/
|
|
2507
|
+
declare function createFunction(props: Omit<FunctionNode, 'kind'>): FunctionNode;
|
|
2508
|
+
/**
|
|
2509
|
+
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
2510
|
+
*
|
|
2511
|
+
* Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.
|
|
2512
|
+
* The component's `children` are represented as `nodes`.
|
|
2513
|
+
*
|
|
2514
|
+
* @example Simple arrow function
|
|
2515
|
+
* ```ts
|
|
2516
|
+
* createArrowFunction({ name: 'getPet' })
|
|
2517
|
+
* // const getPet = () => { ... }
|
|
2518
|
+
* ```
|
|
2519
|
+
*
|
|
2520
|
+
* @example Single-line exported arrow function
|
|
2521
|
+
* ```ts
|
|
2522
|
+
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
2523
|
+
* // export const double = (n: number) => ...
|
|
2524
|
+
* ```
|
|
2525
|
+
*
|
|
2526
|
+
* @example Async arrow function with generics
|
|
2527
|
+
* ```ts
|
|
2528
|
+
* createArrowFunction({
|
|
2529
|
+
* name: 'fetchPet',
|
|
2530
|
+
* export: true,
|
|
2531
|
+
* async: true,
|
|
2532
|
+
* generics: ['T'],
|
|
2533
|
+
* params: 'id: string',
|
|
2534
|
+
* returnType: 'T',
|
|
2535
|
+
* })
|
|
2536
|
+
* // export const fetchPet = async <T>(id: string): Promise<T> => { ... }
|
|
2537
|
+
* ```
|
|
2538
|
+
*/
|
|
2539
|
+
declare function createArrowFunction(props: Omit<ArrowFunctionNode, 'kind'>): ArrowFunctionNode;
|
|
2540
|
+
/**
|
|
2541
|
+
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
2542
|
+
*
|
|
2543
|
+
* Use this instead of bare strings when building `nodes` arrays so that every
|
|
2544
|
+
* entry in the array is a typed {@link CodeNode}.
|
|
2545
|
+
*
|
|
2546
|
+
* @example
|
|
2547
|
+
* ```ts
|
|
2548
|
+
* createText('return fetch(id)')
|
|
2549
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
2550
|
+
* ```
|
|
2551
|
+
*/
|
|
2552
|
+
declare function createText(value: string): TextNode;
|
|
2553
|
+
/**
|
|
2554
|
+
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
2555
|
+
*
|
|
2556
|
+
* Corresponds to `<br/>` in JSX components. Prints as an empty string which,
|
|
2557
|
+
* when joined with `\n` by `printNodes`, produces a blank line.
|
|
2558
|
+
*
|
|
2559
|
+
* @example
|
|
2560
|
+
* ```ts
|
|
2561
|
+
* createBreak()
|
|
2562
|
+
* // { kind: 'Break' }
|
|
2563
|
+
* ```
|
|
2564
|
+
*/
|
|
2565
|
+
declare function createBreak(): BreakNode;
|
|
2566
|
+
/**
|
|
2567
|
+
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
2568
|
+
*
|
|
2569
|
+
* Use this to embed JSX markup (including fragments `<>…</>`) directly in generated code.
|
|
2570
|
+
*
|
|
2571
|
+
* @example
|
|
2572
|
+
* ```ts
|
|
2573
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
2574
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
2575
|
+
* ```
|
|
2576
|
+
*/
|
|
2577
|
+
declare function createJsx(value: string): JsxNode;
|
|
2578
|
+
//#endregion
|
|
4
2579
|
//#region src/guards.d.ts
|
|
5
2580
|
/**
|
|
6
|
-
* Narrows a `SchemaNode` to the
|
|
2581
|
+
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
2582
|
+
*
|
|
2583
|
+
* @example
|
|
2584
|
+
* ```ts
|
|
2585
|
+
* const schema = createSchema({ type: 'string' })
|
|
2586
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | undefined
|
|
2587
|
+
* ```
|
|
7
2588
|
*/
|
|
8
2589
|
declare function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | undefined;
|
|
9
2590
|
/**
|
|
10
|
-
*
|
|
2591
|
+
* Returns `true` when the input is an `InputNode`.
|
|
2592
|
+
*
|
|
2593
|
+
* @example
|
|
2594
|
+
* ```ts
|
|
2595
|
+
* if (isInputNode(node)) {
|
|
2596
|
+
* console.log(node.schemas.length)
|
|
2597
|
+
* }
|
|
2598
|
+
* ```
|
|
2599
|
+
*/
|
|
2600
|
+
declare const isInputNode: (node: unknown) => node is InputNode;
|
|
2601
|
+
/**
|
|
2602
|
+
* Returns `true` when the input is an `OutputNode`.
|
|
2603
|
+
*
|
|
2604
|
+
* @example
|
|
2605
|
+
* ```ts
|
|
2606
|
+
* if (isOutputNode(node)) {
|
|
2607
|
+
* console.log(node.files.length)
|
|
2608
|
+
* }
|
|
2609
|
+
* ```
|
|
11
2610
|
*/
|
|
12
|
-
declare const
|
|
2611
|
+
declare const isOutputNode: (node: unknown) => node is OutputNode;
|
|
13
2612
|
/**
|
|
14
|
-
*
|
|
2613
|
+
* Returns `true` when the input is an `OperationNode`.
|
|
2614
|
+
*
|
|
2615
|
+
* @example
|
|
2616
|
+
* ```ts
|
|
2617
|
+
* if (isOperationNode(node)) {
|
|
2618
|
+
* console.log(node.operationId)
|
|
2619
|
+
* }
|
|
2620
|
+
* ```
|
|
15
2621
|
*/
|
|
16
2622
|
declare const isOperationNode: (node: unknown) => node is OperationNode;
|
|
17
2623
|
/**
|
|
18
|
-
*
|
|
2624
|
+
* Returns `true` when the input is a `SchemaNode`.
|
|
2625
|
+
*
|
|
2626
|
+
* @example
|
|
2627
|
+
* ```ts
|
|
2628
|
+
* if (isSchemaNode(node)) {
|
|
2629
|
+
* console.log(node.type)
|
|
2630
|
+
* }
|
|
2631
|
+
* ```
|
|
19
2632
|
*/
|
|
20
2633
|
declare const isSchemaNode: (node: unknown) => node is SchemaNode;
|
|
2634
|
+
//#endregion
|
|
2635
|
+
//#region src/printer.d.ts
|
|
21
2636
|
/**
|
|
22
|
-
*
|
|
2637
|
+
* Runtime context passed as `this` to printer handlers.
|
|
2638
|
+
*
|
|
2639
|
+
* `this.transform` dispatches to node-level handlers from `nodes`.
|
|
2640
|
+
*
|
|
2641
|
+
* @example
|
|
2642
|
+
* ```ts
|
|
2643
|
+
* const context: PrinterHandlerContext<string, {}> = {
|
|
2644
|
+
* options: {},
|
|
2645
|
+
* transform: () => 'value',
|
|
2646
|
+
* }
|
|
2647
|
+
* ```
|
|
2648
|
+
*/
|
|
2649
|
+
type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
2650
|
+
/**
|
|
2651
|
+
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
|
|
2652
|
+
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
|
|
2653
|
+
*/
|
|
2654
|
+
transform: (node: SchemaNode) => TOutput | null | undefined;
|
|
2655
|
+
/**
|
|
2656
|
+
* Options for this printer instance.
|
|
2657
|
+
*/
|
|
2658
|
+
options: TOptions;
|
|
2659
|
+
};
|
|
2660
|
+
/**
|
|
2661
|
+
* Handler for one schema node type.
|
|
2662
|
+
*
|
|
2663
|
+
* Use a regular function (not an arrow function) if you need `this`.
|
|
2664
|
+
*
|
|
2665
|
+
* @example
|
|
2666
|
+
* ```ts
|
|
2667
|
+
* const handler: PrinterHandler<string, {}, 'string'> = function () {
|
|
2668
|
+
* return 'string'
|
|
2669
|
+
* }
|
|
2670
|
+
* ```
|
|
2671
|
+
*/
|
|
2672
|
+
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null | undefined;
|
|
2673
|
+
/**
|
|
2674
|
+
* Partial map of per-node-type handler overrides for a printer.
|
|
2675
|
+
*
|
|
2676
|
+
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
|
|
2677
|
+
* Supply only the handlers you want to replace; the printer's built-in
|
|
2678
|
+
* defaults fill in the rest.
|
|
2679
|
+
*
|
|
2680
|
+
* @example
|
|
2681
|
+
* ```ts
|
|
2682
|
+
* pluginZod({
|
|
2683
|
+
* printer: {
|
|
2684
|
+
* nodes: {
|
|
2685
|
+
* date(): string {
|
|
2686
|
+
* return 'z.string().date()'
|
|
2687
|
+
* },
|
|
2688
|
+
* } satisfies PrinterPartial<string, PrinterZodOptions>,
|
|
2689
|
+
* },
|
|
2690
|
+
* })
|
|
2691
|
+
* ```
|
|
2692
|
+
*/
|
|
2693
|
+
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
|
|
2694
|
+
/**
|
|
2695
|
+
* Generic shape used by `definePrinter`.
|
|
2696
|
+
*
|
|
2697
|
+
* - `TName` — unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
2698
|
+
* - `TOptions` — options passed to and stored on the printer instance
|
|
2699
|
+
* - `TOutput` — the type emitted by node handlers
|
|
2700
|
+
* - `TPrintOutput` — type returned by public `print` (defaults to `TOutput`)
|
|
2701
|
+
*
|
|
2702
|
+
* @example
|
|
2703
|
+
* ```ts
|
|
2704
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
2705
|
+
* ```
|
|
2706
|
+
*/
|
|
2707
|
+
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
2708
|
+
name: TName;
|
|
2709
|
+
options: TOptions;
|
|
2710
|
+
output: TOutput;
|
|
2711
|
+
printOutput: TPrintOutput;
|
|
2712
|
+
};
|
|
2713
|
+
/**
|
|
2714
|
+
* Printer instance returned by a printer factory.
|
|
2715
|
+
*
|
|
2716
|
+
* @example
|
|
2717
|
+
* ```ts
|
|
2718
|
+
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
2719
|
+
* ```
|
|
2720
|
+
*/
|
|
2721
|
+
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
2722
|
+
/**
|
|
2723
|
+
* Unique identifier supplied at creation time.
|
|
2724
|
+
*/
|
|
2725
|
+
name: T['name'];
|
|
2726
|
+
/**
|
|
2727
|
+
* Options for this printer instance.
|
|
2728
|
+
*/
|
|
2729
|
+
options: T['options'];
|
|
2730
|
+
/**
|
|
2731
|
+
* Node-level dispatcher — converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
|
|
2732
|
+
* Always dispatches through the `nodes` map; never calls the `print` override.
|
|
2733
|
+
* Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
2734
|
+
*/
|
|
2735
|
+
transform: (node: SchemaNode) => T['output'] | null | undefined;
|
|
2736
|
+
/**
|
|
2737
|
+
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
2738
|
+
* higher-level function (which may produce full declarations).
|
|
2739
|
+
* Otherwise, falls back to the node-level dispatcher.
|
|
2740
|
+
*/
|
|
2741
|
+
print: (node: SchemaNode) => T['printOutput'] | null | undefined;
|
|
2742
|
+
};
|
|
2743
|
+
/**
|
|
2744
|
+
* Builder function passed to `definePrinter`.
|
|
2745
|
+
*
|
|
2746
|
+
* It receives resolved options and returns:
|
|
2747
|
+
* - `name`
|
|
2748
|
+
* - `options`
|
|
2749
|
+
* - `nodes` handlers
|
|
2750
|
+
* - optional top-level `print` override
|
|
2751
|
+
*
|
|
2752
|
+
* @example
|
|
2753
|
+
* ```ts
|
|
2754
|
+
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
2755
|
+
* ```
|
|
2756
|
+
*/
|
|
2757
|
+
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
2758
|
+
name: T['name'];
|
|
2759
|
+
/**
|
|
2760
|
+
* Options to store on the printer.
|
|
2761
|
+
*/
|
|
2762
|
+
options: T['options'];
|
|
2763
|
+
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
2764
|
+
/**
|
|
2765
|
+
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
2766
|
+
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
2767
|
+
* not the override itself — so recursion is safe.
|
|
2768
|
+
*/
|
|
2769
|
+
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
|
|
2770
|
+
};
|
|
2771
|
+
/**
|
|
2772
|
+
* Creates a schema printer factory.
|
|
2773
|
+
*
|
|
2774
|
+
* This function wraps a builder and makes options optional at call sites.
|
|
2775
|
+
*
|
|
2776
|
+
* The builder receives resolved options and returns:
|
|
2777
|
+
* - `name` — a unique identifier for the printer
|
|
2778
|
+
* - `options` — options stored on the returned printer instance
|
|
2779
|
+
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
2780
|
+
* - `print` _(optional)_ — top-level override exposed as `printer.print`
|
|
2781
|
+
* - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
|
|
2782
|
+
* - This keeps recursion safe and avoids self-calls
|
|
2783
|
+
*
|
|
2784
|
+
* When no `print` override is provided, `printer.print` falls back to `printer.transform` (the node-level dispatcher).
|
|
2785
|
+
*
|
|
2786
|
+
* @example Basic usage — Zod schema printer
|
|
2787
|
+
* ```ts
|
|
2788
|
+
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
2789
|
+
*
|
|
2790
|
+
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
2791
|
+
* name: 'zod',
|
|
2792
|
+
* options: { strict: options.strict ?? true },
|
|
2793
|
+
* nodes: {
|
|
2794
|
+
* string: () => 'z.string()',
|
|
2795
|
+
* object(node) {
|
|
2796
|
+
* const props = node.properties.map(p => `${p.name}: ${this.transform(p.schema)}`).join(', ')
|
|
2797
|
+
* return `z.object({ ${props} })`
|
|
2798
|
+
* },
|
|
2799
|
+
* },
|
|
2800
|
+
* }))
|
|
2801
|
+
* ```
|
|
2802
|
+
*/
|
|
2803
|
+
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
|
|
2804
|
+
/**
|
|
2805
|
+
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
2806
|
+
**
|
|
2807
|
+
* @example
|
|
2808
|
+
* ```ts
|
|
2809
|
+
* export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>(
|
|
2810
|
+
* (node) => kindToHandlerKey[node.kind],
|
|
2811
|
+
* )
|
|
2812
|
+
* ```
|
|
2813
|
+
*/
|
|
2814
|
+
declare function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | undefined): <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
|
|
2815
|
+
name: T["name"];
|
|
2816
|
+
options: T["options"];
|
|
2817
|
+
nodes: Partial<{ [K in TKey]: (this: {
|
|
2818
|
+
transform: (node: TNode) => T["output"] | null | undefined;
|
|
2819
|
+
options: T["options"];
|
|
2820
|
+
}, node: TNodeByKey[K]) => T["output"] | null | undefined }>;
|
|
2821
|
+
print?: (this: {
|
|
2822
|
+
transform: (node: TNode) => T["output"] | null | undefined;
|
|
2823
|
+
options: T["options"];
|
|
2824
|
+
}, node: TNode) => T["printOutput"] | null | undefined;
|
|
2825
|
+
}) => (options?: T["options"]) => {
|
|
2826
|
+
name: T["name"];
|
|
2827
|
+
options: T["options"];
|
|
2828
|
+
transform: (node: TNode) => T["output"] | null | undefined;
|
|
2829
|
+
print: (node: TNode) => T["printOutput"] | null | undefined;
|
|
2830
|
+
};
|
|
2831
|
+
//#endregion
|
|
2832
|
+
//#region src/refs.d.ts
|
|
2833
|
+
/**
|
|
2834
|
+
* Lookup map from schema name to `SchemaNode`.
|
|
23
2835
|
*/
|
|
24
|
-
|
|
2836
|
+
type RefMap = Map<string, SchemaNode>;
|
|
25
2837
|
/**
|
|
26
|
-
*
|
|
2838
|
+
* Returns the last path segment of a reference string.
|
|
2839
|
+
*
|
|
2840
|
+
* Example: `#/components/schemas/Pet` becomes `Pet`.
|
|
2841
|
+
*
|
|
2842
|
+
* @example
|
|
2843
|
+
* ```ts
|
|
2844
|
+
* extractRefName('#/components/schemas/Pet') // 'Pet'
|
|
2845
|
+
* ```
|
|
2846
|
+
*/
|
|
2847
|
+
declare function extractRefName(ref: string): string;
|
|
2848
|
+
//#endregion
|
|
2849
|
+
//#region src/resolvers.d.ts
|
|
2850
|
+
declare function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null;
|
|
2851
|
+
declare function childName(parentName: string | null | undefined, propName: string): string | null;
|
|
2852
|
+
declare function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string;
|
|
2853
|
+
/**
|
|
2854
|
+
* Collects import entries for all `ref` schema nodes in `node`.
|
|
2855
|
+
*/
|
|
2856
|
+
declare function collectImports<TImport>({
|
|
2857
|
+
node,
|
|
2858
|
+
nameMapping,
|
|
2859
|
+
resolve
|
|
2860
|
+
}: {
|
|
2861
|
+
node: SchemaNode;
|
|
2862
|
+
nameMapping: Map<string, string>;
|
|
2863
|
+
resolve: (schemaName: string) => TImport | undefined;
|
|
2864
|
+
}): Array<TImport>;
|
|
2865
|
+
//#endregion
|
|
2866
|
+
//#region src/transformers.d.ts
|
|
2867
|
+
/**
|
|
2868
|
+
* Replaces a discriminator property's schema with a string enum of allowed values.
|
|
2869
|
+
*
|
|
2870
|
+
* If `node` is not an object schema, or if the property does not exist, the input
|
|
2871
|
+
* node is returned as-is.
|
|
2872
|
+
*
|
|
2873
|
+
* @example
|
|
2874
|
+
* ```ts
|
|
2875
|
+
* const schema = createSchema({
|
|
2876
|
+
* type: 'object',
|
|
2877
|
+
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
|
|
2878
|
+
* })
|
|
2879
|
+
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
|
|
2880
|
+
* ```
|
|
2881
|
+
*/
|
|
2882
|
+
declare function setDiscriminatorEnum({
|
|
2883
|
+
node,
|
|
2884
|
+
propertyName,
|
|
2885
|
+
values,
|
|
2886
|
+
enumName
|
|
2887
|
+
}: {
|
|
2888
|
+
node: SchemaNode;
|
|
2889
|
+
propertyName: string;
|
|
2890
|
+
values: Array<string>;
|
|
2891
|
+
enumName?: string;
|
|
2892
|
+
}): SchemaNode;
|
|
2893
|
+
/**
|
|
2894
|
+
* Merges adjacent anonymous object members into a single anonymous object member.
|
|
2895
|
+
*
|
|
2896
|
+
* @example
|
|
2897
|
+
* ```ts
|
|
2898
|
+
* const merged = mergeAdjacentObjects([
|
|
2899
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
|
|
2900
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
|
|
2901
|
+
* ])
|
|
2902
|
+
* ```
|
|
27
2903
|
*/
|
|
28
|
-
declare
|
|
2904
|
+
declare function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode>;
|
|
29
2905
|
/**
|
|
30
|
-
*
|
|
2906
|
+
* Removes enum members that are covered by broader scalar primitives in the same union.
|
|
2907
|
+
*
|
|
2908
|
+
* @example
|
|
2909
|
+
* ```ts
|
|
2910
|
+
* const simplified = simplifyUnion([
|
|
2911
|
+
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
|
|
2912
|
+
* createSchema({ type: 'string' }),
|
|
2913
|
+
* ])
|
|
2914
|
+
* // keeps only string member
|
|
2915
|
+
* ```
|
|
31
2916
|
*/
|
|
32
|
-
declare
|
|
2917
|
+
declare function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode>;
|
|
2918
|
+
declare function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode;
|
|
2919
|
+
//#endregion
|
|
2920
|
+
//#region src/visitor.d.ts
|
|
2921
|
+
/**
|
|
2922
|
+
* Ordered mapping of `[NodeType, ParentType]` pairs.
|
|
2923
|
+
*
|
|
2924
|
+
* `ParentOf` uses this map to find parent types.
|
|
2925
|
+
*/
|
|
2926
|
+
type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [OperationNode, InputNode], [SchemaNode, InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
|
|
2927
|
+
/**
|
|
2928
|
+
* Resolves the parent node type for a given AST node type.
|
|
2929
|
+
*
|
|
2930
|
+
* This is used by visitor context so `ctx.parent` is correctly typed
|
|
2931
|
+
* for each callback.
|
|
2932
|
+
*
|
|
2933
|
+
* @example
|
|
2934
|
+
* ```ts
|
|
2935
|
+
* type InputParent = ParentOf<InputNode>
|
|
2936
|
+
* // undefined
|
|
2937
|
+
* ```
|
|
2938
|
+
*
|
|
2939
|
+
* @example
|
|
2940
|
+
* ```ts
|
|
2941
|
+
* type PropertyParent = ParentOf<PropertyNode>
|
|
2942
|
+
* // SchemaNode
|
|
2943
|
+
* ```
|
|
2944
|
+
*
|
|
2945
|
+
* @example
|
|
2946
|
+
* ```ts
|
|
2947
|
+
* type SchemaParent = ParentOf<SchemaNode>
|
|
2948
|
+
* // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
|
|
2949
|
+
* ```
|
|
2950
|
+
*/
|
|
2951
|
+
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;
|
|
2952
|
+
/**
|
|
2953
|
+
* Traversal context passed as the second argument to every visitor callback.
|
|
2954
|
+
* `parent` is typed from the current node type.
|
|
2955
|
+
*
|
|
2956
|
+
* @example
|
|
2957
|
+
* ```ts
|
|
2958
|
+
* const visitor: Visitor = {
|
|
2959
|
+
* schema(node, { parent }) {
|
|
2960
|
+
* // parent type is narrowed by node kind
|
|
2961
|
+
* },
|
|
2962
|
+
* }
|
|
2963
|
+
* ```
|
|
2964
|
+
*/
|
|
2965
|
+
type VisitorContext<T extends Node = Node> = {
|
|
2966
|
+
/**
|
|
2967
|
+
* Parent node of the currently visited node.
|
|
2968
|
+
* For `InputNode`, this is `undefined`.
|
|
2969
|
+
*/
|
|
2970
|
+
parent?: ParentOf<T>;
|
|
2971
|
+
};
|
|
2972
|
+
/**
|
|
2973
|
+
* Synchronous visitor used by `transform`.
|
|
2974
|
+
*
|
|
2975
|
+
* @example
|
|
2976
|
+
* ```ts
|
|
2977
|
+
* const visitor: Visitor = {
|
|
2978
|
+
* operation(node) {
|
|
2979
|
+
* return { ...node, operationId: `x_${node.operationId}` }
|
|
2980
|
+
* },
|
|
2981
|
+
* }
|
|
2982
|
+
* ```
|
|
2983
|
+
*/
|
|
2984
|
+
type Visitor = {
|
|
2985
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): void | InputNode;
|
|
2986
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): void | OutputNode;
|
|
2987
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): void | OperationNode;
|
|
2988
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): void | SchemaNode;
|
|
2989
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): void | PropertyNode;
|
|
2990
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): void | ParameterNode;
|
|
2991
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): void | ResponseNode;
|
|
2992
|
+
};
|
|
2993
|
+
/**
|
|
2994
|
+
* Utility type for values that can be returned directly or asynchronously.
|
|
2995
|
+
*/
|
|
2996
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
2997
|
+
/**
|
|
2998
|
+
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
|
|
2999
|
+
*
|
|
3000
|
+
* @example
|
|
3001
|
+
* ```ts
|
|
3002
|
+
* const visitor: AsyncVisitor = {
|
|
3003
|
+
* async operation(node) {
|
|
3004
|
+
* await Promise.resolve(node.operationId)
|
|
3005
|
+
* },
|
|
3006
|
+
* }
|
|
3007
|
+
* ```
|
|
3008
|
+
*/
|
|
3009
|
+
type AsyncVisitor = {
|
|
3010
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<void | InputNode>;
|
|
3011
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<void | OutputNode>;
|
|
3012
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<void | OperationNode>;
|
|
3013
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<void | SchemaNode>;
|
|
3014
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<void | PropertyNode>;
|
|
3015
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<void | ParameterNode>;
|
|
3016
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<void | ResponseNode>;
|
|
3017
|
+
};
|
|
3018
|
+
/**
|
|
3019
|
+
* Visitor used by `collect`.
|
|
3020
|
+
*
|
|
3021
|
+
* @example
|
|
3022
|
+
* ```ts
|
|
3023
|
+
* const visitor: CollectVisitor<string> = {
|
|
3024
|
+
* operation(node) {
|
|
3025
|
+
* return node.operationId
|
|
3026
|
+
* },
|
|
3027
|
+
* }
|
|
3028
|
+
* ```
|
|
3029
|
+
*/
|
|
3030
|
+
type CollectVisitor<T> = {
|
|
3031
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): T | undefined;
|
|
3032
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | undefined;
|
|
3033
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | undefined;
|
|
3034
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | undefined;
|
|
3035
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | undefined;
|
|
3036
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | undefined;
|
|
3037
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | undefined;
|
|
3038
|
+
};
|
|
3039
|
+
/**
|
|
3040
|
+
* Options for `transform`.
|
|
3041
|
+
*
|
|
3042
|
+
* @example
|
|
3043
|
+
* ```ts
|
|
3044
|
+
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
|
|
3045
|
+
* ```
|
|
3046
|
+
*
|
|
3047
|
+
* @example
|
|
3048
|
+
* ```ts
|
|
3049
|
+
* // Only transform the current node, not nested children
|
|
3050
|
+
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
|
|
3051
|
+
* ```
|
|
3052
|
+
*/
|
|
3053
|
+
type TransformOptions = Visitor & {
|
|
3054
|
+
/**
|
|
3055
|
+
* Traversal depth (`'deep'` by default).
|
|
3056
|
+
* @default 'deep'
|
|
3057
|
+
*/
|
|
3058
|
+
depth?: VisitorDepth;
|
|
3059
|
+
/**
|
|
3060
|
+
* Internal parent override used during recursion.
|
|
3061
|
+
*/
|
|
3062
|
+
parent?: Node;
|
|
3063
|
+
};
|
|
3064
|
+
/**
|
|
3065
|
+
* Options for `walk`.
|
|
3066
|
+
*
|
|
3067
|
+
* @example
|
|
3068
|
+
* ```ts
|
|
3069
|
+
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
|
|
3070
|
+
* ```
|
|
3071
|
+
*/
|
|
3072
|
+
type WalkOptions = AsyncVisitor & {
|
|
3073
|
+
/**
|
|
3074
|
+
* Traversal depth (`'deep'` by default).
|
|
3075
|
+
* @default 'deep'
|
|
3076
|
+
*/
|
|
3077
|
+
depth?: VisitorDepth;
|
|
3078
|
+
/**
|
|
3079
|
+
* Maximum number of sibling nodes visited concurrently.
|
|
3080
|
+
* @default 30
|
|
3081
|
+
*/
|
|
3082
|
+
concurrency?: number;
|
|
3083
|
+
};
|
|
3084
|
+
/**
|
|
3085
|
+
* Options for `collect`.
|
|
3086
|
+
*
|
|
3087
|
+
* @example
|
|
3088
|
+
* ```ts
|
|
3089
|
+
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
|
|
3090
|
+
* ```
|
|
3091
|
+
*/
|
|
3092
|
+
type CollectOptions<T> = CollectVisitor<T> & {
|
|
3093
|
+
/**
|
|
3094
|
+
* Traversal depth (`'deep'` by default).
|
|
3095
|
+
* @default 'deep'
|
|
3096
|
+
*/
|
|
3097
|
+
depth?: VisitorDepth;
|
|
3098
|
+
/**
|
|
3099
|
+
* Internal parent override used during recursion.
|
|
3100
|
+
*/
|
|
3101
|
+
parent?: Node;
|
|
3102
|
+
};
|
|
3103
|
+
/**
|
|
3104
|
+
* Depth-first traversal for side effects. Visitor return values are ignored.
|
|
3105
|
+
* Sibling nodes at each level are visited concurrently up to `options.concurrency`
|
|
3106
|
+
* (default: `WALK_CONCURRENCY`).
|
|
3107
|
+
*
|
|
3108
|
+
* @example
|
|
3109
|
+
* ```ts
|
|
3110
|
+
* await walk(root, {
|
|
3111
|
+
* operation(node) {
|
|
3112
|
+
* console.log(node.operationId)
|
|
3113
|
+
* },
|
|
3114
|
+
* })
|
|
3115
|
+
* ```
|
|
3116
|
+
*
|
|
3117
|
+
* @example
|
|
3118
|
+
* ```ts
|
|
3119
|
+
* // Visit only the current node
|
|
3120
|
+
* await walk(root, { depth: 'shallow', root: () => {} })
|
|
3121
|
+
* ```
|
|
3122
|
+
*/
|
|
3123
|
+
declare function walk(node: Node, options: WalkOptions): Promise<void>;
|
|
3124
|
+
/**
|
|
3125
|
+
* Runs a depth-first immutable transform.
|
|
3126
|
+
*
|
|
3127
|
+
* If a visitor returns a node, it replaces the current node.
|
|
3128
|
+
* If it returns `undefined`, the current node stays the same.
|
|
3129
|
+
*
|
|
3130
|
+
* @example
|
|
3131
|
+
* ```ts
|
|
3132
|
+
* const next = transform(root, {
|
|
3133
|
+
* operation(node) {
|
|
3134
|
+
* return { ...node, operationId: `prefixed_${node.operationId}` }
|
|
3135
|
+
* },
|
|
3136
|
+
* })
|
|
3137
|
+
* ```
|
|
3138
|
+
*
|
|
3139
|
+
* @example
|
|
3140
|
+
* ```ts
|
|
3141
|
+
* // Shallow mode: only transform the input node
|
|
3142
|
+
* const next = transform(root, { depth: 'shallow', root: (node) => node })
|
|
3143
|
+
* ```
|
|
3144
|
+
*/
|
|
3145
|
+
declare function transform(node: InputNode, options: TransformOptions): InputNode;
|
|
3146
|
+
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
|
|
3147
|
+
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
|
|
3148
|
+
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
|
|
3149
|
+
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
|
|
3150
|
+
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
|
|
3151
|
+
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
|
|
3152
|
+
declare function transform(node: Node, options: TransformOptions): Node;
|
|
3153
|
+
/**
|
|
3154
|
+
* Runs a depth-first synchronous collection pass.
|
|
3155
|
+
*
|
|
3156
|
+
* Non-`undefined` values returned by visitor callbacks are appended to the result.
|
|
3157
|
+
*
|
|
3158
|
+
* @example
|
|
3159
|
+
* ```ts
|
|
3160
|
+
* const ids = collect(root, {
|
|
3161
|
+
* operation(node) {
|
|
3162
|
+
* return node.operationId
|
|
3163
|
+
* },
|
|
3164
|
+
* })
|
|
3165
|
+
* ```
|
|
3166
|
+
*
|
|
3167
|
+
* @example
|
|
3168
|
+
* ```ts
|
|
3169
|
+
* // Collect from only the current node
|
|
3170
|
+
* const values = collect(root, { depth: 'shallow', root: () => 'root' })
|
|
3171
|
+
* ```
|
|
3172
|
+
*/
|
|
3173
|
+
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
33
3174
|
//#endregion
|
|
34
3175
|
//#region src/utils.d.ts
|
|
35
3176
|
/**
|
|
36
|
-
*
|
|
3177
|
+
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
|
|
3178
|
+
*
|
|
3179
|
+
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node
|
|
3180
|
+
* override the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
|
|
3181
|
+
*
|
|
3182
|
+
* @example
|
|
3183
|
+
* ```ts
|
|
3184
|
+
* // Ref with description override
|
|
3185
|
+
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
|
|
3186
|
+
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
|
|
3187
|
+
* ```
|
|
3188
|
+
*/
|
|
3189
|
+
declare function syncSchemaRef(node: SchemaNode): SchemaNode;
|
|
3190
|
+
/**
|
|
3191
|
+
* Type guard that returns `true` when a schema emits as a plain `string` type.
|
|
3192
|
+
*
|
|
3193
|
+
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
|
|
3194
|
+
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
|
|
3195
|
+
*/
|
|
3196
|
+
declare function isStringType(node: SchemaNode): boolean;
|
|
3197
|
+
/**
|
|
3198
|
+
* Applies casing rules to parameter names and returns a new parameter array.
|
|
3199
|
+
*
|
|
3200
|
+
* Use this before passing parameters to schema builders so output property keys match
|
|
3201
|
+
* the desired casing while preserving `OperationNode.parameters` for other consumers.
|
|
3202
|
+
* The input array is not mutated. When `casing` is not set, the original array is returned unchanged.
|
|
3203
|
+
*/
|
|
3204
|
+
declare function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
|
|
3205
|
+
/**
|
|
3206
|
+
* Creates a single-property object schema used as a discriminator literal.
|
|
3207
|
+
*
|
|
3208
|
+
* @example
|
|
3209
|
+
* ```ts
|
|
3210
|
+
* createDiscriminantNode({ propertyName: 'type', value: 'dog' })
|
|
3211
|
+
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
|
|
3212
|
+
* ```
|
|
3213
|
+
*/
|
|
3214
|
+
declare function createDiscriminantNode({
|
|
3215
|
+
propertyName,
|
|
3216
|
+
value
|
|
3217
|
+
}: {
|
|
3218
|
+
propertyName: string;
|
|
3219
|
+
value: string;
|
|
3220
|
+
}): SchemaNode;
|
|
3221
|
+
/**
|
|
3222
|
+
* Resolver interface for {@link createOperationParams}.
|
|
3223
|
+
*
|
|
3224
|
+
* `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.
|
|
3225
|
+
*/
|
|
3226
|
+
type OperationParamsResolver = {
|
|
3227
|
+
/**
|
|
3228
|
+
* Resolves the type name for an individual parameter.
|
|
3229
|
+
*
|
|
3230
|
+
* @example Individual path parameter name
|
|
3231
|
+
* `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
|
|
3232
|
+
*/
|
|
3233
|
+
resolveParamName(node: OperationNode, param: ParameterNode): string;
|
|
3234
|
+
/**
|
|
3235
|
+
* Resolves the request body type name.
|
|
3236
|
+
*
|
|
3237
|
+
* @example Request body type name
|
|
3238
|
+
* `resolver.resolveDataName(node) // → 'CreatePetData'`
|
|
3239
|
+
*/
|
|
3240
|
+
resolveDataName(node: OperationNode): string;
|
|
3241
|
+
/**
|
|
3242
|
+
* Resolves the grouped path parameters type name.
|
|
3243
|
+
* When the return value equals `resolveParamName`, no indexed access is emitted.
|
|
3244
|
+
*
|
|
3245
|
+
* @example Grouped path params type name
|
|
3246
|
+
* `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
|
|
3247
|
+
*/
|
|
3248
|
+
resolvePathParamsName(node: OperationNode, param: ParameterNode): string;
|
|
3249
|
+
/**
|
|
3250
|
+
* Resolves the grouped query parameters type name.
|
|
3251
|
+
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
|
|
3252
|
+
*
|
|
3253
|
+
* @example Grouped query params type name
|
|
3254
|
+
* `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
|
|
3255
|
+
*/
|
|
3256
|
+
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string;
|
|
3257
|
+
/**
|
|
3258
|
+
* Resolves the grouped header parameters type name.
|
|
3259
|
+
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
|
|
3260
|
+
*
|
|
3261
|
+
* @example Grouped header params type name
|
|
3262
|
+
* `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
|
|
3263
|
+
*/
|
|
3264
|
+
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string;
|
|
3265
|
+
};
|
|
3266
|
+
/**
|
|
3267
|
+
* Options for {@link createOperationParams}.
|
|
3268
|
+
*/
|
|
3269
|
+
type CreateOperationParamsOptions = {
|
|
3270
|
+
/**
|
|
3271
|
+
* How all operation parameters are grouped in the function signature.
|
|
3272
|
+
* - `'object'` wraps all params into a single destructured object `{ petId, data, params }`
|
|
3273
|
+
* - `'inline'` emits each param category as a separate top-level parameter
|
|
3274
|
+
*/
|
|
3275
|
+
paramsType: 'object' | 'inline';
|
|
3276
|
+
/**
|
|
3277
|
+
* How path parameters are emitted when `paramsType` is `'inline'`.
|
|
3278
|
+
* - `'object'` groups them as `{ petId, storeId }: PathParams`
|
|
3279
|
+
* - `'inline'` spreads them as individual parameters `petId: string, storeId: string`
|
|
3280
|
+
* - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`
|
|
3281
|
+
*/
|
|
3282
|
+
pathParamsType: 'object' | 'inline' | 'inlineSpread';
|
|
3283
|
+
/**
|
|
3284
|
+
* Converts parameter names to camelCase before output.
|
|
3285
|
+
*/
|
|
3286
|
+
paramsCasing?: 'camelcase';
|
|
3287
|
+
/**
|
|
3288
|
+
* Resolver for parameter and request body type names.
|
|
3289
|
+
* Pass `ResolverTs` from `@kubb/plugin-ts` directly.
|
|
3290
|
+
* When omitted, falls back to the schema primitive or `'unknown'`.
|
|
3291
|
+
*/
|
|
3292
|
+
resolver?: OperationParamsResolver;
|
|
3293
|
+
/**
|
|
3294
|
+
* Default value for the path parameters binding when `pathParamsType` is `'object'`.
|
|
3295
|
+
* Falls back to `'{}'` when all path params are optional.
|
|
3296
|
+
*/
|
|
3297
|
+
pathParamsDefault?: string;
|
|
3298
|
+
/**
|
|
3299
|
+
* Extra parameters appended after the standard operation parameters.
|
|
3300
|
+
*
|
|
3301
|
+
* @example Plugin-specific trailing parameter
|
|
3302
|
+
* ```ts
|
|
3303
|
+
* extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]
|
|
3304
|
+
* ```
|
|
3305
|
+
*/
|
|
3306
|
+
extraParams?: Array<FunctionParameterNode | ParameterGroupNode>;
|
|
3307
|
+
/**
|
|
3308
|
+
* Override the default parameter names used for body, query, header, and rest-path groups.
|
|
3309
|
+
*
|
|
3310
|
+
* Useful when targeting languages or frameworks with different naming conventions.
|
|
3311
|
+
*
|
|
3312
|
+
* @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }
|
|
3313
|
+
*/
|
|
3314
|
+
paramNames?: {
|
|
3315
|
+
/**
|
|
3316
|
+
* Name for the request body parameter.
|
|
3317
|
+
* @default 'data'
|
|
3318
|
+
*/
|
|
3319
|
+
data?: string;
|
|
3320
|
+
/**
|
|
3321
|
+
* Name for the query parameters group parameter.
|
|
3322
|
+
* @default 'params'
|
|
3323
|
+
*/
|
|
3324
|
+
params?: string;
|
|
3325
|
+
/**
|
|
3326
|
+
* Name for the header parameters group parameter.
|
|
3327
|
+
* @default 'headers'
|
|
3328
|
+
*/
|
|
3329
|
+
headers?: string;
|
|
3330
|
+
/**
|
|
3331
|
+
* Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.
|
|
3332
|
+
* @default 'pathParams'
|
|
3333
|
+
*/
|
|
3334
|
+
path?: string;
|
|
3335
|
+
};
|
|
3336
|
+
/**
|
|
3337
|
+
* Applies a uniform transformation to every resolved type name before it is used
|
|
3338
|
+
* in a parameter node. Use this for framework-level type wrappers.
|
|
3339
|
+
*
|
|
3340
|
+
* @example Vue Query — wrap every parameter type with `MaybeRefOrGetter`
|
|
3341
|
+
* `typeWrapper: (t) => \`MaybeRefOrGetter<${t}>\``
|
|
3342
|
+
*/
|
|
3343
|
+
typeWrapper?: (type: string) => string;
|
|
3344
|
+
};
|
|
3345
|
+
/**
|
|
3346
|
+
* Converts an `OperationNode` into function parameters for code generation.
|
|
3347
|
+
*
|
|
3348
|
+
* Centralizes parameter grouping logic for all plugins. Provide a `resolver` for type name resolution
|
|
3349
|
+
* and `extraParams` for plugin-specific trailing parameters (e.g., `options` objects).
|
|
3350
|
+
* Supports three grouping modes: `object` (single destructured param), `inline` (separate params),
|
|
3351
|
+
* and `inlineSpread` (rest parameter). Use `CreateOperationParamsOptions` to fine-tune output.
|
|
3352
|
+
*/
|
|
3353
|
+
declare function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode;
|
|
3354
|
+
/**
|
|
3355
|
+
* Extracts all string content from a `CodeNode` tree recursively.
|
|
3356
|
+
*
|
|
3357
|
+
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
3358
|
+
* and nested node content. Used internally to build the full source string for import filtering.
|
|
3359
|
+
*/
|
|
3360
|
+
declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string;
|
|
3361
|
+
/**
|
|
3362
|
+
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
|
|
3363
|
+
*
|
|
3364
|
+
* Returns `undefined` for non-ref nodes or when no name can be resolved. Use this to get a schema's
|
|
3365
|
+
* identifier for type definitions or error messages.
|
|
3366
|
+
*
|
|
3367
|
+
* @example
|
|
3368
|
+
* ```ts
|
|
3369
|
+
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
|
|
3370
|
+
* // => 'Pet'
|
|
3371
|
+
* ```
|
|
3372
|
+
*/
|
|
3373
|
+
declare function resolveRefName(node: SchemaNode | undefined): string | undefined;
|
|
3374
|
+
/**
|
|
3375
|
+
* Collects every named schema referenced (transitively) from a node via ref edges.
|
|
3376
|
+
*
|
|
3377
|
+
* Refs are followed by name only — the resolved `node.schema` is not traversed inline.
|
|
3378
|
+
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
|
|
3379
|
+
*
|
|
3380
|
+
* @note Returns a Set of schema names for efficient membership testing.
|
|
3381
|
+
*/
|
|
3382
|
+
declare function collectReferencedSchemaNames(node: SchemaNode | undefined, out?: Set<string>): Set<string>;
|
|
3383
|
+
/**
|
|
3384
|
+
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
3385
|
+
*
|
|
3386
|
+
* Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
|
|
3387
|
+
* in deferred constructs (lazy getter, `z.lazy(() => …)`) to prevent infinite recursion when generated code runs.
|
|
3388
|
+
* Refs are followed by name only, keeping the algorithm linear in the schema graph size.
|
|
37
3389
|
*
|
|
38
|
-
*
|
|
39
|
-
* - `date` and `time` are plain strings when their `representation` is `'string'` rather than `'date'`.
|
|
3390
|
+
* @note Call this once on the full schema graph, then use `containsCircularRef()` to check individual schemas.
|
|
40
3391
|
*/
|
|
41
|
-
declare function
|
|
3392
|
+
declare function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string>;
|
|
42
3393
|
/**
|
|
43
|
-
*
|
|
3394
|
+
* Type guard returning `true` when a schema or anything nested within it contains a ref to a circular schema.
|
|
44
3395
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
3396
|
+
* Use `excludeName` to ignore refs to specific schemas (useful when self-references are handled separately).
|
|
3397
|
+
* Commonly used with `findCircularSchemas()` to detect where lazy wrappers are needed in code generation.
|
|
47
3398
|
*
|
|
48
|
-
*
|
|
49
|
-
* in the generated output match the desired casing while the original
|
|
50
|
-
* `OperationNode.parameters` array remains untouched for other consumers.
|
|
3399
|
+
* @note Returns `true` for the first matching circular ref found; use for fast dependency checks.
|
|
51
3400
|
*/
|
|
52
|
-
declare function
|
|
3401
|
+
declare function containsCircularRef(node: SchemaNode | undefined, {
|
|
3402
|
+
circularSchemas,
|
|
3403
|
+
excludeName
|
|
3404
|
+
}: {
|
|
3405
|
+
circularSchemas: ReadonlySet<string>;
|
|
3406
|
+
excludeName?: string;
|
|
3407
|
+
}): boolean;
|
|
53
3408
|
//#endregion
|
|
54
|
-
export {
|
|
3409
|
+
export { type ArraySchemaNode, type ArrowFunctionNode, AsyncVisitor, type BaseNode, type BreakNode, type CodeNode, CollectOptions, CollectVisitor, type ComplexSchemaType, type ConstNode, type DateSchemaNode, type DatetimeSchemaNode, DistributiveOmit, type EnumSchemaNode, type EnumValueNode, type ExportNode, type FileNode, type FormatStringSchemaNode, type FunctionNode, type FunctionNodeType, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type HttpMethod, type HttpStatusCode, type ImportNode, InferSchema, InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type Ipv4SchemaNode, type Ipv6SchemaNode, type JSDocNode, type JsxNode, type MediaType, Node, type NodeKind, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, OperationParamsResolver, type OutputNode, type ParameterGroupNode, type ParameterLocation, type ParameterNode, type ParamsTypeNode, ParentOf, ParserOptions, type PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, type PropertyNode, RefMap, type RefSchemaNode, type ResponseNode, ScalarPrimitive, type ScalarSchemaNode, type ScalarSchemaType, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type SpecialSchemaType, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, TransformOptions, type TypeDeclarationNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, UserFileNode, Visitor, VisitorContext, VisitorDepth, WalkOptions, caseParams, childName, collect, collectImports, collectReferencedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
55
3410
|
//# sourceMappingURL=index.d.ts.map
|