@kubb/ast 5.0.0-alpha.15 → 5.0.0-alpha.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1069 -626
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +238 -52
- package/dist/index.js +1054 -624
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/visitor-BFn3X90U.d.ts +1974 -0
- package/package.json +1 -1
- package/src/constants.ts +92 -3
- package/src/factory.ts +174 -37
- package/src/guards.ts +37 -10
- package/src/index.ts +8 -6
- package/src/infer.ts +130 -0
- package/src/mocks.ts +4 -3
- package/src/nodes/base.ts +21 -3
- package/src/nodes/function.ts +34 -22
- package/src/nodes/http.ts +17 -5
- package/src/nodes/index.ts +14 -4
- package/src/nodes/operation.ts +47 -9
- package/src/nodes/parameter.ts +27 -1
- package/src/nodes/property.ts +23 -1
- package/src/nodes/response.ts +23 -3
- package/src/nodes/root.ts +29 -8
- package/src/nodes/schema.ts +298 -36
- package/src/{functionPrinter.ts → printers/functionPrinter.ts} +20 -19
- package/src/printers/index.ts +3 -0
- package/src/{printer.ts → printers/printer.ts} +58 -42
- package/src/refs.ts +36 -4
- package/src/resolvers.ts +45 -0
- package/src/transformers.ts +196 -0
- package/src/types.ts +2 -1
- package/src/utils.ts +37 -8
- package/src/visitor.ts +204 -18
- package/dist/visitor-UlWOe-In.d.ts +0 -961
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,141 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { A as
|
|
2
|
+
import { A as createRoot, B as ResponseNode, C as createFunctionParameter, Ct as httpMethods, D as createParameter, E as createOperation, Et as schemaTypes, G as ParameterNode, L as RootNode, M as InferSchema, N as InferSchemaNode, O as createProperty, P as ParserOptions, S as createDiscriminantNode, T as createObjectBindingParameter, Tt as nodeKinds, _ as resolveRef, _t as FunctionParametersNode, at as SchemaNode, b as definePrinter, d as transform, f as walk, g as refMapToObject, gt as FunctionParameterNode, h as extractRefName, j as createSchema, k as createResponse, l as collect, m as buildRefMap, mt as FunctionNode, ot as SchemaNodeByType, pt as PropertyNode, u as composeTransformers, vt as ObjectBindingParameterNode, w as createFunctionParameters, wt as mediaTypes, xt as SCALAR_PRIMITIVE_TYPES, y as PrinterFactoryOptions, z as OperationNode } from "./visitor-BFn3X90U.js";
|
|
3
3
|
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/guards.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const schema = createSchema({ type: 'string' })
|
|
11
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | undefined
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Returns `true` when the input is a `RootNode`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* if (isRootNode(node)) {
|
|
21
|
+
* console.log(node.schemas.length)
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare const isRootNode: (node: unknown) => node is RootNode;
|
|
5
26
|
/**
|
|
6
|
-
*
|
|
27
|
+
* Returns `true` when the input is an `OperationNode`.
|
|
7
28
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* if (isOperationNode(node)) {
|
|
32
|
+
* console.log(node.operationId)
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare const isOperationNode: (node: unknown) => node is OperationNode;
|
|
37
|
+
/**
|
|
38
|
+
* Returns `true` when the input is a `SchemaNode`.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* if (isSchemaNode(node)) {
|
|
43
|
+
* console.log(node.type)
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
declare const isSchemaNode: (node: unknown) => node is SchemaNode;
|
|
48
|
+
/**
|
|
49
|
+
* Returns `true` when the input is a `PropertyNode`.
|
|
50
|
+
*/
|
|
51
|
+
declare const isPropertyNode: (node: unknown) => node is PropertyNode;
|
|
52
|
+
/**
|
|
53
|
+
* Returns `true` when the input is a `ParameterNode`.
|
|
54
|
+
*/
|
|
55
|
+
declare const isParameterNode: (node: unknown) => node is ParameterNode;
|
|
56
|
+
/**
|
|
57
|
+
* Returns `true` when the input is a `ResponseNode`.
|
|
58
|
+
*/
|
|
59
|
+
declare const isResponseNode: (node: unknown) => node is ResponseNode;
|
|
60
|
+
/**
|
|
61
|
+
* Returns `true` when the input is a `FunctionParameterNode`.
|
|
62
|
+
*/
|
|
63
|
+
declare const isFunctionParameterNode: (node: unknown) => node is FunctionParameterNode;
|
|
64
|
+
/**
|
|
65
|
+
* Returns `true` when the input is an `ObjectBindingParameterNode`.
|
|
66
|
+
*/
|
|
67
|
+
declare const isObjectBindingParameterNode: (node: unknown) => node is ObjectBindingParameterNode;
|
|
68
|
+
/**
|
|
69
|
+
* Returns `true` when the input is a `FunctionParametersNode`.
|
|
14
70
|
*/
|
|
71
|
+
declare const isFunctionParametersNode: (node: unknown) => node is FunctionParametersNode;
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/printers/functionPrinter.d.ts
|
|
74
|
+
/**
|
|
75
|
+
* Creates a function-parameter printer factory.
|
|
76
|
+
*
|
|
77
|
+
* This wrapper uses `createPrinterFactory` and dispatches handlers by `node.kind`
|
|
78
|
+
* (for function nodes) rather than by `node.type` (for schema nodes).
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { mode: 'declaration' | 'call' }, string>
|
|
83
|
+
*
|
|
84
|
+
* export const myPrinter = defineFunctionPrinter<MyPrinter>((options) => ({
|
|
85
|
+
* name: 'my',
|
|
86
|
+
* options,
|
|
87
|
+
* nodes: {
|
|
88
|
+
* functionParameter(node) {
|
|
89
|
+
* return options.mode === 'declaration' && node.type ? `${node.name}: ${node.type}` : node.name
|
|
90
|
+
* },
|
|
91
|
+
* objectBindingParameter(node) {
|
|
92
|
+
* const inner = node.properties.map(p => this.print(p)).filter(Boolean).join(', ')
|
|
93
|
+
* return `{ ${inner} }`
|
|
94
|
+
* },
|
|
95
|
+
* functionParameters(node) {
|
|
96
|
+
* return node.params.map(p => this.print(p)).filter(Boolean).join(', ')
|
|
97
|
+
* },
|
|
98
|
+
* },
|
|
99
|
+
* }))
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
declare const defineFunctionPrinter: <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
|
|
103
|
+
name: T["name"];
|
|
104
|
+
options: T["options"];
|
|
105
|
+
nodes: Partial<{
|
|
106
|
+
functionParameter: (this: {
|
|
107
|
+
print: (node: FunctionNode) => T["output"] | null | undefined;
|
|
108
|
+
options: T["options"];
|
|
109
|
+
}, node: FunctionParameterNode) => T["output"] | null | undefined;
|
|
110
|
+
objectBindingParameter: (this: {
|
|
111
|
+
print: (node: FunctionNode) => T["output"] | null | undefined;
|
|
112
|
+
options: T["options"];
|
|
113
|
+
}, node: ObjectBindingParameterNode) => T["output"] | null | undefined;
|
|
114
|
+
functionParameters: (this: {
|
|
115
|
+
print: (node: FunctionNode) => T["output"] | null | undefined;
|
|
116
|
+
options: T["options"];
|
|
117
|
+
}, node: FunctionParametersNode) => T["output"] | null | undefined;
|
|
118
|
+
}>;
|
|
119
|
+
print?: ((this: {
|
|
120
|
+
print: (node: FunctionNode) => T["output"] | null | undefined;
|
|
121
|
+
options: T["options"];
|
|
122
|
+
}, node: FunctionNode) => T["printOutput"] | null | undefined) | undefined;
|
|
123
|
+
}) => (options?: T["options"] | undefined) => {
|
|
124
|
+
name: T["name"];
|
|
125
|
+
options: T["options"];
|
|
126
|
+
print: (node: FunctionNode) => T["printOutput"] | null | undefined;
|
|
127
|
+
};
|
|
15
128
|
type FunctionPrinterOptions = {
|
|
129
|
+
/**
|
|
130
|
+
* Rendering modes supported by `functionPrinter`.
|
|
131
|
+
*
|
|
132
|
+
* | Mode | Output example | Use case |
|
|
133
|
+
* |---------------|---------------------------------------------|---------------------------------|
|
|
134
|
+
* | `declaration` | `id: string, config: Config = {}` | Function parameter declaration |
|
|
135
|
+
* | `call` | `id, { method, url }` | Function call arguments |
|
|
136
|
+
* | `keys` | `{ id, config }` | Key names only (destructuring) |
|
|
137
|
+
* | `values` | `{ id: id, config: config }` | Key/value object entries |
|
|
138
|
+
*/
|
|
16
139
|
mode: 'declaration' | 'call' | 'keys' | 'values';
|
|
17
140
|
/**
|
|
18
141
|
* Optional transformation applied to every parameter name before printing.
|
|
@@ -24,12 +147,12 @@ type FunctionPrinterOptions = {
|
|
|
24
147
|
transformType?: (type: string) => string;
|
|
25
148
|
};
|
|
26
149
|
/**
|
|
27
|
-
* Default function-signature printer.
|
|
28
|
-
* used
|
|
150
|
+
* Default function-signature printer.
|
|
151
|
+
* Covers the four standard output modes used across Kubb plugins.
|
|
29
152
|
*
|
|
30
153
|
* @example
|
|
31
154
|
* ```ts
|
|
32
|
-
* const printer =
|
|
155
|
+
* const printer = functionPrinter({ mode: 'declaration' })
|
|
33
156
|
*
|
|
34
157
|
* const sig = createFunctionParameters({
|
|
35
158
|
* params: [
|
|
@@ -47,67 +170,130 @@ declare const functionPrinter: (options?: FunctionPrinterOptions | undefined) =>
|
|
|
47
170
|
print: (node: FunctionNode) => string | null | undefined;
|
|
48
171
|
};
|
|
49
172
|
//#endregion
|
|
50
|
-
//#region src/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
declare function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | undefined;
|
|
55
|
-
/**
|
|
56
|
-
* Type guard for `RootNode`.
|
|
57
|
-
*/
|
|
58
|
-
declare const isRootNode: (node: unknown) => node is RootNode;
|
|
59
|
-
/**
|
|
60
|
-
* Type guard for `OperationNode`.
|
|
61
|
-
*/
|
|
62
|
-
declare const isOperationNode: (node: unknown) => node is OperationNode;
|
|
63
|
-
/**
|
|
64
|
-
* Type guard for `SchemaNode`.
|
|
65
|
-
*/
|
|
66
|
-
declare const isSchemaNode: (node: unknown) => node is SchemaNode;
|
|
67
|
-
/**
|
|
68
|
-
* Type guard for `PropertyNode`.
|
|
69
|
-
*/
|
|
70
|
-
declare const isPropertyNode: (node: unknown) => node is PropertyNode;
|
|
173
|
+
//#region src/resolvers.d.ts
|
|
174
|
+
declare function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | undefined;
|
|
175
|
+
declare function childName(parentName: string | undefined, propName: string): string | undefined;
|
|
176
|
+
declare function enumPropName(parentName: string | undefined, propName: string, enumSuffix: string): string;
|
|
71
177
|
/**
|
|
72
|
-
*
|
|
178
|
+
* Collects import entries for all `ref` schema nodes in `node`.
|
|
73
179
|
*/
|
|
74
|
-
declare
|
|
180
|
+
declare function collectImports<TImport>({
|
|
181
|
+
node,
|
|
182
|
+
nameMapping,
|
|
183
|
+
resolve
|
|
184
|
+
}: {
|
|
185
|
+
node: SchemaNode;
|
|
186
|
+
nameMapping: Map<string, string>;
|
|
187
|
+
resolve: (schemaName: string) => TImport | undefined;
|
|
188
|
+
}): Array<TImport>;
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region src/transformers.d.ts
|
|
75
191
|
/**
|
|
76
|
-
*
|
|
192
|
+
* Replaces a discriminator property's schema with a string enum of allowed values.
|
|
193
|
+
*
|
|
194
|
+
* If `node` is not an object schema, or if the property does not exist, the input
|
|
195
|
+
* node is returned as-is.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* const schema = createSchema({
|
|
200
|
+
* type: 'object',
|
|
201
|
+
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
|
|
202
|
+
* })
|
|
203
|
+
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
|
|
204
|
+
* ```
|
|
77
205
|
*/
|
|
78
|
-
declare
|
|
206
|
+
declare function setDiscriminatorEnum({
|
|
207
|
+
node,
|
|
208
|
+
propertyName,
|
|
209
|
+
values,
|
|
210
|
+
enumName
|
|
211
|
+
}: {
|
|
212
|
+
node: SchemaNode;
|
|
213
|
+
propertyName: string;
|
|
214
|
+
values: Array<string>;
|
|
215
|
+
enumName?: string;
|
|
216
|
+
}): SchemaNode;
|
|
79
217
|
/**
|
|
80
|
-
*
|
|
218
|
+
* Merges adjacent anonymous object members into a single anonymous object member.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* const merged = mergeAdjacentObjects([
|
|
223
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
|
|
224
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
|
|
225
|
+
* ])
|
|
226
|
+
* ```
|
|
81
227
|
*/
|
|
82
|
-
declare
|
|
228
|
+
declare function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode>;
|
|
83
229
|
/**
|
|
84
|
-
*
|
|
230
|
+
* Removes enum members that are covered by broader scalar primitives in the same union.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* const simplified = simplifyUnion([
|
|
235
|
+
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
|
|
236
|
+
* createSchema({ type: 'string' }),
|
|
237
|
+
* ])
|
|
238
|
+
* // keeps only string member
|
|
239
|
+
* ```
|
|
85
240
|
*/
|
|
86
|
-
declare
|
|
241
|
+
declare function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode>;
|
|
242
|
+
declare function setEnumName(propNode: SchemaNode, parentName: string | undefined, propName: string, enumSuffix: string): SchemaNode;
|
|
87
243
|
/**
|
|
88
|
-
*
|
|
244
|
+
* Walks a schema tree and resolves `ref`/`enum` names through callbacks.
|
|
89
245
|
*/
|
|
90
|
-
declare
|
|
246
|
+
declare function resolveNames({
|
|
247
|
+
node,
|
|
248
|
+
nameMapping,
|
|
249
|
+
resolveName,
|
|
250
|
+
resolveEnumName
|
|
251
|
+
}: {
|
|
252
|
+
node: SchemaNode;
|
|
253
|
+
nameMapping: Map<string, string>;
|
|
254
|
+
resolveName: (ref: string) => string | undefined;
|
|
255
|
+
resolveEnumName?: (name: string) => string | undefined;
|
|
256
|
+
}): SchemaNode;
|
|
91
257
|
//#endregion
|
|
92
258
|
//#region src/utils.d.ts
|
|
93
259
|
/**
|
|
94
|
-
* Returns `true` when a schema
|
|
260
|
+
* Returns `true` when a schema is emitted as a plain TypeScript `string`.
|
|
95
261
|
*
|
|
96
262
|
* - `string`, `uuid`, `email`, `url`, `datetime` are always plain strings.
|
|
97
263
|
* - `date` and `time` are plain strings when their `representation` is `'string'` rather than `'date'`.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* isStringType(createSchema({ type: 'uuid' })) // true
|
|
268
|
+
* isStringType(createSchema({ type: 'date', representation: 'date' })) // false
|
|
269
|
+
* ```
|
|
98
270
|
*/
|
|
99
|
-
declare function
|
|
271
|
+
declare function isStringType(node: SchemaNode): boolean;
|
|
100
272
|
/**
|
|
101
|
-
*
|
|
273
|
+
* Applies casing rules to parameter names and returns a new parameter array.
|
|
102
274
|
*
|
|
103
|
-
* The
|
|
104
|
-
*
|
|
275
|
+
* The input array is not mutated.
|
|
276
|
+
* If `casing` is not set, the original array is returned unchanged.
|
|
105
277
|
*
|
|
106
278
|
* Use this before passing parameters to schema builders so that property keys
|
|
107
|
-
* in
|
|
108
|
-
* `OperationNode.parameters`
|
|
279
|
+
* in generated output match the desired casing while preserving
|
|
280
|
+
* `OperationNode.parameters` for other consumers.
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* const params = [createParameter({ name: 'pet_id', in: 'query', schema: createSchema({ type: 'string' }) })]
|
|
285
|
+
* const cased = caseParams(params, 'camelcase')
|
|
286
|
+
* // cased[0].name === 'petId'
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
declare function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
|
|
290
|
+
/**
|
|
291
|
+
* Syncs property/parameter schema optionality flags from `required` and `schema.nullable`.
|
|
292
|
+
*
|
|
293
|
+
* - `optional` is set for non-required, non-nullable schemas.
|
|
294
|
+
* - `nullish` is set for non-required, nullable schemas.
|
|
109
295
|
*/
|
|
110
|
-
declare function
|
|
296
|
+
declare function syncOptionality(required: boolean, schema: SchemaNode): SchemaNode;
|
|
111
297
|
//#endregion
|
|
112
|
-
export {
|
|
298
|
+
export { type InferSchema, type InferSchemaNode, type ParserOptions, SCALAR_PRIMITIVE_TYPES, buildRefMap, caseParams, childName, collect, collectImports, composeTransformers, createDiscriminantNode, createFunctionParameter, createFunctionParameters, createObjectBindingParameter, createOperation, createParameter, createProperty, createResponse, createRoot, createSchema, defineFunctionPrinter, definePrinter, enumPropName, extractRefName, findDiscriminator, functionPrinter, httpMethods, isFunctionParameterNode, isFunctionParametersNode, isObjectBindingParameterNode, isOperationNode, isParameterNode, isPropertyNode, isResponseNode, isRootNode, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, refMapToObject, resolveNames, resolveRef, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, transform, walk };
|
|
113
299
|
//# sourceMappingURL=index.d.ts.map
|