@kubb/ast 5.0.0-beta.29 → 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/dist/index.cjs +256 -211
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1041 -837
- package/dist/index.js +251 -212
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/dialect.ts +64 -0
- package/src/dispatch.ts +53 -0
- package/src/factory.ts +93 -6
- package/src/guards.ts +15 -0
- package/src/index.ts +6 -1
- package/src/nodes/base.ts +2 -0
- package/src/nodes/content.ts +37 -0
- package/src/nodes/index.ts +6 -2
- package/src/nodes/operation.ts +98 -62
- package/src/nodes/response.ts +2 -16
- package/src/types.ts +6 -0
- package/src/utils.ts +13 -6
- package/src/visitor.ts +140 -174
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/ast",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.30",
|
|
4
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",
|
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,16 +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,
|
|
15
18
|
InputMeta,
|
|
16
19
|
InputNode,
|
|
17
20
|
InputStreamNode,
|
|
18
21
|
JsxNode,
|
|
22
|
+
Node,
|
|
19
23
|
ObjectSchemaNode,
|
|
20
24
|
OperationNode,
|
|
21
25
|
OutputNode,
|
|
@@ -24,6 +28,7 @@ import type {
|
|
|
24
28
|
ParamsTypeNode,
|
|
25
29
|
PrimitiveSchemaType,
|
|
26
30
|
PropertyNode,
|
|
31
|
+
RequestBodyNode,
|
|
27
32
|
ResponseNode,
|
|
28
33
|
SchemaNode,
|
|
29
34
|
SourceNode,
|
|
@@ -64,6 +69,32 @@ export function syncOptionality(schema: SchemaNode, required: boolean): SchemaNo
|
|
|
64
69
|
*/
|
|
65
70
|
export type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never
|
|
66
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
|
+
|
|
67
98
|
type CreateSchemaObjectInput = Omit<ObjectSchemaNode, 'kind' | 'properties' | 'primitive'> & { properties?: Array<PropertyNode>; primitive?: 'object' }
|
|
68
99
|
type CreateSchemaInput = CreateSchemaObjectInput | DistributiveOmit<Exclude<SchemaNode, ObjectSchemaNode>, 'kind'>
|
|
69
100
|
type CreateSchemaOutput<T extends CreateSchemaInput> = InferSchemaNode<T> & {
|
|
@@ -152,16 +183,70 @@ export function createOutput(overrides: Partial<Omit<OutputNode, 'kind'>> = {}):
|
|
|
152
183
|
* })
|
|
153
184
|
* ```
|
|
154
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
|
+
|
|
155
219
|
export function createOperation(
|
|
156
|
-
props: Pick<
|
|
157
|
-
|
|
220
|
+
props: Pick<HttpOperationNode, 'operationId' | 'method' | 'path'> &
|
|
221
|
+
Partial<Omit<HttpOperationNode, 'kind' | 'operationId' | 'method' | 'path' | 'requestBody'>> & {
|
|
222
|
+
requestBody?: UserRequestBody
|
|
223
|
+
},
|
|
224
|
+
): HttpOperationNode
|
|
225
|
+
export function createOperation(
|
|
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
|
+
|
|
158
241
|
return {
|
|
159
242
|
tags: [],
|
|
160
243
|
parameters: [],
|
|
161
244
|
responses: [],
|
|
162
|
-
...
|
|
245
|
+
...rest,
|
|
246
|
+
...(isHttp ? { protocol: 'http' } : {}),
|
|
163
247
|
kind: 'Operation',
|
|
164
|
-
|
|
248
|
+
requestBody: requestBody ? createRequestBody(requestBody) : undefined,
|
|
249
|
+
} as OperationNode
|
|
165
250
|
}
|
|
166
251
|
|
|
167
252
|
/**
|
|
@@ -336,18 +421,20 @@ export function createParameter(
|
|
|
336
421
|
*/
|
|
337
422
|
export function createResponse(
|
|
338
423
|
props: Pick<ResponseNode, 'statusCode'> &
|
|
339
|
-
Partial<Omit<ResponseNode, 'kind' | 'statusCode'>> & {
|
|
424
|
+
Partial<Omit<ResponseNode, 'kind' | 'statusCode' | 'content'>> & {
|
|
425
|
+
content?: Array<UserContent>
|
|
340
426
|
schema?: SchemaNode
|
|
341
427
|
mediaType?: string | null
|
|
342
428
|
keysToOmit?: Array<string> | null
|
|
343
429
|
},
|
|
344
430
|
): ResponseNode {
|
|
345
431
|
const { schema, mediaType, keysToOmit, content, ...rest } = props
|
|
432
|
+
const entries = content ?? (schema ? [{ contentType: mediaType ?? 'application/json', schema, keysToOmit: keysToOmit ?? null }] : undefined)
|
|
346
433
|
|
|
347
434
|
return {
|
|
348
435
|
...rest,
|
|
349
436
|
kind: 'Response',
|
|
350
|
-
content:
|
|
437
|
+
content: entries?.map(createContent),
|
|
351
438
|
}
|
|
352
439
|
}
|
|
353
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,
|
|
@@ -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,
|
|
@@ -18,14 +21,16 @@ export {
|
|
|
18
21
|
createParameterGroup,
|
|
19
22
|
createParamsType,
|
|
20
23
|
createProperty,
|
|
24
|
+
createRequestBody,
|
|
21
25
|
createResponse,
|
|
22
26
|
createSchema,
|
|
23
27
|
createSource,
|
|
24
28
|
createText,
|
|
25
29
|
createType,
|
|
26
30
|
syncOptionality,
|
|
31
|
+
update,
|
|
27
32
|
} from './factory.ts'
|
|
28
|
-
export { isInputNode, isOperationNode, isOutputNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
33
|
+
export { isHttpOperationNode, isInputNode, isOperationNode, isOutputNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
29
34
|
export { createPrinterFactory, definePrinter } from './printer.ts'
|
|
30
35
|
export { extractRefName } from './refs.ts'
|
|
31
36
|
export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
|
package/src/nodes/base.ts
CHANGED
|
@@ -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/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,10 +12,11 @@ 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'
|
|
@@ -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> | null
|
|
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,6 +1,6 @@
|
|
|
1
1
|
import type { BaseNode } from './base.ts'
|
|
2
|
+
import type { ContentNode } from './content.ts'
|
|
2
3
|
import type { StatusCode } from './http.ts'
|
|
3
|
-
import type { SchemaNode } from './schema.ts'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* AST node representing one operation response variant.
|
|
@@ -46,19 +46,5 @@ export type ResponseNode = BaseNode & {
|
|
|
46
46
|
* response.content[1].contentType // 'application/xml'
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
content?: Array<
|
|
50
|
-
/**
|
|
51
|
-
* The content type for this entry (e.g. `'application/json'`).
|
|
52
|
-
*/
|
|
53
|
-
contentType: string
|
|
54
|
-
/**
|
|
55
|
-
* Response body schema for this content type.
|
|
56
|
-
*/
|
|
57
|
-
schema?: SchemaNode
|
|
58
|
-
/**
|
|
59
|
-
* Property keys to exclude from the generated type via `Omit<Type, Keys>`.
|
|
60
|
-
* Set when a referenced schema has `writeOnly` fields that should not appear in response types.
|
|
61
|
-
*/
|
|
62
|
-
keysToOmit?: Array<string> | null
|
|
63
|
-
}>
|
|
49
|
+
content?: Array<ContentNode>
|
|
64
50
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export type { VisitorDepth } from './constants.ts'
|
|
2
|
+
export type { SchemaDialect } from './dialect.ts'
|
|
3
|
+
export type { DispatchRule } from './dispatch.ts'
|
|
2
4
|
export type { DistributiveOmit } from './factory.ts'
|
|
3
5
|
export type { InferSchema, InferSchemaNode, ParserOptions } from './infer.ts'
|
|
4
6
|
export type {
|
|
@@ -21,7 +23,9 @@ export type {
|
|
|
21
23
|
FunctionParameterNode,
|
|
22
24
|
FunctionParametersNode,
|
|
23
25
|
FunctionParamNode,
|
|
26
|
+
GenericOperationNode,
|
|
24
27
|
HttpMethod,
|
|
28
|
+
HttpOperationNode,
|
|
25
29
|
HttpStatusCode,
|
|
26
30
|
ImportNode,
|
|
27
31
|
InputMeta,
|
|
@@ -38,6 +42,8 @@ export type {
|
|
|
38
42
|
NumberSchemaNode,
|
|
39
43
|
ObjectSchemaNode,
|
|
40
44
|
OperationNode,
|
|
45
|
+
OperationNodeBase,
|
|
46
|
+
OperationProtocol,
|
|
41
47
|
OutputNode,
|
|
42
48
|
ParameterGroupNode,
|
|
43
49
|
ParameterLocation,
|