@kubb/ast 5.0.0-beta.5 → 5.0.0-beta.51
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/LICENSE +17 -10
- package/README.md +28 -19
- package/dist/index.cjs +878 -788
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +33 -3326
- package/dist/index.js +863 -750
- package/dist/index.js.map +1 -1
- package/dist/types-BaaNZbSi.d.ts +3581 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/dist/utils-BIcKgbbc.js +626 -0
- package/dist/utils-BIcKgbbc.js.map +1 -0
- package/dist/utils-CMRZrT-w.cjs +794 -0
- package/dist/utils-CMRZrT-w.cjs.map +1 -0
- package/dist/utils.cjs +18 -0
- package/dist/utils.d.ts +205 -0
- package/dist/utils.js +2 -0
- package/package.json +13 -5
- package/src/constants.ts +10 -49
- package/src/dedupe.ts +200 -0
- package/src/dialect.ts +58 -0
- package/src/dispatch.ts +53 -0
- package/src/factory.ts +154 -21
- package/src/guards.ts +18 -48
- package/src/index.ts +11 -8
- package/src/infer.ts +16 -14
- package/src/nodes/base.ts +2 -0
- package/src/nodes/code.ts +22 -28
- package/src/nodes/content.ts +37 -0
- package/src/nodes/file.ts +17 -15
- package/src/nodes/function.ts +11 -11
- package/src/nodes/http.ts +1 -35
- package/src/nodes/index.ts +10 -12
- 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 +18 -15
- package/src/printer.ts +44 -38
- package/src/resolvers.ts +5 -19
- package/src/signature.ts +232 -0
- package/src/transformers.ts +21 -16
- package/src/types.ts +8 -18
- package/src/{utils.ts → utils/ast.ts} +125 -84
- package/src/utils/index.ts +295 -0
- package/src/visitor.ts +239 -281
- package/src/refs.ts +0 -67
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
package/src/nodes/response.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import type { BaseNode } from './base.ts'
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { ContentNode } from './content.ts'
|
|
3
|
+
import type { StatusCode } from './http.ts'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* AST node representing one operation response variant.
|
|
7
7
|
*
|
|
8
|
+
* Mirrors {@link OperationNode.requestBody}: the response body schemas live exclusively inside
|
|
9
|
+
* the `content` array (one entry per content type), so the same schema is never duplicated at the
|
|
10
|
+
* node root and inside `content`.
|
|
11
|
+
*
|
|
8
12
|
* @example
|
|
9
13
|
* ```ts
|
|
10
14
|
* const response: ResponseNode = {
|
|
11
15
|
* kind: 'Response',
|
|
12
16
|
* statusCode: '200',
|
|
13
|
-
* schema: createSchema({ type: 'string' }),
|
|
17
|
+
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'string' }) }],
|
|
14
18
|
* }
|
|
15
19
|
* ```
|
|
16
20
|
*/
|
|
@@ -28,16 +32,19 @@ export type ResponseNode = BaseNode & {
|
|
|
28
32
|
*/
|
|
29
33
|
description?: string
|
|
30
34
|
/**
|
|
31
|
-
*
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
*
|
|
35
|
+
* All available content type entries for this response.
|
|
36
|
+
*
|
|
37
|
+
* When the adapter `contentType` option is set, this array contains exactly one entry for that
|
|
38
|
+
* content type. Otherwise it contains one entry per content type declared in the spec, so that
|
|
39
|
+
* plugins can generate a union of response types (e.g. `application/json` and `application/xml`).
|
|
40
|
+
* Body-less responses keep a single entry whose `schema` is the empty/`void` placeholder.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // spec response declares both application/json and application/xml
|
|
45
|
+
* response.content[0].contentType // 'application/json'
|
|
46
|
+
* response.content[1].contentType // 'application/xml'
|
|
47
|
+
* ```
|
|
41
48
|
*/
|
|
42
|
-
|
|
49
|
+
content?: Array<ContentNode>
|
|
43
50
|
}
|
package/src/nodes/root.ts
CHANGED
|
@@ -3,32 +3,62 @@ import type { OperationNode } from './operation.ts'
|
|
|
3
3
|
import type { SchemaNode } from './schema.ts'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* Metadata for an API document, populated by the adapter and available to every generator.
|
|
7
|
+
*
|
|
8
|
+
* All fields are plain JSON-serializable values, no `Set`, no `Map`, no class instances.
|
|
9
|
+
* Computed fields (`circularNames`, `enumNames`) are pre-calculated once during the adapter
|
|
10
|
+
* pre-scan so generators never need to iterate the full schema list themselves.
|
|
8
11
|
*
|
|
9
12
|
* @example
|
|
10
13
|
* ```ts
|
|
11
|
-
* const meta: InputMeta = { title: 'Pet
|
|
14
|
+
* const meta: InputMeta = { title: 'Pet Store', version: '1.0.0', baseURL: 'https://petstore.swagger.io/v2', circularNames: [], enumNames: [] }
|
|
12
15
|
* ```
|
|
13
16
|
*/
|
|
14
17
|
export type InputMeta = {
|
|
15
18
|
/**
|
|
16
|
-
* API title
|
|
19
|
+
* API title from `info.title` in the source document.
|
|
17
20
|
*/
|
|
18
21
|
title?: string
|
|
19
22
|
/**
|
|
20
|
-
* API description
|
|
23
|
+
* API description from `info.description` in the source document.
|
|
21
24
|
*/
|
|
22
25
|
description?: string
|
|
23
26
|
/**
|
|
24
|
-
* API version string
|
|
27
|
+
* API version string from `info.version` in the source document.
|
|
25
28
|
*/
|
|
26
29
|
version?: string
|
|
27
30
|
/**
|
|
28
|
-
* Resolved
|
|
29
|
-
|
|
31
|
+
* Resolved base URL from the first matching server entry in the source document.
|
|
32
|
+
*/
|
|
33
|
+
baseURL?: string | null
|
|
34
|
+
/**
|
|
35
|
+
* Names of schemas that participate in a circular reference chain.
|
|
36
|
+
* Computed once during the adapter pre-scan, use this instead of calling
|
|
37
|
+
* `findCircularSchemas` per generator call.
|
|
38
|
+
*
|
|
39
|
+
* Convert to a `Set` once at the start of a generator, not per-schema,
|
|
40
|
+
* to keep lookup O(1) without repeated allocations.
|
|
41
|
+
*
|
|
42
|
+
* @example Wrap a circular schema in z.lazy()
|
|
43
|
+
* ```ts
|
|
44
|
+
* const circular = new Set(meta.circularNames)
|
|
45
|
+
* if (circular.has(schema.name)) { ... }
|
|
46
|
+
* ```
|
|
30
47
|
*/
|
|
31
|
-
|
|
48
|
+
circularNames: ReadonlyArray<string>
|
|
49
|
+
/**
|
|
50
|
+
* Names of schemas whose type is `enum`.
|
|
51
|
+
* Computed once during the adapter pre-scan, use this instead of filtering
|
|
52
|
+
* schemas per generator call.
|
|
53
|
+
*
|
|
54
|
+
* Convert to a `Set` once at the start of a generator when you need repeated
|
|
55
|
+
* membership checks, rather than calling `.includes()` per schema.
|
|
56
|
+
*
|
|
57
|
+
* @example Check if a referenced schema is an enum
|
|
58
|
+
* `const enums = new Set(meta.enumNames)`
|
|
59
|
+
* `const isEnum = enums.has(schemaName)`
|
|
60
|
+
*/
|
|
61
|
+
enumNames: ReadonlyArray<string>
|
|
32
62
|
}
|
|
33
63
|
|
|
34
64
|
/**
|
|
@@ -58,7 +88,39 @@ export type InputNode = BaseNode & {
|
|
|
58
88
|
*/
|
|
59
89
|
operations: Array<OperationNode>
|
|
60
90
|
/**
|
|
61
|
-
*
|
|
91
|
+
* Document metadata populated by the adapter.
|
|
92
|
+
*/
|
|
93
|
+
meta: InputMeta
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Streaming variant of `InputNode` for memory-efficient processing of large API specs.
|
|
98
|
+
*
|
|
99
|
+
* `schemas` and `operations` are `AsyncIterable` rather than arrays, each `for await`
|
|
100
|
+
* loop creates a fresh parse pass from the cached in-memory document, so multiple
|
|
101
|
+
* consumers (plugins) can iterate independently without keeping all nodes in memory.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* for await (const schema of inputStreamNode.schemas) {
|
|
106
|
+
* // only this one SchemaNode is live here. Previous ones are GC-eligible
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export type InputStreamNode = {
|
|
111
|
+
kind: 'Input'
|
|
112
|
+
/**
|
|
113
|
+
* Lazily parsed schema nodes. Each `for await` creates a fresh parse pass, so
|
|
114
|
+
* multiple plugins can iterate independently without sharing state.
|
|
115
|
+
*/
|
|
116
|
+
schemas: AsyncIterable<SchemaNode>
|
|
117
|
+
/**
|
|
118
|
+
* Lazily parsed operation nodes. Each `for await` creates a fresh parse pass, so
|
|
119
|
+
* multiple plugins can iterate independently without sharing state.
|
|
120
|
+
*/
|
|
121
|
+
operations: AsyncIterable<OperationNode>
|
|
122
|
+
/**
|
|
123
|
+
* Document metadata available immediately, before the first yielded node.
|
|
62
124
|
*/
|
|
63
125
|
meta?: InputMeta
|
|
64
126
|
}
|
package/src/nodes/schema.ts
CHANGED
|
@@ -58,12 +58,12 @@ export type PrimitiveSchemaType =
|
|
|
58
58
|
/**
|
|
59
59
|
* Composite schema types.
|
|
60
60
|
*/
|
|
61
|
-
|
|
61
|
+
type ComplexSchemaType = 'tuple' | 'union' | 'intersection' | 'enum'
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
64
|
* Schema types that need special handling in generators.
|
|
65
65
|
*/
|
|
66
|
-
|
|
66
|
+
type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'ipv4' | 'ipv6' | 'blob'
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* All schema type strings.
|
|
@@ -154,6 +154,10 @@ type SchemaNodeBase = BaseNode & {
|
|
|
154
154
|
* For example, this is `'string'` for a `uuid` schema.
|
|
155
155
|
*/
|
|
156
156
|
primitive?: PrimitiveSchemaType
|
|
157
|
+
/**
|
|
158
|
+
* Schema `format` value.
|
|
159
|
+
*/
|
|
160
|
+
format?: string
|
|
157
161
|
}
|
|
158
162
|
|
|
159
163
|
/**
|
|
@@ -174,7 +178,7 @@ export type ObjectSchemaNode = SchemaNodeBase & {
|
|
|
174
178
|
*/
|
|
175
179
|
type: 'object'
|
|
176
180
|
/**
|
|
177
|
-
* Primitive type
|
|
181
|
+
* Primitive type, always `'object'` for object schemas.
|
|
178
182
|
*/
|
|
179
183
|
primitive: 'object'
|
|
180
184
|
/**
|
|
@@ -302,7 +306,7 @@ export type IntersectionSchemaNode = CompositeSchemaNodeBase & {
|
|
|
302
306
|
/**
|
|
303
307
|
* One named enum item.
|
|
304
308
|
*/
|
|
305
|
-
|
|
309
|
+
type EnumValueNode = {
|
|
306
310
|
/**
|
|
307
311
|
* Enum item name.
|
|
308
312
|
*/
|
|
@@ -364,8 +368,9 @@ export type RefSchemaNode = SchemaNodeBase & {
|
|
|
364
368
|
type: 'ref'
|
|
365
369
|
/**
|
|
366
370
|
* Referenced schema name.
|
|
371
|
+
* `null` means Kubb has processed this and determined there is no applicable name.
|
|
367
372
|
*/
|
|
368
|
-
name?: string
|
|
373
|
+
name?: string | null
|
|
369
374
|
/**
|
|
370
375
|
* Original `$ref` path, for example, `#/components/schemas/Order`.
|
|
371
376
|
* Used to resolve names later.
|
|
@@ -376,14 +381,12 @@ export type RefSchemaNode = SchemaNodeBase & {
|
|
|
376
381
|
*/
|
|
377
382
|
pattern?: string
|
|
378
383
|
/**
|
|
379
|
-
* The fully-parsed schema
|
|
380
|
-
* Populated during OAS parsing when the
|
|
381
|
-
* `
|
|
382
|
-
*
|
|
383
|
-
* Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
|
|
384
|
-
* without following the reference manually.
|
|
384
|
+
* The fully-parsed schema this ref resolves to, so its structure (`primitive`, `properties`)
|
|
385
|
+
* can be read without following the reference. Populated during OAS parsing when the
|
|
386
|
+
* definition resolves, `null` when it can't or the ref is circular, and `undefined` when
|
|
387
|
+
* resolution has not been attempted.
|
|
385
388
|
*/
|
|
386
|
-
schema?: SchemaNode
|
|
389
|
+
schema?: SchemaNode | null
|
|
387
390
|
}
|
|
388
391
|
|
|
389
392
|
/**
|
|
@@ -556,7 +559,7 @@ export type UrlSchemaNode = SchemaNodeBase & {
|
|
|
556
559
|
* const uuidSchema: FormatStringSchemaNode = { kind: 'Schema', type: 'uuid', min: 36, max: 36 }
|
|
557
560
|
* ```
|
|
558
561
|
*/
|
|
559
|
-
|
|
562
|
+
type FormatStringSchemaNode = SchemaNodeBase & {
|
|
560
563
|
/**
|
|
561
564
|
* Schema type discriminator.
|
|
562
565
|
*/
|
|
@@ -579,7 +582,7 @@ export type FormatStringSchemaNode = SchemaNodeBase & {
|
|
|
579
582
|
* const ipv4Schema: Ipv4SchemaNode = { kind: 'Schema', type: 'ipv4' }
|
|
580
583
|
* ```
|
|
581
584
|
*/
|
|
582
|
-
|
|
585
|
+
type Ipv4SchemaNode = SchemaNodeBase & {
|
|
583
586
|
/**
|
|
584
587
|
* Schema type discriminator.
|
|
585
588
|
*/
|
|
@@ -594,7 +597,7 @@ export type Ipv4SchemaNode = SchemaNodeBase & {
|
|
|
594
597
|
* const ipv6Schema: Ipv6SchemaNode = { kind: 'Schema', type: 'ipv6' }
|
|
595
598
|
* ```
|
|
596
599
|
*/
|
|
597
|
-
|
|
600
|
+
type Ipv6SchemaNode = SchemaNodeBase & {
|
|
598
601
|
/**
|
|
599
602
|
* Schema type discriminator.
|
|
600
603
|
*/
|
package/src/printer.ts
CHANGED
|
@@ -13,12 +13,12 @@ import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'
|
|
|
13
13
|
* }
|
|
14
14
|
* ```
|
|
15
15
|
*/
|
|
16
|
-
|
|
16
|
+
type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
17
17
|
/**
|
|
18
18
|
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
|
|
19
19
|
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
|
|
20
20
|
*/
|
|
21
|
-
transform: (node: SchemaNode) => TOutput | null
|
|
21
|
+
transform: (node: SchemaNode) => TOutput | null
|
|
22
22
|
/**
|
|
23
23
|
* Options for this printer instance.
|
|
24
24
|
*/
|
|
@@ -37,16 +37,16 @@ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
|
37
37
|
* }
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
|
-
|
|
40
|
+
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
|
|
41
41
|
this: PrinterHandlerContext<TOutput, TOptions>,
|
|
42
42
|
node: SchemaNodeByType[T],
|
|
43
|
-
) => TOutput | null
|
|
43
|
+
) => TOutput | null
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* Partial map of per-node-type handler overrides for a printer.
|
|
47
47
|
*
|
|
48
48
|
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
|
|
49
|
-
* Supply only the handlers you want to replace
|
|
49
|
+
* Supply only the handlers you want to replace. The printer's built-in
|
|
50
50
|
* defaults fill in the rest.
|
|
51
51
|
*
|
|
52
52
|
* @example
|
|
@@ -69,10 +69,10 @@ export type PrinterPartial<TOutput, TOptions extends object> = Partial<{
|
|
|
69
69
|
/**
|
|
70
70
|
* Generic shape used by `definePrinter`.
|
|
71
71
|
*
|
|
72
|
-
* - `TName`
|
|
73
|
-
* - `TOptions`
|
|
74
|
-
* - `TOutput`
|
|
75
|
-
* - `TPrintOutput`
|
|
72
|
+
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
73
|
+
* - `TOptions` options passed to and stored on the printer instance
|
|
74
|
+
* - `TOutput` the type emitted by node handlers
|
|
75
|
+
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
|
|
76
76
|
*
|
|
77
77
|
* @example
|
|
78
78
|
* ```ts
|
|
@@ -104,17 +104,17 @@ export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
|
104
104
|
*/
|
|
105
105
|
options: T['options']
|
|
106
106
|
/**
|
|
107
|
-
* Node-level dispatcher
|
|
108
|
-
* Always dispatches through the `nodes` map
|
|
107
|
+
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
|
|
108
|
+
* Always dispatches through the `nodes` map. Never calls the `print` override.
|
|
109
109
|
* Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
110
110
|
*/
|
|
111
|
-
transform: (node: SchemaNode) => T['output'] | null
|
|
111
|
+
transform: (node: SchemaNode) => T['output'] | null
|
|
112
112
|
/**
|
|
113
113
|
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
114
114
|
* higher-level function (which may produce full declarations).
|
|
115
115
|
* Otherwise, falls back to the node-level dispatcher.
|
|
116
116
|
*/
|
|
117
|
-
print: (node: SchemaNode) => T['printOutput'] | null
|
|
117
|
+
print: (node: SchemaNode) => T['printOutput'] | null
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
/**
|
|
@@ -143,28 +143,32 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
|
|
|
143
143
|
/**
|
|
144
144
|
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
145
145
|
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
146
|
-
* not the override itself
|
|
146
|
+
* not the override itself, so recursion is safe.
|
|
147
147
|
*/
|
|
148
148
|
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
|
|
149
149
|
}
|
|
150
|
-
|
|
151
150
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
151
|
+
* Defines a schema printer: a function that takes a `SchemaNode` and emits
|
|
152
|
+
* code in your target language. Each plugin that produces code from schemas
|
|
153
|
+
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
|
|
154
|
+
* with this helper.
|
|
155
155
|
*
|
|
156
156
|
* The builder receives resolved options and returns:
|
|
157
|
-
* - `name` — a unique identifier for the printer
|
|
158
|
-
* - `options` — options stored on the returned printer instance
|
|
159
|
-
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
160
|
-
* - `print` _(optional)_ — top-level override exposed as `printer.print`
|
|
161
|
-
* - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
|
|
162
|
-
* - This keeps recursion safe and avoids self-calls
|
|
163
157
|
*
|
|
164
|
-
*
|
|
158
|
+
* - `name` unique identifier for the printer.
|
|
159
|
+
* - `options` stored on the returned printer instance.
|
|
160
|
+
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
|
|
161
|
+
* output (a string, a TypeScript AST node, ...) for that schema type.
|
|
162
|
+
* - `print` (optional), top-level override exposed as `printer.print`.
|
|
163
|
+
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
|
|
165
164
|
*
|
|
166
|
-
*
|
|
165
|
+
* Without a `print` override, `printer.print` falls back to `printer.transform`
|
|
166
|
+
* (the node-level dispatcher).
|
|
167
|
+
*
|
|
168
|
+
* @example Tiny Zod printer
|
|
167
169
|
* ```ts
|
|
170
|
+
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
171
|
+
*
|
|
168
172
|
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
169
173
|
*
|
|
170
174
|
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
@@ -173,7 +177,9 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
|
|
|
173
177
|
* nodes: {
|
|
174
178
|
* string: () => 'z.string()',
|
|
175
179
|
* object(node) {
|
|
176
|
-
* const props = node.properties
|
|
180
|
+
* const props = node.properties
|
|
181
|
+
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
182
|
+
* .join(', ')
|
|
177
183
|
* return `z.object({ ${props} })`
|
|
178
184
|
* },
|
|
179
185
|
* },
|
|
@@ -194,7 +200,7 @@ export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOp
|
|
|
194
200
|
* )
|
|
195
201
|
* ```
|
|
196
202
|
*/
|
|
197
|
-
export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey |
|
|
203
|
+
export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {
|
|
198
204
|
return function <T extends PrinterFactoryOptions>(
|
|
199
205
|
build: (options: T['options']) => {
|
|
200
206
|
name: T['name']
|
|
@@ -202,40 +208,40 @@ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey exte
|
|
|
202
208
|
nodes: Partial<{
|
|
203
209
|
[K in TKey]: (
|
|
204
210
|
this: {
|
|
205
|
-
transform: (node: TNode) => T['output'] | null
|
|
211
|
+
transform: (node: TNode) => T['output'] | null
|
|
206
212
|
options: T['options']
|
|
207
213
|
},
|
|
208
214
|
node: TNodeByKey[K],
|
|
209
|
-
) => T['output'] | null
|
|
215
|
+
) => T['output'] | null
|
|
210
216
|
}>
|
|
211
217
|
print?: (
|
|
212
218
|
this: {
|
|
213
|
-
transform: (node: TNode) => T['output'] | null
|
|
219
|
+
transform: (node: TNode) => T['output'] | null
|
|
214
220
|
options: T['options']
|
|
215
221
|
},
|
|
216
222
|
node: TNode,
|
|
217
|
-
) => T['printOutput'] | null
|
|
223
|
+
) => T['printOutput'] | null
|
|
218
224
|
},
|
|
219
225
|
): (options?: T['options']) => {
|
|
220
226
|
name: T['name']
|
|
221
227
|
options: T['options']
|
|
222
|
-
transform: (node: TNode) => T['output'] | null
|
|
223
|
-
print: (node: TNode) => T['printOutput'] | null
|
|
228
|
+
transform: (node: TNode) => T['output'] | null
|
|
229
|
+
print: (node: TNode) => T['printOutput'] | null
|
|
224
230
|
} {
|
|
225
231
|
return (options) => {
|
|
226
232
|
const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))
|
|
227
233
|
|
|
228
234
|
const context = {
|
|
229
235
|
options: resolvedOptions,
|
|
230
|
-
transform: (node: TNode): T['output'] | null
|
|
236
|
+
transform: (node: TNode): T['output'] | null => {
|
|
231
237
|
const key = getKey(node)
|
|
232
|
-
if (key ===
|
|
238
|
+
if (key === null) return null
|
|
233
239
|
|
|
234
240
|
const handler = nodes[key]
|
|
235
241
|
|
|
236
242
|
if (!handler) return null
|
|
237
243
|
|
|
238
|
-
return (handler as (this: typeof context, node: TNode) => T['output'] | null
|
|
244
|
+
return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)
|
|
239
245
|
},
|
|
240
246
|
}
|
|
241
247
|
|
|
@@ -243,7 +249,7 @@ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey exte
|
|
|
243
249
|
name,
|
|
244
250
|
options: resolvedOptions,
|
|
245
251
|
transform: context.transform,
|
|
246
|
-
print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null
|
|
252
|
+
print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,
|
|
247
253
|
}
|
|
248
254
|
}
|
|
249
255
|
}
|
package/src/resolvers.ts
CHANGED
|
@@ -1,21 +1,7 @@
|
|
|
1
|
-
import { pascalCase } from '@internals/utils'
|
|
2
1
|
import { narrowSchema } from './guards.ts'
|
|
3
2
|
import type { SchemaNode } from './nodes/schema.ts'
|
|
4
|
-
import { extractRefName } from './refs.ts'
|
|
5
3
|
import { collect } from './visitor.ts'
|
|
6
|
-
|
|
7
|
-
export function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null {
|
|
8
|
-
if (!mapping || !ref) return null
|
|
9
|
-
return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function childName(parentName: string | null | undefined, propName: string): string | null {
|
|
13
|
-
return parentName ? pascalCase([parentName, propName].join(' ')) : null
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {
|
|
17
|
-
return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))
|
|
18
|
-
}
|
|
4
|
+
import { extractRefName } from './utils/index.ts'
|
|
19
5
|
|
|
20
6
|
/**
|
|
21
7
|
* Collects import entries for all `ref` schema nodes in `node`.
|
|
@@ -27,17 +13,17 @@ export function collectImports<TImport>({
|
|
|
27
13
|
}: {
|
|
28
14
|
node: SchemaNode
|
|
29
15
|
nameMapping: Map<string, string>
|
|
30
|
-
resolve: (schemaName: string) => TImport |
|
|
16
|
+
resolve: (schemaName: string) => TImport | null
|
|
31
17
|
}): Array<TImport> {
|
|
32
18
|
return collect<TImport>(node, {
|
|
33
|
-
schema(schemaNode): TImport |
|
|
19
|
+
schema(schemaNode): TImport | null {
|
|
34
20
|
const schemaRef = narrowSchema(schemaNode, 'ref')
|
|
35
|
-
if (!schemaRef?.ref) return
|
|
21
|
+
if (!schemaRef?.ref) return null
|
|
36
22
|
|
|
37
23
|
const rawName = extractRefName(schemaRef.ref)
|
|
38
24
|
const schemaName = nameMapping.get(rawName) ?? rawName
|
|
39
25
|
const result = resolve(schemaName)
|
|
40
|
-
if (!result) return
|
|
26
|
+
if (!result) return null
|
|
41
27
|
|
|
42
28
|
return result
|
|
43
29
|
},
|