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