@kubb/ast 5.0.0-alpha.22 → 5.0.0-alpha.23
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 +471 -441
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +159 -160
- package/dist/index.js +467 -426
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/{visitor-COwfCgLK.d.ts → visitor-DsdLcLjR.d.ts} +206 -117
- package/package.json +1 -1
- package/src/constants.ts +15 -2
- package/src/factory.ts +69 -49
- package/src/guards.ts +3 -3
- package/src/index.ts +13 -20
- package/src/nodes/base.ts +2 -1
- package/src/nodes/function.ts +104 -34
- package/src/nodes/index.ts +1 -1
- package/src/nodes/operation.ts +7 -2
- package/src/nodes/schema.ts +3 -3
- package/src/{printers/printer.ts → printer.ts} +1 -1
- package/src/transformers.ts +2 -42
- package/src/types.ts +3 -2
- package/src/utils.ts +364 -13
- package/src/visitor.ts +6 -4
- package/src/printers/functionPrinter.ts +0 -196
- package/src/printers/index.ts +0 -3
package/src/factory.ts
CHANGED
|
@@ -2,16 +2,32 @@ import type { InferSchemaNode } from './infer.ts'
|
|
|
2
2
|
import type {
|
|
3
3
|
FunctionParameterNode,
|
|
4
4
|
FunctionParametersNode,
|
|
5
|
-
ObjectBindingParameterNode,
|
|
6
5
|
ObjectSchemaNode,
|
|
7
6
|
OperationNode,
|
|
7
|
+
ParameterGroupNode,
|
|
8
8
|
ParameterNode,
|
|
9
9
|
PropertyNode,
|
|
10
10
|
ResponseNode,
|
|
11
11
|
RootNode,
|
|
12
12
|
SchemaNode,
|
|
13
|
+
TypeNode,
|
|
13
14
|
} from './nodes/index.ts'
|
|
14
|
-
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Syncs property/parameter schema optionality flags from `required` and `schema.nullable`.
|
|
18
|
+
*
|
|
19
|
+
* - `optional` is set for non-required, non-nullable schemas.
|
|
20
|
+
* - `nullish` is set for non-required, nullable schemas.
|
|
21
|
+
*/
|
|
22
|
+
export function syncOptionality(schema: SchemaNode, required: boolean): SchemaNode {
|
|
23
|
+
const nullable = schema.nullable ?? false
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
...schema,
|
|
27
|
+
optional: !required && !nullable ? true : undefined,
|
|
28
|
+
nullish: !required && nullable ? true : undefined,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
15
31
|
|
|
16
32
|
/**
|
|
17
33
|
* Distributive `Omit` that preserves each member of a union.
|
|
@@ -156,7 +172,7 @@ export function createProperty(props: Pick<PropertyNode, 'name' | 'schema'> & Pa
|
|
|
156
172
|
...props,
|
|
157
173
|
kind: 'Property',
|
|
158
174
|
required,
|
|
159
|
-
schema: syncOptionality(
|
|
175
|
+
schema: syncOptionality(props.schema, required),
|
|
160
176
|
}
|
|
161
177
|
}
|
|
162
178
|
|
|
@@ -194,7 +210,7 @@ export function createParameter(
|
|
|
194
210
|
...props,
|
|
195
211
|
kind: 'Parameter',
|
|
196
212
|
required,
|
|
197
|
-
schema: syncOptionality(
|
|
213
|
+
schema: syncOptionality(props.schema, required),
|
|
198
214
|
}
|
|
199
215
|
}
|
|
200
216
|
|
|
@@ -219,33 +235,6 @@ export function createResponse(
|
|
|
219
235
|
}
|
|
220
236
|
}
|
|
221
237
|
|
|
222
|
-
/**
|
|
223
|
-
* Creates a single-property object schema used as a discriminator literal.
|
|
224
|
-
*
|
|
225
|
-
* @example
|
|
226
|
-
* ```ts
|
|
227
|
-
* createDiscriminantNode({ propertyName: 'type', value: 'dog' })
|
|
228
|
-
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
export function createDiscriminantNode({ propertyName, value }: { propertyName: string; value: string }): SchemaNode {
|
|
232
|
-
return createSchema({
|
|
233
|
-
type: 'object',
|
|
234
|
-
primitive: 'object',
|
|
235
|
-
properties: [
|
|
236
|
-
createProperty({
|
|
237
|
-
name: propertyName,
|
|
238
|
-
schema: createSchema({
|
|
239
|
-
type: 'enum',
|
|
240
|
-
primitive: 'string',
|
|
241
|
-
enumValues: [value],
|
|
242
|
-
}),
|
|
243
|
-
required: true,
|
|
244
|
-
}),
|
|
245
|
-
],
|
|
246
|
-
})
|
|
247
|
-
}
|
|
248
|
-
|
|
249
238
|
/**
|
|
250
239
|
* Creates a `FunctionParameterNode`.
|
|
251
240
|
*
|
|
@@ -253,24 +242,24 @@ export function createDiscriminantNode({ propertyName, value }: { propertyName:
|
|
|
253
242
|
*
|
|
254
243
|
* @example Required typed param
|
|
255
244
|
* ```ts
|
|
256
|
-
* createFunctionParameter({ name: 'petId', type: 'string' })
|
|
245
|
+
* createFunctionParameter({ name: 'petId', type: createTypeNode({ variant: 'reference', name: 'string' }) })
|
|
257
246
|
* // → petId: string
|
|
258
247
|
* ```
|
|
259
248
|
*
|
|
260
249
|
* @example Optional param
|
|
261
250
|
* ```ts
|
|
262
|
-
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
|
|
251
|
+
* createFunctionParameter({ name: 'params', type: createTypeNode({ variant: 'reference', name: 'QueryParams' }), optional: true })
|
|
263
252
|
* // → params?: QueryParams
|
|
264
253
|
* ```
|
|
265
254
|
*
|
|
266
255
|
* @example Param with default (implicitly optional; cannot combine with `optional: true`)
|
|
267
256
|
* ```ts
|
|
268
|
-
* createFunctionParameter({ name: 'config', type: 'RequestConfig', default: '{}' })
|
|
257
|
+
* createFunctionParameter({ name: 'config', type: createTypeNode({ variant: 'reference', name: 'RequestConfig' }), default: '{}' })
|
|
269
258
|
* // → config: RequestConfig = {}
|
|
270
259
|
* ```
|
|
271
260
|
*/
|
|
272
261
|
export function createFunctionParameter(
|
|
273
|
-
props: { name: string; type?:
|
|
262
|
+
props: { name: string; type?: TypeNode; rest?: boolean } & ({ optional: true; default?: never } | { optional?: false; default?: string }),
|
|
274
263
|
): FunctionParameterNode {
|
|
275
264
|
return {
|
|
276
265
|
optional: false,
|
|
@@ -280,14 +269,45 @@ export function createFunctionParameter(
|
|
|
280
269
|
}
|
|
281
270
|
|
|
282
271
|
/**
|
|
283
|
-
* Creates
|
|
272
|
+
* Creates a {@link TypeNode} representing a language-agnostic structured type expression.
|
|
273
|
+
*
|
|
274
|
+
* Use `variant: 'struct'` for inline anonymous types and `variant: 'member'` for a single
|
|
275
|
+
* named field accessed from a group type. Each language's printer renders the variant
|
|
276
|
+
* into its own syntax (TypeScript, Python, C#, Kotlin, …).
|
|
277
|
+
*
|
|
278
|
+
* @example Reference type (TypeScript: `QueryParams`)
|
|
279
|
+
* ```ts
|
|
280
|
+
* createTypeNode({ variant: 'reference', name: 'QueryParams' })
|
|
281
|
+
* ```
|
|
282
|
+
*
|
|
283
|
+
* @example Struct type (TypeScript: `{ petId: string }`)
|
|
284
|
+
* ```ts
|
|
285
|
+
* createTypeNode({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createTypeNode({ variant: 'reference', name: 'string' }) }] })
|
|
286
|
+
* ```
|
|
287
|
+
*
|
|
288
|
+
* @example Member type (TypeScript: `DeletePetPathParams['petId']`)
|
|
289
|
+
* ```ts
|
|
290
|
+
* createTypeNode({ variant: 'member', base: 'DeletePetPathParams', key: 'petId' })
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
export function createTypeNode(
|
|
294
|
+
props:
|
|
295
|
+
| { variant: 'reference'; name: string }
|
|
296
|
+
| { variant: 'struct'; properties: Array<{ name: string; optional: boolean; type: TypeNode }> }
|
|
297
|
+
| { variant: 'member'; base: string; key: string },
|
|
298
|
+
): TypeNode {
|
|
299
|
+
return { ...props, kind: 'Type' } as TypeNode
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Creates a `ParameterGroupNode` representing a group of related parameters treated as a unit.
|
|
284
304
|
*
|
|
285
|
-
* @example
|
|
305
|
+
* @example Grouped param (TypeScript declaration)
|
|
286
306
|
* ```ts
|
|
287
|
-
*
|
|
307
|
+
* createParameterGroup({
|
|
288
308
|
* properties: [
|
|
289
|
-
* createFunctionParameter({ name: 'id', type: 'string', optional: false }),
|
|
290
|
-
* createFunctionParameter({ name: 'name', type: 'string', optional: true }),
|
|
309
|
+
* createFunctionParameter({ name: 'id', type: createTypeNode({ variant: 'reference', name: 'string' }), optional: false }),
|
|
310
|
+
* createFunctionParameter({ name: 'name', type: createTypeNode({ variant: 'reference', name: 'string' }), optional: true }),
|
|
291
311
|
* ],
|
|
292
312
|
* default: '{}',
|
|
293
313
|
* })
|
|
@@ -295,22 +315,22 @@ export function createFunctionParameter(
|
|
|
295
315
|
* // call → { id, name }
|
|
296
316
|
* ```
|
|
297
317
|
*
|
|
298
|
-
* @example Inline
|
|
318
|
+
* @example Inline (spread) — children emitted as individual top-level parameters
|
|
299
319
|
* ```ts
|
|
300
|
-
*
|
|
301
|
-
* properties: [createFunctionParameter({ name: 'petId', type: 'string', optional: false })],
|
|
320
|
+
* createParameterGroup({
|
|
321
|
+
* properties: [createFunctionParameter({ name: 'petId', type: createTypeNode({ variant: 'reference', name: 'string' }), optional: false })],
|
|
302
322
|
* inline: true,
|
|
303
323
|
* })
|
|
304
324
|
* // declaration → petId: string
|
|
305
325
|
* // call → petId
|
|
306
326
|
* ```
|
|
307
327
|
*/
|
|
308
|
-
export function
|
|
309
|
-
props: Pick<
|
|
310
|
-
):
|
|
328
|
+
export function createParameterGroup(
|
|
329
|
+
props: Pick<ParameterGroupNode, 'properties'> & Partial<Omit<ParameterGroupNode, 'kind' | 'properties'>>,
|
|
330
|
+
): ParameterGroupNode {
|
|
311
331
|
return {
|
|
312
332
|
...props,
|
|
313
|
-
kind: '
|
|
333
|
+
kind: 'ParameterGroup',
|
|
314
334
|
}
|
|
315
335
|
}
|
|
316
336
|
|
|
@@ -321,8 +341,8 @@ export function createObjectBindingParameter(
|
|
|
321
341
|
* ```ts
|
|
322
342
|
* createFunctionParameters({
|
|
323
343
|
* params: [
|
|
324
|
-
* createFunctionParameter({ name: 'petId', type: 'string', optional: false }),
|
|
325
|
-
* createFunctionParameter({ name: 'config', type: 'RequestConfig', optional: false, default: '{}' }),
|
|
344
|
+
* createFunctionParameter({ name: 'petId', type: createTypeNode({ variant: 'reference', name: 'string' }), optional: false }),
|
|
345
|
+
* createFunctionParameter({ name: 'config', type: createTypeNode({ variant: 'reference', name: 'RequestConfig' }), optional: false, default: '{}' }),
|
|
326
346
|
* ],
|
|
327
347
|
* })
|
|
328
348
|
* ```
|
package/src/guards.ts
CHANGED
|
@@ -3,8 +3,8 @@ import type {
|
|
|
3
3
|
FunctionParametersNode,
|
|
4
4
|
Node,
|
|
5
5
|
NodeKind,
|
|
6
|
-
ObjectBindingParameterNode,
|
|
7
6
|
OperationNode,
|
|
7
|
+
ParameterGroupNode,
|
|
8
8
|
ParameterNode,
|
|
9
9
|
PropertyNode,
|
|
10
10
|
ResponseNode,
|
|
@@ -87,9 +87,9 @@ export const isResponseNode = isKind<ResponseNode>('Response')
|
|
|
87
87
|
export const isFunctionParameterNode = isKind<FunctionParameterNode>('FunctionParameter')
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
|
-
* Returns `true` when the input is
|
|
90
|
+
* Returns `true` when the input is a `ParameterGroupNode`.
|
|
91
91
|
*/
|
|
92
|
-
export const
|
|
92
|
+
export const isParameterGroupNode = isKind<ParameterGroupNode>('ParameterGroup')
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
95
|
* Returns `true` when the input is a `FunctionParametersNode`.
|
package/src/index.ts
CHANGED
|
@@ -1,32 +1,25 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export type { ScalarPrimitive } from './constants.ts'
|
|
2
|
+
export { httpMethods, isScalarPrimitive, mediaTypes, schemaTypes } from './constants.ts'
|
|
2
3
|
export {
|
|
3
|
-
createDiscriminantNode,
|
|
4
4
|
createFunctionParameter,
|
|
5
5
|
createFunctionParameters,
|
|
6
|
-
createObjectBindingParameter,
|
|
7
6
|
createOperation,
|
|
8
7
|
createParameter,
|
|
8
|
+
createParameterGroup,
|
|
9
9
|
createProperty,
|
|
10
10
|
createResponse,
|
|
11
11
|
createRoot,
|
|
12
12
|
createSchema,
|
|
13
|
+
createTypeNode,
|
|
14
|
+
syncOptionality,
|
|
13
15
|
} from './factory.ts'
|
|
14
|
-
export {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
isParameterNode,
|
|
20
|
-
isPropertyNode,
|
|
21
|
-
isResponseNode,
|
|
22
|
-
isRootNode,
|
|
23
|
-
isSchemaNode,
|
|
24
|
-
narrowSchema,
|
|
25
|
-
} from './guards.ts'
|
|
26
|
-
export type { InferSchema, InferSchemaNode, ParserOptions } from './infer.ts'
|
|
27
|
-
export { defineFunctionPrinter, definePrinter, functionPrinter } from './printers/index.ts'
|
|
28
|
-
export { buildRefMap, extractRefName, refMapToObject, resolveRef } from './refs.ts'
|
|
16
|
+
export { isOperationNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
17
|
+
export type { ParserOptions } from './infer.ts'
|
|
18
|
+
export type { PrinterFactoryOptions } from './printer.ts'
|
|
19
|
+
export { createPrinterFactory, definePrinter } from './printer.ts'
|
|
20
|
+
export { extractRefName } from './refs.ts'
|
|
29
21
|
export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
|
|
30
|
-
export { mergeAdjacentObjects,
|
|
31
|
-
export {
|
|
22
|
+
export { mergeAdjacentObjects, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
|
|
23
|
+
export type { OperationParamsResolver } from './utils.ts'
|
|
24
|
+
export { caseParams, createDiscriminantNode, createOperationParams, isStringType } from './utils.ts'
|
|
32
25
|
export { collect, composeTransformers, transform, walk } from './visitor.ts'
|
package/src/nodes/base.ts
CHANGED
package/src/nodes/function.ts
CHANGED
|
@@ -1,15 +1,69 @@
|
|
|
1
1
|
import type { BaseNode } from './base.ts'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* AST node representing a language-agnostic type expression produced during parameter resolution.
|
|
5
|
+
* Each language printer renders the variant into its own syntax.
|
|
6
|
+
*
|
|
7
|
+
* - `struct` — an inline anonymous type grouping named fields.
|
|
8
|
+
* TypeScript renders as `{ petId: string; name?: string }`, Python as a `TypedDict` reference.
|
|
9
|
+
* - `member` — a single named field accessed from a named group type.
|
|
10
|
+
* TypeScript renders as `PathParams['petId']`, C# as `PathParams.PetId`.
|
|
11
|
+
*/
|
|
12
|
+
export type TypeNode = BaseNode & {
|
|
13
|
+
/**
|
|
14
|
+
* Node kind.
|
|
15
|
+
*/
|
|
16
|
+
kind: 'Type'
|
|
17
|
+
} & (
|
|
18
|
+
| {
|
|
19
|
+
/**
|
|
20
|
+
* Reference variant — a plain type name or identifier.
|
|
21
|
+
* TypeScript renders as-is, e.g. `string`, `QueryParams`, `Partial<Config>`.
|
|
22
|
+
*/
|
|
23
|
+
variant: 'reference'
|
|
24
|
+
/**
|
|
25
|
+
* The full type name string, e.g. `'string'`, `'QueryParams'`, `'Partial<Config>'`.
|
|
26
|
+
*/
|
|
27
|
+
name: string
|
|
28
|
+
}
|
|
29
|
+
| {
|
|
30
|
+
/**
|
|
31
|
+
* Struct variant — an inline anonymous type grouping named fields.
|
|
32
|
+
* TypeScript renders as `{ key: Type; other?: OtherType }`.
|
|
33
|
+
*/
|
|
34
|
+
variant: 'struct'
|
|
35
|
+
/**
|
|
36
|
+
* Properties of the struct type.
|
|
37
|
+
*/
|
|
38
|
+
properties: Array<{ name: string; optional: boolean; type: TypeNode }>
|
|
39
|
+
}
|
|
40
|
+
| {
|
|
41
|
+
/**
|
|
42
|
+
* Member variant — a single named field accessed from a group type.
|
|
43
|
+
* TypeScript renders as `Base['key']`.
|
|
44
|
+
*/
|
|
45
|
+
variant: 'member'
|
|
46
|
+
/**
|
|
47
|
+
* Base type name, e.g. `'DeletePetPathParams'`.
|
|
48
|
+
*/
|
|
49
|
+
base: string
|
|
50
|
+
/**
|
|
51
|
+
* The field name to access, e.g. `'petId'`.
|
|
52
|
+
*/
|
|
53
|
+
key: string
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
|
|
3
57
|
/**
|
|
4
58
|
* AST node for one function parameter.
|
|
5
59
|
*
|
|
6
60
|
* @example Required parameter
|
|
7
61
|
* `name: Type`
|
|
8
62
|
*
|
|
9
|
-
* @example Optional
|
|
63
|
+
* @example Optional parameter
|
|
10
64
|
* `name?: Type`
|
|
11
65
|
*
|
|
12
|
-
* @example Parameter with default
|
|
66
|
+
* @example Parameter with default value
|
|
13
67
|
* `name: Type = defaultValue`
|
|
14
68
|
*
|
|
15
69
|
* @example Rest parameter
|
|
@@ -25,73 +79,89 @@ export type FunctionParameterNode = BaseNode & {
|
|
|
25
79
|
*/
|
|
26
80
|
name: string
|
|
27
81
|
/**
|
|
28
|
-
*
|
|
29
|
-
* Omit for untyped
|
|
82
|
+
* Type annotation as a structured {@link TypeNode}.
|
|
83
|
+
* Omit for untyped output.
|
|
84
|
+
*
|
|
85
|
+
* @example Reference type node
|
|
86
|
+
* `{ kind: 'Type', variant: 'reference', name: 'string' }` → `petId: string`
|
|
87
|
+
*
|
|
88
|
+
* @example Struct type node
|
|
89
|
+
* `{ kind: 'Type', variant: 'struct', properties: [...] }` → `{ key: Type; other?: OtherType }`
|
|
90
|
+
*
|
|
91
|
+
* @example Member type node
|
|
92
|
+
* `{ kind: 'Type', variant: 'member', base: 'PathParams', key: 'petId' }` → `PathParams['petId']`
|
|
30
93
|
*/
|
|
31
|
-
type?:
|
|
94
|
+
type?: TypeNode
|
|
32
95
|
/**
|
|
33
|
-
* When `true` the parameter is emitted as a rest parameter
|
|
34
|
-
*
|
|
96
|
+
* When `true` the parameter is emitted as a rest parameter.
|
|
97
|
+
*
|
|
98
|
+
* @example Rest parameter
|
|
99
|
+
* `...name: Type[]`
|
|
35
100
|
*/
|
|
36
101
|
rest?: boolean
|
|
37
102
|
} /**
|
|
38
|
-
* Optional parameter
|
|
39
|
-
* Cannot be combined with `default` because defaulted
|
|
40
|
-
* @example `name?: Type`
|
|
103
|
+
* Optional parameter — rendered with `?` and may be omitted by the caller.
|
|
104
|
+
* Cannot be combined with `default` because a defaulted parameter is already optional.
|
|
41
105
|
*/ & (
|
|
42
106
|
| { optional: true; default?: never }
|
|
43
107
|
/**
|
|
44
|
-
* Required parameter, or parameter with a default value.
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
108
|
+
* Required parameter, or a parameter with a default value.
|
|
109
|
+
*
|
|
110
|
+
* @example Required
|
|
111
|
+
* `name: Type`
|
|
112
|
+
*
|
|
113
|
+
* @example With default
|
|
114
|
+
* `name: Type = default`
|
|
47
115
|
*/
|
|
48
116
|
| { optional?: false; default?: string }
|
|
49
117
|
)
|
|
50
118
|
|
|
51
119
|
/**
|
|
52
|
-
* AST node for
|
|
120
|
+
* AST node for a group of related function parameters treated as a single unit.
|
|
53
121
|
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
122
|
+
* Each language printer decides how to render this group:
|
|
123
|
+
* - TypeScript/JS: destructured object `{ key1, key2 }: { key1: Type1; key2: Type2 } = {}`
|
|
124
|
+
* - Python: keyword-only args or a typed dict parameter
|
|
125
|
+
* - C# / Kotlin: named record / data-class parameter
|
|
56
126
|
*
|
|
57
|
-
*
|
|
127
|
+
* When `inline` is `true`, the group is spread as individual top-level parameters
|
|
128
|
+
* rather than wrapped in a single grouped construct.
|
|
58
129
|
*
|
|
59
|
-
* @example
|
|
130
|
+
* @example Grouped destructuring
|
|
60
131
|
* `{ id, name }: { id: string; name: string } = {}`
|
|
61
132
|
*
|
|
62
|
-
* @example Inline (spread
|
|
133
|
+
* @example Inline (spread as individual parameters)
|
|
63
134
|
* `id: string, name: string`
|
|
64
135
|
*/
|
|
65
|
-
export type
|
|
136
|
+
export type ParameterGroupNode = BaseNode & {
|
|
66
137
|
/**
|
|
67
138
|
* Node kind.
|
|
68
139
|
*/
|
|
69
|
-
kind: '
|
|
140
|
+
kind: 'ParameterGroup'
|
|
70
141
|
/**
|
|
71
|
-
* The individual parameters that form the
|
|
72
|
-
* Rendered as
|
|
142
|
+
* The individual parameters that form the group.
|
|
143
|
+
* Rendered as a destructured object or spread inline when `inline` is `true`.
|
|
73
144
|
*/
|
|
74
145
|
properties: Array<FunctionParameterNode>
|
|
75
146
|
/**
|
|
76
|
-
* Optional type
|
|
77
|
-
* When absent,
|
|
147
|
+
* Optional explicit type annotation for the whole group.
|
|
148
|
+
* When absent, printers auto-compute it from `properties`.
|
|
78
149
|
*/
|
|
79
|
-
type?:
|
|
150
|
+
type?: TypeNode
|
|
80
151
|
/**
|
|
81
152
|
* When `true`, `properties` are emitted as individual top-level parameters instead of
|
|
82
|
-
* being wrapped in a
|
|
153
|
+
* being wrapped in a single grouped construct.
|
|
83
154
|
*
|
|
84
|
-
* Equivalent to `mode: 'inlineSpread'` in the legacy `FunctionParams` API.
|
|
85
155
|
* @default false
|
|
86
156
|
*/
|
|
87
157
|
inline?: boolean
|
|
88
158
|
/**
|
|
89
|
-
* Whether the
|
|
159
|
+
* Whether the group as a whole is optional.
|
|
90
160
|
* If omitted, printers infer this from child properties.
|
|
91
161
|
*/
|
|
92
162
|
optional?: boolean
|
|
93
163
|
/**
|
|
94
|
-
* Default value for the
|
|
164
|
+
* Default value for the group, written verbatim after `=`.
|
|
95
165
|
* Commonly `'{}'` to allow omitting the argument entirely.
|
|
96
166
|
*/
|
|
97
167
|
default?: string
|
|
@@ -117,15 +187,15 @@ export type FunctionParametersNode = BaseNode & {
|
|
|
117
187
|
/**
|
|
118
188
|
* Ordered parameter nodes.
|
|
119
189
|
*/
|
|
120
|
-
params:
|
|
190
|
+
params: ReadonlyArray<FunctionParameterNode | ParameterGroupNode>
|
|
121
191
|
}
|
|
122
192
|
|
|
123
193
|
/**
|
|
124
|
-
* The
|
|
194
|
+
* The four function-signature AST node variants.
|
|
125
195
|
*/
|
|
126
|
-
export type FunctionNode = FunctionParameterNode |
|
|
196
|
+
export type FunctionNode = FunctionParameterNode | ParameterGroupNode | FunctionParametersNode | TypeNode
|
|
127
197
|
|
|
128
198
|
/**
|
|
129
199
|
* Handler map keys — one per `FunctionNode` kind.
|
|
130
200
|
*/
|
|
131
|
-
export type FunctionNodeType = 'functionParameter' | '
|
|
201
|
+
export type FunctionNodeType = 'functionParameter' | 'parameterGroup' | 'functionParameters' | 'type'
|
package/src/nodes/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { RootNode } from './root.ts'
|
|
|
7
7
|
import type { SchemaNode } from './schema.ts'
|
|
8
8
|
|
|
9
9
|
export type { BaseNode, NodeKind } from './base.ts'
|
|
10
|
-
export type { FunctionNode, FunctionNodeType, FunctionParameterNode, FunctionParametersNode,
|
|
10
|
+
export type { FunctionNode, FunctionNodeType, FunctionParameterNode, FunctionParametersNode, ParameterGroupNode, TypeNode } from './function.ts'
|
|
11
11
|
export type { HttpStatusCode, MediaType, StatusCode } from './http.ts'
|
|
12
12
|
export type { HttpMethod, OperationNode } from './operation.ts'
|
|
13
13
|
export type { ParameterLocation, ParameterNode } from './parameter.ts'
|
package/src/nodes/operation.ts
CHANGED
|
@@ -35,8 +35,8 @@ export type OperationNode = BaseNode & {
|
|
|
35
35
|
*/
|
|
36
36
|
method: HttpMethod
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
38
|
+
* OpenAPI-style path string, for example `/pets/{petId}`.
|
|
39
|
+
* Path parameters retain the `{param}` notation from the original spec.
|
|
40
40
|
*/
|
|
41
41
|
path: string
|
|
42
42
|
/**
|
|
@@ -78,6 +78,11 @@ export type OperationNode = BaseNode & {
|
|
|
78
78
|
* Set when a referenced schema has `readOnly` fields that should be omitted in request types.
|
|
79
79
|
*/
|
|
80
80
|
keysToOmit?: Array<string>
|
|
81
|
+
/**
|
|
82
|
+
* Whether the request body is required (`requestBody.required: true` in the spec).
|
|
83
|
+
* When `false` or absent, the generated `data` parameter should be optional.
|
|
84
|
+
*/
|
|
85
|
+
required?: boolean
|
|
81
86
|
}
|
|
82
87
|
/**
|
|
83
88
|
* Operation responses.
|
package/src/nodes/schema.ts
CHANGED
|
@@ -475,11 +475,11 @@ export type ScalarSchemaNode = SchemaNodeBase & {
|
|
|
475
475
|
|
|
476
476
|
/**
|
|
477
477
|
* URL schema node.
|
|
478
|
-
* Can include an
|
|
478
|
+
* Can include an OpenAPI-style path template for template literal types.
|
|
479
479
|
*
|
|
480
480
|
* @example
|
|
481
481
|
* ```ts
|
|
482
|
-
* const urlSchema: UrlSchemaNode = { kind: 'Schema', type: 'url', path: '/pets
|
|
482
|
+
* const urlSchema: UrlSchemaNode = { kind: 'Schema', type: 'url', path: '/pets/{petId}' }
|
|
483
483
|
* ```
|
|
484
484
|
*/
|
|
485
485
|
export type UrlSchemaNode = SchemaNodeBase & {
|
|
@@ -488,7 +488,7 @@ export type UrlSchemaNode = SchemaNodeBase & {
|
|
|
488
488
|
*/
|
|
489
489
|
type: 'url'
|
|
490
490
|
/**
|
|
491
|
-
*
|
|
491
|
+
* OpenAPI-style path template, for example, `'/pets/{petId}'`.
|
|
492
492
|
*/
|
|
493
493
|
path?: string
|
|
494
494
|
}
|
package/src/transformers.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isScalarPrimitive } from './constants.ts'
|
|
2
2
|
import { createProperty, createSchema } from './factory.ts'
|
|
3
3
|
import { narrowSchema } from './guards.ts'
|
|
4
4
|
import type { SchemaNode } from './nodes/schema.ts'
|
|
5
5
|
import { enumPropName } from './resolvers.ts'
|
|
6
|
-
import { transform } from './visitor.ts'
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Replaces a discriminator property's schema with a string enum of allowed values.
|
|
@@ -108,9 +107,7 @@ export function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNo
|
|
|
108
107
|
* ```
|
|
109
108
|
*/
|
|
110
109
|
export function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode> {
|
|
111
|
-
const scalarPrimitives = new Set(
|
|
112
|
-
members.filter((member) => SCALAR_PRIMITIVE_TYPES.has(member.type as typeof SCALAR_PRIMITIVE_TYPES extends Set<infer T> ? T : never)).map((m) => m.type),
|
|
113
|
-
)
|
|
110
|
+
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))
|
|
114
111
|
|
|
115
112
|
if (!scalarPrimitives.size) {
|
|
116
113
|
return members
|
|
@@ -157,40 +154,3 @@ export function setEnumName(propNode: SchemaNode, parentName: string | null | un
|
|
|
157
154
|
|
|
158
155
|
return propNode
|
|
159
156
|
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Walks a schema tree and resolves `ref`/`enum` names through callbacks.
|
|
163
|
-
*/
|
|
164
|
-
export function resolveNames({
|
|
165
|
-
node,
|
|
166
|
-
nameMapping,
|
|
167
|
-
resolveName,
|
|
168
|
-
resolveEnumName,
|
|
169
|
-
}: {
|
|
170
|
-
node: SchemaNode
|
|
171
|
-
nameMapping: Map<string, string>
|
|
172
|
-
resolveName: (ref: string) => string | undefined
|
|
173
|
-
resolveEnumName?: (name: string) => string | undefined
|
|
174
|
-
}): SchemaNode {
|
|
175
|
-
return transform(node, {
|
|
176
|
-
schema(schemaNode) {
|
|
177
|
-
const schemaRef = narrowSchema(schemaNode, 'ref')
|
|
178
|
-
|
|
179
|
-
if (schemaRef && (schemaRef.ref || schemaRef.name)) {
|
|
180
|
-
const rawRef = schemaRef.ref ?? schemaRef.name!
|
|
181
|
-
const resolved = resolveName(nameMapping.get(rawRef) ?? rawRef)
|
|
182
|
-
if (resolved) {
|
|
183
|
-
return { ...schemaNode, name: resolved }
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const schemaEnum = narrowSchema(schemaNode, 'enum')
|
|
188
|
-
if (schemaEnum?.name) {
|
|
189
|
-
const resolved = (resolveEnumName ?? resolveName)(schemaEnum.name)
|
|
190
|
-
if (resolved) {
|
|
191
|
-
return { ...schemaNode, name: resolved }
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
},
|
|
195
|
-
}) as SchemaNode
|
|
196
|
-
}
|
package/src/types.ts
CHANGED
|
@@ -20,9 +20,9 @@ export type {
|
|
|
20
20
|
Node,
|
|
21
21
|
NodeKind,
|
|
22
22
|
NumberSchemaNode,
|
|
23
|
-
ObjectBindingParameterNode,
|
|
24
23
|
ObjectSchemaNode,
|
|
25
24
|
OperationNode,
|
|
25
|
+
ParameterGroupNode,
|
|
26
26
|
ParameterLocation,
|
|
27
27
|
ParameterNode,
|
|
28
28
|
PrimitiveSchemaType,
|
|
@@ -40,9 +40,10 @@ export type {
|
|
|
40
40
|
StatusCode,
|
|
41
41
|
StringSchemaNode,
|
|
42
42
|
TimeSchemaNode,
|
|
43
|
+
TypeNode,
|
|
43
44
|
UnionSchemaNode,
|
|
44
45
|
UrlSchemaNode,
|
|
45
46
|
} from './nodes/index.ts'
|
|
46
|
-
export type { Printer, PrinterFactoryOptions } from './
|
|
47
|
+
export type { Printer, PrinterFactoryOptions } from './printer.ts'
|
|
47
48
|
export type { RefMap } from './refs.ts'
|
|
48
49
|
export type { AsyncVisitor, CollectOptions, CollectVisitor, ParentOf, TransformOptions, Visitor, VisitorContext, WalkOptions } from './visitor.ts'
|