@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/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
- * @default hash
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
  }
@@ -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
@@ -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
- * AST node representing one API operation.
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 operation: OperationNode = {
14
- * kind: 'Operation',
15
- * operationId: 'listPets',
16
- * method: 'GET',
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 OperationNode = BaseNode & {
28
+ export type RequestBodyNode = BaseNode & {
25
29
  /**
26
30
  * Node kind.
27
31
  */
28
- kind: 'Operation'
32
+ kind: 'RequestBody'
29
33
  /**
30
- * Operation identifier, usually from OpenAPI `operationId`.
34
+ * Human-readable request body description.
31
35
  */
32
- operationId: string
36
+ description?: string
33
37
  /**
34
- * HTTP Method like 'GET'
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
- method: HttpMethod
41
+ required?: boolean
37
42
  /**
38
- * OpenAPI-style path string, for example `/pets/{petId}`.
39
- * Path parameters retain the `{param}` notation from the original spec.
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
- path: string
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 metadata for the operation.
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
@@ -1,16 +1,20 @@
1
1
  import type { BaseNode } from './base.ts'
2
- import type { MediaType, StatusCode } from './http.ts'
3
- import type { SchemaNode } from './schema.ts'
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
- * Response body schema.
32
- */
33
- schema: SchemaNode
34
- /**
35
- * Response media type.
36
- */
37
- mediaType?: MediaType | null
38
- /**
39
- * Property keys to exclude from the generated type via `Omit<Type, Keys>`.
40
- * Set when a referenced schema has `writeOnly` fields that should not appear in response types.
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
- keysToOmit?: Array<string>
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
- * Basic metadata for an API document.
7
- * Adapters fill fields that exist in their source format.
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 API', version: '1.0.0' }
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 (from `info.title` in OAS/AsyncAPI).
19
+ * API title from `info.title` in the source document.
17
20
  */
18
21
  title?: string
19
22
  /**
20
- * API description (from `info.description` in OAS/AsyncAPI).
23
+ * API description from `info.description` in the source document.
21
24
  */
22
25
  description?: string
23
26
  /**
24
- * API version string (from `info.version` in OAS/AsyncAPI).
27
+ * API version string from `info.version` in the source document.
25
28
  */
26
29
  version?: string
27
30
  /**
28
- * Resolved API base URL.
29
- * For OpenAPI and AsyncAPI, this comes from the selected server URL.
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
- baseURL?: string
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
- * Optional document metadata populated by the adapter.
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
  }
@@ -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
  /**
@@ -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.
@@ -378,12 +383,13 @@ export type RefSchemaNode = SchemaNodeBase & {
378
383
  /**
379
384
  * The fully-parsed schema that this ref resolves to.
380
385
  * Populated during OAS parsing when the referenced definition can be resolved.
381
- * `undefined` when the ref cannot be resolved or is part of a circular chain.
386
+ * `null` when the ref cannot be resolved or is part of a circular chain.
387
+ * `undefined` when resolution has not been attempted.
382
388
  *
383
389
  * Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
384
390
  * without following the reference manually.
385
391
  */
386
- schema?: SchemaNode
392
+ schema?: SchemaNode | null
387
393
  }
388
394
 
389
395
  /**