@kubb/ast 5.0.0-alpha.5 → 5.0.0-alpha.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -10
- package/dist/index.cjs +1643 -433
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3318 -21
- package/dist/index.js +1585 -423
- package/dist/index.js.map +1 -1
- package/package.json +23 -34
- package/src/constants.ts +126 -5
- package/src/factory.ts +680 -22
- package/src/guards.ts +77 -9
- package/src/index.ts +37 -6
- package/src/infer.ts +130 -0
- package/src/mocks.ts +101 -25
- package/src/nodes/base.ts +44 -4
- package/src/nodes/code.ts +304 -0
- package/src/nodes/file.ts +230 -0
- package/src/nodes/function.ts +223 -0
- package/src/nodes/http.ts +17 -5
- package/src/nodes/index.ts +47 -7
- package/src/nodes/operation.ts +69 -6
- package/src/nodes/output.ts +26 -0
- package/src/nodes/parameter.ts +27 -1
- package/src/nodes/property.ts +23 -1
- package/src/nodes/response.ts +29 -3
- package/src/nodes/root.ts +38 -12
- package/src/nodes/schema.ts +414 -42
- package/src/printer.ts +181 -60
- package/src/refs.ts +39 -7
- package/src/resolvers.ts +45 -0
- package/src/transformers.ts +159 -0
- package/src/types.ts +29 -4
- package/src/utils.ts +703 -10
- package/src/visitor.ts +408 -96
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +0 -2
- package/dist/types.js +0 -1
- package/dist/visitor-CE4-xBHW.d.ts +0 -674
package/src/printer.ts
CHANGED
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Runtime context passed as `this` to printer handlers.
|
|
5
|
+
*
|
|
6
|
+
* `this.transform` dispatches to node-level handlers from `nodes`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const context: PrinterHandlerContext<string, {}> = {
|
|
11
|
+
* options: {},
|
|
12
|
+
* transform: () => 'value',
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
6
15
|
*/
|
|
7
16
|
export type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
8
17
|
/**
|
|
9
|
-
* Recursively
|
|
18
|
+
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
|
|
19
|
+
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
|
|
10
20
|
*/
|
|
11
|
-
|
|
21
|
+
transform: (node: SchemaNode) => TOutput | null | undefined
|
|
12
22
|
/**
|
|
13
23
|
* Options for this printer instance.
|
|
14
24
|
*/
|
|
@@ -16,8 +26,16 @@ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
|
16
26
|
}
|
|
17
27
|
|
|
18
28
|
/**
|
|
19
|
-
* Handler for
|
|
20
|
-
*
|
|
29
|
+
* Handler for one schema node type.
|
|
30
|
+
*
|
|
31
|
+
* Use a regular function (not an arrow function) if you need `this`.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const handler: PrinterHandler<string, {}, 'string'> = function () {
|
|
36
|
+
* return 'string'
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
21
39
|
*/
|
|
22
40
|
export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
|
|
23
41
|
this: PrinterHandlerContext<TOutput, TOptions>,
|
|
@@ -25,22 +43,56 @@ export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaTyp
|
|
|
25
43
|
) => TOutput | null | undefined
|
|
26
44
|
|
|
27
45
|
/**
|
|
28
|
-
*
|
|
29
|
-
* Mirrors `AdapterFactoryOptions` / `PluginFactoryOptions` from `@kubb/core`.
|
|
46
|
+
* Partial map of per-node-type handler overrides for a printer.
|
|
30
47
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
48
|
+
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
|
|
49
|
+
* Supply only the handlers you want to replace; the printer's built-in
|
|
50
|
+
* defaults fill in the rest.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* pluginZod({
|
|
55
|
+
* printer: {
|
|
56
|
+
* nodes: {
|
|
57
|
+
* date(): string {
|
|
58
|
+
* return 'z.string().date()'
|
|
59
|
+
* },
|
|
60
|
+
* } satisfies PrinterPartial<string, PrinterZodOptions>,
|
|
61
|
+
* },
|
|
62
|
+
* })
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export type PrinterPartial<TOutput, TOptions extends object> = Partial<{
|
|
66
|
+
[K in SchemaType]: PrinterHandler<TOutput, TOptions, K>
|
|
67
|
+
}>
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Generic shape used by `definePrinter`.
|
|
71
|
+
*
|
|
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
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
80
|
+
* ```
|
|
34
81
|
*/
|
|
35
|
-
export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown> = {
|
|
82
|
+
export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
36
83
|
name: TName
|
|
37
84
|
options: TOptions
|
|
38
85
|
output: TOutput
|
|
86
|
+
printOutput: TPrintOutput
|
|
39
87
|
}
|
|
40
88
|
|
|
41
89
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
90
|
+
* Printer instance returned by a printer factory.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
95
|
+
* ```
|
|
44
96
|
*/
|
|
45
97
|
export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
46
98
|
/**
|
|
@@ -52,15 +104,33 @@ export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
|
52
104
|
*/
|
|
53
105
|
options: T['options']
|
|
54
106
|
/**
|
|
55
|
-
*
|
|
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
|
+
* Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
56
110
|
*/
|
|
57
|
-
|
|
111
|
+
transform: (node: SchemaNode) => T['output'] | null | undefined
|
|
58
112
|
/**
|
|
59
|
-
*
|
|
113
|
+
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
114
|
+
* higher-level function (which may produce full declarations).
|
|
115
|
+
* Otherwise, falls back to the node-level dispatcher.
|
|
60
116
|
*/
|
|
61
|
-
|
|
117
|
+
print: (node: SchemaNode) => T['printOutput'] | null | undefined
|
|
62
118
|
}
|
|
63
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Builder function passed to `definePrinter`.
|
|
122
|
+
*
|
|
123
|
+
* It receives resolved options and returns:
|
|
124
|
+
* - `name`
|
|
125
|
+
* - `options`
|
|
126
|
+
* - `nodes` handlers
|
|
127
|
+
* - optional top-level `print` override
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
64
134
|
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
65
135
|
name: T['name']
|
|
66
136
|
/**
|
|
@@ -70,60 +140,111 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
|
|
|
70
140
|
nodes: Partial<{
|
|
71
141
|
[K in SchemaType]: PrinterHandler<T['output'], T['options'], K>
|
|
72
142
|
}>
|
|
143
|
+
/**
|
|
144
|
+
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
145
|
+
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
146
|
+
* not the override itself — so recursion is safe.
|
|
147
|
+
*/
|
|
148
|
+
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
|
|
73
149
|
}
|
|
74
150
|
|
|
75
151
|
/**
|
|
76
|
-
* Creates a
|
|
77
|
-
* from `@kubb/core` — wraps a builder to make options optional and separates raw options
|
|
78
|
-
* from resolved options.
|
|
152
|
+
* Creates a schema printer factory.
|
|
79
153
|
*
|
|
80
|
-
*
|
|
154
|
+
* This function wraps a builder and makes options optional at call sites.
|
|
155
|
+
*
|
|
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
|
+
*
|
|
164
|
+
* When no `print` override is provided, `printer.print` falls back to `printer.transform` (the node-level dispatcher).
|
|
165
|
+
*
|
|
166
|
+
* @example Basic usage — Zod schema printer
|
|
81
167
|
* ```ts
|
|
82
|
-
* type
|
|
168
|
+
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
83
169
|
*
|
|
84
|
-
* export const zodPrinter = definePrinter<
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* },
|
|
93
|
-
* object(node) {
|
|
94
|
-
* const props = node.properties
|
|
95
|
-
* ?.map(p => `${p.name}: ${this.print(p)}`)
|
|
96
|
-
* .join(', ') ?? ''
|
|
97
|
-
* return `z.object({ ${props} })`
|
|
98
|
-
* },
|
|
170
|
+
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
171
|
+
* name: 'zod',
|
|
172
|
+
* options: { strict: options.strict ?? true },
|
|
173
|
+
* nodes: {
|
|
174
|
+
* string: () => 'z.string()',
|
|
175
|
+
* object(node) {
|
|
176
|
+
* const props = node.properties.map(p => `${p.name}: ${this.transform(p.schema)}`).join(', ')
|
|
177
|
+
* return `z.object({ ${props} })`
|
|
99
178
|
* },
|
|
100
|
-
* }
|
|
101
|
-
* })
|
|
102
|
-
*
|
|
103
|
-
* const printer = zodPrinter({ strict: false })
|
|
104
|
-
* printer.name // 'zod'
|
|
105
|
-
* printer.options // { strict: false }
|
|
106
|
-
* printer.print(node) // 'z.string()'
|
|
179
|
+
* },
|
|
180
|
+
* }))
|
|
107
181
|
* ```
|
|
108
182
|
*/
|
|
109
183
|
export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {
|
|
110
|
-
return (options) =>
|
|
111
|
-
|
|
184
|
+
return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>
|
|
185
|
+
}
|
|
112
186
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
189
|
+
**
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>(
|
|
193
|
+
* (node) => kindToHandlerKey[node.kind],
|
|
194
|
+
* )
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | undefined) {
|
|
198
|
+
return function <T extends PrinterFactoryOptions>(
|
|
199
|
+
build: (options: T['options']) => {
|
|
200
|
+
name: T['name']
|
|
201
|
+
options: T['options']
|
|
202
|
+
nodes: Partial<{
|
|
203
|
+
[K in TKey]: (
|
|
204
|
+
this: {
|
|
205
|
+
transform: (node: TNode) => T['output'] | null | undefined
|
|
206
|
+
options: T['options']
|
|
207
|
+
},
|
|
208
|
+
node: TNodeByKey[K],
|
|
209
|
+
) => T['output'] | null | undefined
|
|
210
|
+
}>
|
|
211
|
+
print?: (
|
|
212
|
+
this: {
|
|
213
|
+
transform: (node: TNode) => T['output'] | null | undefined
|
|
214
|
+
options: T['options']
|
|
215
|
+
},
|
|
216
|
+
node: TNode,
|
|
217
|
+
) => T['printOutput'] | null | undefined
|
|
218
|
+
},
|
|
219
|
+
): (options?: T['options']) => {
|
|
220
|
+
name: T['name']
|
|
221
|
+
options: T['options']
|
|
222
|
+
transform: (node: TNode) => T['output'] | null | undefined
|
|
223
|
+
print: (node: TNode) => T['printOutput'] | null | undefined
|
|
224
|
+
} {
|
|
225
|
+
return (options) => {
|
|
226
|
+
const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))
|
|
227
|
+
|
|
228
|
+
const context = {
|
|
229
|
+
options: resolvedOptions,
|
|
230
|
+
transform: (node: TNode): T['output'] | null | undefined => {
|
|
231
|
+
const key = getKey(node)
|
|
232
|
+
if (key === undefined) return null
|
|
233
|
+
|
|
234
|
+
const handler = nodes[key]
|
|
235
|
+
|
|
236
|
+
if (!handler) return null
|
|
237
|
+
|
|
238
|
+
return (handler as (this: typeof context, node: TNode) => T['output'] | null | undefined).call(context, node)
|
|
239
|
+
},
|
|
240
|
+
}
|
|
121
241
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
242
|
+
return {
|
|
243
|
+
name,
|
|
244
|
+
options: resolvedOptions,
|
|
245
|
+
transform: context.transform,
|
|
246
|
+
print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null | undefined,
|
|
247
|
+
}
|
|
127
248
|
}
|
|
128
249
|
}
|
|
129
250
|
}
|
package/src/refs.ts
CHANGED
|
@@ -1,18 +1,40 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { InputNode } from './nodes/root.ts'
|
|
2
2
|
import type { SchemaNode } from './nodes/schema.ts'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Lookup map from schema name to `SchemaNode`.
|
|
6
6
|
*/
|
|
7
7
|
export type RefMap = Map<string, SchemaNode>
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Returns the last path segment of a reference string.
|
|
11
|
+
*
|
|
12
|
+
* Example: `#/components/schemas/Pet` becomes `Pet`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* extractRefName('#/components/schemas/Pet') // 'Pet'
|
|
17
|
+
* ```
|
|
11
18
|
*/
|
|
12
|
-
export function
|
|
19
|
+
export function extractRefName(ref: string): string {
|
|
20
|
+
return ref.split('/').at(-1) ?? ref
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Builds a `RefMap` from `input.schemas` using each schema's `name`.
|
|
25
|
+
*
|
|
26
|
+
* Unnamed schemas are skipped.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const refMap = buildRefMap(input)
|
|
31
|
+
* const pet = refMap.get('Pet')
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function buildRefMap(input: InputNode): RefMap {
|
|
13
35
|
const map: RefMap = new Map()
|
|
14
36
|
|
|
15
|
-
for (const schema of
|
|
37
|
+
for (const schema of input.schemas) {
|
|
16
38
|
if (schema.name) {
|
|
17
39
|
map.set(schema.name, schema)
|
|
18
40
|
}
|
|
@@ -21,14 +43,24 @@ export function buildRefMap(root: RootNode): RefMap {
|
|
|
21
43
|
}
|
|
22
44
|
|
|
23
45
|
/**
|
|
24
|
-
*
|
|
46
|
+
* Resolves a schema by name from a `RefMap`.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const petSchema = resolveRef(refMap, 'Pet')
|
|
51
|
+
* ```
|
|
25
52
|
*/
|
|
26
53
|
export function resolveRef(refMap: RefMap, ref: string): SchemaNode | undefined {
|
|
27
54
|
return refMap.get(ref)
|
|
28
55
|
}
|
|
29
56
|
|
|
30
57
|
/**
|
|
31
|
-
* Converts a `RefMap`
|
|
58
|
+
* Converts a `RefMap` into a plain object.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* const refsObject = refMapToObject(refMap)
|
|
63
|
+
* ```
|
|
32
64
|
*/
|
|
33
65
|
export function refMapToObject(refMap: RefMap): Record<string, SchemaNode> {
|
|
34
66
|
return Object.fromEntries(refMap)
|
package/src/resolvers.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { pascalCase } from '@internals/utils'
|
|
2
|
+
import { narrowSchema } from './guards.ts'
|
|
3
|
+
import type { SchemaNode } from './nodes/schema.ts'
|
|
4
|
+
import { extractRefName } from './refs.ts'
|
|
5
|
+
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
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Collects import entries for all `ref` schema nodes in `node`.
|
|
22
|
+
*/
|
|
23
|
+
export function collectImports<TImport>({
|
|
24
|
+
node,
|
|
25
|
+
nameMapping,
|
|
26
|
+
resolve,
|
|
27
|
+
}: {
|
|
28
|
+
node: SchemaNode
|
|
29
|
+
nameMapping: Map<string, string>
|
|
30
|
+
resolve: (schemaName: string) => TImport | undefined
|
|
31
|
+
}): Array<TImport> {
|
|
32
|
+
return collect<TImport>(node, {
|
|
33
|
+
schema(schemaNode): TImport | undefined {
|
|
34
|
+
const schemaRef = narrowSchema(schemaNode, 'ref')
|
|
35
|
+
if (!schemaRef?.ref) return
|
|
36
|
+
|
|
37
|
+
const rawName = extractRefName(schemaRef.ref)
|
|
38
|
+
const schemaName = nameMapping.get(rawName) ?? rawName
|
|
39
|
+
const result = resolve(schemaName)
|
|
40
|
+
if (!result) return
|
|
41
|
+
|
|
42
|
+
return result
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { isScalarPrimitive } from './constants.ts'
|
|
2
|
+
import { createProperty, createSchema } from './factory.ts'
|
|
3
|
+
import { narrowSchema } from './guards.ts'
|
|
4
|
+
import type { SchemaNode } from './nodes/schema.ts'
|
|
5
|
+
import { enumPropName } from './resolvers.ts'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Replaces a discriminator property's schema with a string enum of allowed values.
|
|
9
|
+
*
|
|
10
|
+
* If `node` is not an object schema, or if the property does not exist, the input
|
|
11
|
+
* node is returned as-is.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const schema = createSchema({
|
|
16
|
+
* type: 'object',
|
|
17
|
+
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
|
|
18
|
+
* })
|
|
19
|
+
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function setDiscriminatorEnum({
|
|
23
|
+
node,
|
|
24
|
+
propertyName,
|
|
25
|
+
values,
|
|
26
|
+
enumName,
|
|
27
|
+
}: {
|
|
28
|
+
node: SchemaNode
|
|
29
|
+
propertyName: string
|
|
30
|
+
values: Array<string>
|
|
31
|
+
enumName?: string
|
|
32
|
+
}): SchemaNode {
|
|
33
|
+
const objectNode = narrowSchema(node, 'object')
|
|
34
|
+
if (!objectNode?.properties?.length) {
|
|
35
|
+
return node
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const hasProperty = objectNode.properties.some((prop) => prop.name === propertyName)
|
|
39
|
+
if (!hasProperty) {
|
|
40
|
+
return node
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return createSchema({
|
|
44
|
+
...objectNode,
|
|
45
|
+
properties: objectNode.properties.map((prop) => {
|
|
46
|
+
if (prop.name !== propertyName) {
|
|
47
|
+
return prop
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return createProperty({
|
|
51
|
+
...prop,
|
|
52
|
+
schema: createSchema({
|
|
53
|
+
type: 'enum',
|
|
54
|
+
primitive: 'string',
|
|
55
|
+
enumValues: values,
|
|
56
|
+
name: enumName,
|
|
57
|
+
readOnly: prop.schema.readOnly,
|
|
58
|
+
writeOnly: prop.schema.writeOnly,
|
|
59
|
+
}),
|
|
60
|
+
})
|
|
61
|
+
}),
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Merges adjacent anonymous object members into a single anonymous object member.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const merged = mergeAdjacentObjects([
|
|
71
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
|
|
72
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
|
|
73
|
+
* ])
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode> {
|
|
77
|
+
return members.reduce<Array<SchemaNode>>((acc, member) => {
|
|
78
|
+
const objectMember = narrowSchema(member, 'object')
|
|
79
|
+
if (objectMember && !objectMember.name) {
|
|
80
|
+
const previous = acc.at(-1)
|
|
81
|
+
const previousObject = previous ? narrowSchema(previous, 'object') : undefined
|
|
82
|
+
|
|
83
|
+
if (previousObject && !previousObject.name) {
|
|
84
|
+
acc[acc.length - 1] = createSchema({
|
|
85
|
+
...previousObject,
|
|
86
|
+
properties: [...(previousObject.properties ?? []), ...(objectMember.properties ?? [])],
|
|
87
|
+
})
|
|
88
|
+
return acc
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
acc.push(member)
|
|
93
|
+
return acc
|
|
94
|
+
}, [])
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Removes enum members that are covered by broader scalar primitives in the same union.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* const simplified = simplifyUnion([
|
|
103
|
+
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
|
|
104
|
+
* createSchema({ type: 'string' }),
|
|
105
|
+
* ])
|
|
106
|
+
* // keeps only string member
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode> {
|
|
110
|
+
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))
|
|
111
|
+
|
|
112
|
+
if (!scalarPrimitives.size) {
|
|
113
|
+
return members
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return members.filter((member) => {
|
|
117
|
+
const enumNode = narrowSchema(member, 'enum')
|
|
118
|
+
if (!enumNode) {
|
|
119
|
+
return true
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const primitive = enumNode.primitive
|
|
123
|
+
if (!primitive) {
|
|
124
|
+
return true
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0
|
|
128
|
+
if (enumValueCount <= 1) {
|
|
129
|
+
return true
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (scalarPrimitives.has(primitive)) {
|
|
133
|
+
return false
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) {
|
|
137
|
+
return false
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return true
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode {
|
|
145
|
+
const enumNode = narrowSchema(propNode, 'enum')
|
|
146
|
+
|
|
147
|
+
if (enumNode?.primitive === 'boolean') {
|
|
148
|
+
return { ...propNode, name: undefined }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (enumNode) {
|
|
152
|
+
return {
|
|
153
|
+
...propNode,
|
|
154
|
+
name: enumPropName(parentName, propName, enumSuffix),
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return propNode
|
|
159
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,42 +1,67 @@
|
|
|
1
1
|
export type { VisitorDepth } from './constants.ts'
|
|
2
2
|
export type { DistributiveOmit } from './factory.ts'
|
|
3
|
+
export type { InferSchema, InferSchemaNode, ParserOptions } from './infer.ts'
|
|
3
4
|
export type {
|
|
4
5
|
ArraySchemaNode,
|
|
6
|
+
ArrowFunctionNode,
|
|
5
7
|
BaseNode,
|
|
8
|
+
BreakNode,
|
|
9
|
+
CodeNode,
|
|
6
10
|
ComplexSchemaType,
|
|
11
|
+
ConstNode,
|
|
7
12
|
DateSchemaNode,
|
|
8
13
|
DatetimeSchemaNode,
|
|
9
14
|
EnumSchemaNode,
|
|
10
15
|
EnumValueNode,
|
|
16
|
+
ExportNode,
|
|
17
|
+
FileNode,
|
|
18
|
+
FormatStringSchemaNode,
|
|
19
|
+
FunctionNode,
|
|
20
|
+
FunctionNodeType,
|
|
21
|
+
FunctionParameterNode,
|
|
22
|
+
FunctionParametersNode,
|
|
23
|
+
FunctionParamNode,
|
|
11
24
|
HttpMethod,
|
|
12
25
|
HttpStatusCode,
|
|
26
|
+
ImportNode,
|
|
27
|
+
InputMeta,
|
|
28
|
+
InputNode,
|
|
13
29
|
IntersectionSchemaNode,
|
|
30
|
+
Ipv4SchemaNode,
|
|
31
|
+
Ipv6SchemaNode,
|
|
32
|
+
JSDocNode,
|
|
33
|
+
JsxNode,
|
|
14
34
|
MediaType,
|
|
15
35
|
Node,
|
|
16
36
|
NodeKind,
|
|
17
37
|
NumberSchemaNode,
|
|
18
38
|
ObjectSchemaNode,
|
|
19
39
|
OperationNode,
|
|
40
|
+
OutputNode,
|
|
41
|
+
ParameterGroupNode,
|
|
20
42
|
ParameterLocation,
|
|
21
43
|
ParameterNode,
|
|
44
|
+
ParamsTypeNode,
|
|
22
45
|
PrimitiveSchemaType,
|
|
23
46
|
PropertyNode,
|
|
24
47
|
RefSchemaNode,
|
|
25
48
|
ResponseNode,
|
|
26
|
-
RootMeta,
|
|
27
|
-
RootNode,
|
|
28
49
|
ScalarSchemaNode,
|
|
29
50
|
ScalarSchemaType,
|
|
30
51
|
SchemaNode,
|
|
31
52
|
SchemaNodeByType,
|
|
32
53
|
SchemaType,
|
|
54
|
+
SourceNode,
|
|
33
55
|
SpecialSchemaType,
|
|
34
56
|
StatusCode,
|
|
35
57
|
StringSchemaNode,
|
|
58
|
+
TextNode,
|
|
36
59
|
TimeSchemaNode,
|
|
60
|
+
TypeDeclarationNode,
|
|
61
|
+
TypeNode,
|
|
37
62
|
UnionSchemaNode,
|
|
38
63
|
UrlSchemaNode,
|
|
39
64
|
} from './nodes/index.ts'
|
|
40
|
-
export type { Printer, PrinterFactoryOptions,
|
|
65
|
+
export type { Printer, PrinterFactoryOptions, PrinterPartial } from './printer.ts'
|
|
41
66
|
export type { RefMap } from './refs.ts'
|
|
42
|
-
export type { AsyncVisitor, CollectVisitor, Visitor } from './visitor.ts'
|
|
67
|
+
export type { AsyncVisitor, CollectOptions, CollectVisitor, ParentOf, TransformOptions, Visitor, VisitorContext, WalkOptions } from './visitor.ts'
|