@kubb/ast 5.0.0-alpha.9 → 5.0.0-beta.75

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.
@@ -0,0 +1,223 @@
1
+ import type { BaseNode } from './base.ts'
2
+
3
+ /**
4
+ * AST node representing a language-agnostic type expression used as a function parameter
5
+ * type annotation. Each language printer renders the variant into its own syntax.
6
+ *
7
+ * - `struct` — an inline anonymous type grouping named fields.
8
+ * TypeScript renders as `{ petId: string; name?: string }`.
9
+ * - `member` — a single named field accessed from a named group type.
10
+ * TypeScript renders as `PathParams['petId']`.
11
+ *
12
+ * @example Reference variant
13
+ * ```ts
14
+ * createParamsType({ variant: 'reference', name: 'QueryParams' })
15
+ * // QueryParams
16
+ * ```
17
+ *
18
+ * @example Struct variant
19
+ * ```ts
20
+ * createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
21
+ * // { petId: string }
22
+ * ```
23
+ *
24
+ * @example Member variant
25
+ * ```ts
26
+ * createParamsType({ variant: 'member', base: 'PathParams', key: 'petId' })
27
+ * // PathParams['petId']
28
+ * ```
29
+ */
30
+ export type ParamsTypeNode = BaseNode & {
31
+ /**
32
+ * Node kind.
33
+ */
34
+ kind: 'ParamsType'
35
+ } & (
36
+ | {
37
+ /**
38
+ * Reference variant — a plain type name or identifier.
39
+ * TypeScript renders as-is, e.g. `string`, `QueryParams`, `Partial<Config>`.
40
+ */
41
+ variant: 'reference'
42
+ /**
43
+ * The full type name string, e.g. `'string'`, `'QueryParams'`, `'Partial<Config>'`.
44
+ */
45
+ name: string
46
+ }
47
+ | {
48
+ /**
49
+ * Struct variant — an inline anonymous type grouping named fields.
50
+ * TypeScript renders as `{ key: Type; other?: OtherType }`.
51
+ */
52
+ variant: 'struct'
53
+ /**
54
+ * Properties of the struct type.
55
+ */
56
+ properties: Array<{
57
+ name: string
58
+ optional: boolean
59
+ type: ParamsTypeNode
60
+ }>
61
+ }
62
+ | {
63
+ /**
64
+ * Member variant — a single named field accessed from a group type.
65
+ * TypeScript renders as `Base['key']`.
66
+ */
67
+ variant: 'member'
68
+ /**
69
+ * Base type name, e.g. `'DeletePetPathParams'`.
70
+ */
71
+ base: string
72
+ /**
73
+ * The field name to access, e.g. `'petId'`.
74
+ */
75
+ key: string
76
+ }
77
+ )
78
+
79
+ /**
80
+ * AST node for one function parameter.
81
+ *
82
+ * @example Required parameter
83
+ * `name: Type`
84
+ *
85
+ * @example Optional parameter
86
+ * `name?: Type`
87
+ *
88
+ * @example Parameter with default value
89
+ * `name: Type = defaultValue`
90
+ *
91
+ * @example Rest parameter
92
+ * `...name: Type[]`
93
+ */
94
+ export type FunctionParameterNode = BaseNode & {
95
+ /**
96
+ * Node kind.
97
+ */
98
+ kind: 'FunctionParameter'
99
+ /**
100
+ * Parameter name in the generated signature.
101
+ */
102
+ name: string
103
+ /**
104
+ * Type annotation as a structured {@link ParamsTypeNode}.
105
+ * Omit for untyped output.
106
+ *
107
+ * @example Reference type node
108
+ * `{ kind: 'ParamsType', variant: 'reference', name: 'string' }` → `petId: string`
109
+ *
110
+ * @example Struct type node
111
+ * `{ kind: 'ParamsType', variant: 'struct', properties: [...] }` → `{ key: Type; other?: OtherType }`
112
+ *
113
+ * @example Member type node
114
+ * `{ kind: 'ParamsType', variant: 'member', base: 'PathParams', key: 'petId' }` → `PathParams['petId']`
115
+ */
116
+ type?: ParamsTypeNode
117
+ /**
118
+ * When `true` the parameter is emitted as a rest parameter.
119
+ *
120
+ * @example Rest parameter
121
+ * `...name: Type[]`
122
+ */
123
+ rest?: boolean
124
+ } /**
125
+ * Optional parameter — rendered with `?` and may be omitted by the caller.
126
+ * Cannot be combined with `default` because a defaulted parameter is already optional.
127
+ */ & (
128
+ | { optional: true; default?: never }
129
+ /**
130
+ * Required parameter, or a parameter with a default value.
131
+ *
132
+ * @example Required
133
+ * `name: Type`
134
+ *
135
+ * @example With default
136
+ * `name: Type = default`
137
+ */
138
+ | { optional?: false; default?: string }
139
+ )
140
+
141
+ /**
142
+ * AST node for a group of related function parameters treated as a single unit.
143
+ *
144
+ * Each language printer decides how to render this group:
145
+ * - TypeScript/JS: destructured object `{ key1, key2 }: { key1: Type1; key2: Type2 } = {}`
146
+ * - Python: keyword-only args or a typed dict parameter
147
+ * - C# / Kotlin: named record / data-class parameter
148
+ *
149
+ * When `inline` is `true`, the group is spread as individual top-level parameters
150
+ * rather than wrapped in a single grouped construct.
151
+ *
152
+ * @example Grouped destructuring
153
+ * `{ id, name }: { id: string; name: string } = {}`
154
+ *
155
+ * @example Inline (spread as individual parameters)
156
+ * `id: string, name: string`
157
+ */
158
+ export type ParameterGroupNode = BaseNode & {
159
+ /**
160
+ * Node kind.
161
+ */
162
+ kind: 'ParameterGroup'
163
+ /**
164
+ * The individual parameters that form the group.
165
+ * Rendered as a destructured object or spread inline when `inline` is `true`.
166
+ */
167
+ properties: Array<FunctionParameterNode>
168
+ /**
169
+ * Optional explicit type annotation for the whole group.
170
+ * When absent, printers auto-compute it from `properties`.
171
+ */
172
+ type?: ParamsTypeNode
173
+ /**
174
+ * When `true`, `properties` are emitted as individual top-level parameters instead of
175
+ * being wrapped in a single grouped construct.
176
+ *
177
+ * @default false
178
+ */
179
+ inline?: boolean
180
+ /**
181
+ * Whether the group as a whole is optional.
182
+ * If omitted, printers infer this from child properties.
183
+ */
184
+ optional?: boolean
185
+ /**
186
+ * Default value for the group, written verbatim after `=`.
187
+ * Commonly `'{}'` to allow omitting the argument entirely.
188
+ */
189
+ default?: string
190
+ }
191
+
192
+ /**
193
+ * AST node for a complete function parameter list.
194
+ *
195
+ * Printers are responsible for sorting (`required` → `optional` → `defaulted`).
196
+ * Nodes are plain immutable data.
197
+ *
198
+ * Renders differently depending on the output mode:
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
+ */
204
+ export type FunctionParametersNode = BaseNode & {
205
+ /**
206
+ * Node kind.
207
+ */
208
+ kind: 'FunctionParameters'
209
+ /**
210
+ * Ordered parameter nodes.
211
+ */
212
+ params: ReadonlyArray<FunctionParameterNode | ParameterGroupNode>
213
+ }
214
+
215
+ /**
216
+ * Union of all function-parameter AST node variants used by the function-parameter printer.
217
+ */
218
+ export type FunctionParamNode = FunctionParameterNode | ParameterGroupNode | FunctionParametersNode | ParamsTypeNode
219
+
220
+ /**
221
+ * Handler map keys — one per `FunctionParamNode` kind.
222
+ */
223
+ export type FunctionNodeType = 'functionParameter' | 'parameterGroup' | 'functionParameters' | 'paramsType'
package/src/nodes/http.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * All IANA-registered HTTP status codes as string literals, matching how
3
- * they appear as keys in API specifications (e.g. `"200"`, `"404"`).
2
+ * All supported HTTP status code literals as strings, as used in API specs
3
+ * (for example, `"200"` and `"404"`).
4
4
  */
5
5
  export type HttpStatusCode =
6
6
  // 1xx Informational
@@ -72,13 +72,25 @@ export type HttpStatusCode =
72
72
  | '511'
73
73
 
74
74
  /**
75
- * A status code as used in an operation response: a specific HTTP status
76
- * code string or `"default"` as a catch-all fallback.
75
+ * Response status code literal used by operations.
76
+ *
77
+ * Includes specific HTTP status code strings and `"default"` for catch-all responses.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const status: StatusCode = '200'
82
+ * const fallback: StatusCode = 'default'
83
+ * ```
77
84
  */
78
85
  export type StatusCode = HttpStatusCode | 'default'
79
86
 
80
87
  /**
81
- * Common IANA media types used in API request/response bodies.
88
+ * Supported media type strings used in request and response bodies.
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * const mediaType: MediaType = 'application/json'
93
+ * ```
82
94
  */
83
95
  export type MediaType =
84
96
  // Application
@@ -1,17 +1,25 @@
1
+ import type { ArrowFunctionNode, ConstNode, FunctionNode, TypeNode } from './code.ts'
2
+ import type { ExportNode, FileNode, ImportNode, SourceNode } from './file.ts'
3
+ import type { FunctionParamNode, ParamsTypeNode } from './function.ts'
1
4
  import type { OperationNode } from './operation.ts'
5
+ import type { OutputNode } from './output.ts'
2
6
  import type { ParameterNode } from './parameter.ts'
3
7
  import type { PropertyNode } from './property.ts'
4
8
  import type { ResponseNode } from './response.ts'
5
- import type { RootNode } from './root.ts'
9
+ import type { InputNode } from './root.ts'
6
10
  import type { SchemaNode } from './schema.ts'
7
11
 
8
12
  export type { BaseNode, NodeKind } from './base.ts'
13
+ export type { ArrowFunctionNode, BreakNode, CodeNode, ConstNode, FunctionNode, JSDocNode, JsxNode, TextNode, TypeDeclarationNode, TypeNode } from './code.ts'
14
+ export type { ExportNode, FileNode, ImportNode, SourceNode } from './file.ts'
15
+ export type { FunctionNodeType, FunctionParameterNode, FunctionParametersNode, FunctionParamNode, ParameterGroupNode, ParamsTypeNode } from './function.ts'
9
16
  export type { HttpStatusCode, MediaType, StatusCode } from './http.ts'
10
17
  export type { HttpMethod, OperationNode } from './operation.ts'
18
+ export type { OutputNode } from './output.ts'
11
19
  export type { ParameterLocation, ParameterNode } from './parameter.ts'
12
20
  export type { PropertyNode } from './property.ts'
13
21
  export type { ResponseNode } from './response.ts'
14
- export type { RootMeta, RootNode } from './root.ts'
22
+ export type { InputMeta, InputNode } from './root.ts'
15
23
  export type {
16
24
  ArraySchemaNode,
17
25
  ComplexSchemaType,
@@ -19,7 +27,10 @@ export type {
19
27
  DatetimeSchemaNode,
20
28
  EnumSchemaNode,
21
29
  EnumValueNode,
30
+ FormatStringSchemaNode,
22
31
  IntersectionSchemaNode,
32
+ Ipv4SchemaNode,
33
+ Ipv6SchemaNode,
23
34
  NumberSchemaNode,
24
35
  ObjectSchemaNode,
25
36
  PrimitiveSchemaType,
@@ -37,10 +48,39 @@ export type {
37
48
  } from './schema.ts'
38
49
 
39
50
  /**
40
- * Discriminated union of every AST node.
51
+ * Union of all AST node types.
41
52
  *
42
- * Using a concrete union (instead of the bare `BaseNode` alias) lets
43
- * TypeScript narrow the type automatically inside `switch (node.kind)`
44
- * blocks, eliminating the need for manual `as TypeName` casts.
53
+ * This lets TypeScript narrow types in `switch (node.kind)` blocks.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * function getKind(node: Node): string {
58
+ * switch (node.kind) {
59
+ * case 'Input':
60
+ * return 'input'
61
+ * case 'Output':
62
+ * return 'output'
63
+ * default:
64
+ * return 'other'
65
+ * }
66
+ * }
67
+ * ```
45
68
  */
46
- export type Node = RootNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
69
+ export type Node =
70
+ | InputNode
71
+ | OutputNode
72
+ | OperationNode
73
+ | SchemaNode
74
+ | PropertyNode
75
+ | ParameterNode
76
+ | ResponseNode
77
+ | FunctionParamNode
78
+ | FileNode
79
+ | ImportNode
80
+ | ExportNode
81
+ | SourceNode
82
+ | ConstNode
83
+ | TypeNode
84
+ | ParamsTypeNode
85
+ | FunctionNode
86
+ | ArrowFunctionNode
@@ -6,28 +6,106 @@ import type { SchemaNode } from './schema.ts'
6
6
  export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE'
7
7
 
8
8
  /**
9
- * A spec-agnostic representation of a single API operation.
9
+ * AST node representing one API operation.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const operation: OperationNode = {
14
+ * kind: 'Operation',
15
+ * operationId: 'listPets',
16
+ * method: 'GET',
17
+ * path: '/pets',
18
+ * tags: [],
19
+ * parameters: [],
20
+ * responses: [],
21
+ * }
22
+ * ```
10
23
  */
11
24
  export type OperationNode = BaseNode & {
25
+ /**
26
+ * Node kind.
27
+ */
12
28
  kind: 'Operation'
13
29
  /**
14
- * Unique operation identifier (maps to `operationId` in OAS).
30
+ * Operation identifier, usually from OpenAPI `operationId`.
15
31
  */
16
32
  operationId: string
33
+ /**
34
+ * HTTP Method like 'GET'
35
+ */
17
36
  method: HttpMethod
18
37
  /**
19
- * Express-style path string, e.g. `/pets/:petId`.
20
- * Derived from the OpenAPI path by converting `{param}` tokens to `:param`.
38
+ * OpenAPI-style path string, for example `/pets/{petId}`.
39
+ * Path parameters retain the `{param}` notation from the original spec.
21
40
  */
22
41
  path: string
42
+ /**
43
+ * Group labels for the operation.
44
+ * Usually copied from OpenAPI `tags`.
45
+ */
23
46
  tags: Array<string>
47
+ /**
48
+ * Short one-line operation summary.
49
+ */
24
50
  summary?: string
51
+ /**
52
+ * Full operation description.
53
+ */
25
54
  description?: string
55
+ /**
56
+ * Marks the operation as deprecated.
57
+ */
26
58
  deprecated?: boolean
59
+ /**
60
+ * Parameters that could be used, we have QueryParams, PathParams, HeaderParams and CookieParams
61
+ */
27
62
  parameters: Array<ParameterNode>
28
63
  /**
29
- * Request body schema. For OAS, this is the schema of the first content entry.
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
+ }
107
+ /**
108
+ * Operation responses.
30
109
  */
31
- requestBody?: SchemaNode
32
110
  responses: Array<ResponseNode>
33
111
  }
@@ -0,0 +1,26 @@
1
+ import type { BaseNode } from './base.ts'
2
+ import type { FileNode } from './file.ts'
3
+
4
+ /**
5
+ * Output AST node that groups all generated file output for one API document.
6
+ *
7
+ * Produced by generators and consumed by the build pipeline to write files.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const output: OutputNode = {
12
+ * kind: 'Output',
13
+ * files: [],
14
+ * }
15
+ * ```
16
+ */
17
+ export type OutputNode = BaseNode & {
18
+ /**
19
+ * Node kind.
20
+ */
21
+ kind: 'Output'
22
+ /**
23
+ * Generated file nodes.
24
+ */
25
+ files: Array<FileNode>
26
+ }
@@ -4,12 +4,38 @@ import type { SchemaNode } from './schema.ts'
4
4
  export type ParameterLocation = 'path' | 'query' | 'header' | 'cookie'
5
5
 
6
6
  /**
7
- * A single input parameter for an operation.
7
+ * AST node representing one operation parameter.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const param: ParameterNode = {
12
+ * kind: 'Parameter',
13
+ * name: 'petId',
14
+ * in: 'path',
15
+ * schema: createSchema({ type: 'string' }),
16
+ * required: true,
17
+ * }
18
+ * ```
8
19
  */
9
20
  export type ParameterNode = BaseNode & {
21
+ /**
22
+ * Node kind.
23
+ */
10
24
  kind: 'Parameter'
25
+ /**
26
+ * Parameter name.
27
+ */
11
28
  name: string
29
+ /**
30
+ * Parameter location (`path`, `query`, `header`, or `cookie`).
31
+ */
12
32
  in: ParameterLocation
33
+ /**
34
+ * Parameter schema.
35
+ */
13
36
  schema: SchemaNode
37
+ /**
38
+ * Whether the parameter is required.
39
+ */
14
40
  required: boolean
15
41
  }
@@ -2,11 +2,33 @@ import type { BaseNode } from './base.ts'
2
2
  import type { SchemaNode } from './schema.ts'
3
3
 
4
4
  /**
5
- * A named property within an object schema.
5
+ * AST node representing one named object property.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const property: PropertyNode = {
10
+ * kind: 'Property',
11
+ * name: 'id',
12
+ * schema: createSchema({ type: 'integer' }),
13
+ * required: true,
14
+ * }
15
+ * ```
6
16
  */
7
17
  export type PropertyNode = BaseNode & {
18
+ /**
19
+ * Node kind.
20
+ */
8
21
  kind: 'Property'
22
+ /**
23
+ * Property key.
24
+ */
9
25
  name: string
26
+ /**
27
+ * Property schema.
28
+ */
10
29
  schema: SchemaNode
30
+ /**
31
+ * Whether the property is required.
32
+ */
11
33
  required: boolean
12
34
  }
@@ -3,15 +3,41 @@ import type { MediaType, StatusCode } from './http.ts'
3
3
  import type { SchemaNode } from './schema.ts'
4
4
 
5
5
  /**
6
- * A single response variant for an operation.
6
+ * AST node representing one operation response variant.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const response: ResponseNode = {
11
+ * kind: 'Response',
12
+ * statusCode: '200',
13
+ * schema: createSchema({ type: 'string' }),
14
+ * }
15
+ * ```
7
16
  */
8
17
  export type ResponseNode = BaseNode & {
18
+ /**
19
+ * Node kind.
20
+ */
9
21
  kind: 'Response'
10
22
  /**
11
23
  * HTTP status code or `'default'` for a fallback response.
12
24
  */
13
25
  statusCode: StatusCode
26
+ /**
27
+ * Optional response description.
28
+ */
14
29
  description?: string
30
+ /**
31
+ * Response body schema.
32
+ */
15
33
  schema: SchemaNode
16
- mediaType?: MediaType
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.
41
+ */
42
+ keysToOmit?: Array<string>
17
43
  }
package/src/nodes/root.ts CHANGED
@@ -3,10 +3,15 @@ import type { OperationNode } from './operation.ts'
3
3
  import type { SchemaNode } from './schema.ts'
4
4
 
5
5
  /**
6
- * Format-agnostic metadata about the API document.
7
- * Adapters populate whichever fields are available in their source format.
6
+ * Basic metadata for an API document.
7
+ * Adapters fill fields that exist in their source format.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const meta: InputMeta = { title: 'Pet API', version: '1.0.0' }
12
+ * ```
8
13
  */
9
- export type RootMeta = {
14
+ export type InputMeta = {
10
15
  /**
11
16
  * API title (from `info.title` in OAS/AsyncAPI).
12
17
  */
@@ -20,23 +25,40 @@ export type RootMeta = {
20
25
  */
21
26
  version?: string
22
27
  /**
23
- * Resolved base URL for the API.
24
- * OAS: derived from `servers[serverIndex].url` with variable substitution.
25
- * AsyncAPI: derived from `servers[serverIndex].url`.
26
- * Drizzle / schema-only formats: not set.
28
+ * Resolved API base URL.
29
+ * For OpenAPI and AsyncAPI, this comes from the selected server URL.
27
30
  */
28
31
  baseURL?: string
29
32
  }
30
33
 
31
34
  /**
32
- * Top-level container for all schemas and operations in a single API document.
35
+ * Input AST node that contains all schemas and operations for one API document.
36
+ * Produced by the adapter and consumed by all Kubb plugins.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const input: InputNode = {
41
+ * kind: 'Input',
42
+ * schemas: [],
43
+ * operations: [],
44
+ * }
45
+ * ```
33
46
  */
34
- export type RootNode = BaseNode & {
35
- kind: 'Root'
47
+ export type InputNode = BaseNode & {
48
+ /**
49
+ * Node kind.
50
+ */
51
+ kind: 'Input'
52
+ /**
53
+ * All schema nodes in the document.
54
+ */
36
55
  schemas: Array<SchemaNode>
56
+ /**
57
+ * All operation nodes in the document.
58
+ */
37
59
  operations: Array<OperationNode>
38
60
  /**
39
- * Format-agnostic document metadata populated by the adapter.
61
+ * Optional document metadata populated by the adapter.
40
62
  */
41
- meta?: RootMeta
63
+ meta?: InputMeta
42
64
  }