@kubb/ast 5.0.0-beta.3 → 5.0.0-beta.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/README.md +1 -1
- package/dist/index.cjs +473 -331
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1306 -999
- package/dist/index.js +465 -332
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/dialect.ts +64 -0
- package/src/dispatch.ts +53 -0
- package/src/factory.ts +127 -11
- package/src/guards.ts +18 -3
- package/src/index.ts +9 -3
- package/src/infer.ts +16 -5
- package/src/nodes/base.ts +2 -0
- package/src/nodes/code.ts +21 -21
- package/src/nodes/content.ts +37 -0
- package/src/nodes/file.ts +16 -14
- 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 +9 -3
- package/src/printer.ts +34 -28
- package/src/refs.ts +4 -2
- package/src/resolvers.ts +4 -4
- package/src/transformers.ts +20 -15
- package/src/types.ts +7 -0
- package/src/utils.ts +109 -68
- package/src/visitor.ts +229 -275
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/ast",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
4
|
-
"description": "Spec-agnostic AST layer for Kubb. Defines
|
|
3
|
+
"version": "5.0.0-beta.30",
|
|
4
|
+
"description": "Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ast",
|
|
7
7
|
"codegen",
|
|
8
8
|
"kubb",
|
|
9
|
-
"openapi",
|
|
10
9
|
"typescript"
|
|
11
10
|
],
|
|
12
11
|
"license": "MIT",
|
|
@@ -40,7 +39,7 @@
|
|
|
40
39
|
"registry": "https://registry.npmjs.org/"
|
|
41
40
|
},
|
|
42
41
|
"devDependencies": {
|
|
43
|
-
"@types/node": "^
|
|
42
|
+
"@types/node": "^22.19.19",
|
|
44
43
|
"@internals/utils": "0.0.0"
|
|
45
44
|
},
|
|
46
45
|
"engines": {
|
package/src/dialect.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The spec-specific decisions a schema parser makes while converting a source
|
|
3
|
+
* document's schemas into Kubb AST nodes.
|
|
4
|
+
*
|
|
5
|
+
* Everything else in an adapter's schema pipeline is generic JSON Schema shared
|
|
6
|
+
* across specs; the dialect is the one seam where a spec differs — the
|
|
7
|
+
* "dialect layer" analogue of a database driver targeting Postgres vs MySQL.
|
|
8
|
+
* Pair it with {@link dispatch}: the rule table decides *which* converter runs,
|
|
9
|
+
* the dialect answers the spec-specific questions inside them.
|
|
10
|
+
*
|
|
11
|
+
* The guard methods (`isReference`, `isDiscriminator`) are type predicates so
|
|
12
|
+
* converters narrow the schema after a check; the type parameters carry those
|
|
13
|
+
* narrowed types through.
|
|
14
|
+
*
|
|
15
|
+
* Scope: this is the seam for the **JSON Schema family** — OpenAPI, AsyncAPI, and
|
|
16
|
+
* plain JSON Schema all share `$ref`, `allOf`/`oneOf`, `enum`, and `format`, and
|
|
17
|
+
* differ only in these few decisions. A spec built on a different type system
|
|
18
|
+
* (e.g. GraphQL, with non-null wrappers, interfaces, and named-type references
|
|
19
|
+
* instead of `$ref`) does not implement a `SchemaDialect`; it reuses the universal
|
|
20
|
+
* layer directly — the `Adapter` port, the AST factories, and {@link dispatch}
|
|
21
|
+
* with its own rule table — to emit the same nodes.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam TSchema - The adapter's schema object type (e.g. an OpenAPI `SchemaObject`).
|
|
24
|
+
* @typeParam TRef - The narrowed `$ref` pointer type `isReference` proves.
|
|
25
|
+
* @typeParam TDiscriminated - The narrowed discriminated-schema type `isDiscriminator` proves.
|
|
26
|
+
* @typeParam TDocument - The source document `resolveRef` resolves against.
|
|
27
|
+
*/
|
|
28
|
+
export type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {
|
|
29
|
+
/** Identifies the dialect in logs and while debugging dispatch. */
|
|
30
|
+
name: string
|
|
31
|
+
/** Whether a schema should be treated as nullable. */
|
|
32
|
+
isNullable: (schema?: TSchema) => boolean
|
|
33
|
+
/** Whether a value is a `$ref` pointer object. */
|
|
34
|
+
isReference: (value?: unknown) => value is TRef
|
|
35
|
+
/** Whether a schema carries a structured discriminator (polymorphism). */
|
|
36
|
+
isDiscriminator: (value?: unknown) => value is TDiscriminated
|
|
37
|
+
/** Whether a schema represents binary data (converted to a `blob` node). */
|
|
38
|
+
isBinary: (schema: TSchema) => boolean
|
|
39
|
+
/** Resolves a local `$ref` pointer against the document, or nullish when it cannot. */
|
|
40
|
+
resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Identity helper that types a {@link SchemaDialect} for an adapter. Like
|
|
45
|
+
* `defineParser`, it adds no runtime behavior — it pins the dialect's type for
|
|
46
|
+
* inference and gives adapter authors a discoverable anchor.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* export const oasDialect = defineSchemaDialect({
|
|
51
|
+
* name: 'oas',
|
|
52
|
+
* isNullable,
|
|
53
|
+
* isReference,
|
|
54
|
+
* isDiscriminator,
|
|
55
|
+
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
56
|
+
* resolveRef,
|
|
57
|
+
* })
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(
|
|
61
|
+
dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>,
|
|
62
|
+
): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument> {
|
|
63
|
+
return dialect
|
|
64
|
+
}
|
package/src/dispatch.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One entry in an ordered dispatch table: a predicate paired with a converter.
|
|
3
|
+
*
|
|
4
|
+
* @typeParam TContext - Per-input context handed to every rule. A spec adapter typically
|
|
5
|
+
* pre-computes this once per node (the source spec node plus derived fields like a
|
|
6
|
+
* normalized type or resolved options) so individual rules stay cheap predicates.
|
|
7
|
+
* @typeParam TNode - The node a rule produces, e.g. a Kubb AST `SchemaNode`.
|
|
8
|
+
*/
|
|
9
|
+
export type DispatchRule<TContext, TNode> = {
|
|
10
|
+
/** Identifies the rule when reading the table or debugging which branch ran. */
|
|
11
|
+
name: string
|
|
12
|
+
/** Returns `true` when this rule is responsible for the given context. */
|
|
13
|
+
match: (context: TContext) => boolean
|
|
14
|
+
/**
|
|
15
|
+
* Produces a node for the context, or `null` to fall through to the next rule.
|
|
16
|
+
*
|
|
17
|
+
* Returning `null` lets a broad `match` defer: e.g. "has a `format`" matches many schemas,
|
|
18
|
+
* but only some formats are convertible — the rest fall through to plain `type` handling.
|
|
19
|
+
*/
|
|
20
|
+
convert: (context: TContext) => TNode | null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Walks an ordered list of {@link DispatchRule}s and returns the first node produced.
|
|
25
|
+
*
|
|
26
|
+
* This is the shared backbone for spec adapters (OpenAPI today, AsyncAPI and others later).
|
|
27
|
+
* The contract an adapter follows is intentionally minimal:
|
|
28
|
+
*
|
|
29
|
+
* context → [rule.match → rule.convert] → node
|
|
30
|
+
*
|
|
31
|
+
* An adapter derives a context from a source spec node, then declares an ordered table of
|
|
32
|
+
* rules mapping spec shapes onto Kubb AST nodes. To add support for a new spec, write a new
|
|
33
|
+
* context type and a new rules table — the traversal here is reused unchanged.
|
|
34
|
+
*
|
|
35
|
+
* Order is significant: earlier rules win, so list higher-precedence or more specific shapes
|
|
36
|
+
* first (e.g. composition keywords before plain `type`). A rule whose `match` returns `true`
|
|
37
|
+
* may still `convert` to `null` to defer to later rules. When no rule produces a node this
|
|
38
|
+
* returns `null`, leaving the caller to apply its own fallback.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const node = dispatch(schemaRules, schemaContext) ?? createSchema({ type: fallbackType })
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function dispatch<TContext, TNode>(rules: ReadonlyArray<DispatchRule<TContext, TNode>>, context: TContext): TNode | null {
|
|
46
|
+
for (const rule of rules) {
|
|
47
|
+
if (!rule.match(context)) continue
|
|
48
|
+
const node = rule.convert(context)
|
|
49
|
+
if (node !== null && node !== undefined) return node
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return null
|
|
53
|
+
}
|
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,16 +183,70 @@ 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
|
/**
|
|
@@ -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
|
|
package/src/guards.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
FunctionParameterNode,
|
|
3
3
|
FunctionParametersNode,
|
|
4
|
+
HttpOperationNode,
|
|
4
5
|
InputNode,
|
|
5
6
|
Node,
|
|
6
7
|
NodeKind,
|
|
@@ -20,11 +21,11 @@ import type {
|
|
|
20
21
|
* @example
|
|
21
22
|
* ```ts
|
|
22
23
|
* const schema = createSchema({ type: 'string' })
|
|
23
|
-
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode |
|
|
24
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
24
25
|
* ```
|
|
25
26
|
*/
|
|
26
|
-
export function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] |
|
|
27
|
-
return node?.type === type ? (node as SchemaNodeByType[T]) :
|
|
27
|
+
export function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null {
|
|
28
|
+
return node?.type === type ? (node as SchemaNodeByType[T]) : null
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
function isKind<T extends Node>(kind: NodeKind) {
|
|
@@ -67,6 +68,20 @@ export const isOutputNode = isKind<OutputNode>('Output')
|
|
|
67
68
|
*/
|
|
68
69
|
export const isOperationNode = isKind<OperationNode>('Operation')
|
|
69
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Narrows an `OperationNode` to an `HttpOperationNode`, guaranteeing `method` and `path`.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* if (isHttpOperationNode(node)) {
|
|
77
|
+
* console.log(node.method, node.path)
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export function isHttpOperationNode(node: OperationNode): node is HttpOperationNode {
|
|
82
|
+
return node.protocol === 'http' || (node.method !== undefined && node.path !== undefined)
|
|
83
|
+
}
|
|
84
|
+
|
|
70
85
|
/**
|
|
71
86
|
* Returns `true` when the input is a `SchemaNode`.
|
|
72
87
|
*
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
export { httpMethods, isScalarPrimitive, mediaTypes, nodeKinds, schemaTypes } from './constants.ts'
|
|
2
|
+
export { defineSchemaDialect } from './dialect.ts'
|
|
3
|
+
export { dispatch } from './dispatch.ts'
|
|
2
4
|
export {
|
|
3
5
|
createArrowFunction,
|
|
4
6
|
createBreak,
|
|
5
7
|
createConst,
|
|
8
|
+
createContent,
|
|
6
9
|
createExport,
|
|
7
10
|
createFile,
|
|
8
11
|
createFunction,
|
|
@@ -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,
|
|
@@ -17,18 +21,20 @@ export {
|
|
|
17
21
|
createParameterGroup,
|
|
18
22
|
createParamsType,
|
|
19
23
|
createProperty,
|
|
24
|
+
createRequestBody,
|
|
20
25
|
createResponse,
|
|
21
26
|
createSchema,
|
|
22
27
|
createSource,
|
|
23
28
|
createText,
|
|
24
29
|
createType,
|
|
25
30
|
syncOptionality,
|
|
31
|
+
update,
|
|
26
32
|
} from './factory.ts'
|
|
27
|
-
export { isInputNode, isOperationNode, isOutputNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
33
|
+
export { isHttpOperationNode, isInputNode, isOperationNode, isOutputNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
28
34
|
export { createPrinterFactory, definePrinter } from './printer.ts'
|
|
29
35
|
export { extractRefName } from './refs.ts'
|
|
30
36
|
export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
|
|
31
|
-
export { mergeAdjacentObjects, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
|
|
37
|
+
export { mergeAdjacentObjects, mergeAdjacentObjectsLazy, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
|
|
32
38
|
export type * from './types.ts'
|
|
33
39
|
export {
|
|
34
40
|
caseParams,
|
|
@@ -43,4 +49,4 @@ export {
|
|
|
43
49
|
resolveRefName,
|
|
44
50
|
syncSchemaRef,
|
|
45
51
|
} from './utils.ts'
|
|
46
|
-
export { collect, transform, walk } from './visitor.ts'
|
|
52
|
+
export { collect, collectLazy, 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],
|
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.
|
|
@@ -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
|
+
}
|