@kubb/ast 5.0.0-alpha.7 → 5.0.0-alpha.71
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 +1918 -528
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3409 -21
- package/dist/index.js +1854 -516
- package/dist/index.js.map +1 -1
- package/package.json +23 -34
- package/src/constants.ts +120 -17
- 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 +835 -10
- 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-DagMPgGo.d.ts +0 -702
package/dist/index.js
CHANGED
|
@@ -1,44 +1,163 @@
|
|
|
1
1
|
import "./chunk--u3MIqq1.js";
|
|
2
|
-
import {
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import path from "node:path";
|
|
3
4
|
//#region src/constants.ts
|
|
4
5
|
const visitorDepths = {
|
|
5
6
|
shallow: "shallow",
|
|
6
7
|
deep: "deep"
|
|
7
8
|
};
|
|
8
9
|
const nodeKinds = {
|
|
9
|
-
|
|
10
|
+
input: "Input",
|
|
11
|
+
output: "Output",
|
|
10
12
|
operation: "Operation",
|
|
11
13
|
schema: "Schema",
|
|
12
14
|
property: "Property",
|
|
13
15
|
parameter: "Parameter",
|
|
14
|
-
response: "Response"
|
|
16
|
+
response: "Response",
|
|
17
|
+
functionParameter: "FunctionParameter",
|
|
18
|
+
parameterGroup: "ParameterGroup",
|
|
19
|
+
functionParameters: "FunctionParameters",
|
|
20
|
+
type: "Type",
|
|
21
|
+
file: "File",
|
|
22
|
+
import: "Import",
|
|
23
|
+
export: "Export",
|
|
24
|
+
source: "Source",
|
|
25
|
+
text: "Text",
|
|
26
|
+
break: "Break"
|
|
15
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Canonical schema type strings used by AST schema nodes.
|
|
30
|
+
*
|
|
31
|
+
* These values are used across the AST as stable discriminators
|
|
32
|
+
* (for example `schema.type === schemaTypes.object`).
|
|
33
|
+
*
|
|
34
|
+
* The map is grouped by intent:
|
|
35
|
+
* - primitives (`string`, `number`, `boolean`, ...)
|
|
36
|
+
* - structural/composite (`object`, `array`, `union`, ...)
|
|
37
|
+
* - special OpenAPI-oriented types (`ref`, `datetime`, `uuid`, ...)
|
|
38
|
+
*/
|
|
16
39
|
const schemaTypes = {
|
|
40
|
+
/**
|
|
41
|
+
* Text value.
|
|
42
|
+
*/
|
|
17
43
|
string: "string",
|
|
44
|
+
/**
|
|
45
|
+
* Floating-point number (`float`, `double`).
|
|
46
|
+
*/
|
|
18
47
|
number: "number",
|
|
48
|
+
/**
|
|
49
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
50
|
+
*/
|
|
19
51
|
integer: "integer",
|
|
52
|
+
/**
|
|
53
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
54
|
+
*/
|
|
20
55
|
bigint: "bigint",
|
|
56
|
+
/**
|
|
57
|
+
* Boolean value
|
|
58
|
+
*/
|
|
21
59
|
boolean: "boolean",
|
|
60
|
+
/**
|
|
61
|
+
* Explicit null value.
|
|
62
|
+
*/
|
|
22
63
|
null: "null",
|
|
64
|
+
/**
|
|
65
|
+
* Any value (no type restriction).
|
|
66
|
+
*/
|
|
23
67
|
any: "any",
|
|
68
|
+
/**
|
|
69
|
+
* Unknown value (must be narrowed before usage).
|
|
70
|
+
*/
|
|
24
71
|
unknown: "unknown",
|
|
72
|
+
/**
|
|
73
|
+
* No return value (`void`).
|
|
74
|
+
*/
|
|
25
75
|
void: "void",
|
|
76
|
+
/**
|
|
77
|
+
* Object with named properties.
|
|
78
|
+
*/
|
|
26
79
|
object: "object",
|
|
80
|
+
/**
|
|
81
|
+
* Sequential list of items.
|
|
82
|
+
*/
|
|
27
83
|
array: "array",
|
|
84
|
+
/**
|
|
85
|
+
* Fixed-length list with position-specific items.
|
|
86
|
+
*/
|
|
28
87
|
tuple: "tuple",
|
|
88
|
+
/**
|
|
89
|
+
* "One of" multiple schema members.
|
|
90
|
+
*/
|
|
29
91
|
union: "union",
|
|
92
|
+
/**
|
|
93
|
+
* "All of" multiple schema members.
|
|
94
|
+
*/
|
|
30
95
|
intersection: "intersection",
|
|
96
|
+
/**
|
|
97
|
+
* Enum schema.
|
|
98
|
+
*/
|
|
31
99
|
enum: "enum",
|
|
100
|
+
/**
|
|
101
|
+
* Reference to another schema.
|
|
102
|
+
*/
|
|
32
103
|
ref: "ref",
|
|
104
|
+
/**
|
|
105
|
+
* Calendar date (for example `2026-03-24`).
|
|
106
|
+
*/
|
|
33
107
|
date: "date",
|
|
108
|
+
/**
|
|
109
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
110
|
+
*/
|
|
34
111
|
datetime: "datetime",
|
|
112
|
+
/**
|
|
113
|
+
* Time-only value (for example `09:00:00`).
|
|
114
|
+
*/
|
|
35
115
|
time: "time",
|
|
116
|
+
/**
|
|
117
|
+
* UUID value.
|
|
118
|
+
*/
|
|
36
119
|
uuid: "uuid",
|
|
120
|
+
/**
|
|
121
|
+
* Email address value.
|
|
122
|
+
*/
|
|
37
123
|
email: "email",
|
|
124
|
+
/**
|
|
125
|
+
* URL value.
|
|
126
|
+
*/
|
|
38
127
|
url: "url",
|
|
128
|
+
/**
|
|
129
|
+
* IPv4 address value.
|
|
130
|
+
*/
|
|
131
|
+
ipv4: "ipv4",
|
|
132
|
+
/**
|
|
133
|
+
* IPv6 address value.
|
|
134
|
+
*/
|
|
135
|
+
ipv6: "ipv6",
|
|
136
|
+
/**
|
|
137
|
+
* Binary/blob value.
|
|
138
|
+
*/
|
|
39
139
|
blob: "blob",
|
|
140
|
+
/**
|
|
141
|
+
* Impossible value (`never`).
|
|
142
|
+
*/
|
|
40
143
|
never: "never"
|
|
41
144
|
};
|
|
145
|
+
/**
|
|
146
|
+
* Primitive scalar schema types used when simplifying union members.
|
|
147
|
+
*/
|
|
148
|
+
const SCALAR_PRIMITIVE_TYPES = new Set([
|
|
149
|
+
"string",
|
|
150
|
+
"number",
|
|
151
|
+
"integer",
|
|
152
|
+
"bigint",
|
|
153
|
+
"boolean"
|
|
154
|
+
]);
|
|
155
|
+
/**
|
|
156
|
+
* Returns `true` when `type` is a scalar primitive schema type.
|
|
157
|
+
*/
|
|
158
|
+
function isScalarPrimitive(type) {
|
|
159
|
+
return SCALAR_PRIMITIVE_TYPES.has(type);
|
|
160
|
+
}
|
|
42
161
|
const httpMethods = {
|
|
43
162
|
get: "GET",
|
|
44
163
|
post: "POST",
|
|
@@ -71,196 +190,7 @@ const mediaTypes = {
|
|
|
71
190
|
videoMp4: "video/mp4"
|
|
72
191
|
};
|
|
73
192
|
//#endregion
|
|
74
|
-
//#region src/
|
|
75
|
-
/**
|
|
76
|
-
* Creates a `RootNode`.
|
|
77
|
-
*/
|
|
78
|
-
function createRoot(overrides = {}) {
|
|
79
|
-
return {
|
|
80
|
-
schemas: [],
|
|
81
|
-
operations: [],
|
|
82
|
-
...overrides,
|
|
83
|
-
kind: "Root"
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Creates an `OperationNode`.
|
|
88
|
-
*/
|
|
89
|
-
function createOperation(props) {
|
|
90
|
-
return {
|
|
91
|
-
tags: [],
|
|
92
|
-
parameters: [],
|
|
93
|
-
responses: [],
|
|
94
|
-
...props,
|
|
95
|
-
kind: "Operation"
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
function createSchema(props) {
|
|
99
|
-
if (props["type"] === "object") return {
|
|
100
|
-
properties: [],
|
|
101
|
-
...props,
|
|
102
|
-
kind: "Schema"
|
|
103
|
-
};
|
|
104
|
-
return {
|
|
105
|
-
...props,
|
|
106
|
-
kind: "Schema"
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Creates a `PropertyNode`. `required` defaults to `false`.
|
|
111
|
-
*/
|
|
112
|
-
function createProperty(props) {
|
|
113
|
-
return {
|
|
114
|
-
required: false,
|
|
115
|
-
...props,
|
|
116
|
-
kind: "Property"
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Creates a `ParameterNode`. `required` defaults to `false`.
|
|
121
|
-
*/
|
|
122
|
-
function createParameter(props) {
|
|
123
|
-
return {
|
|
124
|
-
required: false,
|
|
125
|
-
...props,
|
|
126
|
-
kind: "Parameter"
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Creates a `ResponseNode`.
|
|
131
|
-
*/
|
|
132
|
-
function createResponse(props) {
|
|
133
|
-
return {
|
|
134
|
-
...props,
|
|
135
|
-
kind: "Response"
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
//#endregion
|
|
139
|
-
//#region src/guards.ts
|
|
140
|
-
/**
|
|
141
|
-
* Narrows a `SchemaNode` to the specific variant matching `type`.
|
|
142
|
-
*/
|
|
143
|
-
function narrowSchema(node, type) {
|
|
144
|
-
return node?.type === type ? node : void 0;
|
|
145
|
-
}
|
|
146
|
-
function isKind(kind) {
|
|
147
|
-
return (node) => node.kind === kind;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Type guard for `RootNode`.
|
|
151
|
-
*/
|
|
152
|
-
const isRootNode = isKind("Root");
|
|
153
|
-
/**
|
|
154
|
-
* Type guard for `OperationNode`.
|
|
155
|
-
*/
|
|
156
|
-
const isOperationNode = isKind("Operation");
|
|
157
|
-
/**
|
|
158
|
-
* Type guard for `SchemaNode`.
|
|
159
|
-
*/
|
|
160
|
-
const isSchemaNode = isKind("Schema");
|
|
161
|
-
/**
|
|
162
|
-
* Type guard for `PropertyNode`.
|
|
163
|
-
*/
|
|
164
|
-
const isPropertyNode = isKind("Property");
|
|
165
|
-
/**
|
|
166
|
-
* Type guard for `ParameterNode`.
|
|
167
|
-
*/
|
|
168
|
-
const isParameterNode = isKind("Parameter");
|
|
169
|
-
/**
|
|
170
|
-
* Type guard for `ResponseNode`.
|
|
171
|
-
*/
|
|
172
|
-
const isResponseNode = isKind("Response");
|
|
173
|
-
//#endregion
|
|
174
|
-
//#region src/printer.ts
|
|
175
|
-
/**
|
|
176
|
-
* Creates a named printer factory. Mirrors the `definePlugin` / `defineAdapter` pattern
|
|
177
|
-
* from `@kubb/core` — wraps a builder to make options optional and separates raw options
|
|
178
|
-
* from resolved options.
|
|
179
|
-
*
|
|
180
|
-
* The builder receives resolved options and returns:
|
|
181
|
-
* - `name` — a unique identifier for the printer
|
|
182
|
-
* - `options` — options stored on the returned printer instance
|
|
183
|
-
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
184
|
-
* - `print` _(optional)_ — a root-level override that becomes the public `printer.print`.
|
|
185
|
-
* Inside it, `this.print(node)` still dispatches to the `nodes` map — safe recursion, no infinite loop.
|
|
186
|
-
*
|
|
187
|
-
* When no `print` override is provided, `printer.print` is the node-level dispatcher directly.
|
|
188
|
-
*
|
|
189
|
-
* @example Basic usage — Zod schema printer
|
|
190
|
-
* ```ts
|
|
191
|
-
* type ZodPrinter = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
192
|
-
*
|
|
193
|
-
* export const zodPrinter = definePrinter<ZodPrinter>((options) => ({
|
|
194
|
-
* name: 'zod',
|
|
195
|
-
* options: { strict: options.strict ?? true },
|
|
196
|
-
* nodes: {
|
|
197
|
-
* string: () => 'z.string()',
|
|
198
|
-
* object(node) {
|
|
199
|
-
* const props = node.properties.map(p => `${p.name}: ${this.print(p.schema)}`).join(', ')
|
|
200
|
-
* return `z.object({ ${props} })`
|
|
201
|
-
* },
|
|
202
|
-
* },
|
|
203
|
-
* }))
|
|
204
|
-
* ```
|
|
205
|
-
*
|
|
206
|
-
* @example With a root-level `print` override to wrap output in a full declaration
|
|
207
|
-
* ```ts
|
|
208
|
-
* type TsPrinter = PrinterFactoryOptions<'ts', { typeName?: string }, ts.TypeNode, ts.Node>
|
|
209
|
-
*
|
|
210
|
-
* export const printerTs = definePrinter<TsPrinter>((options) => ({
|
|
211
|
-
* name: 'ts',
|
|
212
|
-
* options,
|
|
213
|
-
* nodes: { string: () => factory.keywordTypeNodes.string },
|
|
214
|
-
* print(node) {
|
|
215
|
-
* const type = this.print(node) // calls the node-level dispatcher
|
|
216
|
-
* if (!type || !this.options.typeName) return type
|
|
217
|
-
* return factory.createTypeAliasDeclaration(this.options.typeName, type)
|
|
218
|
-
* },
|
|
219
|
-
* }))
|
|
220
|
-
* ```
|
|
221
|
-
*/
|
|
222
|
-
function definePrinter(build) {
|
|
223
|
-
return (options) => {
|
|
224
|
-
const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? {});
|
|
225
|
-
const context = {
|
|
226
|
-
options: resolvedOptions,
|
|
227
|
-
print: (node) => {
|
|
228
|
-
const handler = nodes[node.type];
|
|
229
|
-
if (!handler) return void 0;
|
|
230
|
-
return handler.call(context, node);
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
return {
|
|
234
|
-
name,
|
|
235
|
-
options: resolvedOptions,
|
|
236
|
-
print: printOverride ? printOverride.bind(context) : context.print
|
|
237
|
-
};
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
//#endregion
|
|
241
|
-
//#region src/refs.ts
|
|
242
|
-
/**
|
|
243
|
-
* Indexes named schemas from `root.schemas` by name. Unnamed schemas are skipped.
|
|
244
|
-
*/
|
|
245
|
-
function buildRefMap(root) {
|
|
246
|
-
const map = /* @__PURE__ */ new Map();
|
|
247
|
-
for (const schema of root.schemas) if (schema.name) map.set(schema.name, schema);
|
|
248
|
-
return map;
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Looks up a schema by name. Prefer over `RefMap.get()` to keep the resolution strategy swappable.
|
|
252
|
-
*/
|
|
253
|
-
function resolveRef(refMap, ref) {
|
|
254
|
-
return refMap.get(ref);
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Converts a `RefMap` to a plain object.
|
|
258
|
-
*/
|
|
259
|
-
function refMapToObject(refMap) {
|
|
260
|
-
return Object.fromEntries(refMap);
|
|
261
|
-
}
|
|
262
|
-
//#endregion
|
|
263
|
-
//#region ../../internals/utils/dist/index.js
|
|
193
|
+
//#region ../../internals/utils/src/casing.ts
|
|
264
194
|
/**
|
|
265
195
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
266
196
|
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
@@ -279,10 +209,19 @@ function toCamelOrPascal(text, pascal) {
|
|
|
279
209
|
* Splits `text` on `.` and applies `transformPart` to each segment.
|
|
280
210
|
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
281
211
|
* Segments are joined with `/` to form a file path.
|
|
212
|
+
*
|
|
213
|
+
* Only splits on dots followed by a letter so that version numbers
|
|
214
|
+
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
215
|
+
*
|
|
216
|
+
* Empty segments are filtered before joining. They arise when the text starts with
|
|
217
|
+
* a dot followed immediately by a letter (e.g. `..Schema` splits into `['..', 'Schema']`
|
|
218
|
+
* and `'..'` transforms to an empty string). Without this filter the join would produce
|
|
219
|
+
* a leading `/`, which `path.resolve` would interpret as an absolute path, allowing
|
|
220
|
+
* generated files to escape the configured output directory.
|
|
282
221
|
*/
|
|
283
222
|
function applyToFileParts(text, transformPart) {
|
|
284
|
-
const parts = text.split(
|
|
285
|
-
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
|
|
223
|
+
const parts = text.split(/\.(?=[a-zA-Z])/);
|
|
224
|
+
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).filter(Boolean).join("/");
|
|
286
225
|
}
|
|
287
226
|
/**
|
|
288
227
|
* Converts `text` to camelCase.
|
|
@@ -299,214 +238,32 @@ function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
|
299
238
|
} : {}));
|
|
300
239
|
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
301
240
|
}
|
|
302
|
-
/** Returns a `CLIAdapter` with type inference. Pass a different adapter to `createCLI` to swap the CLI engine. */
|
|
303
|
-
function defineCLIAdapter(adapter) {
|
|
304
|
-
return adapter;
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Serializes `CommandDefinition[]` to a plain, JSON-serializable structure.
|
|
308
|
-
* Use to expose CLI capabilities to AI agents or MCP tools.
|
|
309
|
-
*/
|
|
310
|
-
function getCommandSchema(defs) {
|
|
311
|
-
return defs.map(serializeCommand);
|
|
312
|
-
}
|
|
313
|
-
function serializeCommand(def) {
|
|
314
|
-
return {
|
|
315
|
-
name: def.name,
|
|
316
|
-
description: def.description,
|
|
317
|
-
arguments: def.arguments,
|
|
318
|
-
options: serializeOptions(def.options ?? {}),
|
|
319
|
-
subCommands: def.subCommands ? def.subCommands.map(serializeCommand) : []
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
function serializeOptions(options) {
|
|
323
|
-
return Object.entries(options).map(([name, opt]) => {
|
|
324
|
-
return {
|
|
325
|
-
name,
|
|
326
|
-
flags: `${opt.short ? `-${opt.short}, ` : ""}--${name}${opt.type === "string" ? ` <${opt.hint ?? name}>` : ""}`,
|
|
327
|
-
type: opt.type,
|
|
328
|
-
description: opt.description,
|
|
329
|
-
...opt.default !== void 0 ? { default: opt.default } : {},
|
|
330
|
-
...opt.hint ? { hint: opt.hint } : {},
|
|
331
|
-
...opt.enum ? { enum: opt.enum } : {},
|
|
332
|
-
...opt.required ? { required: opt.required } : {}
|
|
333
|
-
};
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
/** Prints formatted help output for a command using its `CommandDefinition`. */
|
|
337
|
-
function renderHelp(def, parentName) {
|
|
338
|
-
const schema = getCommandSchema([def])[0];
|
|
339
|
-
const programName = parentName ? `${parentName} ${schema.name}` : schema.name;
|
|
340
|
-
const argsPart = schema.arguments?.length ? ` ${schema.arguments.join(" ")}` : "";
|
|
341
|
-
const subCmdPart = schema.subCommands.length ? " <command>" : "";
|
|
342
|
-
console.log(`\n${styleText("bold", "Usage:")} ${programName}${argsPart}${subCmdPart} [options]\n`);
|
|
343
|
-
if (schema.description) console.log(` ${schema.description}\n`);
|
|
344
|
-
if (schema.subCommands.length) {
|
|
345
|
-
console.log(styleText("bold", "Commands:"));
|
|
346
|
-
for (const sub of schema.subCommands) console.log(` ${styleText("cyan", sub.name.padEnd(16))}${sub.description}`);
|
|
347
|
-
console.log();
|
|
348
|
-
}
|
|
349
|
-
const options = [...schema.options, {
|
|
350
|
-
name: "help",
|
|
351
|
-
flags: "-h, --help",
|
|
352
|
-
type: "boolean",
|
|
353
|
-
description: "Show help"
|
|
354
|
-
}];
|
|
355
|
-
console.log(styleText("bold", "Options:"));
|
|
356
|
-
for (const opt of options) {
|
|
357
|
-
const flags = styleText("cyan", opt.flags.padEnd(30));
|
|
358
|
-
const defaultPart = opt.default !== void 0 ? styleText("dim", ` (default: ${opt.default})`) : "";
|
|
359
|
-
console.log(` ${flags}${opt.description}${defaultPart}`);
|
|
360
|
-
}
|
|
361
|
-
console.log();
|
|
362
|
-
}
|
|
363
|
-
function buildParseOptions(def) {
|
|
364
|
-
const result = { help: {
|
|
365
|
-
type: "boolean",
|
|
366
|
-
short: "h"
|
|
367
|
-
} };
|
|
368
|
-
for (const [name, opt] of Object.entries(def.options ?? {})) result[name] = {
|
|
369
|
-
type: opt.type,
|
|
370
|
-
...opt.short ? { short: opt.short } : {},
|
|
371
|
-
...opt.default !== void 0 ? { default: opt.default } : {}
|
|
372
|
-
};
|
|
373
|
-
return result;
|
|
374
|
-
}
|
|
375
|
-
async function runCommand(def, argv, parentName) {
|
|
376
|
-
const parseOptions = buildParseOptions(def);
|
|
377
|
-
let parsed;
|
|
378
|
-
try {
|
|
379
|
-
const result = parseArgs({
|
|
380
|
-
args: argv,
|
|
381
|
-
options: parseOptions,
|
|
382
|
-
allowPositionals: true,
|
|
383
|
-
strict: false
|
|
384
|
-
});
|
|
385
|
-
parsed = {
|
|
386
|
-
values: result.values,
|
|
387
|
-
positionals: result.positionals
|
|
388
|
-
};
|
|
389
|
-
} catch {
|
|
390
|
-
renderHelp(def, parentName);
|
|
391
|
-
process.exit(1);
|
|
392
|
-
}
|
|
393
|
-
if (parsed.values["help"]) {
|
|
394
|
-
renderHelp(def, parentName);
|
|
395
|
-
process.exit(0);
|
|
396
|
-
}
|
|
397
|
-
for (const [name, opt] of Object.entries(def.options ?? {})) if (opt.required && parsed.values[name] === void 0) {
|
|
398
|
-
console.error(styleText("red", `Error: --${name} is required`));
|
|
399
|
-
renderHelp(def, parentName);
|
|
400
|
-
process.exit(1);
|
|
401
|
-
}
|
|
402
|
-
if (!def.run) {
|
|
403
|
-
renderHelp(def, parentName);
|
|
404
|
-
process.exit(0);
|
|
405
|
-
}
|
|
406
|
-
try {
|
|
407
|
-
await def.run(parsed);
|
|
408
|
-
} catch (err) {
|
|
409
|
-
console.error(styleText("red", `Error: ${err instanceof Error ? err.message : String(err)}`));
|
|
410
|
-
renderHelp(def, parentName);
|
|
411
|
-
process.exit(1);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
function printRootHelp(programName, version, defs) {
|
|
415
|
-
console.log(`\n${styleText("bold", "Usage:")} ${programName} <command> [options]\n`);
|
|
416
|
-
console.log(` Kubb generation — v${version}\n`);
|
|
417
|
-
console.log(styleText("bold", "Commands:"));
|
|
418
|
-
for (const def of defs) console.log(` ${styleText("cyan", def.name.padEnd(16))}${def.description}`);
|
|
419
|
-
console.log();
|
|
420
|
-
console.log(styleText("bold", "Options:"));
|
|
421
|
-
console.log(` ${styleText("cyan", "-v, --version".padEnd(30))}Show version number`);
|
|
422
|
-
console.log(` ${styleText("cyan", "-h, --help".padEnd(30))}Show help`);
|
|
423
|
-
console.log();
|
|
424
|
-
console.log(`Run ${styleText("cyan", `${programName} <command> --help`)} for command-specific help.\n`);
|
|
425
|
-
}
|
|
426
|
-
defineCLIAdapter({
|
|
427
|
-
renderHelp(def, parentName) {
|
|
428
|
-
renderHelp(def, parentName);
|
|
429
|
-
},
|
|
430
|
-
async run(defs, argv, opts) {
|
|
431
|
-
const { programName, defaultCommandName, version } = opts;
|
|
432
|
-
const args = argv.length >= 2 && argv[0]?.includes("node") ? argv.slice(2) : argv;
|
|
433
|
-
if (args[0] === "--version" || args[0] === "-v") {
|
|
434
|
-
console.log(version);
|
|
435
|
-
process.exit(0);
|
|
436
|
-
}
|
|
437
|
-
if (args[0] === "--help" || args[0] === "-h") {
|
|
438
|
-
printRootHelp(programName, version, defs);
|
|
439
|
-
process.exit(0);
|
|
440
|
-
}
|
|
441
|
-
if (args.length === 0) {
|
|
442
|
-
const defaultDef = defs.find((d) => d.name === defaultCommandName);
|
|
443
|
-
if (defaultDef?.run) await runCommand(defaultDef, [], programName);
|
|
444
|
-
else printRootHelp(programName, version, defs);
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
const [first, ...rest] = args;
|
|
448
|
-
const isKnownSubcommand = defs.some((d) => d.name === first);
|
|
449
|
-
let def;
|
|
450
|
-
let commandArgv;
|
|
451
|
-
let parentName;
|
|
452
|
-
if (isKnownSubcommand) {
|
|
453
|
-
def = defs.find((d) => d.name === first);
|
|
454
|
-
commandArgv = rest;
|
|
455
|
-
parentName = programName;
|
|
456
|
-
} else {
|
|
457
|
-
def = defs.find((d) => d.name === defaultCommandName);
|
|
458
|
-
commandArgv = args;
|
|
459
|
-
parentName = programName;
|
|
460
|
-
}
|
|
461
|
-
if (!def) {
|
|
462
|
-
console.error(`Unknown command: ${first}`);
|
|
463
|
-
printRootHelp(programName, version, defs);
|
|
464
|
-
process.exit(1);
|
|
465
|
-
}
|
|
466
|
-
if (def.subCommands?.length) {
|
|
467
|
-
const [subName, ...subRest] = commandArgv;
|
|
468
|
-
const subDef = def.subCommands.find((s) => s.name === subName);
|
|
469
|
-
if (subName === "--help" || subName === "-h") {
|
|
470
|
-
renderHelp(def, parentName);
|
|
471
|
-
process.exit(0);
|
|
472
|
-
}
|
|
473
|
-
if (!subDef) {
|
|
474
|
-
renderHelp(def, parentName);
|
|
475
|
-
process.exit(subName ? 1 : 0);
|
|
476
|
-
}
|
|
477
|
-
await runCommand(subDef, subRest, `${parentName} ${def.name}`);
|
|
478
|
-
return;
|
|
479
|
-
}
|
|
480
|
-
await runCommand(def, commandArgv, parentName);
|
|
481
|
-
}
|
|
482
|
-
});
|
|
483
|
-
/**
|
|
484
|
-
* Parses a CSS hex color string (`#RGB`) into its RGB channels.
|
|
485
|
-
* Falls back to `255` for any channel that cannot be parsed.
|
|
486
|
-
*/
|
|
487
|
-
function parseHex(color) {
|
|
488
|
-
const int = Number.parseInt(color.replace("#", ""), 16);
|
|
489
|
-
return Number.isNaN(int) ? {
|
|
490
|
-
r: 255,
|
|
491
|
-
g: 255,
|
|
492
|
-
b: 255
|
|
493
|
-
} : {
|
|
494
|
-
r: int >> 16 & 255,
|
|
495
|
-
g: int >> 8 & 255,
|
|
496
|
-
b: int & 255
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
241
|
/**
|
|
500
|
-
*
|
|
501
|
-
*
|
|
242
|
+
* Converts `text` to PascalCase.
|
|
243
|
+
* When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* pascalCase('hello-world') // 'HelloWorld'
|
|
247
|
+
* pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
|
|
502
248
|
*/
|
|
503
|
-
function
|
|
504
|
-
|
|
505
|
-
|
|
249
|
+
function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
250
|
+
if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
|
|
251
|
+
prefix,
|
|
252
|
+
suffix
|
|
253
|
+
}) : camelCase(part));
|
|
254
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
|
|
506
255
|
}
|
|
507
|
-
|
|
256
|
+
//#endregion
|
|
257
|
+
//#region ../../internals/utils/src/reserved.ts
|
|
508
258
|
/**
|
|
509
259
|
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```ts
|
|
263
|
+
* isValidVarName('status') // true
|
|
264
|
+
* isValidVarName('class') // false (reserved word)
|
|
265
|
+
* isValidVarName('42foo') // false (starts with digit)
|
|
266
|
+
* ```
|
|
510
267
|
*/
|
|
511
268
|
function isValidVarName(name) {
|
|
512
269
|
try {
|
|
@@ -517,85 +274,161 @@ function isValidVarName(name) {
|
|
|
517
274
|
return true;
|
|
518
275
|
}
|
|
519
276
|
//#endregion
|
|
520
|
-
//#region src/
|
|
521
|
-
const plainStringTypes = new Set([
|
|
522
|
-
"string",
|
|
523
|
-
"uuid",
|
|
524
|
-
"email",
|
|
525
|
-
"url",
|
|
526
|
-
"datetime"
|
|
527
|
-
]);
|
|
277
|
+
//#region ../../internals/utils/src/string.ts
|
|
528
278
|
/**
|
|
529
|
-
*
|
|
279
|
+
* Strips the file extension from a path or file name.
|
|
280
|
+
* Only removes the last `.ext` segment when the dot is not part of a directory name.
|
|
530
281
|
*
|
|
531
|
-
*
|
|
532
|
-
*
|
|
282
|
+
* @example
|
|
283
|
+
* trimExtName('petStore.ts') // 'petStore'
|
|
284
|
+
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
|
|
285
|
+
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
|
|
286
|
+
* trimExtName('noExtension') // 'noExtension'
|
|
533
287
|
*/
|
|
534
|
-
function
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
return false;
|
|
288
|
+
function trimExtName(text) {
|
|
289
|
+
const dotIndex = text.lastIndexOf(".");
|
|
290
|
+
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
291
|
+
return text;
|
|
539
292
|
}
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/guards.ts
|
|
540
295
|
/**
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
* The original `params` array is never mutated — a new array of cloned nodes is returned.
|
|
544
|
-
* When no `casing` is provided the original array is returned as-is.
|
|
296
|
+
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
545
297
|
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* const schema = createSchema({ type: 'string' })
|
|
301
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | undefined
|
|
302
|
+
* ```
|
|
549
303
|
*/
|
|
550
|
-
function
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
...param,
|
|
556
|
-
name: transformed
|
|
557
|
-
};
|
|
558
|
-
});
|
|
304
|
+
function narrowSchema(node, type) {
|
|
305
|
+
return node?.type === type ? node : void 0;
|
|
306
|
+
}
|
|
307
|
+
function isKind(kind) {
|
|
308
|
+
return (node) => node.kind === kind;
|
|
559
309
|
}
|
|
560
|
-
//#endregion
|
|
561
|
-
//#region src/visitor.ts
|
|
562
310
|
/**
|
|
563
|
-
*
|
|
564
|
-
*
|
|
311
|
+
* Returns `true` when the input is an `InputNode`.
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```ts
|
|
315
|
+
* if (isInputNode(node)) {
|
|
316
|
+
* console.log(node.schemas.length)
|
|
317
|
+
* }
|
|
318
|
+
* ```
|
|
565
319
|
*/
|
|
566
|
-
|
|
567
|
-
let active = 0;
|
|
568
|
-
const queue = [];
|
|
569
|
-
function next() {
|
|
570
|
-
if (active < concurrency && queue.length > 0) {
|
|
571
|
-
active++;
|
|
572
|
-
queue.shift()();
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
return function limit(fn) {
|
|
576
|
-
return new Promise((resolve, reject) => {
|
|
577
|
-
queue.push(() => {
|
|
578
|
-
Promise.resolve(fn()).then(resolve, reject).finally(() => {
|
|
579
|
-
active--;
|
|
580
|
-
next();
|
|
581
|
-
});
|
|
582
|
-
});
|
|
583
|
-
next();
|
|
584
|
-
});
|
|
585
|
-
};
|
|
586
|
-
}
|
|
320
|
+
const isInputNode = isKind("Input");
|
|
587
321
|
/**
|
|
588
|
-
* Returns the
|
|
322
|
+
* Returns `true` when the input is an `OutputNode`.
|
|
589
323
|
*
|
|
590
|
-
*
|
|
591
|
-
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* if (isOutputNode(node)) {
|
|
327
|
+
* console.log(node.files.length)
|
|
328
|
+
* }
|
|
329
|
+
* ```
|
|
592
330
|
*/
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
331
|
+
const isOutputNode = isKind("Output");
|
|
332
|
+
/**
|
|
333
|
+
* Returns `true` when the input is an `OperationNode`.
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```ts
|
|
337
|
+
* if (isOperationNode(node)) {
|
|
338
|
+
* console.log(node.operationId)
|
|
339
|
+
* }
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
const isOperationNode = isKind("Operation");
|
|
343
|
+
/**
|
|
344
|
+
* Returns `true` when the input is a `SchemaNode`.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```ts
|
|
348
|
+
* if (isSchemaNode(node)) {
|
|
349
|
+
* console.log(node.type)
|
|
350
|
+
* }
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
353
|
+
const isSchemaNode = isKind("Schema");
|
|
354
|
+
isKind("Property");
|
|
355
|
+
isKind("Parameter");
|
|
356
|
+
isKind("Response");
|
|
357
|
+
isKind("FunctionParameter");
|
|
358
|
+
isKind("ParameterGroup");
|
|
359
|
+
isKind("FunctionParameters");
|
|
360
|
+
//#endregion
|
|
361
|
+
//#region src/refs.ts
|
|
362
|
+
/**
|
|
363
|
+
* Returns the last path segment of a reference string.
|
|
364
|
+
*
|
|
365
|
+
* Example: `#/components/schemas/Pet` becomes `Pet`.
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* extractRefName('#/components/schemas/Pet') // 'Pet'
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
function extractRefName(ref) {
|
|
373
|
+
return ref.split("/").at(-1) ?? ref;
|
|
374
|
+
}
|
|
375
|
+
//#endregion
|
|
376
|
+
//#region src/visitor.ts
|
|
377
|
+
/**
|
|
378
|
+
* Creates a small async concurrency limiter.
|
|
379
|
+
*
|
|
380
|
+
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
|
|
381
|
+
*
|
|
382
|
+
* @example
|
|
383
|
+
* ```ts
|
|
384
|
+
* const limit = createLimit(2)
|
|
385
|
+
* for (const task of [taskA, taskB, taskC]) {
|
|
386
|
+
* await limit(() => task())
|
|
387
|
+
* }
|
|
388
|
+
* // only 2 tasks run at the same time
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
function createLimit(concurrency) {
|
|
392
|
+
let active = 0;
|
|
393
|
+
const queue = [];
|
|
394
|
+
function next() {
|
|
395
|
+
if (active < concurrency && queue.length > 0) {
|
|
396
|
+
active++;
|
|
397
|
+
queue.shift()();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return function limit(fn) {
|
|
401
|
+
return new Promise((resolve, reject) => {
|
|
402
|
+
queue.push(() => {
|
|
403
|
+
Promise.resolve(fn()).then(resolve, reject).finally(() => {
|
|
404
|
+
active--;
|
|
405
|
+
next();
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
next();
|
|
409
|
+
});
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Returns the immediate traversable children of `node`.
|
|
414
|
+
*
|
|
415
|
+
* For `Schema` nodes, children (`properties`, `items`, `members`, and non-boolean
|
|
416
|
+
* `additionalProperties`) are only included
|
|
417
|
+
* when `recurse` is `true`; shallow mode skips them.
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```ts
|
|
421
|
+
* const children = getChildren(operationNode, true)
|
|
422
|
+
* // returns parameters, requestBody schema (if present), and responses
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
function getChildren(node, recurse) {
|
|
426
|
+
switch (node.kind) {
|
|
427
|
+
case "Input": return [...node.schemas, ...node.operations];
|
|
428
|
+
case "Output": return [];
|
|
596
429
|
case "Operation": return [
|
|
597
430
|
...node.parameters,
|
|
598
|
-
...node.requestBody ? [
|
|
431
|
+
...node.requestBody?.content?.flatMap((c) => c.schema ? [c.schema] : []) ?? [],
|
|
599
432
|
...node.responses
|
|
600
433
|
];
|
|
601
434
|
case "Schema": {
|
|
@@ -604,143 +437,1648 @@ function getChildren(node, recurse) {
|
|
|
604
437
|
if ("properties" in node && node.properties.length > 0) children.push(...node.properties);
|
|
605
438
|
if ("items" in node && node.items) children.push(...node.items);
|
|
606
439
|
if ("members" in node && node.members) children.push(...node.members);
|
|
440
|
+
if ("additionalProperties" in node && node.additionalProperties && node.additionalProperties !== true) children.push(node.additionalProperties);
|
|
607
441
|
return children;
|
|
608
442
|
}
|
|
609
443
|
case "Property": return [node.schema];
|
|
610
444
|
case "Parameter": return [node.schema];
|
|
611
445
|
case "Response": return node.schema ? [node.schema] : [];
|
|
446
|
+
case "FunctionParameter":
|
|
447
|
+
case "ParameterGroup":
|
|
448
|
+
case "FunctionParameters":
|
|
449
|
+
case "Type": return [];
|
|
450
|
+
default: return [];
|
|
612
451
|
}
|
|
613
452
|
}
|
|
614
453
|
/**
|
|
615
454
|
* Depth-first traversal for side effects. Visitor return values are ignored.
|
|
616
|
-
* Sibling nodes at each level are visited concurrently up to `options.concurrency`
|
|
455
|
+
* Sibling nodes at each level are visited concurrently up to `options.concurrency`
|
|
456
|
+
* (default: `WALK_CONCURRENCY`).
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* ```ts
|
|
460
|
+
* await walk(root, {
|
|
461
|
+
* operation(node) {
|
|
462
|
+
* console.log(node.operationId)
|
|
463
|
+
* },
|
|
464
|
+
* })
|
|
465
|
+
* ```
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* ```ts
|
|
469
|
+
* // Visit only the current node
|
|
470
|
+
* await walk(root, { depth: 'shallow', root: () => {} })
|
|
471
|
+
* ```
|
|
617
472
|
*/
|
|
618
|
-
async function walk(node,
|
|
619
|
-
return _walk(node,
|
|
473
|
+
async function walk(node, options) {
|
|
474
|
+
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
|
|
620
475
|
}
|
|
621
|
-
|
|
622
|
-
* Internal recursive walk implementation — calls visitor then recurses into children.
|
|
623
|
-
*/
|
|
624
|
-
async function _walk(node, visitor, recurse, limit) {
|
|
476
|
+
async function _walk(node, visitor, recurse, limit, parent) {
|
|
625
477
|
switch (node.kind) {
|
|
626
|
-
case "
|
|
627
|
-
await limit(() => visitor.
|
|
478
|
+
case "Input":
|
|
479
|
+
await limit(() => visitor.input?.(node, { parent }));
|
|
480
|
+
break;
|
|
481
|
+
case "Output":
|
|
482
|
+
await limit(() => visitor.output?.(node, { parent }));
|
|
628
483
|
break;
|
|
629
484
|
case "Operation":
|
|
630
|
-
await limit(() => visitor.operation?.(node));
|
|
485
|
+
await limit(() => visitor.operation?.(node, { parent }));
|
|
631
486
|
break;
|
|
632
487
|
case "Schema":
|
|
633
|
-
await limit(() => visitor.schema?.(node));
|
|
488
|
+
await limit(() => visitor.schema?.(node, { parent }));
|
|
634
489
|
break;
|
|
635
490
|
case "Property":
|
|
636
|
-
await limit(() => visitor.property?.(node));
|
|
491
|
+
await limit(() => visitor.property?.(node, { parent }));
|
|
637
492
|
break;
|
|
638
493
|
case "Parameter":
|
|
639
|
-
await limit(() => visitor.parameter?.(node));
|
|
494
|
+
await limit(() => visitor.parameter?.(node, { parent }));
|
|
640
495
|
break;
|
|
641
496
|
case "Response":
|
|
642
|
-
await limit(() => visitor.response?.(node));
|
|
497
|
+
await limit(() => visitor.response?.(node, { parent }));
|
|
643
498
|
break;
|
|
499
|
+
case "FunctionParameter":
|
|
500
|
+
case "ParameterGroup":
|
|
501
|
+
case "FunctionParameters": break;
|
|
644
502
|
}
|
|
645
503
|
const children = getChildren(node, recurse);
|
|
646
|
-
|
|
504
|
+
for (const child of children) await _walk(child, visitor, recurse, limit, node);
|
|
647
505
|
}
|
|
648
|
-
function transform(node,
|
|
649
|
-
const
|
|
506
|
+
function transform(node, options) {
|
|
507
|
+
const { depth, parent, ...visitor } = options;
|
|
508
|
+
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
650
509
|
switch (node.kind) {
|
|
651
|
-
case "
|
|
652
|
-
let
|
|
653
|
-
const replaced = visitor.
|
|
654
|
-
if (replaced)
|
|
510
|
+
case "Input": {
|
|
511
|
+
let input = node;
|
|
512
|
+
const replaced = visitor.input?.(input, { parent });
|
|
513
|
+
if (replaced) input = replaced;
|
|
655
514
|
return {
|
|
656
|
-
...
|
|
657
|
-
schemas:
|
|
658
|
-
|
|
515
|
+
...input,
|
|
516
|
+
schemas: input.schemas.map((s) => transform(s, {
|
|
517
|
+
...options,
|
|
518
|
+
parent: input
|
|
519
|
+
})),
|
|
520
|
+
operations: input.operations.map((op) => transform(op, {
|
|
521
|
+
...options,
|
|
522
|
+
parent: input
|
|
523
|
+
}))
|
|
659
524
|
};
|
|
660
525
|
}
|
|
526
|
+
case "Output": {
|
|
527
|
+
let output = node;
|
|
528
|
+
const replaced = visitor.output?.(output, { parent });
|
|
529
|
+
if (replaced) output = replaced;
|
|
530
|
+
return output;
|
|
531
|
+
}
|
|
661
532
|
case "Operation": {
|
|
662
533
|
let op = node;
|
|
663
|
-
const replaced = visitor.operation?.(op);
|
|
534
|
+
const replaced = visitor.operation?.(op, { parent });
|
|
664
535
|
if (replaced) op = replaced;
|
|
665
536
|
return {
|
|
666
537
|
...op,
|
|
667
|
-
parameters: op.parameters.map((p) => transform(p,
|
|
668
|
-
|
|
669
|
-
|
|
538
|
+
parameters: op.parameters.map((p) => transform(p, {
|
|
539
|
+
...options,
|
|
540
|
+
parent: op
|
|
541
|
+
})),
|
|
542
|
+
requestBody: op.requestBody ? {
|
|
543
|
+
...op.requestBody,
|
|
544
|
+
content: op.requestBody.content?.map((c) => ({
|
|
545
|
+
...c,
|
|
546
|
+
schema: c.schema ? transform(c.schema, {
|
|
547
|
+
...options,
|
|
548
|
+
parent: op
|
|
549
|
+
}) : void 0
|
|
550
|
+
}))
|
|
551
|
+
} : void 0,
|
|
552
|
+
responses: op.responses.map((r) => transform(r, {
|
|
553
|
+
...options,
|
|
554
|
+
parent: op
|
|
555
|
+
}))
|
|
670
556
|
};
|
|
671
557
|
}
|
|
672
558
|
case "Schema": {
|
|
673
559
|
let schema = node;
|
|
674
|
-
const replaced = visitor.schema?.(schema);
|
|
560
|
+
const replaced = visitor.schema?.(schema, { parent });
|
|
675
561
|
if (replaced) schema = replaced;
|
|
562
|
+
const childOptions = {
|
|
563
|
+
...options,
|
|
564
|
+
parent: schema
|
|
565
|
+
};
|
|
676
566
|
return {
|
|
677
567
|
...schema,
|
|
678
|
-
..."properties" in schema && recurse ? { properties: schema.properties.map((p) => transform(p,
|
|
679
|
-
..."items" in schema && recurse ? { items: schema.items?.map((i) => transform(i,
|
|
680
|
-
..."members" in schema && recurse ? { members: schema.members?.map((m) => transform(m,
|
|
568
|
+
..."properties" in schema && recurse ? { properties: schema.properties.map((p) => transform(p, childOptions)) } : {},
|
|
569
|
+
..."items" in schema && recurse ? { items: schema.items?.map((i) => transform(i, childOptions)) } : {},
|
|
570
|
+
..."members" in schema && recurse ? { members: schema.members?.map((m) => transform(m, childOptions)) } : {},
|
|
571
|
+
..."additionalProperties" in schema && recurse && schema.additionalProperties && schema.additionalProperties !== true ? { additionalProperties: transform(schema.additionalProperties, childOptions) } : {}
|
|
681
572
|
};
|
|
682
573
|
}
|
|
683
574
|
case "Property": {
|
|
684
575
|
let prop = node;
|
|
685
|
-
const replaced = visitor.property?.(prop);
|
|
576
|
+
const replaced = visitor.property?.(prop, { parent });
|
|
686
577
|
if (replaced) prop = replaced;
|
|
687
|
-
return {
|
|
578
|
+
return createProperty({
|
|
688
579
|
...prop,
|
|
689
|
-
schema: transform(prop.schema,
|
|
690
|
-
|
|
580
|
+
schema: transform(prop.schema, {
|
|
581
|
+
...options,
|
|
582
|
+
parent: prop
|
|
583
|
+
})
|
|
584
|
+
});
|
|
691
585
|
}
|
|
692
586
|
case "Parameter": {
|
|
693
587
|
let param = node;
|
|
694
|
-
const replaced = visitor.parameter?.(param);
|
|
588
|
+
const replaced = visitor.parameter?.(param, { parent });
|
|
695
589
|
if (replaced) param = replaced;
|
|
696
|
-
return {
|
|
590
|
+
return createParameter({
|
|
697
591
|
...param,
|
|
698
|
-
schema: transform(param.schema,
|
|
699
|
-
|
|
592
|
+
schema: transform(param.schema, {
|
|
593
|
+
...options,
|
|
594
|
+
parent: param
|
|
595
|
+
})
|
|
596
|
+
});
|
|
700
597
|
}
|
|
701
598
|
case "Response": {
|
|
702
599
|
let response = node;
|
|
703
|
-
const replaced = visitor.response?.(response);
|
|
600
|
+
const replaced = visitor.response?.(response, { parent });
|
|
704
601
|
if (replaced) response = replaced;
|
|
705
602
|
return {
|
|
706
603
|
...response,
|
|
707
|
-
schema:
|
|
604
|
+
schema: transform(response.schema, {
|
|
605
|
+
...options,
|
|
606
|
+
parent: response
|
|
607
|
+
})
|
|
708
608
|
};
|
|
709
609
|
}
|
|
610
|
+
case "FunctionParameter":
|
|
611
|
+
case "ParameterGroup":
|
|
612
|
+
case "FunctionParameters":
|
|
613
|
+
case "Type": return node;
|
|
614
|
+
default: return node;
|
|
710
615
|
}
|
|
711
616
|
}
|
|
712
617
|
/**
|
|
713
|
-
*
|
|
618
|
+
* Runs a depth-first synchronous collection pass.
|
|
619
|
+
*
|
|
620
|
+
* Non-`undefined` values returned by visitor callbacks are appended to the result.
|
|
621
|
+
*
|
|
622
|
+
* @example
|
|
623
|
+
* ```ts
|
|
624
|
+
* const ids = collect(root, {
|
|
625
|
+
* operation(node) {
|
|
626
|
+
* return node.operationId
|
|
627
|
+
* },
|
|
628
|
+
* })
|
|
629
|
+
* ```
|
|
630
|
+
*
|
|
631
|
+
* @example
|
|
632
|
+
* ```ts
|
|
633
|
+
* // Collect from only the current node
|
|
634
|
+
* const values = collect(root, { depth: 'shallow', root: () => 'root' })
|
|
635
|
+
* ```
|
|
714
636
|
*/
|
|
715
|
-
function collect(node,
|
|
716
|
-
const
|
|
637
|
+
function collect(node, options) {
|
|
638
|
+
const { depth, parent, ...visitor } = options;
|
|
639
|
+
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
717
640
|
const results = [];
|
|
718
641
|
let v;
|
|
719
642
|
switch (node.kind) {
|
|
720
|
-
case "
|
|
721
|
-
v = visitor.
|
|
643
|
+
case "Input":
|
|
644
|
+
v = visitor.input?.(node, { parent });
|
|
645
|
+
break;
|
|
646
|
+
case "Output":
|
|
647
|
+
v = visitor.output?.(node, { parent });
|
|
722
648
|
break;
|
|
723
649
|
case "Operation":
|
|
724
|
-
v = visitor.operation?.(node);
|
|
650
|
+
v = visitor.operation?.(node, { parent });
|
|
725
651
|
break;
|
|
726
652
|
case "Schema":
|
|
727
|
-
v = visitor.schema?.(node);
|
|
653
|
+
v = visitor.schema?.(node, { parent });
|
|
728
654
|
break;
|
|
729
655
|
case "Property":
|
|
730
|
-
v = visitor.property?.(node);
|
|
656
|
+
v = visitor.property?.(node, { parent });
|
|
731
657
|
break;
|
|
732
658
|
case "Parameter":
|
|
733
|
-
v = visitor.parameter?.(node);
|
|
659
|
+
v = visitor.parameter?.(node, { parent });
|
|
734
660
|
break;
|
|
735
661
|
case "Response":
|
|
736
|
-
v = visitor.response?.(node);
|
|
662
|
+
v = visitor.response?.(node, { parent });
|
|
737
663
|
break;
|
|
664
|
+
case "FunctionParameter":
|
|
665
|
+
case "ParameterGroup":
|
|
666
|
+
case "FunctionParameters": break;
|
|
738
667
|
}
|
|
739
668
|
if (v !== void 0) results.push(v);
|
|
740
|
-
for (const child of getChildren(node, recurse)) for (const item of collect(child,
|
|
669
|
+
for (const child of getChildren(node, recurse)) for (const item of collect(child, {
|
|
670
|
+
...options,
|
|
671
|
+
parent: node
|
|
672
|
+
})) results.push(item);
|
|
741
673
|
return results;
|
|
742
674
|
}
|
|
743
675
|
//#endregion
|
|
744
|
-
|
|
676
|
+
//#region src/utils.ts
|
|
677
|
+
const plainStringTypes = new Set([
|
|
678
|
+
"string",
|
|
679
|
+
"uuid",
|
|
680
|
+
"email",
|
|
681
|
+
"url",
|
|
682
|
+
"datetime"
|
|
683
|
+
]);
|
|
684
|
+
/**
|
|
685
|
+
* Returns a merged schema view for a ref node, combining the resolved `node.schema`
|
|
686
|
+
* (base from the referenced definition) with any usage-site sibling fields set directly
|
|
687
|
+
* on the ref node (description, readOnly, nullable, deprecated, etc.).
|
|
688
|
+
*
|
|
689
|
+
* Usage-site fields take precedence over the resolved schema's own fields when both are defined.
|
|
690
|
+
*
|
|
691
|
+
* For non-ref nodes the node itself is returned unchanged.
|
|
692
|
+
*/
|
|
693
|
+
function syncSchemaRef(node) {
|
|
694
|
+
const ref = narrowSchema(node, "ref");
|
|
695
|
+
if (!ref) return node;
|
|
696
|
+
if (!ref.schema) return node;
|
|
697
|
+
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
|
|
698
|
+
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
|
|
699
|
+
return createSchema({
|
|
700
|
+
...ref.schema,
|
|
701
|
+
...definedOverrides
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Returns `true` when a schema is emitted as a plain `string` type.
|
|
706
|
+
*
|
|
707
|
+
* - `string`, `uuid`, `email`, `url`, `datetime` are always plain strings.
|
|
708
|
+
* - `date` and `time` are plain strings when their `representation` is `'string'` rather than `'date'`.
|
|
709
|
+
*
|
|
710
|
+
* @example
|
|
711
|
+
* ```ts
|
|
712
|
+
* isStringType(createSchema({ type: 'uuid' })) // true
|
|
713
|
+
* isStringType(createSchema({ type: 'date', representation: 'date' })) // false
|
|
714
|
+
* ```
|
|
715
|
+
*/
|
|
716
|
+
function isStringType(node) {
|
|
717
|
+
if (plainStringTypes.has(node.type)) return true;
|
|
718
|
+
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
|
|
719
|
+
if (temporal) return temporal.representation !== "date";
|
|
720
|
+
return false;
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Applies casing rules to parameter names and returns a new parameter array.
|
|
724
|
+
*
|
|
725
|
+
* The input array is not mutated.
|
|
726
|
+
* If `casing` is not set, the original array is returned unchanged.
|
|
727
|
+
*
|
|
728
|
+
* Use this before passing parameters to schema builders so that property keys
|
|
729
|
+
* in generated output match the desired casing while preserving
|
|
730
|
+
* `OperationNode.parameters` for other consumers.
|
|
731
|
+
*
|
|
732
|
+
* @example
|
|
733
|
+
* ```ts
|
|
734
|
+
* const params = [createParameter({ name: 'pet_id', in: 'query', schema: createSchema({ type: 'string' }) })]
|
|
735
|
+
* const cased = caseParams(params, 'camelcase')
|
|
736
|
+
* // cased[0].name === 'petId'
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
739
|
+
function caseParams(params, casing) {
|
|
740
|
+
if (!casing) return params;
|
|
741
|
+
return params.map((param) => {
|
|
742
|
+
const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name;
|
|
743
|
+
return {
|
|
744
|
+
...param,
|
|
745
|
+
name: transformed
|
|
746
|
+
};
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Creates a single-property object schema used as a discriminator literal.
|
|
751
|
+
*
|
|
752
|
+
* @example
|
|
753
|
+
* ```ts
|
|
754
|
+
* createDiscriminantNode({ propertyName: 'type', value: 'dog' })
|
|
755
|
+
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
|
|
756
|
+
* ```
|
|
757
|
+
*/
|
|
758
|
+
function createDiscriminantNode({ propertyName, value }) {
|
|
759
|
+
return createSchema({
|
|
760
|
+
type: "object",
|
|
761
|
+
primitive: "object",
|
|
762
|
+
properties: [createProperty({
|
|
763
|
+
name: propertyName,
|
|
764
|
+
schema: createSchema({
|
|
765
|
+
type: "enum",
|
|
766
|
+
primitive: "string",
|
|
767
|
+
enumValues: [value]
|
|
768
|
+
}),
|
|
769
|
+
required: true
|
|
770
|
+
})]
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
function resolveParamsType({ node, param, resolver }) {
|
|
774
|
+
if (!resolver) return createParamsType({
|
|
775
|
+
variant: "reference",
|
|
776
|
+
name: param.schema.primitive ?? "unknown"
|
|
777
|
+
});
|
|
778
|
+
const individualName = resolver.resolveParamName(node, param);
|
|
779
|
+
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
|
|
780
|
+
const groupResolvers = {
|
|
781
|
+
path: resolver.resolvePathParamsName,
|
|
782
|
+
query: resolver.resolveQueryParamsName,
|
|
783
|
+
header: resolver.resolveHeaderParamsName
|
|
784
|
+
};
|
|
785
|
+
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
|
|
786
|
+
if (groupName && groupName !== individualName) return createParamsType({
|
|
787
|
+
variant: "member",
|
|
788
|
+
base: groupName,
|
|
789
|
+
key: param.name
|
|
790
|
+
});
|
|
791
|
+
return createParamsType({
|
|
792
|
+
variant: "reference",
|
|
793
|
+
name: individualName
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
/**
|
|
797
|
+
* Converts an {@link OperationNode} into a {@link FunctionParametersNode}.
|
|
798
|
+
*
|
|
799
|
+
* Centralizes the per-plugin `getParams()` pattern. Provide a `resolver` for
|
|
800
|
+
* type resolution and `extraParams` for plugin-specific trailing parameters.
|
|
801
|
+
*
|
|
802
|
+
* @example
|
|
803
|
+
* ```ts
|
|
804
|
+
* const params = createOperationParams(node, {
|
|
805
|
+
* paramsType: 'inline',
|
|
806
|
+
* pathParamsType: 'inline',
|
|
807
|
+
* resolver: tsResolver,
|
|
808
|
+
* extraParams: [createFunctionParameter({ name: 'options', type: createParamsType({ variant: 'reference', name: 'Partial<RequestOptions>' }), default: '{}' })],
|
|
809
|
+
* })
|
|
810
|
+
* ```
|
|
811
|
+
*/
|
|
812
|
+
function createOperationParams(node, options) {
|
|
813
|
+
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options;
|
|
814
|
+
const dataName = paramNames?.data ?? "data";
|
|
815
|
+
const paramsName = paramNames?.params ?? "params";
|
|
816
|
+
const headersName = paramNames?.headers ?? "headers";
|
|
817
|
+
const pathName = paramNames?.path ?? "pathParams";
|
|
818
|
+
const wrapType = (type) => createParamsType({
|
|
819
|
+
variant: "reference",
|
|
820
|
+
name: typeWrapper ? typeWrapper(type) : type
|
|
821
|
+
});
|
|
822
|
+
const wrapTypeNode = (type) => type.kind === "ParamsType" && type.variant === "reference" ? wrapType(type.name) : type;
|
|
823
|
+
const casedParams = caseParams(node.parameters, paramsCasing);
|
|
824
|
+
const pathParams = casedParams.filter((p) => p.in === "path");
|
|
825
|
+
const queryParams = casedParams.filter((p) => p.in === "query");
|
|
826
|
+
const headerParams = casedParams.filter((p) => p.in === "header");
|
|
827
|
+
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
|
|
828
|
+
const bodyRequired = node.requestBody?.required ?? false;
|
|
829
|
+
const queryGroupType = resolver ? resolveGroupType({
|
|
830
|
+
node,
|
|
831
|
+
params: queryParams,
|
|
832
|
+
groupMethod: resolver.resolveQueryParamsName,
|
|
833
|
+
resolver
|
|
834
|
+
}) : void 0;
|
|
835
|
+
const headerGroupType = resolver ? resolveGroupType({
|
|
836
|
+
node,
|
|
837
|
+
params: headerParams,
|
|
838
|
+
groupMethod: resolver.resolveHeaderParamsName,
|
|
839
|
+
resolver
|
|
840
|
+
}) : void 0;
|
|
841
|
+
const params = [];
|
|
842
|
+
if (paramsType === "object") {
|
|
843
|
+
const children = [
|
|
844
|
+
...pathParams.map((p) => {
|
|
845
|
+
const type = resolveParamsType({
|
|
846
|
+
node,
|
|
847
|
+
param: p,
|
|
848
|
+
resolver
|
|
849
|
+
});
|
|
850
|
+
return createFunctionParameter({
|
|
851
|
+
name: p.name,
|
|
852
|
+
type: wrapTypeNode(type),
|
|
853
|
+
optional: !p.required
|
|
854
|
+
});
|
|
855
|
+
}),
|
|
856
|
+
...bodyType ? [createFunctionParameter({
|
|
857
|
+
name: dataName,
|
|
858
|
+
type: bodyType,
|
|
859
|
+
optional: !bodyRequired
|
|
860
|
+
})] : [],
|
|
861
|
+
...buildGroupParam({
|
|
862
|
+
name: paramsName,
|
|
863
|
+
node,
|
|
864
|
+
params: queryParams,
|
|
865
|
+
groupType: queryGroupType,
|
|
866
|
+
resolver,
|
|
867
|
+
wrapType
|
|
868
|
+
}),
|
|
869
|
+
...buildGroupParam({
|
|
870
|
+
name: headersName,
|
|
871
|
+
node,
|
|
872
|
+
params: headerParams,
|
|
873
|
+
groupType: headerGroupType,
|
|
874
|
+
resolver,
|
|
875
|
+
wrapType
|
|
876
|
+
})
|
|
877
|
+
];
|
|
878
|
+
if (children.length) params.push(createParameterGroup({
|
|
879
|
+
properties: children,
|
|
880
|
+
default: children.every((c) => c.optional) ? "{}" : void 0
|
|
881
|
+
}));
|
|
882
|
+
} else {
|
|
883
|
+
if (pathParams.length) if (pathParamsType === "inlineSpread") {
|
|
884
|
+
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]) ?? void 0;
|
|
885
|
+
params.push(createFunctionParameter({
|
|
886
|
+
name: pathName,
|
|
887
|
+
type: spreadType ? wrapType(spreadType) : void 0,
|
|
888
|
+
rest: true
|
|
889
|
+
}));
|
|
890
|
+
} else {
|
|
891
|
+
const pathChildren = pathParams.map((p) => {
|
|
892
|
+
const type = resolveParamsType({
|
|
893
|
+
node,
|
|
894
|
+
param: p,
|
|
895
|
+
resolver
|
|
896
|
+
});
|
|
897
|
+
return createFunctionParameter({
|
|
898
|
+
name: p.name,
|
|
899
|
+
type: wrapTypeNode(type),
|
|
900
|
+
optional: !p.required
|
|
901
|
+
});
|
|
902
|
+
});
|
|
903
|
+
params.push(createParameterGroup({
|
|
904
|
+
properties: pathChildren,
|
|
905
|
+
inline: pathParamsType === "inline",
|
|
906
|
+
default: pathParamsDefault ?? (pathChildren.every((c) => c.optional) ? "{}" : void 0)
|
|
907
|
+
}));
|
|
908
|
+
}
|
|
909
|
+
if (bodyType) params.push(createFunctionParameter({
|
|
910
|
+
name: dataName,
|
|
911
|
+
type: bodyType,
|
|
912
|
+
optional: !bodyRequired
|
|
913
|
+
}));
|
|
914
|
+
params.push(...buildGroupParam({
|
|
915
|
+
name: paramsName,
|
|
916
|
+
node,
|
|
917
|
+
params: queryParams,
|
|
918
|
+
groupType: queryGroupType,
|
|
919
|
+
resolver,
|
|
920
|
+
wrapType
|
|
921
|
+
}));
|
|
922
|
+
params.push(...buildGroupParam({
|
|
923
|
+
name: headersName,
|
|
924
|
+
node,
|
|
925
|
+
params: headerParams,
|
|
926
|
+
groupType: headerGroupType,
|
|
927
|
+
resolver,
|
|
928
|
+
wrapType
|
|
929
|
+
}));
|
|
930
|
+
}
|
|
931
|
+
params.push(...extraParams);
|
|
932
|
+
return createFunctionParameters({ params });
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* Builds a single {@link FunctionParameterNode} for a query or header group.
|
|
936
|
+
* Returns an empty array when there are no params to emit.
|
|
937
|
+
*
|
|
938
|
+
* If a pre-resolved `groupType` is provided it emits `name: GroupType`.
|
|
939
|
+
* Otherwise, it builds an inline struct from the individual params.
|
|
940
|
+
*/
|
|
941
|
+
function buildGroupParam({ name, node, params, groupType, resolver, wrapType }) {
|
|
942
|
+
if (groupType) return [createFunctionParameter({
|
|
943
|
+
name,
|
|
944
|
+
type: groupType.type.kind === "ParamsType" && groupType.type.variant === "reference" ? wrapType(groupType.type.name) : groupType.type,
|
|
945
|
+
optional: groupType.optional
|
|
946
|
+
})];
|
|
947
|
+
if (params.length) return [createFunctionParameter({
|
|
948
|
+
name,
|
|
949
|
+
type: toStructType({
|
|
950
|
+
node,
|
|
951
|
+
params,
|
|
952
|
+
resolver
|
|
953
|
+
}),
|
|
954
|
+
optional: params.every((p) => !p.required)
|
|
955
|
+
})];
|
|
956
|
+
return [];
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Derives a {@link ParamGroupType} from the resolver's group method.
|
|
960
|
+
* Returns `undefined` when the group name equals the individual param name (no real group).
|
|
961
|
+
*/
|
|
962
|
+
function resolveGroupType({ node, params, groupMethod, resolver }) {
|
|
963
|
+
if (!params.length) return;
|
|
964
|
+
const firstParam = params[0];
|
|
965
|
+
const groupName = groupMethod.call(resolver, node, firstParam);
|
|
966
|
+
if (groupName === resolver.resolveParamName(node, firstParam)) return;
|
|
967
|
+
const allOptional = params.every((p) => !p.required);
|
|
968
|
+
return {
|
|
969
|
+
type: createParamsType({
|
|
970
|
+
variant: "reference",
|
|
971
|
+
name: groupName
|
|
972
|
+
}),
|
|
973
|
+
optional: allOptional
|
|
974
|
+
};
|
|
975
|
+
}
|
|
976
|
+
/**
|
|
977
|
+
* Builds a {@link TypeNode} with `variant: 'struct'` for an inline anonymous type grouping named fields.
|
|
978
|
+
*
|
|
979
|
+
* Used when query or header parameters have no dedicated group type name.
|
|
980
|
+
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
|
|
981
|
+
*/
|
|
982
|
+
function toStructType({ node, params, resolver }) {
|
|
983
|
+
return createParamsType({
|
|
984
|
+
variant: "struct",
|
|
985
|
+
properties: params.map((p) => ({
|
|
986
|
+
name: p.name,
|
|
987
|
+
optional: !p.required,
|
|
988
|
+
type: resolveParamsType({
|
|
989
|
+
node,
|
|
990
|
+
param: p,
|
|
991
|
+
resolver
|
|
992
|
+
})
|
|
993
|
+
}))
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
function sourceKey(source) {
|
|
997
|
+
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
|
|
998
|
+
}
|
|
999
|
+
function pathTypeKey(path, isTypeOnly) {
|
|
1000
|
+
return `${path}:${isTypeOnly ?? false}`;
|
|
1001
|
+
}
|
|
1002
|
+
function exportKey(path, name, isTypeOnly, asAlias) {
|
|
1003
|
+
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
|
|
1004
|
+
}
|
|
1005
|
+
function importKey(path, name, isTypeOnly) {
|
|
1006
|
+
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* Computes a multi-level sort key for exports and imports:
|
|
1010
|
+
* non-array names first (wildcards/namespace aliases); type-only before value; alphabetical path; unnamed before named.
|
|
1011
|
+
*/
|
|
1012
|
+
function sortKey(node) {
|
|
1013
|
+
const isArray = Array.isArray(node.name) ? "1" : "0";
|
|
1014
|
+
const typeOnly = node.isTypeOnly ? "0" : "1";
|
|
1015
|
+
const hasName = node.name != null ? "1" : "0";
|
|
1016
|
+
const name = Array.isArray(node.name) ? [...node.name].sort().join("\0") : node.name ?? "";
|
|
1017
|
+
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* Deduplicates an array of `SourceNode` objects.
|
|
1021
|
+
* Named sources are deduplicated by `name + isExportable + isTypeOnly`.
|
|
1022
|
+
* Unnamed sources are deduplicated by object reference.
|
|
1023
|
+
*/
|
|
1024
|
+
function combineSources(sources) {
|
|
1025
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1026
|
+
for (const source of sources) {
|
|
1027
|
+
const key = sourceKey(source);
|
|
1028
|
+
if (!seen.has(key)) seen.set(key, source);
|
|
1029
|
+
}
|
|
1030
|
+
return [...seen.values()];
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* Deduplicates and merges an array of `ExportNode` objects.
|
|
1034
|
+
* Exports with the same path and `isTypeOnly` flag have their names merged.
|
|
1035
|
+
*/
|
|
1036
|
+
function combineExports(exports) {
|
|
1037
|
+
const result = [];
|
|
1038
|
+
const namedByPath = /* @__PURE__ */ new Map();
|
|
1039
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1040
|
+
const keyed = exports.map((node) => ({
|
|
1041
|
+
node,
|
|
1042
|
+
key: sortKey(node)
|
|
1043
|
+
}));
|
|
1044
|
+
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
1045
|
+
for (const { node: curr } of keyed) {
|
|
1046
|
+
const { name, path, isTypeOnly, asAlias } = curr;
|
|
1047
|
+
if (Array.isArray(name)) {
|
|
1048
|
+
if (!name.length) continue;
|
|
1049
|
+
const key = pathTypeKey(path, isTypeOnly);
|
|
1050
|
+
const existing = namedByPath.get(key);
|
|
1051
|
+
if (existing && Array.isArray(existing.name)) {
|
|
1052
|
+
const merged = new Set(existing.name);
|
|
1053
|
+
for (const n of name) merged.add(n);
|
|
1054
|
+
existing.name = [...merged];
|
|
1055
|
+
} else {
|
|
1056
|
+
const newItem = {
|
|
1057
|
+
...curr,
|
|
1058
|
+
name: [...new Set(name)]
|
|
1059
|
+
};
|
|
1060
|
+
result.push(newItem);
|
|
1061
|
+
namedByPath.set(key, newItem);
|
|
1062
|
+
}
|
|
1063
|
+
} else {
|
|
1064
|
+
const key = exportKey(path, name, isTypeOnly, asAlias);
|
|
1065
|
+
if (!seen.has(key)) {
|
|
1066
|
+
result.push(curr);
|
|
1067
|
+
seen.add(key);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
return result;
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Deduplicates and merges an array of `ImportNode` objects.
|
|
1075
|
+
* Filters out unused imports (names not referenced in `source` or re-exported).
|
|
1076
|
+
* Imports with the same path and `isTypeOnly` flag have their names merged.
|
|
1077
|
+
*/
|
|
1078
|
+
function combineImports(imports, exports, source) {
|
|
1079
|
+
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
1080
|
+
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
|
|
1081
|
+
const result = [];
|
|
1082
|
+
const namedByPath = /* @__PURE__ */ new Map();
|
|
1083
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1084
|
+
const keyed = imports.map((node) => ({
|
|
1085
|
+
node,
|
|
1086
|
+
key: sortKey(node)
|
|
1087
|
+
}));
|
|
1088
|
+
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
1089
|
+
for (const { node: curr } of keyed) {
|
|
1090
|
+
if (curr.path === curr.root) continue;
|
|
1091
|
+
const { path, isTypeOnly } = curr;
|
|
1092
|
+
let { name } = curr;
|
|
1093
|
+
if (Array.isArray(name)) {
|
|
1094
|
+
name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.propertyName));
|
|
1095
|
+
if (!name.length) continue;
|
|
1096
|
+
const key = pathTypeKey(path, isTypeOnly);
|
|
1097
|
+
const existing = namedByPath.get(key);
|
|
1098
|
+
if (existing && Array.isArray(existing.name)) {
|
|
1099
|
+
const merged = new Set(existing.name);
|
|
1100
|
+
for (const n of name) merged.add(n);
|
|
1101
|
+
existing.name = [...merged];
|
|
1102
|
+
} else {
|
|
1103
|
+
const newItem = {
|
|
1104
|
+
...curr,
|
|
1105
|
+
name
|
|
1106
|
+
};
|
|
1107
|
+
result.push(newItem);
|
|
1108
|
+
namedByPath.set(key, newItem);
|
|
1109
|
+
}
|
|
1110
|
+
} else {
|
|
1111
|
+
if (name && !isUsed(name)) continue;
|
|
1112
|
+
const key = importKey(path, name, isTypeOnly);
|
|
1113
|
+
if (!seen.has(key)) {
|
|
1114
|
+
result.push(curr);
|
|
1115
|
+
seen.add(key);
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
return result;
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* Recursively extracts all string content embedded in a {@link CodeNode} tree.
|
|
1123
|
+
*
|
|
1124
|
+
* Includes text node values, and string attribute fields (`params`, `generics`,
|
|
1125
|
+
* `returnType`, `type`) that may reference identifiers needing imports.
|
|
1126
|
+
* Used by `createFile` to build the full source string for import filtering.
|
|
1127
|
+
*/
|
|
1128
|
+
function extractStringsFromNodes(nodes) {
|
|
1129
|
+
if (!nodes?.length) return "";
|
|
1130
|
+
return nodes.map((node) => {
|
|
1131
|
+
if (typeof node === "string") return node;
|
|
1132
|
+
if (node.kind === "Text") return node.value;
|
|
1133
|
+
if (node.kind === "Break") return "";
|
|
1134
|
+
if (node.kind === "Jsx") return node.value;
|
|
1135
|
+
const parts = [];
|
|
1136
|
+
if ("params" in node && node.params) parts.push(node.params);
|
|
1137
|
+
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
1138
|
+
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
1139
|
+
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
1140
|
+
const nested = extractStringsFromNodes(node.nodes);
|
|
1141
|
+
if (nested) parts.push(nested);
|
|
1142
|
+
return parts.join("\n");
|
|
1143
|
+
}).filter(Boolean).join("\n");
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* Resolves the referenced schema name of a `ref` node, falling back through
|
|
1147
|
+
* `ref` → `name` → nested `schema.name`. Returns `undefined` for non-ref
|
|
1148
|
+
* nodes or when no name can be resolved.
|
|
1149
|
+
*
|
|
1150
|
+
* @example
|
|
1151
|
+
* ```ts
|
|
1152
|
+
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
|
|
1153
|
+
* // => 'Pet'
|
|
1154
|
+
* ```
|
|
1155
|
+
*/
|
|
1156
|
+
function resolveRefName(node) {
|
|
1157
|
+
if (!node || node.type !== "ref") return void 0;
|
|
1158
|
+
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? void 0;
|
|
1159
|
+
return node.name ?? node.schema?.name ?? void 0;
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Recursively collects every named schema referenced (transitively) from
|
|
1163
|
+
* `node` via `ref` edges. Refs are followed by name only — the resolved
|
|
1164
|
+
* `node.schema` of a ref is not traversed inline.
|
|
1165
|
+
*
|
|
1166
|
+
* @example
|
|
1167
|
+
* ```ts
|
|
1168
|
+
* const refs = collectReferencedSchemaNames(petSchema)
|
|
1169
|
+
* // => Set { 'Cat', 'Dog' }
|
|
1170
|
+
* ```
|
|
1171
|
+
*/
|
|
1172
|
+
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
1173
|
+
if (!node) return out;
|
|
1174
|
+
collect(node, { schema(child) {
|
|
1175
|
+
if (child.type === "ref") {
|
|
1176
|
+
const name = resolveRefName(child);
|
|
1177
|
+
if (name) out.add(name);
|
|
1178
|
+
}
|
|
1179
|
+
} });
|
|
1180
|
+
return out;
|
|
1181
|
+
}
|
|
1182
|
+
/**
|
|
1183
|
+
* Identifies every named schema that participates in a circular dependency
|
|
1184
|
+
* chain — including direct self-loops (e.g. `TreeNode → TreeNode`) and indirect
|
|
1185
|
+
* cycles spanning multiple schemas (e.g. `Pet → Cat → Pet`).
|
|
1186
|
+
*
|
|
1187
|
+
* The returned set contains schema names. Plugins that translate schemas into
|
|
1188
|
+
* a host language can use this to wrap recursive positions in a deferred
|
|
1189
|
+
* construct (lazy getter, `z.lazy(() => …)`, etc.) and avoid runtime stack
|
|
1190
|
+
* overflows when the generated code is executed.
|
|
1191
|
+
*
|
|
1192
|
+
* Refs are followed by name only — `node.schema` (the resolved referent) is
|
|
1193
|
+
* not traversed inline, which keeps the algorithm linear in the size of the
|
|
1194
|
+
* schema graph.
|
|
1195
|
+
*
|
|
1196
|
+
* @example
|
|
1197
|
+
* ```ts
|
|
1198
|
+
* const circular = findCircularSchemas(inputNode.schemas)
|
|
1199
|
+
* if (circular.has('Pet')) {
|
|
1200
|
+
* // emit lazy wrapper for any property whose schema references Pet
|
|
1201
|
+
* }
|
|
1202
|
+
* ```
|
|
1203
|
+
*/
|
|
1204
|
+
function findCircularSchemas(schemas) {
|
|
1205
|
+
const graph = /* @__PURE__ */ new Map();
|
|
1206
|
+
for (const schema of schemas) {
|
|
1207
|
+
if (!schema.name) continue;
|
|
1208
|
+
graph.set(schema.name, collectReferencedSchemaNames(schema));
|
|
1209
|
+
}
|
|
1210
|
+
const circular = /* @__PURE__ */ new Set();
|
|
1211
|
+
for (const start of graph.keys()) {
|
|
1212
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1213
|
+
const stack = [...graph.get(start) ?? []];
|
|
1214
|
+
while (stack.length > 0) {
|
|
1215
|
+
const node = stack.pop();
|
|
1216
|
+
if (node === start) {
|
|
1217
|
+
circular.add(start);
|
|
1218
|
+
break;
|
|
1219
|
+
}
|
|
1220
|
+
if (visited.has(node)) continue;
|
|
1221
|
+
visited.add(node);
|
|
1222
|
+
const next = graph.get(node);
|
|
1223
|
+
if (next) for (const r of next) stack.push(r);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
return circular;
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Returns true when `node` (or anything nested within it) carries a `ref`
|
|
1230
|
+
* whose resolved name belongs to `circularSchemas`.
|
|
1231
|
+
*
|
|
1232
|
+
* When `excludeName` is provided, refs to that name are ignored — useful
|
|
1233
|
+
* when self-references are already handled separately from cross-schema
|
|
1234
|
+
* cycles (e.g. the faker plugin emits `undefined as any` for direct
|
|
1235
|
+
* self-recursion but a lazy getter for indirect cycles).
|
|
1236
|
+
*
|
|
1237
|
+
* @example
|
|
1238
|
+
* ```ts
|
|
1239
|
+
* const circular = findCircularSchemas(schemas)
|
|
1240
|
+
* if (containsCircularRef(property.schema, { circularSchemas: circular, excludeName: 'Pet' })) {
|
|
1241
|
+
* // emit `get foo() { return fakeCat() }` instead of eager call
|
|
1242
|
+
* }
|
|
1243
|
+
* ```
|
|
1244
|
+
*/
|
|
1245
|
+
function containsCircularRef(node, { circularSchemas, excludeName }) {
|
|
1246
|
+
if (!node || circularSchemas.size === 0) return false;
|
|
1247
|
+
return collect(node, { schema(child) {
|
|
1248
|
+
if (child.type !== "ref") return void 0;
|
|
1249
|
+
const name = resolveRefName(child);
|
|
1250
|
+
return name && name !== excludeName && circularSchemas.has(name) ? true : void 0;
|
|
1251
|
+
} }).length > 0;
|
|
1252
|
+
}
|
|
1253
|
+
//#endregion
|
|
1254
|
+
//#region src/factory.ts
|
|
1255
|
+
/**
|
|
1256
|
+
* Syncs property/parameter schema optionality flags from `required` and `schema.nullable`.
|
|
1257
|
+
*
|
|
1258
|
+
* - `optional` is set for non-required, non-nullable schemas.
|
|
1259
|
+
* - `nullish` is set for non-required, nullable schemas.
|
|
1260
|
+
*/
|
|
1261
|
+
function syncOptionality(schema, required) {
|
|
1262
|
+
const nullable = schema.nullable ?? false;
|
|
1263
|
+
return {
|
|
1264
|
+
...schema,
|
|
1265
|
+
optional: !required && !nullable ? true : void 0,
|
|
1266
|
+
nullish: !required && nullable ? true : void 0
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
/**
|
|
1270
|
+
* Creates an `InputNode` with stable defaults for `schemas` and `operations`.
|
|
1271
|
+
*
|
|
1272
|
+
* @example
|
|
1273
|
+
* ```ts
|
|
1274
|
+
* const input = createInput()
|
|
1275
|
+
* // { kind: 'Input', schemas: [], operations: [] }
|
|
1276
|
+
* ```
|
|
1277
|
+
*
|
|
1278
|
+
* @example
|
|
1279
|
+
* ```ts
|
|
1280
|
+
* const input = createInput({ schemas: [petSchema] })
|
|
1281
|
+
* // keeps default operations: []
|
|
1282
|
+
* ```
|
|
1283
|
+
*/
|
|
1284
|
+
function createInput(overrides = {}) {
|
|
1285
|
+
return {
|
|
1286
|
+
schemas: [],
|
|
1287
|
+
operations: [],
|
|
1288
|
+
...overrides,
|
|
1289
|
+
kind: "Input"
|
|
1290
|
+
};
|
|
1291
|
+
}
|
|
1292
|
+
/**
|
|
1293
|
+
* Creates an `OutputNode` with a stable default for `files`.
|
|
1294
|
+
*
|
|
1295
|
+
* @example
|
|
1296
|
+
* ```ts
|
|
1297
|
+
* const output = createOutput()
|
|
1298
|
+
* // { kind: 'Output', files: [] }
|
|
1299
|
+
* ```
|
|
1300
|
+
*
|
|
1301
|
+
* @example
|
|
1302
|
+
* ```ts
|
|
1303
|
+
* const output = createOutput({ files: [petFile] })
|
|
1304
|
+
* ```
|
|
1305
|
+
*/
|
|
1306
|
+
function createOutput(overrides = {}) {
|
|
1307
|
+
return {
|
|
1308
|
+
files: [],
|
|
1309
|
+
...overrides,
|
|
1310
|
+
kind: "Output"
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
1313
|
+
/**
|
|
1314
|
+
* Creates an `OperationNode` with default empty arrays for `tags`, `parameters`, and `responses`.
|
|
1315
|
+
*
|
|
1316
|
+
* @example
|
|
1317
|
+
* ```ts
|
|
1318
|
+
* const operation = createOperation({
|
|
1319
|
+
* operationId: 'getPetById',
|
|
1320
|
+
* method: 'GET',
|
|
1321
|
+
* path: '/pet/{petId}',
|
|
1322
|
+
* })
|
|
1323
|
+
* // tags, parameters, and responses are []
|
|
1324
|
+
* ```
|
|
1325
|
+
*
|
|
1326
|
+
* @example
|
|
1327
|
+
* ```ts
|
|
1328
|
+
* const operation = createOperation({
|
|
1329
|
+
* operationId: 'findPets',
|
|
1330
|
+
* method: 'GET',
|
|
1331
|
+
* path: '/pet/findByStatus',
|
|
1332
|
+
* tags: ['pet'],
|
|
1333
|
+
* })
|
|
1334
|
+
* ```
|
|
1335
|
+
*/
|
|
1336
|
+
function createOperation(props) {
|
|
1337
|
+
return {
|
|
1338
|
+
tags: [],
|
|
1339
|
+
parameters: [],
|
|
1340
|
+
responses: [],
|
|
1341
|
+
...props,
|
|
1342
|
+
kind: "Operation"
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
/**
|
|
1346
|
+
* Maps schema `type` to its underlying `primitive`.
|
|
1347
|
+
* Primitive types map to themselves; special string formats map to `'string'`.
|
|
1348
|
+
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
|
|
1349
|
+
*/
|
|
1350
|
+
const TYPE_TO_PRIMITIVE = {
|
|
1351
|
+
string: "string",
|
|
1352
|
+
number: "number",
|
|
1353
|
+
integer: "integer",
|
|
1354
|
+
bigint: "bigint",
|
|
1355
|
+
boolean: "boolean",
|
|
1356
|
+
null: "null",
|
|
1357
|
+
any: "any",
|
|
1358
|
+
unknown: "unknown",
|
|
1359
|
+
void: "void",
|
|
1360
|
+
never: "never",
|
|
1361
|
+
object: "object",
|
|
1362
|
+
array: "array",
|
|
1363
|
+
date: "date",
|
|
1364
|
+
uuid: "string",
|
|
1365
|
+
email: "string",
|
|
1366
|
+
url: "string",
|
|
1367
|
+
datetime: "string",
|
|
1368
|
+
time: "string"
|
|
1369
|
+
};
|
|
1370
|
+
function createSchema(props) {
|
|
1371
|
+
const inferredPrimitive = TYPE_TO_PRIMITIVE[props.type];
|
|
1372
|
+
if (props["type"] === "object") return {
|
|
1373
|
+
properties: [],
|
|
1374
|
+
primitive: "object",
|
|
1375
|
+
...props,
|
|
1376
|
+
kind: "Schema"
|
|
1377
|
+
};
|
|
1378
|
+
return {
|
|
1379
|
+
primitive: inferredPrimitive,
|
|
1380
|
+
...props,
|
|
1381
|
+
kind: "Schema"
|
|
1382
|
+
};
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* Creates a `PropertyNode`.
|
|
1386
|
+
*
|
|
1387
|
+
* `required` defaults to `false`.
|
|
1388
|
+
* `schema.optional` and `schema.nullish` are derived from `required` and `schema.nullable`.
|
|
1389
|
+
*
|
|
1390
|
+
* @example
|
|
1391
|
+
* ```ts
|
|
1392
|
+
* const property = createProperty({
|
|
1393
|
+
* name: 'status',
|
|
1394
|
+
* schema: createSchema({ type: 'string' }),
|
|
1395
|
+
* })
|
|
1396
|
+
* // required=false, schema.optional=true
|
|
1397
|
+
* ```
|
|
1398
|
+
*
|
|
1399
|
+
* @example
|
|
1400
|
+
* ```ts
|
|
1401
|
+
* const property = createProperty({
|
|
1402
|
+
* name: 'status',
|
|
1403
|
+
* required: true,
|
|
1404
|
+
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1405
|
+
* })
|
|
1406
|
+
* // required=true, no optional/nullish
|
|
1407
|
+
* ```
|
|
1408
|
+
*/
|
|
1409
|
+
function createProperty(props) {
|
|
1410
|
+
const required = props.required ?? false;
|
|
1411
|
+
return {
|
|
1412
|
+
...props,
|
|
1413
|
+
kind: "Property",
|
|
1414
|
+
required,
|
|
1415
|
+
schema: syncOptionality(props.schema, required)
|
|
1416
|
+
};
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Creates a `ParameterNode`.
|
|
1420
|
+
*
|
|
1421
|
+
* `required` defaults to `false`.
|
|
1422
|
+
* Nested schema flags are set from `required` and `schema.nullable`.
|
|
1423
|
+
*
|
|
1424
|
+
* @example
|
|
1425
|
+
* ```ts
|
|
1426
|
+
* const param = createParameter({
|
|
1427
|
+
* name: 'petId',
|
|
1428
|
+
* in: 'path',
|
|
1429
|
+
* required: true,
|
|
1430
|
+
* schema: createSchema({ type: 'string' }),
|
|
1431
|
+
* })
|
|
1432
|
+
* ```
|
|
1433
|
+
*
|
|
1434
|
+
* @example
|
|
1435
|
+
* ```ts
|
|
1436
|
+
* const param = createParameter({
|
|
1437
|
+
* name: 'status',
|
|
1438
|
+
* in: 'query',
|
|
1439
|
+
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1440
|
+
* })
|
|
1441
|
+
* // required=false, schema.nullish=true
|
|
1442
|
+
* ```
|
|
1443
|
+
*/
|
|
1444
|
+
function createParameter(props) {
|
|
1445
|
+
const required = props.required ?? false;
|
|
1446
|
+
return {
|
|
1447
|
+
...props,
|
|
1448
|
+
kind: "Parameter",
|
|
1449
|
+
required,
|
|
1450
|
+
schema: syncOptionality(props.schema, required)
|
|
1451
|
+
};
|
|
1452
|
+
}
|
|
1453
|
+
/**
|
|
1454
|
+
* Creates a `ResponseNode`.
|
|
1455
|
+
*
|
|
1456
|
+
* @example
|
|
1457
|
+
* ```ts
|
|
1458
|
+
* const response = createResponse({
|
|
1459
|
+
* statusCode: '200',
|
|
1460
|
+
* description: 'Success',
|
|
1461
|
+
* schema: createSchema({ type: 'object', properties: [] }),
|
|
1462
|
+
* })
|
|
1463
|
+
* ```
|
|
1464
|
+
*/
|
|
1465
|
+
function createResponse(props) {
|
|
1466
|
+
return {
|
|
1467
|
+
...props,
|
|
1468
|
+
kind: "Response"
|
|
1469
|
+
};
|
|
1470
|
+
}
|
|
1471
|
+
/**
|
|
1472
|
+
* Creates a `FunctionParameterNode`.
|
|
1473
|
+
*
|
|
1474
|
+
* `optional` defaults to `false`.
|
|
1475
|
+
*
|
|
1476
|
+
* @example Required typed param
|
|
1477
|
+
* ```ts
|
|
1478
|
+
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }) })
|
|
1479
|
+
* // → petId: string
|
|
1480
|
+
* ```
|
|
1481
|
+
*
|
|
1482
|
+
* @example Optional param
|
|
1483
|
+
* ```ts
|
|
1484
|
+
* createFunctionParameter({ name: 'params', type: createParamsType({ variant: 'reference', name: 'QueryParams' }), optional: true })
|
|
1485
|
+
* // → params?: QueryParams
|
|
1486
|
+
* ```
|
|
1487
|
+
*
|
|
1488
|
+
* @example Param with default (implicitly optional; cannot combine with `optional: true`)
|
|
1489
|
+
* ```ts
|
|
1490
|
+
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), default: '{}' })
|
|
1491
|
+
* // → config: RequestConfig = {}
|
|
1492
|
+
* ```
|
|
1493
|
+
*/
|
|
1494
|
+
function createFunctionParameter(props) {
|
|
1495
|
+
return {
|
|
1496
|
+
optional: false,
|
|
1497
|
+
...props,
|
|
1498
|
+
kind: "FunctionParameter"
|
|
1499
|
+
};
|
|
1500
|
+
}
|
|
1501
|
+
/**
|
|
1502
|
+
* Creates a {@link TypeNode} representing a language-agnostic structured type expression.
|
|
1503
|
+
*
|
|
1504
|
+
* Use `variant: 'struct'` for inline anonymous types and `variant: 'member'` for a single
|
|
1505
|
+
* named field accessed from a group type. Each language's printer renders the variant
|
|
1506
|
+
* into its own syntax (TypeScript, Python, C#, Kotlin, …).
|
|
1507
|
+
*
|
|
1508
|
+
* @example Reference type (TypeScript: `QueryParams`)
|
|
1509
|
+
* ```ts
|
|
1510
|
+
* createParamsType({ variant: 'reference', name: 'QueryParams' })
|
|
1511
|
+
* ```
|
|
1512
|
+
*
|
|
1513
|
+
* @example Struct type (TypeScript: `{ petId: string }`)
|
|
1514
|
+
* ```ts
|
|
1515
|
+
* createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
|
|
1516
|
+
* ```
|
|
1517
|
+
*
|
|
1518
|
+
* @example Member type (TypeScript: `DeletePetPathParams['petId']`)
|
|
1519
|
+
* ```ts
|
|
1520
|
+
* createParamsType({ variant: 'member', base: 'DeletePetPathParams', key: 'petId' })
|
|
1521
|
+
* ```
|
|
1522
|
+
*/
|
|
1523
|
+
function createParamsType(props) {
|
|
1524
|
+
return {
|
|
1525
|
+
...props,
|
|
1526
|
+
kind: "ParamsType"
|
|
1527
|
+
};
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* Creates a `ParameterGroupNode` representing a group of related parameters treated as a unit.
|
|
1531
|
+
*
|
|
1532
|
+
* @example Grouped param (TypeScript declaration)
|
|
1533
|
+
* ```ts
|
|
1534
|
+
* createParameterGroup({
|
|
1535
|
+
* properties: [
|
|
1536
|
+
* createFunctionParameter({ name: 'id', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
1537
|
+
* createFunctionParameter({ name: 'name', type: createParamsType({ variant: 'reference', name: 'string' }), optional: true }),
|
|
1538
|
+
* ],
|
|
1539
|
+
* default: '{}',
|
|
1540
|
+
* })
|
|
1541
|
+
* // declaration → { id, name? }: { id: string; name?: string } = {}
|
|
1542
|
+
* // call → { id, name }
|
|
1543
|
+
* ```
|
|
1544
|
+
*
|
|
1545
|
+
* @example Inline (spread) — children emitted as individual top-level parameters
|
|
1546
|
+
* ```ts
|
|
1547
|
+
* createParameterGroup({
|
|
1548
|
+
* properties: [createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false })],
|
|
1549
|
+
* inline: true,
|
|
1550
|
+
* })
|
|
1551
|
+
* // declaration → petId: string
|
|
1552
|
+
* // call → petId
|
|
1553
|
+
* ```
|
|
1554
|
+
*/
|
|
1555
|
+
function createParameterGroup(props) {
|
|
1556
|
+
return {
|
|
1557
|
+
...props,
|
|
1558
|
+
kind: "ParameterGroup"
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
/**
|
|
1562
|
+
* Creates a `FunctionParametersNode` from an ordered list of parameters.
|
|
1563
|
+
*
|
|
1564
|
+
* @example
|
|
1565
|
+
* ```ts
|
|
1566
|
+
* createFunctionParameters({
|
|
1567
|
+
* params: [
|
|
1568
|
+
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
1569
|
+
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), optional: false, default: '{}' }),
|
|
1570
|
+
* ],
|
|
1571
|
+
* })
|
|
1572
|
+
* ```
|
|
1573
|
+
*
|
|
1574
|
+
* @example
|
|
1575
|
+
* ```ts
|
|
1576
|
+
* const empty = createFunctionParameters()
|
|
1577
|
+
* // { kind: 'FunctionParameters', params: [] }
|
|
1578
|
+
* ```
|
|
1579
|
+
*/
|
|
1580
|
+
function createFunctionParameters(props = {}) {
|
|
1581
|
+
return {
|
|
1582
|
+
params: [],
|
|
1583
|
+
...props,
|
|
1584
|
+
kind: "FunctionParameters"
|
|
1585
|
+
};
|
|
1586
|
+
}
|
|
1587
|
+
/**
|
|
1588
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
1589
|
+
*
|
|
1590
|
+
* @example Named import
|
|
1591
|
+
* ```ts
|
|
1592
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
1593
|
+
* // import { useState } from 'react'
|
|
1594
|
+
* ```
|
|
1595
|
+
*
|
|
1596
|
+
* @example Type-only import
|
|
1597
|
+
* ```ts
|
|
1598
|
+
* createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
|
|
1599
|
+
* // import type { FC } from 'react'
|
|
1600
|
+
* ```
|
|
1601
|
+
*/
|
|
1602
|
+
function createImport(props) {
|
|
1603
|
+
return {
|
|
1604
|
+
...props,
|
|
1605
|
+
kind: "Import"
|
|
1606
|
+
};
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
1610
|
+
*
|
|
1611
|
+
* @example Named export
|
|
1612
|
+
* ```ts
|
|
1613
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
1614
|
+
* // export { Pet } from './Pet'
|
|
1615
|
+
* ```
|
|
1616
|
+
*
|
|
1617
|
+
* @example Wildcard export
|
|
1618
|
+
* ```ts
|
|
1619
|
+
* createExport({ path: './utils' })
|
|
1620
|
+
* // export * from './utils'
|
|
1621
|
+
* ```
|
|
1622
|
+
*/
|
|
1623
|
+
function createExport(props) {
|
|
1624
|
+
return {
|
|
1625
|
+
...props,
|
|
1626
|
+
kind: "Export"
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
/**
|
|
1630
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
1631
|
+
*
|
|
1632
|
+
* @example
|
|
1633
|
+
* ```ts
|
|
1634
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
1635
|
+
* ```
|
|
1636
|
+
*/
|
|
1637
|
+
function createSource(props) {
|
|
1638
|
+
return {
|
|
1639
|
+
...props,
|
|
1640
|
+
kind: "Source"
|
|
1641
|
+
};
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
1645
|
+
*
|
|
1646
|
+
* Computes:
|
|
1647
|
+
* - `id` — SHA256 hash of the file path
|
|
1648
|
+
* - `name` — `baseName` without extension
|
|
1649
|
+
* - `extname` — extension extracted from `baseName`
|
|
1650
|
+
*
|
|
1651
|
+
* Deduplicates:
|
|
1652
|
+
* - `sources` via `combineSources`
|
|
1653
|
+
* - `exports` via `combineExports`
|
|
1654
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
1655
|
+
*
|
|
1656
|
+
* @throws {Error} when `baseName` has no extension.
|
|
1657
|
+
*
|
|
1658
|
+
* @example
|
|
1659
|
+
* ```ts
|
|
1660
|
+
* const file = createFile({
|
|
1661
|
+
* baseName: 'petStore.ts',
|
|
1662
|
+
* path: 'src/models/petStore.ts',
|
|
1663
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
1664
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
1665
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
1666
|
+
* })
|
|
1667
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
1668
|
+
* // file.name = 'petStore'
|
|
1669
|
+
* // file.extname = '.ts'
|
|
1670
|
+
* ```
|
|
1671
|
+
*/
|
|
1672
|
+
function createFile(input) {
|
|
1673
|
+
const extname = path.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
|
|
1674
|
+
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
1675
|
+
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
|
|
1676
|
+
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
1677
|
+
const resolvedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
|
|
1678
|
+
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
1679
|
+
return {
|
|
1680
|
+
kind: "File",
|
|
1681
|
+
...input,
|
|
1682
|
+
id: createHash("sha256").update(input.path).digest("hex"),
|
|
1683
|
+
name: trimExtName(input.baseName),
|
|
1684
|
+
extname,
|
|
1685
|
+
imports: resolvedImports,
|
|
1686
|
+
exports: resolvedExports,
|
|
1687
|
+
sources: resolvedSources,
|
|
1688
|
+
meta: input.meta ?? {}
|
|
1689
|
+
};
|
|
1690
|
+
}
|
|
1691
|
+
/**
|
|
1692
|
+
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
1693
|
+
*
|
|
1694
|
+
* Mirrors the `Const` component from `@kubb/renderer-jsx`.
|
|
1695
|
+
* The component's `children` are represented as `nodes`.
|
|
1696
|
+
*
|
|
1697
|
+
* @example Simple constant
|
|
1698
|
+
* ```ts
|
|
1699
|
+
* createConst({ name: 'pet' })
|
|
1700
|
+
* // const pet = ...
|
|
1701
|
+
* ```
|
|
1702
|
+
*
|
|
1703
|
+
* @example Exported constant with type and `as const`
|
|
1704
|
+
* ```ts
|
|
1705
|
+
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
1706
|
+
* // export const pets: Pet[] = ... as const
|
|
1707
|
+
* ```
|
|
1708
|
+
*
|
|
1709
|
+
* @example With JSDoc and child nodes
|
|
1710
|
+
* ```ts
|
|
1711
|
+
* createConst({
|
|
1712
|
+
* name: 'config',
|
|
1713
|
+
* export: true,
|
|
1714
|
+
* JSDoc: { comments: ['@description App configuration'] },
|
|
1715
|
+
* nodes: [],
|
|
1716
|
+
* })
|
|
1717
|
+
* ```
|
|
1718
|
+
*/
|
|
1719
|
+
function createConst(props) {
|
|
1720
|
+
return {
|
|
1721
|
+
...props,
|
|
1722
|
+
kind: "Const"
|
|
1723
|
+
};
|
|
1724
|
+
}
|
|
1725
|
+
/**
|
|
1726
|
+
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
1727
|
+
*
|
|
1728
|
+
* Mirrors the `Type` component from `@kubb/renderer-jsx`.
|
|
1729
|
+
* The component's `children` are represented as `nodes`.
|
|
1730
|
+
*
|
|
1731
|
+
* @example Simple type alias
|
|
1732
|
+
* ```ts
|
|
1733
|
+
* createType({ name: 'Pet' })
|
|
1734
|
+
* // type Pet = ...
|
|
1735
|
+
* ```
|
|
1736
|
+
*
|
|
1737
|
+
* @example Exported type with JSDoc
|
|
1738
|
+
* ```ts
|
|
1739
|
+
* createType({
|
|
1740
|
+
* name: 'PetStatus',
|
|
1741
|
+
* export: true,
|
|
1742
|
+
* JSDoc: { comments: ['@description Status of a pet'] },
|
|
1743
|
+
* })
|
|
1744
|
+
* // export type PetStatus = ...
|
|
1745
|
+
* ```
|
|
1746
|
+
*/
|
|
1747
|
+
function createType(props) {
|
|
1748
|
+
return {
|
|
1749
|
+
...props,
|
|
1750
|
+
kind: "Type"
|
|
1751
|
+
};
|
|
1752
|
+
}
|
|
1753
|
+
/**
|
|
1754
|
+
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
1755
|
+
*
|
|
1756
|
+
* Mirrors the `Function` component from `@kubb/renderer-jsx`.
|
|
1757
|
+
* The component's `children` are represented as `nodes`.
|
|
1758
|
+
*
|
|
1759
|
+
* @example Simple function
|
|
1760
|
+
* ```ts
|
|
1761
|
+
* createFunction({ name: 'getPet' })
|
|
1762
|
+
* // function getPet() { ... }
|
|
1763
|
+
* ```
|
|
1764
|
+
*
|
|
1765
|
+
* @example Exported async function with return type
|
|
1766
|
+
* ```ts
|
|
1767
|
+
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
1768
|
+
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
1769
|
+
* ```
|
|
1770
|
+
*
|
|
1771
|
+
* @example Function with generics and params
|
|
1772
|
+
* ```ts
|
|
1773
|
+
* createFunction({
|
|
1774
|
+
* name: 'identity',
|
|
1775
|
+
* export: true,
|
|
1776
|
+
* generics: ['T'],
|
|
1777
|
+
* params: 'value: T',
|
|
1778
|
+
* returnType: 'T',
|
|
1779
|
+
* })
|
|
1780
|
+
* // export function identity<T>(value: T): T { ... }
|
|
1781
|
+
* ```
|
|
1782
|
+
*/
|
|
1783
|
+
function createFunction(props) {
|
|
1784
|
+
return {
|
|
1785
|
+
...props,
|
|
1786
|
+
kind: "Function"
|
|
1787
|
+
};
|
|
1788
|
+
}
|
|
1789
|
+
/**
|
|
1790
|
+
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
1791
|
+
*
|
|
1792
|
+
* Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.
|
|
1793
|
+
* The component's `children` are represented as `nodes`.
|
|
1794
|
+
*
|
|
1795
|
+
* @example Simple arrow function
|
|
1796
|
+
* ```ts
|
|
1797
|
+
* createArrowFunction({ name: 'getPet' })
|
|
1798
|
+
* // const getPet = () => { ... }
|
|
1799
|
+
* ```
|
|
1800
|
+
*
|
|
1801
|
+
* @example Single-line exported arrow function
|
|
1802
|
+
* ```ts
|
|
1803
|
+
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
1804
|
+
* // export const double = (n: number) => ...
|
|
1805
|
+
* ```
|
|
1806
|
+
*
|
|
1807
|
+
* @example Async arrow function with generics
|
|
1808
|
+
* ```ts
|
|
1809
|
+
* createArrowFunction({
|
|
1810
|
+
* name: 'fetchPet',
|
|
1811
|
+
* export: true,
|
|
1812
|
+
* async: true,
|
|
1813
|
+
* generics: ['T'],
|
|
1814
|
+
* params: 'id: string',
|
|
1815
|
+
* returnType: 'T',
|
|
1816
|
+
* })
|
|
1817
|
+
* // export const fetchPet = async <T>(id: string): Promise<T> => { ... }
|
|
1818
|
+
* ```
|
|
1819
|
+
*/
|
|
1820
|
+
function createArrowFunction(props) {
|
|
1821
|
+
return {
|
|
1822
|
+
...props,
|
|
1823
|
+
kind: "ArrowFunction"
|
|
1824
|
+
};
|
|
1825
|
+
}
|
|
1826
|
+
/**
|
|
1827
|
+
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
1828
|
+
*
|
|
1829
|
+
* Use this instead of bare strings when building `nodes` arrays so that every
|
|
1830
|
+
* entry in the array is a typed {@link CodeNode}.
|
|
1831
|
+
*
|
|
1832
|
+
* @example
|
|
1833
|
+
* ```ts
|
|
1834
|
+
* createText('return fetch(id)')
|
|
1835
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
1836
|
+
* ```
|
|
1837
|
+
*/
|
|
1838
|
+
function createText(value) {
|
|
1839
|
+
return {
|
|
1840
|
+
value,
|
|
1841
|
+
kind: "Text"
|
|
1842
|
+
};
|
|
1843
|
+
}
|
|
1844
|
+
/**
|
|
1845
|
+
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
1846
|
+
*
|
|
1847
|
+
* Corresponds to `<br/>` in JSX components. Prints as an empty string which,
|
|
1848
|
+
* when joined with `\n` by `printNodes`, produces a blank line.
|
|
1849
|
+
*
|
|
1850
|
+
* @example
|
|
1851
|
+
* ```ts
|
|
1852
|
+
* createBreak()
|
|
1853
|
+
* // { kind: 'Break' }
|
|
1854
|
+
* ```
|
|
1855
|
+
*/
|
|
1856
|
+
function createBreak() {
|
|
1857
|
+
return { kind: "Break" };
|
|
1858
|
+
}
|
|
1859
|
+
/**
|
|
1860
|
+
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
1861
|
+
*
|
|
1862
|
+
* Use this to embed JSX markup (including fragments `<>…</>`) directly in generated code.
|
|
1863
|
+
*
|
|
1864
|
+
* @example
|
|
1865
|
+
* ```ts
|
|
1866
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
1867
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
1868
|
+
* ```
|
|
1869
|
+
*/
|
|
1870
|
+
function createJsx(value) {
|
|
1871
|
+
return {
|
|
1872
|
+
value,
|
|
1873
|
+
kind: "Jsx"
|
|
1874
|
+
};
|
|
1875
|
+
}
|
|
1876
|
+
//#endregion
|
|
1877
|
+
//#region src/printer.ts
|
|
1878
|
+
/**
|
|
1879
|
+
* Creates a schema printer factory.
|
|
1880
|
+
*
|
|
1881
|
+
* This function wraps a builder and makes options optional at call sites.
|
|
1882
|
+
*
|
|
1883
|
+
* The builder receives resolved options and returns:
|
|
1884
|
+
* - `name` — a unique identifier for the printer
|
|
1885
|
+
* - `options` — options stored on the returned printer instance
|
|
1886
|
+
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
1887
|
+
* - `print` _(optional)_ — top-level override exposed as `printer.print`
|
|
1888
|
+
* - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
|
|
1889
|
+
* - This keeps recursion safe and avoids self-calls
|
|
1890
|
+
*
|
|
1891
|
+
* When no `print` override is provided, `printer.print` falls back to `printer.transform` (the node-level dispatcher).
|
|
1892
|
+
*
|
|
1893
|
+
* @example Basic usage — Zod schema printer
|
|
1894
|
+
* ```ts
|
|
1895
|
+
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
1896
|
+
*
|
|
1897
|
+
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
1898
|
+
* name: 'zod',
|
|
1899
|
+
* options: { strict: options.strict ?? true },
|
|
1900
|
+
* nodes: {
|
|
1901
|
+
* string: () => 'z.string()',
|
|
1902
|
+
* object(node) {
|
|
1903
|
+
* const props = node.properties.map(p => `${p.name}: ${this.transform(p.schema)}`).join(', ')
|
|
1904
|
+
* return `z.object({ ${props} })`
|
|
1905
|
+
* },
|
|
1906
|
+
* },
|
|
1907
|
+
* }))
|
|
1908
|
+
* ```
|
|
1909
|
+
*/
|
|
1910
|
+
function definePrinter(build) {
|
|
1911
|
+
return createPrinterFactory((node) => node.type)(build);
|
|
1912
|
+
}
|
|
1913
|
+
/**
|
|
1914
|
+
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
1915
|
+
**
|
|
1916
|
+
* @example
|
|
1917
|
+
* ```ts
|
|
1918
|
+
* export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>(
|
|
1919
|
+
* (node) => kindToHandlerKey[node.kind],
|
|
1920
|
+
* )
|
|
1921
|
+
* ```
|
|
1922
|
+
*/
|
|
1923
|
+
function createPrinterFactory(getKey) {
|
|
1924
|
+
return function(build) {
|
|
1925
|
+
return (options) => {
|
|
1926
|
+
const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? {});
|
|
1927
|
+
const context = {
|
|
1928
|
+
options: resolvedOptions,
|
|
1929
|
+
transform: (node) => {
|
|
1930
|
+
const key = getKey(node);
|
|
1931
|
+
if (key === void 0) return null;
|
|
1932
|
+
const handler = nodes[key];
|
|
1933
|
+
if (!handler) return null;
|
|
1934
|
+
return handler.call(context, node);
|
|
1935
|
+
}
|
|
1936
|
+
};
|
|
1937
|
+
return {
|
|
1938
|
+
name,
|
|
1939
|
+
options: resolvedOptions,
|
|
1940
|
+
transform: context.transform,
|
|
1941
|
+
print: printOverride ? printOverride.bind(context) : context.transform
|
|
1942
|
+
};
|
|
1943
|
+
};
|
|
1944
|
+
};
|
|
1945
|
+
}
|
|
1946
|
+
//#endregion
|
|
1947
|
+
//#region src/resolvers.ts
|
|
1948
|
+
function findDiscriminator(mapping, ref) {
|
|
1949
|
+
if (!mapping || !ref) return null;
|
|
1950
|
+
return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null;
|
|
1951
|
+
}
|
|
1952
|
+
function childName(parentName, propName) {
|
|
1953
|
+
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
|
|
1954
|
+
}
|
|
1955
|
+
function enumPropName(parentName, propName, enumSuffix) {
|
|
1956
|
+
return pascalCase([
|
|
1957
|
+
parentName,
|
|
1958
|
+
propName,
|
|
1959
|
+
enumSuffix
|
|
1960
|
+
].filter(Boolean).join(" "));
|
|
1961
|
+
}
|
|
1962
|
+
/**
|
|
1963
|
+
* Collects import entries for all `ref` schema nodes in `node`.
|
|
1964
|
+
*/
|
|
1965
|
+
function collectImports({ node, nameMapping, resolve }) {
|
|
1966
|
+
return collect(node, { schema(schemaNode) {
|
|
1967
|
+
const schemaRef = narrowSchema(schemaNode, "ref");
|
|
1968
|
+
if (!schemaRef?.ref) return;
|
|
1969
|
+
const rawName = extractRefName(schemaRef.ref);
|
|
1970
|
+
const result = resolve(nameMapping.get(rawName) ?? rawName);
|
|
1971
|
+
if (!result) return;
|
|
1972
|
+
return result;
|
|
1973
|
+
} });
|
|
1974
|
+
}
|
|
1975
|
+
//#endregion
|
|
1976
|
+
//#region src/transformers.ts
|
|
1977
|
+
/**
|
|
1978
|
+
* Replaces a discriminator property's schema with a string enum of allowed values.
|
|
1979
|
+
*
|
|
1980
|
+
* If `node` is not an object schema, or if the property does not exist, the input
|
|
1981
|
+
* node is returned as-is.
|
|
1982
|
+
*
|
|
1983
|
+
* @example
|
|
1984
|
+
* ```ts
|
|
1985
|
+
* const schema = createSchema({
|
|
1986
|
+
* type: 'object',
|
|
1987
|
+
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
|
|
1988
|
+
* })
|
|
1989
|
+
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
|
|
1990
|
+
* ```
|
|
1991
|
+
*/
|
|
1992
|
+
function setDiscriminatorEnum({ node, propertyName, values, enumName }) {
|
|
1993
|
+
const objectNode = narrowSchema(node, "object");
|
|
1994
|
+
if (!objectNode?.properties?.length) return node;
|
|
1995
|
+
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node;
|
|
1996
|
+
return createSchema({
|
|
1997
|
+
...objectNode,
|
|
1998
|
+
properties: objectNode.properties.map((prop) => {
|
|
1999
|
+
if (prop.name !== propertyName) return prop;
|
|
2000
|
+
return createProperty({
|
|
2001
|
+
...prop,
|
|
2002
|
+
schema: createSchema({
|
|
2003
|
+
type: "enum",
|
|
2004
|
+
primitive: "string",
|
|
2005
|
+
enumValues: values,
|
|
2006
|
+
name: enumName,
|
|
2007
|
+
readOnly: prop.schema.readOnly,
|
|
2008
|
+
writeOnly: prop.schema.writeOnly
|
|
2009
|
+
})
|
|
2010
|
+
});
|
|
2011
|
+
})
|
|
2012
|
+
});
|
|
2013
|
+
}
|
|
2014
|
+
/**
|
|
2015
|
+
* Merges adjacent anonymous object members into a single anonymous object member.
|
|
2016
|
+
*
|
|
2017
|
+
* @example
|
|
2018
|
+
* ```ts
|
|
2019
|
+
* const merged = mergeAdjacentObjects([
|
|
2020
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
|
|
2021
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
|
|
2022
|
+
* ])
|
|
2023
|
+
* ```
|
|
2024
|
+
*/
|
|
2025
|
+
function mergeAdjacentObjects(members) {
|
|
2026
|
+
return members.reduce((acc, member) => {
|
|
2027
|
+
const objectMember = narrowSchema(member, "object");
|
|
2028
|
+
if (objectMember && !objectMember.name) {
|
|
2029
|
+
const previous = acc.at(-1);
|
|
2030
|
+
const previousObject = previous ? narrowSchema(previous, "object") : void 0;
|
|
2031
|
+
if (previousObject && !previousObject.name) {
|
|
2032
|
+
acc[acc.length - 1] = createSchema({
|
|
2033
|
+
...previousObject,
|
|
2034
|
+
properties: [...previousObject.properties ?? [], ...objectMember.properties ?? []]
|
|
2035
|
+
});
|
|
2036
|
+
return acc;
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
acc.push(member);
|
|
2040
|
+
return acc;
|
|
2041
|
+
}, []);
|
|
2042
|
+
}
|
|
2043
|
+
/**
|
|
2044
|
+
* Removes enum members that are covered by broader scalar primitives in the same union.
|
|
2045
|
+
*
|
|
2046
|
+
* @example
|
|
2047
|
+
* ```ts
|
|
2048
|
+
* const simplified = simplifyUnion([
|
|
2049
|
+
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
|
|
2050
|
+
* createSchema({ type: 'string' }),
|
|
2051
|
+
* ])
|
|
2052
|
+
* // keeps only string member
|
|
2053
|
+
* ```
|
|
2054
|
+
*/
|
|
2055
|
+
function simplifyUnion(members) {
|
|
2056
|
+
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
|
|
2057
|
+
if (!scalarPrimitives.size) return members;
|
|
2058
|
+
return members.filter((member) => {
|
|
2059
|
+
const enumNode = narrowSchema(member, "enum");
|
|
2060
|
+
if (!enumNode) return true;
|
|
2061
|
+
const primitive = enumNode.primitive;
|
|
2062
|
+
if (!primitive) return true;
|
|
2063
|
+
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
|
|
2064
|
+
if (scalarPrimitives.has(primitive)) return false;
|
|
2065
|
+
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
|
|
2066
|
+
return true;
|
|
2067
|
+
});
|
|
2068
|
+
}
|
|
2069
|
+
function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
2070
|
+
const enumNode = narrowSchema(propNode, "enum");
|
|
2071
|
+
if (enumNode?.primitive === "boolean") return {
|
|
2072
|
+
...propNode,
|
|
2073
|
+
name: void 0
|
|
2074
|
+
};
|
|
2075
|
+
if (enumNode) return {
|
|
2076
|
+
...propNode,
|
|
2077
|
+
name: enumPropName(parentName, propName, enumSuffix)
|
|
2078
|
+
};
|
|
2079
|
+
return propNode;
|
|
2080
|
+
}
|
|
2081
|
+
//#endregion
|
|
2082
|
+
export { caseParams, childName, collect, collectImports, collectReferencedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
745
2083
|
|
|
746
2084
|
//# sourceMappingURL=index.js.map
|