@kubb/ast 5.0.0-beta.56 → 5.0.0-beta.58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -9
- package/dist/{types-BL7RpQAE.d.ts → ast-ClnJg9BN.d.ts} +1630 -2443
- package/dist/chunk-CNktS9qV.js +17 -0
- package/dist/extractStringsFromNodes-Bn9cOos9.d.ts +14 -0
- package/dist/factory-C5gHvtLU.js +138 -0
- package/dist/factory-C5gHvtLU.js.map +1 -0
- package/dist/factory-JN-Ylfl6.cjs +155 -0
- package/dist/factory-JN-Ylfl6.cjs.map +1 -0
- package/dist/factory.cjs +31 -0
- package/dist/factory.d.ts +62 -0
- package/dist/factory.js +3 -0
- package/dist/index.cjs +56 -1751
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -47
- package/dist/index.js +7 -1697
- package/dist/index.js.map +1 -1
- package/dist/types-CB2oY8Dw.d.ts +769 -0
- package/dist/types.d.ts +4 -2
- package/dist/utils-C8bWAzhv.cjs +2696 -0
- package/dist/utils-C8bWAzhv.cjs.map +1 -0
- package/dist/utils-DN4XLVqz.js +2099 -0
- package/dist/utils-DN4XLVqz.js.map +1 -0
- package/dist/utils.cjs +12 -1
- package/dist/utils.d.ts +21 -2
- package/dist/utils.js +2 -2
- package/package.json +5 -1
- package/src/dedupe.ts +1 -1
- package/src/factory.ts +22 -764
- package/src/guards.ts +1 -53
- package/src/index.ts +20 -39
- package/src/mocks.ts +6 -1
- package/src/node.ts +128 -0
- package/src/nodes/base.ts +3 -12
- package/src/nodes/code.ts +115 -0
- package/src/nodes/content.ts +19 -0
- package/src/nodes/file.ts +54 -0
- package/src/nodes/function.ts +222 -147
- package/src/nodes/index.ts +11 -3
- package/src/nodes/input.ts +37 -0
- package/src/nodes/operation.ts +59 -1
- package/src/nodes/output.ts +23 -0
- package/src/nodes/parameter.ts +33 -0
- package/src/nodes/property.ts +36 -0
- package/src/nodes/requestBody.ts +23 -1
- package/src/nodes/response.ts +39 -1
- package/src/nodes/schema.ts +72 -0
- package/src/printer.ts +3 -3
- package/src/registry.ts +70 -0
- package/src/transformers.ts +2 -2
- package/src/types.ts +6 -4
- package/src/utils/ast.ts +103 -243
- package/src/utils/extractStringsFromNodes.ts +34 -0
- package/src/utils/index.ts +44 -0
- package/src/visitor.ts +3 -47
- package/dist/chunk-C0LytTxp.js +0 -8
- package/dist/utils-0p8ZO287.js +0 -570
- package/dist/utils-0p8ZO287.js.map +0 -1
- package/dist/utils-cdQ6Pzyi.cjs +0 -726
- package/dist/utils-cdQ6Pzyi.cjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,1698 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_utils = require("./utils-
|
|
2
|
+
const require_utils = require("./utils-C8bWAzhv.cjs");
|
|
3
|
+
const require_factory = require("./factory-JN-Ylfl6.cjs");
|
|
3
4
|
let node_crypto = require("node:crypto");
|
|
4
|
-
let node_path = require("node:path");
|
|
5
|
-
node_path = require_utils.__toESM(node_path, 1);
|
|
6
|
-
//#region ../../internals/utils/src/fs.ts
|
|
7
|
-
/**
|
|
8
|
-
* Strips the file extension from a path or file name.
|
|
9
|
-
* Only removes the last `.ext` segment when the dot is not part of a directory name.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* trimExtName('petStore.ts') // 'petStore'
|
|
13
|
-
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
|
|
14
|
-
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
|
|
15
|
-
* trimExtName('noExtension') // 'noExtension'
|
|
16
|
-
*/
|
|
17
|
-
function trimExtName(text) {
|
|
18
|
-
const dotIndex = text.lastIndexOf(".");
|
|
19
|
-
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
20
|
-
return text;
|
|
21
|
-
}
|
|
22
|
-
//#endregion
|
|
23
|
-
//#region ../../internals/utils/src/promise.ts
|
|
24
|
-
/**
|
|
25
|
-
* Wraps `factory` with a keyed cache backed by the provided store.
|
|
26
|
-
*
|
|
27
|
-
* Pass a `WeakMap` for object keys (results are GC-eligible when the key is
|
|
28
|
-
* collected) or a `Map` for primitive keys. For multi-argument functions,
|
|
29
|
-
* nest two `memoize` calls — the outer keyed by the first argument, the
|
|
30
|
-
* inner (created once per outer miss) keyed by the second.
|
|
31
|
-
*
|
|
32
|
-
* Because the cache is owned by the caller, it can be shared, inspected, or
|
|
33
|
-
* cleared independently of the memoized function.
|
|
34
|
-
*
|
|
35
|
-
* @example Single WeakMap key
|
|
36
|
-
* ```ts
|
|
37
|
-
* const cache = new WeakMap<SchemaNode, Set<string>>()
|
|
38
|
-
* const getRefs = memoize(cache, (node) => collectRefs(node))
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @example Single Map key (primitive)
|
|
42
|
-
* ```ts
|
|
43
|
-
* const cache = new Map<string, Resolver>()
|
|
44
|
-
* const getResolver = memoize(cache, (name) => buildResolver(name))
|
|
45
|
-
* ```
|
|
46
|
-
*
|
|
47
|
-
* @example Two-level (object + primitive)
|
|
48
|
-
* ```ts
|
|
49
|
-
* const outer = new WeakMap<Params[], Map<string, Params[]>>()
|
|
50
|
-
* const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
|
|
51
|
-
* fn(params)('camelcase')
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
function memoize(store, factory) {
|
|
55
|
-
return (key) => {
|
|
56
|
-
if (store.has(key)) return store.get(key);
|
|
57
|
-
const value = factory(key);
|
|
58
|
-
store.set(key, value);
|
|
59
|
-
return value;
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
//#endregion
|
|
63
|
-
//#region src/guards.ts
|
|
64
|
-
/**
|
|
65
|
-
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* const schema = createSchema({ type: 'string' })
|
|
70
|
-
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
function narrowSchema(node, type) {
|
|
74
|
-
return node?.type === type ? node : null;
|
|
75
|
-
}
|
|
76
|
-
function isKind(kind) {
|
|
77
|
-
return (node) => node.kind === kind;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Returns `true` when the input is an `InputNode`.
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* ```ts
|
|
84
|
-
* if (isInputNode(node)) {
|
|
85
|
-
* console.log(node.schemas.length)
|
|
86
|
-
* }
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
const isInputNode = isKind("Input");
|
|
90
|
-
/**
|
|
91
|
-
* Returns `true` when the input is an `OutputNode`.
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```ts
|
|
95
|
-
* if (isOutputNode(node)) {
|
|
96
|
-
* console.log(node.files.length)
|
|
97
|
-
* }
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
const isOutputNode = isKind("Output");
|
|
101
|
-
/**
|
|
102
|
-
* Returns `true` when the input is an `OperationNode`.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```ts
|
|
106
|
-
* if (isOperationNode(node)) {
|
|
107
|
-
* console.log(node.operationId)
|
|
108
|
-
* }
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
|
-
const isOperationNode = isKind("Operation");
|
|
112
|
-
/**
|
|
113
|
-
* Narrows an `OperationNode` to an `HttpOperationNode`, guaranteeing `method` and `path`.
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```ts
|
|
117
|
-
* if (isHttpOperationNode(node)) {
|
|
118
|
-
* console.log(node.method, node.path)
|
|
119
|
-
* }
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
function isHttpOperationNode(node) {
|
|
123
|
-
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Returns `true` when the input is a `SchemaNode`.
|
|
127
|
-
*
|
|
128
|
-
* @example
|
|
129
|
-
* ```ts
|
|
130
|
-
* if (isSchemaNode(node)) {
|
|
131
|
-
* console.log(node.type)
|
|
132
|
-
* }
|
|
133
|
-
* ```
|
|
134
|
-
*/
|
|
135
|
-
const isSchemaNode = isKind("Schema");
|
|
136
|
-
//#endregion
|
|
137
|
-
//#region src/visitor.ts
|
|
138
|
-
/**
|
|
139
|
-
* Creates a small async concurrency limiter.
|
|
140
|
-
*
|
|
141
|
-
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* ```ts
|
|
145
|
-
* const limit = createLimit(2)
|
|
146
|
-
* for (const task of [taskA, taskB, taskC]) {
|
|
147
|
-
* await limit(() => task())
|
|
148
|
-
* }
|
|
149
|
-
* // only 2 tasks run at the same time
|
|
150
|
-
* ```
|
|
151
|
-
*/
|
|
152
|
-
function createLimit(concurrency) {
|
|
153
|
-
let active = 0;
|
|
154
|
-
const queue = [];
|
|
155
|
-
function next() {
|
|
156
|
-
if (active < concurrency && queue.length > 0) {
|
|
157
|
-
active++;
|
|
158
|
-
queue.shift()();
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return function limit(fn) {
|
|
162
|
-
return new Promise((resolve, reject) => {
|
|
163
|
-
queue.push(() => {
|
|
164
|
-
Promise.resolve(fn()).then(resolve, reject).finally(() => {
|
|
165
|
-
active--;
|
|
166
|
-
next();
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
next();
|
|
170
|
-
});
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
const visitorKeysByKind = {
|
|
174
|
-
Input: ["schemas", "operations"],
|
|
175
|
-
Operation: [
|
|
176
|
-
"parameters",
|
|
177
|
-
"requestBody",
|
|
178
|
-
"responses"
|
|
179
|
-
],
|
|
180
|
-
RequestBody: ["content"],
|
|
181
|
-
Content: ["schema"],
|
|
182
|
-
Response: ["content"],
|
|
183
|
-
Schema: [
|
|
184
|
-
"properties",
|
|
185
|
-
"items",
|
|
186
|
-
"members",
|
|
187
|
-
"additionalProperties"
|
|
188
|
-
],
|
|
189
|
-
Property: ["schema"],
|
|
190
|
-
Parameter: ["schema"]
|
|
191
|
-
};
|
|
192
|
-
/**
|
|
193
|
-
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
|
|
194
|
-
*/
|
|
195
|
-
function isNode(value) {
|
|
196
|
-
return typeof value === "object" && value !== null && "kind" in value;
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
|
|
200
|
-
*
|
|
201
|
-
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
|
|
202
|
-
*
|
|
203
|
-
* @example
|
|
204
|
-
* ```ts
|
|
205
|
-
* const children = getChildren(operationNode, true)
|
|
206
|
-
* // returns parameters, the request body, and responses
|
|
207
|
-
* ```
|
|
208
|
-
*/
|
|
209
|
-
function* getChildren(node, recurse) {
|
|
210
|
-
if (node.kind === "Schema" && !recurse) return;
|
|
211
|
-
const keys = visitorKeysByKind[node.kind];
|
|
212
|
-
if (!keys) return;
|
|
213
|
-
const record = node;
|
|
214
|
-
for (const key of keys) {
|
|
215
|
-
const value = record[key];
|
|
216
|
-
if (Array.isArray(value)) {
|
|
217
|
-
for (const item of value) if (isNode(item)) yield item;
|
|
218
|
-
} else if (isNode(value)) yield value;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Maps a node `kind` to the matching visitor callback name. Only the seven
|
|
223
|
-
* traversable node kinds have an entry. Every other kind resolves to
|
|
224
|
-
* `undefined` and is skipped.
|
|
225
|
-
*/
|
|
226
|
-
const VISITOR_KEY_BY_KIND = {
|
|
227
|
-
Input: "input",
|
|
228
|
-
Output: "output",
|
|
229
|
-
Operation: "operation",
|
|
230
|
-
Schema: "schema",
|
|
231
|
-
Property: "property",
|
|
232
|
-
Parameter: "parameter",
|
|
233
|
-
Response: "response"
|
|
234
|
-
};
|
|
235
|
-
/**
|
|
236
|
-
* Invokes the visitor callback that matches `node.kind`, passing the traversal
|
|
237
|
-
* context. Returns the callback's result (a replacement node, a collected
|
|
238
|
-
* value, or `undefined` when no callback is registered for the kind).
|
|
239
|
-
*
|
|
240
|
-
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
|
|
241
|
-
* in one place. `TResult` is the caller's expected return: the same node type
|
|
242
|
-
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
|
|
243
|
-
*/
|
|
244
|
-
function applyVisitor(node, visitor, parent) {
|
|
245
|
-
const key = VISITOR_KEY_BY_KIND[node.kind];
|
|
246
|
-
if (!key) return void 0;
|
|
247
|
-
const fn = visitor[key];
|
|
248
|
-
return fn?.(node, { parent });
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Async depth-first traversal for side effects. Visitor return values are
|
|
252
|
-
* ignored. Use `transform` when you want to rewrite nodes.
|
|
253
|
-
*
|
|
254
|
-
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
255
|
-
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
256
|
-
* work. Lower values reduce memory pressure.
|
|
257
|
-
*
|
|
258
|
-
* @example Log every operation
|
|
259
|
-
* ```ts
|
|
260
|
-
* await walk(root, {
|
|
261
|
-
* operation(node) {
|
|
262
|
-
* console.log(node.operationId)
|
|
263
|
-
* },
|
|
264
|
-
* })
|
|
265
|
-
* ```
|
|
266
|
-
*
|
|
267
|
-
* @example Only visit the root node
|
|
268
|
-
* ```ts
|
|
269
|
-
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
270
|
-
* ```
|
|
271
|
-
*/
|
|
272
|
-
async function walk(node, options) {
|
|
273
|
-
return _walk(node, options, (options.depth ?? require_utils.visitorDepths.deep) === require_utils.visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
|
|
274
|
-
}
|
|
275
|
-
async function _walk(node, visitor, recurse, limit, parent) {
|
|
276
|
-
await limit(() => applyVisitor(node, visitor, parent));
|
|
277
|
-
const children = Array.from(getChildren(node, recurse));
|
|
278
|
-
if (children.length === 0) return;
|
|
279
|
-
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
|
|
280
|
-
}
|
|
281
|
-
function transform(node, options) {
|
|
282
|
-
const { depth, parent, ...visitor } = options;
|
|
283
|
-
const recurse = (depth ?? require_utils.visitorDepths.deep) === require_utils.visitorDepths.deep;
|
|
284
|
-
const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
|
|
285
|
-
if (rebuilt === node) return node;
|
|
286
|
-
const finalize = nodeFinalizers[rebuilt.kind];
|
|
287
|
-
return finalize ? finalize(rebuilt) : rebuilt;
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Per-kind builders rerun after children are rebuilt. `Property`/`Parameter`
|
|
291
|
-
* resync schema optionality against their `required` flag once the schema may
|
|
292
|
-
* have changed.
|
|
293
|
-
*/
|
|
294
|
-
const nodeFinalizers = {
|
|
295
|
-
Property: (node) => createProperty(node),
|
|
296
|
-
Parameter: (node) => createParameter(node)
|
|
297
|
-
};
|
|
298
|
-
/**
|
|
299
|
-
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
|
|
300
|
-
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
|
|
301
|
-
* `Schema` children are skipped in shallow mode.
|
|
302
|
-
*/
|
|
303
|
-
function transformChildren(node, options, recurse) {
|
|
304
|
-
if (node.kind === "Schema" && !recurse) return node;
|
|
305
|
-
const keys = visitorKeysByKind[node.kind];
|
|
306
|
-
if (!keys) return node;
|
|
307
|
-
const record = node;
|
|
308
|
-
const childOptions = {
|
|
309
|
-
...options,
|
|
310
|
-
parent: node
|
|
311
|
-
};
|
|
312
|
-
let updates;
|
|
313
|
-
for (const key of keys) {
|
|
314
|
-
if (!(key in record)) continue;
|
|
315
|
-
const value = record[key];
|
|
316
|
-
if (Array.isArray(value)) {
|
|
317
|
-
let changed = false;
|
|
318
|
-
const mapped = value.map((item) => {
|
|
319
|
-
if (!isNode(item)) return item;
|
|
320
|
-
const next = transform(item, childOptions);
|
|
321
|
-
if (next !== item) changed = true;
|
|
322
|
-
return next;
|
|
323
|
-
});
|
|
324
|
-
if (changed) (updates ??= {})[key] = mapped;
|
|
325
|
-
} else if (isNode(value)) {
|
|
326
|
-
const next = transform(value, childOptions);
|
|
327
|
-
if (next !== value) (updates ??= {})[key] = next;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
return updates ? {
|
|
331
|
-
...node,
|
|
332
|
-
...updates
|
|
333
|
-
} : node;
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Lazy depth-first collection pass. Yields every non-null value returned by
|
|
337
|
-
* the visitor callbacks. Use `collect` for the eager array form.
|
|
338
|
-
*
|
|
339
|
-
* @example Collect every operationId
|
|
340
|
-
* ```ts
|
|
341
|
-
* const ids: string[] = []
|
|
342
|
-
* for (const id of collectLazy<string>(root, {
|
|
343
|
-
* operation(node) {
|
|
344
|
-
* return node.operationId
|
|
345
|
-
* },
|
|
346
|
-
* })) {
|
|
347
|
-
* ids.push(id)
|
|
348
|
-
* }
|
|
349
|
-
* ```
|
|
350
|
-
*/
|
|
351
|
-
function* collectLazy(node, options) {
|
|
352
|
-
const { depth, parent, ...visitor } = options;
|
|
353
|
-
const recurse = (depth ?? require_utils.visitorDepths.deep) === require_utils.visitorDepths.deep;
|
|
354
|
-
const v = applyVisitor(node, visitor, parent);
|
|
355
|
-
if (v != null) yield v;
|
|
356
|
-
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
|
|
357
|
-
...options,
|
|
358
|
-
parent: node
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Eager depth-first collection pass. Returns an array of every non-null value
|
|
363
|
-
* the visitor callbacks return.
|
|
364
|
-
*
|
|
365
|
-
* @example Collect every operationId
|
|
366
|
-
* ```ts
|
|
367
|
-
* const ids = collect<string>(root, {
|
|
368
|
-
* operation(node) {
|
|
369
|
-
* return node.operationId
|
|
370
|
-
* },
|
|
371
|
-
* })
|
|
372
|
-
* ```
|
|
373
|
-
*/
|
|
374
|
-
function collect(node, options) {
|
|
375
|
-
return Array.from(collectLazy(node, options));
|
|
376
|
-
}
|
|
377
|
-
//#endregion
|
|
378
|
-
//#region src/utils/ast.ts
|
|
379
|
-
const plainStringTypes = new Set([
|
|
380
|
-
"string",
|
|
381
|
-
"uuid",
|
|
382
|
-
"email",
|
|
383
|
-
"url",
|
|
384
|
-
"datetime"
|
|
385
|
-
]);
|
|
386
|
-
/**
|
|
387
|
-
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
|
|
388
|
-
*
|
|
389
|
-
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node
|
|
390
|
-
* override the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
|
|
391
|
-
*
|
|
392
|
-
* @example
|
|
393
|
-
* ```ts
|
|
394
|
-
* // Ref with description override
|
|
395
|
-
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
|
|
396
|
-
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
|
|
397
|
-
* ```
|
|
398
|
-
*/
|
|
399
|
-
function syncSchemaRef(node) {
|
|
400
|
-
const ref = narrowSchema(node, "ref");
|
|
401
|
-
if (!ref) return node;
|
|
402
|
-
if (!ref.schema) return node;
|
|
403
|
-
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
|
|
404
|
-
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
|
|
405
|
-
return createSchema({
|
|
406
|
-
...ref.schema,
|
|
407
|
-
...definedOverrides
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* Type guard that returns `true` when a schema emits as a plain `string` type.
|
|
412
|
-
*
|
|
413
|
-
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
|
|
414
|
-
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
|
|
415
|
-
*/
|
|
416
|
-
function isStringType(node) {
|
|
417
|
-
if (plainStringTypes.has(node.type)) return true;
|
|
418
|
-
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
|
|
419
|
-
if (temporal) return temporal.representation !== "date";
|
|
420
|
-
return false;
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Applies casing rules to parameter names and returns a new parameter array.
|
|
424
|
-
*
|
|
425
|
-
* Use this before passing parameters to schema builders so output property keys match
|
|
426
|
-
* the desired casing while preserving `OperationNode.parameters` for other consumers.
|
|
427
|
-
* The input array is not mutated. When `casing` is not set, the original array is returned unchanged.
|
|
428
|
-
*/
|
|
429
|
-
const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => {
|
|
430
|
-
const transformed = casing === "camelcase" || !require_utils.isValidVarName(param.name) ? require_utils.camelCase(param.name) : param.name;
|
|
431
|
-
return {
|
|
432
|
-
...param,
|
|
433
|
-
name: transformed
|
|
434
|
-
};
|
|
435
|
-
})));
|
|
436
|
-
function caseParams(params, casing) {
|
|
437
|
-
if (!casing) return params;
|
|
438
|
-
return caseParamsMemo(params)(casing);
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* Creates a single-property object schema used as a discriminator literal.
|
|
442
|
-
*
|
|
443
|
-
* @example
|
|
444
|
-
* ```ts
|
|
445
|
-
* createDiscriminantNode({ propertyName: 'type', value: 'dog' })
|
|
446
|
-
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
|
|
447
|
-
* ```
|
|
448
|
-
*/
|
|
449
|
-
function createDiscriminantNode({ propertyName, value }) {
|
|
450
|
-
return createSchema({
|
|
451
|
-
type: "object",
|
|
452
|
-
primitive: "object",
|
|
453
|
-
properties: [createProperty({
|
|
454
|
-
name: propertyName,
|
|
455
|
-
schema: createSchema({
|
|
456
|
-
type: "enum",
|
|
457
|
-
primitive: "string",
|
|
458
|
-
enumValues: [value]
|
|
459
|
-
}),
|
|
460
|
-
required: true
|
|
461
|
-
})]
|
|
462
|
-
});
|
|
463
|
-
}
|
|
464
|
-
function resolveParamsType({ node, param, resolver }) {
|
|
465
|
-
if (!resolver) return createParamsType({
|
|
466
|
-
variant: "reference",
|
|
467
|
-
name: param.schema.primitive ?? "unknown"
|
|
468
|
-
});
|
|
469
|
-
const individualName = resolver.resolveParamName(node, param);
|
|
470
|
-
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
|
|
471
|
-
const groupResolvers = {
|
|
472
|
-
path: resolver.resolvePathParamsName,
|
|
473
|
-
query: resolver.resolveQueryParamsName,
|
|
474
|
-
header: resolver.resolveHeaderParamsName
|
|
475
|
-
};
|
|
476
|
-
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
|
|
477
|
-
if (groupName && groupName !== individualName) return createParamsType({
|
|
478
|
-
variant: "member",
|
|
479
|
-
base: groupName,
|
|
480
|
-
key: param.name
|
|
481
|
-
});
|
|
482
|
-
return createParamsType({
|
|
483
|
-
variant: "reference",
|
|
484
|
-
name: individualName
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* Converts an `OperationNode` into function parameters for code generation.
|
|
489
|
-
*
|
|
490
|
-
* Centralizes parameter grouping logic for all plugins. Provide a `resolver` for type name resolution
|
|
491
|
-
* and `extraParams` for plugin-specific trailing parameters (e.g., `options` objects).
|
|
492
|
-
* Supports three grouping modes: `object` (single destructured param), `inline` (separate params),
|
|
493
|
-
* and `inlineSpread` (rest parameter). Use `CreateOperationParamsOptions` to fine-tune output.
|
|
494
|
-
*/
|
|
495
|
-
function createOperationParams(node, options) {
|
|
496
|
-
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options;
|
|
497
|
-
const dataName = paramNames?.data ?? "data";
|
|
498
|
-
const paramsName = paramNames?.params ?? "params";
|
|
499
|
-
const headersName = paramNames?.headers ?? "headers";
|
|
500
|
-
const pathName = paramNames?.path ?? "pathParams";
|
|
501
|
-
const wrapType = (type) => createParamsType({
|
|
502
|
-
variant: "reference",
|
|
503
|
-
name: typeWrapper ? typeWrapper(type) : type
|
|
504
|
-
});
|
|
505
|
-
const wrapTypeNode = (type) => type.kind === "ParamsType" && type.variant === "reference" ? wrapType(type.name) : type;
|
|
506
|
-
const casedParams = caseParams(node.parameters, paramsCasing);
|
|
507
|
-
const pathParams = casedParams.filter((p) => p.in === "path");
|
|
508
|
-
const queryParams = casedParams.filter((p) => p.in === "query");
|
|
509
|
-
const headerParams = casedParams.filter((p) => p.in === "header");
|
|
510
|
-
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
|
|
511
|
-
const bodyRequired = node.requestBody?.required ?? false;
|
|
512
|
-
const queryGroupType = resolver ? resolveGroupType({
|
|
513
|
-
node,
|
|
514
|
-
params: queryParams,
|
|
515
|
-
groupMethod: resolver.resolveQueryParamsName,
|
|
516
|
-
resolver
|
|
517
|
-
}) : void 0;
|
|
518
|
-
const headerGroupType = resolver ? resolveGroupType({
|
|
519
|
-
node,
|
|
520
|
-
params: headerParams,
|
|
521
|
-
groupMethod: resolver.resolveHeaderParamsName,
|
|
522
|
-
resolver
|
|
523
|
-
}) : void 0;
|
|
524
|
-
const params = [];
|
|
525
|
-
if (paramsType === "object") {
|
|
526
|
-
const children = [
|
|
527
|
-
...pathParams.map((p) => {
|
|
528
|
-
const type = resolveParamsType({
|
|
529
|
-
node,
|
|
530
|
-
param: p,
|
|
531
|
-
resolver
|
|
532
|
-
});
|
|
533
|
-
return createFunctionParameter({
|
|
534
|
-
name: p.name,
|
|
535
|
-
type: wrapTypeNode(type),
|
|
536
|
-
optional: !p.required
|
|
537
|
-
});
|
|
538
|
-
}),
|
|
539
|
-
...bodyType ? [createFunctionParameter({
|
|
540
|
-
name: dataName,
|
|
541
|
-
type: bodyType,
|
|
542
|
-
optional: !bodyRequired
|
|
543
|
-
})] : [],
|
|
544
|
-
...buildGroupParam({
|
|
545
|
-
name: paramsName,
|
|
546
|
-
node,
|
|
547
|
-
params: queryParams,
|
|
548
|
-
groupType: queryGroupType,
|
|
549
|
-
resolver,
|
|
550
|
-
wrapType
|
|
551
|
-
}),
|
|
552
|
-
...buildGroupParam({
|
|
553
|
-
name: headersName,
|
|
554
|
-
node,
|
|
555
|
-
params: headerParams,
|
|
556
|
-
groupType: headerGroupType,
|
|
557
|
-
resolver,
|
|
558
|
-
wrapType
|
|
559
|
-
})
|
|
560
|
-
];
|
|
561
|
-
if (children.length) params.push(createParameterGroup({
|
|
562
|
-
properties: children,
|
|
563
|
-
default: children.every((c) => c.optional) ? "{}" : void 0
|
|
564
|
-
}));
|
|
565
|
-
} else {
|
|
566
|
-
if (pathParams.length) if (pathParamsType === "inlineSpread") {
|
|
567
|
-
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]);
|
|
568
|
-
params.push(createFunctionParameter({
|
|
569
|
-
name: pathName,
|
|
570
|
-
type: spreadType ? wrapType(spreadType) : void 0,
|
|
571
|
-
rest: true
|
|
572
|
-
}));
|
|
573
|
-
} else {
|
|
574
|
-
const pathChildren = pathParams.map((p) => {
|
|
575
|
-
const type = resolveParamsType({
|
|
576
|
-
node,
|
|
577
|
-
param: p,
|
|
578
|
-
resolver
|
|
579
|
-
});
|
|
580
|
-
return createFunctionParameter({
|
|
581
|
-
name: p.name,
|
|
582
|
-
type: wrapTypeNode(type),
|
|
583
|
-
optional: !p.required
|
|
584
|
-
});
|
|
585
|
-
});
|
|
586
|
-
params.push(createParameterGroup({
|
|
587
|
-
properties: pathChildren,
|
|
588
|
-
inline: pathParamsType === "inline",
|
|
589
|
-
default: pathParamsDefault ?? (pathChildren.every((c) => c.optional) ? "{}" : void 0)
|
|
590
|
-
}));
|
|
591
|
-
}
|
|
592
|
-
if (bodyType) params.push(createFunctionParameter({
|
|
593
|
-
name: dataName,
|
|
594
|
-
type: bodyType,
|
|
595
|
-
optional: !bodyRequired
|
|
596
|
-
}));
|
|
597
|
-
params.push(...buildGroupParam({
|
|
598
|
-
name: paramsName,
|
|
599
|
-
node,
|
|
600
|
-
params: queryParams,
|
|
601
|
-
groupType: queryGroupType,
|
|
602
|
-
resolver,
|
|
603
|
-
wrapType
|
|
604
|
-
}));
|
|
605
|
-
params.push(...buildGroupParam({
|
|
606
|
-
name: headersName,
|
|
607
|
-
node,
|
|
608
|
-
params: headerParams,
|
|
609
|
-
groupType: headerGroupType,
|
|
610
|
-
resolver,
|
|
611
|
-
wrapType
|
|
612
|
-
}));
|
|
613
|
-
}
|
|
614
|
-
params.push(...extraParams);
|
|
615
|
-
return createFunctionParameters({ params });
|
|
616
|
-
}
|
|
617
|
-
/**
|
|
618
|
-
* Builds a single {@link FunctionParameterNode} for a query or header group.
|
|
619
|
-
* Returns an empty array when there are no params to emit.
|
|
620
|
-
*
|
|
621
|
-
* If a pre-resolved `groupType` is provided it emits `name: GroupType`.
|
|
622
|
-
* Otherwise, it builds an inline struct from the individual params.
|
|
623
|
-
*/
|
|
624
|
-
function buildGroupParam({ name, node, params, groupType, resolver, wrapType }) {
|
|
625
|
-
if (groupType) return [createFunctionParameter({
|
|
626
|
-
name,
|
|
627
|
-
type: groupType.type.kind === "ParamsType" && groupType.type.variant === "reference" ? wrapType(groupType.type.name) : groupType.type,
|
|
628
|
-
optional: groupType.optional
|
|
629
|
-
})];
|
|
630
|
-
if (params.length) return [createFunctionParameter({
|
|
631
|
-
name,
|
|
632
|
-
type: toStructType({
|
|
633
|
-
node,
|
|
634
|
-
params,
|
|
635
|
-
resolver
|
|
636
|
-
}),
|
|
637
|
-
optional: params.every((p) => !p.required)
|
|
638
|
-
})];
|
|
639
|
-
return [];
|
|
640
|
-
}
|
|
641
|
-
/**
|
|
642
|
-
* Derives a {@link ParamGroupType} from the resolver's group method.
|
|
643
|
-
* Returns `null` when the group name equals the individual param name (no real group).
|
|
644
|
-
*/
|
|
645
|
-
function resolveGroupType({ node, params, groupMethod, resolver }) {
|
|
646
|
-
if (!params.length) return null;
|
|
647
|
-
const firstParam = params[0];
|
|
648
|
-
const groupName = groupMethod.call(resolver, node, firstParam);
|
|
649
|
-
if (groupName === resolver.resolveParamName(node, firstParam)) return null;
|
|
650
|
-
const allOptional = params.every((p) => !p.required);
|
|
651
|
-
return {
|
|
652
|
-
type: createParamsType({
|
|
653
|
-
variant: "reference",
|
|
654
|
-
name: groupName
|
|
655
|
-
}),
|
|
656
|
-
optional: allOptional
|
|
657
|
-
};
|
|
658
|
-
}
|
|
659
|
-
/**
|
|
660
|
-
* Builds a {@link TypeNode} with `variant: 'struct'` for an inline anonymous type grouping named fields.
|
|
661
|
-
*
|
|
662
|
-
* Used when query or header parameters have no dedicated group type name.
|
|
663
|
-
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
|
|
664
|
-
*/
|
|
665
|
-
function toStructType({ node, params, resolver }) {
|
|
666
|
-
return createParamsType({
|
|
667
|
-
variant: "struct",
|
|
668
|
-
properties: params.map((p) => ({
|
|
669
|
-
name: p.name,
|
|
670
|
-
optional: !p.required,
|
|
671
|
-
type: resolveParamsType({
|
|
672
|
-
node,
|
|
673
|
-
param: p,
|
|
674
|
-
resolver
|
|
675
|
-
})
|
|
676
|
-
}))
|
|
677
|
-
});
|
|
678
|
-
}
|
|
679
|
-
function sourceKey(source) {
|
|
680
|
-
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
|
|
681
|
-
}
|
|
682
|
-
function pathTypeKey(path, isTypeOnly) {
|
|
683
|
-
return `${path}:${isTypeOnly ?? false}`;
|
|
684
|
-
}
|
|
685
|
-
function exportKey(path, name, isTypeOnly, asAlias) {
|
|
686
|
-
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
|
|
687
|
-
}
|
|
688
|
-
function importKey(path, name, isTypeOnly) {
|
|
689
|
-
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
|
|
690
|
-
}
|
|
691
|
-
/**
|
|
692
|
-
* Computes a multi-level sort key for exports and imports:
|
|
693
|
-
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
|
|
694
|
-
*/
|
|
695
|
-
function sortKey(node) {
|
|
696
|
-
const isArray = Array.isArray(node.name) ? "1" : "0";
|
|
697
|
-
const typeOnly = node.isTypeOnly ? "0" : "1";
|
|
698
|
-
const hasName = node.name != null ? "1" : "0";
|
|
699
|
-
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
|
|
700
|
-
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
|
|
701
|
-
}
|
|
702
|
-
/**
|
|
703
|
-
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
|
|
704
|
-
*
|
|
705
|
-
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
|
|
706
|
-
*/
|
|
707
|
-
function combineSources(sources) {
|
|
708
|
-
const seen = /* @__PURE__ */ new Map();
|
|
709
|
-
for (const source of sources) {
|
|
710
|
-
const key = sourceKey(source);
|
|
711
|
-
if (!seen.has(key)) seen.set(key, source);
|
|
712
|
-
}
|
|
713
|
-
return [...seen.values()];
|
|
714
|
-
}
|
|
715
|
-
/**
|
|
716
|
-
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
|
|
717
|
-
*
|
|
718
|
-
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
|
|
719
|
-
*/
|
|
720
|
-
function mergeNameArrays(existing, incoming) {
|
|
721
|
-
const merged = new Set(existing);
|
|
722
|
-
for (const name of incoming) merged.add(name);
|
|
723
|
-
return [...merged];
|
|
724
|
-
}
|
|
725
|
-
/**
|
|
726
|
-
* Deduplicates and merges `ExportNode` objects by path and type.
|
|
727
|
-
*
|
|
728
|
-
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
|
|
729
|
-
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
|
|
730
|
-
*/
|
|
731
|
-
function combineExports(exports) {
|
|
732
|
-
const result = [];
|
|
733
|
-
const namedByPath = /* @__PURE__ */ new Map();
|
|
734
|
-
const seen = /* @__PURE__ */ new Set();
|
|
735
|
-
const keyed = exports.map((node) => ({
|
|
736
|
-
node,
|
|
737
|
-
key: sortKey(node)
|
|
738
|
-
}));
|
|
739
|
-
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
740
|
-
for (const { node: curr } of keyed) {
|
|
741
|
-
const { name, path, isTypeOnly, asAlias } = curr;
|
|
742
|
-
if (Array.isArray(name)) {
|
|
743
|
-
if (!name.length) continue;
|
|
744
|
-
const key = pathTypeKey(path, isTypeOnly);
|
|
745
|
-
const existing = namedByPath.get(key);
|
|
746
|
-
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
747
|
-
else {
|
|
748
|
-
const newItem = {
|
|
749
|
-
...curr,
|
|
750
|
-
name: [...new Set(name)]
|
|
751
|
-
};
|
|
752
|
-
result.push(newItem);
|
|
753
|
-
namedByPath.set(key, newItem);
|
|
754
|
-
}
|
|
755
|
-
} else {
|
|
756
|
-
const key = exportKey(path, name, isTypeOnly, asAlias);
|
|
757
|
-
if (!seen.has(key)) {
|
|
758
|
-
result.push(curr);
|
|
759
|
-
seen.add(key);
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
return result;
|
|
764
|
-
}
|
|
765
|
-
/**
|
|
766
|
-
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
|
|
767
|
-
*
|
|
768
|
-
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
|
|
769
|
-
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
|
|
770
|
-
*
|
|
771
|
-
* @note Use this when combining imports from multiple files to avoid duplicate declarations.
|
|
772
|
-
*/
|
|
773
|
-
function combineImports(imports, exports, source) {
|
|
774
|
-
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
775
|
-
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
|
|
776
|
-
const importNameMemo = /* @__PURE__ */ new Map();
|
|
777
|
-
const canonicalizeName = (n) => {
|
|
778
|
-
if (typeof n === "string") return n;
|
|
779
|
-
const key = `${n.propertyName}:${n.name ?? ""}`;
|
|
780
|
-
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
|
|
781
|
-
return importNameMemo.get(key);
|
|
782
|
-
};
|
|
783
|
-
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
|
|
784
|
-
for (const node of imports) {
|
|
785
|
-
if (!Array.isArray(node.name)) continue;
|
|
786
|
-
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
|
|
787
|
-
}
|
|
788
|
-
const result = [];
|
|
789
|
-
const namedByPath = /* @__PURE__ */ new Map();
|
|
790
|
-
const seen = /* @__PURE__ */ new Set();
|
|
791
|
-
const keyed = imports.map((node) => ({
|
|
792
|
-
node,
|
|
793
|
-
key: sortKey(node)
|
|
794
|
-
}));
|
|
795
|
-
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
796
|
-
for (const { node: curr } of keyed) {
|
|
797
|
-
if (curr.path === curr.root) continue;
|
|
798
|
-
const { path, isTypeOnly } = curr;
|
|
799
|
-
let { name } = curr;
|
|
800
|
-
if (Array.isArray(name)) {
|
|
801
|
-
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
|
|
802
|
-
if (!name.length) continue;
|
|
803
|
-
const key = pathTypeKey(path, isTypeOnly);
|
|
804
|
-
const existing = namedByPath.get(key);
|
|
805
|
-
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
806
|
-
else {
|
|
807
|
-
const newItem = {
|
|
808
|
-
...curr,
|
|
809
|
-
name
|
|
810
|
-
};
|
|
811
|
-
result.push(newItem);
|
|
812
|
-
namedByPath.set(key, newItem);
|
|
813
|
-
}
|
|
814
|
-
} else {
|
|
815
|
-
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
|
|
816
|
-
const key = importKey(path, name, isTypeOnly);
|
|
817
|
-
if (!seen.has(key)) {
|
|
818
|
-
result.push(curr);
|
|
819
|
-
seen.add(key);
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
return result;
|
|
824
|
-
}
|
|
825
|
-
/**
|
|
826
|
-
* Extracts all string content from a `CodeNode` tree recursively.
|
|
827
|
-
*
|
|
828
|
-
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
829
|
-
* and nested node content. Used internally to build the full source string for import filtering.
|
|
830
|
-
*/
|
|
831
|
-
function extractStringsFromNodes(nodes) {
|
|
832
|
-
if (!nodes?.length) return "";
|
|
833
|
-
return nodes.map((node) => {
|
|
834
|
-
if (typeof node === "string") return node;
|
|
835
|
-
if (node.kind === "Text") return node.value;
|
|
836
|
-
if (node.kind === "Break") return "";
|
|
837
|
-
if (node.kind === "Jsx") return node.value;
|
|
838
|
-
const parts = [];
|
|
839
|
-
if ("params" in node && node.params) parts.push(node.params);
|
|
840
|
-
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
841
|
-
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
842
|
-
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
843
|
-
const nested = extractStringsFromNodes(node.nodes);
|
|
844
|
-
if (nested) parts.push(nested);
|
|
845
|
-
return parts.join("\n");
|
|
846
|
-
}).filter(Boolean).join("\n");
|
|
847
|
-
}
|
|
848
|
-
/**
|
|
849
|
-
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
|
|
850
|
-
*
|
|
851
|
-
* Returns `null` for non-ref nodes or when no name can be resolved. Use this to get a schema's
|
|
852
|
-
* identifier for type definitions or error messages.
|
|
853
|
-
*
|
|
854
|
-
* @example
|
|
855
|
-
* ```ts
|
|
856
|
-
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
|
|
857
|
-
* // => 'Pet'
|
|
858
|
-
* ```
|
|
859
|
-
*/
|
|
860
|
-
function resolveRefName(node) {
|
|
861
|
-
if (!node || node.type !== "ref") return null;
|
|
862
|
-
if (node.ref) return require_utils.extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
|
|
863
|
-
return node.name ?? node.schema?.name ?? null;
|
|
864
|
-
}
|
|
865
|
-
/**
|
|
866
|
-
* Collects every named schema referenced (transitively) from a node via ref edges.
|
|
867
|
-
*
|
|
868
|
-
* Refs are followed by name only, the resolved `node.schema` is not traversed inline.
|
|
869
|
-
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
|
|
870
|
-
*
|
|
871
|
-
* @example Collect refs from a single schema
|
|
872
|
-
* ```ts
|
|
873
|
-
* const names = collectReferencedSchemaNames(petSchema)
|
|
874
|
-
* // → Set { 'Category', 'Tag' }
|
|
875
|
-
* ```
|
|
876
|
-
*
|
|
877
|
-
* @example Accumulate refs from multiple schemas into one set
|
|
878
|
-
* ```ts
|
|
879
|
-
* const out = new Set<string>()
|
|
880
|
-
* for (const schema of schemas) {
|
|
881
|
-
* collectReferencedSchemaNames(schema, out)
|
|
882
|
-
* }
|
|
883
|
-
* ```
|
|
884
|
-
*/
|
|
885
|
-
const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
|
|
886
|
-
const refs = /* @__PURE__ */ new Set();
|
|
887
|
-
collect(node, { schema(child) {
|
|
888
|
-
if (child.type === "ref") {
|
|
889
|
-
const name = resolveRefName(child);
|
|
890
|
-
if (name) refs.add(name);
|
|
891
|
-
}
|
|
892
|
-
} });
|
|
893
|
-
return refs;
|
|
894
|
-
});
|
|
895
|
-
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
896
|
-
if (!node) return out;
|
|
897
|
-
for (const name of collectSchemaRefs(node)) out.add(name);
|
|
898
|
-
return out;
|
|
899
|
-
}
|
|
900
|
-
/**
|
|
901
|
-
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
902
|
-
*
|
|
903
|
-
* An operation uses a schema when any of its parameters, request body content, or responses
|
|
904
|
-
* reference it, directly or indirectly through other named schemas.
|
|
905
|
-
* The walk is iterative and safe against reference cycles.
|
|
906
|
-
*
|
|
907
|
-
* Use this together with `include` filters to determine which schemas from `components/schemas`
|
|
908
|
-
* are reachable from the allowed operations, so that schemas used only by excluded operations
|
|
909
|
-
* are not generated.
|
|
910
|
-
*
|
|
911
|
-
* @example Only generate schemas referenced by included operations
|
|
912
|
-
* ```ts
|
|
913
|
-
* const includedOps = operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
|
|
914
|
-
* const allowed = collectUsedSchemaNames(includedOps, schemas)
|
|
915
|
-
*
|
|
916
|
-
* for (const schema of schemas) {
|
|
917
|
-
* if (schema.name && !allowed.has(schema.name)) continue
|
|
918
|
-
* // … generate schema
|
|
919
|
-
* }
|
|
920
|
-
* ```
|
|
921
|
-
*
|
|
922
|
-
* @example Check whether a specific schema is needed
|
|
923
|
-
* ```ts
|
|
924
|
-
* const allowed = collectUsedSchemaNames(includedOps, schemas)
|
|
925
|
-
* allowed.has('OrderStatus') // false when no included operation references OrderStatus
|
|
926
|
-
* ```
|
|
927
|
-
*/
|
|
928
|
-
const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
|
|
929
|
-
function computeUsedSchemaNames(operations, schemas) {
|
|
930
|
-
const schemaMap = /* @__PURE__ */ new Map();
|
|
931
|
-
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
|
|
932
|
-
const result = /* @__PURE__ */ new Set();
|
|
933
|
-
function visitSchema(schema) {
|
|
934
|
-
const directRefs = collectReferencedSchemaNames(schema);
|
|
935
|
-
for (const name of directRefs) if (!result.has(name)) {
|
|
936
|
-
result.add(name);
|
|
937
|
-
const namedSchema = schemaMap.get(name);
|
|
938
|
-
if (namedSchema) visitSchema(namedSchema);
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
for (const op of operations) for (const schema of collectLazy(op, {
|
|
942
|
-
depth: "shallow",
|
|
943
|
-
schema: (node) => node
|
|
944
|
-
})) visitSchema(schema);
|
|
945
|
-
return result;
|
|
946
|
-
}
|
|
947
|
-
function collectUsedSchemaNames(operations, schemas) {
|
|
948
|
-
return collectUsedSchemaNamesMemo(operations)(schemas);
|
|
949
|
-
}
|
|
950
|
-
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
|
|
951
|
-
const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
|
|
952
|
-
const graph = /* @__PURE__ */ new Map();
|
|
953
|
-
for (const schema of schemas) {
|
|
954
|
-
if (!schema.name) continue;
|
|
955
|
-
graph.set(schema.name, collectReferencedSchemaNames(schema));
|
|
956
|
-
}
|
|
957
|
-
const circular = /* @__PURE__ */ new Set();
|
|
958
|
-
for (const start of graph.keys()) {
|
|
959
|
-
const visited = /* @__PURE__ */ new Set();
|
|
960
|
-
const stack = [...graph.get(start) ?? []];
|
|
961
|
-
while (stack.length > 0) {
|
|
962
|
-
const node = stack.pop();
|
|
963
|
-
if (node === start) {
|
|
964
|
-
circular.add(start);
|
|
965
|
-
break;
|
|
966
|
-
}
|
|
967
|
-
if (visited.has(node)) continue;
|
|
968
|
-
visited.add(node);
|
|
969
|
-
const next = graph.get(node);
|
|
970
|
-
if (next) for (const r of next) stack.push(r);
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
return circular;
|
|
974
|
-
});
|
|
975
|
-
/**
|
|
976
|
-
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
977
|
-
*
|
|
978
|
-
* Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
|
|
979
|
-
* in deferred constructs (lazy getter, `z.lazy(() => …)`) to prevent infinite recursion when generated code runs.
|
|
980
|
-
* Refs are followed by name only, keeping the algorithm linear in the schema graph size.
|
|
981
|
-
*
|
|
982
|
-
* @note Call this once on the full schema graph, then use `containsCircularRef()` to check individual schemas.
|
|
983
|
-
*/
|
|
984
|
-
function findCircularSchemas(schemas) {
|
|
985
|
-
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
|
|
986
|
-
return findCircularSchemasMemo(schemas);
|
|
987
|
-
}
|
|
988
|
-
/**
|
|
989
|
-
* Type guard returning `true` when a schema or anything nested within it contains a ref to a circular schema.
|
|
990
|
-
*
|
|
991
|
-
* Use `excludeName` to ignore refs to specific schemas (useful when self-references are handled separately).
|
|
992
|
-
* Commonly used with `findCircularSchemas()` to detect where lazy wrappers are needed in code generation.
|
|
993
|
-
*
|
|
994
|
-
* @note Returns `true` for the first matching circular ref found. Use for fast dependency checks.
|
|
995
|
-
*/
|
|
996
|
-
function containsCircularRef(node, { circularSchemas, excludeName }) {
|
|
997
|
-
if (!node || circularSchemas.size === 0) return false;
|
|
998
|
-
for (const _ of collectLazy(node, { schema(child) {
|
|
999
|
-
if (child.type !== "ref") return null;
|
|
1000
|
-
const name = resolveRefName(child);
|
|
1001
|
-
return name && name !== excludeName && circularSchemas.has(name) ? true : null;
|
|
1002
|
-
} })) return true;
|
|
1003
|
-
return false;
|
|
1004
|
-
}
|
|
1005
|
-
//#endregion
|
|
1006
|
-
//#region src/factory.ts
|
|
1007
|
-
/**
|
|
1008
|
-
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
|
|
1009
|
-
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
|
|
1010
|
-
* object properties combine "required" and "nullable" into a single AST.
|
|
1011
|
-
*
|
|
1012
|
-
* - Non-required + non-nullable → `optional: true`.
|
|
1013
|
-
* - Non-required + nullable → `nullish: true`.
|
|
1014
|
-
* - Required → both flags cleared.
|
|
1015
|
-
*/
|
|
1016
|
-
function syncOptionality(schema, required) {
|
|
1017
|
-
const nullable = schema.nullable ?? false;
|
|
1018
|
-
return {
|
|
1019
|
-
...schema,
|
|
1020
|
-
optional: !required && !nullable ? true : void 0,
|
|
1021
|
-
nullish: !required && nullable ? true : void 0
|
|
1022
|
-
};
|
|
1023
|
-
}
|
|
1024
|
-
/**
|
|
1025
|
-
* Identity-preserving node update: returns `node` unchanged when every field in
|
|
1026
|
-
* `changes` already equals (by reference) the current value, otherwise a new node
|
|
1027
|
-
* with the changes applied.
|
|
1028
|
-
*
|
|
1029
|
-
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
|
|
1030
|
-
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
|
|
1031
|
-
* downstream passes can detect "nothing changed" by identity. Comparison is
|
|
1032
|
-
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
|
|
1033
|
-
*
|
|
1034
|
-
* @example
|
|
1035
|
-
* ```ts
|
|
1036
|
-
* update(node, { name: node.name }) // -> same `node` reference
|
|
1037
|
-
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
|
|
1038
|
-
* ```
|
|
1039
|
-
*/
|
|
1040
|
-
function update(node, changes) {
|
|
1041
|
-
for (const key in changes) if (changes[key] !== node[key]) return {
|
|
1042
|
-
...node,
|
|
1043
|
-
...changes
|
|
1044
|
-
};
|
|
1045
|
-
return node;
|
|
1046
|
-
}
|
|
1047
|
-
/**
|
|
1048
|
-
* Creates an `InputNode` with stable defaults for `schemas` and `operations`.
|
|
1049
|
-
*
|
|
1050
|
-
* @example
|
|
1051
|
-
* ```ts
|
|
1052
|
-
* const input = createInput()
|
|
1053
|
-
* // { kind: 'Input', schemas: [], operations: [] }
|
|
1054
|
-
* ```
|
|
1055
|
-
*
|
|
1056
|
-
* @example
|
|
1057
|
-
* ```ts
|
|
1058
|
-
* const input = createInput({ schemas: [petSchema] })
|
|
1059
|
-
* // keeps default operations: []
|
|
1060
|
-
* ```
|
|
1061
|
-
*/
|
|
1062
|
-
function createInput(overrides = {}) {
|
|
1063
|
-
return {
|
|
1064
|
-
schemas: [],
|
|
1065
|
-
operations: [],
|
|
1066
|
-
meta: {
|
|
1067
|
-
circularNames: [],
|
|
1068
|
-
enumNames: []
|
|
1069
|
-
},
|
|
1070
|
-
...overrides,
|
|
1071
|
-
kind: "Input"
|
|
1072
|
-
};
|
|
1073
|
-
}
|
|
1074
|
-
/**
|
|
1075
|
-
* Creates a streaming `InputNode<true>` from pre-built `AsyncIterable` sources.
|
|
1076
|
-
*
|
|
1077
|
-
* @example
|
|
1078
|
-
* ```ts
|
|
1079
|
-
* const node = createStreamInput(schemasIterable, operationsIterable, { title: 'My API' })
|
|
1080
|
-
* ```
|
|
1081
|
-
*/
|
|
1082
|
-
function createStreamInput(schemas, operations, meta) {
|
|
1083
|
-
return {
|
|
1084
|
-
kind: "Input",
|
|
1085
|
-
schemas,
|
|
1086
|
-
operations,
|
|
1087
|
-
meta
|
|
1088
|
-
};
|
|
1089
|
-
}
|
|
1090
|
-
/**
|
|
1091
|
-
* Creates an `OutputNode` with a stable default for `files`.
|
|
1092
|
-
*
|
|
1093
|
-
* @example
|
|
1094
|
-
* ```ts
|
|
1095
|
-
* const output = createOutput()
|
|
1096
|
-
* // { kind: 'Output', files: [] }
|
|
1097
|
-
* ```
|
|
1098
|
-
*
|
|
1099
|
-
* @example
|
|
1100
|
-
* ```ts
|
|
1101
|
-
* const output = createOutput({ files: [petFile] })
|
|
1102
|
-
* ```
|
|
1103
|
-
*/
|
|
1104
|
-
function createOutput(overrides = {}) {
|
|
1105
|
-
return {
|
|
1106
|
-
files: [],
|
|
1107
|
-
...overrides,
|
|
1108
|
-
kind: "Output"
|
|
1109
|
-
};
|
|
1110
|
-
}
|
|
1111
|
-
/**
|
|
1112
|
-
* Creates a `ContentNode` for a single request-body or response content type.
|
|
1113
|
-
*/
|
|
1114
|
-
function createContent(props) {
|
|
1115
|
-
return {
|
|
1116
|
-
...props,
|
|
1117
|
-
kind: "Content"
|
|
1118
|
-
};
|
|
1119
|
-
}
|
|
1120
|
-
/**
|
|
1121
|
-
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
|
|
1122
|
-
*/
|
|
1123
|
-
function createRequestBody(props) {
|
|
1124
|
-
return {
|
|
1125
|
-
...props,
|
|
1126
|
-
kind: "RequestBody",
|
|
1127
|
-
content: props.content?.map(createContent)
|
|
1128
|
-
};
|
|
1129
|
-
}
|
|
1130
|
-
function createOperation(props) {
|
|
1131
|
-
const { requestBody, ...rest } = props;
|
|
1132
|
-
const isHttp = rest.method !== void 0 && rest.path !== void 0;
|
|
1133
|
-
return {
|
|
1134
|
-
tags: [],
|
|
1135
|
-
parameters: [],
|
|
1136
|
-
responses: [],
|
|
1137
|
-
...rest,
|
|
1138
|
-
...isHttp ? { protocol: "http" } : {},
|
|
1139
|
-
kind: "Operation",
|
|
1140
|
-
requestBody: requestBody ? createRequestBody(requestBody) : void 0
|
|
1141
|
-
};
|
|
1142
|
-
}
|
|
1143
|
-
/**
|
|
1144
|
-
* Maps schema `type` to its underlying `primitive`.
|
|
1145
|
-
* Primitive types map to themselves. Special string formats map to `'string'`.
|
|
1146
|
-
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
|
|
1147
|
-
*/
|
|
1148
|
-
const TYPE_TO_PRIMITIVE = {
|
|
1149
|
-
string: "string",
|
|
1150
|
-
number: "number",
|
|
1151
|
-
integer: "integer",
|
|
1152
|
-
bigint: "bigint",
|
|
1153
|
-
boolean: "boolean",
|
|
1154
|
-
null: "null",
|
|
1155
|
-
any: "any",
|
|
1156
|
-
unknown: "unknown",
|
|
1157
|
-
void: "void",
|
|
1158
|
-
never: "never",
|
|
1159
|
-
object: "object",
|
|
1160
|
-
array: "array",
|
|
1161
|
-
date: "date",
|
|
1162
|
-
uuid: "string",
|
|
1163
|
-
email: "string",
|
|
1164
|
-
url: "string",
|
|
1165
|
-
datetime: "string",
|
|
1166
|
-
time: "string"
|
|
1167
|
-
};
|
|
1168
|
-
function createSchema(props) {
|
|
1169
|
-
const inferredPrimitive = TYPE_TO_PRIMITIVE[props.type];
|
|
1170
|
-
if (props["type"] === "object") return {
|
|
1171
|
-
properties: [],
|
|
1172
|
-
primitive: "object",
|
|
1173
|
-
...props,
|
|
1174
|
-
kind: "Schema"
|
|
1175
|
-
};
|
|
1176
|
-
return {
|
|
1177
|
-
primitive: inferredPrimitive,
|
|
1178
|
-
...props,
|
|
1179
|
-
kind: "Schema"
|
|
1180
|
-
};
|
|
1181
|
-
}
|
|
1182
|
-
/**
|
|
1183
|
-
* Creates a `PropertyNode`.
|
|
1184
|
-
*
|
|
1185
|
-
* `required` defaults to `false`.
|
|
1186
|
-
* `schema.optional` and `schema.nullish` are derived from `required` and `schema.nullable`.
|
|
1187
|
-
*
|
|
1188
|
-
* @example
|
|
1189
|
-
* ```ts
|
|
1190
|
-
* const property = createProperty({
|
|
1191
|
-
* name: 'status',
|
|
1192
|
-
* schema: createSchema({ type: 'string' }),
|
|
1193
|
-
* })
|
|
1194
|
-
* // required=false, schema.optional=true
|
|
1195
|
-
* ```
|
|
1196
|
-
*
|
|
1197
|
-
* @example
|
|
1198
|
-
* ```ts
|
|
1199
|
-
* const property = createProperty({
|
|
1200
|
-
* name: 'status',
|
|
1201
|
-
* required: true,
|
|
1202
|
-
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1203
|
-
* })
|
|
1204
|
-
* // required=true, no optional/nullish
|
|
1205
|
-
* ```
|
|
1206
|
-
*/
|
|
1207
|
-
function createProperty(props) {
|
|
1208
|
-
const required = props.required ?? false;
|
|
1209
|
-
return {
|
|
1210
|
-
...props,
|
|
1211
|
-
kind: "Property",
|
|
1212
|
-
required,
|
|
1213
|
-
schema: syncOptionality(props.schema, required)
|
|
1214
|
-
};
|
|
1215
|
-
}
|
|
1216
|
-
/**
|
|
1217
|
-
* Creates a `ParameterNode`.
|
|
1218
|
-
*
|
|
1219
|
-
* `required` defaults to `false`.
|
|
1220
|
-
* Nested schema flags are set from `required` and `schema.nullable`.
|
|
1221
|
-
*
|
|
1222
|
-
* @example
|
|
1223
|
-
* ```ts
|
|
1224
|
-
* const param = createParameter({
|
|
1225
|
-
* name: 'petId',
|
|
1226
|
-
* in: 'path',
|
|
1227
|
-
* required: true,
|
|
1228
|
-
* schema: createSchema({ type: 'string' }),
|
|
1229
|
-
* })
|
|
1230
|
-
* ```
|
|
1231
|
-
*
|
|
1232
|
-
* @example
|
|
1233
|
-
* ```ts
|
|
1234
|
-
* const param = createParameter({
|
|
1235
|
-
* name: 'status',
|
|
1236
|
-
* in: 'query',
|
|
1237
|
-
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1238
|
-
* })
|
|
1239
|
-
* // required=false, schema.nullish=true
|
|
1240
|
-
* ```
|
|
1241
|
-
*/
|
|
1242
|
-
function createParameter(props) {
|
|
1243
|
-
const required = props.required ?? false;
|
|
1244
|
-
return {
|
|
1245
|
-
...props,
|
|
1246
|
-
kind: "Parameter",
|
|
1247
|
-
required,
|
|
1248
|
-
schema: syncOptionality(props.schema, required)
|
|
1249
|
-
};
|
|
1250
|
-
}
|
|
1251
|
-
/**
|
|
1252
|
-
* Creates a `ResponseNode`.
|
|
1253
|
-
*
|
|
1254
|
-
* Response body schemas live inside `content`. For convenience a single legacy `schema`
|
|
1255
|
-
* (with optional `mediaType`/`keysToOmit`) is normalized into one `content` entry, so the same
|
|
1256
|
-
* schema is never stored both at the node root and inside `content`.
|
|
1257
|
-
*
|
|
1258
|
-
* @example
|
|
1259
|
-
* ```ts
|
|
1260
|
-
* const response = createResponse({
|
|
1261
|
-
* statusCode: '200',
|
|
1262
|
-
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }],
|
|
1263
|
-
* })
|
|
1264
|
-
* ```
|
|
1265
|
-
*/
|
|
1266
|
-
function createResponse(props) {
|
|
1267
|
-
const { schema, mediaType, keysToOmit, content, ...rest } = props;
|
|
1268
|
-
const entries = content ?? (schema ? [{
|
|
1269
|
-
contentType: mediaType ?? "application/json",
|
|
1270
|
-
schema,
|
|
1271
|
-
keysToOmit: keysToOmit ?? null
|
|
1272
|
-
}] : void 0);
|
|
1273
|
-
return {
|
|
1274
|
-
...rest,
|
|
1275
|
-
kind: "Response",
|
|
1276
|
-
content: entries?.map(createContent)
|
|
1277
|
-
};
|
|
1278
|
-
}
|
|
1279
|
-
/**
|
|
1280
|
-
* Creates a `FunctionParameterNode`.
|
|
1281
|
-
*
|
|
1282
|
-
* `optional` defaults to `false`.
|
|
1283
|
-
*
|
|
1284
|
-
* @example Required typed param
|
|
1285
|
-
* ```ts
|
|
1286
|
-
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }) })
|
|
1287
|
-
* // → petId: string
|
|
1288
|
-
* ```
|
|
1289
|
-
*
|
|
1290
|
-
* @example Optional param
|
|
1291
|
-
* ```ts
|
|
1292
|
-
* createFunctionParameter({ name: 'params', type: createParamsType({ variant: 'reference', name: 'QueryParams' }), optional: true })
|
|
1293
|
-
* // → params?: QueryParams
|
|
1294
|
-
* ```
|
|
1295
|
-
*
|
|
1296
|
-
* @example Param with default (implicitly optional. Cannot combine with `optional: true`)
|
|
1297
|
-
* ```ts
|
|
1298
|
-
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), default: '{}' })
|
|
1299
|
-
* // → config: RequestConfig = {}
|
|
1300
|
-
* ```
|
|
1301
|
-
*/
|
|
1302
|
-
function createFunctionParameter(props) {
|
|
1303
|
-
return {
|
|
1304
|
-
optional: false,
|
|
1305
|
-
...props,
|
|
1306
|
-
kind: "FunctionParameter"
|
|
1307
|
-
};
|
|
1308
|
-
}
|
|
1309
|
-
/**
|
|
1310
|
-
* Creates a {@link TypeNode} representing a language-agnostic structured type expression.
|
|
1311
|
-
*
|
|
1312
|
-
* Use `variant: 'struct'` for inline anonymous types and `variant: 'member'` for a single
|
|
1313
|
-
* named field accessed from a group type. Each language's printer renders the variant
|
|
1314
|
-
* into its own syntax (TypeScript, Python, C#, Kotlin, …).
|
|
1315
|
-
*
|
|
1316
|
-
* @example Reference type (TypeScript: `QueryParams`)
|
|
1317
|
-
* ```ts
|
|
1318
|
-
* createParamsType({ variant: 'reference', name: 'QueryParams' })
|
|
1319
|
-
* ```
|
|
1320
|
-
*
|
|
1321
|
-
* @example Struct type (TypeScript: `{ petId: string }`)
|
|
1322
|
-
* ```ts
|
|
1323
|
-
* createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
|
|
1324
|
-
* ```
|
|
1325
|
-
*
|
|
1326
|
-
* @example Member type (TypeScript: `DeletePetPathParams['petId']`)
|
|
1327
|
-
* ```ts
|
|
1328
|
-
* createParamsType({ variant: 'member', base: 'DeletePetPathParams', key: 'petId' })
|
|
1329
|
-
* ```
|
|
1330
|
-
*/
|
|
1331
|
-
function createParamsType(props) {
|
|
1332
|
-
return {
|
|
1333
|
-
...props,
|
|
1334
|
-
kind: "ParamsType"
|
|
1335
|
-
};
|
|
1336
|
-
}
|
|
1337
|
-
/**
|
|
1338
|
-
* Creates a `ParameterGroupNode` representing a group of related parameters treated as a unit.
|
|
1339
|
-
*
|
|
1340
|
-
* @example Grouped param (TypeScript declaration)
|
|
1341
|
-
* ```ts
|
|
1342
|
-
* createParameterGroup({
|
|
1343
|
-
* properties: [
|
|
1344
|
-
* createFunctionParameter({ name: 'id', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
1345
|
-
* createFunctionParameter({ name: 'name', type: createParamsType({ variant: 'reference', name: 'string' }), optional: true }),
|
|
1346
|
-
* ],
|
|
1347
|
-
* default: '{}',
|
|
1348
|
-
* })
|
|
1349
|
-
* // declaration → { id, name? }: { id: string; name?: string } = {}
|
|
1350
|
-
* // call → { id, name }
|
|
1351
|
-
* ```
|
|
1352
|
-
*
|
|
1353
|
-
* @example Inline (spread), children emitted as individual top-level parameters
|
|
1354
|
-
* ```ts
|
|
1355
|
-
* createParameterGroup({
|
|
1356
|
-
* properties: [createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false })],
|
|
1357
|
-
* inline: true,
|
|
1358
|
-
* })
|
|
1359
|
-
* // declaration → petId: string
|
|
1360
|
-
* // call → petId
|
|
1361
|
-
* ```
|
|
1362
|
-
*/
|
|
1363
|
-
function createParameterGroup(props) {
|
|
1364
|
-
return {
|
|
1365
|
-
...props,
|
|
1366
|
-
kind: "ParameterGroup"
|
|
1367
|
-
};
|
|
1368
|
-
}
|
|
1369
|
-
/**
|
|
1370
|
-
* Creates a `FunctionParametersNode` from an ordered list of parameters.
|
|
1371
|
-
*
|
|
1372
|
-
* @example
|
|
1373
|
-
* ```ts
|
|
1374
|
-
* createFunctionParameters({
|
|
1375
|
-
* params: [
|
|
1376
|
-
* createFunctionParameter({ name: 'petId', type: createParamsType({ variant: 'reference', name: 'string' }), optional: false }),
|
|
1377
|
-
* createFunctionParameter({ name: 'config', type: createParamsType({ variant: 'reference', name: 'RequestConfig' }), optional: false, default: '{}' }),
|
|
1378
|
-
* ],
|
|
1379
|
-
* })
|
|
1380
|
-
* ```
|
|
1381
|
-
*
|
|
1382
|
-
* @example
|
|
1383
|
-
* ```ts
|
|
1384
|
-
* const empty = createFunctionParameters()
|
|
1385
|
-
* // { kind: 'FunctionParameters', params: [] }
|
|
1386
|
-
* ```
|
|
1387
|
-
*/
|
|
1388
|
-
function createFunctionParameters(props = {}) {
|
|
1389
|
-
return {
|
|
1390
|
-
params: [],
|
|
1391
|
-
...props,
|
|
1392
|
-
kind: "FunctionParameters"
|
|
1393
|
-
};
|
|
1394
|
-
}
|
|
1395
|
-
/**
|
|
1396
|
-
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
1397
|
-
*
|
|
1398
|
-
* @example Named import
|
|
1399
|
-
* ```ts
|
|
1400
|
-
* createImport({ name: ['useState'], path: 'react' })
|
|
1401
|
-
* // import { useState } from 'react'
|
|
1402
|
-
* ```
|
|
1403
|
-
*
|
|
1404
|
-
* @example Type-only import
|
|
1405
|
-
* ```ts
|
|
1406
|
-
* createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
|
|
1407
|
-
* // import type { FC } from 'react'
|
|
1408
|
-
* ```
|
|
1409
|
-
*/
|
|
1410
|
-
function createImport(props) {
|
|
1411
|
-
return {
|
|
1412
|
-
...props,
|
|
1413
|
-
kind: "Import"
|
|
1414
|
-
};
|
|
1415
|
-
}
|
|
1416
|
-
/**
|
|
1417
|
-
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
1418
|
-
*
|
|
1419
|
-
* @example Named export
|
|
1420
|
-
* ```ts
|
|
1421
|
-
* createExport({ name: ['Pet'], path: './Pet' })
|
|
1422
|
-
* // export { Pet } from './Pet'
|
|
1423
|
-
* ```
|
|
1424
|
-
*
|
|
1425
|
-
* @example Wildcard export
|
|
1426
|
-
* ```ts
|
|
1427
|
-
* createExport({ path: './utils' })
|
|
1428
|
-
* // export * from './utils'
|
|
1429
|
-
* ```
|
|
1430
|
-
*/
|
|
1431
|
-
function createExport(props) {
|
|
1432
|
-
return {
|
|
1433
|
-
...props,
|
|
1434
|
-
kind: "Export"
|
|
1435
|
-
};
|
|
1436
|
-
}
|
|
1437
|
-
/**
|
|
1438
|
-
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
1439
|
-
*
|
|
1440
|
-
* @example
|
|
1441
|
-
* ```ts
|
|
1442
|
-
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
1443
|
-
* ```
|
|
1444
|
-
*/
|
|
1445
|
-
function createSource(props) {
|
|
1446
|
-
return {
|
|
1447
|
-
...props,
|
|
1448
|
-
kind: "Source"
|
|
1449
|
-
};
|
|
1450
|
-
}
|
|
1451
|
-
/**
|
|
1452
|
-
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
1453
|
-
*
|
|
1454
|
-
* Computes:
|
|
1455
|
-
* - `id` SHA256 hash of the file path
|
|
1456
|
-
* - `name` `baseName` without extension
|
|
1457
|
-
* - `extname` extension extracted from `baseName`
|
|
1458
|
-
*
|
|
1459
|
-
* Deduplicates:
|
|
1460
|
-
* - `sources` via `combineSources`
|
|
1461
|
-
* - `exports` via `combineExports`
|
|
1462
|
-
* - `imports` via `combineImports` (also filters unused imports)
|
|
1463
|
-
*
|
|
1464
|
-
* @throws {Error} when `baseName` has no extension.
|
|
1465
|
-
*
|
|
1466
|
-
* @example
|
|
1467
|
-
* ```ts
|
|
1468
|
-
* const file = createFile({
|
|
1469
|
-
* baseName: 'petStore.ts',
|
|
1470
|
-
* path: 'src/models/petStore.ts',
|
|
1471
|
-
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
1472
|
-
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
1473
|
-
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
1474
|
-
* })
|
|
1475
|
-
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
1476
|
-
* // file.name = 'petStore'
|
|
1477
|
-
* // file.extname = '.ts'
|
|
1478
|
-
* ```
|
|
1479
|
-
*/
|
|
1480
|
-
function createFile(input) {
|
|
1481
|
-
const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
|
|
1482
|
-
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
1483
|
-
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
|
|
1484
|
-
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
1485
|
-
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
|
|
1486
|
-
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
|
|
1487
|
-
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
|
|
1488
|
-
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
|
|
1489
|
-
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
|
|
1490
|
-
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
|
|
1491
|
-
if (!kept.length) return [];
|
|
1492
|
-
return [kept.length === imp.name.length ? imp : {
|
|
1493
|
-
...imp,
|
|
1494
|
-
name: kept
|
|
1495
|
-
}];
|
|
1496
|
-
});
|
|
1497
|
-
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
1498
|
-
return {
|
|
1499
|
-
kind: "File",
|
|
1500
|
-
...input,
|
|
1501
|
-
id: (0, node_crypto.hash)("sha256", input.path, "hex"),
|
|
1502
|
-
name: trimExtName(input.baseName),
|
|
1503
|
-
extname,
|
|
1504
|
-
imports: resolvedImports,
|
|
1505
|
-
exports: resolvedExports,
|
|
1506
|
-
sources: resolvedSources,
|
|
1507
|
-
meta: input.meta ?? {}
|
|
1508
|
-
};
|
|
1509
|
-
}
|
|
1510
|
-
/**
|
|
1511
|
-
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
1512
|
-
*
|
|
1513
|
-
* Mirrors the `Const` component from `@kubb/renderer-jsx`.
|
|
1514
|
-
* The component's `children` are represented as `nodes`.
|
|
1515
|
-
*
|
|
1516
|
-
* @example Simple constant
|
|
1517
|
-
* ```ts
|
|
1518
|
-
* createConst({ name: 'pet' })
|
|
1519
|
-
* // const pet = ...
|
|
1520
|
-
* ```
|
|
1521
|
-
*
|
|
1522
|
-
* @example Exported constant with type and `as const`
|
|
1523
|
-
* ```ts
|
|
1524
|
-
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
1525
|
-
* // export const pets: Pet[] = ... as const
|
|
1526
|
-
* ```
|
|
1527
|
-
*
|
|
1528
|
-
* @example With JSDoc and child nodes
|
|
1529
|
-
* ```ts
|
|
1530
|
-
* createConst({
|
|
1531
|
-
* name: 'config',
|
|
1532
|
-
* export: true,
|
|
1533
|
-
* JSDoc: { comments: ['@description App configuration'] },
|
|
1534
|
-
* nodes: [],
|
|
1535
|
-
* })
|
|
1536
|
-
* ```
|
|
1537
|
-
*/
|
|
1538
|
-
function createConst(props) {
|
|
1539
|
-
return {
|
|
1540
|
-
...props,
|
|
1541
|
-
kind: "Const"
|
|
1542
|
-
};
|
|
1543
|
-
}
|
|
1544
|
-
/**
|
|
1545
|
-
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
1546
|
-
*
|
|
1547
|
-
* Mirrors the `Type` component from `@kubb/renderer-jsx`.
|
|
1548
|
-
* The component's `children` are represented as `nodes`.
|
|
1549
|
-
*
|
|
1550
|
-
* @example Simple type alias
|
|
1551
|
-
* ```ts
|
|
1552
|
-
* createType({ name: 'Pet' })
|
|
1553
|
-
* // type Pet = ...
|
|
1554
|
-
* ```
|
|
1555
|
-
*
|
|
1556
|
-
* @example Exported type with JSDoc
|
|
1557
|
-
* ```ts
|
|
1558
|
-
* createType({
|
|
1559
|
-
* name: 'PetStatus',
|
|
1560
|
-
* export: true,
|
|
1561
|
-
* JSDoc: { comments: ['@description Status of a pet'] },
|
|
1562
|
-
* })
|
|
1563
|
-
* // export type PetStatus = ...
|
|
1564
|
-
* ```
|
|
1565
|
-
*/
|
|
1566
|
-
function createType(props) {
|
|
1567
|
-
return {
|
|
1568
|
-
...props,
|
|
1569
|
-
kind: "Type"
|
|
1570
|
-
};
|
|
1571
|
-
}
|
|
1572
|
-
/**
|
|
1573
|
-
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
1574
|
-
*
|
|
1575
|
-
* Mirrors the `Function` component from `@kubb/renderer-jsx`.
|
|
1576
|
-
* The component's `children` are represented as `nodes`.
|
|
1577
|
-
*
|
|
1578
|
-
* @example Simple function
|
|
1579
|
-
* ```ts
|
|
1580
|
-
* createFunction({ name: 'getPet' })
|
|
1581
|
-
* // function getPet() { ... }
|
|
1582
|
-
* ```
|
|
1583
|
-
*
|
|
1584
|
-
* @example Exported async function with return type
|
|
1585
|
-
* ```ts
|
|
1586
|
-
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
1587
|
-
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
1588
|
-
* ```
|
|
1589
|
-
*
|
|
1590
|
-
* @example Function with generics and params
|
|
1591
|
-
* ```ts
|
|
1592
|
-
* createFunction({
|
|
1593
|
-
* name: 'identity',
|
|
1594
|
-
* export: true,
|
|
1595
|
-
* generics: ['T'],
|
|
1596
|
-
* params: 'value: T',
|
|
1597
|
-
* returnType: 'T',
|
|
1598
|
-
* })
|
|
1599
|
-
* // export function identity<T>(value: T): T { ... }
|
|
1600
|
-
* ```
|
|
1601
|
-
*/
|
|
1602
|
-
function createFunction(props) {
|
|
1603
|
-
return {
|
|
1604
|
-
...props,
|
|
1605
|
-
kind: "Function"
|
|
1606
|
-
};
|
|
1607
|
-
}
|
|
1608
|
-
/**
|
|
1609
|
-
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
1610
|
-
*
|
|
1611
|
-
* Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.
|
|
1612
|
-
* The component's `children` are represented as `nodes`.
|
|
1613
|
-
*
|
|
1614
|
-
* @example Simple arrow function
|
|
1615
|
-
* ```ts
|
|
1616
|
-
* createArrowFunction({ name: 'getPet' })
|
|
1617
|
-
* // const getPet = () => { ... }
|
|
1618
|
-
* ```
|
|
1619
|
-
*
|
|
1620
|
-
* @example Single-line exported arrow function
|
|
1621
|
-
* ```ts
|
|
1622
|
-
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
1623
|
-
* // export const double = (n: number) => ...
|
|
1624
|
-
* ```
|
|
1625
|
-
*
|
|
1626
|
-
* @example Async arrow function with generics
|
|
1627
|
-
* ```ts
|
|
1628
|
-
* createArrowFunction({
|
|
1629
|
-
* name: 'fetchPet',
|
|
1630
|
-
* export: true,
|
|
1631
|
-
* async: true,
|
|
1632
|
-
* generics: ['T'],
|
|
1633
|
-
* params: 'id: string',
|
|
1634
|
-
* returnType: 'T',
|
|
1635
|
-
* })
|
|
1636
|
-
* // export const fetchPet = async <T>(id: string): Promise<T> => { ... }
|
|
1637
|
-
* ```
|
|
1638
|
-
*/
|
|
1639
|
-
function createArrowFunction(props) {
|
|
1640
|
-
return {
|
|
1641
|
-
...props,
|
|
1642
|
-
kind: "ArrowFunction"
|
|
1643
|
-
};
|
|
1644
|
-
}
|
|
1645
|
-
/**
|
|
1646
|
-
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
1647
|
-
*
|
|
1648
|
-
* Use this instead of bare strings when building `nodes` arrays so that every
|
|
1649
|
-
* entry in the array is a typed {@link CodeNode}.
|
|
1650
|
-
*
|
|
1651
|
-
* @example
|
|
1652
|
-
* ```ts
|
|
1653
|
-
* createText('return fetch(id)')
|
|
1654
|
-
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
1655
|
-
* ```
|
|
1656
|
-
*/
|
|
1657
|
-
function createText(value) {
|
|
1658
|
-
return {
|
|
1659
|
-
value,
|
|
1660
|
-
kind: "Text"
|
|
1661
|
-
};
|
|
1662
|
-
}
|
|
1663
|
-
/**
|
|
1664
|
-
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
1665
|
-
*
|
|
1666
|
-
* Corresponds to `<br/>` in JSX components. Prints as an empty string which,
|
|
1667
|
-
* when joined with `\n` by `printNodes`, produces a blank line.
|
|
1668
|
-
*
|
|
1669
|
-
* @example
|
|
1670
|
-
* ```ts
|
|
1671
|
-
* createBreak()
|
|
1672
|
-
* // { kind: 'Break' }
|
|
1673
|
-
* ```
|
|
1674
|
-
*/
|
|
1675
|
-
function createBreak() {
|
|
1676
|
-
return { kind: "Break" };
|
|
1677
|
-
}
|
|
1678
|
-
/**
|
|
1679
|
-
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
1680
|
-
*
|
|
1681
|
-
* Use this to embed JSX markup (including fragments `<>…</>`) directly in generated code.
|
|
1682
|
-
*
|
|
1683
|
-
* @example
|
|
1684
|
-
* ```ts
|
|
1685
|
-
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
1686
|
-
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
1687
|
-
* ```
|
|
1688
|
-
*/
|
|
1689
|
-
function createJsx(value) {
|
|
1690
|
-
return {
|
|
1691
|
-
value,
|
|
1692
|
-
kind: "Jsx"
|
|
1693
|
-
};
|
|
1694
|
-
}
|
|
1695
|
-
//#endregion
|
|
1696
5
|
//#region src/signature.ts
|
|
1697
6
|
/**
|
|
1698
7
|
* The flags shared by every node kind that affect its type: `primitive`, `format`, `nullable`.
|
|
@@ -1951,7 +260,7 @@ function signatureOf(node) {
|
|
|
1951
260
|
* usage-slot and documentation fields that are not part of the canonical type.
|
|
1952
261
|
*/
|
|
1953
262
|
function createRefNode(node, canonical) {
|
|
1954
|
-
return createSchema({
|
|
263
|
+
return require_utils.createSchema({
|
|
1955
264
|
type: "ref",
|
|
1956
265
|
name: canonical.name,
|
|
1957
266
|
ref: canonical.ref,
|
|
@@ -1969,7 +278,7 @@ function applyDedupe(node, plan, skipRootMatch = false) {
|
|
|
1969
278
|
const { canonicalBySignature, aliasNames } = plan;
|
|
1970
279
|
if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node;
|
|
1971
280
|
const root = node;
|
|
1972
|
-
return transform(node, { schema(schemaNode) {
|
|
281
|
+
return require_utils.transform(node, { schema(schemaNode) {
|
|
1973
282
|
if (schemaNode.type === "ref") {
|
|
1974
283
|
const target = schemaNode.ref ? require_utils.extractRefName(schemaNode.ref) : schemaNode.name;
|
|
1975
284
|
const canonical = target ? aliasNames.get(target) : void 0;
|
|
@@ -2037,7 +346,7 @@ function buildDedupePlan(roots, options) {
|
|
|
2037
346
|
}
|
|
2038
347
|
for (const root of roots) {
|
|
2039
348
|
if (root.kind === "Schema") topLevelNodes.add(root);
|
|
2040
|
-
for (const schemaNode of collectLazy(root, { schema: (node) => node })) record(schemaNode);
|
|
349
|
+
for (const schemaNode of require_utils.collectLazy(root, { schema: (node) => node })) record(schemaNode);
|
|
2041
350
|
}
|
|
2042
351
|
const canonicalBySignature = /* @__PURE__ */ new Map();
|
|
2043
352
|
const aliasNames = /* @__PURE__ */ new Map();
|
|
@@ -2141,11 +450,11 @@ function definePrinter(build) {
|
|
|
2141
450
|
}
|
|
2142
451
|
/**
|
|
2143
452
|
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
2144
|
-
|
|
453
|
+
*
|
|
2145
454
|
* @example
|
|
2146
455
|
* ```ts
|
|
2147
|
-
* export const defineFunctionPrinter = createPrinterFactory<
|
|
2148
|
-
* (node) =>
|
|
456
|
+
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
|
|
457
|
+
* (node) => node.kind,
|
|
2149
458
|
* )
|
|
2150
459
|
* ```
|
|
2151
460
|
*/
|
|
@@ -2190,16 +499,16 @@ function createPrinterFactory(getKey) {
|
|
|
2190
499
|
* ```
|
|
2191
500
|
*/
|
|
2192
501
|
function setDiscriminatorEnum({ node, propertyName, values, enumName }) {
|
|
2193
|
-
const objectNode = narrowSchema(node, "object");
|
|
502
|
+
const objectNode = require_utils.narrowSchema(node, "object");
|
|
2194
503
|
if (!objectNode?.properties?.length) return node;
|
|
2195
504
|
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node;
|
|
2196
|
-
return createSchema({
|
|
505
|
+
return require_utils.createSchema({
|
|
2197
506
|
...objectNode,
|
|
2198
507
|
properties: objectNode.properties.map((prop) => {
|
|
2199
508
|
if (prop.name !== propertyName) return prop;
|
|
2200
|
-
return createProperty({
|
|
509
|
+
return require_utils.createProperty({
|
|
2201
510
|
...prop,
|
|
2202
|
-
schema: createSchema({
|
|
511
|
+
schema: require_utils.createSchema({
|
|
2203
512
|
type: "enum",
|
|
2204
513
|
primitive: "string",
|
|
2205
514
|
enumValues: values,
|
|
@@ -2225,11 +534,11 @@ function setDiscriminatorEnum({ node, propertyName, values, enumName }) {
|
|
|
2225
534
|
function* mergeAdjacentObjectsLazy(members) {
|
|
2226
535
|
let acc;
|
|
2227
536
|
for (const member of members) {
|
|
2228
|
-
const objectMember = narrowSchema(member, "object");
|
|
537
|
+
const objectMember = require_utils.narrowSchema(member, "object");
|
|
2229
538
|
if (objectMember && !objectMember.name && acc !== void 0) {
|
|
2230
|
-
const accObject = narrowSchema(acc, "object");
|
|
539
|
+
const accObject = require_utils.narrowSchema(acc, "object");
|
|
2231
540
|
if (accObject && !accObject.name) {
|
|
2232
|
-
acc = createSchema({
|
|
541
|
+
acc = require_utils.createSchema({
|
|
2233
542
|
...accObject,
|
|
2234
543
|
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
|
|
2235
544
|
});
|
|
@@ -2257,7 +566,7 @@ function simplifyUnion(members) {
|
|
|
2257
566
|
const scalarPrimitives = new Set(members.filter((member) => require_utils.isScalarPrimitive(member.type)).map((m) => m.type));
|
|
2258
567
|
if (!scalarPrimitives.size) return members;
|
|
2259
568
|
return members.filter((member) => {
|
|
2260
|
-
const enumNode = narrowSchema(member, "enum");
|
|
569
|
+
const enumNode = require_utils.narrowSchema(member, "enum");
|
|
2261
570
|
if (!enumNode) return true;
|
|
2262
571
|
const primitive = enumNode.primitive;
|
|
2263
572
|
if (!primitive) return true;
|
|
@@ -2268,7 +577,7 @@ function simplifyUnion(members) {
|
|
|
2268
577
|
});
|
|
2269
578
|
}
|
|
2270
579
|
function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
2271
|
-
const enumNode = narrowSchema(propNode, "enum");
|
|
580
|
+
const enumNode = require_utils.narrowSchema(propNode, "enum");
|
|
2272
581
|
if (enumNode?.primitive === "boolean") return {
|
|
2273
582
|
...propNode,
|
|
2274
583
|
name: null
|
|
@@ -2281,59 +590,55 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
|
2281
590
|
}
|
|
2282
591
|
//#endregion
|
|
2283
592
|
exports.applyDedupe = applyDedupe;
|
|
593
|
+
exports.arrowFunctionDef = require_utils.arrowFunctionDef;
|
|
594
|
+
exports.breakDef = require_utils.breakDef;
|
|
2284
595
|
exports.buildDedupePlan = buildDedupePlan;
|
|
2285
|
-
exports.
|
|
2286
|
-
exports.
|
|
2287
|
-
exports.
|
|
2288
|
-
exports.containsCircularRef = containsCircularRef;
|
|
2289
|
-
exports.createArrowFunction = createArrowFunction;
|
|
2290
|
-
exports.createBreak = createBreak;
|
|
2291
|
-
exports.createConst = createConst;
|
|
2292
|
-
exports.createDiscriminantNode = createDiscriminantNode;
|
|
2293
|
-
exports.createExport = createExport;
|
|
2294
|
-
exports.createFile = createFile;
|
|
2295
|
-
exports.createFunction = createFunction;
|
|
2296
|
-
exports.createFunctionParameter = createFunctionParameter;
|
|
2297
|
-
exports.createFunctionParameters = createFunctionParameters;
|
|
2298
|
-
exports.createImport = createImport;
|
|
2299
|
-
exports.createInput = createInput;
|
|
2300
|
-
exports.createJsx = createJsx;
|
|
2301
|
-
exports.createOperation = createOperation;
|
|
2302
|
-
exports.createOperationParams = createOperationParams;
|
|
2303
|
-
exports.createOutput = createOutput;
|
|
2304
|
-
exports.createParameter = createParameter;
|
|
2305
|
-
exports.createParameterGroup = createParameterGroup;
|
|
2306
|
-
exports.createParamsType = createParamsType;
|
|
596
|
+
exports.collect = require_utils.collect;
|
|
597
|
+
exports.constDef = require_utils.constDef;
|
|
598
|
+
exports.contentDef = require_utils.contentDef;
|
|
2307
599
|
exports.createPrinterFactory = createPrinterFactory;
|
|
2308
|
-
exports.
|
|
2309
|
-
exports.createResponse = createResponse;
|
|
2310
|
-
exports.createSchema = createSchema;
|
|
2311
|
-
exports.createSource = createSource;
|
|
2312
|
-
exports.createStreamInput = createStreamInput;
|
|
2313
|
-
exports.createText = createText;
|
|
2314
|
-
exports.createType = createType;
|
|
600
|
+
exports.defineNode = require_utils.defineNode;
|
|
2315
601
|
exports.definePrinter = definePrinter;
|
|
2316
602
|
exports.defineSchemaDialect = defineSchemaDialect;
|
|
2317
|
-
exports.
|
|
2318
|
-
exports.
|
|
603
|
+
exports.exportDef = require_utils.exportDef;
|
|
604
|
+
exports.extractStringsFromNodes = require_utils.extractStringsFromNodes;
|
|
605
|
+
Object.defineProperty(exports, "factory", {
|
|
606
|
+
enumerable: true,
|
|
607
|
+
get: function() {
|
|
608
|
+
return require_factory.factory_exports;
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
exports.fileDef = require_utils.fileDef;
|
|
612
|
+
exports.functionDef = require_utils.functionDef;
|
|
613
|
+
exports.functionParameterDef = require_utils.functionParameterDef;
|
|
614
|
+
exports.functionParametersDef = require_utils.functionParametersDef;
|
|
2319
615
|
exports.httpMethods = require_utils.httpMethods;
|
|
2320
|
-
exports.
|
|
2321
|
-
exports.
|
|
2322
|
-
exports.
|
|
2323
|
-
exports.
|
|
2324
|
-
exports.
|
|
2325
|
-
exports.isStringType = isStringType;
|
|
616
|
+
exports.importDef = require_utils.importDef;
|
|
617
|
+
exports.indexedAccessTypeDef = require_utils.indexedAccessTypeDef;
|
|
618
|
+
exports.inputDef = require_utils.inputDef;
|
|
619
|
+
exports.isHttpOperationNode = require_utils.isHttpOperationNode;
|
|
620
|
+
exports.jsxDef = require_utils.jsxDef;
|
|
2326
621
|
exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy;
|
|
2327
|
-
exports.narrowSchema = narrowSchema;
|
|
622
|
+
exports.narrowSchema = require_utils.narrowSchema;
|
|
623
|
+
exports.objectBindingPatternDef = require_utils.objectBindingPatternDef;
|
|
624
|
+
exports.operationDef = require_utils.operationDef;
|
|
625
|
+
exports.outputDef = require_utils.outputDef;
|
|
626
|
+
exports.parameterDef = require_utils.parameterDef;
|
|
627
|
+
exports.propertyDef = require_utils.propertyDef;
|
|
628
|
+
exports.requestBodyDef = require_utils.requestBodyDef;
|
|
629
|
+
exports.responseDef = require_utils.responseDef;
|
|
630
|
+
exports.schemaDef = require_utils.schemaDef;
|
|
2328
631
|
exports.schemaTypes = require_utils.schemaTypes;
|
|
2329
632
|
exports.setDiscriminatorEnum = setDiscriminatorEnum;
|
|
2330
633
|
exports.setEnumName = setEnumName;
|
|
2331
634
|
exports.signatureOf = signatureOf;
|
|
2332
635
|
exports.simplifyUnion = simplifyUnion;
|
|
2333
|
-
exports.
|
|
2334
|
-
exports.
|
|
2335
|
-
exports.
|
|
2336
|
-
exports.
|
|
2337
|
-
exports.
|
|
636
|
+
exports.sourceDef = require_utils.sourceDef;
|
|
637
|
+
exports.syncOptionality = require_utils.syncOptionality;
|
|
638
|
+
exports.textDef = require_utils.textDef;
|
|
639
|
+
exports.transform = require_utils.transform;
|
|
640
|
+
exports.typeDef = require_utils.typeDef;
|
|
641
|
+
exports.typeLiteralDef = require_utils.typeLiteralDef;
|
|
642
|
+
exports.walk = require_utils.walk;
|
|
2338
643
|
|
|
2339
644
|
//# sourceMappingURL=index.cjs.map
|