@kubb/ast 5.0.0-beta.9 → 5.0.0-beta.93
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/LICENSE +17 -10
- package/README.md +56 -27
- package/dist/index.cjs +1333 -1652
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +273 -3335
- package/dist/index.js +1279 -1612
- package/dist/index.js.map +1 -1
- package/dist/rolldown-runtime-CNktS9qV.js +17 -0
- package/dist/types-Dno_xT4x.d.ts +2739 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/package.json +8 -7
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/constants.ts +0 -228
- package/src/factory.ts +0 -742
- package/src/guards.ts +0 -110
- package/src/index.ts +0 -46
- package/src/infer.ts +0 -130
- package/src/mocks.ts +0 -176
- package/src/nodes/base.ts +0 -56
- package/src/nodes/code.ts +0 -304
- package/src/nodes/file.ts +0 -230
- package/src/nodes/function.ts +0 -223
- package/src/nodes/http.ts +0 -119
- package/src/nodes/index.ts +0 -86
- package/src/nodes/operation.ts +0 -111
- package/src/nodes/output.ts +0 -26
- package/src/nodes/parameter.ts +0 -41
- package/src/nodes/property.ts +0 -34
- package/src/nodes/response.ts +0 -43
- package/src/nodes/root.ts +0 -64
- package/src/nodes/schema.ts +0 -656
- package/src/printer.ts +0 -250
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/transformers.ts +0 -159
- package/src/types.ts +0 -70
- package/src/utils.ts +0 -915
- package/src/visitor.ts +0 -592
|
@@ -0,0 +1,2739 @@
|
|
|
1
|
+
import { n as __name } from "./rolldown-runtime-CNktS9qV.js";
|
|
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
|
+
/**
|
|
12
|
+
* Schema type discriminators used by all AST schema nodes.
|
|
13
|
+
*
|
|
14
|
+
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
|
|
15
|
+
*/
|
|
16
|
+
declare const schemaTypes: {
|
|
17
|
+
/**
|
|
18
|
+
* Text value.
|
|
19
|
+
*/
|
|
20
|
+
readonly string: "string";
|
|
21
|
+
/**
|
|
22
|
+
* Floating-point number (`float`, `double`).
|
|
23
|
+
*/
|
|
24
|
+
readonly number: "number";
|
|
25
|
+
/**
|
|
26
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
27
|
+
*/
|
|
28
|
+
readonly integer: "integer";
|
|
29
|
+
/**
|
|
30
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
31
|
+
*/
|
|
32
|
+
readonly bigint: "bigint";
|
|
33
|
+
/**
|
|
34
|
+
* Boolean value.
|
|
35
|
+
*/
|
|
36
|
+
readonly boolean: "boolean";
|
|
37
|
+
/**
|
|
38
|
+
* Explicit null value.
|
|
39
|
+
*/
|
|
40
|
+
readonly null: "null";
|
|
41
|
+
/**
|
|
42
|
+
* Any value (no type restriction).
|
|
43
|
+
*/
|
|
44
|
+
readonly any: "any";
|
|
45
|
+
/**
|
|
46
|
+
* Unknown value (must be narrowed before usage).
|
|
47
|
+
*/
|
|
48
|
+
readonly unknown: "unknown";
|
|
49
|
+
/**
|
|
50
|
+
* No return value (`void`).
|
|
51
|
+
*/
|
|
52
|
+
readonly void: "void";
|
|
53
|
+
/**
|
|
54
|
+
* Object with named properties.
|
|
55
|
+
*/
|
|
56
|
+
readonly object: "object";
|
|
57
|
+
/**
|
|
58
|
+
* Sequential list of items.
|
|
59
|
+
*/
|
|
60
|
+
readonly array: "array";
|
|
61
|
+
/**
|
|
62
|
+
* Fixed-length list with position-specific items.
|
|
63
|
+
*/
|
|
64
|
+
readonly tuple: "tuple";
|
|
65
|
+
/**
|
|
66
|
+
* "One of" multiple schema members.
|
|
67
|
+
*/
|
|
68
|
+
readonly union: "union";
|
|
69
|
+
/**
|
|
70
|
+
* "All of" multiple schema members.
|
|
71
|
+
*/
|
|
72
|
+
readonly intersection: "intersection";
|
|
73
|
+
/**
|
|
74
|
+
* Enum schema.
|
|
75
|
+
*/
|
|
76
|
+
readonly enum: "enum";
|
|
77
|
+
/**
|
|
78
|
+
* Reference to another schema.
|
|
79
|
+
*/
|
|
80
|
+
readonly ref: "ref";
|
|
81
|
+
/**
|
|
82
|
+
* Calendar date (for example `2026-03-24`).
|
|
83
|
+
*/
|
|
84
|
+
readonly date: "date";
|
|
85
|
+
/**
|
|
86
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
87
|
+
*/
|
|
88
|
+
readonly datetime: "datetime";
|
|
89
|
+
/**
|
|
90
|
+
* Time-only value (for example `09:00:00`).
|
|
91
|
+
*/
|
|
92
|
+
readonly time: "time";
|
|
93
|
+
/**
|
|
94
|
+
* UUID value.
|
|
95
|
+
*/
|
|
96
|
+
readonly uuid: "uuid";
|
|
97
|
+
/**
|
|
98
|
+
* Email address value.
|
|
99
|
+
*/
|
|
100
|
+
readonly email: "email";
|
|
101
|
+
/**
|
|
102
|
+
* URL value.
|
|
103
|
+
*/
|
|
104
|
+
readonly url: "url";
|
|
105
|
+
/**
|
|
106
|
+
* IPv4 address value.
|
|
107
|
+
*/
|
|
108
|
+
readonly ipv4: "ipv4";
|
|
109
|
+
/**
|
|
110
|
+
* IPv6 address value.
|
|
111
|
+
*/
|
|
112
|
+
readonly ipv6: "ipv6";
|
|
113
|
+
/**
|
|
114
|
+
* Binary/blob value.
|
|
115
|
+
*/
|
|
116
|
+
readonly blob: "blob";
|
|
117
|
+
/**
|
|
118
|
+
* Impossible value (`never`).
|
|
119
|
+
*/
|
|
120
|
+
readonly never: "never";
|
|
121
|
+
};
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/nodes/base.d.ts
|
|
124
|
+
/**
|
|
125
|
+
* `kind` values used by AST nodes.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* const kind: NodeKind = 'Schema'
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
type NodeKind = 'Input' | 'Output' | 'Operation' | 'Schema' | 'Property' | 'Parameter' | 'Response' | 'RequestBody' | 'Content' | 'Type' | 'File' | 'Import' | 'Export' | 'Source' | 'Const' | 'Function' | 'ArrowFunction' | 'Text' | 'Break' | 'Jsx';
|
|
133
|
+
/**
|
|
134
|
+
* Base shape shared by all AST nodes.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* const base: BaseNode = { kind: 'Input' }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
type BaseNode = {
|
|
142
|
+
/**
|
|
143
|
+
* Node discriminator.
|
|
144
|
+
*/
|
|
145
|
+
kind: NodeKind;
|
|
146
|
+
};
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/defineNode.d.ts
|
|
149
|
+
/**
|
|
150
|
+
* Visitor callback names, one per traversable node kind, in traversal order.
|
|
151
|
+
* Kept in sync with the keys of `Visitor` in `visitor.ts`.
|
|
152
|
+
*/
|
|
153
|
+
declare const visitorKeys: readonly ["input", "output", "operation", "schema", "property", "parameter", "response"];
|
|
154
|
+
/**
|
|
155
|
+
* One of the {@link visitorKeys} callback names.
|
|
156
|
+
*/
|
|
157
|
+
type VisitorKey = (typeof visitorKeys)[number];
|
|
158
|
+
/**
|
|
159
|
+
* Distributive `Omit` that preserves each member of a union.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* type A = { kind: 'a'; keep: string; drop: number }
|
|
164
|
+
* type B = { kind: 'b'; keep: boolean; drop: number }
|
|
165
|
+
* type Result = DistributiveOmit<A | B, 'drop'>
|
|
166
|
+
* // -> { kind: 'a'; keep: string } | { kind: 'b'; keep: boolean }
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
|
170
|
+
/**
|
|
171
|
+
* The single definition derived from one {@link defineNode} call: the node's
|
|
172
|
+
* `create` builder, its `is` guard, and the traversal metadata the registry
|
|
173
|
+
* collects into the visitor tables.
|
|
174
|
+
*/
|
|
175
|
+
type NodeDef<TNode extends BaseNode = BaseNode, TInput = never> = {
|
|
176
|
+
/**
|
|
177
|
+
* Node discriminator this definition owns.
|
|
178
|
+
*/
|
|
179
|
+
kind: NodeKind;
|
|
180
|
+
/**
|
|
181
|
+
* Builds a node from its input, applying `defaults` and the optional `build` hook.
|
|
182
|
+
*/
|
|
183
|
+
create: (input: TInput) => TNode;
|
|
184
|
+
/**
|
|
185
|
+
* Type guard matching this node kind.
|
|
186
|
+
*/
|
|
187
|
+
is: (node: unknown) => node is TNode;
|
|
188
|
+
/**
|
|
189
|
+
* Child node fields in traversal order. Feeds `VISITOR_KEYS`.
|
|
190
|
+
*/
|
|
191
|
+
children?: ReadonlyArray<string>;
|
|
192
|
+
/**
|
|
193
|
+
* Visitor callback name. Feeds `VISITOR_KEY_BY_KIND`.
|
|
194
|
+
*/
|
|
195
|
+
visitorKey?: VisitorKey;
|
|
196
|
+
};
|
|
197
|
+
type DefineNodeConfig<TNode extends BaseNode, TInput, TBuilt extends object> = {
|
|
198
|
+
kind: TNode['kind'];
|
|
199
|
+
defaults?: Partial<TNode>;
|
|
200
|
+
build?: (input: TInput) => TBuilt;
|
|
201
|
+
children?: ReadonlyArray<string>;
|
|
202
|
+
visitorKey?: VisitorKey;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Defines a node once and derives its `create` builder, `is` guard, and traversal
|
|
206
|
+
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
|
|
207
|
+
* `kind`, so node construction lives in one place without scattered `as` casts.
|
|
208
|
+
*
|
|
209
|
+
* @example Simple node
|
|
210
|
+
* ```ts
|
|
211
|
+
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
|
|
212
|
+
* const createImport = importDef.create
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* @example Node with a build hook
|
|
216
|
+
* ```ts
|
|
217
|
+
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
|
|
218
|
+
* kind: 'Property',
|
|
219
|
+
* build: (props) => ({ ...props, required: props.required ?? false }),
|
|
220
|
+
* children: ['schema'],
|
|
221
|
+
* visitorKey: 'property',
|
|
222
|
+
* })
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
declare function defineNode<TNode extends BaseNode, TInput = Omit<TNode, 'kind'>, TBuilt extends object = Omit<TNode, 'kind'>>(config: DefineNodeConfig<TNode, TInput, TBuilt>): NodeDef<TNode, TInput>;
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/nodes/code.d.ts
|
|
228
|
+
/**
|
|
229
|
+
* JSDoc documentation metadata attached to code declarations.
|
|
230
|
+
*/
|
|
231
|
+
type JSDocNode = {
|
|
232
|
+
/**
|
|
233
|
+
* JSDoc comment lines. `undefined` entries are filtered out during rendering.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* ['@description A pet resource', '@deprecated']
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
comments?: Array<string | undefined>;
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* AST node representing a TypeScript `const` declaration.
|
|
244
|
+
*
|
|
245
|
+
* Mirrors the props of the `Const` component from `@kubb/renderer-jsx`.
|
|
246
|
+
* The `children` prop of the component is represented as `nodes`.
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* createConst({ name: 'pet', export: true, asConst: true })
|
|
251
|
+
* // export const pet = ... as const
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
type ConstNode = BaseNode & {
|
|
255
|
+
kind: 'Const';
|
|
256
|
+
/**
|
|
257
|
+
* Name of the constant declaration.
|
|
258
|
+
*/
|
|
259
|
+
name: string;
|
|
260
|
+
/**
|
|
261
|
+
* Whether the declaration should be exported.
|
|
262
|
+
*/
|
|
263
|
+
export?: boolean | null;
|
|
264
|
+
/**
|
|
265
|
+
* Explicit type annotation.
|
|
266
|
+
*
|
|
267
|
+
* @example Type reference
|
|
268
|
+
* `'Pet'`
|
|
269
|
+
*/
|
|
270
|
+
type?: string | null;
|
|
271
|
+
/**
|
|
272
|
+
* JSDoc documentation metadata.
|
|
273
|
+
*/
|
|
274
|
+
JSDoc?: JSDocNode | null;
|
|
275
|
+
/**
|
|
276
|
+
* Whether to append `as const` to the declaration.
|
|
277
|
+
*/
|
|
278
|
+
asConst?: boolean | null;
|
|
279
|
+
/**
|
|
280
|
+
* Child nodes representing the value of the constant (children of the `Const` component).
|
|
281
|
+
* Each entry is a {@link CodeNode}. Use {@link TextNode} for raw string content.
|
|
282
|
+
*/
|
|
283
|
+
nodes?: Array<CodeNode>;
|
|
284
|
+
};
|
|
285
|
+
/**
|
|
286
|
+
* AST node representing a TypeScript `type` alias declaration.
|
|
287
|
+
*
|
|
288
|
+
* Mirrors the props of the `Type` component from `@kubb/renderer-jsx`.
|
|
289
|
+
* The `children` prop of the component is represented as `nodes`.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```ts
|
|
293
|
+
* createType({ name: 'Pet', export: true })
|
|
294
|
+
* // export type Pet = ...
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
type TypeNode = BaseNode & {
|
|
298
|
+
kind: 'Type';
|
|
299
|
+
/**
|
|
300
|
+
* Name of the type alias.
|
|
301
|
+
*/
|
|
302
|
+
name: string;
|
|
303
|
+
/**
|
|
304
|
+
* Whether the declaration should be exported.
|
|
305
|
+
*/
|
|
306
|
+
export?: boolean | null;
|
|
307
|
+
/**
|
|
308
|
+
* JSDoc documentation metadata.
|
|
309
|
+
*/
|
|
310
|
+
JSDoc?: JSDocNode | null;
|
|
311
|
+
/**
|
|
312
|
+
* Child nodes representing the type body (children of the `Type` component).
|
|
313
|
+
* Each entry is a {@link CodeNode}. Use {@link TextNode} for raw string content.
|
|
314
|
+
*/
|
|
315
|
+
nodes?: Array<CodeNode>;
|
|
316
|
+
};
|
|
317
|
+
/**
|
|
318
|
+
* AST node representing a TypeScript `function` declaration.
|
|
319
|
+
*
|
|
320
|
+
* Mirrors the props of the `Function` component from `@kubb/renderer-jsx`.
|
|
321
|
+
* The `children` prop of the component is represented as `nodes`.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* createFunctionDeclaration({ name: 'getPet', export: true, async: true, returnType: 'Pet' })
|
|
326
|
+
* // export async function getPet(): Promise<Pet> { ... }
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
type FunctionNode = BaseNode & {
|
|
330
|
+
kind: 'Function';
|
|
331
|
+
/**
|
|
332
|
+
* Name of the function.
|
|
333
|
+
*/
|
|
334
|
+
name: string;
|
|
335
|
+
/**
|
|
336
|
+
* Whether the function is a default export.
|
|
337
|
+
*/
|
|
338
|
+
default?: boolean | null;
|
|
339
|
+
/**
|
|
340
|
+
* Function parameter list as a pre-rendered string, written verbatim between the parentheses.
|
|
341
|
+
*
|
|
342
|
+
* @example
|
|
343
|
+
* `'id: string, config: Config = {}'`
|
|
344
|
+
*/
|
|
345
|
+
params?: string | null;
|
|
346
|
+
/**
|
|
347
|
+
* Whether the function should be exported.
|
|
348
|
+
*/
|
|
349
|
+
export?: boolean | null;
|
|
350
|
+
/**
|
|
351
|
+
* Whether the function is async. When `true`, the return type is wrapped in `Promise<>`.
|
|
352
|
+
*/
|
|
353
|
+
async?: boolean | null;
|
|
354
|
+
/**
|
|
355
|
+
* TypeScript generic type parameters.
|
|
356
|
+
*
|
|
357
|
+
* @example Constrained generics
|
|
358
|
+
* `['T', 'U extends string']`
|
|
359
|
+
*/
|
|
360
|
+
generics?: string | Array<string> | null;
|
|
361
|
+
/**
|
|
362
|
+
* Return type annotation.
|
|
363
|
+
*
|
|
364
|
+
* @example Type reference
|
|
365
|
+
* `'Pet'`
|
|
366
|
+
*/
|
|
367
|
+
returnType?: string | null;
|
|
368
|
+
/**
|
|
369
|
+
* JSDoc documentation metadata.
|
|
370
|
+
*/
|
|
371
|
+
JSDoc?: JSDocNode | null;
|
|
372
|
+
/**
|
|
373
|
+
* Child nodes representing the function body (children of the `Function` component).
|
|
374
|
+
* Each entry is a {@link CodeNode}. Use {@link TextNode} for raw string content.
|
|
375
|
+
*/
|
|
376
|
+
nodes?: Array<CodeNode>;
|
|
377
|
+
};
|
|
378
|
+
/**
|
|
379
|
+
* AST node representing a TypeScript arrow function (`const name = () => { ... }`).
|
|
380
|
+
*
|
|
381
|
+
* Mirrors the props of the `Function.Arrow` component from `@kubb/renderer-jsx`.
|
|
382
|
+
* The `children` prop of the component is represented as `nodes`.
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```ts
|
|
386
|
+
* createArrowFunctionDeclaration({ name: 'getPet', export: true, singleLine: true })
|
|
387
|
+
* // export const getPet = () => ...
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
type ArrowFunctionNode = Omit<FunctionNode, 'kind'> & {
|
|
391
|
+
kind: 'ArrowFunction';
|
|
392
|
+
/**
|
|
393
|
+
* Render the arrow function body as a single-line expression.
|
|
394
|
+
*/
|
|
395
|
+
singleLine?: boolean | null;
|
|
396
|
+
};
|
|
397
|
+
/**
|
|
398
|
+
* AST node representing a raw text/string fragment in the source output.
|
|
399
|
+
*
|
|
400
|
+
* Used instead of bare `string` values so that all entries in `nodes` arrays
|
|
401
|
+
* are typed `CodeNode` objects rather than a mixed `CodeNode | string` union.
|
|
402
|
+
*
|
|
403
|
+
* @example
|
|
404
|
+
* ```ts
|
|
405
|
+
* createText('return fetch(id)')
|
|
406
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
407
|
+
* ```
|
|
408
|
+
*/
|
|
409
|
+
type TextNode = BaseNode & {
|
|
410
|
+
kind: 'Text';
|
|
411
|
+
/**
|
|
412
|
+
* The raw string content.
|
|
413
|
+
*/
|
|
414
|
+
value: string;
|
|
415
|
+
};
|
|
416
|
+
/**
|
|
417
|
+
* AST node representing a blank line in the source output.
|
|
418
|
+
*
|
|
419
|
+
* Corresponds to `<br/>` in JSX components. `printNodes` turns a `Break` between two
|
|
420
|
+
* statements into one blank line. Consecutive breaks, and breaks at the start or end of
|
|
421
|
+
* the list, are folded away, so a `Break` never produces more than one blank line.
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* createBreak()
|
|
426
|
+
* // { kind: 'Break' }
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
type BreakNode = BaseNode & {
|
|
430
|
+
kind: 'Break';
|
|
431
|
+
};
|
|
432
|
+
/**
|
|
433
|
+
* AST node representing a raw JSX fragment in the source output.
|
|
434
|
+
*
|
|
435
|
+
* Mirrors the `Jsx` component from `@kubb/renderer-jsx`. Embeds raw JSX/TSX markup
|
|
436
|
+
* (including fragments `<>…</>`) directly in generated code.
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* ```ts
|
|
440
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
441
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
type JsxNode = BaseNode & {
|
|
445
|
+
kind: 'Jsx';
|
|
446
|
+
/**
|
|
447
|
+
* The raw JSX string content.
|
|
448
|
+
*/
|
|
449
|
+
value: string;
|
|
450
|
+
};
|
|
451
|
+
/**
|
|
452
|
+
* Union of all code-generation AST nodes.
|
|
453
|
+
*
|
|
454
|
+
* These nodes mirror the JSX components from `@kubb/renderer-jsx` and are used as
|
|
455
|
+
* structured children in {@link SourceNode.nodes}.
|
|
456
|
+
*/
|
|
457
|
+
type CodeNode = ConstNode | TypeNode | FunctionNode | ArrowFunctionNode | TextNode | BreakNode | JsxNode;
|
|
458
|
+
/**
|
|
459
|
+
* Definition for the {@link ConstNode}.
|
|
460
|
+
*/
|
|
461
|
+
declare const constDef: NodeDef<ConstNode, Omit<ConstNode, "kind">>;
|
|
462
|
+
/**
|
|
463
|
+
* Definition for the {@link TypeNode}.
|
|
464
|
+
*/
|
|
465
|
+
declare const typeDef: NodeDef<TypeNode, Omit<TypeNode, "kind">>;
|
|
466
|
+
/**
|
|
467
|
+
* Definition for the {@link FunctionNode}.
|
|
468
|
+
*/
|
|
469
|
+
declare const functionDef: NodeDef<FunctionNode, Omit<FunctionNode, "kind">>;
|
|
470
|
+
/**
|
|
471
|
+
* Definition for the {@link ArrowFunctionNode}.
|
|
472
|
+
*/
|
|
473
|
+
declare const arrowFunctionDef: NodeDef<ArrowFunctionNode, Omit<ArrowFunctionNode, "kind">>;
|
|
474
|
+
/**
|
|
475
|
+
* Definition for the {@link TextNode}.
|
|
476
|
+
*/
|
|
477
|
+
declare const textDef: NodeDef<TextNode, string>;
|
|
478
|
+
/**
|
|
479
|
+
* Definition for the {@link BreakNode}.
|
|
480
|
+
*/
|
|
481
|
+
declare const breakDef: NodeDef<BreakNode, void>;
|
|
482
|
+
/**
|
|
483
|
+
* Definition for the {@link JsxNode}.
|
|
484
|
+
*/
|
|
485
|
+
declare const jsxDef: NodeDef<JsxNode, string>;
|
|
486
|
+
/**
|
|
487
|
+
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
488
|
+
*
|
|
489
|
+
* @example Exported constant with type and `as const`
|
|
490
|
+
* ```ts
|
|
491
|
+
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
492
|
+
* // export const pets: Pet[] = ... as const
|
|
493
|
+
* ```
|
|
494
|
+
*/
|
|
495
|
+
declare const createConst: (input: Omit<ConstNode, "kind">) => ConstNode;
|
|
496
|
+
/**
|
|
497
|
+
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* ```ts
|
|
501
|
+
* createType({ name: 'Pet', export: true })
|
|
502
|
+
* // export type Pet = ...
|
|
503
|
+
* ```
|
|
504
|
+
*/
|
|
505
|
+
declare const createType: (input: Omit<TypeNode, "kind">) => TypeNode;
|
|
506
|
+
/**
|
|
507
|
+
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* ```ts
|
|
511
|
+
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
512
|
+
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
513
|
+
* ```
|
|
514
|
+
*/
|
|
515
|
+
declare const createFunction: (input: Omit<FunctionNode, "kind">) => FunctionNode;
|
|
516
|
+
/**
|
|
517
|
+
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
520
|
+
* ```ts
|
|
521
|
+
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
522
|
+
* // export const double = (n: number) => ...
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
525
|
+
declare const createArrowFunction: (input: Omit<ArrowFunctionNode, "kind">) => ArrowFunctionNode;
|
|
526
|
+
/**
|
|
527
|
+
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* ```ts
|
|
531
|
+
* createText('return fetch(id)')
|
|
532
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
535
|
+
declare const createText: (input: string) => TextNode;
|
|
536
|
+
/**
|
|
537
|
+
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
538
|
+
*
|
|
539
|
+
* @example
|
|
540
|
+
* ```ts
|
|
541
|
+
* createBreak()
|
|
542
|
+
* // { kind: 'Break' }
|
|
543
|
+
* ```
|
|
544
|
+
*/
|
|
545
|
+
declare function createBreak(): BreakNode;
|
|
546
|
+
/**
|
|
547
|
+
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* ```ts
|
|
551
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
552
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
553
|
+
* ```
|
|
554
|
+
*/
|
|
555
|
+
declare const createJsx: (input: string) => JsxNode;
|
|
556
|
+
//#endregion
|
|
557
|
+
//#region src/infer.d.ts
|
|
558
|
+
/**
|
|
559
|
+
* Options that control how the adapter parser maps source schemas to AST nodes.
|
|
560
|
+
*/
|
|
561
|
+
type ParserOptions = {
|
|
562
|
+
/**
|
|
563
|
+
* How `format: 'date-time'` schemas are represented downstream.
|
|
564
|
+
* - `false` falls through to a plain `string` (no validation).
|
|
565
|
+
* - `'string'` emits a datetime string node.
|
|
566
|
+
* - `'stringOffset'` emits a datetime node with timezone offset.
|
|
567
|
+
* - `'stringLocal'` emits a local datetime node.
|
|
568
|
+
* - `'date'` emits a `date` node (JavaScript `Date` object).
|
|
569
|
+
*/
|
|
570
|
+
dateType: false | 'string' | 'stringOffset' | 'stringLocal' | 'date';
|
|
571
|
+
/**
|
|
572
|
+
* How `type: 'integer'` (and `format: 'int64'`) maps to TypeScript.
|
|
573
|
+
* - `'bigint'` is exact for 64-bit IDs, but does not round-trip through JSON.
|
|
574
|
+
* - `'number'` fits most JSON APIs. Loses precision above `Number.MAX_SAFE_INTEGER`.
|
|
575
|
+
*
|
|
576
|
+
* @default 'bigint'
|
|
577
|
+
*/
|
|
578
|
+
integerType?: 'number' | 'bigint';
|
|
579
|
+
/**
|
|
580
|
+
* AST type used when a schema's type cannot be inferred from the spec
|
|
581
|
+
* (`additionalProperties: true`, missing `type`, ...).
|
|
582
|
+
*/
|
|
583
|
+
unknownType: 'any' | 'unknown' | 'void';
|
|
584
|
+
/**
|
|
585
|
+
* AST type used for completely empty schemas (`{}`).
|
|
586
|
+
*/
|
|
587
|
+
emptySchemaType: 'any' | 'unknown' | 'void';
|
|
588
|
+
/**
|
|
589
|
+
* Suffix appended to derived enum names when Kubb has to invent one
|
|
590
|
+
* (typically for inline enums on object properties).
|
|
591
|
+
*/
|
|
592
|
+
enumSuffix: 'enum' | (string & {});
|
|
593
|
+
};
|
|
594
|
+
/**
|
|
595
|
+
* Maps each `dateType` option value to the AST node produced by `format: 'date-time'`.
|
|
596
|
+
*/
|
|
597
|
+
type DateTimeNodeByDateType = {
|
|
598
|
+
date: DateSchemaNode;
|
|
599
|
+
string: DatetimeSchemaNode;
|
|
600
|
+
stringOffset: DatetimeSchemaNode;
|
|
601
|
+
stringLocal: DatetimeSchemaNode;
|
|
602
|
+
false: StringSchemaNode;
|
|
603
|
+
};
|
|
604
|
+
/**
|
|
605
|
+
* Resolves the AST node produced by `format: 'date-time'` based on the `dateType` option.
|
|
606
|
+
*/
|
|
607
|
+
type ResolveDateTimeNode<TDateType extends ParserOptions['dateType']> = DateTimeNodeByDateType[TDateType extends keyof DateTimeNodeByDateType ? TDateType : 'string'];
|
|
608
|
+
/**
|
|
609
|
+
* Ordered list of `[schema-shape, SchemaNode]` pairs.
|
|
610
|
+
* `InferSchemaNode` walks this tuple in order and returns the first matching node type.
|
|
611
|
+
*/
|
|
612
|
+
type SchemaNodeMap<TDateType extends ParserOptions['dateType'] = 'string'> = [[{
|
|
613
|
+
$ref: string;
|
|
614
|
+
}, RefSchemaNode], [{
|
|
615
|
+
allOf: ReadonlyArray<unknown>;
|
|
616
|
+
properties: object;
|
|
617
|
+
}, IntersectionSchemaNode], [{
|
|
618
|
+
allOf: readonly [unknown, unknown, ...Array<unknown>];
|
|
619
|
+
}, IntersectionSchemaNode], [{
|
|
620
|
+
allOf: ReadonlyArray<unknown>;
|
|
621
|
+
}, SchemaNode], [{
|
|
622
|
+
oneOf: ReadonlyArray<unknown>;
|
|
623
|
+
}, UnionSchemaNode], [{
|
|
624
|
+
anyOf: ReadonlyArray<unknown>;
|
|
625
|
+
}, UnionSchemaNode], [{
|
|
626
|
+
const: null;
|
|
627
|
+
}, ScalarSchemaNode], [{
|
|
628
|
+
const: string | number | boolean;
|
|
629
|
+
}, EnumSchemaNode], [{
|
|
630
|
+
type: ReadonlyArray<string>;
|
|
631
|
+
}, UnionSchemaNode], [{
|
|
632
|
+
type: 'array';
|
|
633
|
+
enum: ReadonlyArray<unknown>;
|
|
634
|
+
}, ArraySchemaNode], [{
|
|
635
|
+
enum: ReadonlyArray<unknown>;
|
|
636
|
+
}, EnumSchemaNode], [{
|
|
637
|
+
type: 'enum';
|
|
638
|
+
}, EnumSchemaNode], [{
|
|
639
|
+
type: 'union';
|
|
640
|
+
}, UnionSchemaNode], [{
|
|
641
|
+
type: 'intersection';
|
|
642
|
+
}, IntersectionSchemaNode], [{
|
|
643
|
+
type: 'tuple';
|
|
644
|
+
}, ArraySchemaNode], [{
|
|
645
|
+
type: 'ref';
|
|
646
|
+
}, RefSchemaNode], [{
|
|
647
|
+
type: 'datetime';
|
|
648
|
+
}, DatetimeSchemaNode], [{
|
|
649
|
+
type: 'date';
|
|
650
|
+
}, DateSchemaNode], [{
|
|
651
|
+
type: 'time';
|
|
652
|
+
}, TimeSchemaNode], [{
|
|
653
|
+
type: 'url';
|
|
654
|
+
}, UrlSchemaNode], [{
|
|
655
|
+
type: 'object';
|
|
656
|
+
}, ObjectSchemaNode], [{
|
|
657
|
+
additionalProperties: boolean | {};
|
|
658
|
+
}, ObjectSchemaNode], [{
|
|
659
|
+
type: 'array';
|
|
660
|
+
}, ArraySchemaNode], [{
|
|
661
|
+
items: object;
|
|
662
|
+
}, ArraySchemaNode], [{
|
|
663
|
+
prefixItems: ReadonlyArray<unknown>;
|
|
664
|
+
}, ArraySchemaNode], [{
|
|
665
|
+
type: string;
|
|
666
|
+
format: 'date-time';
|
|
667
|
+
}, ResolveDateTimeNode<TDateType>], [{
|
|
668
|
+
type: string;
|
|
669
|
+
format: 'date';
|
|
670
|
+
}, DateSchemaNode], [{
|
|
671
|
+
type: string;
|
|
672
|
+
format: 'time';
|
|
673
|
+
}, TimeSchemaNode], [{
|
|
674
|
+
format: 'date-time';
|
|
675
|
+
}, ResolveDateTimeNode<TDateType>], [{
|
|
676
|
+
format: 'date';
|
|
677
|
+
}, DateSchemaNode], [{
|
|
678
|
+
format: 'time';
|
|
679
|
+
}, TimeSchemaNode], [{
|
|
680
|
+
type: 'string';
|
|
681
|
+
}, StringSchemaNode], [{
|
|
682
|
+
type: 'number';
|
|
683
|
+
}, NumberSchemaNode], [{
|
|
684
|
+
type: 'integer';
|
|
685
|
+
}, NumberSchemaNode], [{
|
|
686
|
+
type: 'bigint';
|
|
687
|
+
}, NumberSchemaNode], [{
|
|
688
|
+
type: string;
|
|
689
|
+
}, ScalarSchemaNode], [{
|
|
690
|
+
minLength: number;
|
|
691
|
+
}, StringSchemaNode], [{
|
|
692
|
+
maxLength: number;
|
|
693
|
+
}, StringSchemaNode], [{
|
|
694
|
+
pattern: string;
|
|
695
|
+
}, StringSchemaNode], [{
|
|
696
|
+
minimum: number;
|
|
697
|
+
}, NumberSchemaNode], [{
|
|
698
|
+
maximum: number;
|
|
699
|
+
}, NumberSchemaNode]];
|
|
700
|
+
/**
|
|
701
|
+
* Infers the matching AST `SchemaNode` type from an input schema shape.
|
|
702
|
+
*/
|
|
703
|
+
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;
|
|
704
|
+
//#endregion
|
|
705
|
+
//#region src/nodes/property.d.ts
|
|
706
|
+
/**
|
|
707
|
+
* AST node representing one named object property.
|
|
708
|
+
*
|
|
709
|
+
* @example
|
|
710
|
+
* ```ts
|
|
711
|
+
* const property: PropertyNode = {
|
|
712
|
+
* kind: 'Property',
|
|
713
|
+
* name: 'id',
|
|
714
|
+
* schema: createSchema({ type: 'integer' }),
|
|
715
|
+
* required: true,
|
|
716
|
+
* }
|
|
717
|
+
* ```
|
|
718
|
+
*/
|
|
719
|
+
type PropertyNode = BaseNode & {
|
|
720
|
+
kind: 'Property';
|
|
721
|
+
/**
|
|
722
|
+
* Property key.
|
|
723
|
+
*/
|
|
724
|
+
name: string;
|
|
725
|
+
/**
|
|
726
|
+
* Property schema.
|
|
727
|
+
*/
|
|
728
|
+
schema: SchemaNode;
|
|
729
|
+
/**
|
|
730
|
+
* Whether the property is required.
|
|
731
|
+
*/
|
|
732
|
+
required: boolean;
|
|
733
|
+
};
|
|
734
|
+
/**
|
|
735
|
+
* Loosely-typed property accepted by `createProperty`, with `required` optional.
|
|
736
|
+
*/
|
|
737
|
+
type UserPropertyNode = Pick<PropertyNode, 'name' | 'schema'> & Partial<Omit<PropertyNode, 'kind' | 'name' | 'schema'>>;
|
|
738
|
+
/**
|
|
739
|
+
* Definition for the {@link PropertyNode}. `required` defaults to `false`, and the schema's
|
|
740
|
+
* `optional`/`nullish` flags are derived from it through {@link optionality}.
|
|
741
|
+
*/
|
|
742
|
+
declare const propertyDef: NodeDef<PropertyNode, UserPropertyNode>;
|
|
743
|
+
/**
|
|
744
|
+
* Creates a `PropertyNode`.
|
|
745
|
+
*
|
|
746
|
+
* @example
|
|
747
|
+
* ```ts
|
|
748
|
+
* const property = createProperty({
|
|
749
|
+
* name: 'status',
|
|
750
|
+
* required: true,
|
|
751
|
+
* schema: createSchema({ type: 'string', nullable: true }),
|
|
752
|
+
* })
|
|
753
|
+
* // required=true, no optional/nullish
|
|
754
|
+
* ```
|
|
755
|
+
*/
|
|
756
|
+
declare const createProperty: (input: UserPropertyNode) => PropertyNode;
|
|
757
|
+
//#endregion
|
|
758
|
+
//#region src/nodes/schema.d.ts
|
|
759
|
+
type PrimitiveSchemaType =
|
|
760
|
+
/**
|
|
761
|
+
* Text value.
|
|
762
|
+
*/
|
|
763
|
+
'string'
|
|
764
|
+
/**
|
|
765
|
+
* Floating-point number.
|
|
766
|
+
*/
|
|
767
|
+
| 'number'
|
|
768
|
+
/**
|
|
769
|
+
* Integer number.
|
|
770
|
+
*/
|
|
771
|
+
| 'integer'
|
|
772
|
+
/**
|
|
773
|
+
* Big integer number.
|
|
774
|
+
*/
|
|
775
|
+
| 'bigint'
|
|
776
|
+
/**
|
|
777
|
+
* Boolean value.
|
|
778
|
+
*/
|
|
779
|
+
| 'boolean'
|
|
780
|
+
/**
|
|
781
|
+
* Null value.
|
|
782
|
+
*/
|
|
783
|
+
| 'null'
|
|
784
|
+
/**
|
|
785
|
+
* Any value.
|
|
786
|
+
*/
|
|
787
|
+
| 'any'
|
|
788
|
+
/**
|
|
789
|
+
* Unknown value.
|
|
790
|
+
*/
|
|
791
|
+
| 'unknown'
|
|
792
|
+
/**
|
|
793
|
+
* No value (`void`).
|
|
794
|
+
*/
|
|
795
|
+
| 'void'
|
|
796
|
+
/**
|
|
797
|
+
* Never value.
|
|
798
|
+
*/
|
|
799
|
+
| 'never'
|
|
800
|
+
/**
|
|
801
|
+
* Object value.
|
|
802
|
+
*/
|
|
803
|
+
| 'object'
|
|
804
|
+
/**
|
|
805
|
+
* Array value.
|
|
806
|
+
*/
|
|
807
|
+
| 'array'
|
|
808
|
+
/**
|
|
809
|
+
* Date value.
|
|
810
|
+
*/
|
|
811
|
+
| 'date';
|
|
812
|
+
/**
|
|
813
|
+
* Composite schema types.
|
|
814
|
+
*/
|
|
815
|
+
type ComplexSchemaType = 'tuple' | 'union' | 'intersection' | 'enum';
|
|
816
|
+
/**
|
|
817
|
+
* Schema types that need special handling in generators.
|
|
818
|
+
*/
|
|
819
|
+
type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'ipv4' | 'ipv6' | 'blob';
|
|
820
|
+
/**
|
|
821
|
+
* All schema type strings.
|
|
822
|
+
*/
|
|
823
|
+
type SchemaType = PrimitiveSchemaType | ComplexSchemaType | SpecialSchemaType;
|
|
824
|
+
/**
|
|
825
|
+
* Scalar schema types without extra object/array/ref structure.
|
|
826
|
+
*/
|
|
827
|
+
type ScalarSchemaType = Exclude<SchemaType, 'object' | 'array' | 'tuple' | 'union' | 'intersection' | 'enum' | 'ref' | 'datetime' | 'date' | 'time' | 'string' | 'number' | 'integer' | 'bigint' | 'url' | 'uuid' | 'email'>;
|
|
828
|
+
/**
|
|
829
|
+
* Fields shared by all schema nodes.
|
|
830
|
+
*/
|
|
831
|
+
type SchemaNodeBase = BaseNode & {
|
|
832
|
+
/**
|
|
833
|
+
* Node kind.
|
|
834
|
+
*/
|
|
835
|
+
kind: 'Schema';
|
|
836
|
+
/**
|
|
837
|
+
* Schema name for named definitions (for example, `"Pet"`).
|
|
838
|
+
* Inline schemas omit this field.
|
|
839
|
+
* `null` means Kubb has processed this and determined there is no applicable name.
|
|
840
|
+
* `undefined` means the name has not been set yet.
|
|
841
|
+
*/
|
|
842
|
+
name?: string | null;
|
|
843
|
+
/**
|
|
844
|
+
* Short schema title.
|
|
845
|
+
*/
|
|
846
|
+
title?: string;
|
|
847
|
+
/**
|
|
848
|
+
* Schema description text.
|
|
849
|
+
*/
|
|
850
|
+
description?: string;
|
|
851
|
+
/**
|
|
852
|
+
* Whether `null` is allowed.
|
|
853
|
+
*/
|
|
854
|
+
nullable?: boolean;
|
|
855
|
+
/**
|
|
856
|
+
* Whether the field is optional.
|
|
857
|
+
*/
|
|
858
|
+
optional?: boolean;
|
|
859
|
+
/**
|
|
860
|
+
* Both optional and nullable (`optional` + `nullable`).
|
|
861
|
+
*/
|
|
862
|
+
nullish?: boolean;
|
|
863
|
+
/**
|
|
864
|
+
* Whether the schema is deprecated.
|
|
865
|
+
*/
|
|
866
|
+
deprecated?: boolean;
|
|
867
|
+
/**
|
|
868
|
+
* Whether the schema is read-only.
|
|
869
|
+
*/
|
|
870
|
+
readOnly?: boolean;
|
|
871
|
+
/**
|
|
872
|
+
* Whether the schema is write-only.
|
|
873
|
+
*/
|
|
874
|
+
writeOnly?: boolean;
|
|
875
|
+
/**
|
|
876
|
+
* Default value.
|
|
877
|
+
*/
|
|
878
|
+
default?: unknown;
|
|
879
|
+
/**
|
|
880
|
+
* Example values from an `examples` array.
|
|
881
|
+
*/
|
|
882
|
+
examples?: Array<unknown>;
|
|
883
|
+
/**
|
|
884
|
+
* Base primitive type.
|
|
885
|
+
* For example, this is `'string'` for a `uuid` schema.
|
|
886
|
+
*/
|
|
887
|
+
primitive?: PrimitiveSchemaType;
|
|
888
|
+
/**
|
|
889
|
+
* Schema `format` value.
|
|
890
|
+
*/
|
|
891
|
+
format?: string;
|
|
892
|
+
};
|
|
893
|
+
/**
|
|
894
|
+
* Object schema with ordered properties.
|
|
895
|
+
*
|
|
896
|
+
* @example
|
|
897
|
+
* ```ts
|
|
898
|
+
* const objectSchema: ObjectSchemaNode = {
|
|
899
|
+
* kind: 'Schema',
|
|
900
|
+
* type: 'object',
|
|
901
|
+
* properties: [],
|
|
902
|
+
* }
|
|
903
|
+
* ```
|
|
904
|
+
*/
|
|
905
|
+
type ObjectSchemaNode = SchemaNodeBase & {
|
|
906
|
+
/**
|
|
907
|
+
* Schema type discriminator.
|
|
908
|
+
*/
|
|
909
|
+
type: 'object';
|
|
910
|
+
/**
|
|
911
|
+
* Primitive type, always `'object'` for object schemas.
|
|
912
|
+
*/
|
|
913
|
+
primitive: 'object';
|
|
914
|
+
/**
|
|
915
|
+
* Ordered object properties.
|
|
916
|
+
*/
|
|
917
|
+
properties: Array<PropertyNode>;
|
|
918
|
+
/**
|
|
919
|
+
* Additional object properties behavior:
|
|
920
|
+
* - `true`: allow any value
|
|
921
|
+
* - `false`: reject unknown properties
|
|
922
|
+
* - `SchemaNode`: allow values that match that schema
|
|
923
|
+
* - `undefined`: no additional properties constraint (open object)
|
|
924
|
+
*/
|
|
925
|
+
additionalProperties?: SchemaNode | boolean;
|
|
926
|
+
/**
|
|
927
|
+
* Pattern-based property schemas.
|
|
928
|
+
*/
|
|
929
|
+
patternProperties?: Record<string, SchemaNode>;
|
|
930
|
+
/**
|
|
931
|
+
* Minimum number of properties allowed.
|
|
932
|
+
*/
|
|
933
|
+
minProperties?: number;
|
|
934
|
+
/**
|
|
935
|
+
* Maximum number of properties allowed.
|
|
936
|
+
*/
|
|
937
|
+
maxProperties?: number;
|
|
938
|
+
};
|
|
939
|
+
/**
|
|
940
|
+
* Array-like schema (`array` or `tuple`).
|
|
941
|
+
*
|
|
942
|
+
* @example
|
|
943
|
+
* ```ts
|
|
944
|
+
* const arraySchema: ArraySchemaNode = {
|
|
945
|
+
* kind: 'Schema',
|
|
946
|
+
* type: 'array',
|
|
947
|
+
* items: [],
|
|
948
|
+
* }
|
|
949
|
+
* ```
|
|
950
|
+
*/
|
|
951
|
+
type ArraySchemaNode = SchemaNodeBase & {
|
|
952
|
+
/**
|
|
953
|
+
* Schema type discriminator (`array` or `tuple`).
|
|
954
|
+
*/
|
|
955
|
+
type: 'array' | 'tuple';
|
|
956
|
+
/**
|
|
957
|
+
* Item schemas.
|
|
958
|
+
*/
|
|
959
|
+
items?: Array<SchemaNode>;
|
|
960
|
+
/**
|
|
961
|
+
* Tuple rest-item schema for elements beyond positional `items`.
|
|
962
|
+
*/
|
|
963
|
+
rest?: SchemaNode;
|
|
964
|
+
/**
|
|
965
|
+
* Minimum item count (or tuple length).
|
|
966
|
+
*/
|
|
967
|
+
min?: number;
|
|
968
|
+
/**
|
|
969
|
+
* Maximum item count (or tuple length).
|
|
970
|
+
*/
|
|
971
|
+
max?: number;
|
|
972
|
+
/**
|
|
973
|
+
* Whether all items must be unique.
|
|
974
|
+
*/
|
|
975
|
+
unique?: boolean;
|
|
976
|
+
};
|
|
977
|
+
/**
|
|
978
|
+
* Shared shape for union and intersection schemas.
|
|
979
|
+
*/
|
|
980
|
+
type CompositeSchemaNodeBase = SchemaNodeBase & {
|
|
981
|
+
/**
|
|
982
|
+
* Member schemas.
|
|
983
|
+
*/
|
|
984
|
+
members?: Array<SchemaNode>;
|
|
985
|
+
};
|
|
986
|
+
/**
|
|
987
|
+
* Union schema, often from `oneOf` or `anyOf`.
|
|
988
|
+
*
|
|
989
|
+
* @example
|
|
990
|
+
* ```ts
|
|
991
|
+
* const unionSchema: UnionSchemaNode = {
|
|
992
|
+
* kind: 'Schema',
|
|
993
|
+
* type: 'union',
|
|
994
|
+
* members: [],
|
|
995
|
+
* }
|
|
996
|
+
* ```
|
|
997
|
+
*/
|
|
998
|
+
type UnionSchemaNode = CompositeSchemaNodeBase & {
|
|
999
|
+
/**
|
|
1000
|
+
* Schema type discriminator.
|
|
1001
|
+
*/
|
|
1002
|
+
type: 'union';
|
|
1003
|
+
/**
|
|
1004
|
+
* Discriminator property name for a polymorphic union.
|
|
1005
|
+
*/
|
|
1006
|
+
discriminatorPropertyName?: string;
|
|
1007
|
+
/**
|
|
1008
|
+
* How many union members must be valid.
|
|
1009
|
+
* - `'one'`: exactly one member, from `oneOf`
|
|
1010
|
+
* - `'any'`: any number of members, from `anyOf`
|
|
1011
|
+
*/
|
|
1012
|
+
strategy?: 'one' | 'any';
|
|
1013
|
+
};
|
|
1014
|
+
/**
|
|
1015
|
+
* Intersection schema, often from `allOf`.
|
|
1016
|
+
*
|
|
1017
|
+
* @example
|
|
1018
|
+
* ```ts
|
|
1019
|
+
* const intersectionSchema: IntersectionSchemaNode = {
|
|
1020
|
+
* kind: 'Schema',
|
|
1021
|
+
* type: 'intersection',
|
|
1022
|
+
* members: [],
|
|
1023
|
+
* }
|
|
1024
|
+
* ```
|
|
1025
|
+
*/
|
|
1026
|
+
type IntersectionSchemaNode = CompositeSchemaNodeBase & {
|
|
1027
|
+
/**
|
|
1028
|
+
* Schema type discriminator.
|
|
1029
|
+
*/
|
|
1030
|
+
type: 'intersection';
|
|
1031
|
+
};
|
|
1032
|
+
/**
|
|
1033
|
+
* One named enum item.
|
|
1034
|
+
*/
|
|
1035
|
+
type EnumValueNode = {
|
|
1036
|
+
/**
|
|
1037
|
+
* Enum item name.
|
|
1038
|
+
*/
|
|
1039
|
+
name: string;
|
|
1040
|
+
/**
|
|
1041
|
+
* Enum item value.
|
|
1042
|
+
*/
|
|
1043
|
+
value: string | number | boolean;
|
|
1044
|
+
/**
|
|
1045
|
+
* Primitive type of the enum value.
|
|
1046
|
+
*/
|
|
1047
|
+
primitive: Extract<PrimitiveSchemaType, 'string' | 'number' | 'boolean'>;
|
|
1048
|
+
/**
|
|
1049
|
+
* Label for the enum item, taken from the `x-enumDescriptions` or
|
|
1050
|
+
* `x-enum-descriptions` vendor extension. `@kubb/plugin-ts` renders it as
|
|
1051
|
+
* JSDoc on the matching enum member.
|
|
1052
|
+
*/
|
|
1053
|
+
description?: string;
|
|
1054
|
+
};
|
|
1055
|
+
/**
|
|
1056
|
+
* Enum schema node.
|
|
1057
|
+
*
|
|
1058
|
+
* @example
|
|
1059
|
+
* ```ts
|
|
1060
|
+
* const enumSchema: EnumSchemaNode = {
|
|
1061
|
+
* kind: 'Schema',
|
|
1062
|
+
* type: 'enum',
|
|
1063
|
+
* enumValues: ['a', 'b'],
|
|
1064
|
+
* }
|
|
1065
|
+
* ```
|
|
1066
|
+
*/
|
|
1067
|
+
type EnumSchemaNode = SchemaNodeBase & {
|
|
1068
|
+
/**
|
|
1069
|
+
* Schema type discriminator.
|
|
1070
|
+
*/
|
|
1071
|
+
type: 'enum';
|
|
1072
|
+
/**
|
|
1073
|
+
* Enum values in simple form.
|
|
1074
|
+
*/
|
|
1075
|
+
enumValues?: Array<string | number | boolean | null>;
|
|
1076
|
+
/**
|
|
1077
|
+
* Enum values in named form.
|
|
1078
|
+
* If present, this is used instead of `enumValues`.
|
|
1079
|
+
*/
|
|
1080
|
+
namedEnumValues?: Array<EnumValueNode>;
|
|
1081
|
+
};
|
|
1082
|
+
/**
|
|
1083
|
+
* Reference schema that points to another schema definition.
|
|
1084
|
+
*
|
|
1085
|
+
* @example
|
|
1086
|
+
* ```ts
|
|
1087
|
+
* const refSchema: RefSchemaNode = {
|
|
1088
|
+
* kind: 'Schema',
|
|
1089
|
+
* type: 'ref',
|
|
1090
|
+
* ref: '#/components/schemas/Pet',
|
|
1091
|
+
* }
|
|
1092
|
+
* ```
|
|
1093
|
+
*/
|
|
1094
|
+
type RefSchemaNode = SchemaNodeBase & {
|
|
1095
|
+
/**
|
|
1096
|
+
* Schema type discriminator.
|
|
1097
|
+
*/
|
|
1098
|
+
type: 'ref';
|
|
1099
|
+
/**
|
|
1100
|
+
* Referenced schema name.
|
|
1101
|
+
* `null` means Kubb has processed this and determined there is no applicable name.
|
|
1102
|
+
*/
|
|
1103
|
+
name?: string | null;
|
|
1104
|
+
/**
|
|
1105
|
+
* Original `$ref` path, for example, `#/components/schemas/Order`.
|
|
1106
|
+
* Used to resolve names later.
|
|
1107
|
+
*/
|
|
1108
|
+
ref?: string;
|
|
1109
|
+
/**
|
|
1110
|
+
* Pattern copied from a sibling `pattern` field.
|
|
1111
|
+
*/
|
|
1112
|
+
pattern?: string;
|
|
1113
|
+
/**
|
|
1114
|
+
* The fully-parsed schema this ref resolves to, so its structure (`primitive`, `properties`)
|
|
1115
|
+
* can be read without following the reference. Populated during parsing when the
|
|
1116
|
+
* definition resolves, `null` when it can't or the ref is circular, and `undefined` when
|
|
1117
|
+
* resolution has not been attempted.
|
|
1118
|
+
*/
|
|
1119
|
+
schema?: SchemaNode | null;
|
|
1120
|
+
};
|
|
1121
|
+
/**
|
|
1122
|
+
* Datetime schema.
|
|
1123
|
+
*
|
|
1124
|
+
* @example
|
|
1125
|
+
* ```ts
|
|
1126
|
+
* const datetimeSchema: DatetimeSchemaNode = { kind: 'Schema', type: 'datetime' }
|
|
1127
|
+
* ```
|
|
1128
|
+
*/
|
|
1129
|
+
type DatetimeSchemaNode = SchemaNodeBase & {
|
|
1130
|
+
/**
|
|
1131
|
+
* Schema type discriminator.
|
|
1132
|
+
*/
|
|
1133
|
+
type: 'datetime';
|
|
1134
|
+
/**
|
|
1135
|
+
* Whether the datetime includes a timezone offset (`dateType: 'stringOffset'`).
|
|
1136
|
+
*/
|
|
1137
|
+
offset?: boolean;
|
|
1138
|
+
/**
|
|
1139
|
+
* Whether the datetime is local (no timezone, `dateType: 'stringLocal'`).
|
|
1140
|
+
*/
|
|
1141
|
+
local?: boolean;
|
|
1142
|
+
};
|
|
1143
|
+
/**
|
|
1144
|
+
* Shared base for `date` and `time` schemas.
|
|
1145
|
+
*/
|
|
1146
|
+
type TemporalSchemaNodeBase<T extends 'date' | 'time'> = SchemaNodeBase & {
|
|
1147
|
+
/**
|
|
1148
|
+
* Schema type discriminator.
|
|
1149
|
+
*/
|
|
1150
|
+
type: T;
|
|
1151
|
+
/**
|
|
1152
|
+
* Output representation in generated code.
|
|
1153
|
+
*/
|
|
1154
|
+
representation: 'date' | 'string';
|
|
1155
|
+
};
|
|
1156
|
+
/**
|
|
1157
|
+
* Date schema node.
|
|
1158
|
+
*
|
|
1159
|
+
* @example
|
|
1160
|
+
* ```ts
|
|
1161
|
+
* const dateSchema: DateSchemaNode = { kind: 'Schema', type: 'date', representation: 'string' }
|
|
1162
|
+
* ```
|
|
1163
|
+
*/
|
|
1164
|
+
type DateSchemaNode = TemporalSchemaNodeBase<'date'>;
|
|
1165
|
+
/**
|
|
1166
|
+
* Time schema node.
|
|
1167
|
+
*
|
|
1168
|
+
* @example
|
|
1169
|
+
* ```ts
|
|
1170
|
+
* const timeSchema: TimeSchemaNode = { kind: 'Schema', type: 'time', representation: 'string' }
|
|
1171
|
+
* ```
|
|
1172
|
+
*/
|
|
1173
|
+
type TimeSchemaNode = TemporalSchemaNodeBase<'time'>;
|
|
1174
|
+
/**
|
|
1175
|
+
* String schema node.
|
|
1176
|
+
*
|
|
1177
|
+
* @example
|
|
1178
|
+
* ```ts
|
|
1179
|
+
* const stringSchema: StringSchemaNode = { kind: 'Schema', type: 'string' }
|
|
1180
|
+
* ```
|
|
1181
|
+
*/
|
|
1182
|
+
type StringSchemaNode = SchemaNodeBase & {
|
|
1183
|
+
/**
|
|
1184
|
+
* Schema type discriminator.
|
|
1185
|
+
*/
|
|
1186
|
+
type: 'string';
|
|
1187
|
+
/**
|
|
1188
|
+
* Minimum string length.
|
|
1189
|
+
*/
|
|
1190
|
+
min?: number;
|
|
1191
|
+
/**
|
|
1192
|
+
* Maximum string length.
|
|
1193
|
+
*/
|
|
1194
|
+
max?: number;
|
|
1195
|
+
/**
|
|
1196
|
+
* Regex pattern.
|
|
1197
|
+
*/
|
|
1198
|
+
pattern?: string;
|
|
1199
|
+
};
|
|
1200
|
+
/**
|
|
1201
|
+
* Numeric schema (`number`, `integer`, or `bigint`).
|
|
1202
|
+
*
|
|
1203
|
+
* @example
|
|
1204
|
+
* ```ts
|
|
1205
|
+
* const numberSchema: NumberSchemaNode = { kind: 'Schema', type: 'number' }
|
|
1206
|
+
* ```
|
|
1207
|
+
*/
|
|
1208
|
+
type NumberSchemaNode = SchemaNodeBase & {
|
|
1209
|
+
/**
|
|
1210
|
+
* Schema type discriminator.
|
|
1211
|
+
*/
|
|
1212
|
+
type: 'number' | 'integer' | 'bigint';
|
|
1213
|
+
/**
|
|
1214
|
+
* Minimum value.
|
|
1215
|
+
*/
|
|
1216
|
+
min?: number;
|
|
1217
|
+
/**
|
|
1218
|
+
* Maximum value.
|
|
1219
|
+
*/
|
|
1220
|
+
max?: number;
|
|
1221
|
+
/**
|
|
1222
|
+
* Exclusive minimum value.
|
|
1223
|
+
*/
|
|
1224
|
+
exclusiveMinimum?: number;
|
|
1225
|
+
/**
|
|
1226
|
+
* Exclusive maximum value.
|
|
1227
|
+
*/
|
|
1228
|
+
exclusiveMaximum?: number;
|
|
1229
|
+
/**
|
|
1230
|
+
* The value must be a multiple of this number.
|
|
1231
|
+
*/
|
|
1232
|
+
multipleOf?: number;
|
|
1233
|
+
};
|
|
1234
|
+
/**
|
|
1235
|
+
* Scalar schema with no extra constraints.
|
|
1236
|
+
*
|
|
1237
|
+
* @example
|
|
1238
|
+
* ```ts
|
|
1239
|
+
* const anySchema: ScalarSchemaNode = { kind: 'Schema', type: 'any' }
|
|
1240
|
+
* ```
|
|
1241
|
+
*/
|
|
1242
|
+
type ScalarSchemaNode = SchemaNodeBase & {
|
|
1243
|
+
/**
|
|
1244
|
+
* Schema type discriminator.
|
|
1245
|
+
*/
|
|
1246
|
+
type: ScalarSchemaType;
|
|
1247
|
+
};
|
|
1248
|
+
/**
|
|
1249
|
+
* URL schema node.
|
|
1250
|
+
* Can include a path template for template literal types.
|
|
1251
|
+
*
|
|
1252
|
+
* @example
|
|
1253
|
+
* ```ts
|
|
1254
|
+
* const urlSchema: UrlSchemaNode = { kind: 'Schema', type: 'url', path: '/pets/{petId}' }
|
|
1255
|
+
* ```
|
|
1256
|
+
*/
|
|
1257
|
+
type UrlSchemaNode = SchemaNodeBase & {
|
|
1258
|
+
/**
|
|
1259
|
+
* Schema type discriminator.
|
|
1260
|
+
*/
|
|
1261
|
+
type: 'url';
|
|
1262
|
+
/**
|
|
1263
|
+
* Path template, for example, `'/pets/{petId}'`.
|
|
1264
|
+
*/
|
|
1265
|
+
path?: string;
|
|
1266
|
+
/**
|
|
1267
|
+
* Minimum string length.
|
|
1268
|
+
*/
|
|
1269
|
+
min?: number;
|
|
1270
|
+
/**
|
|
1271
|
+
* Maximum string length.
|
|
1272
|
+
*/
|
|
1273
|
+
max?: number;
|
|
1274
|
+
};
|
|
1275
|
+
/**
|
|
1276
|
+
* Format-string schema for string-based formats that support length constraints.
|
|
1277
|
+
*
|
|
1278
|
+
* @example
|
|
1279
|
+
* ```ts
|
|
1280
|
+
* const uuidSchema: FormatStringSchemaNode = { kind: 'Schema', type: 'uuid', min: 36, max: 36 }
|
|
1281
|
+
* ```
|
|
1282
|
+
*/
|
|
1283
|
+
type FormatStringSchemaNode = SchemaNodeBase & {
|
|
1284
|
+
/**
|
|
1285
|
+
* Schema type discriminator.
|
|
1286
|
+
*/
|
|
1287
|
+
type: 'uuid' | 'email';
|
|
1288
|
+
/**
|
|
1289
|
+
* Minimum string length.
|
|
1290
|
+
*/
|
|
1291
|
+
min?: number;
|
|
1292
|
+
/**
|
|
1293
|
+
* Maximum string length.
|
|
1294
|
+
*/
|
|
1295
|
+
max?: number;
|
|
1296
|
+
};
|
|
1297
|
+
/**
|
|
1298
|
+
* IPv4 address schema node.
|
|
1299
|
+
*
|
|
1300
|
+
* @example
|
|
1301
|
+
* ```ts
|
|
1302
|
+
* const ipv4Schema: Ipv4SchemaNode = { kind: 'Schema', type: 'ipv4' }
|
|
1303
|
+
* ```
|
|
1304
|
+
*/
|
|
1305
|
+
type Ipv4SchemaNode = SchemaNodeBase & {
|
|
1306
|
+
/**
|
|
1307
|
+
* Schema type discriminator.
|
|
1308
|
+
*/
|
|
1309
|
+
type: 'ipv4';
|
|
1310
|
+
};
|
|
1311
|
+
/**
|
|
1312
|
+
* IPv6 address schema node.
|
|
1313
|
+
*
|
|
1314
|
+
* @example
|
|
1315
|
+
* ```ts
|
|
1316
|
+
* const ipv6Schema: Ipv6SchemaNode = { kind: 'Schema', type: 'ipv6' }
|
|
1317
|
+
* ```
|
|
1318
|
+
*/
|
|
1319
|
+
type Ipv6SchemaNode = SchemaNodeBase & {
|
|
1320
|
+
/**
|
|
1321
|
+
* Schema type discriminator.
|
|
1322
|
+
*/
|
|
1323
|
+
type: 'ipv6';
|
|
1324
|
+
};
|
|
1325
|
+
/**
|
|
1326
|
+
* Mapping from schema type literals to concrete schema node types.
|
|
1327
|
+
* Used by `narrowSchema`.
|
|
1328
|
+
*/
|
|
1329
|
+
type SchemaNodeByType = {
|
|
1330
|
+
object: ObjectSchemaNode;
|
|
1331
|
+
array: ArraySchemaNode;
|
|
1332
|
+
tuple: ArraySchemaNode;
|
|
1333
|
+
union: UnionSchemaNode;
|
|
1334
|
+
intersection: IntersectionSchemaNode;
|
|
1335
|
+
enum: EnumSchemaNode;
|
|
1336
|
+
ref: RefSchemaNode;
|
|
1337
|
+
datetime: DatetimeSchemaNode;
|
|
1338
|
+
date: DateSchemaNode;
|
|
1339
|
+
time: TimeSchemaNode;
|
|
1340
|
+
string: StringSchemaNode;
|
|
1341
|
+
number: NumberSchemaNode;
|
|
1342
|
+
integer: NumberSchemaNode;
|
|
1343
|
+
bigint: NumberSchemaNode;
|
|
1344
|
+
boolean: ScalarSchemaNode;
|
|
1345
|
+
null: ScalarSchemaNode;
|
|
1346
|
+
any: ScalarSchemaNode;
|
|
1347
|
+
unknown: ScalarSchemaNode;
|
|
1348
|
+
void: ScalarSchemaNode;
|
|
1349
|
+
never: ScalarSchemaNode;
|
|
1350
|
+
uuid: FormatStringSchemaNode;
|
|
1351
|
+
email: FormatStringSchemaNode;
|
|
1352
|
+
url: UrlSchemaNode;
|
|
1353
|
+
ipv4: Ipv4SchemaNode;
|
|
1354
|
+
ipv6: Ipv6SchemaNode;
|
|
1355
|
+
blob: ScalarSchemaNode;
|
|
1356
|
+
};
|
|
1357
|
+
/**
|
|
1358
|
+
* Union of all schema node types.
|
|
1359
|
+
*/
|
|
1360
|
+
type SchemaNode = ObjectSchemaNode | ArraySchemaNode | UnionSchemaNode | IntersectionSchemaNode | EnumSchemaNode | RefSchemaNode | DatetimeSchemaNode | DateSchemaNode | TimeSchemaNode | StringSchemaNode | NumberSchemaNode | UrlSchemaNode | FormatStringSchemaNode | Ipv4SchemaNode | Ipv6SchemaNode | ScalarSchemaNode;
|
|
1361
|
+
type CreateSchemaObjectInput = Omit<ObjectSchemaNode, 'kind' | 'properties' | 'primitive'> & {
|
|
1362
|
+
properties?: Array<PropertyNode>;
|
|
1363
|
+
primitive?: 'object';
|
|
1364
|
+
};
|
|
1365
|
+
type CreateSchemaInput = CreateSchemaObjectInput | DistributiveOmit<Exclude<SchemaNode, ObjectSchemaNode>, 'kind'>;
|
|
1366
|
+
type CreateSchemaOutput<T extends CreateSchemaInput> = InferSchemaNode<T> & {
|
|
1367
|
+
kind: 'Schema';
|
|
1368
|
+
};
|
|
1369
|
+
/**
|
|
1370
|
+
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
|
|
1371
|
+
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
|
|
1372
|
+
*/
|
|
1373
|
+
declare const schemaDef: NodeDef<SchemaNode, CreateSchemaInput>;
|
|
1374
|
+
/**
|
|
1375
|
+
* Creates a `SchemaNode`, narrowed to the variant of `props.type`.
|
|
1376
|
+
*
|
|
1377
|
+
* @example
|
|
1378
|
+
* ```ts
|
|
1379
|
+
* const scalar = createSchema({ type: 'string' })
|
|
1380
|
+
* // { kind: 'Schema', type: 'string', primitive: 'string' }
|
|
1381
|
+
* ```
|
|
1382
|
+
*
|
|
1383
|
+
* @example
|
|
1384
|
+
* ```ts
|
|
1385
|
+
* const object = createSchema({ type: 'object' })
|
|
1386
|
+
* // { kind: 'Schema', type: 'object', primitive: 'object', properties: [] }
|
|
1387
|
+
* ```
|
|
1388
|
+
*/
|
|
1389
|
+
declare function createSchema<T extends CreateSchemaInput>(props: T): CreateSchemaOutput<T>;
|
|
1390
|
+
declare function createSchema(props: CreateSchemaInput): SchemaNode;
|
|
1391
|
+
//#endregion
|
|
1392
|
+
//#region src/nodes/content.d.ts
|
|
1393
|
+
/**
|
|
1394
|
+
* AST node representing one content-type entry of a request body or response.
|
|
1395
|
+
*
|
|
1396
|
+
* There is one entry per content type declared in the spec (e.g. `application/json`,
|
|
1397
|
+
* `multipart/form-data`), and each entry holds its own body schema.
|
|
1398
|
+
*
|
|
1399
|
+
* @example
|
|
1400
|
+
* ```ts
|
|
1401
|
+
* const content: ContentNode = {
|
|
1402
|
+
* kind: 'Content',
|
|
1403
|
+
* contentType: 'application/json',
|
|
1404
|
+
* schema: createSchema({ type: 'string' }),
|
|
1405
|
+
* }
|
|
1406
|
+
* ```
|
|
1407
|
+
*/
|
|
1408
|
+
type ContentNode = BaseNode & {
|
|
1409
|
+
/**
|
|
1410
|
+
* Node kind.
|
|
1411
|
+
*/
|
|
1412
|
+
kind: 'Content';
|
|
1413
|
+
/**
|
|
1414
|
+
* The content type for this entry (e.g. `'application/json'`).
|
|
1415
|
+
*/
|
|
1416
|
+
contentType: string;
|
|
1417
|
+
/**
|
|
1418
|
+
* Body schema for this content type.
|
|
1419
|
+
*/
|
|
1420
|
+
schema?: SchemaNode;
|
|
1421
|
+
/**
|
|
1422
|
+
* Property keys to exclude from the generated type via `Omit<Type, Keys>`.
|
|
1423
|
+
* Set when a referenced schema has `readOnly`/`writeOnly` fields that should be omitted.
|
|
1424
|
+
*/
|
|
1425
|
+
keysToOmit?: Array<string> | null;
|
|
1426
|
+
};
|
|
1427
|
+
/**
|
|
1428
|
+
* Definition for the {@link ContentNode}.
|
|
1429
|
+
*/
|
|
1430
|
+
declare const contentDef: NodeDef<ContentNode, Omit<ContentNode, "kind">>;
|
|
1431
|
+
/**
|
|
1432
|
+
* Creates a `ContentNode` for a single request-body or response content type.
|
|
1433
|
+
*/
|
|
1434
|
+
declare const createContent: (input: Omit<ContentNode, "kind">) => ContentNode;
|
|
1435
|
+
//#endregion
|
|
1436
|
+
//#region src/nodes/file.d.ts
|
|
1437
|
+
/**
|
|
1438
|
+
* Supported file extensions.
|
|
1439
|
+
*/
|
|
1440
|
+
type Extname = '.ts' | '.js' | '.tsx' | '.json' | `.${string}`;
|
|
1441
|
+
type ImportName = string | Array<string | {
|
|
1442
|
+
propertyName: string;
|
|
1443
|
+
name?: string;
|
|
1444
|
+
}>;
|
|
1445
|
+
/**
|
|
1446
|
+
* Represents a language-agnostic import/dependency declaration.
|
|
1447
|
+
*
|
|
1448
|
+
* @example Named import (TypeScript: `import { useState } from 'react'`)
|
|
1449
|
+
* ```ts
|
|
1450
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
1451
|
+
* ```
|
|
1452
|
+
*
|
|
1453
|
+
* @example Default import (TypeScript: `import React from 'react'`)
|
|
1454
|
+
* ```ts
|
|
1455
|
+
* createImport({ name: 'React', path: 'react' })
|
|
1456
|
+
* ```
|
|
1457
|
+
*
|
|
1458
|
+
* @example Type-only import (TypeScript: `import type { FC } from 'react'`)
|
|
1459
|
+
* ```ts
|
|
1460
|
+
* createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
|
|
1461
|
+
* ```
|
|
1462
|
+
*
|
|
1463
|
+
* @example Namespace import (TypeScript: `import * as React from 'react'`)
|
|
1464
|
+
* ```ts
|
|
1465
|
+
* createImport({ name: 'React', path: 'react', isNameSpace: true })
|
|
1466
|
+
* ```
|
|
1467
|
+
*/
|
|
1468
|
+
type ImportNode = BaseNode & {
|
|
1469
|
+
kind: 'Import';
|
|
1470
|
+
/**
|
|
1471
|
+
* Import name(s) to be used.
|
|
1472
|
+
*
|
|
1473
|
+
* @example Named imports
|
|
1474
|
+
* `['useState']`
|
|
1475
|
+
*
|
|
1476
|
+
* @example Default import
|
|
1477
|
+
* `'React'`
|
|
1478
|
+
*/
|
|
1479
|
+
name: ImportName;
|
|
1480
|
+
/**
|
|
1481
|
+
* Path for the import.
|
|
1482
|
+
*
|
|
1483
|
+
* @example
|
|
1484
|
+
* `'@kubb/core'`
|
|
1485
|
+
*/
|
|
1486
|
+
path: string;
|
|
1487
|
+
/**
|
|
1488
|
+
* Add a type-only import prefix.
|
|
1489
|
+
* - `true` generates `import type { Type } from './path'`
|
|
1490
|
+
* - `false` generates `import { Type } from './path'`
|
|
1491
|
+
*/
|
|
1492
|
+
isTypeOnly?: boolean | null;
|
|
1493
|
+
/**
|
|
1494
|
+
* Import the entire module as a namespace.
|
|
1495
|
+
* - `true` generates `import * as Name from './path'`
|
|
1496
|
+
* - `false` generates a standard import
|
|
1497
|
+
*/
|
|
1498
|
+
isNameSpace?: boolean | null;
|
|
1499
|
+
/**
|
|
1500
|
+
* When set, the import path is resolved relative to this root.
|
|
1501
|
+
*/
|
|
1502
|
+
root?: string | null;
|
|
1503
|
+
};
|
|
1504
|
+
/**
|
|
1505
|
+
* Represents a language-agnostic export/public API declaration.
|
|
1506
|
+
*
|
|
1507
|
+
* @example Named export (TypeScript: `export { Pets } from './Pets'`)
|
|
1508
|
+
* ```ts
|
|
1509
|
+
* createExport({ name: ['Pets'], path: './Pets' })
|
|
1510
|
+
* ```
|
|
1511
|
+
*
|
|
1512
|
+
* @example Type-only export (TypeScript: `export type { Pet } from './Pet'`)
|
|
1513
|
+
* ```ts
|
|
1514
|
+
* createExport({ name: ['Pet'], path: './Pet', isTypeOnly: true })
|
|
1515
|
+
* ```
|
|
1516
|
+
*
|
|
1517
|
+
* @example Wildcard export (TypeScript: `export * from './utils'`)
|
|
1518
|
+
* ```ts
|
|
1519
|
+
* createExport({ path: './utils' })
|
|
1520
|
+
* ```
|
|
1521
|
+
*
|
|
1522
|
+
* @example Namespace alias (TypeScript: `export * as utils from './utils'`)
|
|
1523
|
+
* ```ts
|
|
1524
|
+
* createExport({ name: 'utils', path: './utils', asAlias: true })
|
|
1525
|
+
* ```
|
|
1526
|
+
*/
|
|
1527
|
+
type ExportNode = BaseNode & {
|
|
1528
|
+
kind: 'Export';
|
|
1529
|
+
/**
|
|
1530
|
+
* Export name(s) to be used. When omitted, generates a wildcard export.
|
|
1531
|
+
*
|
|
1532
|
+
* @example Named exports
|
|
1533
|
+
* `['useState']`
|
|
1534
|
+
*
|
|
1535
|
+
* @example Single export
|
|
1536
|
+
* `'React'`
|
|
1537
|
+
*/
|
|
1538
|
+
name?: string | Array<string> | null;
|
|
1539
|
+
/**
|
|
1540
|
+
* Path for the export.
|
|
1541
|
+
*
|
|
1542
|
+
* @example
|
|
1543
|
+
* `'@kubb/core'`
|
|
1544
|
+
*/
|
|
1545
|
+
path: string;
|
|
1546
|
+
/**
|
|
1547
|
+
* Add a type-only export prefix.
|
|
1548
|
+
* - `true` generates `export type { Type } from './path'`
|
|
1549
|
+
* - `false` generates `export { Type } from './path'`
|
|
1550
|
+
*/
|
|
1551
|
+
isTypeOnly?: boolean | null;
|
|
1552
|
+
/**
|
|
1553
|
+
* Export as an aliased namespace.
|
|
1554
|
+
* - `true` generates `export * as aliasName from './path'`
|
|
1555
|
+
* - `false` generates a standard export
|
|
1556
|
+
*/
|
|
1557
|
+
asAlias?: boolean | null;
|
|
1558
|
+
};
|
|
1559
|
+
/**
|
|
1560
|
+
* Represents a fragment of source code within a file.
|
|
1561
|
+
*
|
|
1562
|
+
* @example Named exportable source
|
|
1563
|
+
* ```ts
|
|
1564
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true, isIndexable: true })
|
|
1565
|
+
* ```
|
|
1566
|
+
*
|
|
1567
|
+
* @example Inline unnamed code block
|
|
1568
|
+
* ```ts
|
|
1569
|
+
* createSource({ nodes: [createText('const x = 1')] })
|
|
1570
|
+
* ```
|
|
1571
|
+
*/
|
|
1572
|
+
type SourceNode = BaseNode & {
|
|
1573
|
+
kind: 'Source';
|
|
1574
|
+
/**
|
|
1575
|
+
* Optional name identifying this source (used for deduplication and barrel generation).
|
|
1576
|
+
*/
|
|
1577
|
+
name?: string | null;
|
|
1578
|
+
/**
|
|
1579
|
+
* Mark this source as a type-only export.
|
|
1580
|
+
*/
|
|
1581
|
+
isTypeOnly?: boolean | null;
|
|
1582
|
+
/**
|
|
1583
|
+
* Include the `export` keyword in the generated source.
|
|
1584
|
+
*/
|
|
1585
|
+
isExportable?: boolean | null;
|
|
1586
|
+
/**
|
|
1587
|
+
* Include this source in barrel/index file generation.
|
|
1588
|
+
*/
|
|
1589
|
+
isIndexable?: boolean | null;
|
|
1590
|
+
/**
|
|
1591
|
+
* Child nodes that make up this source fragment, in DOM order.
|
|
1592
|
+
* Use a {@link TextNode} for raw string content.
|
|
1593
|
+
*/
|
|
1594
|
+
nodes?: Array<CodeNode>;
|
|
1595
|
+
};
|
|
1596
|
+
/**
|
|
1597
|
+
* Represents a fully resolved file in the AST.
|
|
1598
|
+
*
|
|
1599
|
+
* Created via `createFile()`, which computes the `id`, `name`, and `extname` from the input
|
|
1600
|
+
* and deduplicates `imports`, `exports`, and `sources`.
|
|
1601
|
+
*
|
|
1602
|
+
* @example
|
|
1603
|
+
* ```ts
|
|
1604
|
+
* const file = createFile({
|
|
1605
|
+
* baseName: 'petStore.ts',
|
|
1606
|
+
* path: 'src/models/petStore.ts',
|
|
1607
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })],
|
|
1608
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
1609
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
1610
|
+
* })
|
|
1611
|
+
* // file.id = SHA256 hash of the path
|
|
1612
|
+
* // file.name = 'petStore'
|
|
1613
|
+
* // file.extname = '.ts'
|
|
1614
|
+
* ```
|
|
1615
|
+
*/
|
|
1616
|
+
type FileNode<TMeta extends object = object> = BaseNode & {
|
|
1617
|
+
kind: 'File';
|
|
1618
|
+
/**
|
|
1619
|
+
* Unique identifier derived from a SHA256 hash of the file path. `createFile`
|
|
1620
|
+
* computes it, so callers do not need to provide it.
|
|
1621
|
+
*/
|
|
1622
|
+
id: string;
|
|
1623
|
+
/**
|
|
1624
|
+
* File name without extension, derived from `baseName`.
|
|
1625
|
+
*
|
|
1626
|
+
* @see https://nodejs.org/api/path.html#pathformatpathobject
|
|
1627
|
+
*/
|
|
1628
|
+
name: string;
|
|
1629
|
+
/**
|
|
1630
|
+
* File base name, including extension, shaped like `${name}${extname}`.
|
|
1631
|
+
*
|
|
1632
|
+
* @see https://nodejs.org/api/path.html#pathbasenamepath-suffix
|
|
1633
|
+
*/
|
|
1634
|
+
baseName: `${string}.${string}`;
|
|
1635
|
+
/**
|
|
1636
|
+
* Full qualified path to the file.
|
|
1637
|
+
*/
|
|
1638
|
+
path: string;
|
|
1639
|
+
/**
|
|
1640
|
+
* File extension extracted from `baseName`.
|
|
1641
|
+
*/
|
|
1642
|
+
extname: Extname;
|
|
1643
|
+
/**
|
|
1644
|
+
* Deduplicated list of source code fragments.
|
|
1645
|
+
*/
|
|
1646
|
+
sources: Array<SourceNode>;
|
|
1647
|
+
/**
|
|
1648
|
+
* Deduplicated list of import declarations.
|
|
1649
|
+
*/
|
|
1650
|
+
imports: Array<ImportNode>;
|
|
1651
|
+
/**
|
|
1652
|
+
* Deduplicated list of export declarations.
|
|
1653
|
+
*/
|
|
1654
|
+
exports: Array<ExportNode>;
|
|
1655
|
+
/**
|
|
1656
|
+
* Optional metadata attached to this file, read by plugins during barrel generation.
|
|
1657
|
+
*/
|
|
1658
|
+
meta?: TMeta;
|
|
1659
|
+
/**
|
|
1660
|
+
* Optional banner prepended to the generated file content.
|
|
1661
|
+
* Accepts `null` so `resolver.default.banner()` results can be passed directly.
|
|
1662
|
+
*/
|
|
1663
|
+
banner?: string | null;
|
|
1664
|
+
/**
|
|
1665
|
+
* Optional footer appended to the generated file content.
|
|
1666
|
+
* Accepts `null` so `resolver.default.footer()` results can be passed directly.
|
|
1667
|
+
*/
|
|
1668
|
+
footer?: string | null;
|
|
1669
|
+
/**
|
|
1670
|
+
* Absolute on-disk path to copy verbatim into the output, bypassing the parser.
|
|
1671
|
+
*
|
|
1672
|
+
* Use to emit a real source file shipped inside a package (a template) into the generated
|
|
1673
|
+
* folder without reformatting or import reordering. Only `banner` and `footer` are applied
|
|
1674
|
+
* around the copied content. When set, `copy` provides the file content and any `sources`
|
|
1675
|
+
* nodes are ignored for output; `sources` may still carry `name`/`isExportable`/`isIndexable`
|
|
1676
|
+
* so barrel generation treats the file the same as a rendered one.
|
|
1677
|
+
*/
|
|
1678
|
+
copy?: string | null;
|
|
1679
|
+
};
|
|
1680
|
+
/**
|
|
1681
|
+
* Definition for the {@link ImportNode}.
|
|
1682
|
+
*/
|
|
1683
|
+
declare const importDef: NodeDef<ImportNode, Omit<ImportNode, "kind">>;
|
|
1684
|
+
/**
|
|
1685
|
+
* Definition for the {@link ExportNode}.
|
|
1686
|
+
*/
|
|
1687
|
+
declare const exportDef: NodeDef<ExportNode, Omit<ExportNode, "kind">>;
|
|
1688
|
+
/**
|
|
1689
|
+
* Definition for the {@link SourceNode}.
|
|
1690
|
+
*/
|
|
1691
|
+
declare const sourceDef: NodeDef<SourceNode, Omit<SourceNode, "kind">>;
|
|
1692
|
+
/**
|
|
1693
|
+
* Definition for the {@link FileNode}. The fully resolved builder lives in
|
|
1694
|
+
* `createFile`, so this definition only supplies the guard.
|
|
1695
|
+
*/
|
|
1696
|
+
declare const fileDef: NodeDef<FileNode<object>, Omit<FileNode<object>, "kind">>;
|
|
1697
|
+
/**
|
|
1698
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
1699
|
+
*
|
|
1700
|
+
* @example Named import
|
|
1701
|
+
* ```ts
|
|
1702
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
1703
|
+
* // import { useState } from 'react'
|
|
1704
|
+
* ```
|
|
1705
|
+
*/
|
|
1706
|
+
declare const createImport: (input: Omit<ImportNode, "kind">) => ImportNode;
|
|
1707
|
+
/**
|
|
1708
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
1709
|
+
*
|
|
1710
|
+
* @example Named export
|
|
1711
|
+
* ```ts
|
|
1712
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
1713
|
+
* // export { Pet } from './Pet'
|
|
1714
|
+
* ```
|
|
1715
|
+
*/
|
|
1716
|
+
declare const createExport: (input: Omit<ExportNode, "kind">) => ExportNode;
|
|
1717
|
+
/**
|
|
1718
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
1719
|
+
*
|
|
1720
|
+
* @example
|
|
1721
|
+
* ```ts
|
|
1722
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
1723
|
+
* ```
|
|
1724
|
+
*/
|
|
1725
|
+
declare const createSource: (input: Omit<SourceNode, "kind">) => SourceNode;
|
|
1726
|
+
/**
|
|
1727
|
+
* Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed
|
|
1728
|
+
* and `imports`/`exports`/`sources` are deduplicated.
|
|
1729
|
+
*/
|
|
1730
|
+
type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> & Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>;
|
|
1731
|
+
/**
|
|
1732
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
1733
|
+
*
|
|
1734
|
+
* Computes:
|
|
1735
|
+
* - `id` SHA256 hash of the file path
|
|
1736
|
+
* - `name` `baseName` without extension
|
|
1737
|
+
* - `extname` extension extracted from `baseName`
|
|
1738
|
+
*
|
|
1739
|
+
* Deduplicates:
|
|
1740
|
+
* - `sources` via `combineSources`
|
|
1741
|
+
* - `exports` via `combineExports`
|
|
1742
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
1743
|
+
*
|
|
1744
|
+
* @throws {Error} when `baseName` has no extension.
|
|
1745
|
+
*
|
|
1746
|
+
* @example
|
|
1747
|
+
* ```ts
|
|
1748
|
+
* const file = createFile({
|
|
1749
|
+
* baseName: 'petStore.ts',
|
|
1750
|
+
* path: 'src/models/petStore.ts',
|
|
1751
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
1752
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
1753
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
1754
|
+
* })
|
|
1755
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
1756
|
+
* // file.name = 'petStore'
|
|
1757
|
+
* // file.extname = '.ts'
|
|
1758
|
+
* ```
|
|
1759
|
+
*
|
|
1760
|
+
* @example Copy a real file into the output verbatim
|
|
1761
|
+
* ```ts
|
|
1762
|
+
* const file = createFile({
|
|
1763
|
+
* baseName: 'client.ts',
|
|
1764
|
+
* path: 'src/gen/client.ts',
|
|
1765
|
+
* copy: '/abs/path/to/templates/client.ts',
|
|
1766
|
+
* })
|
|
1767
|
+
* ```
|
|
1768
|
+
*/
|
|
1769
|
+
declare function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta>;
|
|
1770
|
+
//#endregion
|
|
1771
|
+
//#region src/nodes/parameter.d.ts
|
|
1772
|
+
type ParameterLocation = 'path' | 'query' | 'header' | 'cookie';
|
|
1773
|
+
/**
|
|
1774
|
+
* Parameter serialization style, controlling how a parameter value is rendered into the request.
|
|
1775
|
+
*/
|
|
1776
|
+
type ParameterStyle = 'matrix' | 'label' | 'form' | 'simple' | 'spaceDelimited' | 'pipeDelimited' | 'deepObject';
|
|
1777
|
+
/**
|
|
1778
|
+
* AST node representing one operation parameter.
|
|
1779
|
+
*
|
|
1780
|
+
* @example
|
|
1781
|
+
* ```ts
|
|
1782
|
+
* const param: ParameterNode = {
|
|
1783
|
+
* kind: 'Parameter',
|
|
1784
|
+
* name: 'petId',
|
|
1785
|
+
* in: 'path',
|
|
1786
|
+
* schema: createSchema({ type: 'string' }),
|
|
1787
|
+
* required: true,
|
|
1788
|
+
* }
|
|
1789
|
+
* ```
|
|
1790
|
+
*/
|
|
1791
|
+
type ParameterNode = BaseNode & {
|
|
1792
|
+
kind: 'Parameter';
|
|
1793
|
+
/**
|
|
1794
|
+
* Parameter name.
|
|
1795
|
+
*/
|
|
1796
|
+
name: string;
|
|
1797
|
+
/**
|
|
1798
|
+
* Parameter location (`path`, `query`, `header`, or `cookie`).
|
|
1799
|
+
*/
|
|
1800
|
+
in: ParameterLocation;
|
|
1801
|
+
/**
|
|
1802
|
+
* Parameter schema.
|
|
1803
|
+
*/
|
|
1804
|
+
schema: SchemaNode;
|
|
1805
|
+
/**
|
|
1806
|
+
* Whether the parameter is required.
|
|
1807
|
+
*/
|
|
1808
|
+
required: boolean;
|
|
1809
|
+
/**
|
|
1810
|
+
* Serialization style. Absent when the source omits it, leaving consumers to apply the
|
|
1811
|
+
* per-location default.
|
|
1812
|
+
*/
|
|
1813
|
+
style?: ParameterStyle;
|
|
1814
|
+
/**
|
|
1815
|
+
* Whether array and object values expand into separate values. Absent when the source omits it,
|
|
1816
|
+
* leaving consumers to apply the default for the style.
|
|
1817
|
+
*/
|
|
1818
|
+
explode?: boolean;
|
|
1819
|
+
};
|
|
1820
|
+
type UserParameterNode = Pick<ParameterNode, 'name' | 'in' | 'schema'> & Partial<Omit<ParameterNode, 'kind' | 'name' | 'in' | 'schema'>>;
|
|
1821
|
+
/**
|
|
1822
|
+
* Definition for the {@link ParameterNode}. `required` defaults to `false`, and the schema's
|
|
1823
|
+
* `optional`/`nullish` flags are derived from it through {@link optionality}.
|
|
1824
|
+
*/
|
|
1825
|
+
declare const parameterDef: NodeDef<ParameterNode, UserParameterNode>;
|
|
1826
|
+
/**
|
|
1827
|
+
* Creates a `ParameterNode`.
|
|
1828
|
+
*
|
|
1829
|
+
* @example
|
|
1830
|
+
* ```ts
|
|
1831
|
+
* const param = createParameter({
|
|
1832
|
+
* name: 'petId',
|
|
1833
|
+
* in: 'path',
|
|
1834
|
+
* required: true,
|
|
1835
|
+
* schema: createSchema({ type: 'string' }),
|
|
1836
|
+
* })
|
|
1837
|
+
* ```
|
|
1838
|
+
*/
|
|
1839
|
+
declare const createParameter: (input: UserParameterNode) => ParameterNode;
|
|
1840
|
+
//#endregion
|
|
1841
|
+
//#region src/nodes/requestBody.d.ts
|
|
1842
|
+
/**
|
|
1843
|
+
* AST node representing an operation request body.
|
|
1844
|
+
*
|
|
1845
|
+
* Body schemas live exclusively inside the `content` array (one entry per content type),
|
|
1846
|
+
* mirroring {@link ResponseNode}.
|
|
1847
|
+
*
|
|
1848
|
+
* @example
|
|
1849
|
+
* ```ts
|
|
1850
|
+
* const requestBody: RequestBodyNode = {
|
|
1851
|
+
* kind: 'RequestBody',
|
|
1852
|
+
* required: true,
|
|
1853
|
+
* content: [{ kind: 'Content', contentType: 'application/json', schema: createSchema({ type: 'string' }) }],
|
|
1854
|
+
* }
|
|
1855
|
+
* ```
|
|
1856
|
+
*/
|
|
1857
|
+
type RequestBodyNode = BaseNode & {
|
|
1858
|
+
kind: 'RequestBody';
|
|
1859
|
+
/**
|
|
1860
|
+
* Request body description carried over from the spec.
|
|
1861
|
+
*/
|
|
1862
|
+
description?: string;
|
|
1863
|
+
/**
|
|
1864
|
+
* Whether the request body is required (`requestBody.required: true` in the spec).
|
|
1865
|
+
* When `false` or absent, the generated `data` parameter should be optional.
|
|
1866
|
+
*/
|
|
1867
|
+
required?: boolean;
|
|
1868
|
+
/**
|
|
1869
|
+
* Content type entries for this request body.
|
|
1870
|
+
*
|
|
1871
|
+
* When the adapter `contentType` option is set, this array contains exactly one entry for
|
|
1872
|
+
* that content type. Otherwise it contains one entry per content type declared in the spec,
|
|
1873
|
+
* so plugins can generate code for every variant (for example, separate hooks for
|
|
1874
|
+
* `application/json` and `multipart/form-data`).
|
|
1875
|
+
*/
|
|
1876
|
+
content?: Array<ContentNode>;
|
|
1877
|
+
};
|
|
1878
|
+
/**
|
|
1879
|
+
* Definition for the {@link RequestBodyNode}. Content entries are built upfront with
|
|
1880
|
+
* {@link createContent}, mirroring how `parameters` and `responses` take prebuilt nodes.
|
|
1881
|
+
*/
|
|
1882
|
+
declare const requestBodyDef: NodeDef<RequestBodyNode, Omit<RequestBodyNode, "kind">>;
|
|
1883
|
+
/**
|
|
1884
|
+
* Creates a `RequestBodyNode`.
|
|
1885
|
+
*/
|
|
1886
|
+
declare const createRequestBody: (input: Omit<RequestBodyNode, "kind">) => RequestBodyNode;
|
|
1887
|
+
//#endregion
|
|
1888
|
+
//#region src/nodes/response.d.ts
|
|
1889
|
+
/**
|
|
1890
|
+
* All supported HTTP status code literals as strings, as used in API specs
|
|
1891
|
+
* (for example, `"200"` and `"404"`).
|
|
1892
|
+
*/
|
|
1893
|
+
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';
|
|
1894
|
+
/**
|
|
1895
|
+
* Response status code literal used by operations.
|
|
1896
|
+
*
|
|
1897
|
+
* Includes specific HTTP status code strings and `"default"` for catch-all responses.
|
|
1898
|
+
*
|
|
1899
|
+
* @example
|
|
1900
|
+
* ```ts
|
|
1901
|
+
* const status: StatusCode = '200'
|
|
1902
|
+
* const fallback: StatusCode = 'default'
|
|
1903
|
+
* ```
|
|
1904
|
+
*/
|
|
1905
|
+
type StatusCode = HttpStatusCode | 'default';
|
|
1906
|
+
/**
|
|
1907
|
+
* AST node representing one operation response variant.
|
|
1908
|
+
*
|
|
1909
|
+
* Mirrors {@link OperationNode.requestBody}: the response body schemas live exclusively inside
|
|
1910
|
+
* the `content` array (one entry per content type), so the same schema is never duplicated at the
|
|
1911
|
+
* node root and inside `content`.
|
|
1912
|
+
*
|
|
1913
|
+
* @example
|
|
1914
|
+
* ```ts
|
|
1915
|
+
* const response: ResponseNode = {
|
|
1916
|
+
* kind: 'Response',
|
|
1917
|
+
* statusCode: '200',
|
|
1918
|
+
* content: [{ kind: 'Content', contentType: 'application/json', schema: createSchema({ type: 'string' }) }],
|
|
1919
|
+
* }
|
|
1920
|
+
* ```
|
|
1921
|
+
*/
|
|
1922
|
+
type ResponseNode = BaseNode & {
|
|
1923
|
+
/**
|
|
1924
|
+
* Node kind.
|
|
1925
|
+
*/
|
|
1926
|
+
kind: 'Response';
|
|
1927
|
+
/**
|
|
1928
|
+
* HTTP status code or `'default'` for a fallback response.
|
|
1929
|
+
*/
|
|
1930
|
+
statusCode: StatusCode;
|
|
1931
|
+
/**
|
|
1932
|
+
* Optional response description.
|
|
1933
|
+
*/
|
|
1934
|
+
description?: string;
|
|
1935
|
+
/**
|
|
1936
|
+
* All available content type entries for this response.
|
|
1937
|
+
*
|
|
1938
|
+
* When the adapter `contentType` option is set, this array contains exactly one entry for that
|
|
1939
|
+
* content type. Otherwise it contains one entry per content type declared in the spec, so that
|
|
1940
|
+
* plugins can generate a union of response types (e.g. `application/json` and `application/xml`).
|
|
1941
|
+
* Body-less responses keep a single entry whose `schema` is the empty/`void` placeholder.
|
|
1942
|
+
*
|
|
1943
|
+
* @example
|
|
1944
|
+
* ```ts
|
|
1945
|
+
* // spec response declares both application/json and application/xml
|
|
1946
|
+
* response.content[0].contentType // 'application/json'
|
|
1947
|
+
* response.content[1].contentType // 'application/xml'
|
|
1948
|
+
* ```
|
|
1949
|
+
*/
|
|
1950
|
+
content?: Array<ContentNode>;
|
|
1951
|
+
};
|
|
1952
|
+
type ResponseInput = Pick<ResponseNode, 'statusCode'> & Partial<Omit<ResponseNode, 'kind' | 'statusCode' | 'content'>> & {
|
|
1953
|
+
content?: Array<ContentNode>;
|
|
1954
|
+
schema?: SchemaNode;
|
|
1955
|
+
mediaType?: string | null;
|
|
1956
|
+
keysToOmit?: Array<string> | null;
|
|
1957
|
+
};
|
|
1958
|
+
/**
|
|
1959
|
+
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
|
|
1960
|
+
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
|
|
1961
|
+
*/
|
|
1962
|
+
declare const responseDef: NodeDef<ResponseNode, ResponseInput>;
|
|
1963
|
+
/**
|
|
1964
|
+
* Creates a `ResponseNode`.
|
|
1965
|
+
*
|
|
1966
|
+
* @example
|
|
1967
|
+
* ```ts
|
|
1968
|
+
* const response = createResponse({
|
|
1969
|
+
* statusCode: '200',
|
|
1970
|
+
* content: [createContent({ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) })],
|
|
1971
|
+
* })
|
|
1972
|
+
* ```
|
|
1973
|
+
*/
|
|
1974
|
+
declare const createResponse: (input: ResponseInput) => ResponseNode;
|
|
1975
|
+
//#endregion
|
|
1976
|
+
//#region src/nodes/operation.d.ts
|
|
1977
|
+
/**
|
|
1978
|
+
* HTTP method an operation responds to.
|
|
1979
|
+
*/
|
|
1980
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE';
|
|
1981
|
+
/**
|
|
1982
|
+
* Transport an operation belongs to.
|
|
1983
|
+
*/
|
|
1984
|
+
type OperationProtocol = 'http';
|
|
1985
|
+
/**
|
|
1986
|
+
* Fields shared by every operation, regardless of transport.
|
|
1987
|
+
*/
|
|
1988
|
+
type OperationNodeBase = BaseNode & {
|
|
1989
|
+
/**
|
|
1990
|
+
* Node kind.
|
|
1991
|
+
*/
|
|
1992
|
+
kind: 'Operation';
|
|
1993
|
+
/**
|
|
1994
|
+
* Stable identifier for the operation.
|
|
1995
|
+
*/
|
|
1996
|
+
operationId: string;
|
|
1997
|
+
/**
|
|
1998
|
+
* Group labels for the operation.
|
|
1999
|
+
*/
|
|
2000
|
+
tags: Array<string>;
|
|
2001
|
+
/**
|
|
2002
|
+
* Short one-line operation summary.
|
|
2003
|
+
*/
|
|
2004
|
+
summary?: string;
|
|
2005
|
+
/**
|
|
2006
|
+
* Full operation description.
|
|
2007
|
+
*/
|
|
2008
|
+
description?: string;
|
|
2009
|
+
/**
|
|
2010
|
+
* Marks the operation as deprecated.
|
|
2011
|
+
*/
|
|
2012
|
+
deprecated?: boolean;
|
|
2013
|
+
/**
|
|
2014
|
+
* Query, path, header, and cookie parameters for the operation.
|
|
2015
|
+
*/
|
|
2016
|
+
parameters: Array<ParameterNode>;
|
|
2017
|
+
/**
|
|
2018
|
+
* Request body for the operation.
|
|
2019
|
+
*/
|
|
2020
|
+
requestBody?: RequestBodyNode;
|
|
2021
|
+
/**
|
|
2022
|
+
* Operation responses.
|
|
2023
|
+
*/
|
|
2024
|
+
responses: Array<ResponseNode>;
|
|
2025
|
+
};
|
|
2026
|
+
/**
|
|
2027
|
+
* Operation served over HTTP. `method` and `path` are guaranteed.
|
|
2028
|
+
*
|
|
2029
|
+
* @example
|
|
2030
|
+
* ```ts
|
|
2031
|
+
* const operation: HttpOperationNode = {
|
|
2032
|
+
* kind: 'Operation',
|
|
2033
|
+
* operationId: 'listPets',
|
|
2034
|
+
* protocol: 'http',
|
|
2035
|
+
* method: 'GET',
|
|
2036
|
+
* path: '/pets',
|
|
2037
|
+
* tags: [],
|
|
2038
|
+
* parameters: [],
|
|
2039
|
+
* responses: [],
|
|
2040
|
+
* }
|
|
2041
|
+
* ```
|
|
2042
|
+
*/
|
|
2043
|
+
type HttpOperationNode = OperationNodeBase & {
|
|
2044
|
+
/**
|
|
2045
|
+
* Transport the operation belongs to.
|
|
2046
|
+
*/
|
|
2047
|
+
protocol?: 'http';
|
|
2048
|
+
/**
|
|
2049
|
+
* HTTP method like `'GET'`.
|
|
2050
|
+
*/
|
|
2051
|
+
method: HttpMethod;
|
|
2052
|
+
/**
|
|
2053
|
+
* Path string, for example `/pets/{petId}`, with `{param}` notation preserved.
|
|
2054
|
+
*/
|
|
2055
|
+
path: string;
|
|
2056
|
+
};
|
|
2057
|
+
/**
|
|
2058
|
+
* Operation for a non-HTTP transport. HTTP-only fields are forbidden.
|
|
2059
|
+
*/
|
|
2060
|
+
type GenericOperationNode = OperationNodeBase & {
|
|
2061
|
+
/**
|
|
2062
|
+
* Transport the operation belongs to.
|
|
2063
|
+
*/
|
|
2064
|
+
protocol?: Exclude<OperationProtocol, 'http'>;
|
|
2065
|
+
method?: never;
|
|
2066
|
+
path?: never;
|
|
2067
|
+
};
|
|
2068
|
+
/**
|
|
2069
|
+
* AST node representing one API operation.
|
|
2070
|
+
*
|
|
2071
|
+
* Discriminated on `protocol`: an {@link HttpOperationNode} (`protocol: 'http'`) guarantees
|
|
2072
|
+
* `method` and `path`, while a {@link GenericOperationNode} omits them. Narrow with
|
|
2073
|
+
* `isHttpOperationNode(node)` or `node.protocol === 'http'` before reading `method`/`path`.
|
|
2074
|
+
*/
|
|
2075
|
+
type OperationNode = HttpOperationNode | GenericOperationNode;
|
|
2076
|
+
type OperationInput = {
|
|
2077
|
+
operationId: string;
|
|
2078
|
+
method?: HttpOperationNode['method'];
|
|
2079
|
+
path?: HttpOperationNode['path'];
|
|
2080
|
+
requestBody?: Omit<RequestBodyNode, 'kind'>;
|
|
2081
|
+
[key: string]: unknown;
|
|
2082
|
+
};
|
|
2083
|
+
/**
|
|
2084
|
+
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
|
|
2085
|
+
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
|
|
2086
|
+
* normalized into a `RequestBodyNode`.
|
|
2087
|
+
*/
|
|
2088
|
+
declare const operationDef: NodeDef<OperationNode, OperationInput>;
|
|
2089
|
+
/**
|
|
2090
|
+
* Creates an `OperationNode` with default empty arrays for `tags`, `parameters`, and `responses`.
|
|
2091
|
+
*
|
|
2092
|
+
* @example
|
|
2093
|
+
* ```ts
|
|
2094
|
+
* const operation = createOperation({ operationId: 'getPetById', method: 'GET', path: '/pet/{petId}' })
|
|
2095
|
+
* // tags, parameters, and responses are []
|
|
2096
|
+
* ```
|
|
2097
|
+
*/
|
|
2098
|
+
declare function createOperation(props: Pick<HttpOperationNode, 'operationId' | 'method' | 'path'> & Partial<Omit<HttpOperationNode, 'kind' | 'operationId' | 'method' | 'path' | 'requestBody'>> & {
|
|
2099
|
+
requestBody?: Omit<RequestBodyNode, 'kind'>;
|
|
2100
|
+
}): HttpOperationNode;
|
|
2101
|
+
declare function createOperation(props: Pick<GenericOperationNode, 'operationId'> & Partial<Omit<GenericOperationNode, 'kind' | 'operationId' | 'requestBody'>> & {
|
|
2102
|
+
requestBody?: Omit<RequestBodyNode, 'kind'>;
|
|
2103
|
+
}): GenericOperationNode;
|
|
2104
|
+
//#endregion
|
|
2105
|
+
//#region src/nodes/input.d.ts
|
|
2106
|
+
/**
|
|
2107
|
+
* Metadata for an API document, populated by the adapter and available to every generator.
|
|
2108
|
+
*
|
|
2109
|
+
* All fields are plain JSON-serializable values, no `Set`, no `Map`, no class instances.
|
|
2110
|
+
* Computed fields (`circularNames`, `enumNames`) are pre-calculated once during the adapter
|
|
2111
|
+
* pre-scan so generators never need to iterate the full schema list themselves.
|
|
2112
|
+
*
|
|
2113
|
+
* @example
|
|
2114
|
+
* ```ts
|
|
2115
|
+
* const meta: InputMeta = { title: 'Pet Store', version: '1.0.0', baseURL: 'https://api.example.com/v2', circularNames: [], enumNames: [] }
|
|
2116
|
+
* ```
|
|
2117
|
+
*/
|
|
2118
|
+
type InputMeta = {
|
|
2119
|
+
/**
|
|
2120
|
+
* API title from `info.title` in the source document.
|
|
2121
|
+
*/
|
|
2122
|
+
title?: string;
|
|
2123
|
+
/**
|
|
2124
|
+
* API description from `info.description` in the source document.
|
|
2125
|
+
*/
|
|
2126
|
+
description?: string;
|
|
2127
|
+
/**
|
|
2128
|
+
* API version string from `info.version` in the source document.
|
|
2129
|
+
*/
|
|
2130
|
+
version?: string;
|
|
2131
|
+
/**
|
|
2132
|
+
* Resolved base URL from the first matching server entry in the source document.
|
|
2133
|
+
*/
|
|
2134
|
+
baseURL?: string | null;
|
|
2135
|
+
/**
|
|
2136
|
+
* Names of schemas that participate in a circular reference chain.
|
|
2137
|
+
* Computed once during the adapter pre-scan, so a generator never has to
|
|
2138
|
+
* call `findCircularSchemas` itself.
|
|
2139
|
+
*
|
|
2140
|
+
* Convert to a `Set` once at the start of a generator, not per-schema,
|
|
2141
|
+
* so lookups stay O(1) without repeated allocations.
|
|
2142
|
+
*
|
|
2143
|
+
* @example Wrap a circular schema in z.lazy()
|
|
2144
|
+
* ```ts
|
|
2145
|
+
* const circular = new Set(meta.circularNames)
|
|
2146
|
+
* if (circular.has(schema.name)) { ... }
|
|
2147
|
+
* ```
|
|
2148
|
+
*/
|
|
2149
|
+
circularNames: ReadonlyArray<string>;
|
|
2150
|
+
/**
|
|
2151
|
+
* Names of schemas whose type is `enum`.
|
|
2152
|
+
* Computed once during the adapter pre-scan, so a generator never has to
|
|
2153
|
+
* filter the schema list itself.
|
|
2154
|
+
*
|
|
2155
|
+
* Convert to a `Set` once at the start of a generator when you need repeated
|
|
2156
|
+
* membership checks, so each check stays O(1) instead of an array scan.
|
|
2157
|
+
*
|
|
2158
|
+
* @example Check if a referenced schema is an enum
|
|
2159
|
+
* `const enums = new Set(meta.enumNames)`
|
|
2160
|
+
* `const isEnum = enums.has(schemaName)`
|
|
2161
|
+
*/
|
|
2162
|
+
enumNames: ReadonlyArray<string>;
|
|
2163
|
+
};
|
|
2164
|
+
/**
|
|
2165
|
+
* Input AST node that contains all schemas and operations for one API document.
|
|
2166
|
+
* Produced by the adapter and consumed by all Kubb plugins.
|
|
2167
|
+
*
|
|
2168
|
+
* @example
|
|
2169
|
+
* ```ts
|
|
2170
|
+
* const input: InputNode = {
|
|
2171
|
+
* kind: 'Input',
|
|
2172
|
+
* schemas: [],
|
|
2173
|
+
* operations: [],
|
|
2174
|
+
* meta: { circularNames: [], enumNames: [] },
|
|
2175
|
+
* }
|
|
2176
|
+
* ```
|
|
2177
|
+
*/
|
|
2178
|
+
type InputNode = BaseNode & {
|
|
2179
|
+
/**
|
|
2180
|
+
* Node kind.
|
|
2181
|
+
*/
|
|
2182
|
+
kind: 'Input';
|
|
2183
|
+
/**
|
|
2184
|
+
* All schema nodes in the document.
|
|
2185
|
+
*/
|
|
2186
|
+
schemas: Array<SchemaNode>;
|
|
2187
|
+
/**
|
|
2188
|
+
* All operation nodes in the document.
|
|
2189
|
+
*/
|
|
2190
|
+
operations: Array<OperationNode>;
|
|
2191
|
+
/**
|
|
2192
|
+
* Document metadata populated by the adapter.
|
|
2193
|
+
*/
|
|
2194
|
+
meta: InputMeta;
|
|
2195
|
+
};
|
|
2196
|
+
/**
|
|
2197
|
+
* Definition for the {@link InputNode}.
|
|
2198
|
+
*/
|
|
2199
|
+
declare const inputDef: NodeDef<InputNode, Partial<Omit<InputNode, "kind">>>;
|
|
2200
|
+
/**
|
|
2201
|
+
* Creates an `InputNode`, defaulting `schemas`/`operations` to empty arrays and `meta` per
|
|
2202
|
+
* {@link inputDef}.
|
|
2203
|
+
*
|
|
2204
|
+
* @example
|
|
2205
|
+
* ```ts
|
|
2206
|
+
* const input = createInput()
|
|
2207
|
+
* // { kind: 'Input', schemas: [], operations: [] }
|
|
2208
|
+
* ```
|
|
2209
|
+
*/
|
|
2210
|
+
declare function createInput(overrides?: Partial<Omit<InputNode, 'kind'>>): InputNode;
|
|
2211
|
+
//#endregion
|
|
2212
|
+
//#region src/nodes/output.d.ts
|
|
2213
|
+
/**
|
|
2214
|
+
* Output AST node that groups all generated file output for one API document.
|
|
2215
|
+
*
|
|
2216
|
+
* Produced by generators and consumed by the build pipeline to write files.
|
|
2217
|
+
*
|
|
2218
|
+
* @example
|
|
2219
|
+
* ```ts
|
|
2220
|
+
* const output: OutputNode = {
|
|
2221
|
+
* kind: 'Output',
|
|
2222
|
+
* files: [],
|
|
2223
|
+
* }
|
|
2224
|
+
* ```
|
|
2225
|
+
*/
|
|
2226
|
+
type OutputNode = BaseNode & {
|
|
2227
|
+
/**
|
|
2228
|
+
* Node kind.
|
|
2229
|
+
*/
|
|
2230
|
+
kind: 'Output';
|
|
2231
|
+
/**
|
|
2232
|
+
* Generated file nodes.
|
|
2233
|
+
*/
|
|
2234
|
+
files: Array<FileNode>;
|
|
2235
|
+
};
|
|
2236
|
+
/**
|
|
2237
|
+
* Definition for the {@link OutputNode}.
|
|
2238
|
+
*/
|
|
2239
|
+
declare const outputDef: NodeDef<OutputNode, Partial<Omit<OutputNode, "kind">>>;
|
|
2240
|
+
/**
|
|
2241
|
+
* Creates an `OutputNode` with a stable default for `files`.
|
|
2242
|
+
*
|
|
2243
|
+
* @example
|
|
2244
|
+
* ```ts
|
|
2245
|
+
* const output = createOutput()
|
|
2246
|
+
* // { kind: 'Output', files: [] }
|
|
2247
|
+
* ```
|
|
2248
|
+
*/
|
|
2249
|
+
declare function createOutput(overrides?: Partial<Omit<OutputNode, 'kind'>>): OutputNode;
|
|
2250
|
+
//#endregion
|
|
2251
|
+
//#region src/nodes/index.d.ts
|
|
2252
|
+
/**
|
|
2253
|
+
* Union of all AST node types.
|
|
2254
|
+
*
|
|
2255
|
+
* This lets TypeScript narrow types in `switch (node.kind)` blocks.
|
|
2256
|
+
*
|
|
2257
|
+
* @example
|
|
2258
|
+
* ```ts
|
|
2259
|
+
* function getKind(node: Node): string {
|
|
2260
|
+
* switch (node.kind) {
|
|
2261
|
+
* case 'Input':
|
|
2262
|
+
* return 'input'
|
|
2263
|
+
* case 'Output':
|
|
2264
|
+
* return 'output'
|
|
2265
|
+
* default:
|
|
2266
|
+
* return 'other'
|
|
2267
|
+
* }
|
|
2268
|
+
* }
|
|
2269
|
+
* ```
|
|
2270
|
+
*/
|
|
2271
|
+
type Node = InputNode | OutputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode | RequestBodyNode | ContentNode | FileNode | ImportNode | ExportNode | SourceNode | ConstNode | TypeNode | FunctionNode | ArrowFunctionNode;
|
|
2272
|
+
//#endregion
|
|
2273
|
+
//#region src/visitor.d.ts
|
|
2274
|
+
/**
|
|
2275
|
+
* Ordered mapping of `[NodeType, ParentType]` pairs.
|
|
2276
|
+
*
|
|
2277
|
+
* `ParentOf` uses this map to find parent types.
|
|
2278
|
+
*/
|
|
2279
|
+
type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [OperationNode, InputNode], [RequestBodyNode, OperationNode], [ContentNode, RequestBodyNode | ResponseNode], [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
|
|
2280
|
+
/**
|
|
2281
|
+
* Resolves the parent node type for a given AST node type.
|
|
2282
|
+
*
|
|
2283
|
+
* Visitor context relies on this so `ctx.parent` is typed for each callback.
|
|
2284
|
+
*
|
|
2285
|
+
* @example
|
|
2286
|
+
* ```ts
|
|
2287
|
+
* type InputParent = ParentOf<InputNode>
|
|
2288
|
+
* // undefined
|
|
2289
|
+
* ```
|
|
2290
|
+
*
|
|
2291
|
+
* @example
|
|
2292
|
+
* ```ts
|
|
2293
|
+
* type PropertyParent = ParentOf<PropertyNode>
|
|
2294
|
+
* // SchemaNode
|
|
2295
|
+
* ```
|
|
2296
|
+
*
|
|
2297
|
+
* @example
|
|
2298
|
+
* ```ts
|
|
2299
|
+
* type SchemaParent = ParentOf<SchemaNode>
|
|
2300
|
+
* // InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode
|
|
2301
|
+
* ```
|
|
2302
|
+
*/
|
|
2303
|
+
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;
|
|
2304
|
+
/**
|
|
2305
|
+
* Traversal context passed as the second argument to every visitor callback.
|
|
2306
|
+
* `parent` is typed from the current node type.
|
|
2307
|
+
*
|
|
2308
|
+
* @example
|
|
2309
|
+
* ```ts
|
|
2310
|
+
* const visitor: Visitor = {
|
|
2311
|
+
* schema(node, { parent }) {
|
|
2312
|
+
* // parent type is narrowed by node kind
|
|
2313
|
+
* },
|
|
2314
|
+
* }
|
|
2315
|
+
* ```
|
|
2316
|
+
*/
|
|
2317
|
+
type VisitorContext<T extends Node = Node> = {
|
|
2318
|
+
/**
|
|
2319
|
+
* Parent node of the currently visited node.
|
|
2320
|
+
* For `InputNode`, this is `undefined`.
|
|
2321
|
+
*/
|
|
2322
|
+
parent?: ParentOf<T>;
|
|
2323
|
+
};
|
|
2324
|
+
/**
|
|
2325
|
+
* Synchronous visitor consumed by `transform`. Each optional callback runs
|
|
2326
|
+
* for the matching node type. Return a new node to replace it, or `undefined`
|
|
2327
|
+
* to leave it untouched.
|
|
2328
|
+
*
|
|
2329
|
+
* Plugins typically expose `transformer` so users can supply a `Visitor` that
|
|
2330
|
+
* rewrites the AST before printing.
|
|
2331
|
+
*
|
|
2332
|
+
* @example Prefix every operationId
|
|
2333
|
+
* ```ts
|
|
2334
|
+
* const visitor: Visitor = {
|
|
2335
|
+
* operation(node) {
|
|
2336
|
+
* return { ...node, operationId: `api_${node.operationId}` }
|
|
2337
|
+
* },
|
|
2338
|
+
* }
|
|
2339
|
+
* ```
|
|
2340
|
+
*
|
|
2341
|
+
* @example Strip schema descriptions
|
|
2342
|
+
* ```ts
|
|
2343
|
+
* const visitor: Visitor = {
|
|
2344
|
+
* schema(node) {
|
|
2345
|
+
* return { ...node, description: undefined }
|
|
2346
|
+
* },
|
|
2347
|
+
* }
|
|
2348
|
+
* ```
|
|
2349
|
+
*/
|
|
2350
|
+
type Visitor = {
|
|
2351
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode;
|
|
2352
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode;
|
|
2353
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode;
|
|
2354
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode;
|
|
2355
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode;
|
|
2356
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode;
|
|
2357
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
|
|
2358
|
+
};
|
|
2359
|
+
/**
|
|
2360
|
+
* Visitor used by `collect`.
|
|
2361
|
+
*
|
|
2362
|
+
* @example
|
|
2363
|
+
* ```ts
|
|
2364
|
+
* const visitor: CollectVisitor<string> = {
|
|
2365
|
+
* operation(node) {
|
|
2366
|
+
* return node.operationId
|
|
2367
|
+
* },
|
|
2368
|
+
* }
|
|
2369
|
+
* ```
|
|
2370
|
+
*/
|
|
2371
|
+
type CollectVisitor<T> = {
|
|
2372
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
|
|
2373
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
|
|
2374
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
|
|
2375
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
|
|
2376
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
|
|
2377
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
|
|
2378
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
|
|
2379
|
+
};
|
|
2380
|
+
/**
|
|
2381
|
+
* Options for `transform`.
|
|
2382
|
+
*
|
|
2383
|
+
* @example
|
|
2384
|
+
* ```ts
|
|
2385
|
+
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
|
|
2386
|
+
* ```
|
|
2387
|
+
*
|
|
2388
|
+
* @example
|
|
2389
|
+
* ```ts
|
|
2390
|
+
* // Only transform the current node, not nested children
|
|
2391
|
+
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
|
|
2392
|
+
* ```
|
|
2393
|
+
*/
|
|
2394
|
+
type TransformOptions = Visitor & {
|
|
2395
|
+
/**
|
|
2396
|
+
* Traversal depth.
|
|
2397
|
+
* @default 'deep'
|
|
2398
|
+
*/
|
|
2399
|
+
depth?: VisitorDepth;
|
|
2400
|
+
/**
|
|
2401
|
+
* Internal parent override used during recursion.
|
|
2402
|
+
*/
|
|
2403
|
+
parent?: Node;
|
|
2404
|
+
};
|
|
2405
|
+
/**
|
|
2406
|
+
* Options for `collect`.
|
|
2407
|
+
*
|
|
2408
|
+
* @example
|
|
2409
|
+
* ```ts
|
|
2410
|
+
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
|
|
2411
|
+
* ```
|
|
2412
|
+
*/
|
|
2413
|
+
type CollectOptions<T> = CollectVisitor<T> & {
|
|
2414
|
+
/**
|
|
2415
|
+
* Traversal depth.
|
|
2416
|
+
* @default 'deep'
|
|
2417
|
+
*/
|
|
2418
|
+
depth?: VisitorDepth;
|
|
2419
|
+
/**
|
|
2420
|
+
* Internal parent override used during recursion.
|
|
2421
|
+
*/
|
|
2422
|
+
parent?: Node;
|
|
2423
|
+
};
|
|
2424
|
+
/**
|
|
2425
|
+
* Synchronous depth-first transform. Each visitor callback can return a
|
|
2426
|
+
* replacement node. Returning `undefined` keeps the original.
|
|
2427
|
+
*
|
|
2428
|
+
* The original tree is never mutated, a new tree is returned. Pass
|
|
2429
|
+
* `depth: 'shallow'` to skip recursion into children.
|
|
2430
|
+
*
|
|
2431
|
+
* @example Prefix every operationId
|
|
2432
|
+
* ```ts
|
|
2433
|
+
* const next = transform(root, {
|
|
2434
|
+
* operation(node) {
|
|
2435
|
+
* return { ...node, operationId: `prefixed_${node.operationId}` }
|
|
2436
|
+
* },
|
|
2437
|
+
* })
|
|
2438
|
+
* ```
|
|
2439
|
+
*
|
|
2440
|
+
* @example Replace only the root node
|
|
2441
|
+
* ```ts
|
|
2442
|
+
* const next = transform(root, {
|
|
2443
|
+
* depth: 'shallow',
|
|
2444
|
+
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
|
|
2445
|
+
* })
|
|
2446
|
+
* ```
|
|
2447
|
+
*/
|
|
2448
|
+
declare function transform(node: InputNode, options: TransformOptions): InputNode;
|
|
2449
|
+
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
|
|
2450
|
+
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
|
|
2451
|
+
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
|
|
2452
|
+
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
|
|
2453
|
+
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
|
|
2454
|
+
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
|
|
2455
|
+
declare function transform(node: Node, options: TransformOptions): Node;
|
|
2456
|
+
/**
|
|
2457
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
2458
|
+
* callbacks return into an array.
|
|
2459
|
+
*
|
|
2460
|
+
* @example Collect every operationId
|
|
2461
|
+
* ```ts
|
|
2462
|
+
* const ids = collect<string>(root, {
|
|
2463
|
+
* operation(node) {
|
|
2464
|
+
* return node.operationId
|
|
2465
|
+
* },
|
|
2466
|
+
* })
|
|
2467
|
+
* ```
|
|
2468
|
+
*/
|
|
2469
|
+
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
2470
|
+
//#endregion
|
|
2471
|
+
//#region src/defineMacro.d.ts
|
|
2472
|
+
/**
|
|
2473
|
+
* Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,
|
|
2474
|
+
* and `undefined` keeps declaration order.
|
|
2475
|
+
*/
|
|
2476
|
+
type Enforce = 'pre' | 'post';
|
|
2477
|
+
/**
|
|
2478
|
+
* A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a
|
|
2479
|
+
* {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an
|
|
2480
|
+
* optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter
|
|
2481
|
+
* and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).
|
|
2482
|
+
*/
|
|
2483
|
+
type Macro = Visitor & {
|
|
2484
|
+
/**
|
|
2485
|
+
* Macro identifier used to tell macros apart, for example `'simplify-union'`.
|
|
2486
|
+
*/
|
|
2487
|
+
name: string;
|
|
2488
|
+
/**
|
|
2489
|
+
* Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.
|
|
2490
|
+
* Ordering within a bucket follows list order.
|
|
2491
|
+
*/
|
|
2492
|
+
enforce?: Enforce;
|
|
2493
|
+
/**
|
|
2494
|
+
* Gate checked against the current node before any callback runs. When it returns `false`
|
|
2495
|
+
* the macro is skipped for that node.
|
|
2496
|
+
*/
|
|
2497
|
+
when?: (node: Node) => boolean;
|
|
2498
|
+
};
|
|
2499
|
+
/**
|
|
2500
|
+
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
2501
|
+
* Adds no runtime behavior.
|
|
2502
|
+
*
|
|
2503
|
+
* @example
|
|
2504
|
+
* ```ts
|
|
2505
|
+
* const macroUntagged = defineMacro({
|
|
2506
|
+
* name: 'untagged',
|
|
2507
|
+
* operation(node) {
|
|
2508
|
+
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
2509
|
+
* },
|
|
2510
|
+
* })
|
|
2511
|
+
* ```
|
|
2512
|
+
*/
|
|
2513
|
+
declare function defineMacro(macro: Macro): Macro;
|
|
2514
|
+
/**
|
|
2515
|
+
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
|
|
2516
|
+
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
|
|
2517
|
+
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
|
|
2518
|
+
* has no names, ordering, or composition.
|
|
2519
|
+
*
|
|
2520
|
+
* @example
|
|
2521
|
+
* ```ts
|
|
2522
|
+
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
2523
|
+
* const next = transform(root, visitor)
|
|
2524
|
+
* ```
|
|
2525
|
+
*/
|
|
2526
|
+
declare function composeMacros(macros: ReadonlyArray<Macro>): Visitor;
|
|
2527
|
+
/**
|
|
2528
|
+
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
|
|
2529
|
+
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
|
|
2530
|
+
* `depth: 'shallow'` to rewrite the root node only.
|
|
2531
|
+
*
|
|
2532
|
+
* @example
|
|
2533
|
+
* ```ts
|
|
2534
|
+
* const next = applyMacros(root, [macroIntegerToString])
|
|
2535
|
+
* ```
|
|
2536
|
+
*
|
|
2537
|
+
* @example Apply to the root node only
|
|
2538
|
+
* ```ts
|
|
2539
|
+
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
2540
|
+
* ```
|
|
2541
|
+
*/
|
|
2542
|
+
declare function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: {
|
|
2543
|
+
depth?: VisitorDepth;
|
|
2544
|
+
}): TNode;
|
|
2545
|
+
//#endregion
|
|
2546
|
+
//#region src/createPrinter.d.ts
|
|
2547
|
+
/**
|
|
2548
|
+
* Runtime context passed as `this` to printer handlers.
|
|
2549
|
+
*
|
|
2550
|
+
* `this.transform` dispatches to node-level handlers from `nodes`.
|
|
2551
|
+
*
|
|
2552
|
+
* @example
|
|
2553
|
+
* ```ts
|
|
2554
|
+
* const context: PrinterHandlerContext<string, {}> = {
|
|
2555
|
+
* options: {},
|
|
2556
|
+
* transform: () => 'value',
|
|
2557
|
+
* }
|
|
2558
|
+
* ```
|
|
2559
|
+
*/
|
|
2560
|
+
type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
2561
|
+
/**
|
|
2562
|
+
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
|
|
2563
|
+
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
|
|
2564
|
+
*/
|
|
2565
|
+
transform: (node: SchemaNode) => TOutput | null;
|
|
2566
|
+
/**
|
|
2567
|
+
* Run the printer's built-in handler for the node, ignoring any override for its type.
|
|
2568
|
+
* Inside an override, `this.base(node)` returns what the printer would have emitted,
|
|
2569
|
+
* so the override can wrap it instead of re-implementing the handler. Nested nodes
|
|
2570
|
+
* still dispatch through the overrides.
|
|
2571
|
+
*/
|
|
2572
|
+
base: (node: SchemaNode) => TOutput | null;
|
|
2573
|
+
/**
|
|
2574
|
+
* Options for this printer instance.
|
|
2575
|
+
*/
|
|
2576
|
+
options: TOptions;
|
|
2577
|
+
};
|
|
2578
|
+
/**
|
|
2579
|
+
* Handler for one schema node type.
|
|
2580
|
+
*
|
|
2581
|
+
* Use a regular function (not an arrow function) if you need `this`.
|
|
2582
|
+
*
|
|
2583
|
+
* @example
|
|
2584
|
+
* ```ts
|
|
2585
|
+
* const handler: PrinterHandler<string, {}, 'string'> = function () {
|
|
2586
|
+
* return 'string'
|
|
2587
|
+
* }
|
|
2588
|
+
* ```
|
|
2589
|
+
*/
|
|
2590
|
+
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
|
|
2591
|
+
/**
|
|
2592
|
+
* Partial map of per-node-type handler overrides for a printer.
|
|
2593
|
+
*
|
|
2594
|
+
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
|
|
2595
|
+
* Supply only the handlers you want to replace. The printer's built-in
|
|
2596
|
+
* defaults fill in the rest.
|
|
2597
|
+
*
|
|
2598
|
+
* @example
|
|
2599
|
+
* ```ts
|
|
2600
|
+
* pluginZod({
|
|
2601
|
+
* printer: {
|
|
2602
|
+
* nodes: {
|
|
2603
|
+
* date(): string {
|
|
2604
|
+
* return 'z.string().date()'
|
|
2605
|
+
* },
|
|
2606
|
+
* } satisfies PrinterPartial<string, PrinterZodOptions>,
|
|
2607
|
+
* },
|
|
2608
|
+
* })
|
|
2609
|
+
* ```
|
|
2610
|
+
*/
|
|
2611
|
+
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
|
|
2612
|
+
/**
|
|
2613
|
+
* Generic shape used by `definePrinter`.
|
|
2614
|
+
*
|
|
2615
|
+
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
2616
|
+
* - `TOptions` options passed to and stored on the printer instance
|
|
2617
|
+
* - `TOutput` the type emitted by node handlers
|
|
2618
|
+
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
|
|
2619
|
+
*
|
|
2620
|
+
* @example
|
|
2621
|
+
* ```ts
|
|
2622
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
2623
|
+
* ```
|
|
2624
|
+
*/
|
|
2625
|
+
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
2626
|
+
name: TName;
|
|
2627
|
+
options: TOptions;
|
|
2628
|
+
output: TOutput;
|
|
2629
|
+
printOutput: TPrintOutput;
|
|
2630
|
+
};
|
|
2631
|
+
/**
|
|
2632
|
+
* Printer instance returned by a printer factory.
|
|
2633
|
+
*
|
|
2634
|
+
* @example
|
|
2635
|
+
* ```ts
|
|
2636
|
+
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
2637
|
+
* ```
|
|
2638
|
+
*/
|
|
2639
|
+
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
2640
|
+
/**
|
|
2641
|
+
* Unique identifier supplied at creation time.
|
|
2642
|
+
*/
|
|
2643
|
+
name: T['name'];
|
|
2644
|
+
/**
|
|
2645
|
+
* Options for this printer instance.
|
|
2646
|
+
*/
|
|
2647
|
+
options: T['options'];
|
|
2648
|
+
/**
|
|
2649
|
+
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
|
|
2650
|
+
* Always dispatches through the `nodes` map. Never calls the `print` override.
|
|
2651
|
+
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
2652
|
+
*/
|
|
2653
|
+
transform: (node: SchemaNode) => T['output'] | null;
|
|
2654
|
+
/**
|
|
2655
|
+
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
2656
|
+
* higher-level function (which may produce full declarations).
|
|
2657
|
+
* Otherwise, falls back to the node-level dispatcher.
|
|
2658
|
+
*/
|
|
2659
|
+
print: (node: SchemaNode) => T['printOutput'] | null;
|
|
2660
|
+
};
|
|
2661
|
+
/**
|
|
2662
|
+
* Builder function passed to `definePrinter`.
|
|
2663
|
+
*
|
|
2664
|
+
* It receives resolved options and returns:
|
|
2665
|
+
* - `name`
|
|
2666
|
+
* - `options`
|
|
2667
|
+
* - `nodes` handlers
|
|
2668
|
+
* - optional top-level `print` override
|
|
2669
|
+
*
|
|
2670
|
+
* @example
|
|
2671
|
+
* ```ts
|
|
2672
|
+
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
2673
|
+
* ```
|
|
2674
|
+
*/
|
|
2675
|
+
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
2676
|
+
name: T['name'];
|
|
2677
|
+
/**
|
|
2678
|
+
* Options to store on the printer.
|
|
2679
|
+
*/
|
|
2680
|
+
options: T['options'];
|
|
2681
|
+
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
2682
|
+
/**
|
|
2683
|
+
* User-supplied handler overrides. An override wins over the matching `nodes` handler,
|
|
2684
|
+
* and can call `this.base(node)` to reuse the handler it replaced. Pass overrides here
|
|
2685
|
+
* instead of spreading them into `nodes`, otherwise `this.base` cannot find the original.
|
|
2686
|
+
*/
|
|
2687
|
+
overrides?: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
2688
|
+
/**
|
|
2689
|
+
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
2690
|
+
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
2691
|
+
* not the override itself, so recursion is safe.
|
|
2692
|
+
*/
|
|
2693
|
+
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
|
|
2694
|
+
};
|
|
2695
|
+
/**
|
|
2696
|
+
* Creates a schema printer: a function that takes a `SchemaNode` and emits
|
|
2697
|
+
* code in your target language. Each plugin that produces code from schemas
|
|
2698
|
+
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
|
|
2699
|
+
* with this helper.
|
|
2700
|
+
*
|
|
2701
|
+
* The builder receives resolved options and returns:
|
|
2702
|
+
*
|
|
2703
|
+
* - `name` unique identifier for the printer.
|
|
2704
|
+
* - `options` stored on the returned printer instance.
|
|
2705
|
+
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
|
|
2706
|
+
* output (a string, a TypeScript AST node, ...) for that schema type.
|
|
2707
|
+
* - `overrides` (optional), user-supplied handlers that win over `nodes`.
|
|
2708
|
+
* An override can call `this.base(node)` to reuse the handler it replaced.
|
|
2709
|
+
* - `print` (optional), top-level override exposed as `printer.print`.
|
|
2710
|
+
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
|
|
2711
|
+
*
|
|
2712
|
+
* Without a `print` override, `printer.print` falls back to `printer.transform`
|
|
2713
|
+
* (the node-level dispatcher).
|
|
2714
|
+
*
|
|
2715
|
+
* @example Tiny Zod printer
|
|
2716
|
+
* ```ts
|
|
2717
|
+
* import { createPrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
2718
|
+
*
|
|
2719
|
+
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
2720
|
+
*
|
|
2721
|
+
* export const zodPrinter = createPrinter<PrinterZod>((options) => ({
|
|
2722
|
+
* name: 'zod',
|
|
2723
|
+
* options: { strict: options.strict ?? true },
|
|
2724
|
+
* nodes: {
|
|
2725
|
+
* string: () => 'z.string()',
|
|
2726
|
+
* object(node) {
|
|
2727
|
+
* const props = node.properties
|
|
2728
|
+
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
2729
|
+
* .join(', ')
|
|
2730
|
+
* return `z.object({ ${props} })`
|
|
2731
|
+
* },
|
|
2732
|
+
* },
|
|
2733
|
+
* }))
|
|
2734
|
+
* ```
|
|
2735
|
+
*/
|
|
2736
|
+
declare function createPrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
|
|
2737
|
+
//#endregion
|
|
2738
|
+
export { sourceDef as $, NodeDef as $t, StatusCode as A, BreakNode as At, parameterDef as B, constDef as Bt, GenericOperationNode as C, PropertyNode as Ct, createOperation as D, InferSchemaNode as Dt, OperationNode as E, propertyDef as Et, requestBodyDef as F, JsxNode as Ft, UserFileNode as G, createJsx as Gt, FileNode as H, createBreak as Ht, ParameterLocation as I, TextNode as It, createImport as J, functionDef as Jt, createExport as K, createText as Kt, ParameterNode as L, TypeNode as Lt, responseDef as M, ConstNode as Mt, RequestBodyNode as N, FunctionNode as Nt, operationDef as O, ParserOptions as Ot, createRequestBody as P, JSDocNode as Pt, importDef as Q, DistributiveOmit as Qt, ParameterStyle as R, arrowFunctionDef as Rt, inputDef as S, schemaDef as St, HttpOperationNode as T, createProperty as Tt, ImportNode as U, createConst as Ut, ExportNode as V, createArrowFunction as Vt, SourceNode as W, createFunction as Wt, exportDef as X, textDef as Xt, createSource as Y, jsxDef as Yt, fileDef as Z, typeDef as Zt, createOutput as _, StringSchemaNode as _t, Enforce as a, DatetimeSchemaNode as at, InputNode as b, UrlSchemaNode as bt, composeMacros as c, NumberSchemaNode as ct, Visitor as d, RefSchemaNode as dt, defineNode as en, ContentNode as et, VisitorContext as f, ScalarSchemaNode as ft, OutputNode as g, SchemaType as gt, Node as h, SchemaNodeByType as ht, createPrinter as i, DateSchemaNode as it, createResponse as j, CodeNode as jt, ResponseNode as k, ArrowFunctionNode as kt, defineMacro as l, ObjectSchemaNode as lt, transform as m, SchemaNode as mt, PrinterFactoryOptions as n, NodeKind as nn, createContent as nt, Macro as o, EnumSchemaNode as ot, collect as p, ScalarSchemaType as pt, createFile as q, createType as qt, PrinterPartial as r, schemaTypes as rn, ArraySchemaNode as rt, applyMacros as s, IntersectionSchemaNode as st, Printer as t, BaseNode as tn, contentDef as tt, ParentOf as u, PrimitiveSchemaType as ut, outputDef as v, TimeSchemaNode as vt, HttpMethod as w, UserPropertyNode as wt, createInput as x, createSchema as xt, InputMeta as y, UnionSchemaNode as yt, createParameter as z, breakDef as zt };
|
|
2739
|
+
//# sourceMappingURL=types-Dno_xT4x.d.ts.map
|