@kubb/ast 5.0.0-beta.3 → 5.0.0-beta.31
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 +694 -331
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1433 -1007
- package/dist/index.js +682 -332
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/dedupe.ts +202 -0
- 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 +11 -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/signature.ts +135 -0
- package/src/transformers.ts +20 -15
- package/src/types.ts +8 -0
- package/src/utils.ts +109 -68
- package/src/visitor.ts +229 -275
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
|
+
}
|
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/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
|
}
|