@kubb/ast 5.0.0-beta.1 → 5.0.0-beta.100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +17 -10
- package/README.md +56 -36
- package/dist/index.cjs +1071 -1665
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +136 -3335
- package/dist/index.js +1022 -1620
- package/dist/index.js.map +1 -1
- package/dist/rolldown-runtime-CNktS9qV.js +17 -0
- package/dist/types-DK2c0yY4.d.ts +2764 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/package.json +9 -9
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/constants.ts +0 -228
- package/src/factory.ts +0 -742
- package/src/guards.ts +0 -110
- package/src/index.ts +0 -45
- package/src/infer.ts +0 -130
- package/src/mocks.ts +0 -176
- package/src/nodes/base.ts +0 -56
- package/src/nodes/code.ts +0 -304
- package/src/nodes/file.ts +0 -230
- package/src/nodes/function.ts +0 -223
- package/src/nodes/http.ts +0 -119
- package/src/nodes/index.ts +0 -86
- package/src/nodes/operation.ts +0 -111
- package/src/nodes/output.ts +0 -26
- package/src/nodes/parameter.ts +0 -41
- package/src/nodes/property.ts +0 -34
- package/src/nodes/response.ts +0 -43
- package/src/nodes/root.ts +0 -64
- package/src/nodes/schema.ts +0 -656
- package/src/printer.ts +0 -250
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/transformers.ts +0 -159
- package/src/types.ts +0 -70
- package/src/utils.ts +0 -833
- package/src/visitor.ts +0 -592
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,15 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
6
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
8
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __exportAll = (all, no_symbols) => {
|
|
10
|
+
let target = {};
|
|
11
|
+
for (var name in all) __defProp(target, name, {
|
|
12
|
+
get: all[name],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
16
|
+
return target;
|
|
17
|
+
};
|
|
9
18
|
var __copyProps = (to, from, except, desc) => {
|
|
10
19
|
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
20
|
key = keys[i];
|
|
@@ -29,31 +38,10 @@ const visitorDepths = {
|
|
|
29
38
|
shallow: "shallow",
|
|
30
39
|
deep: "deep"
|
|
31
40
|
};
|
|
32
|
-
const nodeKinds = {
|
|
33
|
-
input: "Input",
|
|
34
|
-
output: "Output",
|
|
35
|
-
operation: "Operation",
|
|
36
|
-
schema: "Schema",
|
|
37
|
-
property: "Property",
|
|
38
|
-
parameter: "Parameter",
|
|
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"
|
|
50
|
-
};
|
|
51
41
|
/**
|
|
52
42
|
* Schema type discriminators used by all AST schema nodes.
|
|
53
43
|
*
|
|
54
|
-
*
|
|
55
|
-
* Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),
|
|
56
|
-
* and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.
|
|
44
|
+
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
|
|
57
45
|
*/
|
|
58
46
|
const schemaTypes = {
|
|
59
47
|
/**
|
|
@@ -73,7 +61,7 @@ const schemaTypes = {
|
|
|
73
61
|
*/
|
|
74
62
|
bigint: "bigint",
|
|
75
63
|
/**
|
|
76
|
-
* Boolean value
|
|
64
|
+
* Boolean value.
|
|
77
65
|
*/
|
|
78
66
|
boolean: "boolean",
|
|
79
67
|
/**
|
|
@@ -161,935 +149,314 @@ const schemaTypes = {
|
|
|
161
149
|
*/
|
|
162
150
|
never: "never"
|
|
163
151
|
};
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/guards.ts
|
|
164
154
|
/**
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
* Use `isScalarPrimitive()` to safely check whether a type is a scalar primitive.
|
|
168
|
-
*/
|
|
169
|
-
const SCALAR_PRIMITIVE_TYPES = new Set([
|
|
170
|
-
"string",
|
|
171
|
-
"number",
|
|
172
|
-
"integer",
|
|
173
|
-
"bigint",
|
|
174
|
-
"boolean"
|
|
175
|
-
]);
|
|
176
|
-
/**
|
|
177
|
-
* Type guard that returns `true` when `type` is a scalar primitive schema type.
|
|
155
|
+
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
178
156
|
*
|
|
179
|
-
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* const schema = createSchema({ type: 'string' })
|
|
160
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
161
|
+
* ```
|
|
180
162
|
*/
|
|
181
|
-
function
|
|
182
|
-
return
|
|
163
|
+
function narrowSchema(node, type) {
|
|
164
|
+
return node?.type === type ? node : null;
|
|
183
165
|
}
|
|
184
166
|
/**
|
|
185
|
-
*
|
|
167
|
+
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
|
|
186
168
|
*
|
|
187
|
-
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* if (isHttpOperationNode(node)) {
|
|
172
|
+
* console.log(node.method, node.path)
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
188
175
|
*/
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
put: "PUT",
|
|
193
|
-
patch: "PATCH",
|
|
194
|
-
delete: "DELETE",
|
|
195
|
-
head: "HEAD",
|
|
196
|
-
options: "OPTIONS",
|
|
197
|
-
trace: "TRACE"
|
|
198
|
-
};
|
|
199
|
-
/**
|
|
200
|
-
* Common MIME types used in request/response content negotiation.
|
|
201
|
-
*
|
|
202
|
-
* Covers JSON, XML, form data, PDFs, images, audio, and video formats.
|
|
203
|
-
* Use these as keys when serializing request/response bodies.
|
|
204
|
-
*/
|
|
205
|
-
const mediaTypes = {
|
|
206
|
-
applicationJson: "application/json",
|
|
207
|
-
applicationXml: "application/xml",
|
|
208
|
-
applicationFormUrlEncoded: "application/x-www-form-urlencoded",
|
|
209
|
-
applicationOctetStream: "application/octet-stream",
|
|
210
|
-
applicationPdf: "application/pdf",
|
|
211
|
-
applicationZip: "application/zip",
|
|
212
|
-
applicationGraphql: "application/graphql",
|
|
213
|
-
multipartFormData: "multipart/form-data",
|
|
214
|
-
textPlain: "text/plain",
|
|
215
|
-
textHtml: "text/html",
|
|
216
|
-
textCsv: "text/csv",
|
|
217
|
-
textXml: "text/xml",
|
|
218
|
-
imagePng: "image/png",
|
|
219
|
-
imageJpeg: "image/jpeg",
|
|
220
|
-
imageGif: "image/gif",
|
|
221
|
-
imageWebp: "image/webp",
|
|
222
|
-
imageSvgXml: "image/svg+xml",
|
|
223
|
-
audioMpeg: "audio/mpeg",
|
|
224
|
-
videoMp4: "video/mp4"
|
|
225
|
-
};
|
|
176
|
+
function isHttpOperationNode(node) {
|
|
177
|
+
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
|
|
178
|
+
}
|
|
226
179
|
//#endregion
|
|
227
|
-
//#region
|
|
180
|
+
//#region src/defineNode.ts
|
|
228
181
|
/**
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* and capitalizes each word according to `pascal`.
|
|
232
|
-
*
|
|
233
|
-
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
182
|
+
* Visitor callback names, one per traversable node kind, in traversal order.
|
|
183
|
+
* Kept in sync with the keys of `Visitor` in `visitor.ts`.
|
|
234
184
|
*/
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
185
|
+
const visitorKeys = [
|
|
186
|
+
"input",
|
|
187
|
+
"output",
|
|
188
|
+
"operation",
|
|
189
|
+
"schema",
|
|
190
|
+
"property",
|
|
191
|
+
"parameter",
|
|
192
|
+
"response"
|
|
193
|
+
];
|
|
242
194
|
/**
|
|
243
|
-
*
|
|
244
|
-
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
245
|
-
* Segments are joined with `/` to form a file path.
|
|
246
|
-
*
|
|
247
|
-
* Only splits on dots followed by a letter so that version numbers
|
|
248
|
-
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
249
|
-
*
|
|
250
|
-
* Empty segments are filtered before joining. They arise when the text starts with
|
|
251
|
-
* a dot followed immediately by a letter (e.g. `..Schema` splits into `['..', 'Schema']`
|
|
252
|
-
* and `'..'` transforms to an empty string). Without this filter the join would produce
|
|
253
|
-
* a leading `/`, which `path.resolve` would interpret as an absolute path, allowing
|
|
254
|
-
* generated files to escape the configured output directory.
|
|
195
|
+
* Builds a type guard that matches nodes of the given `kind`.
|
|
255
196
|
*/
|
|
256
|
-
function
|
|
257
|
-
|
|
258
|
-
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).filter(Boolean).join("/");
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Converts `text` to camelCase.
|
|
262
|
-
* When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
|
|
263
|
-
*
|
|
264
|
-
* @example
|
|
265
|
-
* camelCase('hello-world') // 'helloWorld'
|
|
266
|
-
* camelCase('pet.petId', { isFile: true }) // 'pet/petId'
|
|
267
|
-
*/
|
|
268
|
-
function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
269
|
-
if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
|
|
270
|
-
prefix,
|
|
271
|
-
suffix
|
|
272
|
-
} : {}));
|
|
273
|
-
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
197
|
+
function isKind(kind) {
|
|
198
|
+
return (node) => node?.kind === kind;
|
|
274
199
|
}
|
|
275
200
|
/**
|
|
276
|
-
*
|
|
277
|
-
*
|
|
201
|
+
* Defines a node once and derives its `create` builder, `is` guard, and traversal
|
|
202
|
+
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
|
|
203
|
+
* `kind`, so node construction lives in one place without scattered `as` casts.
|
|
278
204
|
*
|
|
279
|
-
* @example
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
|
|
285
|
-
prefix,
|
|
286
|
-
suffix
|
|
287
|
-
}) : camelCase(part));
|
|
288
|
-
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
|
|
289
|
-
}
|
|
290
|
-
//#endregion
|
|
291
|
-
//#region ../../internals/utils/src/reserved.ts
|
|
292
|
-
/**
|
|
293
|
-
* JavaScript and Java reserved words.
|
|
294
|
-
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
295
|
-
*/
|
|
296
|
-
const reservedWords = new Set([
|
|
297
|
-
"abstract",
|
|
298
|
-
"arguments",
|
|
299
|
-
"boolean",
|
|
300
|
-
"break",
|
|
301
|
-
"byte",
|
|
302
|
-
"case",
|
|
303
|
-
"catch",
|
|
304
|
-
"char",
|
|
305
|
-
"class",
|
|
306
|
-
"const",
|
|
307
|
-
"continue",
|
|
308
|
-
"debugger",
|
|
309
|
-
"default",
|
|
310
|
-
"delete",
|
|
311
|
-
"do",
|
|
312
|
-
"double",
|
|
313
|
-
"else",
|
|
314
|
-
"enum",
|
|
315
|
-
"eval",
|
|
316
|
-
"export",
|
|
317
|
-
"extends",
|
|
318
|
-
"false",
|
|
319
|
-
"final",
|
|
320
|
-
"finally",
|
|
321
|
-
"float",
|
|
322
|
-
"for",
|
|
323
|
-
"function",
|
|
324
|
-
"goto",
|
|
325
|
-
"if",
|
|
326
|
-
"implements",
|
|
327
|
-
"import",
|
|
328
|
-
"in",
|
|
329
|
-
"instanceof",
|
|
330
|
-
"int",
|
|
331
|
-
"interface",
|
|
332
|
-
"let",
|
|
333
|
-
"long",
|
|
334
|
-
"native",
|
|
335
|
-
"new",
|
|
336
|
-
"null",
|
|
337
|
-
"package",
|
|
338
|
-
"private",
|
|
339
|
-
"protected",
|
|
340
|
-
"public",
|
|
341
|
-
"return",
|
|
342
|
-
"short",
|
|
343
|
-
"static",
|
|
344
|
-
"super",
|
|
345
|
-
"switch",
|
|
346
|
-
"synchronized",
|
|
347
|
-
"this",
|
|
348
|
-
"throw",
|
|
349
|
-
"throws",
|
|
350
|
-
"transient",
|
|
351
|
-
"true",
|
|
352
|
-
"try",
|
|
353
|
-
"typeof",
|
|
354
|
-
"var",
|
|
355
|
-
"void",
|
|
356
|
-
"volatile",
|
|
357
|
-
"while",
|
|
358
|
-
"with",
|
|
359
|
-
"yield",
|
|
360
|
-
"Array",
|
|
361
|
-
"Date",
|
|
362
|
-
"hasOwnProperty",
|
|
363
|
-
"Infinity",
|
|
364
|
-
"isFinite",
|
|
365
|
-
"isNaN",
|
|
366
|
-
"isPrototypeOf",
|
|
367
|
-
"length",
|
|
368
|
-
"Math",
|
|
369
|
-
"name",
|
|
370
|
-
"NaN",
|
|
371
|
-
"Number",
|
|
372
|
-
"Object",
|
|
373
|
-
"prototype",
|
|
374
|
-
"String",
|
|
375
|
-
"toString",
|
|
376
|
-
"undefined",
|
|
377
|
-
"valueOf"
|
|
378
|
-
]);
|
|
379
|
-
/**
|
|
380
|
-
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
205
|
+
* @example Simple node
|
|
206
|
+
* ```ts
|
|
207
|
+
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
|
|
208
|
+
* const createImport = importDef.create
|
|
209
|
+
* ```
|
|
381
210
|
*
|
|
382
|
-
* @example
|
|
211
|
+
* @example Node with a build hook
|
|
383
212
|
* ```ts
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
213
|
+
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
|
|
214
|
+
* kind: 'Property',
|
|
215
|
+
* build: (props) => ({ ...props, required: props.required ?? false }),
|
|
216
|
+
* children: ['schema'],
|
|
217
|
+
* visitorKey: 'property',
|
|
218
|
+
* })
|
|
387
219
|
* ```
|
|
388
220
|
*/
|
|
389
|
-
function
|
|
390
|
-
|
|
391
|
-
|
|
221
|
+
function defineNode(config) {
|
|
222
|
+
const { kind, defaults, build, children, visitorKey } = config;
|
|
223
|
+
function create(input) {
|
|
224
|
+
const base = build ? build(input) : input;
|
|
225
|
+
const node = {
|
|
226
|
+
kind,
|
|
227
|
+
...defaults,
|
|
228
|
+
...base
|
|
229
|
+
};
|
|
230
|
+
node.kind = kind;
|
|
231
|
+
return node;
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
kind,
|
|
235
|
+
create,
|
|
236
|
+
is: isKind(kind),
|
|
237
|
+
children,
|
|
238
|
+
visitorKey
|
|
239
|
+
};
|
|
392
240
|
}
|
|
393
241
|
//#endregion
|
|
394
|
-
//#region
|
|
242
|
+
//#region src/nodes/code.ts
|
|
395
243
|
/**
|
|
396
|
-
*
|
|
397
|
-
* Only removes the last `.ext` segment when the dot is not part of a directory name.
|
|
398
|
-
*
|
|
399
|
-
* @example
|
|
400
|
-
* trimExtName('petStore.ts') // 'petStore'
|
|
401
|
-
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
|
|
402
|
-
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
|
|
403
|
-
* trimExtName('noExtension') // 'noExtension'
|
|
244
|
+
* Definition for the {@link ConstNode}.
|
|
404
245
|
*/
|
|
405
|
-
|
|
406
|
-
const dotIndex = text.lastIndexOf(".");
|
|
407
|
-
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
408
|
-
return text;
|
|
409
|
-
}
|
|
410
|
-
//#endregion
|
|
411
|
-
//#region src/guards.ts
|
|
246
|
+
const constDef = defineNode({ kind: "Const" });
|
|
412
247
|
/**
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
* @example
|
|
416
|
-
* ```ts
|
|
417
|
-
* const schema = createSchema({ type: 'string' })
|
|
418
|
-
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | undefined
|
|
419
|
-
* ```
|
|
248
|
+
* Definition for the {@link TypeNode}.
|
|
420
249
|
*/
|
|
421
|
-
|
|
422
|
-
return node?.type === type ? node : void 0;
|
|
423
|
-
}
|
|
424
|
-
function isKind(kind) {
|
|
425
|
-
return (node) => node.kind === kind;
|
|
426
|
-
}
|
|
250
|
+
const typeDef = defineNode({ kind: "Type" });
|
|
427
251
|
/**
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
* @example
|
|
431
|
-
* ```ts
|
|
432
|
-
* if (isInputNode(node)) {
|
|
433
|
-
* console.log(node.schemas.length)
|
|
434
|
-
* }
|
|
435
|
-
* ```
|
|
252
|
+
* Definition for the {@link FunctionNode}.
|
|
436
253
|
*/
|
|
437
|
-
const
|
|
254
|
+
const functionDef = defineNode({ kind: "Function" });
|
|
438
255
|
/**
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
* @example
|
|
442
|
-
* ```ts
|
|
443
|
-
* if (isOutputNode(node)) {
|
|
444
|
-
* console.log(node.files.length)
|
|
445
|
-
* }
|
|
446
|
-
* ```
|
|
256
|
+
* Definition for the {@link ArrowFunctionNode}.
|
|
447
257
|
*/
|
|
448
|
-
const
|
|
258
|
+
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
|
|
449
259
|
/**
|
|
450
|
-
*
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
*
|
|
260
|
+
* Definition for the {@link TextNode}.
|
|
261
|
+
*/
|
|
262
|
+
const textDef = defineNode({
|
|
263
|
+
kind: "Text",
|
|
264
|
+
build: (value) => ({ value })
|
|
265
|
+
});
|
|
266
|
+
/**
|
|
267
|
+
* Definition for the {@link BreakNode}.
|
|
458
268
|
*/
|
|
459
|
-
const
|
|
269
|
+
const breakDef = defineNode({
|
|
270
|
+
kind: "Break",
|
|
271
|
+
build: () => ({})
|
|
272
|
+
});
|
|
460
273
|
/**
|
|
461
|
-
*
|
|
274
|
+
* Definition for the {@link JsxNode}.
|
|
275
|
+
*/
|
|
276
|
+
const jsxDef = defineNode({
|
|
277
|
+
kind: "Jsx",
|
|
278
|
+
build: (value) => ({ value })
|
|
279
|
+
});
|
|
280
|
+
/**
|
|
281
|
+
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
462
282
|
*
|
|
463
|
-
* @example
|
|
283
|
+
* @example Exported constant with type and `as const`
|
|
464
284
|
* ```ts
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
* }
|
|
285
|
+
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
286
|
+
* // export const pets: Pet[] = ... as const
|
|
468
287
|
* ```
|
|
469
288
|
*/
|
|
470
|
-
const
|
|
471
|
-
isKind("Property");
|
|
472
|
-
isKind("Parameter");
|
|
473
|
-
isKind("Response");
|
|
474
|
-
isKind("FunctionParameter");
|
|
475
|
-
isKind("ParameterGroup");
|
|
476
|
-
isKind("FunctionParameters");
|
|
477
|
-
//#endregion
|
|
478
|
-
//#region src/refs.ts
|
|
289
|
+
const createConst = constDef.create;
|
|
479
290
|
/**
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
* Example: `#/components/schemas/Pet` becomes `Pet`.
|
|
291
|
+
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
483
292
|
*
|
|
484
293
|
* @example
|
|
485
294
|
* ```ts
|
|
486
|
-
*
|
|
295
|
+
* createType({ name: 'Pet', export: true })
|
|
296
|
+
* // export type Pet = ...
|
|
487
297
|
* ```
|
|
488
298
|
*/
|
|
489
|
-
|
|
490
|
-
return ref.split("/").at(-1) ?? ref;
|
|
491
|
-
}
|
|
492
|
-
//#endregion
|
|
493
|
-
//#region src/visitor.ts
|
|
299
|
+
const createType = typeDef.create;
|
|
494
300
|
/**
|
|
495
|
-
* Creates a
|
|
496
|
-
*
|
|
497
|
-
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
|
|
301
|
+
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
498
302
|
*
|
|
499
303
|
* @example
|
|
500
304
|
* ```ts
|
|
501
|
-
*
|
|
502
|
-
*
|
|
503
|
-
* await limit(() => task())
|
|
504
|
-
* }
|
|
505
|
-
* // only 2 tasks run at the same time
|
|
305
|
+
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
306
|
+
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
506
307
|
* ```
|
|
507
308
|
*/
|
|
508
|
-
|
|
509
|
-
let active = 0;
|
|
510
|
-
const queue = [];
|
|
511
|
-
function next() {
|
|
512
|
-
if (active < concurrency && queue.length > 0) {
|
|
513
|
-
active++;
|
|
514
|
-
queue.shift()();
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
return function limit(fn) {
|
|
518
|
-
return new Promise((resolve, reject) => {
|
|
519
|
-
queue.push(() => {
|
|
520
|
-
Promise.resolve(fn()).then(resolve, reject).finally(() => {
|
|
521
|
-
active--;
|
|
522
|
-
next();
|
|
523
|
-
});
|
|
524
|
-
});
|
|
525
|
-
next();
|
|
526
|
-
});
|
|
527
|
-
};
|
|
528
|
-
}
|
|
309
|
+
const createFunction = functionDef.create;
|
|
529
310
|
/**
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
* For `Schema` nodes, children (`properties`, `items`, `members`, and non-boolean
|
|
533
|
-
* `additionalProperties`) are only included
|
|
534
|
-
* when `recurse` is `true`; shallow mode skips them.
|
|
311
|
+
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
535
312
|
*
|
|
536
313
|
* @example
|
|
537
314
|
* ```ts
|
|
538
|
-
*
|
|
539
|
-
* //
|
|
315
|
+
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
316
|
+
* // export const double = (n: number) => ...
|
|
540
317
|
* ```
|
|
541
318
|
*/
|
|
542
|
-
|
|
543
|
-
switch (node.kind) {
|
|
544
|
-
case "Input": return [...node.schemas, ...node.operations];
|
|
545
|
-
case "Output": return [];
|
|
546
|
-
case "Operation": return [
|
|
547
|
-
...node.parameters,
|
|
548
|
-
...node.requestBody?.content?.flatMap((c) => c.schema ? [c.schema] : []) ?? [],
|
|
549
|
-
...node.responses
|
|
550
|
-
];
|
|
551
|
-
case "Schema": {
|
|
552
|
-
const children = [];
|
|
553
|
-
if (!recurse) return [];
|
|
554
|
-
if ("properties" in node && node.properties.length > 0) children.push(...node.properties);
|
|
555
|
-
if ("items" in node && node.items) children.push(...node.items);
|
|
556
|
-
if ("members" in node && node.members) children.push(...node.members);
|
|
557
|
-
if ("additionalProperties" in node && node.additionalProperties && node.additionalProperties !== true) children.push(node.additionalProperties);
|
|
558
|
-
return children;
|
|
559
|
-
}
|
|
560
|
-
case "Property": return [node.schema];
|
|
561
|
-
case "Parameter": return [node.schema];
|
|
562
|
-
case "Response": return node.schema ? [node.schema] : [];
|
|
563
|
-
case "FunctionParameter":
|
|
564
|
-
case "ParameterGroup":
|
|
565
|
-
case "FunctionParameters":
|
|
566
|
-
case "Type": return [];
|
|
567
|
-
default: return [];
|
|
568
|
-
}
|
|
569
|
-
}
|
|
319
|
+
const createArrowFunction = arrowFunctionDef.create;
|
|
570
320
|
/**
|
|
571
|
-
*
|
|
572
|
-
* Sibling nodes at each level are visited concurrently up to `options.concurrency`
|
|
573
|
-
* (default: `WALK_CONCURRENCY`).
|
|
321
|
+
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
574
322
|
*
|
|
575
323
|
* @example
|
|
576
324
|
* ```ts
|
|
577
|
-
*
|
|
578
|
-
*
|
|
579
|
-
* console.log(node.operationId)
|
|
580
|
-
* },
|
|
581
|
-
* })
|
|
325
|
+
* createText('return fetch(id)')
|
|
326
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
582
327
|
* ```
|
|
328
|
+
*/
|
|
329
|
+
const createText = textDef.create;
|
|
330
|
+
/**
|
|
331
|
+
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
583
332
|
*
|
|
584
333
|
* @example
|
|
585
334
|
* ```ts
|
|
586
|
-
*
|
|
587
|
-
*
|
|
335
|
+
* createBreak()
|
|
336
|
+
* // { kind: 'Break' }
|
|
588
337
|
* ```
|
|
589
338
|
*/
|
|
590
|
-
|
|
591
|
-
return
|
|
592
|
-
}
|
|
593
|
-
async function _walk(node, visitor, recurse, limit, parent) {
|
|
594
|
-
switch (node.kind) {
|
|
595
|
-
case "Input":
|
|
596
|
-
await limit(() => visitor.input?.(node, { parent }));
|
|
597
|
-
break;
|
|
598
|
-
case "Output":
|
|
599
|
-
await limit(() => visitor.output?.(node, { parent }));
|
|
600
|
-
break;
|
|
601
|
-
case "Operation":
|
|
602
|
-
await limit(() => visitor.operation?.(node, { parent }));
|
|
603
|
-
break;
|
|
604
|
-
case "Schema":
|
|
605
|
-
await limit(() => visitor.schema?.(node, { parent }));
|
|
606
|
-
break;
|
|
607
|
-
case "Property":
|
|
608
|
-
await limit(() => visitor.property?.(node, { parent }));
|
|
609
|
-
break;
|
|
610
|
-
case "Parameter":
|
|
611
|
-
await limit(() => visitor.parameter?.(node, { parent }));
|
|
612
|
-
break;
|
|
613
|
-
case "Response":
|
|
614
|
-
await limit(() => visitor.response?.(node, { parent }));
|
|
615
|
-
break;
|
|
616
|
-
case "FunctionParameter":
|
|
617
|
-
case "ParameterGroup":
|
|
618
|
-
case "FunctionParameters": break;
|
|
619
|
-
}
|
|
620
|
-
const children = getChildren(node, recurse);
|
|
621
|
-
for (const child of children) await _walk(child, visitor, recurse, limit, node);
|
|
622
|
-
}
|
|
623
|
-
function transform(node, options) {
|
|
624
|
-
const { depth, parent, ...visitor } = options;
|
|
625
|
-
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
626
|
-
switch (node.kind) {
|
|
627
|
-
case "Input": {
|
|
628
|
-
let input = node;
|
|
629
|
-
const replaced = visitor.input?.(input, { parent });
|
|
630
|
-
if (replaced) input = replaced;
|
|
631
|
-
return {
|
|
632
|
-
...input,
|
|
633
|
-
schemas: input.schemas.map((s) => transform(s, {
|
|
634
|
-
...options,
|
|
635
|
-
parent: input
|
|
636
|
-
})),
|
|
637
|
-
operations: input.operations.map((op) => transform(op, {
|
|
638
|
-
...options,
|
|
639
|
-
parent: input
|
|
640
|
-
}))
|
|
641
|
-
};
|
|
642
|
-
}
|
|
643
|
-
case "Output": {
|
|
644
|
-
let output = node;
|
|
645
|
-
const replaced = visitor.output?.(output, { parent });
|
|
646
|
-
if (replaced) output = replaced;
|
|
647
|
-
return output;
|
|
648
|
-
}
|
|
649
|
-
case "Operation": {
|
|
650
|
-
let op = node;
|
|
651
|
-
const replaced = visitor.operation?.(op, { parent });
|
|
652
|
-
if (replaced) op = replaced;
|
|
653
|
-
return {
|
|
654
|
-
...op,
|
|
655
|
-
parameters: op.parameters.map((p) => transform(p, {
|
|
656
|
-
...options,
|
|
657
|
-
parent: op
|
|
658
|
-
})),
|
|
659
|
-
requestBody: op.requestBody ? {
|
|
660
|
-
...op.requestBody,
|
|
661
|
-
content: op.requestBody.content?.map((c) => ({
|
|
662
|
-
...c,
|
|
663
|
-
schema: c.schema ? transform(c.schema, {
|
|
664
|
-
...options,
|
|
665
|
-
parent: op
|
|
666
|
-
}) : void 0
|
|
667
|
-
}))
|
|
668
|
-
} : void 0,
|
|
669
|
-
responses: op.responses.map((r) => transform(r, {
|
|
670
|
-
...options,
|
|
671
|
-
parent: op
|
|
672
|
-
}))
|
|
673
|
-
};
|
|
674
|
-
}
|
|
675
|
-
case "Schema": {
|
|
676
|
-
let schema = node;
|
|
677
|
-
const replaced = visitor.schema?.(schema, { parent });
|
|
678
|
-
if (replaced) schema = replaced;
|
|
679
|
-
const childOptions = {
|
|
680
|
-
...options,
|
|
681
|
-
parent: schema
|
|
682
|
-
};
|
|
683
|
-
return {
|
|
684
|
-
...schema,
|
|
685
|
-
..."properties" in schema && recurse ? { properties: schema.properties.map((p) => transform(p, childOptions)) } : {},
|
|
686
|
-
..."items" in schema && recurse ? { items: schema.items?.map((i) => transform(i, childOptions)) } : {},
|
|
687
|
-
..."members" in schema && recurse ? { members: schema.members?.map((m) => transform(m, childOptions)) } : {},
|
|
688
|
-
..."additionalProperties" in schema && recurse && schema.additionalProperties && schema.additionalProperties !== true ? { additionalProperties: transform(schema.additionalProperties, childOptions) } : {}
|
|
689
|
-
};
|
|
690
|
-
}
|
|
691
|
-
case "Property": {
|
|
692
|
-
let prop = node;
|
|
693
|
-
const replaced = visitor.property?.(prop, { parent });
|
|
694
|
-
if (replaced) prop = replaced;
|
|
695
|
-
return createProperty({
|
|
696
|
-
...prop,
|
|
697
|
-
schema: transform(prop.schema, {
|
|
698
|
-
...options,
|
|
699
|
-
parent: prop
|
|
700
|
-
})
|
|
701
|
-
});
|
|
702
|
-
}
|
|
703
|
-
case "Parameter": {
|
|
704
|
-
let param = node;
|
|
705
|
-
const replaced = visitor.parameter?.(param, { parent });
|
|
706
|
-
if (replaced) param = replaced;
|
|
707
|
-
return createParameter({
|
|
708
|
-
...param,
|
|
709
|
-
schema: transform(param.schema, {
|
|
710
|
-
...options,
|
|
711
|
-
parent: param
|
|
712
|
-
})
|
|
713
|
-
});
|
|
714
|
-
}
|
|
715
|
-
case "Response": {
|
|
716
|
-
let response = node;
|
|
717
|
-
const replaced = visitor.response?.(response, { parent });
|
|
718
|
-
if (replaced) response = replaced;
|
|
719
|
-
return {
|
|
720
|
-
...response,
|
|
721
|
-
schema: transform(response.schema, {
|
|
722
|
-
...options,
|
|
723
|
-
parent: response
|
|
724
|
-
})
|
|
725
|
-
};
|
|
726
|
-
}
|
|
727
|
-
case "FunctionParameter":
|
|
728
|
-
case "ParameterGroup":
|
|
729
|
-
case "FunctionParameters":
|
|
730
|
-
case "Type": return node;
|
|
731
|
-
default: return node;
|
|
732
|
-
}
|
|
339
|
+
function createBreak() {
|
|
340
|
+
return breakDef.create();
|
|
733
341
|
}
|
|
734
342
|
/**
|
|
735
|
-
*
|
|
736
|
-
*
|
|
737
|
-
* Non-`undefined` values returned by visitor callbacks are appended to the result.
|
|
738
|
-
*
|
|
739
|
-
* @example
|
|
740
|
-
* ```ts
|
|
741
|
-
* const ids = collect(root, {
|
|
742
|
-
* operation(node) {
|
|
743
|
-
* return node.operationId
|
|
744
|
-
* },
|
|
745
|
-
* })
|
|
746
|
-
* ```
|
|
343
|
+
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
747
344
|
*
|
|
748
345
|
* @example
|
|
749
346
|
* ```ts
|
|
750
|
-
*
|
|
751
|
-
*
|
|
347
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
348
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
752
349
|
* ```
|
|
753
350
|
*/
|
|
754
|
-
|
|
755
|
-
const { depth, parent, ...visitor } = options;
|
|
756
|
-
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
757
|
-
const results = [];
|
|
758
|
-
let v;
|
|
759
|
-
switch (node.kind) {
|
|
760
|
-
case "Input":
|
|
761
|
-
v = visitor.input?.(node, { parent });
|
|
762
|
-
break;
|
|
763
|
-
case "Output":
|
|
764
|
-
v = visitor.output?.(node, { parent });
|
|
765
|
-
break;
|
|
766
|
-
case "Operation":
|
|
767
|
-
v = visitor.operation?.(node, { parent });
|
|
768
|
-
break;
|
|
769
|
-
case "Schema":
|
|
770
|
-
v = visitor.schema?.(node, { parent });
|
|
771
|
-
break;
|
|
772
|
-
case "Property":
|
|
773
|
-
v = visitor.property?.(node, { parent });
|
|
774
|
-
break;
|
|
775
|
-
case "Parameter":
|
|
776
|
-
v = visitor.parameter?.(node, { parent });
|
|
777
|
-
break;
|
|
778
|
-
case "Response":
|
|
779
|
-
v = visitor.response?.(node, { parent });
|
|
780
|
-
break;
|
|
781
|
-
case "FunctionParameter":
|
|
782
|
-
case "ParameterGroup":
|
|
783
|
-
case "FunctionParameters": break;
|
|
784
|
-
}
|
|
785
|
-
if (v !== void 0) results.push(v);
|
|
786
|
-
for (const child of getChildren(node, recurse)) for (const item of collect(child, {
|
|
787
|
-
...options,
|
|
788
|
-
parent: node
|
|
789
|
-
})) results.push(item);
|
|
790
|
-
return results;
|
|
791
|
-
}
|
|
351
|
+
const createJsx = jsxDef.create;
|
|
792
352
|
//#endregion
|
|
793
|
-
//#region src/
|
|
794
|
-
const plainStringTypes = new Set([
|
|
795
|
-
"string",
|
|
796
|
-
"uuid",
|
|
797
|
-
"email",
|
|
798
|
-
"url",
|
|
799
|
-
"datetime"
|
|
800
|
-
]);
|
|
353
|
+
//#region src/nodes/content.ts
|
|
801
354
|
/**
|
|
802
|
-
*
|
|
803
|
-
*
|
|
804
|
-
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node
|
|
805
|
-
* override the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
|
|
806
|
-
*
|
|
807
|
-
* @example
|
|
808
|
-
* ```ts
|
|
809
|
-
* // Ref with description override
|
|
810
|
-
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
|
|
811
|
-
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
|
|
812
|
-
* ```
|
|
355
|
+
* Definition for the {@link ContentNode}.
|
|
813
356
|
*/
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
|
|
819
|
-
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
|
|
820
|
-
return createSchema({
|
|
821
|
-
...ref.schema,
|
|
822
|
-
...definedOverrides
|
|
823
|
-
});
|
|
824
|
-
}
|
|
357
|
+
const contentDef = defineNode({
|
|
358
|
+
kind: "Content",
|
|
359
|
+
children: ["schema"]
|
|
360
|
+
});
|
|
825
361
|
/**
|
|
826
|
-
*
|
|
827
|
-
*
|
|
828
|
-
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
|
|
829
|
-
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
|
|
362
|
+
* Creates a `ContentNode` for a single request-body or response content type.
|
|
830
363
|
*/
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
if (temporal) return temporal.representation !== "date";
|
|
835
|
-
return false;
|
|
836
|
-
}
|
|
364
|
+
const createContent = contentDef.create;
|
|
365
|
+
//#endregion
|
|
366
|
+
//#region ../../internals/utils/src/fs.ts
|
|
837
367
|
/**
|
|
838
|
-
*
|
|
368
|
+
* Strips the file extension from a path or file name.
|
|
369
|
+
* Only removes the last `.ext` segment when the dot is not part of a directory name.
|
|
839
370
|
*
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
371
|
+
* @example
|
|
372
|
+
* trimExtName('petStore.ts') // 'petStore'
|
|
373
|
+
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
|
|
374
|
+
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
|
|
375
|
+
* trimExtName('noExtension') // 'noExtension'
|
|
843
376
|
*/
|
|
844
|
-
function
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
return {
|
|
849
|
-
...param,
|
|
850
|
-
name: transformed
|
|
851
|
-
};
|
|
852
|
-
});
|
|
377
|
+
function trimExtName(text) {
|
|
378
|
+
const dotIndex = text.lastIndexOf(".");
|
|
379
|
+
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
380
|
+
return text;
|
|
853
381
|
}
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region ../../internals/utils/src/promise.ts
|
|
854
384
|
/**
|
|
855
|
-
*
|
|
385
|
+
* Wraps `factory` with a keyed cache backed by the provided store.
|
|
856
386
|
*
|
|
857
|
-
*
|
|
387
|
+
* Pass a `WeakMap` for object keys (results are GC-eligible when the key is
|
|
388
|
+
* collected) or a `Map` for primitive keys. For multi-argument functions,
|
|
389
|
+
* nest two `memoize` calls — the outer keyed by the first argument, the
|
|
390
|
+
* inner (created once per outer miss) keyed by the second.
|
|
391
|
+
*
|
|
392
|
+
* Because the cache is owned by the caller, it can be shared, inspected, or
|
|
393
|
+
* cleared independently of the memoized function.
|
|
394
|
+
*
|
|
395
|
+
* @example Single WeakMap key
|
|
858
396
|
* ```ts
|
|
859
|
-
*
|
|
860
|
-
*
|
|
397
|
+
* const cache = new WeakMap<SchemaNode, Set<string>>()
|
|
398
|
+
* const getRefs = memoize(cache, (node) => collectRefs(node))
|
|
399
|
+
* ```
|
|
400
|
+
*
|
|
401
|
+
* @example Single Map key (primitive)
|
|
402
|
+
* ```ts
|
|
403
|
+
* const cache = new Map<string, Resolver>()
|
|
404
|
+
* const getResolver = memoize(cache, (name) => buildResolver(name))
|
|
405
|
+
* ```
|
|
406
|
+
*
|
|
407
|
+
* @example Two-level (object + primitive)
|
|
408
|
+
* ```ts
|
|
409
|
+
* const outer = new WeakMap<Params[], Map<string, Params[]>>()
|
|
410
|
+
* const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
|
|
411
|
+
* fn(params)('camelcase')
|
|
861
412
|
* ```
|
|
862
413
|
*/
|
|
863
|
-
function
|
|
864
|
-
return
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
schema: createSchema({
|
|
870
|
-
type: "enum",
|
|
871
|
-
primitive: "string",
|
|
872
|
-
enumValues: [value]
|
|
873
|
-
}),
|
|
874
|
-
required: true
|
|
875
|
-
})]
|
|
876
|
-
});
|
|
877
|
-
}
|
|
878
|
-
function resolveParamsType({ node, param, resolver }) {
|
|
879
|
-
if (!resolver) return createParamsType({
|
|
880
|
-
variant: "reference",
|
|
881
|
-
name: param.schema.primitive ?? "unknown"
|
|
882
|
-
});
|
|
883
|
-
const individualName = resolver.resolveParamName(node, param);
|
|
884
|
-
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
|
|
885
|
-
const groupResolvers = {
|
|
886
|
-
path: resolver.resolvePathParamsName,
|
|
887
|
-
query: resolver.resolveQueryParamsName,
|
|
888
|
-
header: resolver.resolveHeaderParamsName
|
|
414
|
+
function memoize(store, factory) {
|
|
415
|
+
return (key) => {
|
|
416
|
+
if (store.has(key)) return store.get(key);
|
|
417
|
+
const value = factory(key);
|
|
418
|
+
store.set(key, value);
|
|
419
|
+
return value;
|
|
889
420
|
};
|
|
890
|
-
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
|
|
891
|
-
if (groupName && groupName !== individualName) return createParamsType({
|
|
892
|
-
variant: "member",
|
|
893
|
-
base: groupName,
|
|
894
|
-
key: param.name
|
|
895
|
-
});
|
|
896
|
-
return createParamsType({
|
|
897
|
-
variant: "reference",
|
|
898
|
-
name: individualName
|
|
899
|
-
});
|
|
900
421
|
}
|
|
422
|
+
//#endregion
|
|
423
|
+
//#region src/utils/extractStringsFromNodes.ts
|
|
901
424
|
/**
|
|
902
|
-
*
|
|
903
|
-
*
|
|
904
|
-
*
|
|
905
|
-
* and
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
const
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
const pathName = paramNames?.path ?? "pathParams";
|
|
915
|
-
const wrapType = (type) => createParamsType({
|
|
916
|
-
variant: "reference",
|
|
917
|
-
name: typeWrapper ? typeWrapper(type) : type
|
|
918
|
-
});
|
|
919
|
-
const wrapTypeNode = (type) => type.kind === "ParamsType" && type.variant === "reference" ? wrapType(type.name) : type;
|
|
920
|
-
const casedParams = caseParams(node.parameters, paramsCasing);
|
|
921
|
-
const pathParams = casedParams.filter((p) => p.in === "path");
|
|
922
|
-
const queryParams = casedParams.filter((p) => p.in === "query");
|
|
923
|
-
const headerParams = casedParams.filter((p) => p.in === "header");
|
|
924
|
-
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
|
|
925
|
-
const bodyRequired = node.requestBody?.required ?? false;
|
|
926
|
-
const queryGroupType = resolver ? resolveGroupType({
|
|
927
|
-
node,
|
|
928
|
-
params: queryParams,
|
|
929
|
-
groupMethod: resolver.resolveQueryParamsName,
|
|
930
|
-
resolver
|
|
931
|
-
}) : void 0;
|
|
932
|
-
const headerGroupType = resolver ? resolveGroupType({
|
|
933
|
-
node,
|
|
934
|
-
params: headerParams,
|
|
935
|
-
groupMethod: resolver.resolveHeaderParamsName,
|
|
936
|
-
resolver
|
|
937
|
-
}) : void 0;
|
|
938
|
-
const params = [];
|
|
939
|
-
if (paramsType === "object") {
|
|
940
|
-
const children = [
|
|
941
|
-
...pathParams.map((p) => {
|
|
942
|
-
const type = resolveParamsType({
|
|
943
|
-
node,
|
|
944
|
-
param: p,
|
|
945
|
-
resolver
|
|
946
|
-
});
|
|
947
|
-
return createFunctionParameter({
|
|
948
|
-
name: p.name,
|
|
949
|
-
type: wrapTypeNode(type),
|
|
950
|
-
optional: !p.required
|
|
951
|
-
});
|
|
952
|
-
}),
|
|
953
|
-
...bodyType ? [createFunctionParameter({
|
|
954
|
-
name: dataName,
|
|
955
|
-
type: bodyType,
|
|
956
|
-
optional: !bodyRequired
|
|
957
|
-
})] : [],
|
|
958
|
-
...buildGroupParam({
|
|
959
|
-
name: paramsName,
|
|
960
|
-
node,
|
|
961
|
-
params: queryParams,
|
|
962
|
-
groupType: queryGroupType,
|
|
963
|
-
resolver,
|
|
964
|
-
wrapType
|
|
965
|
-
}),
|
|
966
|
-
...buildGroupParam({
|
|
967
|
-
name: headersName,
|
|
968
|
-
node,
|
|
969
|
-
params: headerParams,
|
|
970
|
-
groupType: headerGroupType,
|
|
971
|
-
resolver,
|
|
972
|
-
wrapType
|
|
973
|
-
})
|
|
974
|
-
];
|
|
975
|
-
if (children.length) params.push(createParameterGroup({
|
|
976
|
-
properties: children,
|
|
977
|
-
default: children.every((c) => c.optional) ? "{}" : void 0
|
|
978
|
-
}));
|
|
979
|
-
} else {
|
|
980
|
-
if (pathParams.length) if (pathParamsType === "inlineSpread") {
|
|
981
|
-
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]) ?? void 0;
|
|
982
|
-
params.push(createFunctionParameter({
|
|
983
|
-
name: pathName,
|
|
984
|
-
type: spreadType ? wrapType(spreadType) : void 0,
|
|
985
|
-
rest: true
|
|
986
|
-
}));
|
|
987
|
-
} else {
|
|
988
|
-
const pathChildren = pathParams.map((p) => {
|
|
989
|
-
const type = resolveParamsType({
|
|
990
|
-
node,
|
|
991
|
-
param: p,
|
|
992
|
-
resolver
|
|
993
|
-
});
|
|
994
|
-
return createFunctionParameter({
|
|
995
|
-
name: p.name,
|
|
996
|
-
type: wrapTypeNode(type),
|
|
997
|
-
optional: !p.required
|
|
998
|
-
});
|
|
999
|
-
});
|
|
1000
|
-
params.push(createParameterGroup({
|
|
1001
|
-
properties: pathChildren,
|
|
1002
|
-
inline: pathParamsType === "inline",
|
|
1003
|
-
default: pathParamsDefault ?? (pathChildren.every((c) => c.optional) ? "{}" : void 0)
|
|
1004
|
-
}));
|
|
425
|
+
* Extracts all string content from a `CodeNode` tree recursively.
|
|
426
|
+
*
|
|
427
|
+
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
428
|
+
* and nested node content. Used to build the full source string for import filtering.
|
|
429
|
+
*/
|
|
430
|
+
function extractStringsFromNodes(nodes) {
|
|
431
|
+
if (!nodes?.length) return "";
|
|
432
|
+
const collected = [];
|
|
433
|
+
for (const node of nodes) {
|
|
434
|
+
if (typeof node === "string") {
|
|
435
|
+
if (node) collected.push(node);
|
|
436
|
+
continue;
|
|
1005
437
|
}
|
|
1006
|
-
if (
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
groupType: headerGroupType,
|
|
1024
|
-
resolver,
|
|
1025
|
-
wrapType
|
|
1026
|
-
}));
|
|
438
|
+
if (node.kind === "Text") {
|
|
439
|
+
if (node.value) collected.push(node.value);
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
if (node.kind === "Break") continue;
|
|
443
|
+
if (node.kind === "Jsx") {
|
|
444
|
+
if (node.value) collected.push(node.value);
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
const parts = [];
|
|
448
|
+
if ("params" in node && node.params) parts.push(node.params);
|
|
449
|
+
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
450
|
+
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
451
|
+
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
452
|
+
const nested = extractStringsFromNodes(node.nodes);
|
|
453
|
+
if (nested) parts.push(nested);
|
|
454
|
+
if (parts.length) collected.push(parts.join("\n"));
|
|
1027
455
|
}
|
|
1028
|
-
|
|
1029
|
-
return createFunctionParameters({ params });
|
|
1030
|
-
}
|
|
1031
|
-
/**
|
|
1032
|
-
* Builds a single {@link FunctionParameterNode} for a query or header group.
|
|
1033
|
-
* Returns an empty array when there are no params to emit.
|
|
1034
|
-
*
|
|
1035
|
-
* If a pre-resolved `groupType` is provided it emits `name: GroupType`.
|
|
1036
|
-
* Otherwise, it builds an inline struct from the individual params.
|
|
1037
|
-
*/
|
|
1038
|
-
function buildGroupParam({ name, node, params, groupType, resolver, wrapType }) {
|
|
1039
|
-
if (groupType) return [createFunctionParameter({
|
|
1040
|
-
name,
|
|
1041
|
-
type: groupType.type.kind === "ParamsType" && groupType.type.variant === "reference" ? wrapType(groupType.type.name) : groupType.type,
|
|
1042
|
-
optional: groupType.optional
|
|
1043
|
-
})];
|
|
1044
|
-
if (params.length) return [createFunctionParameter({
|
|
1045
|
-
name,
|
|
1046
|
-
type: toStructType({
|
|
1047
|
-
node,
|
|
1048
|
-
params,
|
|
1049
|
-
resolver
|
|
1050
|
-
}),
|
|
1051
|
-
optional: params.every((p) => !p.required)
|
|
1052
|
-
})];
|
|
1053
|
-
return [];
|
|
1054
|
-
}
|
|
1055
|
-
/**
|
|
1056
|
-
* Derives a {@link ParamGroupType} from the resolver's group method.
|
|
1057
|
-
* Returns `undefined` when the group name equals the individual param name (no real group).
|
|
1058
|
-
*/
|
|
1059
|
-
function resolveGroupType({ node, params, groupMethod, resolver }) {
|
|
1060
|
-
if (!params.length) return;
|
|
1061
|
-
const firstParam = params[0];
|
|
1062
|
-
const groupName = groupMethod.call(resolver, node, firstParam);
|
|
1063
|
-
if (groupName === resolver.resolveParamName(node, firstParam)) return;
|
|
1064
|
-
const allOptional = params.every((p) => !p.required);
|
|
1065
|
-
return {
|
|
1066
|
-
type: createParamsType({
|
|
1067
|
-
variant: "reference",
|
|
1068
|
-
name: groupName
|
|
1069
|
-
}),
|
|
1070
|
-
optional: allOptional
|
|
1071
|
-
};
|
|
1072
|
-
}
|
|
1073
|
-
/**
|
|
1074
|
-
* Builds a {@link TypeNode} with `variant: 'struct'` for an inline anonymous type grouping named fields.
|
|
1075
|
-
*
|
|
1076
|
-
* Used when query or header parameters have no dedicated group type name.
|
|
1077
|
-
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
|
|
1078
|
-
*/
|
|
1079
|
-
function toStructType({ node, params, resolver }) {
|
|
1080
|
-
return createParamsType({
|
|
1081
|
-
variant: "struct",
|
|
1082
|
-
properties: params.map((p) => ({
|
|
1083
|
-
name: p.name,
|
|
1084
|
-
optional: !p.required,
|
|
1085
|
-
type: resolveParamsType({
|
|
1086
|
-
node,
|
|
1087
|
-
param: p,
|
|
1088
|
-
resolver
|
|
1089
|
-
})
|
|
1090
|
-
}))
|
|
1091
|
-
});
|
|
456
|
+
return collected.join("\n");
|
|
1092
457
|
}
|
|
458
|
+
//#endregion
|
|
459
|
+
//#region src/utils/combineFileMembers.ts
|
|
1093
460
|
function sourceKey(source) {
|
|
1094
461
|
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
|
|
1095
462
|
}
|
|
@@ -1104,19 +471,19 @@ function importKey(path, name, isTypeOnly) {
|
|
|
1104
471
|
}
|
|
1105
472
|
/**
|
|
1106
473
|
* Computes a multi-level sort key for exports and imports:
|
|
1107
|
-
* non-array names first (wildcards/namespace aliases)
|
|
474
|
+
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
|
|
1108
475
|
*/
|
|
1109
476
|
function sortKey(node) {
|
|
1110
477
|
const isArray = Array.isArray(node.name) ? "1" : "0";
|
|
1111
478
|
const typeOnly = node.isTypeOnly ? "0" : "1";
|
|
1112
479
|
const hasName = node.name != null ? "1" : "0";
|
|
1113
|
-
const name = Array.isArray(node.name) ?
|
|
480
|
+
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
|
|
1114
481
|
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
|
|
1115
482
|
}
|
|
1116
483
|
/**
|
|
1117
|
-
* Deduplicates
|
|
1118
|
-
*
|
|
1119
|
-
*
|
|
484
|
+
* Deduplicates `SourceNode` objects by `name + isExportable + isTypeOnly`, keeping the first of each
|
|
485
|
+
* key. Unnamed sources fall back to their extracted node strings as the name part of the key. Returns
|
|
486
|
+
* the deduplicated array in original order.
|
|
1120
487
|
*/
|
|
1121
488
|
function combineSources(sources) {
|
|
1122
489
|
const seen = /* @__PURE__ */ new Map();
|
|
@@ -1127,6 +494,16 @@ function combineSources(sources) {
|
|
|
1127
494
|
return [...seen.values()];
|
|
1128
495
|
}
|
|
1129
496
|
/**
|
|
497
|
+
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
|
|
498
|
+
*
|
|
499
|
+
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
|
|
500
|
+
*/
|
|
501
|
+
function mergeNameArrays(existing, incoming) {
|
|
502
|
+
const merged = new Set(existing);
|
|
503
|
+
for (const name of incoming) merged.add(name);
|
|
504
|
+
return [...merged];
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
1130
507
|
* Deduplicates and merges `ExportNode` objects by path and type.
|
|
1131
508
|
*
|
|
1132
509
|
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
|
|
@@ -1147,11 +524,8 @@ function combineExports(exports) {
|
|
|
1147
524
|
if (!name.length) continue;
|
|
1148
525
|
const key = pathTypeKey(path, isTypeOnly);
|
|
1149
526
|
const existing = namedByPath.get(key);
|
|
1150
|
-
if (existing && Array.isArray(existing.name))
|
|
1151
|
-
|
|
1152
|
-
for (const n of name) merged.add(n);
|
|
1153
|
-
existing.name = [...merged];
|
|
1154
|
-
} else {
|
|
527
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
528
|
+
else {
|
|
1155
529
|
const newItem = {
|
|
1156
530
|
...curr,
|
|
1157
531
|
name: [...new Set(name)]
|
|
@@ -1174,12 +548,22 @@ function combineExports(exports) {
|
|
|
1174
548
|
*
|
|
1175
549
|
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
|
|
1176
550
|
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
|
|
1177
|
-
*
|
|
1178
|
-
* @note Use this when combining imports from multiple files to avoid duplicate declarations.
|
|
1179
551
|
*/
|
|
1180
552
|
function combineImports(imports, exports, source) {
|
|
1181
553
|
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
1182
554
|
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
|
|
555
|
+
const importNameMemo = /* @__PURE__ */ new Map();
|
|
556
|
+
const canonicalizeName = (n) => {
|
|
557
|
+
if (typeof n === "string") return n;
|
|
558
|
+
const key = `${n.propertyName}:${n.name ?? ""}`;
|
|
559
|
+
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
|
|
560
|
+
return importNameMemo.get(key);
|
|
561
|
+
};
|
|
562
|
+
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
|
|
563
|
+
for (const node of imports) {
|
|
564
|
+
if (!Array.isArray(node.name)) continue;
|
|
565
|
+
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
|
|
566
|
+
}
|
|
1183
567
|
const result = [];
|
|
1184
568
|
const namedByPath = /* @__PURE__ */ new Map();
|
|
1185
569
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -1193,15 +577,12 @@ function combineImports(imports, exports, source) {
|
|
|
1193
577
|
const { path, isTypeOnly } = curr;
|
|
1194
578
|
let { name } = curr;
|
|
1195
579
|
if (Array.isArray(name)) {
|
|
1196
|
-
name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.propertyName));
|
|
580
|
+
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
|
|
1197
581
|
if (!name.length) continue;
|
|
1198
582
|
const key = pathTypeKey(path, isTypeOnly);
|
|
1199
583
|
const existing = namedByPath.get(key);
|
|
1200
|
-
if (existing && Array.isArray(existing.name))
|
|
1201
|
-
|
|
1202
|
-
for (const n of name) merged.add(n);
|
|
1203
|
-
existing.name = [...merged];
|
|
1204
|
-
} else {
|
|
584
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
585
|
+
else {
|
|
1205
586
|
const newItem = {
|
|
1206
587
|
...curr,
|
|
1207
588
|
name
|
|
@@ -1210,7 +591,7 @@ function combineImports(imports, exports, source) {
|
|
|
1210
591
|
namedByPath.set(key, newItem);
|
|
1211
592
|
}
|
|
1212
593
|
} else {
|
|
1213
|
-
if (name && !isUsed(name)) continue;
|
|
594
|
+
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
|
|
1214
595
|
const key = importKey(path, name, isTypeOnly);
|
|
1215
596
|
if (!seen.has(key)) {
|
|
1216
597
|
result.push(curr);
|
|
@@ -1220,284 +601,266 @@ function combineImports(imports, exports, source) {
|
|
|
1220
601
|
}
|
|
1221
602
|
return result;
|
|
1222
603
|
}
|
|
604
|
+
//#endregion
|
|
605
|
+
//#region src/nodes/file.ts
|
|
1223
606
|
/**
|
|
1224
|
-
*
|
|
1225
|
-
*
|
|
1226
|
-
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
1227
|
-
* and nested node content. Used internally to build the full source string for import filtering.
|
|
607
|
+
* Definition for the {@link ImportNode}.
|
|
1228
608
|
*/
|
|
1229
|
-
|
|
1230
|
-
if (!nodes?.length) return "";
|
|
1231
|
-
return nodes.map((node) => {
|
|
1232
|
-
if (typeof node === "string") return node;
|
|
1233
|
-
if (node.kind === "Text") return node.value;
|
|
1234
|
-
if (node.kind === "Break") return "";
|
|
1235
|
-
if (node.kind === "Jsx") return node.value;
|
|
1236
|
-
const parts = [];
|
|
1237
|
-
if ("params" in node && node.params) parts.push(node.params);
|
|
1238
|
-
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
1239
|
-
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
1240
|
-
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
1241
|
-
const nested = extractStringsFromNodes(node.nodes);
|
|
1242
|
-
if (nested) parts.push(nested);
|
|
1243
|
-
return parts.join("\n");
|
|
1244
|
-
}).filter(Boolean).join("\n");
|
|
1245
|
-
}
|
|
609
|
+
const importDef = defineNode({ kind: "Import" });
|
|
1246
610
|
/**
|
|
1247
|
-
*
|
|
1248
|
-
*
|
|
1249
|
-
* Returns `undefined` for non-ref nodes or when no name can be resolved. Use this to get a schema's
|
|
1250
|
-
* identifier for type definitions or error messages.
|
|
1251
|
-
*
|
|
1252
|
-
* @example
|
|
1253
|
-
* ```ts
|
|
1254
|
-
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
|
|
1255
|
-
* // => 'Pet'
|
|
1256
|
-
* ```
|
|
611
|
+
* Definition for the {@link ExportNode}.
|
|
1257
612
|
*/
|
|
1258
|
-
|
|
1259
|
-
if (!node || node.type !== "ref") return void 0;
|
|
1260
|
-
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? void 0;
|
|
1261
|
-
return node.name ?? node.schema?.name ?? void 0;
|
|
1262
|
-
}
|
|
1263
|
-
/**
|
|
1264
|
-
* Collects every named schema referenced (transitively) from a node via ref edges.
|
|
1265
|
-
*
|
|
1266
|
-
* Refs are followed by name only — the resolved `node.schema` is not traversed inline.
|
|
1267
|
-
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
|
|
1268
|
-
*
|
|
1269
|
-
* @note Returns a Set of schema names for efficient membership testing.
|
|
1270
|
-
*/
|
|
1271
|
-
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
1272
|
-
if (!node) return out;
|
|
1273
|
-
collect(node, { schema(child) {
|
|
1274
|
-
if (child.type === "ref") {
|
|
1275
|
-
const name = resolveRefName(child);
|
|
1276
|
-
if (name) out.add(name);
|
|
1277
|
-
}
|
|
1278
|
-
} });
|
|
1279
|
-
return out;
|
|
1280
|
-
}
|
|
1281
|
-
/**
|
|
1282
|
-
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
1283
|
-
*
|
|
1284
|
-
* Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
|
|
1285
|
-
* in deferred constructs (lazy getter, `z.lazy(() => …)`) to prevent infinite recursion when generated code runs.
|
|
1286
|
-
* Refs are followed by name only, keeping the algorithm linear in the schema graph size.
|
|
1287
|
-
*
|
|
1288
|
-
* @note Call this once on the full schema graph, then use `containsCircularRef()` to check individual schemas.
|
|
1289
|
-
*/
|
|
1290
|
-
function findCircularSchemas(schemas) {
|
|
1291
|
-
const graph = /* @__PURE__ */ new Map();
|
|
1292
|
-
for (const schema of schemas) {
|
|
1293
|
-
if (!schema.name) continue;
|
|
1294
|
-
graph.set(schema.name, collectReferencedSchemaNames(schema));
|
|
1295
|
-
}
|
|
1296
|
-
const circular = /* @__PURE__ */ new Set();
|
|
1297
|
-
for (const start of graph.keys()) {
|
|
1298
|
-
const visited = /* @__PURE__ */ new Set();
|
|
1299
|
-
const stack = [...graph.get(start) ?? []];
|
|
1300
|
-
while (stack.length > 0) {
|
|
1301
|
-
const node = stack.pop();
|
|
1302
|
-
if (node === start) {
|
|
1303
|
-
circular.add(start);
|
|
1304
|
-
break;
|
|
1305
|
-
}
|
|
1306
|
-
if (visited.has(node)) continue;
|
|
1307
|
-
visited.add(node);
|
|
1308
|
-
const next = graph.get(node);
|
|
1309
|
-
if (next) for (const r of next) stack.push(r);
|
|
1310
|
-
}
|
|
1311
|
-
}
|
|
1312
|
-
return circular;
|
|
1313
|
-
}
|
|
613
|
+
const exportDef = defineNode({ kind: "Export" });
|
|
1314
614
|
/**
|
|
1315
|
-
*
|
|
1316
|
-
*
|
|
1317
|
-
* Use `excludeName` to ignore refs to specific schemas (useful when self-references are handled separately).
|
|
1318
|
-
* Commonly used with `findCircularSchemas()` to detect where lazy wrappers are needed in code generation.
|
|
1319
|
-
*
|
|
1320
|
-
* @note Returns `true` for the first matching circular ref found; use for fast dependency checks.
|
|
615
|
+
* Definition for the {@link SourceNode}.
|
|
1321
616
|
*/
|
|
1322
|
-
|
|
1323
|
-
if (!node || circularSchemas.size === 0) return false;
|
|
1324
|
-
return collect(node, { schema(child) {
|
|
1325
|
-
if (child.type !== "ref") return void 0;
|
|
1326
|
-
const name = resolveRefName(child);
|
|
1327
|
-
return name && name !== excludeName && circularSchemas.has(name) ? true : void 0;
|
|
1328
|
-
} }).length > 0;
|
|
1329
|
-
}
|
|
1330
|
-
//#endregion
|
|
1331
|
-
//#region src/factory.ts
|
|
617
|
+
const sourceDef = defineNode({ kind: "Source" });
|
|
1332
618
|
/**
|
|
1333
|
-
*
|
|
1334
|
-
*
|
|
1335
|
-
* - `optional` is set for non-required, non-nullable schemas.
|
|
1336
|
-
* - `nullish` is set for non-required, nullable schemas.
|
|
619
|
+
* Definition for the {@link FileNode}. The fully resolved builder lives in
|
|
620
|
+
* `createFile`, so this definition only supplies the guard.
|
|
1337
621
|
*/
|
|
1338
|
-
|
|
1339
|
-
const nullable = schema.nullable ?? false;
|
|
1340
|
-
return {
|
|
1341
|
-
...schema,
|
|
1342
|
-
optional: !required && !nullable ? true : void 0,
|
|
1343
|
-
nullish: !required && nullable ? true : void 0
|
|
1344
|
-
};
|
|
1345
|
-
}
|
|
622
|
+
const fileDef = defineNode({ kind: "File" });
|
|
1346
623
|
/**
|
|
1347
|
-
* Creates an `
|
|
1348
|
-
*
|
|
1349
|
-
* @example
|
|
1350
|
-
* ```ts
|
|
1351
|
-
* const input = createInput()
|
|
1352
|
-
* // { kind: 'Input', schemas: [], operations: [] }
|
|
1353
|
-
* ```
|
|
624
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
1354
625
|
*
|
|
1355
|
-
* @example
|
|
626
|
+
* @example Named import
|
|
1356
627
|
* ```ts
|
|
1357
|
-
*
|
|
1358
|
-
* //
|
|
628
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
629
|
+
* // import { useState } from 'react'
|
|
1359
630
|
* ```
|
|
1360
631
|
*/
|
|
1361
|
-
|
|
1362
|
-
return {
|
|
1363
|
-
schemas: [],
|
|
1364
|
-
operations: [],
|
|
1365
|
-
...overrides,
|
|
1366
|
-
kind: "Input"
|
|
1367
|
-
};
|
|
1368
|
-
}
|
|
632
|
+
const createImport = importDef.create;
|
|
1369
633
|
/**
|
|
1370
|
-
* Creates an `
|
|
634
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
1371
635
|
*
|
|
1372
|
-
* @example
|
|
636
|
+
* @example Named export
|
|
1373
637
|
* ```ts
|
|
1374
|
-
*
|
|
1375
|
-
* // {
|
|
638
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
639
|
+
* // export { Pet } from './Pet'
|
|
1376
640
|
* ```
|
|
641
|
+
*/
|
|
642
|
+
const createExport = exportDef.create;
|
|
643
|
+
/**
|
|
644
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
1377
645
|
*
|
|
1378
646
|
* @example
|
|
1379
647
|
* ```ts
|
|
1380
|
-
*
|
|
648
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
1381
649
|
* ```
|
|
1382
650
|
*/
|
|
1383
|
-
|
|
1384
|
-
return {
|
|
1385
|
-
files: [],
|
|
1386
|
-
...overrides,
|
|
1387
|
-
kind: "Output"
|
|
1388
|
-
};
|
|
1389
|
-
}
|
|
651
|
+
const createSource = sourceDef.create;
|
|
1390
652
|
/**
|
|
1391
|
-
* Creates
|
|
653
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
654
|
+
*
|
|
655
|
+
* Computes:
|
|
656
|
+
* - `id` SHA256 hash of the file path
|
|
657
|
+
* - `name` `baseName` without extension
|
|
658
|
+
* - `extname` extension extracted from `baseName`
|
|
659
|
+
*
|
|
660
|
+
* Deduplicates:
|
|
661
|
+
* - `sources` via `combineSources`
|
|
662
|
+
* - `exports` via `combineExports`
|
|
663
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
664
|
+
*
|
|
665
|
+
* @throws {Error} when `baseName` has no extension.
|
|
1392
666
|
*
|
|
1393
667
|
* @example
|
|
1394
668
|
* ```ts
|
|
1395
|
-
* const
|
|
1396
|
-
*
|
|
1397
|
-
*
|
|
1398
|
-
*
|
|
669
|
+
* const file = createFile({
|
|
670
|
+
* baseName: 'petStore.ts',
|
|
671
|
+
* path: 'src/models/petStore.ts',
|
|
672
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
673
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
674
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
1399
675
|
* })
|
|
1400
|
-
* //
|
|
676
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
677
|
+
* // file.name = 'petStore'
|
|
678
|
+
* // file.extname = '.ts'
|
|
1401
679
|
* ```
|
|
1402
680
|
*
|
|
1403
|
-
* @example
|
|
681
|
+
* @example Copy a real file into the output verbatim
|
|
1404
682
|
* ```ts
|
|
1405
|
-
* const
|
|
1406
|
-
*
|
|
1407
|
-
*
|
|
1408
|
-
*
|
|
1409
|
-
* tags: ['pet'],
|
|
683
|
+
* const file = createFile({
|
|
684
|
+
* baseName: 'client.ts',
|
|
685
|
+
* path: 'src/gen/client.ts',
|
|
686
|
+
* copy: '/abs/path/to/templates/client.ts',
|
|
1410
687
|
* })
|
|
1411
688
|
* ```
|
|
1412
689
|
*/
|
|
1413
|
-
function
|
|
690
|
+
function createFile(input) {
|
|
691
|
+
const extname = node_path.default.extname(input.baseName);
|
|
692
|
+
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
693
|
+
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
694
|
+
const resolvedImports = (() => {
|
|
695
|
+
if (!input.imports?.length) return [];
|
|
696
|
+
const sourceParts = [];
|
|
697
|
+
const localNames = /* @__PURE__ */ new Set();
|
|
698
|
+
for (const item of input.sources ?? []) {
|
|
699
|
+
const extracted = item.nodes && extractStringsFromNodes(item.nodes);
|
|
700
|
+
if (extracted) sourceParts.push(extracted);
|
|
701
|
+
if (item.name) localNames.add(item.name);
|
|
702
|
+
}
|
|
703
|
+
const source = sourceParts.join("\n") || void 0;
|
|
704
|
+
const combinedImports = combineImports(input.imports, resolvedExports, source);
|
|
705
|
+
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
|
|
706
|
+
return combinedImports.flatMap((imp) => {
|
|
707
|
+
if (imp.path === input.path) return [];
|
|
708
|
+
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
|
|
709
|
+
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
|
|
710
|
+
if (!kept.length) return [];
|
|
711
|
+
return [kept.length === imp.name.length ? imp : {
|
|
712
|
+
...imp,
|
|
713
|
+
name: kept
|
|
714
|
+
}];
|
|
715
|
+
});
|
|
716
|
+
})();
|
|
717
|
+
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
1414
718
|
return {
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
719
|
+
kind: "File",
|
|
720
|
+
...input,
|
|
721
|
+
id: (0, node_crypto.hash)("sha256", input.path, "hex"),
|
|
722
|
+
name: trimExtName(input.baseName),
|
|
723
|
+
extname,
|
|
724
|
+
imports: resolvedImports,
|
|
725
|
+
exports: resolvedExports,
|
|
726
|
+
sources: resolvedSources,
|
|
727
|
+
meta: input.meta ?? {}
|
|
1420
728
|
};
|
|
1421
729
|
}
|
|
730
|
+
//#endregion
|
|
731
|
+
//#region src/nodes/input.ts
|
|
1422
732
|
/**
|
|
1423
|
-
*
|
|
1424
|
-
* Primitive types map to themselves; special string formats map to `'string'`.
|
|
1425
|
-
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
|
|
733
|
+
* Definition for the {@link InputNode}.
|
|
1426
734
|
*/
|
|
1427
|
-
const
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
date: "date",
|
|
1441
|
-
uuid: "string",
|
|
1442
|
-
email: "string",
|
|
1443
|
-
url: "string",
|
|
1444
|
-
datetime: "string",
|
|
1445
|
-
time: "string"
|
|
1446
|
-
};
|
|
1447
|
-
function createSchema(props) {
|
|
1448
|
-
const inferredPrimitive = TYPE_TO_PRIMITIVE[props.type];
|
|
1449
|
-
if (props["type"] === "object") return {
|
|
1450
|
-
properties: [],
|
|
1451
|
-
primitive: "object",
|
|
1452
|
-
...props,
|
|
1453
|
-
kind: "Schema"
|
|
1454
|
-
};
|
|
1455
|
-
return {
|
|
1456
|
-
primitive: inferredPrimitive,
|
|
1457
|
-
...props,
|
|
1458
|
-
kind: "Schema"
|
|
1459
|
-
};
|
|
1460
|
-
}
|
|
735
|
+
const inputDef = defineNode({
|
|
736
|
+
kind: "Input",
|
|
737
|
+
defaults: {
|
|
738
|
+
schemas: [],
|
|
739
|
+
operations: [],
|
|
740
|
+
meta: {
|
|
741
|
+
circularNames: [],
|
|
742
|
+
enumNames: []
|
|
743
|
+
}
|
|
744
|
+
},
|
|
745
|
+
children: ["schemas", "operations"],
|
|
746
|
+
visitorKey: "input"
|
|
747
|
+
});
|
|
1461
748
|
/**
|
|
1462
|
-
* Creates
|
|
1463
|
-
*
|
|
1464
|
-
* `required` defaults to `false`.
|
|
1465
|
-
* `schema.optional` and `schema.nullish` are derived from `required` and `schema.nullable`.
|
|
749
|
+
* Creates an `InputNode`, defaulting `schemas`/`operations` to empty arrays and `meta` per
|
|
750
|
+
* {@link inputDef}.
|
|
1466
751
|
*
|
|
1467
752
|
* @example
|
|
1468
753
|
* ```ts
|
|
1469
|
-
* const
|
|
1470
|
-
*
|
|
1471
|
-
* schema: createSchema({ type: 'string' }),
|
|
1472
|
-
* })
|
|
1473
|
-
* // required=false, schema.optional=true
|
|
754
|
+
* const input = createInput()
|
|
755
|
+
* // { kind: 'Input', schemas: [], operations: [] }
|
|
1474
756
|
* ```
|
|
757
|
+
*/
|
|
758
|
+
function createInput(overrides = {}) {
|
|
759
|
+
return inputDef.create(overrides);
|
|
760
|
+
}
|
|
761
|
+
//#endregion
|
|
762
|
+
//#region src/nodes/requestBody.ts
|
|
763
|
+
/**
|
|
764
|
+
* Definition for the {@link RequestBodyNode}. Content entries are built upfront with
|
|
765
|
+
* {@link createContent}, mirroring how `parameters` and `responses` take prebuilt nodes.
|
|
766
|
+
*/
|
|
767
|
+
const requestBodyDef = defineNode({
|
|
768
|
+
kind: "RequestBody",
|
|
769
|
+
children: ["content"]
|
|
770
|
+
});
|
|
771
|
+
/**
|
|
772
|
+
* Creates a `RequestBodyNode`.
|
|
773
|
+
*/
|
|
774
|
+
const createRequestBody = requestBodyDef.create;
|
|
775
|
+
//#endregion
|
|
776
|
+
//#region src/nodes/operation.ts
|
|
777
|
+
/**
|
|
778
|
+
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
|
|
779
|
+
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
|
|
780
|
+
* normalized into a `RequestBodyNode`.
|
|
781
|
+
*/
|
|
782
|
+
const operationDef = defineNode({
|
|
783
|
+
kind: "Operation",
|
|
784
|
+
build: (props) => {
|
|
785
|
+
const { requestBody, ...rest } = props;
|
|
786
|
+
const isHttp = rest.method !== void 0 && rest.path !== void 0;
|
|
787
|
+
return {
|
|
788
|
+
tags: [],
|
|
789
|
+
parameters: [],
|
|
790
|
+
responses: [],
|
|
791
|
+
...rest,
|
|
792
|
+
...isHttp ? { protocol: "http" } : {},
|
|
793
|
+
requestBody: requestBody ? createRequestBody(requestBody) : void 0
|
|
794
|
+
};
|
|
795
|
+
},
|
|
796
|
+
children: [
|
|
797
|
+
"parameters",
|
|
798
|
+
"requestBody",
|
|
799
|
+
"responses"
|
|
800
|
+
],
|
|
801
|
+
visitorKey: "operation"
|
|
802
|
+
});
|
|
803
|
+
function createOperation(props) {
|
|
804
|
+
return operationDef.create(props);
|
|
805
|
+
}
|
|
806
|
+
//#endregion
|
|
807
|
+
//#region src/nodes/output.ts
|
|
808
|
+
/**
|
|
809
|
+
* Definition for the {@link OutputNode}.
|
|
810
|
+
*/
|
|
811
|
+
const outputDef = defineNode({
|
|
812
|
+
kind: "Output",
|
|
813
|
+
defaults: { files: [] },
|
|
814
|
+
visitorKey: "output"
|
|
815
|
+
});
|
|
816
|
+
/**
|
|
817
|
+
* Creates an `OutputNode` with a stable default for `files`.
|
|
1475
818
|
*
|
|
1476
819
|
* @example
|
|
1477
820
|
* ```ts
|
|
1478
|
-
* const
|
|
1479
|
-
*
|
|
1480
|
-
* required: true,
|
|
1481
|
-
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1482
|
-
* })
|
|
1483
|
-
* // required=true, no optional/nullish
|
|
821
|
+
* const output = createOutput()
|
|
822
|
+
* // { kind: 'Output', files: [] }
|
|
1484
823
|
* ```
|
|
1485
824
|
*/
|
|
1486
|
-
function
|
|
1487
|
-
|
|
825
|
+
function createOutput(overrides = {}) {
|
|
826
|
+
return outputDef.create(overrides);
|
|
827
|
+
}
|
|
828
|
+
//#endregion
|
|
829
|
+
//#region src/optionality.ts
|
|
830
|
+
/**
|
|
831
|
+
* Generic JSON Schema optionality: a non-required field is optional, and a
|
|
832
|
+
* non-required nullable field is nullish.
|
|
833
|
+
*/
|
|
834
|
+
function optionality(schema, required) {
|
|
835
|
+
const nullable = schema.nullable ?? false;
|
|
1488
836
|
return {
|
|
1489
|
-
...
|
|
1490
|
-
|
|
1491
|
-
required
|
|
1492
|
-
schema: syncOptionality(props.schema, required)
|
|
837
|
+
...schema,
|
|
838
|
+
optional: !required && !nullable ? true : void 0,
|
|
839
|
+
nullish: !required && nullable ? true : void 0
|
|
1493
840
|
};
|
|
1494
841
|
}
|
|
842
|
+
//#endregion
|
|
843
|
+
//#region src/nodes/parameter.ts
|
|
844
|
+
/**
|
|
845
|
+
* Definition for the {@link ParameterNode}. `required` defaults to `false`, and the schema's
|
|
846
|
+
* `optional`/`nullish` flags are derived from it through {@link optionality}.
|
|
847
|
+
*/
|
|
848
|
+
const parameterDef = defineNode({
|
|
849
|
+
kind: "Parameter",
|
|
850
|
+
build: (props) => {
|
|
851
|
+
const required = props.required ?? false;
|
|
852
|
+
return {
|
|
853
|
+
...props,
|
|
854
|
+
required,
|
|
855
|
+
schema: optionality(props.schema, required)
|
|
856
|
+
};
|
|
857
|
+
},
|
|
858
|
+
children: ["schema"],
|
|
859
|
+
visitorKey: "parameter"
|
|
860
|
+
});
|
|
1495
861
|
/**
|
|
1496
862
|
* Creates a `ParameterNode`.
|
|
1497
863
|
*
|
|
1498
|
-
* `required` defaults to `false`.
|
|
1499
|
-
* Nested schema flags are set from `required` and `schema.nullable`.
|
|
1500
|
-
*
|
|
1501
864
|
* @example
|
|
1502
865
|
* ```ts
|
|
1503
866
|
* const param = createParameter({
|
|
@@ -1507,26 +870,64 @@ function createProperty(props) {
|
|
|
1507
870
|
* schema: createSchema({ type: 'string' }),
|
|
1508
871
|
* })
|
|
1509
872
|
* ```
|
|
873
|
+
*/
|
|
874
|
+
const createParameter = parameterDef.create;
|
|
875
|
+
//#endregion
|
|
876
|
+
//#region src/nodes/property.ts
|
|
877
|
+
/**
|
|
878
|
+
* Definition for the {@link PropertyNode}. `required` defaults to `false`, and the schema's
|
|
879
|
+
* `optional`/`nullish` flags are derived from it through {@link optionality}.
|
|
880
|
+
*/
|
|
881
|
+
const propertyDef = defineNode({
|
|
882
|
+
kind: "Property",
|
|
883
|
+
build: (props) => {
|
|
884
|
+
const required = props.required ?? false;
|
|
885
|
+
return {
|
|
886
|
+
...props,
|
|
887
|
+
required,
|
|
888
|
+
schema: optionality(props.schema, required)
|
|
889
|
+
};
|
|
890
|
+
},
|
|
891
|
+
children: ["schema"],
|
|
892
|
+
visitorKey: "property"
|
|
893
|
+
});
|
|
894
|
+
/**
|
|
895
|
+
* Creates a `PropertyNode`.
|
|
1510
896
|
*
|
|
1511
897
|
* @example
|
|
1512
898
|
* ```ts
|
|
1513
|
-
* const
|
|
899
|
+
* const property = createProperty({
|
|
1514
900
|
* name: 'status',
|
|
1515
|
-
*
|
|
901
|
+
* required: true,
|
|
1516
902
|
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1517
903
|
* })
|
|
1518
|
-
* // required=
|
|
904
|
+
* // required=true, no optional/nullish
|
|
1519
905
|
* ```
|
|
1520
906
|
*/
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
907
|
+
const createProperty = propertyDef.create;
|
|
908
|
+
//#endregion
|
|
909
|
+
//#region src/nodes/response.ts
|
|
910
|
+
/**
|
|
911
|
+
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
|
|
912
|
+
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
|
|
913
|
+
*/
|
|
914
|
+
const responseDef = defineNode({
|
|
915
|
+
kind: "Response",
|
|
916
|
+
build: (props) => {
|
|
917
|
+
const { schema, mediaType, keysToOmit, content, ...rest } = props;
|
|
918
|
+
const entries = content ?? (schema ? [createContent({
|
|
919
|
+
contentType: mediaType ?? "application/json",
|
|
920
|
+
schema,
|
|
921
|
+
keysToOmit: keysToOmit ?? null
|
|
922
|
+
})] : void 0);
|
|
923
|
+
return {
|
|
924
|
+
...rest,
|
|
925
|
+
content: entries
|
|
926
|
+
};
|
|
927
|
+
},
|
|
928
|
+
children: ["content"],
|
|
929
|
+
visitorKey: "response"
|
|
930
|
+
});
|
|
1530
931
|
/**
|
|
1531
932
|
* Creates a `ResponseNode`.
|
|
1532
933
|
*
|
|
@@ -1534,684 +935,689 @@ function createParameter(props) {
|
|
|
1534
935
|
* ```ts
|
|
1535
936
|
* const response = createResponse({
|
|
1536
937
|
* statusCode: '200',
|
|
1537
|
-
*
|
|
1538
|
-
* schema: createSchema({ type: 'object', properties: [] }),
|
|
938
|
+
* content: [createContent({ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) })],
|
|
1539
939
|
* })
|
|
1540
940
|
* ```
|
|
1541
941
|
*/
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
kind: "Response"
|
|
1546
|
-
};
|
|
1547
|
-
}
|
|
942
|
+
const createResponse = responseDef.create;
|
|
943
|
+
//#endregion
|
|
944
|
+
//#region src/nodes/schema.ts
|
|
1548
945
|
/**
|
|
1549
|
-
*
|
|
1550
|
-
*
|
|
1551
|
-
* `
|
|
1552
|
-
*
|
|
1553
|
-
* @example Required typed param
|
|
1554
|
-
* ```ts
|
|
1555
|
-
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }) })
|
|
1556
|
-
* // → petId: string
|
|
1557
|
-
* ```
|
|
1558
|
-
*
|
|
1559
|
-
* @example Optional param
|
|
1560
|
-
* ```ts
|
|
1561
|
-
* createFunctionParameter({ name: 'params', type: createParamsType({ variant: 'reference', name: 'QueryParams' }), optional: true })
|
|
1562
|
-
* // → params?: QueryParams
|
|
1563
|
-
* ```
|
|
1564
|
-
*
|
|
1565
|
-
* @example Param with default (implicitly optional; cannot combine with `optional: true`)
|
|
1566
|
-
* ```ts
|
|
1567
|
-
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), default: '{}' })
|
|
1568
|
-
* // → config: RequestConfig = {}
|
|
1569
|
-
* ```
|
|
946
|
+
* Maps schema `type` to its underlying `primitive`.
|
|
947
|
+
* Primitive types map to themselves and special string formats map to `'string'`.
|
|
948
|
+
* Any type not listed here (such as `ref`, `enum`, `union`, `intersection`, `tuple`, `ipv4`, `ipv6`, `blob`) has no `primitive`.
|
|
1570
949
|
*/
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
950
|
+
const TYPE_TO_PRIMITIVE = {
|
|
951
|
+
string: "string",
|
|
952
|
+
number: "number",
|
|
953
|
+
integer: "integer",
|
|
954
|
+
bigint: "bigint",
|
|
955
|
+
boolean: "boolean",
|
|
956
|
+
null: "null",
|
|
957
|
+
any: "any",
|
|
958
|
+
unknown: "unknown",
|
|
959
|
+
void: "void",
|
|
960
|
+
never: "never",
|
|
961
|
+
object: "object",
|
|
962
|
+
array: "array",
|
|
963
|
+
date: "date",
|
|
964
|
+
uuid: "string",
|
|
965
|
+
email: "string",
|
|
966
|
+
url: "string",
|
|
967
|
+
datetime: "string",
|
|
968
|
+
time: "string"
|
|
969
|
+
};
|
|
1578
970
|
/**
|
|
1579
|
-
*
|
|
1580
|
-
*
|
|
1581
|
-
* Use `variant: 'struct'` for inline anonymous types and `variant: 'member'` for a single
|
|
1582
|
-
* named field accessed from a group type. Each language's printer renders the variant
|
|
1583
|
-
* into its own syntax (TypeScript, Python, C#, Kotlin, …).
|
|
1584
|
-
*
|
|
1585
|
-
* @example Reference type (TypeScript: `QueryParams`)
|
|
1586
|
-
* ```ts
|
|
1587
|
-
* createParamsType({ variant: 'reference', name: 'QueryParams' })
|
|
1588
|
-
* ```
|
|
1589
|
-
*
|
|
1590
|
-
* @example Struct type (TypeScript: `{ petId: string }`)
|
|
1591
|
-
* ```ts
|
|
1592
|
-
* createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
|
|
1593
|
-
* ```
|
|
1594
|
-
*
|
|
1595
|
-
* @example Member type (TypeScript: `DeletePetPathParams['petId']`)
|
|
1596
|
-
* ```ts
|
|
1597
|
-
* createParamsType({ variant: 'member', base: 'DeletePetPathParams', key: 'petId' })
|
|
1598
|
-
* ```
|
|
971
|
+
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
|
|
972
|
+
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
|
|
1599
973
|
*/
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
974
|
+
const schemaDef = defineNode({
|
|
975
|
+
kind: "Schema",
|
|
976
|
+
build: (props) => {
|
|
977
|
+
if (props.type === "object") return {
|
|
978
|
+
properties: [],
|
|
979
|
+
primitive: "object",
|
|
980
|
+
...props
|
|
981
|
+
};
|
|
982
|
+
return {
|
|
983
|
+
primitive: TYPE_TO_PRIMITIVE[props.type],
|
|
984
|
+
...props
|
|
985
|
+
};
|
|
986
|
+
},
|
|
987
|
+
children: [
|
|
988
|
+
"properties",
|
|
989
|
+
"items",
|
|
990
|
+
"members",
|
|
991
|
+
"additionalProperties"
|
|
992
|
+
],
|
|
993
|
+
visitorKey: "schema"
|
|
994
|
+
});
|
|
995
|
+
function createSchema(props) {
|
|
996
|
+
return schemaDef.create(props);
|
|
1605
997
|
}
|
|
998
|
+
//#endregion
|
|
999
|
+
//#region src/registry.ts
|
|
1000
|
+
/**
|
|
1001
|
+
* Every node definition. Adding a node means adding its `defineNode` to one
|
|
1002
|
+
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
|
|
1003
|
+
*/
|
|
1004
|
+
const nodeDefs = [
|
|
1005
|
+
inputDef,
|
|
1006
|
+
outputDef,
|
|
1007
|
+
operationDef,
|
|
1008
|
+
requestBodyDef,
|
|
1009
|
+
contentDef,
|
|
1010
|
+
responseDef,
|
|
1011
|
+
schemaDef,
|
|
1012
|
+
propertyDef,
|
|
1013
|
+
parameterDef,
|
|
1014
|
+
constDef,
|
|
1015
|
+
typeDef,
|
|
1016
|
+
functionDef,
|
|
1017
|
+
arrowFunctionDef,
|
|
1018
|
+
textDef,
|
|
1019
|
+
breakDef,
|
|
1020
|
+
jsxDef,
|
|
1021
|
+
importDef,
|
|
1022
|
+
exportDef,
|
|
1023
|
+
sourceDef,
|
|
1024
|
+
fileDef
|
|
1025
|
+
];
|
|
1026
|
+
//#endregion
|
|
1027
|
+
//#region src/visitor.ts
|
|
1606
1028
|
/**
|
|
1607
|
-
*
|
|
1608
|
-
*
|
|
1609
|
-
* @example Grouped param (TypeScript declaration)
|
|
1610
|
-
* ```ts
|
|
1611
|
-
* createParameterGroup({
|
|
1612
|
-
* properties: [
|
|
1613
|
-
* createFunctionParameter({ name: 'id', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
1614
|
-
* createFunctionParameter({ name: 'name', type: createParamsType({ variant: 'reference', name: 'string' }), optional: true }),
|
|
1615
|
-
* ],
|
|
1616
|
-
* default: '{}',
|
|
1617
|
-
* })
|
|
1618
|
-
* // declaration → { id, name? }: { id: string; name?: string } = {}
|
|
1619
|
-
* // call → { id, name }
|
|
1620
|
-
* ```
|
|
1621
|
-
*
|
|
1622
|
-
* @example Inline (spread) — children emitted as individual top-level parameters
|
|
1623
|
-
* ```ts
|
|
1624
|
-
* createParameterGroup({
|
|
1625
|
-
* properties: [createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false })],
|
|
1626
|
-
* inline: true,
|
|
1627
|
-
* })
|
|
1628
|
-
* // declaration → petId: string
|
|
1629
|
-
* // call → petId
|
|
1630
|
-
* ```
|
|
1029
|
+
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
|
|
1030
|
+
* Derived from each definition's `children`.
|
|
1631
1031
|
*/
|
|
1632
|
-
|
|
1633
|
-
return {
|
|
1634
|
-
...props,
|
|
1635
|
-
kind: "ParameterGroup"
|
|
1636
|
-
};
|
|
1637
|
-
}
|
|
1032
|
+
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
|
|
1638
1033
|
/**
|
|
1639
|
-
*
|
|
1640
|
-
*
|
|
1641
|
-
* @example
|
|
1642
|
-
* ```ts
|
|
1643
|
-
* createFunctionParameters({
|
|
1644
|
-
* params: [
|
|
1645
|
-
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
1646
|
-
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), optional: false, default: '{}' }),
|
|
1647
|
-
* ],
|
|
1648
|
-
* })
|
|
1649
|
-
* ```
|
|
1650
|
-
*
|
|
1651
|
-
* @example
|
|
1652
|
-
* ```ts
|
|
1653
|
-
* const empty = createFunctionParameters()
|
|
1654
|
-
* // { kind: 'FunctionParameters', params: [] }
|
|
1655
|
-
* ```
|
|
1034
|
+
* Maps a node kind to the matching visitor callback name. Derived from each
|
|
1035
|
+
* definition's `visitorKey`.
|
|
1656
1036
|
*/
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
params: [],
|
|
1660
|
-
...props,
|
|
1661
|
-
kind: "FunctionParameters"
|
|
1662
|
-
};
|
|
1663
|
-
}
|
|
1037
|
+
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
|
|
1038
|
+
const visitorKeysByKind = VISITOR_KEYS;
|
|
1664
1039
|
/**
|
|
1665
|
-
*
|
|
1666
|
-
*
|
|
1667
|
-
* @example Named import
|
|
1668
|
-
* ```ts
|
|
1669
|
-
* createImport({ name: ['useState'], path: 'react' })
|
|
1670
|
-
* // import { useState } from 'react'
|
|
1671
|
-
* ```
|
|
1672
|
-
*
|
|
1673
|
-
* @example Type-only import
|
|
1674
|
-
* ```ts
|
|
1675
|
-
* createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
|
|
1676
|
-
* // import type { FC } from 'react'
|
|
1677
|
-
* ```
|
|
1040
|
+
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
|
|
1678
1041
|
*/
|
|
1679
|
-
function
|
|
1680
|
-
return
|
|
1681
|
-
...props,
|
|
1682
|
-
kind: "Import"
|
|
1683
|
-
};
|
|
1042
|
+
function isNode(value) {
|
|
1043
|
+
return typeof value === "object" && value !== null && typeof value.kind === "string";
|
|
1684
1044
|
}
|
|
1685
1045
|
/**
|
|
1686
|
-
*
|
|
1046
|
+
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
|
|
1687
1047
|
*
|
|
1688
|
-
*
|
|
1689
|
-
* ```ts
|
|
1690
|
-
* createExport({ name: ['Pet'], path: './Pet' })
|
|
1691
|
-
* // export { Pet } from './Pet'
|
|
1692
|
-
* ```
|
|
1048
|
+
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
|
|
1693
1049
|
*
|
|
1694
|
-
* @example
|
|
1050
|
+
* @example
|
|
1695
1051
|
* ```ts
|
|
1696
|
-
*
|
|
1697
|
-
* //
|
|
1052
|
+
* const children = getChildren(operationNode, true)
|
|
1053
|
+
* // returns parameters, the request body, and responses
|
|
1698
1054
|
* ```
|
|
1699
1055
|
*/
|
|
1700
|
-
function
|
|
1701
|
-
return
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1056
|
+
function* getChildren(node, recurse) {
|
|
1057
|
+
if (node.kind === "Schema" && !recurse) return;
|
|
1058
|
+
const keys = visitorKeysByKind[node.kind];
|
|
1059
|
+
if (!keys) return;
|
|
1060
|
+
const record = node;
|
|
1061
|
+
for (const key of keys) {
|
|
1062
|
+
const value = record[key];
|
|
1063
|
+
if (Array.isArray(value)) {
|
|
1064
|
+
for (const item of value) if (isNode(item)) yield item;
|
|
1065
|
+
} else if (isNode(value)) yield value;
|
|
1066
|
+
}
|
|
1705
1067
|
}
|
|
1706
1068
|
/**
|
|
1707
|
-
*
|
|
1069
|
+
* Runs the visitor callback that matches `node.kind` with the traversal
|
|
1070
|
+
* context. The result is a replacement node, a collected value, or `undefined`
|
|
1071
|
+
* when no callback is registered for the kind.
|
|
1708
1072
|
*
|
|
1709
|
-
*
|
|
1710
|
-
*
|
|
1711
|
-
*
|
|
1712
|
-
* ```
|
|
1073
|
+
* Shared by `transform` and `collect` so node-kind dispatch lives in one place.
|
|
1074
|
+
* `TResult` is the caller's expected return: the same node type for `transform`,
|
|
1075
|
+
* the collected value type for `collect`.
|
|
1713
1076
|
*/
|
|
1714
|
-
function
|
|
1077
|
+
function applyVisitor(node, visitor, parent) {
|
|
1078
|
+
const key = VISITOR_KEY_BY_KIND[node.kind];
|
|
1079
|
+
if (!key) return void 0;
|
|
1080
|
+
const fn = visitor[key];
|
|
1081
|
+
return fn?.(node, { parent });
|
|
1082
|
+
}
|
|
1083
|
+
function transform(node, options) {
|
|
1084
|
+
const { depth, parent, ...visitor } = options;
|
|
1085
|
+
return transformNode(node, visitor, (depth ?? visitorDepths.deep) === visitorDepths.deep, parent);
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Visits a single node, then immutably rebuilds its children. Returns the original
|
|
1089
|
+
* reference when neither the visitor nor the child rebuild changed anything, so callers
|
|
1090
|
+
* can detect "nothing changed" by identity and ancestors avoid reallocating.
|
|
1091
|
+
*/
|
|
1092
|
+
function transformNode(node, visitor, recurse, parent) {
|
|
1093
|
+
return transformChildren(applyVisitor(node, visitor, parent) ?? node, visitor, recurse);
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
|
|
1097
|
+
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
|
|
1098
|
+
* `Schema` children are skipped in shallow mode.
|
|
1099
|
+
*/
|
|
1100
|
+
function transformChildren(node, visitor, recurse) {
|
|
1101
|
+
if (node.kind === "Schema" && !recurse) return node;
|
|
1102
|
+
const keys = visitorKeysByKind[node.kind];
|
|
1103
|
+
if (!keys) return node;
|
|
1104
|
+
const record = node;
|
|
1105
|
+
let updates;
|
|
1106
|
+
for (const key of keys) {
|
|
1107
|
+
if (!(key in record)) continue;
|
|
1108
|
+
const value = record[key];
|
|
1109
|
+
if (Array.isArray(value)) {
|
|
1110
|
+
let mapped;
|
|
1111
|
+
for (const [i, item] of value.entries()) {
|
|
1112
|
+
const next = isNode(item) ? transformNode(item, visitor, recurse, node) : item;
|
|
1113
|
+
if (mapped) {
|
|
1114
|
+
mapped.push(next);
|
|
1115
|
+
continue;
|
|
1116
|
+
}
|
|
1117
|
+
if (next !== item) mapped = [...value.slice(0, i), next];
|
|
1118
|
+
}
|
|
1119
|
+
if (mapped) (updates ??= {})[key] = mapped;
|
|
1120
|
+
} else if (isNode(value)) {
|
|
1121
|
+
const next = transformNode(value, visitor, recurse, node);
|
|
1122
|
+
if (next !== value) (updates ??= {})[key] = next;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
if (!updates) return node;
|
|
1715
1126
|
return {
|
|
1716
|
-
...
|
|
1717
|
-
|
|
1127
|
+
...node,
|
|
1128
|
+
...updates
|
|
1718
1129
|
};
|
|
1719
1130
|
}
|
|
1720
1131
|
/**
|
|
1721
|
-
*
|
|
1722
|
-
*
|
|
1723
|
-
* Computes:
|
|
1724
|
-
* - `id` — SHA256 hash of the file path
|
|
1725
|
-
* - `name` — `baseName` without extension
|
|
1726
|
-
* - `extname` — extension extracted from `baseName`
|
|
1727
|
-
*
|
|
1728
|
-
* Deduplicates:
|
|
1729
|
-
* - `sources` via `combineSources`
|
|
1730
|
-
* - `exports` via `combineExports`
|
|
1731
|
-
* - `imports` via `combineImports` (also filters unused imports)
|
|
1732
|
-
*
|
|
1733
|
-
* @throws {Error} when `baseName` has no extension.
|
|
1132
|
+
* Lazy depth-first collection pass. Yields every non-null value returned by
|
|
1133
|
+
* the visitor callbacks. Use `collectSync` for the eager array form.
|
|
1734
1134
|
*
|
|
1735
|
-
* @example
|
|
1135
|
+
* @example Collect every operationId
|
|
1736
1136
|
* ```ts
|
|
1737
|
-
* const
|
|
1738
|
-
*
|
|
1739
|
-
*
|
|
1740
|
-
*
|
|
1741
|
-
*
|
|
1742
|
-
*
|
|
1743
|
-
*
|
|
1744
|
-
*
|
|
1745
|
-
* // file.name = 'petStore'
|
|
1746
|
-
* // file.extname = '.ts'
|
|
1137
|
+
* const ids: string[] = []
|
|
1138
|
+
* for (const id of collect<string>(root, {
|
|
1139
|
+
* operation(node) {
|
|
1140
|
+
* return node.operationId
|
|
1141
|
+
* },
|
|
1142
|
+
* })) {
|
|
1143
|
+
* ids.push(id)
|
|
1144
|
+
* }
|
|
1747
1145
|
* ```
|
|
1748
1146
|
*/
|
|
1749
|
-
function
|
|
1750
|
-
const
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
const
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
kind: "File",
|
|
1758
|
-
...input,
|
|
1759
|
-
id: (0, node_crypto.createHash)("sha256").update(input.path).digest("hex"),
|
|
1760
|
-
name: trimExtName(input.baseName),
|
|
1761
|
-
extname,
|
|
1762
|
-
imports: resolvedImports,
|
|
1763
|
-
exports: resolvedExports,
|
|
1764
|
-
sources: resolvedSources,
|
|
1765
|
-
meta: input.meta ?? {}
|
|
1766
|
-
};
|
|
1147
|
+
function* collect(node, options) {
|
|
1148
|
+
const { depth, parent, ...visitor } = options;
|
|
1149
|
+
yield* collectNode(node, visitor, (depth ?? visitorDepths.deep) === visitorDepths.deep, parent);
|
|
1150
|
+
}
|
|
1151
|
+
function* collectNode(node, visitor, recurse, parent) {
|
|
1152
|
+
const v = applyVisitor(node, visitor, parent);
|
|
1153
|
+
if (v != null) yield v;
|
|
1154
|
+
for (const child of getChildren(node, recurse)) yield* collectNode(child, visitor, recurse, node);
|
|
1767
1155
|
}
|
|
1768
1156
|
/**
|
|
1769
|
-
*
|
|
1770
|
-
*
|
|
1771
|
-
* Mirrors the `Const` component from `@kubb/renderer-jsx`.
|
|
1772
|
-
* The component's `children` are represented as `nodes`.
|
|
1773
|
-
*
|
|
1774
|
-
* @example Simple constant
|
|
1775
|
-
* ```ts
|
|
1776
|
-
* createConst({ name: 'pet' })
|
|
1777
|
-
* // const pet = ...
|
|
1778
|
-
* ```
|
|
1157
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
1158
|
+
* callbacks return into an array.
|
|
1779
1159
|
*
|
|
1780
|
-
* @example
|
|
1781
|
-
* ```ts
|
|
1782
|
-
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
1783
|
-
* // export const pets: Pet[] = ... as const
|
|
1784
|
-
* ```
|
|
1785
|
-
*
|
|
1786
|
-
* @example With JSDoc and child nodes
|
|
1160
|
+
* @example Collect every operationId
|
|
1787
1161
|
* ```ts
|
|
1788
|
-
*
|
|
1789
|
-
*
|
|
1790
|
-
*
|
|
1791
|
-
*
|
|
1792
|
-
* nodes: [],
|
|
1162
|
+
* const ids = collectSync<string>(root, {
|
|
1163
|
+
* operation(node) {
|
|
1164
|
+
* return node.operationId
|
|
1165
|
+
* },
|
|
1793
1166
|
* })
|
|
1794
1167
|
* ```
|
|
1795
1168
|
*/
|
|
1796
|
-
function
|
|
1797
|
-
return
|
|
1798
|
-
...props,
|
|
1799
|
-
kind: "Const"
|
|
1800
|
-
};
|
|
1169
|
+
function collectSync(node, options) {
|
|
1170
|
+
return Array.from(collect(node, options));
|
|
1801
1171
|
}
|
|
1172
|
+
//#endregion
|
|
1173
|
+
//#region src/defineMacro.ts
|
|
1802
1174
|
/**
|
|
1803
|
-
*
|
|
1804
|
-
*
|
|
1805
|
-
* Mirrors the `Type` component from `@kubb/renderer-jsx`.
|
|
1806
|
-
* The component's `children` are represented as `nodes`.
|
|
1807
|
-
*
|
|
1808
|
-
* @example Simple type alias
|
|
1809
|
-
* ```ts
|
|
1810
|
-
* createType({ name: 'Pet' })
|
|
1811
|
-
* // type Pet = ...
|
|
1812
|
-
* ```
|
|
1813
|
-
*
|
|
1814
|
-
* @example Exported type with JSDoc
|
|
1815
|
-
* ```ts
|
|
1816
|
-
* createType({
|
|
1817
|
-
* name: 'PetStatus',
|
|
1818
|
-
* export: true,
|
|
1819
|
-
* JSDoc: { comments: ['@description Status of a pet'] },
|
|
1820
|
-
* })
|
|
1821
|
-
* // export type PetStatus = ...
|
|
1822
|
-
* ```
|
|
1175
|
+
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
|
|
1176
|
+
* list keeps its authored order.
|
|
1823
1177
|
*/
|
|
1824
|
-
function
|
|
1825
|
-
return
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
};
|
|
1178
|
+
function enforceWeight(enforce) {
|
|
1179
|
+
if (enforce === "pre") return 0;
|
|
1180
|
+
if (enforce === "post") return 2;
|
|
1181
|
+
return 1;
|
|
1829
1182
|
}
|
|
1830
1183
|
/**
|
|
1831
|
-
*
|
|
1832
|
-
*
|
|
1833
|
-
* Mirrors the `Function` component from `@kubb/renderer-jsx`.
|
|
1834
|
-
* The component's `children` are represented as `nodes`.
|
|
1835
|
-
*
|
|
1836
|
-
* @example Simple function
|
|
1837
|
-
* ```ts
|
|
1838
|
-
* createFunction({ name: 'getPet' })
|
|
1839
|
-
* // function getPet() { ... }
|
|
1840
|
-
* ```
|
|
1841
|
-
*
|
|
1842
|
-
* @example Exported async function with return type
|
|
1843
|
-
* ```ts
|
|
1844
|
-
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
1845
|
-
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
1846
|
-
* ```
|
|
1184
|
+
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
1185
|
+
* Adds no runtime behavior.
|
|
1847
1186
|
*
|
|
1848
|
-
* @example
|
|
1187
|
+
* @example
|
|
1849
1188
|
* ```ts
|
|
1850
|
-
*
|
|
1851
|
-
* name: '
|
|
1852
|
-
*
|
|
1853
|
-
*
|
|
1854
|
-
*
|
|
1855
|
-
* returnType: 'T',
|
|
1189
|
+
* const macroUntagged = defineMacro({
|
|
1190
|
+
* name: 'untagged',
|
|
1191
|
+
* operation(node) {
|
|
1192
|
+
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
1193
|
+
* },
|
|
1856
1194
|
* })
|
|
1857
|
-
* // export function identity<T>(value: T): T { ... }
|
|
1858
1195
|
* ```
|
|
1859
1196
|
*/
|
|
1860
|
-
function
|
|
1861
|
-
return
|
|
1862
|
-
...props,
|
|
1863
|
-
kind: "Function"
|
|
1864
|
-
};
|
|
1197
|
+
function defineMacro(macro) {
|
|
1198
|
+
return macro;
|
|
1865
1199
|
}
|
|
1866
1200
|
/**
|
|
1867
|
-
*
|
|
1868
|
-
*
|
|
1869
|
-
*
|
|
1870
|
-
* The component's `children` are represented as `nodes`.
|
|
1871
|
-
*
|
|
1872
|
-
* @example Simple arrow function
|
|
1873
|
-
* ```ts
|
|
1874
|
-
* createArrowFunction({ name: 'getPet' })
|
|
1875
|
-
* // const getPet = () => { ... }
|
|
1876
|
-
* ```
|
|
1877
|
-
*
|
|
1878
|
-
* @example Single-line exported arrow function
|
|
1879
|
-
* ```ts
|
|
1880
|
-
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
1881
|
-
* // export const double = (n: number) => ...
|
|
1882
|
-
* ```
|
|
1883
|
-
*
|
|
1884
|
-
* @example Async arrow function with generics
|
|
1885
|
-
* ```ts
|
|
1886
|
-
* createArrowFunction({
|
|
1887
|
-
* name: 'fetchPet',
|
|
1888
|
-
* export: true,
|
|
1889
|
-
* async: true,
|
|
1890
|
-
* generics: ['T'],
|
|
1891
|
-
* params: 'id: string',
|
|
1892
|
-
* returnType: 'T',
|
|
1893
|
-
* })
|
|
1894
|
-
* // export const fetchPet = async <T>(id: string): Promise<T> => { ... }
|
|
1895
|
-
* ```
|
|
1201
|
+
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
|
|
1202
|
+
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
|
|
1203
|
+
* original reference (structural sharing).
|
|
1896
1204
|
*/
|
|
1897
|
-
function
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1205
|
+
function chain({ macros, key, node, context }) {
|
|
1206
|
+
let current = node;
|
|
1207
|
+
for (const macro of macros) {
|
|
1208
|
+
const callback = macro[key];
|
|
1209
|
+
if (!callback) continue;
|
|
1210
|
+
if (macro.when && !macro.when(current)) continue;
|
|
1211
|
+
const next = callback(current, context);
|
|
1212
|
+
if (next != null) current = next;
|
|
1213
|
+
}
|
|
1214
|
+
return current === node ? void 0 : current;
|
|
1902
1215
|
}
|
|
1903
1216
|
/**
|
|
1904
|
-
*
|
|
1905
|
-
*
|
|
1906
|
-
*
|
|
1907
|
-
*
|
|
1217
|
+
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
|
|
1218
|
+
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
|
|
1219
|
+
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
|
|
1220
|
+
* has no names, ordering, or composition.
|
|
1908
1221
|
*
|
|
1909
1222
|
* @example
|
|
1910
1223
|
* ```ts
|
|
1911
|
-
*
|
|
1912
|
-
*
|
|
1224
|
+
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
1225
|
+
* const next = transform(root, visitor)
|
|
1913
1226
|
* ```
|
|
1914
1227
|
*/
|
|
1915
|
-
function
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1228
|
+
function composeMacros(macros) {
|
|
1229
|
+
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
|
|
1230
|
+
const visitor = {};
|
|
1231
|
+
for (const key of visitorKeys) {
|
|
1232
|
+
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
|
|
1233
|
+
const callback = (node, context) => chain({
|
|
1234
|
+
macros: ordered,
|
|
1235
|
+
key,
|
|
1236
|
+
node,
|
|
1237
|
+
context
|
|
1238
|
+
});
|
|
1239
|
+
visitor[key] = callback;
|
|
1240
|
+
}
|
|
1241
|
+
return visitor;
|
|
1920
1242
|
}
|
|
1921
1243
|
/**
|
|
1922
|
-
*
|
|
1923
|
-
*
|
|
1924
|
-
*
|
|
1925
|
-
* when joined with `\n` by `printNodes`, produces a blank line.
|
|
1244
|
+
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
|
|
1245
|
+
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
|
|
1246
|
+
* `depth: 'shallow'` to rewrite the root node only.
|
|
1926
1247
|
*
|
|
1927
1248
|
* @example
|
|
1928
1249
|
* ```ts
|
|
1929
|
-
*
|
|
1930
|
-
* // { kind: 'Break' }
|
|
1250
|
+
* const next = applyMacros(root, [macroIntegerToString])
|
|
1931
1251
|
* ```
|
|
1932
|
-
*/
|
|
1933
|
-
function createBreak() {
|
|
1934
|
-
return { kind: "Break" };
|
|
1935
|
-
}
|
|
1936
|
-
/**
|
|
1937
|
-
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
1938
|
-
*
|
|
1939
|
-
* Use this to embed JSX markup (including fragments `<>…</>`) directly in generated code.
|
|
1940
1252
|
*
|
|
1941
|
-
* @example
|
|
1253
|
+
* @example Apply to the root node only
|
|
1942
1254
|
* ```ts
|
|
1943
|
-
*
|
|
1944
|
-
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
1255
|
+
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
1945
1256
|
* ```
|
|
1946
1257
|
*/
|
|
1947
|
-
function
|
|
1948
|
-
return
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1258
|
+
function applyMacros(root, macros, options) {
|
|
1259
|
+
if (macros.length === 0) return root;
|
|
1260
|
+
return transform(root, {
|
|
1261
|
+
...composeMacros(macros),
|
|
1262
|
+
...options
|
|
1263
|
+
});
|
|
1952
1264
|
}
|
|
1953
1265
|
//#endregion
|
|
1954
|
-
//#region src/
|
|
1266
|
+
//#region src/createPrinter.ts
|
|
1955
1267
|
/**
|
|
1956
|
-
* Creates a schema printer
|
|
1957
|
-
*
|
|
1958
|
-
*
|
|
1268
|
+
* Creates a schema printer: a function that takes a `SchemaNode` and emits
|
|
1269
|
+
* code in your target language. Each plugin that produces code from schemas
|
|
1270
|
+
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
|
|
1271
|
+
* with this helper.
|
|
1959
1272
|
*
|
|
1960
1273
|
* The builder receives resolved options and returns:
|
|
1961
|
-
* - `name` — a unique identifier for the printer
|
|
1962
|
-
* - `options` — options stored on the returned printer instance
|
|
1963
|
-
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
1964
|
-
* - `print` _(optional)_ — top-level override exposed as `printer.print`
|
|
1965
|
-
* - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
|
|
1966
|
-
* - This keeps recursion safe and avoids self-calls
|
|
1967
1274
|
*
|
|
1968
|
-
*
|
|
1275
|
+
* - `name` unique identifier for the printer.
|
|
1276
|
+
* - `options` stored on the returned printer instance.
|
|
1277
|
+
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
|
|
1278
|
+
* output (a string, a TypeScript AST node, ...) for that schema type.
|
|
1279
|
+
* - `overrides` (optional), user-supplied handlers that win over `nodes`.
|
|
1280
|
+
* An override can call `this.base(node)` to reuse the handler it replaced.
|
|
1281
|
+
* - `print` (optional), top-level override exposed as `printer.print`.
|
|
1282
|
+
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
|
|
1283
|
+
*
|
|
1284
|
+
* Without a `print` override, `printer.print` falls back to `printer.transform`
|
|
1285
|
+
* (the node-level dispatcher).
|
|
1969
1286
|
*
|
|
1970
|
-
* @example
|
|
1287
|
+
* @example Tiny Zod printer
|
|
1971
1288
|
* ```ts
|
|
1289
|
+
* import { createPrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
1290
|
+
*
|
|
1972
1291
|
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
1973
1292
|
*
|
|
1974
|
-
* export const zodPrinter =
|
|
1293
|
+
* export const zodPrinter = createPrinter<PrinterZod>((options) => ({
|
|
1975
1294
|
* name: 'zod',
|
|
1976
1295
|
* options: { strict: options.strict ?? true },
|
|
1977
1296
|
* nodes: {
|
|
1978
1297
|
* string: () => 'z.string()',
|
|
1979
1298
|
* object(node) {
|
|
1980
|
-
* const props = node.properties
|
|
1299
|
+
* const props = node.properties
|
|
1300
|
+
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
1301
|
+
* .join(', ')
|
|
1981
1302
|
* return `z.object({ ${props} })`
|
|
1982
1303
|
* },
|
|
1983
1304
|
* },
|
|
1984
1305
|
* }))
|
|
1985
1306
|
* ```
|
|
1986
1307
|
*/
|
|
1987
|
-
function
|
|
1988
|
-
return
|
|
1308
|
+
function createPrinter(build) {
|
|
1309
|
+
return (options) => {
|
|
1310
|
+
const { name, options: resolvedOptions, nodes, overrides, print: printOverride } = build(options ?? {});
|
|
1311
|
+
const merged = overrides ? {
|
|
1312
|
+
...nodes,
|
|
1313
|
+
...overrides
|
|
1314
|
+
} : nodes;
|
|
1315
|
+
const context = {
|
|
1316
|
+
options: resolvedOptions,
|
|
1317
|
+
transform: (node) => {
|
|
1318
|
+
const handler = merged[node.type];
|
|
1319
|
+
if (!handler) return null;
|
|
1320
|
+
return handler.call(context, node);
|
|
1321
|
+
},
|
|
1322
|
+
base: (node) => {
|
|
1323
|
+
const handler = nodes[node.type];
|
|
1324
|
+
if (!handler) return null;
|
|
1325
|
+
return handler.call(context, node);
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
return {
|
|
1329
|
+
name,
|
|
1330
|
+
options: resolvedOptions,
|
|
1331
|
+
transform: context.transform,
|
|
1332
|
+
print: printOverride ? printOverride.bind(context) : context.transform
|
|
1333
|
+
};
|
|
1334
|
+
};
|
|
1989
1335
|
}
|
|
1336
|
+
//#endregion
|
|
1337
|
+
//#region src/utils/refs.ts
|
|
1990
1338
|
/**
|
|
1991
|
-
*
|
|
1992
|
-
|
|
1339
|
+
* Resolves the emitted name of the schema a ref node points at. Prefers `targetName` (set when
|
|
1340
|
+
* the referenced schema was renamed, e.g. to break a collision), then the last segment of `ref`,
|
|
1341
|
+
* then `name`, then the nested `schema.name`.
|
|
1342
|
+
*
|
|
1343
|
+
* Returns `null` for non-ref nodes or when no name resolves.
|
|
1344
|
+
*
|
|
1993
1345
|
* @example
|
|
1994
|
-
*
|
|
1995
|
-
*
|
|
1996
|
-
*
|
|
1997
|
-
* )
|
|
1998
|
-
* ```
|
|
1346
|
+
* `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) // 'Pet'`
|
|
1347
|
+
*
|
|
1348
|
+
* @example Collision-renamed target
|
|
1349
|
+
* `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Order', targetName: 'OrderSchema' }) // 'OrderSchema'`
|
|
1999
1350
|
*/
|
|
2000
|
-
function
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
options: resolvedOptions,
|
|
2006
|
-
transform: (node) => {
|
|
2007
|
-
const key = getKey(node);
|
|
2008
|
-
if (key === void 0) return null;
|
|
2009
|
-
const handler = nodes[key];
|
|
2010
|
-
if (!handler) return null;
|
|
2011
|
-
return handler.call(context, node);
|
|
2012
|
-
}
|
|
2013
|
-
};
|
|
2014
|
-
return {
|
|
2015
|
-
name,
|
|
2016
|
-
options: resolvedOptions,
|
|
2017
|
-
transform: context.transform,
|
|
2018
|
-
print: printOverride ? printOverride.bind(context) : context.transform
|
|
2019
|
-
};
|
|
2020
|
-
};
|
|
2021
|
-
};
|
|
1351
|
+
function resolveRefName(node) {
|
|
1352
|
+
if (!node || node.type !== "ref") return null;
|
|
1353
|
+
if (node.targetName) return node.targetName;
|
|
1354
|
+
if (node.ref) return node.ref.split("/").at(-1) ?? node.ref;
|
|
1355
|
+
return node.name ?? node.schema?.name ?? null;
|
|
2022
1356
|
}
|
|
2023
1357
|
//#endregion
|
|
2024
|
-
//#region src/
|
|
2025
|
-
function findDiscriminator(mapping, ref) {
|
|
2026
|
-
if (!mapping || !ref) return null;
|
|
2027
|
-
return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null;
|
|
2028
|
-
}
|
|
2029
|
-
function childName(parentName, propName) {
|
|
2030
|
-
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
|
|
2031
|
-
}
|
|
2032
|
-
function enumPropName(parentName, propName, enumSuffix) {
|
|
2033
|
-
return pascalCase([
|
|
2034
|
-
parentName,
|
|
2035
|
-
propName,
|
|
2036
|
-
enumSuffix
|
|
2037
|
-
].filter(Boolean).join(" "));
|
|
2038
|
-
}
|
|
1358
|
+
//#region src/utils/schemaGraph.ts
|
|
2039
1359
|
/**
|
|
2040
|
-
*
|
|
1360
|
+
* Memoized inner pass that walks a single node and returns the names of every schema it references.
|
|
2041
1361
|
*/
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
if (
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
return result;
|
|
1362
|
+
const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
|
|
1363
|
+
const refs = /* @__PURE__ */ new Set();
|
|
1364
|
+
collectSync(node, { schema(child) {
|
|
1365
|
+
if (child.type === "ref") {
|
|
1366
|
+
const name = resolveRefName(child);
|
|
1367
|
+
if (name) refs.add(name);
|
|
1368
|
+
}
|
|
2050
1369
|
} });
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
//#region src/transformers.ts
|
|
1370
|
+
return refs;
|
|
1371
|
+
});
|
|
2054
1372
|
/**
|
|
2055
|
-
*
|
|
1373
|
+
* Collects the names of every ref found anywhere inside a node's own subtree.
|
|
2056
1374
|
*
|
|
2057
|
-
*
|
|
2058
|
-
*
|
|
1375
|
+
* Each ref contributes its name only, so the schema it points to is never traversed here. Pass `out`
|
|
1376
|
+
* to accumulate names from several nodes into one set.
|
|
2059
1377
|
*
|
|
2060
|
-
* @example
|
|
1378
|
+
* @example Collect refs from a single schema
|
|
2061
1379
|
* ```ts
|
|
2062
|
-
* const
|
|
2063
|
-
*
|
|
2064
|
-
*
|
|
2065
|
-
*
|
|
2066
|
-
*
|
|
1380
|
+
* const names = collectReferencedSchemaNames(petSchema)
|
|
1381
|
+
* // Set { 'Category', 'Tag' }
|
|
1382
|
+
* ```
|
|
1383
|
+
*
|
|
1384
|
+
* @example Accumulate refs from multiple schemas into one set
|
|
1385
|
+
* ```ts
|
|
1386
|
+
* const out = new Set<string>()
|
|
1387
|
+
* for (const schema of schemas) {
|
|
1388
|
+
* collectReferencedSchemaNames(schema, out)
|
|
1389
|
+
* }
|
|
2067
1390
|
* ```
|
|
2068
1391
|
*/
|
|
2069
|
-
function
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
});
|
|
1392
|
+
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
1393
|
+
if (!node) return out;
|
|
1394
|
+
for (const name of collectSchemaRefs(node)) out.add(name);
|
|
1395
|
+
return out;
|
|
1396
|
+
}
|
|
1397
|
+
function computeUsedSchemaNames(operations, schemas) {
|
|
1398
|
+
const schemaMap = /* @__PURE__ */ new Map();
|
|
1399
|
+
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
|
|
1400
|
+
const result = /* @__PURE__ */ new Set();
|
|
1401
|
+
function visitSchema(schema) {
|
|
1402
|
+
const directRefs = collectReferencedSchemaNames(schema);
|
|
1403
|
+
for (const name of directRefs) if (!result.has(name)) {
|
|
1404
|
+
result.add(name);
|
|
1405
|
+
const namedSchema = schemaMap.get(name);
|
|
1406
|
+
if (namedSchema) visitSchema(namedSchema);
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
for (const op of operations) for (const schema of collect(op, {
|
|
1410
|
+
depth: "shallow",
|
|
1411
|
+
schema: (node) => node
|
|
1412
|
+
})) visitSchema(schema);
|
|
1413
|
+
return result;
|
|
2090
1414
|
}
|
|
2091
1415
|
/**
|
|
2092
|
-
*
|
|
1416
|
+
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
2093
1417
|
*
|
|
2094
|
-
*
|
|
1418
|
+
* An operation uses a schema when its parameters, request body, or responses reference it, directly
|
|
1419
|
+
* or through other named schemas. Once a name is added to the result it is not revisited, so
|
|
1420
|
+
* reference cycles terminate.
|
|
1421
|
+
*
|
|
1422
|
+
* Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.
|
|
1423
|
+
*
|
|
1424
|
+
* @example Only generate schemas referenced by included operations
|
|
2095
1425
|
* ```ts
|
|
2096
|
-
* const
|
|
2097
|
-
*
|
|
2098
|
-
*
|
|
2099
|
-
*
|
|
1426
|
+
* const includedOps = operations.filter((op) => resolver.default.options(op, { options, include }) !== null)
|
|
1427
|
+
* const allowed = collectUsedSchemaNames(includedOps, schemas)
|
|
1428
|
+
*
|
|
1429
|
+
* for (const schema of schemas) {
|
|
1430
|
+
* if (schema.name && !allowed.has(schema.name)) continue
|
|
1431
|
+
* // generate schema
|
|
1432
|
+
* }
|
|
2100
1433
|
* ```
|
|
2101
1434
|
*/
|
|
2102
|
-
function
|
|
2103
|
-
return
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
1435
|
+
function collectUsedSchemaNames(operations, schemas) {
|
|
1436
|
+
return computeUsedSchemaNames(operations, schemas);
|
|
1437
|
+
}
|
|
1438
|
+
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
|
|
1439
|
+
const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
|
|
1440
|
+
const graph = /* @__PURE__ */ new Map();
|
|
1441
|
+
for (const schema of schemas) {
|
|
1442
|
+
if (!schema.name) continue;
|
|
1443
|
+
graph.set(schema.name, collectReferencedSchemaNames(schema));
|
|
1444
|
+
}
|
|
1445
|
+
const circular = /* @__PURE__ */ new Set();
|
|
1446
|
+
for (const start of graph.keys()) {
|
|
1447
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1448
|
+
const stack = [...graph.get(start) ?? []];
|
|
1449
|
+
while (stack.length > 0) {
|
|
1450
|
+
const node = stack.pop();
|
|
1451
|
+
if (node === start) {
|
|
1452
|
+
circular.add(start);
|
|
1453
|
+
break;
|
|
2114
1454
|
}
|
|
1455
|
+
if (visited.has(node)) continue;
|
|
1456
|
+
visited.add(node);
|
|
1457
|
+
const next = graph.get(node);
|
|
1458
|
+
if (next) for (const r of next) stack.push(r);
|
|
2115
1459
|
}
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
}
|
|
1460
|
+
}
|
|
1461
|
+
return circular;
|
|
1462
|
+
});
|
|
2120
1463
|
/**
|
|
2121
|
-
*
|
|
1464
|
+
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
|
|
1465
|
+
*
|
|
1466
|
+
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
|
|
1467
|
+
* the generated code does not recurse forever. Refs are followed by name only, so the walk stays
|
|
1468
|
+
* linear in the size of the schema graph.
|
|
1469
|
+
*
|
|
1470
|
+
* @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.
|
|
1471
|
+
*/
|
|
1472
|
+
function findCircularSchemas(schemas) {
|
|
1473
|
+
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
|
|
1474
|
+
return findCircularSchemasMemo(schemas);
|
|
1475
|
+
}
|
|
1476
|
+
//#endregion
|
|
1477
|
+
//#region src/factory.ts
|
|
1478
|
+
var factory_exports = /* @__PURE__ */ __exportAll({
|
|
1479
|
+
createArrowFunction: () => createArrowFunction,
|
|
1480
|
+
createBreak: () => createBreak,
|
|
1481
|
+
createConst: () => createConst,
|
|
1482
|
+
createContent: () => createContent,
|
|
1483
|
+
createExport: () => createExport,
|
|
1484
|
+
createFile: () => createFile,
|
|
1485
|
+
createFunction: () => createFunction,
|
|
1486
|
+
createImport: () => createImport,
|
|
1487
|
+
createInput: () => createInput,
|
|
1488
|
+
createJsx: () => createJsx,
|
|
1489
|
+
createOperation: () => createOperation,
|
|
1490
|
+
createOutput: () => createOutput,
|
|
1491
|
+
createParameter: () => createParameter,
|
|
1492
|
+
createProperty: () => createProperty,
|
|
1493
|
+
createRequestBody: () => createRequestBody,
|
|
1494
|
+
createResponse: () => createResponse,
|
|
1495
|
+
createSchema: () => createSchema,
|
|
1496
|
+
createSource: () => createSource,
|
|
1497
|
+
createText: () => createText,
|
|
1498
|
+
createType: () => createType,
|
|
1499
|
+
update: () => update
|
|
1500
|
+
});
|
|
1501
|
+
/**
|
|
1502
|
+
* Identity-preserving node update: returns `node` unchanged when every field in
|
|
1503
|
+
* `changes` already equals (by reference) the current value, otherwise a new node
|
|
1504
|
+
* with the changes applied.
|
|
1505
|
+
*
|
|
1506
|
+
* Mirrors the TypeScript compiler's `factory.updateX` contract. Pair it with the
|
|
1507
|
+
* structural sharing in {@link transform} so a no-op rewrite does not allocate and
|
|
1508
|
+
* downstream passes can detect "nothing changed" by identity. Comparison is shallow,
|
|
1509
|
+
* so a structurally equal but newly allocated array or object counts as a change.
|
|
2122
1510
|
*
|
|
2123
1511
|
* @example
|
|
2124
1512
|
* ```ts
|
|
2125
|
-
*
|
|
2126
|
-
*
|
|
2127
|
-
* createSchema({ type: 'string' }),
|
|
2128
|
-
* ])
|
|
2129
|
-
* // keeps only string member
|
|
1513
|
+
* update(node, { name: node.name }) // -> same `node` reference
|
|
1514
|
+
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
|
|
2130
1515
|
* ```
|
|
2131
1516
|
*/
|
|
2132
|
-
function
|
|
2133
|
-
const
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
const enumNode = narrowSchema(member, "enum");
|
|
2137
|
-
if (!enumNode) return true;
|
|
2138
|
-
const primitive = enumNode.primitive;
|
|
2139
|
-
if (!primitive) return true;
|
|
2140
|
-
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
|
|
2141
|
-
if (scalarPrimitives.has(primitive)) return false;
|
|
2142
|
-
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
|
|
2143
|
-
return true;
|
|
2144
|
-
});
|
|
2145
|
-
}
|
|
2146
|
-
function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
2147
|
-
const enumNode = narrowSchema(propNode, "enum");
|
|
2148
|
-
if (enumNode?.primitive === "boolean") return {
|
|
2149
|
-
...propNode,
|
|
2150
|
-
name: void 0
|
|
1517
|
+
function update(node, changes) {
|
|
1518
|
+
for (const key in changes) if (changes[key] !== node[key]) return {
|
|
1519
|
+
...node,
|
|
1520
|
+
...changes
|
|
2151
1521
|
};
|
|
2152
|
-
|
|
2153
|
-
...propNode,
|
|
2154
|
-
name: enumPropName(parentName, propName, enumSuffix)
|
|
2155
|
-
};
|
|
2156
|
-
return propNode;
|
|
1522
|
+
return node;
|
|
2157
1523
|
}
|
|
2158
1524
|
//#endregion
|
|
2159
|
-
exports.
|
|
2160
|
-
|
|
1525
|
+
//#region src/exports.ts
|
|
1526
|
+
var exports_exports = /* @__PURE__ */ __exportAll({
|
|
1527
|
+
applyMacros: () => applyMacros,
|
|
1528
|
+
arrowFunctionDef: () => arrowFunctionDef,
|
|
1529
|
+
breakDef: () => breakDef,
|
|
1530
|
+
collect: () => collect,
|
|
1531
|
+
collectSync: () => collectSync,
|
|
1532
|
+
collectUsedSchemaNames: () => collectUsedSchemaNames,
|
|
1533
|
+
combineExports: () => combineExports,
|
|
1534
|
+
combineImports: () => combineImports,
|
|
1535
|
+
combineSources: () => combineSources,
|
|
1536
|
+
composeMacros: () => composeMacros,
|
|
1537
|
+
constDef: () => constDef,
|
|
1538
|
+
contentDef: () => contentDef,
|
|
1539
|
+
createPrinter: () => createPrinter,
|
|
1540
|
+
defineMacro: () => defineMacro,
|
|
1541
|
+
defineNode: () => defineNode,
|
|
1542
|
+
exportDef: () => exportDef,
|
|
1543
|
+
extractStringsFromNodes: () => extractStringsFromNodes,
|
|
1544
|
+
factory: () => factory_exports,
|
|
1545
|
+
fileDef: () => fileDef,
|
|
1546
|
+
findCircularSchemas: () => findCircularSchemas,
|
|
1547
|
+
functionDef: () => functionDef,
|
|
1548
|
+
importDef: () => importDef,
|
|
1549
|
+
inputDef: () => inputDef,
|
|
1550
|
+
isHttpOperationNode: () => isHttpOperationNode,
|
|
1551
|
+
jsxDef: () => jsxDef,
|
|
1552
|
+
narrowSchema: () => narrowSchema,
|
|
1553
|
+
nodeDefs: () => nodeDefs,
|
|
1554
|
+
operationDef: () => operationDef,
|
|
1555
|
+
optionality: () => optionality,
|
|
1556
|
+
outputDef: () => outputDef,
|
|
1557
|
+
parameterDef: () => parameterDef,
|
|
1558
|
+
propertyDef: () => propertyDef,
|
|
1559
|
+
requestBodyDef: () => requestBodyDef,
|
|
1560
|
+
resolveRefName: () => resolveRefName,
|
|
1561
|
+
responseDef: () => responseDef,
|
|
1562
|
+
schemaDef: () => schemaDef,
|
|
1563
|
+
schemaTypes: () => schemaTypes,
|
|
1564
|
+
sourceDef: () => sourceDef,
|
|
1565
|
+
textDef: () => textDef,
|
|
1566
|
+
transform: () => transform,
|
|
1567
|
+
typeDef: () => typeDef
|
|
1568
|
+
});
|
|
1569
|
+
//#endregion
|
|
1570
|
+
exports.applyMacros = applyMacros;
|
|
1571
|
+
exports.arrowFunctionDef = arrowFunctionDef;
|
|
1572
|
+
Object.defineProperty(exports, "ast", {
|
|
1573
|
+
enumerable: true,
|
|
1574
|
+
get: function() {
|
|
1575
|
+
return exports_exports;
|
|
1576
|
+
}
|
|
1577
|
+
});
|
|
1578
|
+
exports.breakDef = breakDef;
|
|
2161
1579
|
exports.collect = collect;
|
|
2162
|
-
exports.
|
|
2163
|
-
exports.
|
|
2164
|
-
exports.
|
|
2165
|
-
exports.
|
|
2166
|
-
exports.
|
|
2167
|
-
exports.
|
|
2168
|
-
exports.
|
|
2169
|
-
exports.
|
|
2170
|
-
exports.
|
|
2171
|
-
exports.
|
|
2172
|
-
exports.
|
|
2173
|
-
exports.
|
|
2174
|
-
exports.createImport = createImport;
|
|
2175
|
-
exports.createInput = createInput;
|
|
2176
|
-
exports.createJsx = createJsx;
|
|
2177
|
-
exports.createOperation = createOperation;
|
|
2178
|
-
exports.createOperationParams = createOperationParams;
|
|
2179
|
-
exports.createOutput = createOutput;
|
|
2180
|
-
exports.createParameter = createParameter;
|
|
2181
|
-
exports.createParameterGroup = createParameterGroup;
|
|
2182
|
-
exports.createParamsType = createParamsType;
|
|
2183
|
-
exports.createPrinterFactory = createPrinterFactory;
|
|
2184
|
-
exports.createProperty = createProperty;
|
|
2185
|
-
exports.createResponse = createResponse;
|
|
2186
|
-
exports.createSchema = createSchema;
|
|
2187
|
-
exports.createSource = createSource;
|
|
2188
|
-
exports.createText = createText;
|
|
2189
|
-
exports.createType = createType;
|
|
2190
|
-
exports.definePrinter = definePrinter;
|
|
2191
|
-
exports.enumPropName = enumPropName;
|
|
2192
|
-
exports.extractRefName = extractRefName;
|
|
1580
|
+
exports.collectSync = collectSync;
|
|
1581
|
+
exports.collectUsedSchemaNames = collectUsedSchemaNames;
|
|
1582
|
+
exports.combineExports = combineExports;
|
|
1583
|
+
exports.combineImports = combineImports;
|
|
1584
|
+
exports.combineSources = combineSources;
|
|
1585
|
+
exports.composeMacros = composeMacros;
|
|
1586
|
+
exports.constDef = constDef;
|
|
1587
|
+
exports.contentDef = contentDef;
|
|
1588
|
+
exports.createPrinter = createPrinter;
|
|
1589
|
+
exports.defineMacro = defineMacro;
|
|
1590
|
+
exports.defineNode = defineNode;
|
|
1591
|
+
exports.exportDef = exportDef;
|
|
2193
1592
|
exports.extractStringsFromNodes = extractStringsFromNodes;
|
|
1593
|
+
Object.defineProperty(exports, "factory", {
|
|
1594
|
+
enumerable: true,
|
|
1595
|
+
get: function() {
|
|
1596
|
+
return factory_exports;
|
|
1597
|
+
}
|
|
1598
|
+
});
|
|
1599
|
+
exports.fileDef = fileDef;
|
|
2194
1600
|
exports.findCircularSchemas = findCircularSchemas;
|
|
2195
|
-
exports.
|
|
2196
|
-
exports.
|
|
2197
|
-
exports.
|
|
2198
|
-
exports.
|
|
2199
|
-
exports.
|
|
2200
|
-
exports.isScalarPrimitive = isScalarPrimitive;
|
|
2201
|
-
exports.isSchemaNode = isSchemaNode;
|
|
2202
|
-
exports.isStringType = isStringType;
|
|
2203
|
-
exports.mediaTypes = mediaTypes;
|
|
2204
|
-
exports.mergeAdjacentObjects = mergeAdjacentObjects;
|
|
1601
|
+
exports.functionDef = functionDef;
|
|
1602
|
+
exports.importDef = importDef;
|
|
1603
|
+
exports.inputDef = inputDef;
|
|
1604
|
+
exports.isHttpOperationNode = isHttpOperationNode;
|
|
1605
|
+
exports.jsxDef = jsxDef;
|
|
2205
1606
|
exports.narrowSchema = narrowSchema;
|
|
2206
|
-
exports.
|
|
1607
|
+
exports.nodeDefs = nodeDefs;
|
|
1608
|
+
exports.operationDef = operationDef;
|
|
1609
|
+
exports.optionality = optionality;
|
|
1610
|
+
exports.outputDef = outputDef;
|
|
1611
|
+
exports.parameterDef = parameterDef;
|
|
1612
|
+
exports.propertyDef = propertyDef;
|
|
1613
|
+
exports.requestBodyDef = requestBodyDef;
|
|
2207
1614
|
exports.resolveRefName = resolveRefName;
|
|
1615
|
+
exports.responseDef = responseDef;
|
|
1616
|
+
exports.schemaDef = schemaDef;
|
|
2208
1617
|
exports.schemaTypes = schemaTypes;
|
|
2209
|
-
exports.
|
|
2210
|
-
exports.
|
|
2211
|
-
exports.simplifyUnion = simplifyUnion;
|
|
2212
|
-
exports.syncOptionality = syncOptionality;
|
|
2213
|
-
exports.syncSchemaRef = syncSchemaRef;
|
|
1618
|
+
exports.sourceDef = sourceDef;
|
|
1619
|
+
exports.textDef = textDef;
|
|
2214
1620
|
exports.transform = transform;
|
|
2215
|
-
exports.
|
|
1621
|
+
exports.typeDef = typeDef;
|
|
2216
1622
|
|
|
2217
1623
|
//# sourceMappingURL=index.cjs.map
|