@kubb/ast 5.0.0-alpha.8 → 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.
- package/README.md +24 -10
- package/dist/index.cjs +1975 -531
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3379 -24
- package/dist/index.js +1911 -519
- package/dist/index.js.map +1 -1
- package/package.json +23 -34
- package/src/constants.ts +133 -15
- package/src/factory.ts +680 -22
- package/src/guards.ts +77 -9
- package/src/index.ts +44 -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 +84 -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 +34 -12
- package/src/nodes/schema.ts +419 -42
- package/src/printer.ts +152 -59
- package/src/refs.ts +39 -7
- package/src/resolvers.ts +45 -0
- package/src/transformers.ts +159 -0
- package/src/types.ts +32 -4
- package/src/utils.ts +799 -14
- package/src/visitor.ts +411 -96
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +0 -2
- package/dist/types.js +0 -1
- package/dist/visitor-CrkOJoGa.d.ts +0 -702
package/src/printer.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* `this.
|
|
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
|
+
* ```
|
|
7
15
|
*/
|
|
8
16
|
export type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
9
17
|
/**
|
|
10
|
-
* 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.
|
|
11
20
|
*/
|
|
12
|
-
|
|
21
|
+
transform: (node: SchemaNode) => TOutput | null | undefined
|
|
13
22
|
/**
|
|
14
23
|
* Options for this printer instance.
|
|
15
24
|
*/
|
|
@@ -17,8 +26,16 @@ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
|
17
26
|
}
|
|
18
27
|
|
|
19
28
|
/**
|
|
20
|
-
* Handler for
|
|
21
|
-
*
|
|
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
|
+
* ```
|
|
22
39
|
*/
|
|
23
40
|
export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
|
|
24
41
|
this: PrinterHandlerContext<TOutput, TOptions>,
|
|
@@ -26,13 +43,41 @@ export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaTyp
|
|
|
26
43
|
) => TOutput | null | undefined
|
|
27
44
|
|
|
28
45
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
46
|
+
* Partial map of per-node-type handler overrides for a printer.
|
|
47
|
+
*
|
|
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`.
|
|
31
71
|
*
|
|
32
72
|
* - `TName` — unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
33
|
-
* - `TOptions` — options passed to and stored on the printer
|
|
73
|
+
* - `TOptions` — options passed to and stored on the printer instance
|
|
34
74
|
* - `TOutput` — the type emitted by node handlers
|
|
35
|
-
* - `TPrintOutput` —
|
|
75
|
+
* - `TPrintOutput` — type returned by public `print` (defaults to `TOutput`)
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
80
|
+
* ```
|
|
36
81
|
*/
|
|
37
82
|
export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
38
83
|
name: TName
|
|
@@ -42,7 +87,12 @@ export type PrinterFactoryOptions<TName extends string = string, TOptions extend
|
|
|
42
87
|
}
|
|
43
88
|
|
|
44
89
|
/**
|
|
45
|
-
*
|
|
90
|
+
* Printer instance returned by a printer factory.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
95
|
+
* ```
|
|
46
96
|
*/
|
|
47
97
|
export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
48
98
|
/**
|
|
@@ -53,18 +103,33 @@ export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
|
53
103
|
* Options for this printer instance.
|
|
54
104
|
*/
|
|
55
105
|
options: T['options']
|
|
106
|
+
/**
|
|
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.
|
|
110
|
+
*/
|
|
111
|
+
transform: (node: SchemaNode) => T['output'] | null | undefined
|
|
56
112
|
/**
|
|
57
113
|
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
58
|
-
* higher-level function (which may produce full declarations).
|
|
59
|
-
* to the node-level dispatcher
|
|
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
|
|
|
64
120
|
/**
|
|
65
|
-
* Builder function passed to `definePrinter`.
|
|
66
|
-
*
|
|
67
|
-
*
|
|
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
|
+
* ```
|
|
68
133
|
*/
|
|
69
134
|
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
70
135
|
name: T['name']
|
|
@@ -77,81 +142,109 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
|
|
|
77
142
|
}>
|
|
78
143
|
/**
|
|
79
144
|
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
80
|
-
* `this.
|
|
145
|
+
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
81
146
|
* not the override itself — so recursion is safe.
|
|
82
147
|
*/
|
|
83
|
-
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
|
|
148
|
+
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
|
|
84
149
|
}
|
|
85
150
|
|
|
86
151
|
/**
|
|
87
|
-
* Creates a
|
|
88
|
-
*
|
|
89
|
-
*
|
|
152
|
+
* Creates a schema printer factory.
|
|
153
|
+
*
|
|
154
|
+
* This function wraps a builder and makes options optional at call sites.
|
|
90
155
|
*
|
|
91
156
|
* The builder receives resolved options and returns:
|
|
92
157
|
* - `name` — a unique identifier for the printer
|
|
93
158
|
* - `options` — options stored on the returned printer instance
|
|
94
159
|
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
95
|
-
* - `print` _(optional)_ —
|
|
96
|
-
* Inside
|
|
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
|
|
97
163
|
*
|
|
98
|
-
* When no `print` override is provided, `printer.print`
|
|
164
|
+
* When no `print` override is provided, `printer.print` falls back to `printer.transform` (the node-level dispatcher).
|
|
99
165
|
*
|
|
100
166
|
* @example Basic usage — Zod schema printer
|
|
101
167
|
* ```ts
|
|
102
|
-
* type
|
|
168
|
+
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
103
169
|
*
|
|
104
|
-
* export const zodPrinter = definePrinter<
|
|
170
|
+
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
105
171
|
* name: 'zod',
|
|
106
172
|
* options: { strict: options.strict ?? true },
|
|
107
173
|
* nodes: {
|
|
108
174
|
* string: () => 'z.string()',
|
|
109
175
|
* object(node) {
|
|
110
|
-
* const props = node.properties.map(p => `${p.name}: ${this.
|
|
176
|
+
* const props = node.properties.map(p => `${p.name}: ${this.transform(p.schema)}`).join(', ')
|
|
111
177
|
* return `z.object({ ${props} })`
|
|
112
178
|
* },
|
|
113
179
|
* },
|
|
114
180
|
* }))
|
|
115
181
|
* ```
|
|
116
|
-
|
|
117
|
-
|
|
182
|
+
*/
|
|
183
|
+
export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {
|
|
184
|
+
return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
189
|
+
**
|
|
190
|
+
* @example
|
|
118
191
|
* ```ts
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* name: 'ts',
|
|
123
|
-
* options,
|
|
124
|
-
* nodes: { string: () => factory.keywordTypeNodes.string },
|
|
125
|
-
* print(node) {
|
|
126
|
-
* const type = this.print(node) // calls the node-level dispatcher
|
|
127
|
-
* if (!type || !this.options.typeName) return type
|
|
128
|
-
* return factory.createTypeAliasDeclaration(this.options.typeName, type)
|
|
129
|
-
* },
|
|
130
|
-
* }))
|
|
192
|
+
* export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>(
|
|
193
|
+
* (node) => kindToHandlerKey[node.kind],
|
|
194
|
+
* )
|
|
131
195
|
* ```
|
|
132
196
|
*/
|
|
133
|
-
export function
|
|
134
|
-
return
|
|
135
|
-
|
|
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']))
|
|
136
227
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
|
142
233
|
|
|
143
|
-
|
|
234
|
+
const handler = nodes[key]
|
|
144
235
|
|
|
145
|
-
|
|
236
|
+
if (!handler) return null
|
|
146
237
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
238
|
+
return (handler as (this: typeof context, node: TNode) => T['output'] | null | undefined).call(context, node)
|
|
239
|
+
},
|
|
240
|
+
}
|
|
150
241
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
+
}
|
|
155
248
|
}
|
|
156
249
|
}
|
|
157
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,70 @@
|
|
|
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, PrinterHandler, PrinterHandlerContext } from './printer.ts'
|
|
41
65
|
export type { RefMap } from './refs.ts'
|
|
42
|
-
export type { AsyncVisitor, CollectVisitor, Visitor } from './visitor.ts'
|
|
66
|
+
export type { AsyncVisitor, CollectOptions, CollectVisitor, ParentOf, TransformOptions, Visitor, VisitorContext, WalkOptions } from './visitor.ts'
|
|
67
|
+
export type { Printer, PrinterFactoryOptions, PrinterPartial } from './printer.ts'
|
|
68
|
+
export type { ScalarPrimitive } from './constants.ts'
|
|
69
|
+
export type { OperationParamsResolver } from './utils.ts'
|
|
70
|
+
export type { UserFileNode } from './factory.ts'
|