@kubb/ast 5.0.0-alpha.3 → 5.0.0-alpha.30
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/src/utils.ts
CHANGED
|
@@ -1,16 +1,48 @@
|
|
|
1
|
+
import { camelCase, isValidVarName } from '@internals/utils'
|
|
2
|
+
|
|
3
|
+
import { createFunctionParameter, createFunctionParameters, createParameterGroup, createProperty, createSchema, createTypeNode } from './factory.ts'
|
|
1
4
|
import { narrowSchema } from './guards.ts'
|
|
2
|
-
import type { SchemaNode } from './nodes/index.ts'
|
|
5
|
+
import type { FunctionParameterNode, FunctionParametersNode, OperationNode, ParameterGroupNode, ParameterNode, SchemaNode, TypeNode } from './nodes/index.ts'
|
|
3
6
|
import type { SchemaType } from './nodes/schema.ts'
|
|
4
7
|
|
|
5
|
-
const plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'])
|
|
8
|
+
const plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns a merged schema view for a ref node, combining the resolved `node.schema`
|
|
12
|
+
* (base from the referenced definition) with any usage-site sibling fields set directly
|
|
13
|
+
* on the ref node (description, readOnly, nullable, deprecated, etc.).
|
|
14
|
+
*
|
|
15
|
+
* Usage-site fields take precedence over the resolved schema's own fields when both are defined.
|
|
16
|
+
*
|
|
17
|
+
* For non-ref nodes the node itself is returned unchanged.
|
|
18
|
+
*/
|
|
19
|
+
export function syncSchemaRef(node: SchemaNode): SchemaNode {
|
|
20
|
+
const ref = narrowSchema(node, 'ref')
|
|
21
|
+
|
|
22
|
+
if (!ref) return node
|
|
23
|
+
if (!ref.schema) return node
|
|
24
|
+
|
|
25
|
+
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref
|
|
26
|
+
|
|
27
|
+
// Filter out undefined override values so they don't shadow the resolved schema's fields.
|
|
28
|
+
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))
|
|
29
|
+
|
|
30
|
+
return createSchema({ ...ref.schema, ...definedOverrides })
|
|
31
|
+
}
|
|
6
32
|
|
|
7
33
|
/**
|
|
8
|
-
* Returns `true` when a schema
|
|
34
|
+
* Returns `true` when a schema is emitted as a plain `string` type.
|
|
9
35
|
*
|
|
10
36
|
* - `string`, `uuid`, `email`, `url`, `datetime` are always plain strings.
|
|
11
37
|
* - `date` and `time` are plain strings when their `representation` is `'string'` rather than `'date'`.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* isStringType(createSchema({ type: 'uuid' })) // true
|
|
42
|
+
* isStringType(createSchema({ type: 'date', representation: 'date' })) // false
|
|
43
|
+
* ```
|
|
12
44
|
*/
|
|
13
|
-
export function
|
|
45
|
+
export function isStringType(node: SchemaNode): boolean {
|
|
14
46
|
if (plainStringTypes.has(node.type)) {
|
|
15
47
|
return true
|
|
16
48
|
}
|
|
@@ -22,3 +54,398 @@ export function isPlainStringType(node: SchemaNode): boolean {
|
|
|
22
54
|
|
|
23
55
|
return false
|
|
24
56
|
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Applies casing rules to parameter names and returns a new parameter array.
|
|
60
|
+
*
|
|
61
|
+
* The input array is not mutated.
|
|
62
|
+
* If `casing` is not set, the original array is returned unchanged.
|
|
63
|
+
*
|
|
64
|
+
* Use this before passing parameters to schema builders so that property keys
|
|
65
|
+
* in generated output match the desired casing while preserving
|
|
66
|
+
* `OperationNode.parameters` for other consumers.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const params = [createParameter({ name: 'pet_id', in: 'query', schema: createSchema({ type: 'string' }) })]
|
|
71
|
+
* const cased = caseParams(params, 'camelcase')
|
|
72
|
+
* // cased[0].name === 'petId'
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {
|
|
76
|
+
if (!casing) {
|
|
77
|
+
return params
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return params.map((param) => {
|
|
81
|
+
const transformed = casing === 'camelcase' || !isValidVarName(param.name) ? camelCase(param.name) : param.name
|
|
82
|
+
|
|
83
|
+
return { ...param, name: transformed }
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Creates a single-property object schema used as a discriminator literal.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* createDiscriminantNode({ propertyName: 'type', value: 'dog' })
|
|
93
|
+
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export function createDiscriminantNode({ propertyName, value }: { propertyName: string; value: string }): SchemaNode {
|
|
97
|
+
return createSchema({
|
|
98
|
+
type: 'object',
|
|
99
|
+
primitive: 'object',
|
|
100
|
+
properties: [
|
|
101
|
+
createProperty({
|
|
102
|
+
name: propertyName,
|
|
103
|
+
schema: createSchema({
|
|
104
|
+
type: 'enum',
|
|
105
|
+
primitive: 'string',
|
|
106
|
+
enumValues: [value],
|
|
107
|
+
}),
|
|
108
|
+
required: true,
|
|
109
|
+
}),
|
|
110
|
+
],
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Named type for a group of parameters (query or header) emitted as a single typed parameter.
|
|
116
|
+
*/
|
|
117
|
+
export type ParamGroupType = {
|
|
118
|
+
/**
|
|
119
|
+
* TypeNode for the group type.
|
|
120
|
+
*/
|
|
121
|
+
type: TypeNode
|
|
122
|
+
/**
|
|
123
|
+
* Whether the parameter group is optional.
|
|
124
|
+
*/
|
|
125
|
+
optional: boolean
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Resolver interface for {@link createOperationParams}.
|
|
130
|
+
*
|
|
131
|
+
* `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.
|
|
132
|
+
*/
|
|
133
|
+
export type OperationParamsResolver = {
|
|
134
|
+
/**
|
|
135
|
+
* Resolves the type name for an individual parameter.
|
|
136
|
+
*
|
|
137
|
+
* @example Individual path parameter name
|
|
138
|
+
* `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
|
|
139
|
+
*/
|
|
140
|
+
resolveParamName(node: OperationNode, param: ParameterNode): string
|
|
141
|
+
/**
|
|
142
|
+
* Resolves the request body type name.
|
|
143
|
+
*
|
|
144
|
+
* @example Request body type name
|
|
145
|
+
* `resolver.resolveDataName(node) // → 'CreatePetData'`
|
|
146
|
+
*/
|
|
147
|
+
resolveDataName(node: OperationNode): string
|
|
148
|
+
/**
|
|
149
|
+
* Resolves the grouped path parameters type name.
|
|
150
|
+
* When the return value equals `resolveParamName`, no indexed access is emitted.
|
|
151
|
+
*
|
|
152
|
+
* @example Grouped path params type name
|
|
153
|
+
* `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
|
|
154
|
+
*/
|
|
155
|
+
resolvePathParamsName(node: OperationNode, param: ParameterNode): string
|
|
156
|
+
/**
|
|
157
|
+
* Resolves the grouped query parameters type name.
|
|
158
|
+
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
|
|
159
|
+
*
|
|
160
|
+
* @example Grouped query params type name
|
|
161
|
+
* `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
|
|
162
|
+
*/
|
|
163
|
+
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string
|
|
164
|
+
/**
|
|
165
|
+
* Resolves the grouped header parameters type name.
|
|
166
|
+
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
|
|
167
|
+
*
|
|
168
|
+
* @example Grouped header params type name
|
|
169
|
+
* `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
|
|
170
|
+
*/
|
|
171
|
+
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Options for {@link createOperationParams}.
|
|
176
|
+
*/
|
|
177
|
+
export type CreateOperationParamsOptions = {
|
|
178
|
+
/**
|
|
179
|
+
* How all operation parameters are grouped in the function signature.
|
|
180
|
+
* - `'object'` wraps all params into a single destructured object `{ petId, data, params }`
|
|
181
|
+
* - `'inline'` emits each param category as a separate top-level parameter
|
|
182
|
+
*/
|
|
183
|
+
paramsType: 'object' | 'inline'
|
|
184
|
+
/**
|
|
185
|
+
* How path parameters are emitted when `paramsType` is `'inline'`.
|
|
186
|
+
* - `'object'` groups them as `{ petId, storeId }: PathParams`
|
|
187
|
+
* - `'inline'` spreads them as individual parameters `petId: string, storeId: string`
|
|
188
|
+
* - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`
|
|
189
|
+
*/
|
|
190
|
+
pathParamsType: 'object' | 'inline' | 'inlineSpread'
|
|
191
|
+
/**
|
|
192
|
+
* Converts parameter names to camelCase before output.
|
|
193
|
+
*/
|
|
194
|
+
paramsCasing?: 'camelcase'
|
|
195
|
+
/**
|
|
196
|
+
* Resolver for parameter and request body type names.
|
|
197
|
+
* Pass `ResolverTs` from `@kubb/plugin-ts` directly.
|
|
198
|
+
* When omitted, falls back to the schema primitive or `'unknown'`.
|
|
199
|
+
*/
|
|
200
|
+
resolver?: OperationParamsResolver
|
|
201
|
+
/**
|
|
202
|
+
* Default value for the path parameters binding when `pathParamsType` is `'object'`.
|
|
203
|
+
* Falls back to `'{}'` when all path params are optional.
|
|
204
|
+
*/
|
|
205
|
+
pathParamsDefault?: string
|
|
206
|
+
/**
|
|
207
|
+
* Extra parameters appended after the standard operation parameters.
|
|
208
|
+
*
|
|
209
|
+
* @example Plugin-specific trailing parameter
|
|
210
|
+
* ```ts
|
|
211
|
+
* extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
extraParams?: Array<FunctionParameterNode | ParameterGroupNode>
|
|
215
|
+
/**
|
|
216
|
+
* Override the default parameter names used for body, query, header, and rest-path groups.
|
|
217
|
+
*
|
|
218
|
+
* Useful when targeting languages or frameworks with different naming conventions.
|
|
219
|
+
*
|
|
220
|
+
* @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }
|
|
221
|
+
*/
|
|
222
|
+
paramNames?: {
|
|
223
|
+
/**
|
|
224
|
+
* Name for the request body parameter.
|
|
225
|
+
* @default 'data'
|
|
226
|
+
*/
|
|
227
|
+
data?: string
|
|
228
|
+
/**
|
|
229
|
+
* Name for the query parameters group parameter.
|
|
230
|
+
* @default 'params'
|
|
231
|
+
*/
|
|
232
|
+
params?: string
|
|
233
|
+
/**
|
|
234
|
+
* Name for the header parameters group parameter.
|
|
235
|
+
* @default 'headers'
|
|
236
|
+
*/
|
|
237
|
+
headers?: string
|
|
238
|
+
/**
|
|
239
|
+
* Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.
|
|
240
|
+
* @default 'pathParams'
|
|
241
|
+
*/
|
|
242
|
+
path?: string
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Applies a uniform transformation to every resolved type name before it is used
|
|
246
|
+
* in a parameter node. Use this for framework-level type wrappers.
|
|
247
|
+
*
|
|
248
|
+
* @example Vue Query — wrap every parameter type with `MaybeRefOrGetter`
|
|
249
|
+
* `typeWrapper: (t) => \`MaybeRefOrGetter<${t}>\``
|
|
250
|
+
*/
|
|
251
|
+
typeWrapper?: (type: string) => string
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function resolveType({ node, param, resolver }: { node: OperationNode; param: ParameterNode; resolver: OperationParamsResolver | undefined }): TypeNode {
|
|
255
|
+
if (!resolver) {
|
|
256
|
+
return createTypeNode({ variant: 'reference', name: param.schema.primitive ?? 'unknown' })
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const individualName = resolver.resolveParamName(node, param)
|
|
260
|
+
|
|
261
|
+
const groupLocation = param.in === 'path' || param.in === 'query' || param.in === 'header' ? param.in : undefined
|
|
262
|
+
|
|
263
|
+
const groupResolvers = {
|
|
264
|
+
path: resolver.resolvePathParamsName,
|
|
265
|
+
query: resolver.resolveQueryParamsName,
|
|
266
|
+
header: resolver.resolveHeaderParamsName,
|
|
267
|
+
} as const
|
|
268
|
+
|
|
269
|
+
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : undefined
|
|
270
|
+
|
|
271
|
+
if (groupName && groupName !== individualName) {
|
|
272
|
+
return createTypeNode({ variant: 'member', base: groupName, key: param.name })
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return createTypeNode({ variant: 'reference', name: individualName })
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Converts an {@link OperationNode} into a {@link FunctionParametersNode}.
|
|
280
|
+
*
|
|
281
|
+
* Centralizes the per-plugin `getParams()` pattern. Provide a `resolver` for
|
|
282
|
+
* type resolution and `extraParams` for plugin-specific trailing parameters.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* const params = createOperationParams(node, {
|
|
287
|
+
* paramsType: 'inline',
|
|
288
|
+
* pathParamsType: 'inline',
|
|
289
|
+
* resolver: tsResolver,
|
|
290
|
+
* extraParams: [createFunctionParameter({ name: 'options', type: createTypeNode({ variant: 'reference', name: 'Partial<RequestOptions>' }), default: '{}' })],
|
|
291
|
+
* })
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
export function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode {
|
|
295
|
+
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options
|
|
296
|
+
|
|
297
|
+
const dataName = paramNames?.data ?? 'data'
|
|
298
|
+
const paramsName = paramNames?.params ?? 'params'
|
|
299
|
+
const headersName = paramNames?.headers ?? 'headers'
|
|
300
|
+
const pathName = paramNames?.path ?? 'pathParams'
|
|
301
|
+
|
|
302
|
+
const wrapType = (type: string): TypeNode => createTypeNode({ variant: 'reference', name: typeWrapper ? typeWrapper(type) : type })
|
|
303
|
+
// Only reference TypeNodes are wrapped (they hold a plain type name string).
|
|
304
|
+
// Member and struct TypeNodes are pre-resolved structured expressions and are passed through unchanged.
|
|
305
|
+
const wrapTypeNode = (type: TypeNode): TypeNode => (type.variant === 'reference' ? wrapType(type.name) : type)
|
|
306
|
+
|
|
307
|
+
const casedParams = caseParams(node.parameters, paramsCasing)
|
|
308
|
+
const pathParams = casedParams.filter((p) => p.in === 'path')
|
|
309
|
+
const queryParams = casedParams.filter((p) => p.in === 'query')
|
|
310
|
+
const headerParams = casedParams.filter((p) => p.in === 'header')
|
|
311
|
+
|
|
312
|
+
const bodyType = node.requestBody?.schema ? wrapType(resolver?.resolveDataName(node) ?? 'unknown') : undefined
|
|
313
|
+
const bodyRequired = node.requestBody?.required ?? false
|
|
314
|
+
|
|
315
|
+
const queryGroupType = resolver ? resolveGroupType({ node, params: queryParams, groupMethod: resolver.resolveQueryParamsName, resolver }) : undefined
|
|
316
|
+
const headerGroupType = resolver ? resolveGroupType({ node, params: headerParams, groupMethod: resolver.resolveHeaderParamsName, resolver }) : undefined
|
|
317
|
+
|
|
318
|
+
const params: Array<FunctionParameterNode | ParameterGroupNode> = []
|
|
319
|
+
|
|
320
|
+
if (paramsType === 'object') {
|
|
321
|
+
const children: Array<FunctionParameterNode> = [
|
|
322
|
+
...pathParams.map((p) => {
|
|
323
|
+
const type = resolveType({ node, param: p, resolver })
|
|
324
|
+
return createFunctionParameter({ name: p.name, type: wrapTypeNode(type), optional: !p.required })
|
|
325
|
+
}),
|
|
326
|
+
...(bodyType ? [createFunctionParameter({ name: dataName, type: bodyType, optional: !bodyRequired })] : []),
|
|
327
|
+
...buildGroupParam({ name: paramsName, node, params: queryParams, groupType: queryGroupType, resolver, wrapType }),
|
|
328
|
+
...buildGroupParam({ name: headersName, node, params: headerParams, groupType: headerGroupType, resolver, wrapType }),
|
|
329
|
+
]
|
|
330
|
+
|
|
331
|
+
if (children.length) {
|
|
332
|
+
params.push(createParameterGroup({ properties: children, default: children.every((c) => c.optional) ? '{}' : undefined }))
|
|
333
|
+
}
|
|
334
|
+
} else {
|
|
335
|
+
if (pathParams.length) {
|
|
336
|
+
if (pathParamsType === 'inlineSpread') {
|
|
337
|
+
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]!) ?? undefined
|
|
338
|
+
params.push(createFunctionParameter({ name: pathName, type: spreadType ? wrapType(spreadType) : undefined, rest: true }))
|
|
339
|
+
} else {
|
|
340
|
+
const pathChildren = pathParams.map((p) => {
|
|
341
|
+
const type = resolveType({ node, param: p, resolver })
|
|
342
|
+
return createFunctionParameter({ name: p.name, type: wrapTypeNode(type), optional: !p.required })
|
|
343
|
+
})
|
|
344
|
+
params.push(
|
|
345
|
+
createParameterGroup({
|
|
346
|
+
properties: pathChildren,
|
|
347
|
+
inline: pathParamsType === 'inline',
|
|
348
|
+
default: pathParamsDefault ?? (pathChildren.every((c) => c.optional) ? '{}' : undefined),
|
|
349
|
+
}),
|
|
350
|
+
)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (bodyType) {
|
|
355
|
+
params.push(createFunctionParameter({ name: dataName, type: bodyType, optional: !bodyRequired }))
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
params.push(...buildGroupParam({ name: paramsName, node, params: queryParams, groupType: queryGroupType, resolver, wrapType }))
|
|
359
|
+
params.push(...buildGroupParam({ name: headersName, node, params: headerParams, groupType: headerGroupType, resolver, wrapType }))
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
params.push(...extraParams)
|
|
363
|
+
|
|
364
|
+
return createFunctionParameters({ params })
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Builds a single {@link FunctionParameterNode} for a query or header group.
|
|
369
|
+
* Returns an empty array when there are no params to emit.
|
|
370
|
+
*
|
|
371
|
+
* If a pre-resolved `groupType` is provided it emits `name: GroupType`.
|
|
372
|
+
* Otherwise, it builds an inline struct from the individual params.
|
|
373
|
+
*/
|
|
374
|
+
function buildGroupParam({
|
|
375
|
+
name,
|
|
376
|
+
node,
|
|
377
|
+
params,
|
|
378
|
+
groupType,
|
|
379
|
+
resolver,
|
|
380
|
+
wrapType,
|
|
381
|
+
}: {
|
|
382
|
+
name: string
|
|
383
|
+
node: OperationNode
|
|
384
|
+
params: Array<ParameterNode>
|
|
385
|
+
groupType: ParamGroupType | undefined
|
|
386
|
+
resolver: OperationParamsResolver | undefined
|
|
387
|
+
wrapType: (type: string) => TypeNode
|
|
388
|
+
}): Array<FunctionParameterNode> {
|
|
389
|
+
if (groupType) {
|
|
390
|
+
const type = groupType.type.variant === 'reference' ? wrapType(groupType.type.name) : groupType.type
|
|
391
|
+
return [createFunctionParameter({ name, type, optional: groupType.optional })]
|
|
392
|
+
}
|
|
393
|
+
if (params.length) {
|
|
394
|
+
return [
|
|
395
|
+
createFunctionParameter({
|
|
396
|
+
name,
|
|
397
|
+
type: toStructType({ node, params, resolver }),
|
|
398
|
+
optional: params.every((p) => !p.required),
|
|
399
|
+
}),
|
|
400
|
+
]
|
|
401
|
+
}
|
|
402
|
+
return []
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Derives a {@link ParamGroupType} from the resolver's group method.
|
|
407
|
+
* Returns `undefined` when the group name equals the individual param name (no real group).
|
|
408
|
+
*/
|
|
409
|
+
function resolveGroupType({
|
|
410
|
+
node,
|
|
411
|
+
params,
|
|
412
|
+
groupMethod,
|
|
413
|
+
resolver,
|
|
414
|
+
}: {
|
|
415
|
+
node: OperationNode
|
|
416
|
+
params: Array<ParameterNode>
|
|
417
|
+
groupMethod: (_node: OperationNode, _param: ParameterNode) => string
|
|
418
|
+
resolver: OperationParamsResolver
|
|
419
|
+
}): ParamGroupType | undefined {
|
|
420
|
+
if (!params.length) {
|
|
421
|
+
return undefined
|
|
422
|
+
}
|
|
423
|
+
const firstParam = params[0]!
|
|
424
|
+
const groupName = groupMethod.call(resolver, node, firstParam)
|
|
425
|
+
if (groupName === resolver.resolveParamName(node, firstParam)) {
|
|
426
|
+
return undefined
|
|
427
|
+
}
|
|
428
|
+
const allOptional = params.every((p) => !p.required)
|
|
429
|
+
return { type: createTypeNode({ variant: 'reference', name: groupName }), optional: allOptional }
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Builds a {@link TypeNode} with `variant: 'struct'` for an inline anonymous type grouping named fields.
|
|
434
|
+
*
|
|
435
|
+
* Used when query or header parameters have no dedicated group type name.
|
|
436
|
+
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
|
|
437
|
+
*/
|
|
438
|
+
function toStructType({
|
|
439
|
+
node,
|
|
440
|
+
params,
|
|
441
|
+
resolver,
|
|
442
|
+
}: {
|
|
443
|
+
node: OperationNode
|
|
444
|
+
params: Array<ParameterNode>
|
|
445
|
+
resolver: OperationParamsResolver | undefined
|
|
446
|
+
}): TypeNode {
|
|
447
|
+
return createTypeNode({
|
|
448
|
+
variant: 'struct',
|
|
449
|
+
properties: params.map((p) => ({ name: p.name, optional: !p.required, type: resolveType({ node, param: p, resolver }) })),
|
|
450
|
+
})
|
|
451
|
+
}
|