@kubb/ast 5.0.0-beta.4 → 5.0.0-beta.40
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/README.md +17 -8
- package/dist/index.cjs +866 -395
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +33 -3310
- package/dist/index.js +859 -391
- package/dist/index.js.map +1 -1
- package/dist/types-CEIHPTfs.d.ts +3596 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/package.json +8 -4
- package/src/constants.ts +2 -52
- package/src/dedupe.ts +200 -0
- package/src/dialect.ts +58 -0
- package/src/dispatch.ts +53 -0
- package/src/factory.ts +133 -17
- package/src/guards.ts +18 -48
- package/src/index.ts +9 -5
- package/src/infer.ts +16 -14
- package/src/nodes/base.ts +2 -0
- package/src/nodes/code.ts +22 -22
- package/src/nodes/content.ts +37 -0
- package/src/nodes/file.ts +16 -14
- package/src/nodes/function.ts +11 -11
- package/src/nodes/index.ts +7 -3
- package/src/nodes/operation.ts +98 -62
- package/src/nodes/response.ts +21 -14
- package/src/nodes/root.ts +72 -10
- package/src/nodes/schema.ts +12 -9
- package/src/printer.ts +42 -36
- package/src/refs.ts +0 -54
- package/src/resolvers.ts +4 -4
- package/src/signature.ts +232 -0
- package/src/transformers.ts +20 -15
- package/src/types.ts +9 -2
- package/src/utils.ts +118 -77
- package/src/visitor.ts +237 -279
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
package/src/factory.ts
CHANGED
|
@@ -6,14 +6,20 @@ import type {
|
|
|
6
6
|
ArrowFunctionNode,
|
|
7
7
|
BreakNode,
|
|
8
8
|
ConstNode,
|
|
9
|
+
ContentNode,
|
|
9
10
|
ExportNode,
|
|
10
11
|
FileNode,
|
|
11
12
|
FunctionNode,
|
|
12
13
|
FunctionParameterNode,
|
|
13
14
|
FunctionParametersNode,
|
|
15
|
+
GenericOperationNode,
|
|
16
|
+
HttpOperationNode,
|
|
14
17
|
ImportNode,
|
|
18
|
+
InputMeta,
|
|
15
19
|
InputNode,
|
|
20
|
+
InputStreamNode,
|
|
16
21
|
JsxNode,
|
|
22
|
+
Node,
|
|
17
23
|
ObjectSchemaNode,
|
|
18
24
|
OperationNode,
|
|
19
25
|
OutputNode,
|
|
@@ -22,6 +28,7 @@ import type {
|
|
|
22
28
|
ParamsTypeNode,
|
|
23
29
|
PrimitiveSchemaType,
|
|
24
30
|
PropertyNode,
|
|
31
|
+
RequestBodyNode,
|
|
25
32
|
ResponseNode,
|
|
26
33
|
SchemaNode,
|
|
27
34
|
SourceNode,
|
|
@@ -31,10 +38,13 @@ import type {
|
|
|
31
38
|
import { combineExports, combineImports, combineSources, extractStringsFromNodes } from './utils.ts'
|
|
32
39
|
|
|
33
40
|
/**
|
|
34
|
-
*
|
|
41
|
+
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
|
|
42
|
+
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
|
|
43
|
+
* object properties combine "required" and "nullable" into a single AST.
|
|
35
44
|
*
|
|
36
|
-
* -
|
|
37
|
-
* -
|
|
45
|
+
* - Non-required + non-nullable → `optional: true`.
|
|
46
|
+
* - Non-required + nullable → `nullish: true`.
|
|
47
|
+
* - Required → both flags cleared.
|
|
38
48
|
*/
|
|
39
49
|
export function syncOptionality(schema: SchemaNode, required: boolean): SchemaNode {
|
|
40
50
|
const nullable = schema.nullable ?? false
|
|
@@ -59,6 +69,32 @@ export function syncOptionality(schema: SchemaNode, required: boolean): SchemaNo
|
|
|
59
69
|
*/
|
|
60
70
|
export type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never
|
|
61
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Identity-preserving node update: returns `node` unchanged when every field in
|
|
74
|
+
* `changes` already equals (by reference) the current value, otherwise a new node
|
|
75
|
+
* with the changes applied.
|
|
76
|
+
*
|
|
77
|
+
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
|
|
78
|
+
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
|
|
79
|
+
* downstream passes can detect "nothing changed" by identity. Comparison is
|
|
80
|
+
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* update(node, { name: node.name }) // -> same `node` reference
|
|
85
|
+
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export function update<T extends Node>(node: T, changes: Partial<T>): T {
|
|
89
|
+
for (const key in changes) {
|
|
90
|
+
if (changes[key] !== node[key as keyof T]) {
|
|
91
|
+
return { ...node, ...changes }
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return node
|
|
96
|
+
}
|
|
97
|
+
|
|
62
98
|
type CreateSchemaObjectInput = Omit<ObjectSchemaNode, 'kind' | 'properties' | 'primitive'> & { properties?: Array<PropertyNode>; primitive?: 'object' }
|
|
63
99
|
type CreateSchemaInput = CreateSchemaObjectInput | DistributiveOmit<Exclude<SchemaNode, ObjectSchemaNode>, 'kind'>
|
|
64
100
|
type CreateSchemaOutput<T extends CreateSchemaInput> = InferSchemaNode<T> & {
|
|
@@ -84,11 +120,24 @@ export function createInput(overrides: Partial<Omit<InputNode, 'kind'>> = {}): I
|
|
|
84
120
|
return {
|
|
85
121
|
schemas: [],
|
|
86
122
|
operations: [],
|
|
123
|
+
meta: { circularNames: [], enumNames: [] },
|
|
87
124
|
...overrides,
|
|
88
125
|
kind: 'Input',
|
|
89
126
|
}
|
|
90
127
|
}
|
|
91
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Creates an `InputStreamNode` from pre-built `AsyncIterable` sources.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* const node = createStreamInput(schemasIterable, operationsIterable, { title: 'My API' })
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export function createStreamInput(schemas: AsyncIterable<SchemaNode>, operations: AsyncIterable<OperationNode>, meta?: InputMeta): InputStreamNode {
|
|
138
|
+
return { kind: 'Input', schemas, operations, meta }
|
|
139
|
+
}
|
|
140
|
+
|
|
92
141
|
/**
|
|
93
142
|
* Creates an `OutputNode` with a stable default for `files`.
|
|
94
143
|
*
|
|
@@ -134,21 +183,75 @@ export function createOutput(overrides: Partial<Omit<OutputNode, 'kind'>> = {}):
|
|
|
134
183
|
* })
|
|
135
184
|
* ```
|
|
136
185
|
*/
|
|
186
|
+
/**
|
|
187
|
+
* Loosely-typed content entry accepted by the builders, normalized into a {@link ContentNode}.
|
|
188
|
+
*/
|
|
189
|
+
type UserContent = Omit<ContentNode, 'kind'>
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Creates a `ContentNode` for a single request-body or response content type.
|
|
193
|
+
*/
|
|
194
|
+
export function createContent(props: UserContent): ContentNode {
|
|
195
|
+
return {
|
|
196
|
+
...props,
|
|
197
|
+
kind: 'Content',
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Loosely-typed request body accepted by `createOperation`, normalized into a {@link RequestBodyNode}.
|
|
203
|
+
*/
|
|
204
|
+
type UserRequestBody = Omit<RequestBodyNode, 'kind' | 'content'> & {
|
|
205
|
+
content?: Array<UserContent>
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
|
|
210
|
+
*/
|
|
211
|
+
export function createRequestBody(props: UserRequestBody): RequestBodyNode {
|
|
212
|
+
return {
|
|
213
|
+
...props,
|
|
214
|
+
kind: 'RequestBody',
|
|
215
|
+
content: props.content?.map(createContent),
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function createOperation(
|
|
220
|
+
props: Pick<HttpOperationNode, 'operationId' | 'method' | 'path'> &
|
|
221
|
+
Partial<Omit<HttpOperationNode, 'kind' | 'operationId' | 'method' | 'path' | 'requestBody'>> & {
|
|
222
|
+
requestBody?: UserRequestBody
|
|
223
|
+
},
|
|
224
|
+
): HttpOperationNode
|
|
137
225
|
export function createOperation(
|
|
138
|
-
props: Pick<
|
|
139
|
-
|
|
226
|
+
props: Pick<GenericOperationNode, 'operationId'> &
|
|
227
|
+
Partial<Omit<GenericOperationNode, 'kind' | 'operationId' | 'requestBody'>> & {
|
|
228
|
+
requestBody?: UserRequestBody
|
|
229
|
+
},
|
|
230
|
+
): GenericOperationNode
|
|
231
|
+
export function createOperation(props: {
|
|
232
|
+
operationId: string
|
|
233
|
+
method?: HttpOperationNode['method']
|
|
234
|
+
path?: HttpOperationNode['path']
|
|
235
|
+
requestBody?: UserRequestBody
|
|
236
|
+
[key: string]: unknown
|
|
237
|
+
}): OperationNode {
|
|
238
|
+
const { requestBody, ...rest } = props
|
|
239
|
+
const isHttp = rest.method !== undefined && rest.path !== undefined
|
|
240
|
+
|
|
140
241
|
return {
|
|
141
242
|
tags: [],
|
|
142
243
|
parameters: [],
|
|
143
244
|
responses: [],
|
|
144
|
-
...
|
|
245
|
+
...rest,
|
|
246
|
+
...(isHttp ? { protocol: 'http' } : {}),
|
|
145
247
|
kind: 'Operation',
|
|
146
|
-
|
|
248
|
+
requestBody: requestBody ? createRequestBody(requestBody) : undefined,
|
|
249
|
+
} as OperationNode
|
|
147
250
|
}
|
|
148
251
|
|
|
149
252
|
/**
|
|
150
253
|
* Maps schema `type` to its underlying `primitive`.
|
|
151
|
-
* Primitive types map to themselves
|
|
254
|
+
* Primitive types map to themselves. Special string formats map to `'string'`.
|
|
152
255
|
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
|
|
153
256
|
*/
|
|
154
257
|
const TYPE_TO_PRIMITIVE: Partial<Record<SchemaNode['type'], PrimitiveSchemaType>> = {
|
|
@@ -304,21 +407,34 @@ export function createParameter(
|
|
|
304
407
|
/**
|
|
305
408
|
* Creates a `ResponseNode`.
|
|
306
409
|
*
|
|
410
|
+
* Response body schemas live inside `content`. For convenience a single legacy `schema`
|
|
411
|
+
* (with optional `mediaType`/`keysToOmit`) is normalized into one `content` entry, so the same
|
|
412
|
+
* schema is never stored both at the node root and inside `content`.
|
|
413
|
+
*
|
|
307
414
|
* @example
|
|
308
415
|
* ```ts
|
|
309
416
|
* const response = createResponse({
|
|
310
417
|
* statusCode: '200',
|
|
311
|
-
*
|
|
312
|
-
* schema: createSchema({ type: 'object', properties: [] }),
|
|
418
|
+
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }],
|
|
313
419
|
* })
|
|
314
420
|
* ```
|
|
315
421
|
*/
|
|
316
422
|
export function createResponse(
|
|
317
|
-
props: Pick<ResponseNode, 'statusCode'
|
|
423
|
+
props: Pick<ResponseNode, 'statusCode'> &
|
|
424
|
+
Partial<Omit<ResponseNode, 'kind' | 'statusCode' | 'content'>> & {
|
|
425
|
+
content?: Array<UserContent>
|
|
426
|
+
schema?: SchemaNode
|
|
427
|
+
mediaType?: string | null
|
|
428
|
+
keysToOmit?: Array<string> | null
|
|
429
|
+
},
|
|
318
430
|
): ResponseNode {
|
|
431
|
+
const { schema, mediaType, keysToOmit, content, ...rest } = props
|
|
432
|
+
const entries = content ?? (schema ? [{ contentType: mediaType ?? 'application/json', schema, keysToOmit: keysToOmit ?? null }] : undefined)
|
|
433
|
+
|
|
319
434
|
return {
|
|
320
|
-
...
|
|
435
|
+
...rest,
|
|
321
436
|
kind: 'Response',
|
|
437
|
+
content: entries?.map(createContent),
|
|
322
438
|
}
|
|
323
439
|
}
|
|
324
440
|
|
|
@@ -339,7 +455,7 @@ export function createResponse(
|
|
|
339
455
|
* // → params?: QueryParams
|
|
340
456
|
* ```
|
|
341
457
|
*
|
|
342
|
-
* @example Param with default (implicitly optional
|
|
458
|
+
* @example Param with default (implicitly optional. Cannot combine with `optional: true`)
|
|
343
459
|
* ```ts
|
|
344
460
|
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), default: '{}' })
|
|
345
461
|
* // → config: RequestConfig = {}
|
|
@@ -409,7 +525,7 @@ export function createParamsType(
|
|
|
409
525
|
* // call → { id, name }
|
|
410
526
|
* ```
|
|
411
527
|
*
|
|
412
|
-
* @example Inline (spread)
|
|
528
|
+
* @example Inline (spread), children emitted as individual top-level parameters
|
|
413
529
|
* ```ts
|
|
414
530
|
* createParameterGroup({
|
|
415
531
|
* properties: [createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false })],
|
|
@@ -512,9 +628,9 @@ export type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>,
|
|
|
512
628
|
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
513
629
|
*
|
|
514
630
|
* Computes:
|
|
515
|
-
* - `id`
|
|
516
|
-
* - `name`
|
|
517
|
-
* - `extname`
|
|
631
|
+
* - `id` SHA256 hash of the file path
|
|
632
|
+
* - `name` `baseName` without extension
|
|
633
|
+
* - `extname` extension extracted from `baseName`
|
|
518
634
|
*
|
|
519
635
|
* Deduplicates:
|
|
520
636
|
* - `sources` via `combineSources`
|
package/src/guards.ts
CHANGED
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
FunctionParameterNode,
|
|
3
|
-
FunctionParametersNode,
|
|
4
|
-
InputNode,
|
|
5
|
-
Node,
|
|
6
|
-
NodeKind,
|
|
7
|
-
OperationNode,
|
|
8
|
-
OutputNode,
|
|
9
|
-
ParameterGroupNode,
|
|
10
|
-
ParameterNode,
|
|
11
|
-
PropertyNode,
|
|
12
|
-
ResponseNode,
|
|
13
|
-
SchemaNode,
|
|
14
|
-
SchemaNodeByType,
|
|
15
|
-
} from './nodes/index.ts'
|
|
1
|
+
import type { HttpOperationNode, InputNode, Node, NodeKind, OperationNode, OutputNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'
|
|
16
2
|
|
|
17
3
|
/**
|
|
18
4
|
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
@@ -20,11 +6,11 @@ import type {
|
|
|
20
6
|
* @example
|
|
21
7
|
* ```ts
|
|
22
8
|
* const schema = createSchema({ type: 'string' })
|
|
23
|
-
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode |
|
|
9
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
24
10
|
* ```
|
|
25
11
|
*/
|
|
26
|
-
export function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] |
|
|
27
|
-
return node?.type === type ? (node as SchemaNodeByType[T]) :
|
|
12
|
+
export function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null {
|
|
13
|
+
return node?.type === type ? (node as SchemaNodeByType[T]) : null
|
|
28
14
|
}
|
|
29
15
|
|
|
30
16
|
function isKind<T extends Node>(kind: NodeKind) {
|
|
@@ -67,6 +53,20 @@ export const isOutputNode = isKind<OutputNode>('Output')
|
|
|
67
53
|
*/
|
|
68
54
|
export const isOperationNode = isKind<OperationNode>('Operation')
|
|
69
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Narrows an `OperationNode` to an `HttpOperationNode`, guaranteeing `method` and `path`.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* if (isHttpOperationNode(node)) {
|
|
62
|
+
* console.log(node.method, node.path)
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export function isHttpOperationNode(node: OperationNode): node is HttpOperationNode {
|
|
67
|
+
return node.protocol === 'http' || (node.method !== undefined && node.path !== undefined)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
70
|
/**
|
|
71
71
|
* Returns `true` when the input is a `SchemaNode`.
|
|
72
72
|
*
|
|
@@ -78,33 +78,3 @@ export const isOperationNode = isKind<OperationNode>('Operation')
|
|
|
78
78
|
* ```
|
|
79
79
|
*/
|
|
80
80
|
export const isSchemaNode = isKind<SchemaNode>('Schema')
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Returns `true` when the input is a `PropertyNode`.
|
|
84
|
-
*/
|
|
85
|
-
export const isPropertyNode = isKind<PropertyNode>('Property')
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Returns `true` when the input is a `ParameterNode`.
|
|
89
|
-
*/
|
|
90
|
-
export const isParameterNode = isKind<ParameterNode>('Parameter')
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Returns `true` when the input is a `ResponseNode`.
|
|
94
|
-
*/
|
|
95
|
-
export const isResponseNode = isKind<ResponseNode>('Response')
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Returns `true` when the input is a `FunctionParameterNode`.
|
|
99
|
-
*/
|
|
100
|
-
export const isFunctionParameterNode = isKind<FunctionParameterNode>('FunctionParameter')
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Returns `true` when the input is a `ParameterGroupNode`.
|
|
104
|
-
*/
|
|
105
|
-
export const isParameterGroupNode = isKind<ParameterGroupNode>('ParameterGroup')
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Returns `true` when the input is a `FunctionParametersNode`.
|
|
109
|
-
*/
|
|
110
|
-
export const isFunctionParametersNode = isKind<FunctionParametersNode>('FunctionParameters')
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
export { httpMethods,
|
|
1
|
+
export { httpMethods, schemaTypes } from './constants.ts'
|
|
2
|
+
export { applyDedupe, buildDedupePlan } from './dedupe.ts'
|
|
3
|
+
export { defineSchemaDialect } from './dialect.ts'
|
|
4
|
+
export { dispatch } from './dispatch.ts'
|
|
2
5
|
export {
|
|
3
6
|
createArrowFunction,
|
|
4
7
|
createBreak,
|
|
@@ -10,6 +13,7 @@ export {
|
|
|
10
13
|
createFunctionParameters,
|
|
11
14
|
createImport,
|
|
12
15
|
createInput,
|
|
16
|
+
createStreamInput,
|
|
13
17
|
createJsx,
|
|
14
18
|
createOperation,
|
|
15
19
|
createOutput,
|
|
@@ -23,16 +27,17 @@ export {
|
|
|
23
27
|
createText,
|
|
24
28
|
createType,
|
|
25
29
|
syncOptionality,
|
|
30
|
+
update,
|
|
26
31
|
} from './factory.ts'
|
|
27
|
-
export { isInputNode, isOperationNode, isOutputNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
32
|
+
export { isHttpOperationNode, isInputNode, isOperationNode, isOutputNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
28
33
|
export { createPrinterFactory, definePrinter } from './printer.ts'
|
|
29
34
|
export { extractRefName } from './refs.ts'
|
|
30
35
|
export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
|
|
31
|
-
export {
|
|
36
|
+
export { schemaSignature } from './signature.ts'
|
|
37
|
+
export { mergeAdjacentObjectsLazy, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
|
|
32
38
|
export type * from './types.ts'
|
|
33
39
|
export {
|
|
34
40
|
caseParams,
|
|
35
|
-
collectReferencedSchemaNames,
|
|
36
41
|
collectUsedSchemaNames,
|
|
37
42
|
containsCircularRef,
|
|
38
43
|
createDiscriminantNode,
|
|
@@ -40,7 +45,6 @@ export {
|
|
|
40
45
|
extractStringsFromNodes,
|
|
41
46
|
findCircularSchemas,
|
|
42
47
|
isStringType,
|
|
43
|
-
resolveRefName,
|
|
44
48
|
syncSchemaRef,
|
|
45
49
|
} from './utils.ts'
|
|
46
50
|
export { collect, transform, walk } from './visitor.ts'
|
package/src/infer.ts
CHANGED
|
@@ -20,15 +20,25 @@ import type {
|
|
|
20
20
|
*/
|
|
21
21
|
export type ParserOptions = {
|
|
22
22
|
/**
|
|
23
|
-
* How `format: 'date-time'` schemas are represented
|
|
23
|
+
* How `format: 'date-time'` schemas are represented downstream.
|
|
24
|
+
* - `false` falls through to a plain `string` (no validation).
|
|
25
|
+
* - `'string'` emits a datetime string node.
|
|
26
|
+
* - `'stringOffset'` emits a datetime node with timezone offset.
|
|
27
|
+
* - `'stringLocal'` emits a local datetime node.
|
|
28
|
+
* - `'date'` emits a `date` node (JavaScript `Date` object).
|
|
24
29
|
*/
|
|
25
30
|
dateType: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
|
|
26
31
|
/**
|
|
27
|
-
*
|
|
32
|
+
* How `type: 'integer'` (and `format: 'int64'`) maps to TypeScript.
|
|
33
|
+
* - `'number'` fits most JSON APIs. Loses precision above `Number.MAX_SAFE_INTEGER`.
|
|
34
|
+
* - `'bigint'` is exact for 64-bit IDs, but does not round-trip through JSON.
|
|
35
|
+
*
|
|
36
|
+
* @default 'number'
|
|
28
37
|
*/
|
|
29
38
|
integerType?: 'number' | 'bigint'
|
|
30
39
|
/**
|
|
31
|
-
* AST type used when
|
|
40
|
+
* AST type used when a schema's type cannot be inferred from the spec
|
|
41
|
+
* (`additionalProperties: true`, missing `type`, ...).
|
|
32
42
|
*/
|
|
33
43
|
unknownType: 'any' | 'unknown' | 'void'
|
|
34
44
|
/**
|
|
@@ -36,7 +46,8 @@ export type ParserOptions = {
|
|
|
36
46
|
*/
|
|
37
47
|
emptySchemaType: 'any' | 'unknown' | 'void'
|
|
38
48
|
/**
|
|
39
|
-
* Suffix appended to derived enum names when
|
|
49
|
+
* Suffix appended to derived enum names when Kubb has to invent one
|
|
50
|
+
* (typically for inline enums on object properties).
|
|
40
51
|
*/
|
|
41
52
|
enumSuffix: 'enum' | (string & {})
|
|
42
53
|
}
|
|
@@ -66,7 +77,7 @@ type ResolveDateTimeNode<TDateType extends ParserOptions['dateType']> = DateTime
|
|
|
66
77
|
type SchemaNodeMap<TDateType extends ParserOptions['dateType'] = 'string'> = [
|
|
67
78
|
[{ $ref: string }, RefSchemaNode],
|
|
68
79
|
[{ allOf: ReadonlyArray<unknown>; properties: object }, IntersectionSchemaNode],
|
|
69
|
-
[{ allOf: readonly [unknown, unknown, ...unknown
|
|
80
|
+
[{ allOf: readonly [unknown, unknown, ...Array<unknown>] }, IntersectionSchemaNode],
|
|
70
81
|
[{ allOf: ReadonlyArray<unknown> }, SchemaNode],
|
|
71
82
|
[{ oneOf: ReadonlyArray<unknown> }, UnionSchemaNode],
|
|
72
83
|
[{ anyOf: ReadonlyArray<unknown> }, UnionSchemaNode],
|
|
@@ -119,12 +130,3 @@ export type InferSchemaNode<
|
|
|
119
130
|
? TEntry[1]
|
|
120
131
|
: InferSchemaNode<TSchema, TDateType, TRest>
|
|
121
132
|
: SchemaNode
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Backward-compatible alias for `InferSchemaNode`.
|
|
125
|
-
*/
|
|
126
|
-
export type InferSchema<
|
|
127
|
-
TSchema extends object,
|
|
128
|
-
TDateType extends ParserOptions['dateType'] = 'string',
|
|
129
|
-
TEntries extends ReadonlyArray<[object, SchemaNode]> = SchemaNodeMap<TDateType>,
|
|
130
|
-
> = InferSchemaNode<TSchema, TDateType, TEntries>
|
package/src/nodes/base.ts
CHANGED
package/src/nodes/code.ts
CHANGED
|
@@ -36,21 +36,21 @@ export type ConstNode = BaseNode & {
|
|
|
36
36
|
* Whether the declaration should be exported.
|
|
37
37
|
* @default false
|
|
38
38
|
*/
|
|
39
|
-
export?: boolean
|
|
39
|
+
export?: boolean | null
|
|
40
40
|
/**
|
|
41
41
|
* Optional explicit type annotation.
|
|
42
42
|
* @example 'Pet'
|
|
43
43
|
*/
|
|
44
|
-
type?: string
|
|
44
|
+
type?: string | null
|
|
45
45
|
/**
|
|
46
46
|
* JSDoc documentation metadata.
|
|
47
47
|
*/
|
|
48
|
-
JSDoc?: JSDocNode
|
|
48
|
+
JSDoc?: JSDocNode | null
|
|
49
49
|
/**
|
|
50
50
|
* Whether to append `as const` to the declaration.
|
|
51
51
|
* @default false
|
|
52
52
|
*/
|
|
53
|
-
asConst?: boolean
|
|
53
|
+
asConst?: boolean | null
|
|
54
54
|
/**
|
|
55
55
|
* Child nodes representing the value of the constant (children of the `Const` component).
|
|
56
56
|
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
@@ -83,11 +83,11 @@ export type TypeNode = BaseNode & {
|
|
|
83
83
|
* Whether the declaration should be exported.
|
|
84
84
|
* @default false
|
|
85
85
|
*/
|
|
86
|
-
export?: boolean
|
|
86
|
+
export?: boolean | null
|
|
87
87
|
/**
|
|
88
88
|
* JSDoc documentation metadata.
|
|
89
89
|
*/
|
|
90
|
-
JSDoc?: JSDocNode
|
|
90
|
+
JSDoc?: JSDocNode | null
|
|
91
91
|
/**
|
|
92
92
|
* Child nodes representing the type body (children of the `Type` component).
|
|
93
93
|
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
@@ -126,35 +126,35 @@ export type FunctionNode = BaseNode & {
|
|
|
126
126
|
* Whether the function is a default export.
|
|
127
127
|
* @default false
|
|
128
128
|
*/
|
|
129
|
-
default?: boolean
|
|
129
|
+
default?: boolean | null
|
|
130
130
|
/**
|
|
131
131
|
* Function parameter list rendered as a string (e.g. from `FunctionParams.toConstructor()`).
|
|
132
132
|
*/
|
|
133
|
-
params?: string
|
|
133
|
+
params?: string | null
|
|
134
134
|
/**
|
|
135
135
|
* Whether the function should be exported.
|
|
136
136
|
* @default false
|
|
137
137
|
*/
|
|
138
|
-
export?: boolean
|
|
138
|
+
export?: boolean | null
|
|
139
139
|
/**
|
|
140
140
|
* Whether the function is async. When `true`, the return type is wrapped in `Promise<>`.
|
|
141
141
|
* @default false
|
|
142
142
|
*/
|
|
143
|
-
async?: boolean
|
|
143
|
+
async?: boolean | null
|
|
144
144
|
/**
|
|
145
145
|
* TypeScript generic type parameters.
|
|
146
146
|
* @example ['T', 'U extends string']
|
|
147
147
|
*/
|
|
148
|
-
generics?: string | string
|
|
148
|
+
generics?: string | Array<string> | null
|
|
149
149
|
/**
|
|
150
150
|
* Return type annotation.
|
|
151
151
|
* @example 'Pet'
|
|
152
152
|
*/
|
|
153
|
-
returnType?: string
|
|
153
|
+
returnType?: string | null
|
|
154
154
|
/**
|
|
155
155
|
* JSDoc documentation metadata.
|
|
156
156
|
*/
|
|
157
|
-
JSDoc?: JSDocNode
|
|
157
|
+
JSDoc?: JSDocNode | null
|
|
158
158
|
/**
|
|
159
159
|
* Child nodes representing the function body (children of the `Function` component).
|
|
160
160
|
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
@@ -187,40 +187,40 @@ export type ArrowFunctionNode = BaseNode & {
|
|
|
187
187
|
* Whether the function is a default export.
|
|
188
188
|
* @default false
|
|
189
189
|
*/
|
|
190
|
-
default?: boolean
|
|
190
|
+
default?: boolean | null
|
|
191
191
|
/**
|
|
192
192
|
* Function parameter list rendered as a string (e.g. from `FunctionParams.toConstructor()`).
|
|
193
193
|
*/
|
|
194
|
-
params?: string
|
|
194
|
+
params?: string | null
|
|
195
195
|
/**
|
|
196
196
|
* Whether the arrow function should be exported.
|
|
197
197
|
* @default false
|
|
198
198
|
*/
|
|
199
|
-
export?: boolean
|
|
199
|
+
export?: boolean | null
|
|
200
200
|
/**
|
|
201
201
|
* Whether the arrow function is async. When `true`, the return type is wrapped in `Promise<>`.
|
|
202
202
|
* @default false
|
|
203
203
|
*/
|
|
204
|
-
async?: boolean
|
|
204
|
+
async?: boolean | null
|
|
205
205
|
/**
|
|
206
206
|
* TypeScript generic type parameters.
|
|
207
207
|
* @example ['T', 'U extends string']
|
|
208
208
|
*/
|
|
209
|
-
generics?: string | string
|
|
209
|
+
generics?: string | Array<string> | null
|
|
210
210
|
/**
|
|
211
211
|
* Return type annotation.
|
|
212
212
|
* @example 'Pet'
|
|
213
213
|
*/
|
|
214
|
-
returnType?: string
|
|
214
|
+
returnType?: string | null
|
|
215
215
|
/**
|
|
216
216
|
* JSDoc documentation metadata.
|
|
217
217
|
*/
|
|
218
|
-
JSDoc?: JSDocNode
|
|
218
|
+
JSDoc?: JSDocNode | null
|
|
219
219
|
/**
|
|
220
220
|
* Render the arrow function body as a single-line expression.
|
|
221
221
|
* @default false
|
|
222
222
|
*/
|
|
223
|
-
singleLine?: boolean
|
|
223
|
+
singleLine?: boolean | null
|
|
224
224
|
/**
|
|
225
225
|
* Child nodes representing the function body (children of the `Function.Arrow` component).
|
|
226
226
|
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
@@ -255,7 +255,7 @@ export type TextNode = BaseNode & {
|
|
|
255
255
|
* AST node representing a line break in the source output.
|
|
256
256
|
*
|
|
257
257
|
* Corresponds to `<br/>` in JSX components. When printed, produces an empty
|
|
258
|
-
* string that
|
|
258
|
+
* string that, joined with `\n` by `printNodes` creates a blank line
|
|
259
259
|
* between surrounding code nodes.
|
|
260
260
|
*
|
|
261
261
|
* @example
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { BaseNode } from './base.ts'
|
|
2
|
+
import type { SchemaNode } from './schema.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* AST node representing one content-type entry of a request body or response.
|
|
6
|
+
*
|
|
7
|
+
* One entry per content type declared in the spec (e.g. `application/json`,
|
|
8
|
+
* `multipart/form-data`), each carrying its own body schema.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const content: ContentNode = {
|
|
13
|
+
* kind: 'Content',
|
|
14
|
+
* contentType: 'application/json',
|
|
15
|
+
* schema: createSchema({ type: 'string' }),
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export type ContentNode = BaseNode & {
|
|
20
|
+
/**
|
|
21
|
+
* Node kind.
|
|
22
|
+
*/
|
|
23
|
+
kind: 'Content'
|
|
24
|
+
/**
|
|
25
|
+
* The content type for this entry (e.g. `'application/json'`).
|
|
26
|
+
*/
|
|
27
|
+
contentType: string
|
|
28
|
+
/**
|
|
29
|
+
* Body schema for this content type.
|
|
30
|
+
*/
|
|
31
|
+
schema?: SchemaNode
|
|
32
|
+
/**
|
|
33
|
+
* Property keys to exclude from the generated type via `Omit<Type, Keys>`.
|
|
34
|
+
* Set when a referenced schema has `readOnly`/`writeOnly` fields that should be omitted.
|
|
35
|
+
*/
|
|
36
|
+
keysToOmit?: Array<string> | null
|
|
37
|
+
}
|