@kubb/ast 5.0.0-alpha.3 → 5.0.0-alpha.31
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 +1123 -163
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +283 -17
- package/dist/index.js +1102 -154
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/visitor-z-5U8NoF.d.ts +2200 -0
- package/package.json +3 -2
- package/src/constants.ts +118 -4
- package/src/factory.ts +332 -18
- package/src/guards.ts +63 -8
- package/src/index.ts +25 -7
- package/src/infer.ts +130 -0
- package/src/mocks.ts +12 -5
- package/src/nodes/base.ts +32 -4
- package/src/nodes/function.ts +201 -0
- package/src/nodes/http.ts +17 -5
- package/src/nodes/index.ts +21 -5
- package/src/nodes/operation.ts +69 -6
- package/src/nodes/parameter.ts +27 -1
- package/src/nodes/property.ts +23 -1
- package/src/nodes/response.ts +29 -3
- package/src/nodes/root.ts +41 -10
- package/src/nodes/schema.ts +440 -42
- package/src/printer.ts +172 -60
- package/src/refs.ts +36 -4
- package/src/resolvers.ts +45 -0
- package/src/transformers.ts +156 -0
- package/src/types.ts +13 -2
- package/src/utils.ts +431 -4
- package/src/visitor.ts +378 -81
- package/dist/visitor-oFfdU8QA.d.ts +0 -653
package/dist/index.d.ts
CHANGED
|
@@ -1,44 +1,310 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { D as
|
|
2
|
+
import { A as createTypeNode, At as schemaTypes, C as createOperation, D as createResponse, Dt as httpMethods, E as createProperty, G as ParameterNode, O as createRoot, Ot as isScalarPrimitive, P as ParserOptions, S as createFunctionParameters, T as createParameterGroup, Tt as ScalarPrimitive, _ as PrinterPartial, bt as FunctionParametersNode, ct as SchemaNode, d as transform, f as walk, g as PrinterFactoryOptions, h as Printer, j as syncOptionality, k as createSchema, kt as mediaTypes, l as collect, lt as SchemaNodeByType, m as extractRefName, u as composeTransformers, v as createPrinterFactory, w as createParameter, x as createFunctionParameter, xt as ParameterGroupNode, y as definePrinter, yt as FunctionParameterNode, z as OperationNode } from "./visitor-z-5U8NoF.js";
|
|
3
3
|
|
|
4
4
|
//#region src/guards.d.ts
|
|
5
5
|
/**
|
|
6
|
-
* Narrows a `SchemaNode` to the
|
|
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
|
+
* ```
|
|
7
13
|
*/
|
|
8
14
|
declare function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | undefined;
|
|
9
15
|
/**
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
16
|
+
* Returns `true` when the input is an `OperationNode`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* if (isOperationNode(node)) {
|
|
21
|
+
* console.log(node.operationId)
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
15
24
|
*/
|
|
16
25
|
declare const isOperationNode: (node: unknown) => node is OperationNode;
|
|
17
26
|
/**
|
|
18
|
-
*
|
|
27
|
+
* Returns `true` when the input is a `SchemaNode`.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* if (isSchemaNode(node)) {
|
|
32
|
+
* console.log(node.type)
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
19
35
|
*/
|
|
20
36
|
declare const isSchemaNode: (node: unknown) => node is SchemaNode;
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/resolvers.d.ts
|
|
39
|
+
declare function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null;
|
|
40
|
+
declare function childName(parentName: string | null | undefined, propName: string): string | null;
|
|
41
|
+
declare function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Collects import entries for all `ref` schema nodes in `node`.
|
|
44
|
+
*/
|
|
45
|
+
declare function collectImports<TImport>({
|
|
46
|
+
node,
|
|
47
|
+
nameMapping,
|
|
48
|
+
resolve
|
|
49
|
+
}: {
|
|
50
|
+
node: SchemaNode;
|
|
51
|
+
nameMapping: Map<string, string>;
|
|
52
|
+
resolve: (schemaName: string) => TImport | undefined;
|
|
53
|
+
}): Array<TImport>;
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/transformers.d.ts
|
|
21
56
|
/**
|
|
22
|
-
*
|
|
57
|
+
* Replaces a discriminator property's schema with a string enum of allowed values.
|
|
58
|
+
*
|
|
59
|
+
* If `node` is not an object schema, or if the property does not exist, the input
|
|
60
|
+
* node is returned as-is.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const schema = createSchema({
|
|
65
|
+
* type: 'object',
|
|
66
|
+
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
|
|
67
|
+
* })
|
|
68
|
+
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
|
|
69
|
+
* ```
|
|
23
70
|
*/
|
|
24
|
-
declare
|
|
71
|
+
declare function setDiscriminatorEnum({
|
|
72
|
+
node,
|
|
73
|
+
propertyName,
|
|
74
|
+
values,
|
|
75
|
+
enumName
|
|
76
|
+
}: {
|
|
77
|
+
node: SchemaNode;
|
|
78
|
+
propertyName: string;
|
|
79
|
+
values: Array<string>;
|
|
80
|
+
enumName?: string;
|
|
81
|
+
}): SchemaNode;
|
|
25
82
|
/**
|
|
26
|
-
*
|
|
83
|
+
* Merges adjacent anonymous object members into a single anonymous object member.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* const merged = mergeAdjacentObjects([
|
|
88
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
|
|
89
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
|
|
90
|
+
* ])
|
|
91
|
+
* ```
|
|
27
92
|
*/
|
|
28
|
-
declare
|
|
93
|
+
declare function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode>;
|
|
29
94
|
/**
|
|
30
|
-
*
|
|
95
|
+
* Removes enum members that are covered by broader scalar primitives in the same union.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* const simplified = simplifyUnion([
|
|
100
|
+
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
|
|
101
|
+
* createSchema({ type: 'string' }),
|
|
102
|
+
* ])
|
|
103
|
+
* // keeps only string member
|
|
104
|
+
* ```
|
|
31
105
|
*/
|
|
32
|
-
declare
|
|
106
|
+
declare function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode>;
|
|
107
|
+
declare function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode;
|
|
33
108
|
//#endregion
|
|
34
109
|
//#region src/utils.d.ts
|
|
35
110
|
/**
|
|
36
|
-
* Returns
|
|
111
|
+
* Returns a merged schema view for a ref node, combining the resolved `node.schema`
|
|
112
|
+
* (base from the referenced definition) with any usage-site sibling fields set directly
|
|
113
|
+
* on the ref node (description, readOnly, nullable, deprecated, etc.).
|
|
114
|
+
*
|
|
115
|
+
* Usage-site fields take precedence over the resolved schema's own fields when both are defined.
|
|
116
|
+
*
|
|
117
|
+
* For non-ref nodes the node itself is returned unchanged.
|
|
118
|
+
*/
|
|
119
|
+
declare function syncSchemaRef(node: SchemaNode): SchemaNode;
|
|
120
|
+
/**
|
|
121
|
+
* Returns `true` when a schema is emitted as a plain `string` type.
|
|
37
122
|
*
|
|
38
123
|
* - `string`, `uuid`, `email`, `url`, `datetime` are always plain strings.
|
|
39
124
|
* - `date` and `time` are plain strings when their `representation` is `'string'` rather than `'date'`.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* isStringType(createSchema({ type: 'uuid' })) // true
|
|
129
|
+
* isStringType(createSchema({ type: 'date', representation: 'date' })) // false
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
declare function isStringType(node: SchemaNode): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Applies casing rules to parameter names and returns a new parameter array.
|
|
135
|
+
*
|
|
136
|
+
* The input array is not mutated.
|
|
137
|
+
* If `casing` is not set, the original array is returned unchanged.
|
|
138
|
+
*
|
|
139
|
+
* Use this before passing parameters to schema builders so that property keys
|
|
140
|
+
* in generated output match the desired casing while preserving
|
|
141
|
+
* `OperationNode.parameters` for other consumers.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* const params = [createParameter({ name: 'pet_id', in: 'query', schema: createSchema({ type: 'string' }) })]
|
|
146
|
+
* const cased = caseParams(params, 'camelcase')
|
|
147
|
+
* // cased[0].name === 'petId'
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
declare function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
|
|
151
|
+
/**
|
|
152
|
+
* Creates a single-property object schema used as a discriminator literal.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* createDiscriminantNode({ propertyName: 'type', value: 'dog' })
|
|
157
|
+
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
declare function createDiscriminantNode({
|
|
161
|
+
propertyName,
|
|
162
|
+
value
|
|
163
|
+
}: {
|
|
164
|
+
propertyName: string;
|
|
165
|
+
value: string;
|
|
166
|
+
}): SchemaNode;
|
|
167
|
+
/**
|
|
168
|
+
* Resolver interface for {@link createOperationParams}.
|
|
169
|
+
*
|
|
170
|
+
* `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.
|
|
171
|
+
*/
|
|
172
|
+
type OperationParamsResolver = {
|
|
173
|
+
/**
|
|
174
|
+
* Resolves the type name for an individual parameter.
|
|
175
|
+
*
|
|
176
|
+
* @example Individual path parameter name
|
|
177
|
+
* `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
|
|
178
|
+
*/
|
|
179
|
+
resolveParamName(node: OperationNode, param: ParameterNode): string;
|
|
180
|
+
/**
|
|
181
|
+
* Resolves the request body type name.
|
|
182
|
+
*
|
|
183
|
+
* @example Request body type name
|
|
184
|
+
* `resolver.resolveDataName(node) // → 'CreatePetData'`
|
|
185
|
+
*/
|
|
186
|
+
resolveDataName(node: OperationNode): string;
|
|
187
|
+
/**
|
|
188
|
+
* Resolves the grouped path parameters type name.
|
|
189
|
+
* When the return value equals `resolveParamName`, no indexed access is emitted.
|
|
190
|
+
*
|
|
191
|
+
* @example Grouped path params type name
|
|
192
|
+
* `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
|
|
193
|
+
*/
|
|
194
|
+
resolvePathParamsName(node: OperationNode, param: ParameterNode): string;
|
|
195
|
+
/**
|
|
196
|
+
* Resolves the grouped query parameters type name.
|
|
197
|
+
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
|
|
198
|
+
*
|
|
199
|
+
* @example Grouped query params type name
|
|
200
|
+
* `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
|
|
201
|
+
*/
|
|
202
|
+
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string;
|
|
203
|
+
/**
|
|
204
|
+
* Resolves the grouped header parameters type name.
|
|
205
|
+
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
|
|
206
|
+
*
|
|
207
|
+
* @example Grouped header params type name
|
|
208
|
+
* `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
|
|
209
|
+
*/
|
|
210
|
+
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string;
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Options for {@link createOperationParams}.
|
|
214
|
+
*/
|
|
215
|
+
type CreateOperationParamsOptions = {
|
|
216
|
+
/**
|
|
217
|
+
* How all operation parameters are grouped in the function signature.
|
|
218
|
+
* - `'object'` wraps all params into a single destructured object `{ petId, data, params }`
|
|
219
|
+
* - `'inline'` emits each param category as a separate top-level parameter
|
|
220
|
+
*/
|
|
221
|
+
paramsType: 'object' | 'inline';
|
|
222
|
+
/**
|
|
223
|
+
* How path parameters are emitted when `paramsType` is `'inline'`.
|
|
224
|
+
* - `'object'` groups them as `{ petId, storeId }: PathParams`
|
|
225
|
+
* - `'inline'` spreads them as individual parameters `petId: string, storeId: string`
|
|
226
|
+
* - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`
|
|
227
|
+
*/
|
|
228
|
+
pathParamsType: 'object' | 'inline' | 'inlineSpread';
|
|
229
|
+
/**
|
|
230
|
+
* Converts parameter names to camelCase before output.
|
|
231
|
+
*/
|
|
232
|
+
paramsCasing?: 'camelcase';
|
|
233
|
+
/**
|
|
234
|
+
* Resolver for parameter and request body type names.
|
|
235
|
+
* Pass `ResolverTs` from `@kubb/plugin-ts` directly.
|
|
236
|
+
* When omitted, falls back to the schema primitive or `'unknown'`.
|
|
237
|
+
*/
|
|
238
|
+
resolver?: OperationParamsResolver;
|
|
239
|
+
/**
|
|
240
|
+
* Default value for the path parameters binding when `pathParamsType` is `'object'`.
|
|
241
|
+
* Falls back to `'{}'` when all path params are optional.
|
|
242
|
+
*/
|
|
243
|
+
pathParamsDefault?: string;
|
|
244
|
+
/**
|
|
245
|
+
* Extra parameters appended after the standard operation parameters.
|
|
246
|
+
*
|
|
247
|
+
* @example Plugin-specific trailing parameter
|
|
248
|
+
* ```ts
|
|
249
|
+
* extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
extraParams?: Array<FunctionParameterNode | ParameterGroupNode>;
|
|
253
|
+
/**
|
|
254
|
+
* Override the default parameter names used for body, query, header, and rest-path groups.
|
|
255
|
+
*
|
|
256
|
+
* Useful when targeting languages or frameworks with different naming conventions.
|
|
257
|
+
*
|
|
258
|
+
* @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }
|
|
259
|
+
*/
|
|
260
|
+
paramNames?: {
|
|
261
|
+
/**
|
|
262
|
+
* Name for the request body parameter.
|
|
263
|
+
* @default 'data'
|
|
264
|
+
*/
|
|
265
|
+
data?: string;
|
|
266
|
+
/**
|
|
267
|
+
* Name for the query parameters group parameter.
|
|
268
|
+
* @default 'params'
|
|
269
|
+
*/
|
|
270
|
+
params?: string;
|
|
271
|
+
/**
|
|
272
|
+
* Name for the header parameters group parameter.
|
|
273
|
+
* @default 'headers'
|
|
274
|
+
*/
|
|
275
|
+
headers?: string;
|
|
276
|
+
/**
|
|
277
|
+
* Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.
|
|
278
|
+
* @default 'pathParams'
|
|
279
|
+
*/
|
|
280
|
+
path?: string;
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* Applies a uniform transformation to every resolved type name before it is used
|
|
284
|
+
* in a parameter node. Use this for framework-level type wrappers.
|
|
285
|
+
*
|
|
286
|
+
* @example Vue Query — wrap every parameter type with `MaybeRefOrGetter`
|
|
287
|
+
* `typeWrapper: (t) => \`MaybeRefOrGetter<${t}>\``
|
|
288
|
+
*/
|
|
289
|
+
typeWrapper?: (type: string) => string;
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Converts an {@link OperationNode} into a {@link FunctionParametersNode}.
|
|
293
|
+
*
|
|
294
|
+
* Centralizes the per-plugin `getParams()` pattern. Provide a `resolver` for
|
|
295
|
+
* type resolution and `extraParams` for plugin-specific trailing parameters.
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```ts
|
|
299
|
+
* const params = createOperationParams(node, {
|
|
300
|
+
* paramsType: 'inline',
|
|
301
|
+
* pathParamsType: 'inline',
|
|
302
|
+
* resolver: tsResolver,
|
|
303
|
+
* extraParams: [createFunctionParameter({ name: 'options', type: createTypeNode({ variant: 'reference', name: 'Partial<RequestOptions>' }), default: '{}' })],
|
|
304
|
+
* })
|
|
305
|
+
* ```
|
|
40
306
|
*/
|
|
41
|
-
declare function
|
|
307
|
+
declare function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode;
|
|
42
308
|
//#endregion
|
|
43
|
-
export {
|
|
309
|
+
export { type OperationParamsResolver, type ParserOptions, type Printer, type PrinterFactoryOptions, type PrinterPartial, type ScalarPrimitive, caseParams, childName, collect, collectImports, composeTransformers, createDiscriminantNode, createFunctionParameter, createFunctionParameters, createOperation, createOperationParams, createParameter, createParameterGroup, createPrinterFactory, createProperty, createResponse, createRoot, createSchema, createTypeNode, definePrinter, enumPropName, extractRefName, findDiscriminator, httpMethods, isOperationNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
44
310
|
//# sourceMappingURL=index.d.ts.map
|