@kubb/ast 5.0.0-beta.10 → 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 +1060 -1714
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +136 -3376
- package/dist/index.js +1011 -1668
- 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 +8 -7
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/constants.ts +0 -228
- package/src/factory.ts +0 -742
- package/src/guards.ts +0 -110
- package/src/index.ts +0 -46
- package/src/infer.ts +0 -130
- package/src/mocks.ts +0 -176
- package/src/nodes/base.ts +0 -56
- package/src/nodes/code.ts +0 -304
- package/src/nodes/file.ts +0 -230
- package/src/nodes/function.ts +0 -223
- package/src/nodes/http.ts +0 -119
- package/src/nodes/index.ts +0 -86
- package/src/nodes/operation.ts +0 -111
- package/src/nodes/output.ts +0 -26
- package/src/nodes/parameter.ts +0 -41
- package/src/nodes/property.ts +0 -34
- package/src/nodes/response.ts +0 -43
- package/src/nodes/root.ts +0 -64
- package/src/nodes/schema.ts +0 -656
- package/src/printer.ts +0 -250
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/transformers.ts +0 -159
- package/src/types.ts +0 -70
- package/src/utils.ts +0 -915
- package/src/visitor.ts +0 -592
package/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,929 +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
|
-
//#endregion
|
|
472
|
-
//#region src/refs.ts
|
|
289
|
+
const createConst = constDef.create;
|
|
473
290
|
/**
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
* Example: `#/components/schemas/Pet` becomes `Pet`.
|
|
291
|
+
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
477
292
|
*
|
|
478
293
|
* @example
|
|
479
294
|
* ```ts
|
|
480
|
-
*
|
|
295
|
+
* createType({ name: 'Pet', export: true })
|
|
296
|
+
* // export type Pet = ...
|
|
481
297
|
* ```
|
|
482
298
|
*/
|
|
483
|
-
|
|
484
|
-
return ref.split("/").at(-1) ?? ref;
|
|
485
|
-
}
|
|
486
|
-
//#endregion
|
|
487
|
-
//#region src/visitor.ts
|
|
299
|
+
const createType = typeDef.create;
|
|
488
300
|
/**
|
|
489
|
-
* Creates a
|
|
490
|
-
*
|
|
491
|
-
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
|
|
301
|
+
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
492
302
|
*
|
|
493
303
|
* @example
|
|
494
304
|
* ```ts
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
* await limit(() => task())
|
|
498
|
-
* }
|
|
499
|
-
* // 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> { ... }
|
|
500
307
|
* ```
|
|
501
308
|
*/
|
|
502
|
-
|
|
503
|
-
let active = 0;
|
|
504
|
-
const queue = [];
|
|
505
|
-
function next() {
|
|
506
|
-
if (active < concurrency && queue.length > 0) {
|
|
507
|
-
active++;
|
|
508
|
-
queue.shift()();
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
return function limit(fn) {
|
|
512
|
-
return new Promise((resolve, reject) => {
|
|
513
|
-
queue.push(() => {
|
|
514
|
-
Promise.resolve(fn()).then(resolve, reject).finally(() => {
|
|
515
|
-
active--;
|
|
516
|
-
next();
|
|
517
|
-
});
|
|
518
|
-
});
|
|
519
|
-
next();
|
|
520
|
-
});
|
|
521
|
-
};
|
|
522
|
-
}
|
|
309
|
+
const createFunction = functionDef.create;
|
|
523
310
|
/**
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
* For `Schema` nodes, children (`properties`, `items`, `members`, and non-boolean
|
|
527
|
-
* `additionalProperties`) are only included
|
|
528
|
-
* when `recurse` is `true`; shallow mode skips them.
|
|
311
|
+
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
529
312
|
*
|
|
530
313
|
* @example
|
|
531
314
|
* ```ts
|
|
532
|
-
*
|
|
533
|
-
* //
|
|
315
|
+
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
316
|
+
* // export const double = (n: number) => ...
|
|
534
317
|
* ```
|
|
535
318
|
*/
|
|
536
|
-
|
|
537
|
-
switch (node.kind) {
|
|
538
|
-
case "Input": return [...node.schemas, ...node.operations];
|
|
539
|
-
case "Output": return [];
|
|
540
|
-
case "Operation": return [
|
|
541
|
-
...node.parameters,
|
|
542
|
-
...node.requestBody?.content?.flatMap((c) => c.schema ? [c.schema] : []) ?? [],
|
|
543
|
-
...node.responses
|
|
544
|
-
];
|
|
545
|
-
case "Schema": {
|
|
546
|
-
const children = [];
|
|
547
|
-
if (!recurse) return [];
|
|
548
|
-
if ("properties" in node && node.properties.length > 0) children.push(...node.properties);
|
|
549
|
-
if ("items" in node && node.items) children.push(...node.items);
|
|
550
|
-
if ("members" in node && node.members) children.push(...node.members);
|
|
551
|
-
if ("additionalProperties" in node && node.additionalProperties && node.additionalProperties !== true) children.push(node.additionalProperties);
|
|
552
|
-
return children;
|
|
553
|
-
}
|
|
554
|
-
case "Property": return [node.schema];
|
|
555
|
-
case "Parameter": return [node.schema];
|
|
556
|
-
case "Response": return node.schema ? [node.schema] : [];
|
|
557
|
-
case "FunctionParameter":
|
|
558
|
-
case "ParameterGroup":
|
|
559
|
-
case "FunctionParameters":
|
|
560
|
-
case "Type": return [];
|
|
561
|
-
default: return [];
|
|
562
|
-
}
|
|
563
|
-
}
|
|
319
|
+
const createArrowFunction = arrowFunctionDef.create;
|
|
564
320
|
/**
|
|
565
|
-
*
|
|
566
|
-
* Sibling nodes at each level are visited concurrently up to `options.concurrency`
|
|
567
|
-
* (default: `WALK_CONCURRENCY`).
|
|
321
|
+
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
568
322
|
*
|
|
569
323
|
* @example
|
|
570
324
|
* ```ts
|
|
571
|
-
*
|
|
572
|
-
*
|
|
573
|
-
* console.log(node.operationId)
|
|
574
|
-
* },
|
|
575
|
-
* })
|
|
325
|
+
* createText('return fetch(id)')
|
|
326
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
576
327
|
* ```
|
|
328
|
+
*/
|
|
329
|
+
const createText = textDef.create;
|
|
330
|
+
/**
|
|
331
|
+
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
577
332
|
*
|
|
578
333
|
* @example
|
|
579
334
|
* ```ts
|
|
580
|
-
*
|
|
581
|
-
*
|
|
335
|
+
* createBreak()
|
|
336
|
+
* // { kind: 'Break' }
|
|
582
337
|
* ```
|
|
583
338
|
*/
|
|
584
|
-
|
|
585
|
-
return
|
|
586
|
-
}
|
|
587
|
-
async function _walk(node, visitor, recurse, limit, parent) {
|
|
588
|
-
switch (node.kind) {
|
|
589
|
-
case "Input":
|
|
590
|
-
await limit(() => visitor.input?.(node, { parent }));
|
|
591
|
-
break;
|
|
592
|
-
case "Output":
|
|
593
|
-
await limit(() => visitor.output?.(node, { parent }));
|
|
594
|
-
break;
|
|
595
|
-
case "Operation":
|
|
596
|
-
await limit(() => visitor.operation?.(node, { parent }));
|
|
597
|
-
break;
|
|
598
|
-
case "Schema":
|
|
599
|
-
await limit(() => visitor.schema?.(node, { parent }));
|
|
600
|
-
break;
|
|
601
|
-
case "Property":
|
|
602
|
-
await limit(() => visitor.property?.(node, { parent }));
|
|
603
|
-
break;
|
|
604
|
-
case "Parameter":
|
|
605
|
-
await limit(() => visitor.parameter?.(node, { parent }));
|
|
606
|
-
break;
|
|
607
|
-
case "Response":
|
|
608
|
-
await limit(() => visitor.response?.(node, { parent }));
|
|
609
|
-
break;
|
|
610
|
-
case "FunctionParameter":
|
|
611
|
-
case "ParameterGroup":
|
|
612
|
-
case "FunctionParameters": break;
|
|
613
|
-
}
|
|
614
|
-
const children = getChildren(node, recurse);
|
|
615
|
-
for (const child of children) await _walk(child, visitor, recurse, limit, node);
|
|
616
|
-
}
|
|
617
|
-
function transform(node, options) {
|
|
618
|
-
const { depth, parent, ...visitor } = options;
|
|
619
|
-
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
620
|
-
switch (node.kind) {
|
|
621
|
-
case "Input": {
|
|
622
|
-
let input = node;
|
|
623
|
-
const replaced = visitor.input?.(input, { parent });
|
|
624
|
-
if (replaced) input = replaced;
|
|
625
|
-
return {
|
|
626
|
-
...input,
|
|
627
|
-
schemas: input.schemas.map((s) => transform(s, {
|
|
628
|
-
...options,
|
|
629
|
-
parent: input
|
|
630
|
-
})),
|
|
631
|
-
operations: input.operations.map((op) => transform(op, {
|
|
632
|
-
...options,
|
|
633
|
-
parent: input
|
|
634
|
-
}))
|
|
635
|
-
};
|
|
636
|
-
}
|
|
637
|
-
case "Output": {
|
|
638
|
-
let output = node;
|
|
639
|
-
const replaced = visitor.output?.(output, { parent });
|
|
640
|
-
if (replaced) output = replaced;
|
|
641
|
-
return output;
|
|
642
|
-
}
|
|
643
|
-
case "Operation": {
|
|
644
|
-
let op = node;
|
|
645
|
-
const replaced = visitor.operation?.(op, { parent });
|
|
646
|
-
if (replaced) op = replaced;
|
|
647
|
-
return {
|
|
648
|
-
...op,
|
|
649
|
-
parameters: op.parameters.map((p) => transform(p, {
|
|
650
|
-
...options,
|
|
651
|
-
parent: op
|
|
652
|
-
})),
|
|
653
|
-
requestBody: op.requestBody ? {
|
|
654
|
-
...op.requestBody,
|
|
655
|
-
content: op.requestBody.content?.map((c) => ({
|
|
656
|
-
...c,
|
|
657
|
-
schema: c.schema ? transform(c.schema, {
|
|
658
|
-
...options,
|
|
659
|
-
parent: op
|
|
660
|
-
}) : void 0
|
|
661
|
-
}))
|
|
662
|
-
} : void 0,
|
|
663
|
-
responses: op.responses.map((r) => transform(r, {
|
|
664
|
-
...options,
|
|
665
|
-
parent: op
|
|
666
|
-
}))
|
|
667
|
-
};
|
|
668
|
-
}
|
|
669
|
-
case "Schema": {
|
|
670
|
-
let schema = node;
|
|
671
|
-
const replaced = visitor.schema?.(schema, { parent });
|
|
672
|
-
if (replaced) schema = replaced;
|
|
673
|
-
const childOptions = {
|
|
674
|
-
...options,
|
|
675
|
-
parent: schema
|
|
676
|
-
};
|
|
677
|
-
return {
|
|
678
|
-
...schema,
|
|
679
|
-
..."properties" in schema && recurse ? { properties: schema.properties.map((p) => transform(p, childOptions)) } : {},
|
|
680
|
-
..."items" in schema && recurse ? { items: schema.items?.map((i) => transform(i, childOptions)) } : {},
|
|
681
|
-
..."members" in schema && recurse ? { members: schema.members?.map((m) => transform(m, childOptions)) } : {},
|
|
682
|
-
..."additionalProperties" in schema && recurse && schema.additionalProperties && schema.additionalProperties !== true ? { additionalProperties: transform(schema.additionalProperties, childOptions) } : {}
|
|
683
|
-
};
|
|
684
|
-
}
|
|
685
|
-
case "Property": {
|
|
686
|
-
let prop = node;
|
|
687
|
-
const replaced = visitor.property?.(prop, { parent });
|
|
688
|
-
if (replaced) prop = replaced;
|
|
689
|
-
return createProperty({
|
|
690
|
-
...prop,
|
|
691
|
-
schema: transform(prop.schema, {
|
|
692
|
-
...options,
|
|
693
|
-
parent: prop
|
|
694
|
-
})
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
case "Parameter": {
|
|
698
|
-
let param = node;
|
|
699
|
-
const replaced = visitor.parameter?.(param, { parent });
|
|
700
|
-
if (replaced) param = replaced;
|
|
701
|
-
return createParameter({
|
|
702
|
-
...param,
|
|
703
|
-
schema: transform(param.schema, {
|
|
704
|
-
...options,
|
|
705
|
-
parent: param
|
|
706
|
-
})
|
|
707
|
-
});
|
|
708
|
-
}
|
|
709
|
-
case "Response": {
|
|
710
|
-
let response = node;
|
|
711
|
-
const replaced = visitor.response?.(response, { parent });
|
|
712
|
-
if (replaced) response = replaced;
|
|
713
|
-
return {
|
|
714
|
-
...response,
|
|
715
|
-
schema: transform(response.schema, {
|
|
716
|
-
...options,
|
|
717
|
-
parent: response
|
|
718
|
-
})
|
|
719
|
-
};
|
|
720
|
-
}
|
|
721
|
-
case "FunctionParameter":
|
|
722
|
-
case "ParameterGroup":
|
|
723
|
-
case "FunctionParameters":
|
|
724
|
-
case "Type": return node;
|
|
725
|
-
default: return node;
|
|
726
|
-
}
|
|
339
|
+
function createBreak() {
|
|
340
|
+
return breakDef.create();
|
|
727
341
|
}
|
|
728
342
|
/**
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
* Non-`undefined` values returned by visitor callbacks are appended to the result.
|
|
732
|
-
*
|
|
733
|
-
* @example
|
|
734
|
-
* ```ts
|
|
735
|
-
* const ids = collect(root, {
|
|
736
|
-
* operation(node) {
|
|
737
|
-
* return node.operationId
|
|
738
|
-
* },
|
|
739
|
-
* })
|
|
740
|
-
* ```
|
|
343
|
+
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
741
344
|
*
|
|
742
345
|
* @example
|
|
743
346
|
* ```ts
|
|
744
|
-
*
|
|
745
|
-
*
|
|
347
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
348
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
746
349
|
* ```
|
|
747
350
|
*/
|
|
748
|
-
|
|
749
|
-
const { depth, parent, ...visitor } = options;
|
|
750
|
-
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
751
|
-
const results = [];
|
|
752
|
-
let v;
|
|
753
|
-
switch (node.kind) {
|
|
754
|
-
case "Input":
|
|
755
|
-
v = visitor.input?.(node, { parent });
|
|
756
|
-
break;
|
|
757
|
-
case "Output":
|
|
758
|
-
v = visitor.output?.(node, { parent });
|
|
759
|
-
break;
|
|
760
|
-
case "Operation":
|
|
761
|
-
v = visitor.operation?.(node, { parent });
|
|
762
|
-
break;
|
|
763
|
-
case "Schema":
|
|
764
|
-
v = visitor.schema?.(node, { parent });
|
|
765
|
-
break;
|
|
766
|
-
case "Property":
|
|
767
|
-
v = visitor.property?.(node, { parent });
|
|
768
|
-
break;
|
|
769
|
-
case "Parameter":
|
|
770
|
-
v = visitor.parameter?.(node, { parent });
|
|
771
|
-
break;
|
|
772
|
-
case "Response":
|
|
773
|
-
v = visitor.response?.(node, { parent });
|
|
774
|
-
break;
|
|
775
|
-
case "FunctionParameter":
|
|
776
|
-
case "ParameterGroup":
|
|
777
|
-
case "FunctionParameters": break;
|
|
778
|
-
}
|
|
779
|
-
if (v !== void 0) results.push(v);
|
|
780
|
-
for (const child of getChildren(node, recurse)) for (const item of collect(child, {
|
|
781
|
-
...options,
|
|
782
|
-
parent: node
|
|
783
|
-
})) results.push(item);
|
|
784
|
-
return results;
|
|
785
|
-
}
|
|
351
|
+
const createJsx = jsxDef.create;
|
|
786
352
|
//#endregion
|
|
787
|
-
//#region src/
|
|
788
|
-
const plainStringTypes = new Set([
|
|
789
|
-
"string",
|
|
790
|
-
"uuid",
|
|
791
|
-
"email",
|
|
792
|
-
"url",
|
|
793
|
-
"datetime"
|
|
794
|
-
]);
|
|
353
|
+
//#region src/nodes/content.ts
|
|
795
354
|
/**
|
|
796
|
-
*
|
|
797
|
-
*
|
|
798
|
-
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node
|
|
799
|
-
* override the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
|
|
800
|
-
*
|
|
801
|
-
* @example
|
|
802
|
-
* ```ts
|
|
803
|
-
* // Ref with description override
|
|
804
|
-
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
|
|
805
|
-
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
|
|
806
|
-
* ```
|
|
355
|
+
* Definition for the {@link ContentNode}.
|
|
807
356
|
*/
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
|
|
813
|
-
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
|
|
814
|
-
return createSchema({
|
|
815
|
-
...ref.schema,
|
|
816
|
-
...definedOverrides
|
|
817
|
-
});
|
|
818
|
-
}
|
|
357
|
+
const contentDef = defineNode({
|
|
358
|
+
kind: "Content",
|
|
359
|
+
children: ["schema"]
|
|
360
|
+
});
|
|
819
361
|
/**
|
|
820
|
-
*
|
|
821
|
-
*
|
|
822
|
-
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
|
|
823
|
-
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
|
|
362
|
+
* Creates a `ContentNode` for a single request-body or response content type.
|
|
824
363
|
*/
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
if (temporal) return temporal.representation !== "date";
|
|
829
|
-
return false;
|
|
830
|
-
}
|
|
364
|
+
const createContent = contentDef.create;
|
|
365
|
+
//#endregion
|
|
366
|
+
//#region ../../internals/utils/src/fs.ts
|
|
831
367
|
/**
|
|
832
|
-
*
|
|
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.
|
|
833
370
|
*
|
|
834
|
-
*
|
|
835
|
-
*
|
|
836
|
-
*
|
|
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'
|
|
837
376
|
*/
|
|
838
|
-
function
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
return {
|
|
843
|
-
...param,
|
|
844
|
-
name: transformed
|
|
845
|
-
};
|
|
846
|
-
});
|
|
377
|
+
function trimExtName(text) {
|
|
378
|
+
const dotIndex = text.lastIndexOf(".");
|
|
379
|
+
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
380
|
+
return text;
|
|
847
381
|
}
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region ../../internals/utils/src/promise.ts
|
|
848
384
|
/**
|
|
849
|
-
*
|
|
385
|
+
* Wraps `factory` with a keyed cache backed by the provided store.
|
|
850
386
|
*
|
|
851
|
-
*
|
|
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
|
|
852
396
|
* ```ts
|
|
853
|
-
*
|
|
854
|
-
*
|
|
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')
|
|
855
412
|
* ```
|
|
856
413
|
*/
|
|
857
|
-
function
|
|
858
|
-
return
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
schema: createSchema({
|
|
864
|
-
type: "enum",
|
|
865
|
-
primitive: "string",
|
|
866
|
-
enumValues: [value]
|
|
867
|
-
}),
|
|
868
|
-
required: true
|
|
869
|
-
})]
|
|
870
|
-
});
|
|
871
|
-
}
|
|
872
|
-
function resolveParamsType({ node, param, resolver }) {
|
|
873
|
-
if (!resolver) return createParamsType({
|
|
874
|
-
variant: "reference",
|
|
875
|
-
name: param.schema.primitive ?? "unknown"
|
|
876
|
-
});
|
|
877
|
-
const individualName = resolver.resolveParamName(node, param);
|
|
878
|
-
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
|
|
879
|
-
const groupResolvers = {
|
|
880
|
-
path: resolver.resolvePathParamsName,
|
|
881
|
-
query: resolver.resolveQueryParamsName,
|
|
882
|
-
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;
|
|
883
420
|
};
|
|
884
|
-
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
|
|
885
|
-
if (groupName && groupName !== individualName) return createParamsType({
|
|
886
|
-
variant: "member",
|
|
887
|
-
base: groupName,
|
|
888
|
-
key: param.name
|
|
889
|
-
});
|
|
890
|
-
return createParamsType({
|
|
891
|
-
variant: "reference",
|
|
892
|
-
name: individualName
|
|
893
|
-
});
|
|
894
421
|
}
|
|
422
|
+
//#endregion
|
|
423
|
+
//#region src/utils/extractStringsFromNodes.ts
|
|
895
424
|
/**
|
|
896
|
-
*
|
|
897
|
-
*
|
|
898
|
-
*
|
|
899
|
-
* and
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
const
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
const pathName = paramNames?.path ?? "pathParams";
|
|
909
|
-
const wrapType = (type) => createParamsType({
|
|
910
|
-
variant: "reference",
|
|
911
|
-
name: typeWrapper ? typeWrapper(type) : type
|
|
912
|
-
});
|
|
913
|
-
const wrapTypeNode = (type) => type.kind === "ParamsType" && type.variant === "reference" ? wrapType(type.name) : type;
|
|
914
|
-
const casedParams = caseParams(node.parameters, paramsCasing);
|
|
915
|
-
const pathParams = casedParams.filter((p) => p.in === "path");
|
|
916
|
-
const queryParams = casedParams.filter((p) => p.in === "query");
|
|
917
|
-
const headerParams = casedParams.filter((p) => p.in === "header");
|
|
918
|
-
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
|
|
919
|
-
const bodyRequired = node.requestBody?.required ?? false;
|
|
920
|
-
const queryGroupType = resolver ? resolveGroupType({
|
|
921
|
-
node,
|
|
922
|
-
params: queryParams,
|
|
923
|
-
groupMethod: resolver.resolveQueryParamsName,
|
|
924
|
-
resolver
|
|
925
|
-
}) : void 0;
|
|
926
|
-
const headerGroupType = resolver ? resolveGroupType({
|
|
927
|
-
node,
|
|
928
|
-
params: headerParams,
|
|
929
|
-
groupMethod: resolver.resolveHeaderParamsName,
|
|
930
|
-
resolver
|
|
931
|
-
}) : void 0;
|
|
932
|
-
const params = [];
|
|
933
|
-
if (paramsType === "object") {
|
|
934
|
-
const children = [
|
|
935
|
-
...pathParams.map((p) => {
|
|
936
|
-
const type = resolveParamsType({
|
|
937
|
-
node,
|
|
938
|
-
param: p,
|
|
939
|
-
resolver
|
|
940
|
-
});
|
|
941
|
-
return createFunctionParameter({
|
|
942
|
-
name: p.name,
|
|
943
|
-
type: wrapTypeNode(type),
|
|
944
|
-
optional: !p.required
|
|
945
|
-
});
|
|
946
|
-
}),
|
|
947
|
-
...bodyType ? [createFunctionParameter({
|
|
948
|
-
name: dataName,
|
|
949
|
-
type: bodyType,
|
|
950
|
-
optional: !bodyRequired
|
|
951
|
-
})] : [],
|
|
952
|
-
...buildGroupParam({
|
|
953
|
-
name: paramsName,
|
|
954
|
-
node,
|
|
955
|
-
params: queryParams,
|
|
956
|
-
groupType: queryGroupType,
|
|
957
|
-
resolver,
|
|
958
|
-
wrapType
|
|
959
|
-
}),
|
|
960
|
-
...buildGroupParam({
|
|
961
|
-
name: headersName,
|
|
962
|
-
node,
|
|
963
|
-
params: headerParams,
|
|
964
|
-
groupType: headerGroupType,
|
|
965
|
-
resolver,
|
|
966
|
-
wrapType
|
|
967
|
-
})
|
|
968
|
-
];
|
|
969
|
-
if (children.length) params.push(createParameterGroup({
|
|
970
|
-
properties: children,
|
|
971
|
-
default: children.every((c) => c.optional) ? "{}" : void 0
|
|
972
|
-
}));
|
|
973
|
-
} else {
|
|
974
|
-
if (pathParams.length) if (pathParamsType === "inlineSpread") {
|
|
975
|
-
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]) ?? void 0;
|
|
976
|
-
params.push(createFunctionParameter({
|
|
977
|
-
name: pathName,
|
|
978
|
-
type: spreadType ? wrapType(spreadType) : void 0,
|
|
979
|
-
rest: true
|
|
980
|
-
}));
|
|
981
|
-
} else {
|
|
982
|
-
const pathChildren = pathParams.map((p) => {
|
|
983
|
-
const type = resolveParamsType({
|
|
984
|
-
node,
|
|
985
|
-
param: p,
|
|
986
|
-
resolver
|
|
987
|
-
});
|
|
988
|
-
return createFunctionParameter({
|
|
989
|
-
name: p.name,
|
|
990
|
-
type: wrapTypeNode(type),
|
|
991
|
-
optional: !p.required
|
|
992
|
-
});
|
|
993
|
-
});
|
|
994
|
-
params.push(createParameterGroup({
|
|
995
|
-
properties: pathChildren,
|
|
996
|
-
inline: pathParamsType === "inline",
|
|
997
|
-
default: pathParamsDefault ?? (pathChildren.every((c) => c.optional) ? "{}" : void 0)
|
|
998
|
-
}));
|
|
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;
|
|
999
437
|
}
|
|
1000
|
-
if (
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
groupType: headerGroupType,
|
|
1018
|
-
resolver,
|
|
1019
|
-
wrapType
|
|
1020
|
-
}));
|
|
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"));
|
|
1021
455
|
}
|
|
1022
|
-
|
|
1023
|
-
return createFunctionParameters({ params });
|
|
1024
|
-
}
|
|
1025
|
-
/**
|
|
1026
|
-
* Builds a single {@link FunctionParameterNode} for a query or header group.
|
|
1027
|
-
* Returns an empty array when there are no params to emit.
|
|
1028
|
-
*
|
|
1029
|
-
* If a pre-resolved `groupType` is provided it emits `name: GroupType`.
|
|
1030
|
-
* Otherwise, it builds an inline struct from the individual params.
|
|
1031
|
-
*/
|
|
1032
|
-
function buildGroupParam({ name, node, params, groupType, resolver, wrapType }) {
|
|
1033
|
-
if (groupType) return [createFunctionParameter({
|
|
1034
|
-
name,
|
|
1035
|
-
type: groupType.type.kind === "ParamsType" && groupType.type.variant === "reference" ? wrapType(groupType.type.name) : groupType.type,
|
|
1036
|
-
optional: groupType.optional
|
|
1037
|
-
})];
|
|
1038
|
-
if (params.length) return [createFunctionParameter({
|
|
1039
|
-
name,
|
|
1040
|
-
type: toStructType({
|
|
1041
|
-
node,
|
|
1042
|
-
params,
|
|
1043
|
-
resolver
|
|
1044
|
-
}),
|
|
1045
|
-
optional: params.every((p) => !p.required)
|
|
1046
|
-
})];
|
|
1047
|
-
return [];
|
|
1048
|
-
}
|
|
1049
|
-
/**
|
|
1050
|
-
* Derives a {@link ParamGroupType} from the resolver's group method.
|
|
1051
|
-
* Returns `undefined` when the group name equals the individual param name (no real group).
|
|
1052
|
-
*/
|
|
1053
|
-
function resolveGroupType({ node, params, groupMethod, resolver }) {
|
|
1054
|
-
if (!params.length) return;
|
|
1055
|
-
const firstParam = params[0];
|
|
1056
|
-
const groupName = groupMethod.call(resolver, node, firstParam);
|
|
1057
|
-
if (groupName === resolver.resolveParamName(node, firstParam)) return;
|
|
1058
|
-
const allOptional = params.every((p) => !p.required);
|
|
1059
|
-
return {
|
|
1060
|
-
type: createParamsType({
|
|
1061
|
-
variant: "reference",
|
|
1062
|
-
name: groupName
|
|
1063
|
-
}),
|
|
1064
|
-
optional: allOptional
|
|
1065
|
-
};
|
|
1066
|
-
}
|
|
1067
|
-
/**
|
|
1068
|
-
* Builds a {@link TypeNode} with `variant: 'struct'` for an inline anonymous type grouping named fields.
|
|
1069
|
-
*
|
|
1070
|
-
* Used when query or header parameters have no dedicated group type name.
|
|
1071
|
-
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
|
|
1072
|
-
*/
|
|
1073
|
-
function toStructType({ node, params, resolver }) {
|
|
1074
|
-
return createParamsType({
|
|
1075
|
-
variant: "struct",
|
|
1076
|
-
properties: params.map((p) => ({
|
|
1077
|
-
name: p.name,
|
|
1078
|
-
optional: !p.required,
|
|
1079
|
-
type: resolveParamsType({
|
|
1080
|
-
node,
|
|
1081
|
-
param: p,
|
|
1082
|
-
resolver
|
|
1083
|
-
})
|
|
1084
|
-
}))
|
|
1085
|
-
});
|
|
456
|
+
return collected.join("\n");
|
|
1086
457
|
}
|
|
458
|
+
//#endregion
|
|
459
|
+
//#region src/utils/combineFileMembers.ts
|
|
1087
460
|
function sourceKey(source) {
|
|
1088
461
|
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
|
|
1089
462
|
}
|
|
@@ -1098,19 +471,19 @@ function importKey(path, name, isTypeOnly) {
|
|
|
1098
471
|
}
|
|
1099
472
|
/**
|
|
1100
473
|
* Computes a multi-level sort key for exports and imports:
|
|
1101
|
-
* non-array names first (wildcards/namespace aliases)
|
|
474
|
+
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
|
|
1102
475
|
*/
|
|
1103
476
|
function sortKey(node) {
|
|
1104
477
|
const isArray = Array.isArray(node.name) ? "1" : "0";
|
|
1105
478
|
const typeOnly = node.isTypeOnly ? "0" : "1";
|
|
1106
479
|
const hasName = node.name != null ? "1" : "0";
|
|
1107
|
-
const name = Array.isArray(node.name) ?
|
|
480
|
+
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
|
|
1108
481
|
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
|
|
1109
482
|
}
|
|
1110
483
|
/**
|
|
1111
|
-
* Deduplicates
|
|
1112
|
-
*
|
|
1113
|
-
*
|
|
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.
|
|
1114
487
|
*/
|
|
1115
488
|
function combineSources(sources) {
|
|
1116
489
|
const seen = /* @__PURE__ */ new Map();
|
|
@@ -1121,6 +494,16 @@ function combineSources(sources) {
|
|
|
1121
494
|
return [...seen.values()];
|
|
1122
495
|
}
|
|
1123
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
|
+
/**
|
|
1124
507
|
* Deduplicates and merges `ExportNode` objects by path and type.
|
|
1125
508
|
*
|
|
1126
509
|
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
|
|
@@ -1141,11 +524,8 @@ function combineExports(exports) {
|
|
|
1141
524
|
if (!name.length) continue;
|
|
1142
525
|
const key = pathTypeKey(path, isTypeOnly);
|
|
1143
526
|
const existing = namedByPath.get(key);
|
|
1144
|
-
if (existing && Array.isArray(existing.name))
|
|
1145
|
-
|
|
1146
|
-
for (const n of name) merged.add(n);
|
|
1147
|
-
existing.name = [...merged];
|
|
1148
|
-
} else {
|
|
527
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
528
|
+
else {
|
|
1149
529
|
const newItem = {
|
|
1150
530
|
...curr,
|
|
1151
531
|
name: [...new Set(name)]
|
|
@@ -1168,8 +548,6 @@ function combineExports(exports) {
|
|
|
1168
548
|
*
|
|
1169
549
|
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
|
|
1170
550
|
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
|
|
1171
|
-
*
|
|
1172
|
-
* @note Use this when combining imports from multiple files to avoid duplicate declarations.
|
|
1173
551
|
*/
|
|
1174
552
|
function combineImports(imports, exports, source) {
|
|
1175
553
|
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
@@ -1181,6 +559,11 @@ function combineImports(imports, exports, source) {
|
|
|
1181
559
|
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
|
|
1182
560
|
return importNameMemo.get(key);
|
|
1183
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
|
+
}
|
|
1184
567
|
const result = [];
|
|
1185
568
|
const namedByPath = /* @__PURE__ */ new Map();
|
|
1186
569
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -1198,11 +581,8 @@ function combineImports(imports, exports, source) {
|
|
|
1198
581
|
if (!name.length) continue;
|
|
1199
582
|
const key = pathTypeKey(path, isTypeOnly);
|
|
1200
583
|
const existing = namedByPath.get(key);
|
|
1201
|
-
if (existing && Array.isArray(existing.name))
|
|
1202
|
-
|
|
1203
|
-
for (const n of name) merged.add(n);
|
|
1204
|
-
existing.name = [...merged];
|
|
1205
|
-
} else {
|
|
584
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
585
|
+
else {
|
|
1206
586
|
const newItem = {
|
|
1207
587
|
...curr,
|
|
1208
588
|
name
|
|
@@ -1211,7 +591,7 @@ function combineImports(imports, exports, source) {
|
|
|
1211
591
|
namedByPath.set(key, newItem);
|
|
1212
592
|
}
|
|
1213
593
|
} else {
|
|
1214
|
-
if (name && !isUsed(name)) continue;
|
|
594
|
+
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
|
|
1215
595
|
const key = importKey(path, name, isTypeOnly);
|
|
1216
596
|
if (!seen.has(key)) {
|
|
1217
597
|
result.push(curr);
|
|
@@ -1221,210 +601,218 @@ function combineImports(imports, exports, source) {
|
|
|
1221
601
|
}
|
|
1222
602
|
return result;
|
|
1223
603
|
}
|
|
604
|
+
//#endregion
|
|
605
|
+
//#region src/nodes/file.ts
|
|
1224
606
|
/**
|
|
1225
|
-
*
|
|
1226
|
-
*
|
|
1227
|
-
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
1228
|
-
* and nested node content. Used internally to build the full source string for import filtering.
|
|
607
|
+
* Definition for the {@link ImportNode}.
|
|
1229
608
|
*/
|
|
1230
|
-
|
|
1231
|
-
if (!nodes?.length) return "";
|
|
1232
|
-
return nodes.map((node) => {
|
|
1233
|
-
if (typeof node === "string") return node;
|
|
1234
|
-
if (node.kind === "Text") return node.value;
|
|
1235
|
-
if (node.kind === "Break") return "";
|
|
1236
|
-
if (node.kind === "Jsx") return node.value;
|
|
1237
|
-
const parts = [];
|
|
1238
|
-
if ("params" in node && node.params) parts.push(node.params);
|
|
1239
|
-
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
1240
|
-
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
1241
|
-
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
1242
|
-
const nested = extractStringsFromNodes(node.nodes);
|
|
1243
|
-
if (nested) parts.push(nested);
|
|
1244
|
-
return parts.join("\n");
|
|
1245
|
-
}).filter(Boolean).join("\n");
|
|
1246
|
-
}
|
|
609
|
+
const importDef = defineNode({ kind: "Import" });
|
|
1247
610
|
/**
|
|
1248
|
-
*
|
|
1249
|
-
*
|
|
1250
|
-
* Returns `undefined` for non-ref nodes or when no name can be resolved. Use this to get a schema's
|
|
1251
|
-
* identifier for type definitions or error messages.
|
|
1252
|
-
*
|
|
1253
|
-
* @example
|
|
1254
|
-
* ```ts
|
|
1255
|
-
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
|
|
1256
|
-
* // => 'Pet'
|
|
1257
|
-
* ```
|
|
611
|
+
* Definition for the {@link ExportNode}.
|
|
1258
612
|
*/
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
}
|
|
613
|
+
const exportDef = defineNode({ kind: "Export" });
|
|
614
|
+
/**
|
|
615
|
+
* Definition for the {@link SourceNode}.
|
|
616
|
+
*/
|
|
617
|
+
const sourceDef = defineNode({ kind: "Source" });
|
|
1264
618
|
/**
|
|
1265
|
-
*
|
|
619
|
+
* Definition for the {@link FileNode}. The fully resolved builder lives in
|
|
620
|
+
* `createFile`, so this definition only supplies the guard.
|
|
621
|
+
*/
|
|
622
|
+
const fileDef = defineNode({ kind: "File" });
|
|
623
|
+
/**
|
|
624
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
1266
625
|
*
|
|
1267
|
-
*
|
|
1268
|
-
*
|
|
626
|
+
* @example Named import
|
|
627
|
+
* ```ts
|
|
628
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
629
|
+
* // import { useState } from 'react'
|
|
630
|
+
* ```
|
|
631
|
+
*/
|
|
632
|
+
const createImport = importDef.create;
|
|
633
|
+
/**
|
|
634
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
1269
635
|
*
|
|
1270
|
-
* @example
|
|
636
|
+
* @example Named export
|
|
1271
637
|
* ```ts
|
|
1272
|
-
*
|
|
1273
|
-
* //
|
|
638
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
639
|
+
* // export { Pet } from './Pet'
|
|
1274
640
|
* ```
|
|
641
|
+
*/
|
|
642
|
+
const createExport = exportDef.create;
|
|
643
|
+
/**
|
|
644
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
1275
645
|
*
|
|
1276
|
-
* @example
|
|
646
|
+
* @example
|
|
1277
647
|
* ```ts
|
|
1278
|
-
*
|
|
1279
|
-
* for (const schema of schemas) {
|
|
1280
|
-
* collectReferencedSchemaNames(schema, out)
|
|
1281
|
-
* }
|
|
648
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
1282
649
|
* ```
|
|
1283
650
|
*/
|
|
1284
|
-
|
|
1285
|
-
if (!node) return out;
|
|
1286
|
-
collect(node, { schema(child) {
|
|
1287
|
-
if (child.type === "ref") {
|
|
1288
|
-
const name = resolveRefName(child);
|
|
1289
|
-
if (name) out.add(name);
|
|
1290
|
-
}
|
|
1291
|
-
} });
|
|
1292
|
-
return out;
|
|
1293
|
-
}
|
|
651
|
+
const createSource = sourceDef.create;
|
|
1294
652
|
/**
|
|
1295
|
-
*
|
|
653
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
1296
654
|
*
|
|
1297
|
-
*
|
|
1298
|
-
*
|
|
1299
|
-
*
|
|
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)
|
|
1300
664
|
*
|
|
1301
|
-
*
|
|
1302
|
-
* are reachable from the allowed operations, so that schemas used only by excluded operations
|
|
1303
|
-
* are not generated.
|
|
665
|
+
* @throws {Error} when `baseName` has no extension.
|
|
1304
666
|
*
|
|
1305
|
-
* @example
|
|
667
|
+
* @example
|
|
1306
668
|
* ```ts
|
|
1307
|
-
* const
|
|
1308
|
-
*
|
|
1309
|
-
*
|
|
1310
|
-
*
|
|
1311
|
-
*
|
|
1312
|
-
*
|
|
1313
|
-
* }
|
|
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' })],
|
|
675
|
+
* })
|
|
676
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
677
|
+
* // file.name = 'petStore'
|
|
678
|
+
* // file.extname = '.ts'
|
|
1314
679
|
* ```
|
|
1315
680
|
*
|
|
1316
|
-
* @example
|
|
681
|
+
* @example Copy a real file into the output verbatim
|
|
1317
682
|
* ```ts
|
|
1318
|
-
* const
|
|
1319
|
-
*
|
|
683
|
+
* const file = createFile({
|
|
684
|
+
* baseName: 'client.ts',
|
|
685
|
+
* path: 'src/gen/client.ts',
|
|
686
|
+
* copy: '/abs/path/to/templates/client.ts',
|
|
687
|
+
* })
|
|
1320
688
|
* ```
|
|
1321
689
|
*/
|
|
1322
|
-
function
|
|
1323
|
-
const
|
|
1324
|
-
|
|
1325
|
-
const
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
for (const op of operations) for (const schema of collect(op, {
|
|
1335
|
-
depth: "shallow",
|
|
1336
|
-
schema: (node) => node
|
|
1337
|
-
})) visitSchema(schema);
|
|
1338
|
-
return result;
|
|
1339
|
-
}
|
|
1340
|
-
/**
|
|
1341
|
-
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
1342
|
-
*
|
|
1343
|
-
* Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
|
|
1344
|
-
* in deferred constructs (lazy getter, `z.lazy(() => …)`) to prevent infinite recursion when generated code runs.
|
|
1345
|
-
* Refs are followed by name only, keeping the algorithm linear in the schema graph size.
|
|
1346
|
-
*
|
|
1347
|
-
* @note Call this once on the full schema graph, then use `containsCircularRef()` to check individual schemas.
|
|
1348
|
-
*/
|
|
1349
|
-
function findCircularSchemas(schemas) {
|
|
1350
|
-
const graph = /* @__PURE__ */ new Map();
|
|
1351
|
-
for (const schema of schemas) {
|
|
1352
|
-
if (!schema.name) continue;
|
|
1353
|
-
graph.set(schema.name, collectReferencedSchemaNames(schema));
|
|
1354
|
-
}
|
|
1355
|
-
const circular = /* @__PURE__ */ new Set();
|
|
1356
|
-
for (const start of graph.keys()) {
|
|
1357
|
-
const visited = /* @__PURE__ */ new Set();
|
|
1358
|
-
const stack = [...graph.get(start) ?? []];
|
|
1359
|
-
while (stack.length > 0) {
|
|
1360
|
-
const node = stack.pop();
|
|
1361
|
-
if (node === start) {
|
|
1362
|
-
circular.add(start);
|
|
1363
|
-
break;
|
|
1364
|
-
}
|
|
1365
|
-
if (visited.has(node)) continue;
|
|
1366
|
-
visited.add(node);
|
|
1367
|
-
const next = graph.get(node);
|
|
1368
|
-
if (next) for (const r of next) stack.push(r);
|
|
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);
|
|
1369
702
|
}
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
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) : [];
|
|
718
|
+
return {
|
|
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 ?? {}
|
|
728
|
+
};
|
|
1388
729
|
}
|
|
1389
730
|
//#endregion
|
|
1390
|
-
//#region src/
|
|
731
|
+
//#region src/nodes/input.ts
|
|
1391
732
|
/**
|
|
1392
|
-
*
|
|
1393
|
-
*
|
|
1394
|
-
* - `optional` is set for non-required, non-nullable schemas.
|
|
1395
|
-
* - `nullish` is set for non-required, nullable schemas.
|
|
733
|
+
* Definition for the {@link InputNode}.
|
|
1396
734
|
*/
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
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
|
+
});
|
|
1405
748
|
/**
|
|
1406
|
-
* Creates an `InputNode`
|
|
749
|
+
* Creates an `InputNode`, defaulting `schemas`/`operations` to empty arrays and `meta` per
|
|
750
|
+
* {@link inputDef}.
|
|
1407
751
|
*
|
|
1408
752
|
* @example
|
|
1409
753
|
* ```ts
|
|
1410
754
|
* const input = createInput()
|
|
1411
755
|
* // { kind: 'Input', schemas: [], operations: [] }
|
|
1412
756
|
* ```
|
|
1413
|
-
*
|
|
1414
|
-
* @example
|
|
1415
|
-
* ```ts
|
|
1416
|
-
* const input = createInput({ schemas: [petSchema] })
|
|
1417
|
-
* // keeps default operations: []
|
|
1418
|
-
* ```
|
|
1419
757
|
*/
|
|
1420
758
|
function createInput(overrides = {}) {
|
|
1421
|
-
return
|
|
1422
|
-
schemas: [],
|
|
1423
|
-
operations: [],
|
|
1424
|
-
...overrides,
|
|
1425
|
-
kind: "Input"
|
|
1426
|
-
};
|
|
759
|
+
return inputDef.create(overrides);
|
|
1427
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
|
+
});
|
|
1428
816
|
/**
|
|
1429
817
|
* Creates an `OutputNode` with a stable default for `files`.
|
|
1430
818
|
*
|
|
@@ -1433,130 +821,46 @@ function createInput(overrides = {}) {
|
|
|
1433
821
|
* const output = createOutput()
|
|
1434
822
|
* // { kind: 'Output', files: [] }
|
|
1435
823
|
* ```
|
|
1436
|
-
*
|
|
1437
|
-
* @example
|
|
1438
|
-
* ```ts
|
|
1439
|
-
* const output = createOutput({ files: [petFile] })
|
|
1440
|
-
* ```
|
|
1441
824
|
*/
|
|
1442
825
|
function createOutput(overrides = {}) {
|
|
1443
|
-
return
|
|
1444
|
-
files: [],
|
|
1445
|
-
...overrides,
|
|
1446
|
-
kind: "Output"
|
|
1447
|
-
};
|
|
1448
|
-
}
|
|
1449
|
-
/**
|
|
1450
|
-
* Creates an `OperationNode` with default empty arrays for `tags`, `parameters`, and `responses`.
|
|
1451
|
-
*
|
|
1452
|
-
* @example
|
|
1453
|
-
* ```ts
|
|
1454
|
-
* const operation = createOperation({
|
|
1455
|
-
* operationId: 'getPetById',
|
|
1456
|
-
* method: 'GET',
|
|
1457
|
-
* path: '/pet/{petId}',
|
|
1458
|
-
* })
|
|
1459
|
-
* // tags, parameters, and responses are []
|
|
1460
|
-
* ```
|
|
1461
|
-
*
|
|
1462
|
-
* @example
|
|
1463
|
-
* ```ts
|
|
1464
|
-
* const operation = createOperation({
|
|
1465
|
-
* operationId: 'findPets',
|
|
1466
|
-
* method: 'GET',
|
|
1467
|
-
* path: '/pet/findByStatus',
|
|
1468
|
-
* tags: ['pet'],
|
|
1469
|
-
* })
|
|
1470
|
-
* ```
|
|
1471
|
-
*/
|
|
1472
|
-
function createOperation(props) {
|
|
1473
|
-
return {
|
|
1474
|
-
tags: [],
|
|
1475
|
-
parameters: [],
|
|
1476
|
-
responses: [],
|
|
1477
|
-
...props,
|
|
1478
|
-
kind: "Operation"
|
|
1479
|
-
};
|
|
826
|
+
return outputDef.create(overrides);
|
|
1480
827
|
}
|
|
828
|
+
//#endregion
|
|
829
|
+
//#region src/optionality.ts
|
|
1481
830
|
/**
|
|
1482
|
-
*
|
|
1483
|
-
*
|
|
1484
|
-
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
|
|
831
|
+
* Generic JSON Schema optionality: a non-required field is optional, and a
|
|
832
|
+
* non-required nullable field is nullish.
|
|
1485
833
|
*/
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
number: "number",
|
|
1489
|
-
integer: "integer",
|
|
1490
|
-
bigint: "bigint",
|
|
1491
|
-
boolean: "boolean",
|
|
1492
|
-
null: "null",
|
|
1493
|
-
any: "any",
|
|
1494
|
-
unknown: "unknown",
|
|
1495
|
-
void: "void",
|
|
1496
|
-
never: "never",
|
|
1497
|
-
object: "object",
|
|
1498
|
-
array: "array",
|
|
1499
|
-
date: "date",
|
|
1500
|
-
uuid: "string",
|
|
1501
|
-
email: "string",
|
|
1502
|
-
url: "string",
|
|
1503
|
-
datetime: "string",
|
|
1504
|
-
time: "string"
|
|
1505
|
-
};
|
|
1506
|
-
function createSchema(props) {
|
|
1507
|
-
const inferredPrimitive = TYPE_TO_PRIMITIVE[props.type];
|
|
1508
|
-
if (props["type"] === "object") return {
|
|
1509
|
-
properties: [],
|
|
1510
|
-
primitive: "object",
|
|
1511
|
-
...props,
|
|
1512
|
-
kind: "Schema"
|
|
1513
|
-
};
|
|
834
|
+
function optionality(schema, required) {
|
|
835
|
+
const nullable = schema.nullable ?? false;
|
|
1514
836
|
return {
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
837
|
+
...schema,
|
|
838
|
+
optional: !required && !nullable ? true : void 0,
|
|
839
|
+
nullish: !required && nullable ? true : void 0
|
|
1518
840
|
};
|
|
1519
841
|
}
|
|
842
|
+
//#endregion
|
|
843
|
+
//#region src/nodes/parameter.ts
|
|
1520
844
|
/**
|
|
1521
|
-
*
|
|
1522
|
-
*
|
|
1523
|
-
* `required` defaults to `false`.
|
|
1524
|
-
* `schema.optional` and `schema.nullish` are derived from `required` and `schema.nullable`.
|
|
1525
|
-
*
|
|
1526
|
-
* @example
|
|
1527
|
-
* ```ts
|
|
1528
|
-
* const property = createProperty({
|
|
1529
|
-
* name: 'status',
|
|
1530
|
-
* schema: createSchema({ type: 'string' }),
|
|
1531
|
-
* })
|
|
1532
|
-
* // required=false, schema.optional=true
|
|
1533
|
-
* ```
|
|
1534
|
-
*
|
|
1535
|
-
* @example
|
|
1536
|
-
* ```ts
|
|
1537
|
-
* const property = createProperty({
|
|
1538
|
-
* name: 'status',
|
|
1539
|
-
* required: true,
|
|
1540
|
-
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1541
|
-
* })
|
|
1542
|
-
* // required=true, no optional/nullish
|
|
1543
|
-
* ```
|
|
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}.
|
|
1544
847
|
*/
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
}
|
|
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
|
+
});
|
|
1554
861
|
/**
|
|
1555
862
|
* Creates a `ParameterNode`.
|
|
1556
863
|
*
|
|
1557
|
-
* `required` defaults to `false`.
|
|
1558
|
-
* Nested schema flags are set from `required` and `schema.nullable`.
|
|
1559
|
-
*
|
|
1560
864
|
* @example
|
|
1561
865
|
* ```ts
|
|
1562
866
|
* const param = createParameter({
|
|
@@ -1566,26 +870,64 @@ function createProperty(props) {
|
|
|
1566
870
|
* schema: createSchema({ type: 'string' }),
|
|
1567
871
|
* })
|
|
1568
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`.
|
|
1569
896
|
*
|
|
1570
897
|
* @example
|
|
1571
898
|
* ```ts
|
|
1572
|
-
* const
|
|
899
|
+
* const property = createProperty({
|
|
1573
900
|
* name: 'status',
|
|
1574
|
-
*
|
|
901
|
+
* required: true,
|
|
1575
902
|
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1576
903
|
* })
|
|
1577
|
-
* // required=
|
|
904
|
+
* // required=true, no optional/nullish
|
|
1578
905
|
* ```
|
|
1579
906
|
*/
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
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
|
+
});
|
|
1589
931
|
/**
|
|
1590
932
|
* Creates a `ResponseNode`.
|
|
1591
933
|
*
|
|
@@ -1593,685 +935,689 @@ function createParameter(props) {
|
|
|
1593
935
|
* ```ts
|
|
1594
936
|
* const response = createResponse({
|
|
1595
937
|
* statusCode: '200',
|
|
1596
|
-
*
|
|
1597
|
-
* schema: createSchema({ type: 'object', properties: [] }),
|
|
938
|
+
* content: [createContent({ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) })],
|
|
1598
939
|
* })
|
|
1599
940
|
* ```
|
|
1600
941
|
*/
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
kind: "Response"
|
|
1605
|
-
};
|
|
1606
|
-
}
|
|
942
|
+
const createResponse = responseDef.create;
|
|
943
|
+
//#endregion
|
|
944
|
+
//#region src/nodes/schema.ts
|
|
1607
945
|
/**
|
|
1608
|
-
*
|
|
1609
|
-
*
|
|
1610
|
-
* `
|
|
1611
|
-
*
|
|
1612
|
-
* @example Required typed param
|
|
1613
|
-
* ```ts
|
|
1614
|
-
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }) })
|
|
1615
|
-
* // → petId: string
|
|
1616
|
-
* ```
|
|
1617
|
-
*
|
|
1618
|
-
* @example Optional param
|
|
1619
|
-
* ```ts
|
|
1620
|
-
* createFunctionParameter({ name: 'params', type: createParamsType({ variant: 'reference', name: 'QueryParams' }), optional: true })
|
|
1621
|
-
* // → params?: QueryParams
|
|
1622
|
-
* ```
|
|
1623
|
-
*
|
|
1624
|
-
* @example Param with default (implicitly optional; cannot combine with `optional: true`)
|
|
1625
|
-
* ```ts
|
|
1626
|
-
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), default: '{}' })
|
|
1627
|
-
* // → config: RequestConfig = {}
|
|
1628
|
-
* ```
|
|
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`.
|
|
1629
949
|
*/
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
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
|
+
};
|
|
1637
970
|
/**
|
|
1638
|
-
*
|
|
1639
|
-
*
|
|
1640
|
-
* Use `variant: 'struct'` for inline anonymous types and `variant: 'member'` for a single
|
|
1641
|
-
* named field accessed from a group type. Each language's printer renders the variant
|
|
1642
|
-
* into its own syntax (TypeScript, Python, C#, Kotlin, …).
|
|
1643
|
-
*
|
|
1644
|
-
* @example Reference type (TypeScript: `QueryParams`)
|
|
1645
|
-
* ```ts
|
|
1646
|
-
* createParamsType({ variant: 'reference', name: 'QueryParams' })
|
|
1647
|
-
* ```
|
|
1648
|
-
*
|
|
1649
|
-
* @example Struct type (TypeScript: `{ petId: string }`)
|
|
1650
|
-
* ```ts
|
|
1651
|
-
* createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
|
|
1652
|
-
* ```
|
|
1653
|
-
*
|
|
1654
|
-
* @example Member type (TypeScript: `DeletePetPathParams['petId']`)
|
|
1655
|
-
* ```ts
|
|
1656
|
-
* createParamsType({ variant: 'member', base: 'DeletePetPathParams', key: 'petId' })
|
|
1657
|
-
* ```
|
|
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.
|
|
1658
973
|
*/
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
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);
|
|
1664
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
|
|
1665
1028
|
/**
|
|
1666
|
-
*
|
|
1667
|
-
*
|
|
1668
|
-
* @example Grouped param (TypeScript declaration)
|
|
1669
|
-
* ```ts
|
|
1670
|
-
* createParameterGroup({
|
|
1671
|
-
* properties: [
|
|
1672
|
-
* createFunctionParameter({ name: 'id', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
1673
|
-
* createFunctionParameter({ name: 'name', type: createParamsType({ variant: 'reference', name: 'string' }), optional: true }),
|
|
1674
|
-
* ],
|
|
1675
|
-
* default: '{}',
|
|
1676
|
-
* })
|
|
1677
|
-
* // declaration → { id, name? }: { id: string; name?: string } = {}
|
|
1678
|
-
* // call → { id, name }
|
|
1679
|
-
* ```
|
|
1680
|
-
*
|
|
1681
|
-
* @example Inline (spread) — children emitted as individual top-level parameters
|
|
1682
|
-
* ```ts
|
|
1683
|
-
* createParameterGroup({
|
|
1684
|
-
* properties: [createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false })],
|
|
1685
|
-
* inline: true,
|
|
1686
|
-
* })
|
|
1687
|
-
* // declaration → petId: string
|
|
1688
|
-
* // call → petId
|
|
1689
|
-
* ```
|
|
1029
|
+
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
|
|
1030
|
+
* Derived from each definition's `children`.
|
|
1690
1031
|
*/
|
|
1691
|
-
|
|
1692
|
-
return {
|
|
1693
|
-
...props,
|
|
1694
|
-
kind: "ParameterGroup"
|
|
1695
|
-
};
|
|
1696
|
-
}
|
|
1032
|
+
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
|
|
1697
1033
|
/**
|
|
1698
|
-
*
|
|
1699
|
-
*
|
|
1700
|
-
* @example
|
|
1701
|
-
* ```ts
|
|
1702
|
-
* createFunctionParameters({
|
|
1703
|
-
* params: [
|
|
1704
|
-
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
1705
|
-
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), optional: false, default: '{}' }),
|
|
1706
|
-
* ],
|
|
1707
|
-
* })
|
|
1708
|
-
* ```
|
|
1709
|
-
*
|
|
1710
|
-
* @example
|
|
1711
|
-
* ```ts
|
|
1712
|
-
* const empty = createFunctionParameters()
|
|
1713
|
-
* // { kind: 'FunctionParameters', params: [] }
|
|
1714
|
-
* ```
|
|
1034
|
+
* Maps a node kind to the matching visitor callback name. Derived from each
|
|
1035
|
+
* definition's `visitorKey`.
|
|
1715
1036
|
*/
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
params: [],
|
|
1719
|
-
...props,
|
|
1720
|
-
kind: "FunctionParameters"
|
|
1721
|
-
};
|
|
1722
|
-
}
|
|
1037
|
+
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
|
|
1038
|
+
const visitorKeysByKind = VISITOR_KEYS;
|
|
1723
1039
|
/**
|
|
1724
|
-
*
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
* createImport({ name: ['useState'], path: 'react' })
|
|
1729
|
-
* // import { useState } from 'react'
|
|
1730
|
-
* ```
|
|
1731
|
-
*
|
|
1732
|
-
* @example Type-only import
|
|
1733
|
-
* ```ts
|
|
1734
|
-
* createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
|
|
1735
|
-
* // import type { FC } from 'react'
|
|
1736
|
-
* ```
|
|
1737
|
-
*/
|
|
1738
|
-
function createImport(props) {
|
|
1739
|
-
return {
|
|
1740
|
-
...props,
|
|
1741
|
-
kind: "Import"
|
|
1742
|
-
};
|
|
1040
|
+
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
|
|
1041
|
+
*/
|
|
1042
|
+
function isNode(value) {
|
|
1043
|
+
return typeof value === "object" && value !== null && typeof value.kind === "string";
|
|
1743
1044
|
}
|
|
1744
1045
|
/**
|
|
1745
|
-
*
|
|
1046
|
+
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
|
|
1746
1047
|
*
|
|
1747
|
-
*
|
|
1748
|
-
* ```ts
|
|
1749
|
-
* createExport({ name: ['Pet'], path: './Pet' })
|
|
1750
|
-
* // export { Pet } from './Pet'
|
|
1751
|
-
* ```
|
|
1048
|
+
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
|
|
1752
1049
|
*
|
|
1753
|
-
* @example
|
|
1050
|
+
* @example
|
|
1754
1051
|
* ```ts
|
|
1755
|
-
*
|
|
1756
|
-
* //
|
|
1757
|
-
* ```
|
|
1758
|
-
*/
|
|
1759
|
-
function
|
|
1760
|
-
return
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1052
|
+
* const children = getChildren(operationNode, true)
|
|
1053
|
+
* // returns parameters, the request body, and responses
|
|
1054
|
+
* ```
|
|
1055
|
+
*/
|
|
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
|
+
}
|
|
1764
1067
|
}
|
|
1765
1068
|
/**
|
|
1766
|
-
*
|
|
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.
|
|
1767
1072
|
*
|
|
1768
|
-
*
|
|
1769
|
-
*
|
|
1770
|
-
*
|
|
1771
|
-
* ```
|
|
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`.
|
|
1772
1076
|
*/
|
|
1773
|
-
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;
|
|
1774
1126
|
return {
|
|
1775
|
-
...
|
|
1776
|
-
|
|
1127
|
+
...node,
|
|
1128
|
+
...updates
|
|
1777
1129
|
};
|
|
1778
1130
|
}
|
|
1779
1131
|
/**
|
|
1780
|
-
*
|
|
1781
|
-
*
|
|
1782
|
-
* Computes:
|
|
1783
|
-
* - `id` — SHA256 hash of the file path
|
|
1784
|
-
* - `name` — `baseName` without extension
|
|
1785
|
-
* - `extname` — extension extracted from `baseName`
|
|
1786
|
-
*
|
|
1787
|
-
* Deduplicates:
|
|
1788
|
-
* - `sources` via `combineSources`
|
|
1789
|
-
* - `exports` via `combineExports`
|
|
1790
|
-
* - `imports` via `combineImports` (also filters unused imports)
|
|
1791
|
-
*
|
|
1792
|
-
* @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.
|
|
1793
1134
|
*
|
|
1794
|
-
* @example
|
|
1135
|
+
* @example Collect every operationId
|
|
1795
1136
|
* ```ts
|
|
1796
|
-
* const
|
|
1797
|
-
*
|
|
1798
|
-
*
|
|
1799
|
-
*
|
|
1800
|
-
*
|
|
1801
|
-
*
|
|
1802
|
-
*
|
|
1803
|
-
*
|
|
1804
|
-
* // file.name = 'petStore'
|
|
1805
|
-
* // 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
|
+
* }
|
|
1806
1145
|
* ```
|
|
1807
1146
|
*/
|
|
1808
|
-
function
|
|
1809
|
-
const
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
const
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
kind: "File",
|
|
1817
|
-
...input,
|
|
1818
|
-
id: (0, node_crypto.createHash)("sha256").update(input.path).digest("hex"),
|
|
1819
|
-
name: trimExtName(input.baseName),
|
|
1820
|
-
extname,
|
|
1821
|
-
imports: resolvedImports,
|
|
1822
|
-
exports: resolvedExports,
|
|
1823
|
-
sources: resolvedSources,
|
|
1824
|
-
meta: input.meta ?? {}
|
|
1825
|
-
};
|
|
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);
|
|
1826
1155
|
}
|
|
1827
1156
|
/**
|
|
1828
|
-
*
|
|
1829
|
-
*
|
|
1830
|
-
* Mirrors the `Const` component from `@kubb/renderer-jsx`.
|
|
1831
|
-
* The component's `children` are represented as `nodes`.
|
|
1832
|
-
*
|
|
1833
|
-
* @example Simple constant
|
|
1834
|
-
* ```ts
|
|
1835
|
-
* createConst({ name: 'pet' })
|
|
1836
|
-
* // const pet = ...
|
|
1837
|
-
* ```
|
|
1157
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
1158
|
+
* callbacks return into an array.
|
|
1838
1159
|
*
|
|
1839
|
-
* @example
|
|
1840
|
-
* ```ts
|
|
1841
|
-
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
1842
|
-
* // export const pets: Pet[] = ... as const
|
|
1843
|
-
* ```
|
|
1844
|
-
*
|
|
1845
|
-
* @example With JSDoc and child nodes
|
|
1160
|
+
* @example Collect every operationId
|
|
1846
1161
|
* ```ts
|
|
1847
|
-
*
|
|
1848
|
-
*
|
|
1849
|
-
*
|
|
1850
|
-
*
|
|
1851
|
-
* nodes: [],
|
|
1162
|
+
* const ids = collectSync<string>(root, {
|
|
1163
|
+
* operation(node) {
|
|
1164
|
+
* return node.operationId
|
|
1165
|
+
* },
|
|
1852
1166
|
* })
|
|
1853
1167
|
* ```
|
|
1854
1168
|
*/
|
|
1855
|
-
function
|
|
1856
|
-
return
|
|
1857
|
-
...props,
|
|
1858
|
-
kind: "Const"
|
|
1859
|
-
};
|
|
1169
|
+
function collectSync(node, options) {
|
|
1170
|
+
return Array.from(collect(node, options));
|
|
1860
1171
|
}
|
|
1172
|
+
//#endregion
|
|
1173
|
+
//#region src/defineMacro.ts
|
|
1861
1174
|
/**
|
|
1862
|
-
*
|
|
1863
|
-
*
|
|
1864
|
-
* Mirrors the `Type` component from `@kubb/renderer-jsx`.
|
|
1865
|
-
* The component's `children` are represented as `nodes`.
|
|
1866
|
-
*
|
|
1867
|
-
* @example Simple type alias
|
|
1868
|
-
* ```ts
|
|
1869
|
-
* createType({ name: 'Pet' })
|
|
1870
|
-
* // type Pet = ...
|
|
1871
|
-
* ```
|
|
1872
|
-
*
|
|
1873
|
-
* @example Exported type with JSDoc
|
|
1874
|
-
* ```ts
|
|
1875
|
-
* createType({
|
|
1876
|
-
* name: 'PetStatus',
|
|
1877
|
-
* export: true,
|
|
1878
|
-
* JSDoc: { comments: ['@description Status of a pet'] },
|
|
1879
|
-
* })
|
|
1880
|
-
* // export type PetStatus = ...
|
|
1881
|
-
* ```
|
|
1175
|
+
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
|
|
1176
|
+
* list keeps its authored order.
|
|
1882
1177
|
*/
|
|
1883
|
-
function
|
|
1884
|
-
return
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
};
|
|
1178
|
+
function enforceWeight(enforce) {
|
|
1179
|
+
if (enforce === "pre") return 0;
|
|
1180
|
+
if (enforce === "post") return 2;
|
|
1181
|
+
return 1;
|
|
1888
1182
|
}
|
|
1889
1183
|
/**
|
|
1890
|
-
*
|
|
1184
|
+
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
1185
|
+
* Adds no runtime behavior.
|
|
1891
1186
|
*
|
|
1892
|
-
*
|
|
1893
|
-
* The component's `children` are represented as `nodes`.
|
|
1894
|
-
*
|
|
1895
|
-
* @example Simple function
|
|
1896
|
-
* ```ts
|
|
1897
|
-
* createFunction({ name: 'getPet' })
|
|
1898
|
-
* // function getPet() { ... }
|
|
1899
|
-
* ```
|
|
1900
|
-
*
|
|
1901
|
-
* @example Exported async function with return type
|
|
1902
|
-
* ```ts
|
|
1903
|
-
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
1904
|
-
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
1905
|
-
* ```
|
|
1906
|
-
*
|
|
1907
|
-
* @example Function with generics and params
|
|
1187
|
+
* @example
|
|
1908
1188
|
* ```ts
|
|
1909
|
-
*
|
|
1910
|
-
* name: '
|
|
1911
|
-
*
|
|
1912
|
-
*
|
|
1913
|
-
*
|
|
1914
|
-
* returnType: 'T',
|
|
1189
|
+
* const macroUntagged = defineMacro({
|
|
1190
|
+
* name: 'untagged',
|
|
1191
|
+
* operation(node) {
|
|
1192
|
+
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
1193
|
+
* },
|
|
1915
1194
|
* })
|
|
1916
|
-
* // export function identity<T>(value: T): T { ... }
|
|
1917
1195
|
* ```
|
|
1918
1196
|
*/
|
|
1919
|
-
function
|
|
1920
|
-
return
|
|
1921
|
-
...props,
|
|
1922
|
-
kind: "Function"
|
|
1923
|
-
};
|
|
1197
|
+
function defineMacro(macro) {
|
|
1198
|
+
return macro;
|
|
1924
1199
|
}
|
|
1925
1200
|
/**
|
|
1926
|
-
*
|
|
1927
|
-
*
|
|
1928
|
-
*
|
|
1929
|
-
* The component's `children` are represented as `nodes`.
|
|
1930
|
-
*
|
|
1931
|
-
* @example Simple arrow function
|
|
1932
|
-
* ```ts
|
|
1933
|
-
* createArrowFunction({ name: 'getPet' })
|
|
1934
|
-
* // const getPet = () => { ... }
|
|
1935
|
-
* ```
|
|
1936
|
-
*
|
|
1937
|
-
* @example Single-line exported arrow function
|
|
1938
|
-
* ```ts
|
|
1939
|
-
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
1940
|
-
* // export const double = (n: number) => ...
|
|
1941
|
-
* ```
|
|
1942
|
-
*
|
|
1943
|
-
* @example Async arrow function with generics
|
|
1944
|
-
* ```ts
|
|
1945
|
-
* createArrowFunction({
|
|
1946
|
-
* name: 'fetchPet',
|
|
1947
|
-
* export: true,
|
|
1948
|
-
* async: true,
|
|
1949
|
-
* generics: ['T'],
|
|
1950
|
-
* params: 'id: string',
|
|
1951
|
-
* returnType: 'T',
|
|
1952
|
-
* })
|
|
1953
|
-
* // export const fetchPet = async <T>(id: string): Promise<T> => { ... }
|
|
1954
|
-
* ```
|
|
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).
|
|
1955
1204
|
*/
|
|
1956
|
-
function
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
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;
|
|
1961
1215
|
}
|
|
1962
1216
|
/**
|
|
1963
|
-
*
|
|
1964
|
-
*
|
|
1965
|
-
*
|
|
1966
|
-
*
|
|
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.
|
|
1967
1221
|
*
|
|
1968
1222
|
* @example
|
|
1969
1223
|
* ```ts
|
|
1970
|
-
*
|
|
1971
|
-
*
|
|
1224
|
+
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
1225
|
+
* const next = transform(root, visitor)
|
|
1972
1226
|
* ```
|
|
1973
1227
|
*/
|
|
1974
|
-
function
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
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;
|
|
1979
1242
|
}
|
|
1980
1243
|
/**
|
|
1981
|
-
*
|
|
1982
|
-
*
|
|
1983
|
-
*
|
|
1984
|
-
* 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.
|
|
1985
1247
|
*
|
|
1986
1248
|
* @example
|
|
1987
1249
|
* ```ts
|
|
1988
|
-
*
|
|
1989
|
-
* // { kind: 'Break' }
|
|
1250
|
+
* const next = applyMacros(root, [macroIntegerToString])
|
|
1990
1251
|
* ```
|
|
1991
|
-
*/
|
|
1992
|
-
function createBreak() {
|
|
1993
|
-
return { kind: "Break" };
|
|
1994
|
-
}
|
|
1995
|
-
/**
|
|
1996
|
-
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
1997
|
-
*
|
|
1998
|
-
* Use this to embed JSX markup (including fragments `<>…</>`) directly in generated code.
|
|
1999
1252
|
*
|
|
2000
|
-
* @example
|
|
1253
|
+
* @example Apply to the root node only
|
|
2001
1254
|
* ```ts
|
|
2002
|
-
*
|
|
2003
|
-
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
1255
|
+
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
2004
1256
|
* ```
|
|
2005
1257
|
*/
|
|
2006
|
-
function
|
|
2007
|
-
return
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
1258
|
+
function applyMacros(root, macros, options) {
|
|
1259
|
+
if (macros.length === 0) return root;
|
|
1260
|
+
return transform(root, {
|
|
1261
|
+
...composeMacros(macros),
|
|
1262
|
+
...options
|
|
1263
|
+
});
|
|
2011
1264
|
}
|
|
2012
1265
|
//#endregion
|
|
2013
|
-
//#region src/
|
|
1266
|
+
//#region src/createPrinter.ts
|
|
2014
1267
|
/**
|
|
2015
|
-
* Creates a schema printer
|
|
2016
|
-
*
|
|
2017
|
-
*
|
|
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.
|
|
2018
1272
|
*
|
|
2019
1273
|
* The builder receives resolved options and returns:
|
|
2020
|
-
* - `name` — a unique identifier for the printer
|
|
2021
|
-
* - `options` — options stored on the returned printer instance
|
|
2022
|
-
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
2023
|
-
* - `print` _(optional)_ — top-level override exposed as `printer.print`
|
|
2024
|
-
* - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
|
|
2025
|
-
* - This keeps recursion safe and avoids self-calls
|
|
2026
1274
|
*
|
|
2027
|
-
*
|
|
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).
|
|
2028
1286
|
*
|
|
2029
|
-
* @example
|
|
1287
|
+
* @example Tiny Zod printer
|
|
2030
1288
|
* ```ts
|
|
1289
|
+
* import { createPrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
1290
|
+
*
|
|
2031
1291
|
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
2032
1292
|
*
|
|
2033
|
-
* export const zodPrinter =
|
|
1293
|
+
* export const zodPrinter = createPrinter<PrinterZod>((options) => ({
|
|
2034
1294
|
* name: 'zod',
|
|
2035
1295
|
* options: { strict: options.strict ?? true },
|
|
2036
1296
|
* nodes: {
|
|
2037
1297
|
* string: () => 'z.string()',
|
|
2038
1298
|
* object(node) {
|
|
2039
|
-
* const props = node.properties
|
|
1299
|
+
* const props = node.properties
|
|
1300
|
+
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
1301
|
+
* .join(', ')
|
|
2040
1302
|
* return `z.object({ ${props} })`
|
|
2041
1303
|
* },
|
|
2042
1304
|
* },
|
|
2043
1305
|
* }))
|
|
2044
1306
|
* ```
|
|
2045
1307
|
*/
|
|
2046
|
-
function
|
|
2047
|
-
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
|
+
};
|
|
2048
1335
|
}
|
|
1336
|
+
//#endregion
|
|
1337
|
+
//#region src/utils/refs.ts
|
|
2049
1338
|
/**
|
|
2050
|
-
*
|
|
2051
|
-
|
|
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
|
+
*
|
|
2052
1345
|
* @example
|
|
2053
|
-
*
|
|
2054
|
-
*
|
|
2055
|
-
*
|
|
2056
|
-
* )
|
|
2057
|
-
* ```
|
|
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'`
|
|
2058
1350
|
*/
|
|
2059
|
-
function
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
options: resolvedOptions,
|
|
2065
|
-
transform: (node) => {
|
|
2066
|
-
const key = getKey(node);
|
|
2067
|
-
if (key === void 0) return null;
|
|
2068
|
-
const handler = nodes[key];
|
|
2069
|
-
if (!handler) return null;
|
|
2070
|
-
return handler.call(context, node);
|
|
2071
|
-
}
|
|
2072
|
-
};
|
|
2073
|
-
return {
|
|
2074
|
-
name,
|
|
2075
|
-
options: resolvedOptions,
|
|
2076
|
-
transform: context.transform,
|
|
2077
|
-
print: printOverride ? printOverride.bind(context) : context.transform
|
|
2078
|
-
};
|
|
2079
|
-
};
|
|
2080
|
-
};
|
|
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;
|
|
2081
1356
|
}
|
|
2082
1357
|
//#endregion
|
|
2083
|
-
//#region src/
|
|
2084
|
-
function findDiscriminator(mapping, ref) {
|
|
2085
|
-
if (!mapping || !ref) return null;
|
|
2086
|
-
return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null;
|
|
2087
|
-
}
|
|
2088
|
-
function childName(parentName, propName) {
|
|
2089
|
-
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
|
|
2090
|
-
}
|
|
2091
|
-
function enumPropName(parentName, propName, enumSuffix) {
|
|
2092
|
-
return pascalCase([
|
|
2093
|
-
parentName,
|
|
2094
|
-
propName,
|
|
2095
|
-
enumSuffix
|
|
2096
|
-
].filter(Boolean).join(" "));
|
|
2097
|
-
}
|
|
1358
|
+
//#region src/utils/schemaGraph.ts
|
|
2098
1359
|
/**
|
|
2099
|
-
*
|
|
1360
|
+
* Memoized inner pass that walks a single node and returns the names of every schema it references.
|
|
2100
1361
|
*/
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
if (
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
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
|
+
}
|
|
2109
1369
|
} });
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
//#region src/transformers.ts
|
|
1370
|
+
return refs;
|
|
1371
|
+
});
|
|
2113
1372
|
/**
|
|
2114
|
-
*
|
|
1373
|
+
* Collects the names of every ref found anywhere inside a node's own subtree.
|
|
2115
1374
|
*
|
|
2116
|
-
*
|
|
2117
|
-
*
|
|
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.
|
|
2118
1377
|
*
|
|
2119
|
-
* @example
|
|
1378
|
+
* @example Collect refs from a single schema
|
|
2120
1379
|
* ```ts
|
|
2121
|
-
* const
|
|
2122
|
-
*
|
|
2123
|
-
*
|
|
2124
|
-
*
|
|
2125
|
-
*
|
|
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
|
+
* }
|
|
2126
1390
|
* ```
|
|
2127
1391
|
*/
|
|
2128
|
-
function
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
});
|
|
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;
|
|
2149
1414
|
}
|
|
2150
1415
|
/**
|
|
2151
|
-
*
|
|
1416
|
+
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
2152
1417
|
*
|
|
2153
|
-
*
|
|
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
|
|
2154
1425
|
* ```ts
|
|
2155
|
-
* const
|
|
2156
|
-
*
|
|
2157
|
-
*
|
|
2158
|
-
*
|
|
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
|
+
* }
|
|
2159
1433
|
* ```
|
|
2160
1434
|
*/
|
|
2161
|
-
function
|
|
2162
|
-
return
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
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;
|
|
2173
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);
|
|
2174
1459
|
}
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
}
|
|
1460
|
+
}
|
|
1461
|
+
return circular;
|
|
1462
|
+
});
|
|
2179
1463
|
/**
|
|
2180
|
-
*
|
|
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.
|
|
2181
1510
|
*
|
|
2182
1511
|
* @example
|
|
2183
1512
|
* ```ts
|
|
2184
|
-
*
|
|
2185
|
-
*
|
|
2186
|
-
* createSchema({ type: 'string' }),
|
|
2187
|
-
* ])
|
|
2188
|
-
* // keeps only string member
|
|
1513
|
+
* update(node, { name: node.name }) // -> same `node` reference
|
|
1514
|
+
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
|
|
2189
1515
|
* ```
|
|
2190
1516
|
*/
|
|
2191
|
-
function
|
|
2192
|
-
const
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
const enumNode = narrowSchema(member, "enum");
|
|
2196
|
-
if (!enumNode) return true;
|
|
2197
|
-
const primitive = enumNode.primitive;
|
|
2198
|
-
if (!primitive) return true;
|
|
2199
|
-
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
|
|
2200
|
-
if (scalarPrimitives.has(primitive)) return false;
|
|
2201
|
-
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
|
|
2202
|
-
return true;
|
|
2203
|
-
});
|
|
2204
|
-
}
|
|
2205
|
-
function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
2206
|
-
const enumNode = narrowSchema(propNode, "enum");
|
|
2207
|
-
if (enumNode?.primitive === "boolean") return {
|
|
2208
|
-
...propNode,
|
|
2209
|
-
name: void 0
|
|
1517
|
+
function update(node, changes) {
|
|
1518
|
+
for (const key in changes) if (changes[key] !== node[key]) return {
|
|
1519
|
+
...node,
|
|
1520
|
+
...changes
|
|
2210
1521
|
};
|
|
2211
|
-
|
|
2212
|
-
...propNode,
|
|
2213
|
-
name: enumPropName(parentName, propName, enumSuffix)
|
|
2214
|
-
};
|
|
2215
|
-
return propNode;
|
|
1522
|
+
return node;
|
|
2216
1523
|
}
|
|
2217
1524
|
//#endregion
|
|
2218
|
-
exports.
|
|
2219
|
-
|
|
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;
|
|
2220
1579
|
exports.collect = collect;
|
|
2221
|
-
exports.
|
|
2222
|
-
exports.collectReferencedSchemaNames = collectReferencedSchemaNames;
|
|
1580
|
+
exports.collectSync = collectSync;
|
|
2223
1581
|
exports.collectUsedSchemaNames = collectUsedSchemaNames;
|
|
2224
|
-
exports.
|
|
2225
|
-
exports.
|
|
2226
|
-
exports.
|
|
2227
|
-
exports.
|
|
2228
|
-
exports.
|
|
2229
|
-
exports.
|
|
2230
|
-
exports.
|
|
2231
|
-
exports.
|
|
2232
|
-
exports.
|
|
2233
|
-
exports.
|
|
2234
|
-
exports.createImport = createImport;
|
|
2235
|
-
exports.createInput = createInput;
|
|
2236
|
-
exports.createJsx = createJsx;
|
|
2237
|
-
exports.createOperation = createOperation;
|
|
2238
|
-
exports.createOperationParams = createOperationParams;
|
|
2239
|
-
exports.createOutput = createOutput;
|
|
2240
|
-
exports.createParameter = createParameter;
|
|
2241
|
-
exports.createParameterGroup = createParameterGroup;
|
|
2242
|
-
exports.createParamsType = createParamsType;
|
|
2243
|
-
exports.createPrinterFactory = createPrinterFactory;
|
|
2244
|
-
exports.createProperty = createProperty;
|
|
2245
|
-
exports.createResponse = createResponse;
|
|
2246
|
-
exports.createSchema = createSchema;
|
|
2247
|
-
exports.createSource = createSource;
|
|
2248
|
-
exports.createText = createText;
|
|
2249
|
-
exports.createType = createType;
|
|
2250
|
-
exports.definePrinter = definePrinter;
|
|
2251
|
-
exports.enumPropName = enumPropName;
|
|
2252
|
-
exports.extractRefName = extractRefName;
|
|
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;
|
|
2253
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;
|
|
2254
1600
|
exports.findCircularSchemas = findCircularSchemas;
|
|
2255
|
-
exports.
|
|
2256
|
-
exports.
|
|
2257
|
-
exports.
|
|
2258
|
-
exports.
|
|
2259
|
-
exports.
|
|
2260
|
-
exports.isScalarPrimitive = isScalarPrimitive;
|
|
2261
|
-
exports.isSchemaNode = isSchemaNode;
|
|
2262
|
-
exports.isStringType = isStringType;
|
|
2263
|
-
exports.mediaTypes = mediaTypes;
|
|
2264
|
-
exports.mergeAdjacentObjects = mergeAdjacentObjects;
|
|
1601
|
+
exports.functionDef = functionDef;
|
|
1602
|
+
exports.importDef = importDef;
|
|
1603
|
+
exports.inputDef = inputDef;
|
|
1604
|
+
exports.isHttpOperationNode = isHttpOperationNode;
|
|
1605
|
+
exports.jsxDef = jsxDef;
|
|
2265
1606
|
exports.narrowSchema = narrowSchema;
|
|
2266
|
-
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;
|
|
2267
1614
|
exports.resolveRefName = resolveRefName;
|
|
1615
|
+
exports.responseDef = responseDef;
|
|
1616
|
+
exports.schemaDef = schemaDef;
|
|
2268
1617
|
exports.schemaTypes = schemaTypes;
|
|
2269
|
-
exports.
|
|
2270
|
-
exports.
|
|
2271
|
-
exports.simplifyUnion = simplifyUnion;
|
|
2272
|
-
exports.syncOptionality = syncOptionality;
|
|
2273
|
-
exports.syncSchemaRef = syncSchemaRef;
|
|
1618
|
+
exports.sourceDef = sourceDef;
|
|
1619
|
+
exports.textDef = textDef;
|
|
2274
1620
|
exports.transform = transform;
|
|
2275
|
-
exports.
|
|
1621
|
+
exports.typeDef = typeDef;
|
|
2276
1622
|
|
|
2277
1623
|
//# sourceMappingURL=index.cjs.map
|