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