@kubb/ast 5.0.0-alpha.2 → 5.0.0-alpha.21
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/dist/index.cjs +1032 -129
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +275 -9
- package/dist/index.js +1009 -129
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/visitor-COwfCgLK.d.ts +1983 -0
- package/package.json +3 -2
- package/src/constants.ts +97 -4
- package/src/factory.ts +276 -18
- package/src/guards.ts +63 -8
- package/src/index.ts +32 -6
- package/src/infer.ts +130 -0
- package/src/mocks.ts +12 -5
- package/src/nodes/base.ts +31 -4
- package/src/nodes/function.ts +131 -0
- package/src/nodes/http.ts +17 -5
- package/src/nodes/index.ts +18 -5
- package/src/nodes/operation.ts +61 -4
- 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 +41 -10
- package/src/nodes/schema.ts +328 -38
- package/src/printers/functionPrinter.ts +196 -0
- package/src/printers/index.ts +3 -0
- package/src/printers/printer.ts +217 -0
- package/src/refs.ts +36 -4
- package/src/resolvers.ts +45 -0
- package/src/transformers.ts +196 -0
- package/src/types.ts +9 -2
- package/src/utils.ts +77 -0
- package/src/visitor.ts +376 -81
- package/dist/visitor-CmsfJzro.d.ts +0 -649
- package/src/printer.ts +0 -129
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import type { FunctionNode, FunctionNodeType } from '../nodes/function.ts'
|
|
2
|
+
import type { FunctionParameterNode, FunctionParametersNode, ObjectBindingParameterNode } from '../nodes/index.ts'
|
|
3
|
+
import type { PrinterFactoryOptions } from './printer.ts'
|
|
4
|
+
import { createPrinterFactory } from './printer.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Maps each function-printer handler key to its concrete node type.
|
|
8
|
+
*/
|
|
9
|
+
export type FunctionNodeByType = {
|
|
10
|
+
functionParameter: FunctionParameterNode
|
|
11
|
+
objectBindingParameter: ObjectBindingParameterNode
|
|
12
|
+
functionParameters: FunctionParametersNode
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const kindToHandlerKey = {
|
|
16
|
+
FunctionParameter: 'functionParameter',
|
|
17
|
+
ObjectBindingParameter: 'objectBindingParameter',
|
|
18
|
+
FunctionParameters: 'functionParameters',
|
|
19
|
+
} satisfies Record<string, FunctionNodeType>
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a function-parameter printer factory.
|
|
23
|
+
*
|
|
24
|
+
* This wrapper uses `createPrinterFactory` and dispatches handlers by `node.kind`
|
|
25
|
+
* (for function nodes) rather than by `node.type` (for schema nodes).
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { mode: 'declaration' | 'call' }, string>
|
|
30
|
+
*
|
|
31
|
+
* export const myPrinter = defineFunctionPrinter<MyPrinter>((options) => ({
|
|
32
|
+
* name: 'my',
|
|
33
|
+
* options,
|
|
34
|
+
* nodes: {
|
|
35
|
+
* functionParameter(node) {
|
|
36
|
+
* return options.mode === 'declaration' && node.type ? `${node.name}: ${node.type}` : node.name
|
|
37
|
+
* },
|
|
38
|
+
* objectBindingParameter(node) {
|
|
39
|
+
* const inner = node.properties.map(p => this.transform(p)).filter(Boolean).join(', ')
|
|
40
|
+
* return `{ ${inner} }`
|
|
41
|
+
* },
|
|
42
|
+
* functionParameters(node) {
|
|
43
|
+
* return node.params.map(p => this.transform(p)).filter(Boolean).join(', ')
|
|
44
|
+
* },
|
|
45
|
+
* },
|
|
46
|
+
* }))
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>((node) => kindToHandlerKey[node.kind])
|
|
50
|
+
|
|
51
|
+
export type FunctionPrinterOptions = {
|
|
52
|
+
/**
|
|
53
|
+
* Rendering modes supported by `functionPrinter`.
|
|
54
|
+
*
|
|
55
|
+
* | Mode | Output example | Use case |
|
|
56
|
+
* |---------------|---------------------------------------------|---------------------------------|
|
|
57
|
+
* | `declaration` | `id: string, config: Config = {}` | Function parameter declaration |
|
|
58
|
+
* | `call` | `id, { method, url }` | Function call arguments |
|
|
59
|
+
* | `keys` | `{ id, config }` | Key names only (destructuring) |
|
|
60
|
+
* | `values` | `{ id: id, config: config }` | Key/value object entries |
|
|
61
|
+
*/
|
|
62
|
+
mode: 'declaration' | 'call' | 'keys' | 'values'
|
|
63
|
+
/**
|
|
64
|
+
* Optional transformation applied to every parameter name before printing.
|
|
65
|
+
*/
|
|
66
|
+
transformName?: (name: string) => string
|
|
67
|
+
/**
|
|
68
|
+
* Optional transformation applied to every type string before printing.
|
|
69
|
+
*/
|
|
70
|
+
transformType?: (type: string) => string
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
type DefaultPrinter = PrinterFactoryOptions<'functionParameters', FunctionPrinterOptions, string>
|
|
74
|
+
|
|
75
|
+
function rank(param: FunctionParameterNode | ObjectBindingParameterNode): number {
|
|
76
|
+
if (param.kind === 'ObjectBindingParameter') {
|
|
77
|
+
if (param.default) return 2
|
|
78
|
+
const isOptional = param.optional ?? param.properties.every((p) => p.optional || p.default !== undefined)
|
|
79
|
+
return isOptional ? 1 : 0
|
|
80
|
+
}
|
|
81
|
+
if (param.rest) return 3
|
|
82
|
+
if (param.default) return 2
|
|
83
|
+
return param.optional ? 1 : 0
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function sortParams(params: Array<FunctionParameterNode | ObjectBindingParameterNode>): Array<FunctionParameterNode | ObjectBindingParameterNode> {
|
|
87
|
+
return [...params].sort((a, b) => rank(a) - rank(b))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function sortChildParams(params: Array<FunctionParameterNode>): Array<FunctionParameterNode> {
|
|
91
|
+
return [...params].sort((a, b) => rank(a) - rank(b))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Default function-signature printer.
|
|
96
|
+
* Covers the four standard output modes used across Kubb plugins.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const printer = functionPrinter({ mode: 'declaration' })
|
|
101
|
+
*
|
|
102
|
+
* const sig = createFunctionParameters({
|
|
103
|
+
* params: [
|
|
104
|
+
* createFunctionParameter({ name: 'petId', type: 'string', optional: false }),
|
|
105
|
+
* createFunctionParameter({ name: 'config', type: 'Config', optional: false, default: '{}' }),
|
|
106
|
+
* ],
|
|
107
|
+
* })
|
|
108
|
+
*
|
|
109
|
+
* printer.print(sig) // → "petId: string, config: Config = {}"
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export const functionPrinter = defineFunctionPrinter<DefaultPrinter>((options) => ({
|
|
113
|
+
name: 'functionParameters',
|
|
114
|
+
options,
|
|
115
|
+
nodes: {
|
|
116
|
+
functionParameter(node) {
|
|
117
|
+
const { mode, transformName, transformType } = this.options
|
|
118
|
+
const name = transformName ? transformName(node.name) : node.name
|
|
119
|
+
const type = node.type && transformType ? transformType(node.type) : node.type
|
|
120
|
+
|
|
121
|
+
if (mode === 'keys' || mode === 'values') {
|
|
122
|
+
return node.rest ? `...${name}` : name
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (mode === 'call') {
|
|
126
|
+
return node.rest ? `...${name}` : name
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (node.rest) {
|
|
130
|
+
return type ? `...${name}: ${type}` : `...${name}`
|
|
131
|
+
}
|
|
132
|
+
if (type) {
|
|
133
|
+
if (node.optional) return `${name}?: ${type}`
|
|
134
|
+
return node.default ? `${name}: ${type} = ${node.default}` : `${name}: ${type}`
|
|
135
|
+
}
|
|
136
|
+
return node.default ? `${name} = ${node.default}` : name
|
|
137
|
+
},
|
|
138
|
+
objectBindingParameter(node) {
|
|
139
|
+
const { mode, transformName, transformType } = this.options
|
|
140
|
+
const sorted = sortChildParams(node.properties)
|
|
141
|
+
const isOptional = node.optional ?? sorted.every((p) => p.optional || p.default !== undefined)
|
|
142
|
+
|
|
143
|
+
if (node.inline) {
|
|
144
|
+
return sorted
|
|
145
|
+
.map((p) => this.transform(p))
|
|
146
|
+
.filter(Boolean)
|
|
147
|
+
.join(', ')
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (mode === 'keys' || mode === 'values') {
|
|
151
|
+
const keys = sorted.map((p) => p.name).join(', ')
|
|
152
|
+
return `{ ${keys} }`
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (mode === 'call') {
|
|
156
|
+
const keys = sorted.map((p) => p.name).join(', ')
|
|
157
|
+
return `{ ${keys} }`
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const names = sorted.map((p) => {
|
|
161
|
+
const n = transformName ? transformName(p.name) : p.name
|
|
162
|
+
|
|
163
|
+
return n
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
const nameStr = names.length ? `{ ${names.join(', ')} }` : undefined
|
|
167
|
+
if (!nameStr) return null
|
|
168
|
+
|
|
169
|
+
let typeAnnotation = node.type
|
|
170
|
+
if (!typeAnnotation) {
|
|
171
|
+
const typeParts = sorted
|
|
172
|
+
.filter((p) => p.type)
|
|
173
|
+
.map((p) => {
|
|
174
|
+
const t = transformType && p.type ? transformType(p.type) : p.type!
|
|
175
|
+
return p.optional || p.default !== undefined ? `${p.name}?: ${t}` : `${p.name}: ${t}`
|
|
176
|
+
})
|
|
177
|
+
typeAnnotation = typeParts.length ? `{ ${typeParts.join('; ')} }` : undefined
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (typeAnnotation) {
|
|
181
|
+
if (isOptional) return `${nameStr}: ${typeAnnotation} = ${node.default ?? '{}'}`
|
|
182
|
+
return node.default ? `${nameStr}: ${typeAnnotation} = ${node.default}` : `${nameStr}: ${typeAnnotation}`
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return node.default ? `${nameStr} = ${node.default}` : nameStr
|
|
186
|
+
},
|
|
187
|
+
functionParameters(node) {
|
|
188
|
+
const sorted = sortParams(node.params)
|
|
189
|
+
|
|
190
|
+
return sorted
|
|
191
|
+
.map((p) => this.transform(p))
|
|
192
|
+
.filter(Boolean)
|
|
193
|
+
.join(', ')
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
}))
|
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
* Generic shape used by `definePrinter`.
|
|
47
|
+
*
|
|
48
|
+
* - `TName` — unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
49
|
+
* - `TOptions` — options passed to and stored on the printer instance
|
|
50
|
+
* - `TOutput` — the type emitted by node handlers
|
|
51
|
+
* - `TPrintOutput` — type returned by public `print` (defaults to `TOutput`)
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
59
|
+
name: TName
|
|
60
|
+
options: TOptions
|
|
61
|
+
output: TOutput
|
|
62
|
+
printOutput: TPrintOutput
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Printer instance returned by a printer factory.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
74
|
+
/**
|
|
75
|
+
* Unique identifier supplied at creation time.
|
|
76
|
+
*/
|
|
77
|
+
name: T['name']
|
|
78
|
+
/**
|
|
79
|
+
* Options for this printer instance.
|
|
80
|
+
*/
|
|
81
|
+
options: T['options']
|
|
82
|
+
/**
|
|
83
|
+
* Node-level dispatcher — converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
|
|
84
|
+
* Always dispatches through the `nodes` map; never calls the `print` override.
|
|
85
|
+
* Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
86
|
+
*/
|
|
87
|
+
transform: (node: SchemaNode) => T['output'] | null | undefined
|
|
88
|
+
/**
|
|
89
|
+
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
90
|
+
* higher-level function (which may produce full declarations).
|
|
91
|
+
* Otherwise, falls back to the node-level dispatcher.
|
|
92
|
+
*/
|
|
93
|
+
print: (node: SchemaNode) => T['printOutput'] | null | undefined
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Builder function passed to `definePrinter`.
|
|
98
|
+
*
|
|
99
|
+
* It receives resolved options and returns:
|
|
100
|
+
* - `name`
|
|
101
|
+
* - `options`
|
|
102
|
+
* - `nodes` handlers
|
|
103
|
+
* - optional top-level `print` override
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
111
|
+
name: T['name']
|
|
112
|
+
/**
|
|
113
|
+
* Options to store on the printer.
|
|
114
|
+
*/
|
|
115
|
+
options: T['options']
|
|
116
|
+
nodes: Partial<{
|
|
117
|
+
[K in SchemaType]: PrinterHandler<T['output'], T['options'], K>
|
|
118
|
+
}>
|
|
119
|
+
/**
|
|
120
|
+
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
121
|
+
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
122
|
+
* not the override itself — so recursion is safe.
|
|
123
|
+
*/
|
|
124
|
+
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Creates a schema printer factory.
|
|
129
|
+
*
|
|
130
|
+
* This function wraps a builder and makes options optional at call sites.
|
|
131
|
+
*
|
|
132
|
+
* The builder receives resolved options and returns:
|
|
133
|
+
* - `name` — a unique identifier for the printer
|
|
134
|
+
* - `options` — options stored on the returned printer instance
|
|
135
|
+
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
136
|
+
* - `print` _(optional)_ — top-level override exposed as `printer.print`
|
|
137
|
+
* - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
|
|
138
|
+
* - This keeps recursion safe and avoids self-calls
|
|
139
|
+
*
|
|
140
|
+
* When no `print` override is provided, `printer.print` falls back to `printer.transform` (the node-level dispatcher).
|
|
141
|
+
*
|
|
142
|
+
* @example Basic usage — Zod schema printer
|
|
143
|
+
* ```ts
|
|
144
|
+
* type ZodPrinter = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
145
|
+
*
|
|
146
|
+
* export const zodPrinter = definePrinter<ZodPrinter>((options) => ({
|
|
147
|
+
* name: 'zod',
|
|
148
|
+
* options: { strict: options.strict ?? true },
|
|
149
|
+
* nodes: {
|
|
150
|
+
* string: () => 'z.string()',
|
|
151
|
+
* object(node) {
|
|
152
|
+
* const props = node.properties.map(p => `${p.name}: ${this.transform(p.schema)}`).join(', ')
|
|
153
|
+
* return `z.object({ ${props} })`
|
|
154
|
+
* },
|
|
155
|
+
* },
|
|
156
|
+
* }))
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {
|
|
160
|
+
return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
165
|
+
**
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>(
|
|
169
|
+
* (node) => kindToHandlerKey[node.kind],
|
|
170
|
+
* )
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | undefined) {
|
|
174
|
+
return function <T extends PrinterFactoryOptions>(
|
|
175
|
+
build: (options: T['options']) => {
|
|
176
|
+
name: T['name']
|
|
177
|
+
options: T['options']
|
|
178
|
+
nodes: Partial<{
|
|
179
|
+
[K in TKey]: (
|
|
180
|
+
this: { transform: (node: TNode) => T['output'] | null | undefined; options: T['options'] },
|
|
181
|
+
node: TNodeByKey[K],
|
|
182
|
+
) => T['output'] | null | undefined
|
|
183
|
+
}>
|
|
184
|
+
print?: (this: { transform: (node: TNode) => T['output'] | null | undefined; options: T['options'] }, node: TNode) => T['printOutput'] | null | undefined
|
|
185
|
+
},
|
|
186
|
+
): (options?: T['options']) => {
|
|
187
|
+
name: T['name']
|
|
188
|
+
options: T['options']
|
|
189
|
+
transform: (node: TNode) => T['output'] | null | undefined
|
|
190
|
+
print: (node: TNode) => T['printOutput'] | null | undefined
|
|
191
|
+
} {
|
|
192
|
+
return (options) => {
|
|
193
|
+
const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))
|
|
194
|
+
|
|
195
|
+
const context = {
|
|
196
|
+
options: resolvedOptions,
|
|
197
|
+
transform: (node: TNode): T['output'] | null | undefined => {
|
|
198
|
+
const key = getKey(node)
|
|
199
|
+
if (key === undefined) return null
|
|
200
|
+
|
|
201
|
+
const handler = nodes[key]
|
|
202
|
+
|
|
203
|
+
if (!handler) return null
|
|
204
|
+
|
|
205
|
+
return (handler as (this: typeof context, node: TNode) => T['output'] | null | undefined).call(context, node)
|
|
206
|
+
},
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
name,
|
|
211
|
+
options: resolvedOptions,
|
|
212
|
+
transform: context.transform,
|
|
213
|
+
print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null | undefined,
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
package/src/refs.ts
CHANGED
|
@@ -2,12 +2,34 @@ import type { RootNode } 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
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function extractRefName(ref: string): string {
|
|
20
|
+
return ref.split('/').at(-1) ?? ref
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Builds a `RefMap` from `root.schemas` using each schema's `name`.
|
|
25
|
+
*
|
|
26
|
+
* Unnamed schemas are skipped.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const refMap = buildRefMap(root)
|
|
31
|
+
* const pet = refMap.get('Pet')
|
|
32
|
+
* ```
|
|
11
33
|
*/
|
|
12
34
|
export function buildRefMap(root: RootNode): RefMap {
|
|
13
35
|
const map: RefMap = new Map()
|
|
@@ -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
|
+
}
|