@kubb/ast 5.0.0-beta.7 → 5.0.0-beta.70
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 +53 -27
- package/dist/defineMacro-Bh5E_Il6.cjs +114 -0
- package/dist/defineMacro-Bh5E_Il6.cjs.map +1 -0
- package/dist/defineMacro-CVrU3ajV.js +98 -0
- package/dist/defineMacro-CVrU3ajV.js.map +1 -0
- package/dist/defineMacro-D9ekmTW5.d.ts +466 -0
- package/dist/index-q_ldM1YP.d.ts +2422 -0
- package/dist/index.cjs +198 -2239
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +97 -3399
- package/dist/index.js +149 -2160
- package/dist/index.js.map +1 -1
- package/dist/macros.cjs +130 -0
- package/dist/macros.cjs.map +1 -0
- package/dist/macros.d.ts +61 -0
- package/dist/macros.js +128 -0
- package/dist/macros.js.map +1 -0
- package/dist/operationParams-Dme8H-sR.d.ts +211 -0
- package/dist/refs-8IwpesXW.cjs +157 -0
- package/dist/refs-8IwpesXW.cjs.map +1 -0
- package/dist/refs-D2qwT5ck.js +117 -0
- package/dist/refs-D2qwT5ck.js.map +1 -0
- package/dist/rolldown-runtime-CNktS9qV.js +17 -0
- package/dist/types-Df1jVmRy.d.ts +244 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +1 -0
- package/dist/utils.cjs +808 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.ts +381 -0
- package/dist/utils.js +777 -0
- package/dist/utils.js.map +1 -0
- package/dist/visitor-CLLDwBvv.cjs +1743 -0
- package/dist/visitor-CLLDwBvv.cjs.map +1 -0
- package/dist/visitor-DG5ROqRx.js +1307 -0
- package/dist/visitor-DG5ROqRx.js.map +1 -0
- package/package.json +17 -5
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/constants.ts +0 -228
- package/src/factory.ts +0 -742
- package/src/guards.ts +0 -110
- package/src/index.ts +0 -46
- package/src/infer.ts +0 -130
- package/src/mocks.ts +0 -176
- package/src/nodes/base.ts +0 -56
- package/src/nodes/code.ts +0 -304
- package/src/nodes/file.ts +0 -230
- package/src/nodes/function.ts +0 -223
- package/src/nodes/http.ts +0 -119
- package/src/nodes/index.ts +0 -86
- package/src/nodes/operation.ts +0 -111
- package/src/nodes/output.ts +0 -26
- package/src/nodes/parameter.ts +0 -41
- package/src/nodes/property.ts +0 -34
- package/src/nodes/response.ts +0 -43
- package/src/nodes/root.ts +0 -64
- package/src/nodes/schema.ts +0 -656
- package/src/printer.ts +0 -250
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/transformers.ts +0 -159
- package/src/types.ts +0 -70
- package/src/utils.ts +0 -915
- package/src/visitor.ts +0 -592
package/src/printer.ts
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'
|
|
2
|
-
|
|
3
|
-
/**
|
|
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
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
17
|
-
/**
|
|
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.
|
|
20
|
-
*/
|
|
21
|
-
transform: (node: SchemaNode) => TOutput | null | undefined
|
|
22
|
-
/**
|
|
23
|
-
* Options for this printer instance.
|
|
24
|
-
*/
|
|
25
|
-
options: TOptions
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
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
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
|
|
41
|
-
this: PrinterHandlerContext<TOutput, TOptions>,
|
|
42
|
-
node: SchemaNodeByType[T],
|
|
43
|
-
) => TOutput | null | undefined
|
|
44
|
-
|
|
45
|
-
/**
|
|
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`.
|
|
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
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
83
|
-
name: TName
|
|
84
|
-
options: TOptions
|
|
85
|
-
output: TOutput
|
|
86
|
-
printOutput: TPrintOutput
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Printer instance returned by a printer factory.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```ts
|
|
94
|
-
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
98
|
-
/**
|
|
99
|
-
* Unique identifier supplied at creation time.
|
|
100
|
-
*/
|
|
101
|
-
name: T['name']
|
|
102
|
-
/**
|
|
103
|
-
* Options for this printer instance.
|
|
104
|
-
*/
|
|
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
|
|
112
|
-
/**
|
|
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.
|
|
116
|
-
*/
|
|
117
|
-
print: (node: SchemaNode) => T['printOutput'] | null | undefined
|
|
118
|
-
}
|
|
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
|
-
*/
|
|
134
|
-
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
135
|
-
name: T['name']
|
|
136
|
-
/**
|
|
137
|
-
* Options to store on the printer.
|
|
138
|
-
*/
|
|
139
|
-
options: T['options']
|
|
140
|
-
nodes: Partial<{
|
|
141
|
-
[K in SchemaType]: PrinterHandler<T['output'], T['options'], K>
|
|
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
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Creates a schema printer factory.
|
|
153
|
-
*
|
|
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
|
|
167
|
-
* ```ts
|
|
168
|
-
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
169
|
-
*
|
|
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} })`
|
|
178
|
-
* },
|
|
179
|
-
* },
|
|
180
|
-
* }))
|
|
181
|
-
* ```
|
|
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
|
|
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
|
-
}
|
|
241
|
-
|
|
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
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
package/src/refs.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { InputNode } from './nodes/root.ts'
|
|
2
|
-
import type { SchemaNode } from './nodes/schema.ts'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Lookup map from schema name to `SchemaNode`.
|
|
6
|
-
*/
|
|
7
|
-
export type RefMap = Map<string, SchemaNode>
|
|
8
|
-
|
|
9
|
-
/**
|
|
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
|
-
* ```
|
|
18
|
-
*/
|
|
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 {
|
|
35
|
-
const map: RefMap = new Map()
|
|
36
|
-
|
|
37
|
-
for (const schema of input.schemas) {
|
|
38
|
-
if (schema.name) {
|
|
39
|
-
map.set(schema.name, schema)
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return map
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Resolves a schema by name from a `RefMap`.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```ts
|
|
50
|
-
* const petSchema = resolveRef(refMap, 'Pet')
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
export function resolveRef(refMap: RefMap, ref: string): SchemaNode | undefined {
|
|
54
|
-
return refMap.get(ref)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Converts a `RefMap` into a plain object.
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```ts
|
|
62
|
-
* const refsObject = refMapToObject(refMap)
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
export function refMapToObject(refMap: RefMap): Record<string, SchemaNode> {
|
|
66
|
-
return Object.fromEntries(refMap)
|
|
67
|
-
}
|
package/src/resolvers.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
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
|
-
}
|
package/src/transformers.ts
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
export type { VisitorDepth } from './constants.ts'
|
|
2
|
-
export type { DistributiveOmit } from './factory.ts'
|
|
3
|
-
export type { InferSchema, InferSchemaNode, ParserOptions } from './infer.ts'
|
|
4
|
-
export type {
|
|
5
|
-
ArraySchemaNode,
|
|
6
|
-
ArrowFunctionNode,
|
|
7
|
-
BaseNode,
|
|
8
|
-
BreakNode,
|
|
9
|
-
CodeNode,
|
|
10
|
-
ComplexSchemaType,
|
|
11
|
-
ConstNode,
|
|
12
|
-
DateSchemaNode,
|
|
13
|
-
DatetimeSchemaNode,
|
|
14
|
-
EnumSchemaNode,
|
|
15
|
-
EnumValueNode,
|
|
16
|
-
ExportNode,
|
|
17
|
-
FileNode,
|
|
18
|
-
FormatStringSchemaNode,
|
|
19
|
-
FunctionNode,
|
|
20
|
-
FunctionNodeType,
|
|
21
|
-
FunctionParameterNode,
|
|
22
|
-
FunctionParametersNode,
|
|
23
|
-
FunctionParamNode,
|
|
24
|
-
HttpMethod,
|
|
25
|
-
HttpStatusCode,
|
|
26
|
-
ImportNode,
|
|
27
|
-
InputMeta,
|
|
28
|
-
InputNode,
|
|
29
|
-
IntersectionSchemaNode,
|
|
30
|
-
Ipv4SchemaNode,
|
|
31
|
-
Ipv6SchemaNode,
|
|
32
|
-
JSDocNode,
|
|
33
|
-
JsxNode,
|
|
34
|
-
MediaType,
|
|
35
|
-
Node,
|
|
36
|
-
NodeKind,
|
|
37
|
-
NumberSchemaNode,
|
|
38
|
-
ObjectSchemaNode,
|
|
39
|
-
OperationNode,
|
|
40
|
-
OutputNode,
|
|
41
|
-
ParameterGroupNode,
|
|
42
|
-
ParameterLocation,
|
|
43
|
-
ParameterNode,
|
|
44
|
-
ParamsTypeNode,
|
|
45
|
-
PrimitiveSchemaType,
|
|
46
|
-
PropertyNode,
|
|
47
|
-
RefSchemaNode,
|
|
48
|
-
ResponseNode,
|
|
49
|
-
ScalarSchemaNode,
|
|
50
|
-
ScalarSchemaType,
|
|
51
|
-
SchemaNode,
|
|
52
|
-
SchemaNodeByType,
|
|
53
|
-
SchemaType,
|
|
54
|
-
SourceNode,
|
|
55
|
-
SpecialSchemaType,
|
|
56
|
-
StatusCode,
|
|
57
|
-
StringSchemaNode,
|
|
58
|
-
TextNode,
|
|
59
|
-
TimeSchemaNode,
|
|
60
|
-
TypeDeclarationNode,
|
|
61
|
-
TypeNode,
|
|
62
|
-
UnionSchemaNode,
|
|
63
|
-
UrlSchemaNode,
|
|
64
|
-
} from './nodes/index.ts'
|
|
65
|
-
export type { RefMap } from './refs.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'
|