@kubb/ast 5.0.0-beta.4 → 5.0.0-beta.41
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 +28 -19
- 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/nodes/file.ts
CHANGED
|
@@ -50,18 +50,18 @@ export type ImportNode = BaseNode & {
|
|
|
50
50
|
* - `false` generates `import { Type } from './path'`
|
|
51
51
|
* @default false
|
|
52
52
|
*/
|
|
53
|
-
isTypeOnly?: boolean
|
|
53
|
+
isTypeOnly?: boolean | null
|
|
54
54
|
/**
|
|
55
55
|
* Import entire module as namespace.
|
|
56
56
|
* - `true` generates `import * as Name from './path'`
|
|
57
57
|
* - `false` generates standard import
|
|
58
58
|
* @default false
|
|
59
59
|
*/
|
|
60
|
-
isNameSpace?: boolean
|
|
60
|
+
isNameSpace?: boolean | null
|
|
61
61
|
/**
|
|
62
62
|
* When set, the import path is resolved relative to this root.
|
|
63
63
|
*/
|
|
64
|
-
root?: string
|
|
64
|
+
root?: string | null
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
/**
|
|
@@ -94,7 +94,7 @@ export type ExportNode = BaseNode & {
|
|
|
94
94
|
* @example ['useState']
|
|
95
95
|
* @example 'React'
|
|
96
96
|
*/
|
|
97
|
-
name?: string | Array<string>
|
|
97
|
+
name?: string | Array<string> | null
|
|
98
98
|
/**
|
|
99
99
|
* Path for the export.
|
|
100
100
|
* @example '@kubb/core'
|
|
@@ -106,14 +106,14 @@ export type ExportNode = BaseNode & {
|
|
|
106
106
|
* - `false` generates `export { Type } from './path'`
|
|
107
107
|
* @default false
|
|
108
108
|
*/
|
|
109
|
-
isTypeOnly?: boolean
|
|
109
|
+
isTypeOnly?: boolean | null
|
|
110
110
|
/**
|
|
111
111
|
* Export as an aliased namespace.
|
|
112
112
|
* - `true` generates `export * as aliasName from './path'`
|
|
113
113
|
* - `false` generates a standard export
|
|
114
114
|
* @default false
|
|
115
115
|
*/
|
|
116
|
-
asAlias?: boolean
|
|
116
|
+
asAlias?: boolean | null
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
/**
|
|
@@ -134,22 +134,22 @@ export type SourceNode = BaseNode & {
|
|
|
134
134
|
/**
|
|
135
135
|
* Optional name identifying this source (used for deduplication and barrel generation).
|
|
136
136
|
*/
|
|
137
|
-
name?: string
|
|
137
|
+
name?: string | null
|
|
138
138
|
/**
|
|
139
139
|
* Mark this source as a type-only export.
|
|
140
140
|
* @default false
|
|
141
141
|
*/
|
|
142
|
-
isTypeOnly?: boolean
|
|
142
|
+
isTypeOnly?: boolean | null
|
|
143
143
|
/**
|
|
144
144
|
* Include `export` keyword in the generated source.
|
|
145
145
|
* @default false
|
|
146
146
|
*/
|
|
147
|
-
isExportable?: boolean
|
|
147
|
+
isExportable?: boolean | null
|
|
148
148
|
/**
|
|
149
149
|
* Include this source in barrel/index file generation.
|
|
150
150
|
* @default false
|
|
151
151
|
*/
|
|
152
|
-
isIndexable?: boolean
|
|
152
|
+
isIndexable?: boolean | null
|
|
153
153
|
/**
|
|
154
154
|
* Structured child nodes representing the content of this source fragment, in DOM order.
|
|
155
155
|
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
|
|
@@ -180,8 +180,8 @@ export type SourceNode = BaseNode & {
|
|
|
180
180
|
export type FileNode<TMeta extends object = object> = BaseNode & {
|
|
181
181
|
kind: 'File'
|
|
182
182
|
/**
|
|
183
|
-
* Unique identifier derived from a SHA256 hash of the file path.
|
|
184
|
-
*
|
|
183
|
+
* Unique identifier derived from a SHA256 hash of the file path. Computed
|
|
184
|
+
* by `createFile`; callers do not need to provide it.
|
|
185
185
|
*/
|
|
186
186
|
id: string
|
|
187
187
|
/**
|
|
@@ -221,10 +221,12 @@ export type FileNode<TMeta extends object = object> = BaseNode & {
|
|
|
221
221
|
meta?: TMeta
|
|
222
222
|
/**
|
|
223
223
|
* Optional banner prepended to the generated file content.
|
|
224
|
+
* Accepts `null` so `resolver.resolveBanner()` results can be passed directly.
|
|
224
225
|
*/
|
|
225
|
-
banner?: string
|
|
226
|
+
banner?: string | null
|
|
226
227
|
/**
|
|
227
228
|
* Optional footer appended to the generated file content.
|
|
229
|
+
* Accepts `null` so `resolver.resolveFooter()` results can be passed directly.
|
|
228
230
|
*/
|
|
229
|
-
footer?: string
|
|
231
|
+
footer?: string | null
|
|
230
232
|
}
|
package/src/nodes/function.ts
CHANGED
|
@@ -4,9 +4,9 @@ import type { BaseNode } from './base.ts'
|
|
|
4
4
|
* AST node representing a language-agnostic type expression used as a function parameter
|
|
5
5
|
* type annotation. Each language printer renders the variant into its own syntax.
|
|
6
6
|
*
|
|
7
|
-
* - `struct`
|
|
7
|
+
* - `struct` an inline anonymous type grouping named fields.
|
|
8
8
|
* TypeScript renders as `{ petId: string; name?: string }`.
|
|
9
|
-
* - `member`
|
|
9
|
+
* - `member` a single named field accessed from a named group type.
|
|
10
10
|
* TypeScript renders as `PathParams['petId']`.
|
|
11
11
|
*
|
|
12
12
|
* @example Reference variant
|
|
@@ -35,7 +35,7 @@ export type ParamsTypeNode = BaseNode & {
|
|
|
35
35
|
} & (
|
|
36
36
|
| {
|
|
37
37
|
/**
|
|
38
|
-
* Reference variant
|
|
38
|
+
* Reference variant, a plain type name or identifier.
|
|
39
39
|
* TypeScript renders as-is, e.g. `string`, `QueryParams`, `Partial<Config>`.
|
|
40
40
|
*/
|
|
41
41
|
variant: 'reference'
|
|
@@ -46,7 +46,7 @@ export type ParamsTypeNode = BaseNode & {
|
|
|
46
46
|
}
|
|
47
47
|
| {
|
|
48
48
|
/**
|
|
49
|
-
* Struct variant
|
|
49
|
+
* Struct variant, an inline anonymous type grouping named fields.
|
|
50
50
|
* TypeScript renders as `{ key: Type; other?: OtherType }`.
|
|
51
51
|
*/
|
|
52
52
|
variant: 'struct'
|
|
@@ -61,7 +61,7 @@ export type ParamsTypeNode = BaseNode & {
|
|
|
61
61
|
}
|
|
62
62
|
| {
|
|
63
63
|
/**
|
|
64
|
-
* Member variant
|
|
64
|
+
* Member variant, a single named field accessed from a group type.
|
|
65
65
|
* TypeScript renders as `Base['key']`.
|
|
66
66
|
*/
|
|
67
67
|
variant: 'member'
|
|
@@ -122,7 +122,7 @@ export type FunctionParameterNode = BaseNode & {
|
|
|
122
122
|
*/
|
|
123
123
|
rest?: boolean
|
|
124
124
|
} /**
|
|
125
|
-
* Optional parameter
|
|
125
|
+
* Optional parameter, rendered with `?` and may be omitted by the caller.
|
|
126
126
|
* Cannot be combined with `default` because a defaulted parameter is already optional.
|
|
127
127
|
*/ & (
|
|
128
128
|
| { optional: true; default?: never }
|
|
@@ -196,10 +196,10 @@ export type ParameterGroupNode = BaseNode & {
|
|
|
196
196
|
* Nodes are plain immutable data.
|
|
197
197
|
*
|
|
198
198
|
* Renders differently depending on the output mode:
|
|
199
|
-
* - `declaration` → `(id: string, config: Config = {})`
|
|
200
|
-
* - `call` → `(id, { method, url })`
|
|
201
|
-
* - `keys` → `{ id, config }`
|
|
202
|
-
* - `values` → `{ id: id, config: config }`
|
|
199
|
+
* - `declaration` → `(id: string, config: Config = {})` function declaration parameters
|
|
200
|
+
* - `call` → `(id, { method, url })` function call arguments
|
|
201
|
+
* - `keys` → `{ id, config }` key names only (for destructuring)
|
|
202
|
+
* - `values` → `{ id: id, config: config }` key → value pairs
|
|
203
203
|
*/
|
|
204
204
|
export type FunctionParametersNode = BaseNode & {
|
|
205
205
|
/**
|
|
@@ -218,6 +218,6 @@ export type FunctionParametersNode = BaseNode & {
|
|
|
218
218
|
export type FunctionParamNode = FunctionParameterNode | ParameterGroupNode | FunctionParametersNode | ParamsTypeNode
|
|
219
219
|
|
|
220
220
|
/**
|
|
221
|
-
* Handler map keys
|
|
221
|
+
* Handler map keys, one per `FunctionParamNode` kind.
|
|
222
222
|
*/
|
|
223
223
|
export type FunctionNodeType = 'functionParameter' | 'parameterGroup' | 'functionParameters' | 'paramsType'
|
package/src/nodes/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { ArrowFunctionNode, ConstNode, FunctionNode, TypeNode } from './code.ts'
|
|
2
|
+
import type { ContentNode } from './content.ts'
|
|
2
3
|
import type { ExportNode, FileNode, ImportNode, SourceNode } from './file.ts'
|
|
3
4
|
import type { FunctionParamNode, ParamsTypeNode } from './function.ts'
|
|
4
|
-
import type { OperationNode } from './operation.ts'
|
|
5
|
+
import type { OperationNode, RequestBodyNode } from './operation.ts'
|
|
5
6
|
import type { OutputNode } from './output.ts'
|
|
6
7
|
import type { ParameterNode } from './parameter.ts'
|
|
7
8
|
import type { PropertyNode } from './property.ts'
|
|
@@ -11,15 +12,16 @@ import type { SchemaNode } from './schema.ts'
|
|
|
11
12
|
|
|
12
13
|
export type { BaseNode, NodeKind } from './base.ts'
|
|
13
14
|
export type { ArrowFunctionNode, BreakNode, CodeNode, ConstNode, FunctionNode, JSDocNode, JsxNode, TextNode, TypeDeclarationNode, TypeNode } from './code.ts'
|
|
15
|
+
export type { ContentNode } from './content.ts'
|
|
14
16
|
export type { ExportNode, FileNode, ImportNode, SourceNode } from './file.ts'
|
|
15
17
|
export type { FunctionNodeType, FunctionParameterNode, FunctionParametersNode, FunctionParamNode, ParameterGroupNode, ParamsTypeNode } from './function.ts'
|
|
16
18
|
export type { HttpStatusCode, MediaType, StatusCode } from './http.ts'
|
|
17
|
-
export type { HttpMethod, OperationNode } from './operation.ts'
|
|
19
|
+
export type { GenericOperationNode, HttpMethod, HttpOperationNode, OperationNode, OperationNodeBase, OperationProtocol, RequestBodyNode } from './operation.ts'
|
|
18
20
|
export type { OutputNode } from './output.ts'
|
|
19
21
|
export type { ParameterLocation, ParameterNode } from './parameter.ts'
|
|
20
22
|
export type { PropertyNode } from './property.ts'
|
|
21
23
|
export type { ResponseNode } from './response.ts'
|
|
22
|
-
export type { InputMeta, InputNode } from './root.ts'
|
|
24
|
+
export type { InputMeta, InputNode, InputStreamNode } from './root.ts'
|
|
23
25
|
export type {
|
|
24
26
|
ArraySchemaNode,
|
|
25
27
|
ComplexSchemaType,
|
|
@@ -74,6 +76,8 @@ export type Node =
|
|
|
74
76
|
| PropertyNode
|
|
75
77
|
| ParameterNode
|
|
76
78
|
| ResponseNode
|
|
79
|
+
| RequestBodyNode
|
|
80
|
+
| ContentNode
|
|
77
81
|
| FunctionParamNode
|
|
78
82
|
| FileNode
|
|
79
83
|
| ImportNode
|
package/src/nodes/operation.ts
CHANGED
|
@@ -1,44 +1,67 @@
|
|
|
1
1
|
import type { BaseNode } from './base.ts'
|
|
2
|
+
import type { ContentNode } from './content.ts'
|
|
2
3
|
import type { ParameterNode } from './parameter.ts'
|
|
3
4
|
import type { ResponseNode } from './response.ts'
|
|
4
|
-
import type { SchemaNode } from './schema.ts'
|
|
5
5
|
|
|
6
6
|
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Transport an operation belongs to.
|
|
10
|
+
*/
|
|
11
|
+
export type OperationProtocol = 'http'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* AST node representing an operation request body.
|
|
15
|
+
*
|
|
16
|
+
* Body schemas live exclusively inside the `content` array (one entry per content type),
|
|
17
|
+
* mirroring {@link ResponseNode}.
|
|
10
18
|
*
|
|
11
19
|
* @example
|
|
12
20
|
* ```ts
|
|
13
|
-
* const
|
|
14
|
-
* kind: '
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* path: '/pets',
|
|
18
|
-
* tags: [],
|
|
19
|
-
* parameters: [],
|
|
20
|
-
* responses: [],
|
|
21
|
+
* const requestBody: RequestBodyNode = {
|
|
22
|
+
* kind: 'RequestBody',
|
|
23
|
+
* required: true,
|
|
24
|
+
* content: [{ kind: 'Content', contentType: 'application/json', schema: createSchema({ type: 'string' }) }],
|
|
21
25
|
* }
|
|
22
26
|
* ```
|
|
23
27
|
*/
|
|
24
|
-
export type
|
|
28
|
+
export type RequestBodyNode = BaseNode & {
|
|
25
29
|
/**
|
|
26
30
|
* Node kind.
|
|
27
31
|
*/
|
|
28
|
-
kind: '
|
|
32
|
+
kind: 'RequestBody'
|
|
29
33
|
/**
|
|
30
|
-
*
|
|
34
|
+
* Human-readable request body description.
|
|
31
35
|
*/
|
|
32
|
-
|
|
36
|
+
description?: string
|
|
33
37
|
/**
|
|
34
|
-
*
|
|
38
|
+
* Whether the request body is required (`requestBody.required: true` in the spec).
|
|
39
|
+
* When `false` or absent, the generated `data` parameter should be optional.
|
|
35
40
|
*/
|
|
36
|
-
|
|
41
|
+
required?: boolean
|
|
37
42
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
43
|
+
* All available content type entries for this request body.
|
|
44
|
+
*
|
|
45
|
+
* When the adapter `contentType` option is set, this array contains exactly one entry for
|
|
46
|
+
* that content type. Otherwise it contains one entry per content type declared in the spec,
|
|
47
|
+
* so that plugins can generate code for every variant (e.g. separate hooks for
|
|
48
|
+
* `application/json` and `multipart/form-data`).
|
|
40
49
|
*/
|
|
41
|
-
|
|
50
|
+
content?: Array<ContentNode>
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Fields shared by every operation, regardless of transport.
|
|
55
|
+
*/
|
|
56
|
+
export type OperationNodeBase = BaseNode & {
|
|
57
|
+
/**
|
|
58
|
+
* Node kind.
|
|
59
|
+
*/
|
|
60
|
+
kind: 'Operation'
|
|
61
|
+
/**
|
|
62
|
+
* Operation identifier, usually from OpenAPI `operationId`.
|
|
63
|
+
*/
|
|
64
|
+
operationId: string
|
|
42
65
|
/**
|
|
43
66
|
* Group labels for the operation.
|
|
44
67
|
* Usually copied from OpenAPI `tags`.
|
|
@@ -61,51 +84,64 @@ export type OperationNode = BaseNode & {
|
|
|
61
84
|
*/
|
|
62
85
|
parameters: Array<ParameterNode>
|
|
63
86
|
/**
|
|
64
|
-
* Request body
|
|
65
|
-
*/
|
|
66
|
-
requestBody?:
|
|
67
|
-
/**
|
|
68
|
-
* Human-readable request body description.
|
|
69
|
-
*/
|
|
70
|
-
description?: string
|
|
71
|
-
/**
|
|
72
|
-
* Whether the request body is required (`requestBody.required: true` in the spec).
|
|
73
|
-
* When `false` or absent, the generated `data` parameter should be optional.
|
|
74
|
-
*/
|
|
75
|
-
required?: boolean
|
|
76
|
-
/**
|
|
77
|
-
* All available content type entries for this request body.
|
|
78
|
-
*
|
|
79
|
-
* When the adapter `contentType` option is set, this array contains exactly one entry for
|
|
80
|
-
* that content type. Otherwise it contains one entry per content type declared in the spec,
|
|
81
|
-
* so that plugins can generate code for every variant (e.g. separate hooks for
|
|
82
|
-
* `application/json` and `multipart/form-data`).
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```ts
|
|
86
|
-
* // spec has both application/json and multipart/form-data
|
|
87
|
-
* requestBody.content[0].contentType // 'application/json'
|
|
88
|
-
* requestBody.content[1].contentType // 'multipart/form-data'
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
content?: Array<{
|
|
92
|
-
/**
|
|
93
|
-
* The content type for this entry (e.g. `'application/json'`).
|
|
94
|
-
*/
|
|
95
|
-
contentType: string
|
|
96
|
-
/**
|
|
97
|
-
* Request body schema for this content type.
|
|
98
|
-
*/
|
|
99
|
-
schema?: SchemaNode
|
|
100
|
-
/**
|
|
101
|
-
* Property keys to exclude from the generated request body type via `Omit<Type, Keys>`.
|
|
102
|
-
* Set when a referenced schema has `readOnly` fields that should be omitted in request types.
|
|
103
|
-
*/
|
|
104
|
-
keysToOmit?: Array<string>
|
|
105
|
-
}>
|
|
106
|
-
}
|
|
87
|
+
* Request body for the operation.
|
|
88
|
+
*/
|
|
89
|
+
requestBody?: RequestBodyNode
|
|
107
90
|
/**
|
|
108
91
|
* Operation responses.
|
|
109
92
|
*/
|
|
110
93
|
responses: Array<ResponseNode>
|
|
111
94
|
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Operation served over HTTP/REST (OpenAPI). `method` and `path` are guaranteed.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const operation: HttpOperationNode = {
|
|
102
|
+
* kind: 'Operation',
|
|
103
|
+
* operationId: 'listPets',
|
|
104
|
+
* protocol: 'http',
|
|
105
|
+
* method: 'GET',
|
|
106
|
+
* path: '/pets',
|
|
107
|
+
* tags: [],
|
|
108
|
+
* parameters: [],
|
|
109
|
+
* responses: [],
|
|
110
|
+
* }
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export type HttpOperationNode = OperationNodeBase & {
|
|
114
|
+
/**
|
|
115
|
+
* Transport the operation belongs to.
|
|
116
|
+
*/
|
|
117
|
+
protocol?: 'http'
|
|
118
|
+
/**
|
|
119
|
+
* HTTP method like `'GET'`.
|
|
120
|
+
*/
|
|
121
|
+
method: HttpMethod
|
|
122
|
+
/**
|
|
123
|
+
* OpenAPI-style path string, for example `/pets/{petId}`, with `{param}` notation preserved.
|
|
124
|
+
*/
|
|
125
|
+
path: string
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Operation for a non-HTTP transport. HTTP-only fields are forbidden.
|
|
130
|
+
*/
|
|
131
|
+
export type GenericOperationNode = OperationNodeBase & {
|
|
132
|
+
/**
|
|
133
|
+
* Transport the operation belongs to.
|
|
134
|
+
*/
|
|
135
|
+
protocol?: Exclude<OperationProtocol, 'http'>
|
|
136
|
+
method?: never
|
|
137
|
+
path?: never
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* AST node representing one API operation.
|
|
142
|
+
*
|
|
143
|
+
* Discriminated on `protocol`: an {@link HttpOperationNode} (`protocol: 'http'`) guarantees
|
|
144
|
+
* `method` and `path`, while a {@link GenericOperationNode} omits them. Narrow with
|
|
145
|
+
* `isHttpOperationNode(node)` or `node.protocol === 'http'` before reading `method`/`path`.
|
|
146
|
+
*/
|
|
147
|
+
export type OperationNode = HttpOperationNode | GenericOperationNode
|
package/src/nodes/response.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import type { BaseNode } from './base.ts'
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { ContentNode } from './content.ts'
|
|
3
|
+
import type { StatusCode } from './http.ts'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* AST node representing one operation response variant.
|
|
7
7
|
*
|
|
8
|
+
* Mirrors {@link OperationNode.requestBody}: the response body schemas live exclusively inside
|
|
9
|
+
* the `content` array (one entry per content type), so the same schema is never duplicated at the
|
|
10
|
+
* node root and inside `content`.
|
|
11
|
+
*
|
|
8
12
|
* @example
|
|
9
13
|
* ```ts
|
|
10
14
|
* const response: ResponseNode = {
|
|
11
15
|
* kind: 'Response',
|
|
12
16
|
* statusCode: '200',
|
|
13
|
-
* schema: createSchema({ type: 'string' }),
|
|
17
|
+
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'string' }) }],
|
|
14
18
|
* }
|
|
15
19
|
* ```
|
|
16
20
|
*/
|
|
@@ -28,16 +32,19 @@ export type ResponseNode = BaseNode & {
|
|
|
28
32
|
*/
|
|
29
33
|
description?: string
|
|
30
34
|
/**
|
|
31
|
-
*
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
*
|
|
35
|
+
* All available content type entries for this response.
|
|
36
|
+
*
|
|
37
|
+
* When the adapter `contentType` option is set, this array contains exactly one entry for that
|
|
38
|
+
* content type. Otherwise it contains one entry per content type declared in the spec, so that
|
|
39
|
+
* plugins can generate a union of response types (e.g. `application/json` and `application/xml`).
|
|
40
|
+
* Body-less responses keep a single entry whose `schema` is the empty/`void` placeholder.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // spec response declares both application/json and application/xml
|
|
45
|
+
* response.content[0].contentType // 'application/json'
|
|
46
|
+
* response.content[1].contentType // 'application/xml'
|
|
47
|
+
* ```
|
|
41
48
|
*/
|
|
42
|
-
|
|
49
|
+
content?: Array<ContentNode>
|
|
43
50
|
}
|
package/src/nodes/root.ts
CHANGED
|
@@ -3,32 +3,62 @@ import type { OperationNode } from './operation.ts'
|
|
|
3
3
|
import type { SchemaNode } from './schema.ts'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* Metadata for an API document, populated by the adapter and available to every generator.
|
|
7
|
+
*
|
|
8
|
+
* All fields are plain JSON-serializable values, no `Set`, no `Map`, no class instances.
|
|
9
|
+
* Computed fields (`circularNames`, `enumNames`) are pre-calculated once during the adapter
|
|
10
|
+
* pre-scan so generators never need to iterate the full schema list themselves.
|
|
8
11
|
*
|
|
9
12
|
* @example
|
|
10
13
|
* ```ts
|
|
11
|
-
* const meta: InputMeta = { title: 'Pet
|
|
14
|
+
* const meta: InputMeta = { title: 'Pet Store', version: '1.0.0', baseURL: 'https://petstore.swagger.io/v2', circularNames: [], enumNames: [] }
|
|
12
15
|
* ```
|
|
13
16
|
*/
|
|
14
17
|
export type InputMeta = {
|
|
15
18
|
/**
|
|
16
|
-
* API title
|
|
19
|
+
* API title from `info.title` in the source document.
|
|
17
20
|
*/
|
|
18
21
|
title?: string
|
|
19
22
|
/**
|
|
20
|
-
* API description
|
|
23
|
+
* API description from `info.description` in the source document.
|
|
21
24
|
*/
|
|
22
25
|
description?: string
|
|
23
26
|
/**
|
|
24
|
-
* API version string
|
|
27
|
+
* API version string from `info.version` in the source document.
|
|
25
28
|
*/
|
|
26
29
|
version?: string
|
|
27
30
|
/**
|
|
28
|
-
* Resolved
|
|
29
|
-
|
|
31
|
+
* Resolved base URL from the first matching server entry in the source document.
|
|
32
|
+
*/
|
|
33
|
+
baseURL?: string | null
|
|
34
|
+
/**
|
|
35
|
+
* Names of schemas that participate in a circular reference chain.
|
|
36
|
+
* Computed once during the adapter pre-scan, use this instead of calling
|
|
37
|
+
* `findCircularSchemas` per generator call.
|
|
38
|
+
*
|
|
39
|
+
* Convert to a `Set` once at the start of a generator, not per-schema,
|
|
40
|
+
* to keep lookup O(1) without repeated allocations.
|
|
41
|
+
*
|
|
42
|
+
* @example Wrap a circular schema in z.lazy()
|
|
43
|
+
* ```ts
|
|
44
|
+
* const circular = new Set(meta.circularNames)
|
|
45
|
+
* if (circular.has(schema.name)) { ... }
|
|
46
|
+
* ```
|
|
30
47
|
*/
|
|
31
|
-
|
|
48
|
+
circularNames: ReadonlyArray<string>
|
|
49
|
+
/**
|
|
50
|
+
* Names of schemas whose type is `enum`.
|
|
51
|
+
* Computed once during the adapter pre-scan, use this instead of filtering
|
|
52
|
+
* schemas per generator call.
|
|
53
|
+
*
|
|
54
|
+
* Convert to a `Set` once at the start of a generator when you need repeated
|
|
55
|
+
* membership checks, rather than calling `.includes()` per schema.
|
|
56
|
+
*
|
|
57
|
+
* @example Check if a referenced schema is an enum
|
|
58
|
+
* `const enums = new Set(meta.enumNames)`
|
|
59
|
+
* `const isEnum = enums.has(schemaName)`
|
|
60
|
+
*/
|
|
61
|
+
enumNames: ReadonlyArray<string>
|
|
32
62
|
}
|
|
33
63
|
|
|
34
64
|
/**
|
|
@@ -58,7 +88,39 @@ export type InputNode = BaseNode & {
|
|
|
58
88
|
*/
|
|
59
89
|
operations: Array<OperationNode>
|
|
60
90
|
/**
|
|
61
|
-
*
|
|
91
|
+
* Document metadata populated by the adapter.
|
|
92
|
+
*/
|
|
93
|
+
meta: InputMeta
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Streaming variant of `InputNode` for memory-efficient processing of large API specs.
|
|
98
|
+
*
|
|
99
|
+
* `schemas` and `operations` are `AsyncIterable` rather than arrays, each `for await`
|
|
100
|
+
* loop creates a fresh parse pass from the cached in-memory document, so multiple
|
|
101
|
+
* consumers (plugins) can iterate independently without keeping all nodes in memory.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* for await (const schema of inputStreamNode.schemas) {
|
|
106
|
+
* // only this one SchemaNode is live here. Previous ones are GC-eligible
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export type InputStreamNode = {
|
|
111
|
+
kind: 'Input'
|
|
112
|
+
/**
|
|
113
|
+
* Lazily parsed schema nodes. Each `for await` creates a fresh parse pass, so
|
|
114
|
+
* multiple plugins can iterate independently without sharing state.
|
|
115
|
+
*/
|
|
116
|
+
schemas: AsyncIterable<SchemaNode>
|
|
117
|
+
/**
|
|
118
|
+
* Lazily parsed operation nodes. Each `for await` creates a fresh parse pass, so
|
|
119
|
+
* multiple plugins can iterate independently without sharing state.
|
|
120
|
+
*/
|
|
121
|
+
operations: AsyncIterable<OperationNode>
|
|
122
|
+
/**
|
|
123
|
+
* Document metadata available immediately, before the first yielded node.
|
|
62
124
|
*/
|
|
63
125
|
meta?: InputMeta
|
|
64
126
|
}
|
package/src/nodes/schema.ts
CHANGED
|
@@ -154,6 +154,10 @@ type SchemaNodeBase = BaseNode & {
|
|
|
154
154
|
* For example, this is `'string'` for a `uuid` schema.
|
|
155
155
|
*/
|
|
156
156
|
primitive?: PrimitiveSchemaType
|
|
157
|
+
/**
|
|
158
|
+
* Schema `format` value.
|
|
159
|
+
*/
|
|
160
|
+
format?: string
|
|
157
161
|
}
|
|
158
162
|
|
|
159
163
|
/**
|
|
@@ -174,7 +178,7 @@ export type ObjectSchemaNode = SchemaNodeBase & {
|
|
|
174
178
|
*/
|
|
175
179
|
type: 'object'
|
|
176
180
|
/**
|
|
177
|
-
* Primitive type
|
|
181
|
+
* Primitive type, always `'object'` for object schemas.
|
|
178
182
|
*/
|
|
179
183
|
primitive: 'object'
|
|
180
184
|
/**
|
|
@@ -364,8 +368,9 @@ export type RefSchemaNode = SchemaNodeBase & {
|
|
|
364
368
|
type: 'ref'
|
|
365
369
|
/**
|
|
366
370
|
* Referenced schema name.
|
|
371
|
+
* `null` means Kubb has processed this and determined there is no applicable name.
|
|
367
372
|
*/
|
|
368
|
-
name?: string
|
|
373
|
+
name?: string | null
|
|
369
374
|
/**
|
|
370
375
|
* Original `$ref` path, for example, `#/components/schemas/Order`.
|
|
371
376
|
* Used to resolve names later.
|
|
@@ -376,14 +381,12 @@ export type RefSchemaNode = SchemaNodeBase & {
|
|
|
376
381
|
*/
|
|
377
382
|
pattern?: string
|
|
378
383
|
/**
|
|
379
|
-
* The fully-parsed schema
|
|
380
|
-
* Populated during OAS parsing when the
|
|
381
|
-
* `
|
|
382
|
-
*
|
|
383
|
-
* Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
|
|
384
|
-
* without following the reference manually.
|
|
384
|
+
* The fully-parsed schema this ref resolves to, so its structure (`primitive`, `properties`)
|
|
385
|
+
* can be read without following the reference. Populated during OAS parsing when the
|
|
386
|
+
* definition resolves, `null` when it can't or the ref is circular, and `undefined` when
|
|
387
|
+
* resolution has not been attempted.
|
|
385
388
|
*/
|
|
386
|
-
schema?: SchemaNode
|
|
389
|
+
schema?: SchemaNode | null
|
|
387
390
|
}
|
|
388
391
|
|
|
389
392
|
/**
|