@kubb/ast 5.0.0-beta.58 → 5.0.0-beta.59
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 +10 -0
- package/dist/casing-BE2R1RXg.cjs +88 -0
- package/dist/casing-BE2R1RXg.cjs.map +1 -0
- package/dist/extractStringsFromNodes-WMYJ8nQL.d.ts +82 -0
- package/dist/factory-BmcGBdeg.cjs +1251 -0
- package/dist/factory-BmcGBdeg.cjs.map +1 -0
- package/dist/factory-Du7nEP4B.js +282 -0
- package/dist/factory-Du7nEP4B.js.map +1 -0
- package/dist/factory.cjs +25 -28
- package/dist/factory.d.ts +3 -3
- package/dist/factory.js +3 -3
- package/dist/{ast-ClnJg9BN.d.ts → index-BzjwdK2M.d.ts} +74 -372
- package/dist/index.cjs +445 -46
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +94 -59
- package/dist/index.js +7 -113
- package/dist/index.js.map +1 -1
- package/dist/operationParams-BZ07xDm0.d.ts +204 -0
- package/dist/response-DKxTr522.js +683 -0
- package/dist/response-DKxTr522.js.map +1 -0
- package/dist/{types-CB2oY8Dw.d.ts → types-olVl9v5p.d.ts} +222 -227
- package/dist/types.d.ts +4 -3
- package/dist/utils-BCtRXfhI.cjs +275 -0
- package/dist/utils-BCtRXfhI.cjs.map +1 -0
- package/dist/{utils-DN4XLVqz.js → utils-SdZU0F3H.js} +472 -1235
- package/dist/utils-SdZU0F3H.js.map +1 -0
- package/dist/utils.cjs +127 -22
- package/dist/utils.d.ts +112 -80
- package/dist/utils.js +3 -2
- package/package.json +1 -1
- package/src/constants.ts +8 -14
- package/src/dedupe.ts +8 -0
- package/src/factory.ts +1 -2
- package/src/guards.ts +1 -1
- package/src/index.ts +4 -13
- package/src/nodes/code.ts +29 -47
- package/src/nodes/content.ts +2 -2
- package/src/nodes/file.ts +28 -23
- package/src/nodes/function.ts +0 -15
- package/src/nodes/input.ts +6 -6
- package/src/nodes/operation.ts +1 -1
- package/src/nodes/parameter.ts +0 -3
- package/src/nodes/property.ts +0 -3
- package/src/nodes/requestBody.ts +3 -6
- package/src/nodes/schema.ts +3 -2
- package/src/printer.ts +1 -1
- package/src/registry.ts +31 -26
- package/src/signature.ts +3 -3
- package/src/transformers.ts +28 -1
- package/src/types.ts +2 -53
- package/src/utils/codegen.ts +104 -0
- package/src/utils/fileMerge.ts +184 -0
- package/src/utils/index.ts +7 -339
- package/src/utils/operationParams.ts +353 -0
- package/src/utils/refs.ts +112 -0
- package/src/utils/schemaGraph.ts +169 -0
- package/src/utils/strings.ts +139 -0
- package/src/visitor.ts +43 -19
- package/dist/extractStringsFromNodes-Bn9cOos9.d.ts +0 -14
- package/dist/factory-C5gHvtLU.js +0 -138
- package/dist/factory-C5gHvtLU.js.map +0 -1
- package/dist/factory-JN-Ylfl6.cjs +0 -155
- package/dist/factory-JN-Ylfl6.cjs.map +0 -1
- package/dist/utils-C8bWAzhv.cjs +0 -2696
- package/dist/utils-C8bWAzhv.cjs.map +0 -1
- package/dist/utils-DN4XLVqz.js.map +0 -1
- package/src/utils/ast.ts +0 -816
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,157 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
const require_factory = require("./factory-
|
|
2
|
+
require("./casing-BE2R1RXg.cjs");
|
|
3
|
+
const require_factory = require("./factory-BmcGBdeg.cjs");
|
|
4
|
+
const require_utils = require("./utils-BCtRXfhI.cjs");
|
|
4
5
|
let node_crypto = require("node:crypto");
|
|
6
|
+
//#region src/constants.ts
|
|
7
|
+
const visitorDepths = {
|
|
8
|
+
shallow: "shallow",
|
|
9
|
+
deep: "deep"
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Schema type discriminators used by all AST schema nodes.
|
|
13
|
+
*
|
|
14
|
+
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
|
|
15
|
+
* Call `isScalarPrimitive()` to check for the scalar types.
|
|
16
|
+
*/
|
|
17
|
+
const schemaTypes = {
|
|
18
|
+
/**
|
|
19
|
+
* Text value.
|
|
20
|
+
*/
|
|
21
|
+
string: "string",
|
|
22
|
+
/**
|
|
23
|
+
* Floating-point number (`float`, `double`).
|
|
24
|
+
*/
|
|
25
|
+
number: "number",
|
|
26
|
+
/**
|
|
27
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
28
|
+
*/
|
|
29
|
+
integer: "integer",
|
|
30
|
+
/**
|
|
31
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
32
|
+
*/
|
|
33
|
+
bigint: "bigint",
|
|
34
|
+
/**
|
|
35
|
+
* Boolean value.
|
|
36
|
+
*/
|
|
37
|
+
boolean: "boolean",
|
|
38
|
+
/**
|
|
39
|
+
* Explicit null value.
|
|
40
|
+
*/
|
|
41
|
+
null: "null",
|
|
42
|
+
/**
|
|
43
|
+
* Any value (no type restriction).
|
|
44
|
+
*/
|
|
45
|
+
any: "any",
|
|
46
|
+
/**
|
|
47
|
+
* Unknown value (must be narrowed before usage).
|
|
48
|
+
*/
|
|
49
|
+
unknown: "unknown",
|
|
50
|
+
/**
|
|
51
|
+
* No return value (`void`).
|
|
52
|
+
*/
|
|
53
|
+
void: "void",
|
|
54
|
+
/**
|
|
55
|
+
* Object with named properties.
|
|
56
|
+
*/
|
|
57
|
+
object: "object",
|
|
58
|
+
/**
|
|
59
|
+
* Sequential list of items.
|
|
60
|
+
*/
|
|
61
|
+
array: "array",
|
|
62
|
+
/**
|
|
63
|
+
* Fixed-length list with position-specific items.
|
|
64
|
+
*/
|
|
65
|
+
tuple: "tuple",
|
|
66
|
+
/**
|
|
67
|
+
* "One of" multiple schema members.
|
|
68
|
+
*/
|
|
69
|
+
union: "union",
|
|
70
|
+
/**
|
|
71
|
+
* "All of" multiple schema members.
|
|
72
|
+
*/
|
|
73
|
+
intersection: "intersection",
|
|
74
|
+
/**
|
|
75
|
+
* Enum schema.
|
|
76
|
+
*/
|
|
77
|
+
enum: "enum",
|
|
78
|
+
/**
|
|
79
|
+
* Reference to another schema.
|
|
80
|
+
*/
|
|
81
|
+
ref: "ref",
|
|
82
|
+
/**
|
|
83
|
+
* Calendar date (for example `2026-03-24`).
|
|
84
|
+
*/
|
|
85
|
+
date: "date",
|
|
86
|
+
/**
|
|
87
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
88
|
+
*/
|
|
89
|
+
datetime: "datetime",
|
|
90
|
+
/**
|
|
91
|
+
* Time-only value (for example `09:00:00`).
|
|
92
|
+
*/
|
|
93
|
+
time: "time",
|
|
94
|
+
/**
|
|
95
|
+
* UUID value.
|
|
96
|
+
*/
|
|
97
|
+
uuid: "uuid",
|
|
98
|
+
/**
|
|
99
|
+
* Email address value.
|
|
100
|
+
*/
|
|
101
|
+
email: "email",
|
|
102
|
+
/**
|
|
103
|
+
* URL value.
|
|
104
|
+
*/
|
|
105
|
+
url: "url",
|
|
106
|
+
/**
|
|
107
|
+
* IPv4 address value.
|
|
108
|
+
*/
|
|
109
|
+
ipv4: "ipv4",
|
|
110
|
+
/**
|
|
111
|
+
* IPv6 address value.
|
|
112
|
+
*/
|
|
113
|
+
ipv6: "ipv6",
|
|
114
|
+
/**
|
|
115
|
+
* Binary/blob value.
|
|
116
|
+
*/
|
|
117
|
+
blob: "blob",
|
|
118
|
+
/**
|
|
119
|
+
* Impossible value (`never`).
|
|
120
|
+
*/
|
|
121
|
+
never: "never"
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Scalar primitive schema types used for union simplification and type narrowing.
|
|
125
|
+
*/
|
|
126
|
+
const SCALAR_PRIMITIVE_TYPES = new Set([
|
|
127
|
+
"string",
|
|
128
|
+
"number",
|
|
129
|
+
"integer",
|
|
130
|
+
"bigint",
|
|
131
|
+
"boolean"
|
|
132
|
+
]);
|
|
133
|
+
/**
|
|
134
|
+
* Returns `true` when `type` is a scalar primitive that can be assigned without wrapping
|
|
135
|
+
* (for example `string | number | boolean`).
|
|
136
|
+
*/
|
|
137
|
+
function isScalarPrimitive(type) {
|
|
138
|
+
return SCALAR_PRIMITIVE_TYPES.has(type);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* HTTP method identifiers used by operation nodes.
|
|
142
|
+
*/
|
|
143
|
+
const httpMethods = {
|
|
144
|
+
get: "GET",
|
|
145
|
+
post: "POST",
|
|
146
|
+
put: "PUT",
|
|
147
|
+
patch: "PATCH",
|
|
148
|
+
delete: "DELETE",
|
|
149
|
+
head: "HEAD",
|
|
150
|
+
options: "OPTIONS",
|
|
151
|
+
trace: "TRACE"
|
|
152
|
+
};
|
|
153
|
+
Array.from({ length: 2 }, () => " ").join("");
|
|
154
|
+
//#endregion
|
|
5
155
|
//#region src/signature.ts
|
|
6
156
|
/**
|
|
7
157
|
* The flags shared by every node kind that affect its type: `primitive`, `format`, `nullable`.
|
|
@@ -228,9 +378,9 @@ function describeShape(node) {
|
|
|
228
378
|
return parts.join("|");
|
|
229
379
|
}
|
|
230
380
|
/**
|
|
231
|
-
*
|
|
232
|
-
* hashed during dedupe planning is not walked again when it is rewritten during streaming.
|
|
233
|
-
* is safe because a signature depends only on content, and nodes are immutable once created.
|
|
381
|
+
* Caches the digest per node, keyed by identity. A `WeakMap` so entries die with the node, and so
|
|
382
|
+
* a tree hashed during dedupe planning is not walked again when it is rewritten during streaming.
|
|
383
|
+
* Reuse is safe because a signature depends only on content, and nodes are immutable once created.
|
|
234
384
|
*/
|
|
235
385
|
const signatureCache = /* @__PURE__ */ new WeakMap();
|
|
236
386
|
/**
|
|
@@ -254,13 +404,261 @@ function signatureOf(node) {
|
|
|
254
404
|
return signature;
|
|
255
405
|
}
|
|
256
406
|
//#endregion
|
|
407
|
+
//#region src/registry.ts
|
|
408
|
+
/**
|
|
409
|
+
* Every node definition. Adding a node means adding its `defineNode` to one
|
|
410
|
+
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
|
|
411
|
+
*/
|
|
412
|
+
const nodeDefs = [
|
|
413
|
+
require_factory.inputDef,
|
|
414
|
+
require_factory.outputDef,
|
|
415
|
+
require_factory.operationDef,
|
|
416
|
+
require_factory.requestBodyDef,
|
|
417
|
+
require_factory.contentDef,
|
|
418
|
+
require_factory.responseDef,
|
|
419
|
+
require_factory.schemaDef,
|
|
420
|
+
require_factory.propertyDef,
|
|
421
|
+
require_factory.parameterDef,
|
|
422
|
+
require_factory.functionParameterDef,
|
|
423
|
+
require_factory.functionParametersDef,
|
|
424
|
+
require_factory.typeLiteralDef,
|
|
425
|
+
require_factory.indexedAccessTypeDef,
|
|
426
|
+
require_factory.objectBindingPatternDef,
|
|
427
|
+
require_factory.constDef,
|
|
428
|
+
require_factory.typeDef,
|
|
429
|
+
require_factory.functionDef,
|
|
430
|
+
require_factory.arrowFunctionDef,
|
|
431
|
+
require_factory.textDef,
|
|
432
|
+
require_factory.breakDef,
|
|
433
|
+
require_factory.jsxDef,
|
|
434
|
+
require_factory.importDef,
|
|
435
|
+
require_factory.exportDef,
|
|
436
|
+
require_factory.sourceDef,
|
|
437
|
+
require_factory.fileDef
|
|
438
|
+
];
|
|
439
|
+
//#endregion
|
|
440
|
+
//#region src/visitor.ts
|
|
441
|
+
/**
|
|
442
|
+
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
|
|
443
|
+
* Derived from each definition's `children`.
|
|
444
|
+
*/
|
|
445
|
+
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
|
|
446
|
+
/**
|
|
447
|
+
* Maps a node kind to the matching visitor callback name. Derived from each
|
|
448
|
+
* definition's `visitorKey`.
|
|
449
|
+
*/
|
|
450
|
+
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
|
|
451
|
+
/**
|
|
452
|
+
* Per-kind builders rerun after children are rebuilt. Derived from each
|
|
453
|
+
* definition's `rebuild` flag.
|
|
454
|
+
*/
|
|
455
|
+
const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : []));
|
|
456
|
+
/**
|
|
457
|
+
* Creates a small async concurrency limiter.
|
|
458
|
+
*
|
|
459
|
+
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
|
|
460
|
+
*
|
|
461
|
+
* @example
|
|
462
|
+
* ```ts
|
|
463
|
+
* const limit = createLimit(2)
|
|
464
|
+
* for (const task of [taskA, taskB, taskC]) {
|
|
465
|
+
* await limit(() => task())
|
|
466
|
+
* }
|
|
467
|
+
* // only 2 tasks run at the same time
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
470
|
+
function createLimit(concurrency) {
|
|
471
|
+
let active = 0;
|
|
472
|
+
const queue = [];
|
|
473
|
+
function next() {
|
|
474
|
+
if (active < concurrency && queue.length > 0) {
|
|
475
|
+
active++;
|
|
476
|
+
queue.shift()();
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return function limit(fn) {
|
|
480
|
+
return new Promise((resolve, reject) => {
|
|
481
|
+
queue.push(() => {
|
|
482
|
+
Promise.resolve(fn()).then(resolve, reject).finally(() => {
|
|
483
|
+
active--;
|
|
484
|
+
next();
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
next();
|
|
488
|
+
});
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
const visitorKeysByKind = VISITOR_KEYS;
|
|
492
|
+
/**
|
|
493
|
+
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
|
|
494
|
+
*/
|
|
495
|
+
function isNode(value) {
|
|
496
|
+
return typeof value === "object" && value !== null && "kind" in value;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
|
|
500
|
+
*
|
|
501
|
+
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
* ```ts
|
|
505
|
+
* const children = getChildren(operationNode, true)
|
|
506
|
+
* // returns parameters, the request body, and responses
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
509
|
+
function* getChildren(node, recurse) {
|
|
510
|
+
if (node.kind === "Schema" && !recurse) return;
|
|
511
|
+
const keys = visitorKeysByKind[node.kind];
|
|
512
|
+
if (!keys) return;
|
|
513
|
+
const record = node;
|
|
514
|
+
for (const key of keys) {
|
|
515
|
+
const value = record[key];
|
|
516
|
+
if (Array.isArray(value)) {
|
|
517
|
+
for (const item of value) if (isNode(item)) yield item;
|
|
518
|
+
} else if (isNode(value)) yield value;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Runs the visitor callback that matches `node.kind` with the traversal
|
|
523
|
+
* context. The result is a replacement node, a collected value, or `undefined`
|
|
524
|
+
* when no callback is registered for the kind.
|
|
525
|
+
*
|
|
526
|
+
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
|
|
527
|
+
* in one place. `TResult` is the caller's expected return: the same node type
|
|
528
|
+
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
|
|
529
|
+
*/
|
|
530
|
+
function applyVisitor(node, visitor, parent) {
|
|
531
|
+
const key = VISITOR_KEY_BY_KIND[node.kind];
|
|
532
|
+
if (!key) return void 0;
|
|
533
|
+
const fn = visitor[key];
|
|
534
|
+
return fn?.(node, { parent });
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Async depth-first traversal for side effects. Visitor return values are
|
|
538
|
+
* ignored. Use `transform` when you want to rewrite nodes.
|
|
539
|
+
*
|
|
540
|
+
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
541
|
+
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
542
|
+
* work. Lower values reduce memory pressure.
|
|
543
|
+
*
|
|
544
|
+
* @example Log every operation
|
|
545
|
+
* ```ts
|
|
546
|
+
* await walk(root, {
|
|
547
|
+
* operation(node) {
|
|
548
|
+
* console.log(node.operationId)
|
|
549
|
+
* },
|
|
550
|
+
* })
|
|
551
|
+
* ```
|
|
552
|
+
*
|
|
553
|
+
* @example Only visit the root node
|
|
554
|
+
* ```ts
|
|
555
|
+
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
556
|
+
* ```
|
|
557
|
+
*/
|
|
558
|
+
async function walk(node, options) {
|
|
559
|
+
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
|
|
560
|
+
}
|
|
561
|
+
async function _walk(node, visitor, recurse, limit, parent) {
|
|
562
|
+
await limit(() => applyVisitor(node, visitor, parent));
|
|
563
|
+
const children = Array.from(getChildren(node, recurse));
|
|
564
|
+
if (children.length === 0) return;
|
|
565
|
+
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
|
|
566
|
+
}
|
|
567
|
+
function transform(node, options) {
|
|
568
|
+
const { depth, parent, ...visitor } = options;
|
|
569
|
+
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
570
|
+
const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
|
|
571
|
+
if (rebuilt === node) return node;
|
|
572
|
+
const rebuild = nodeRebuilders[rebuilt.kind];
|
|
573
|
+
return rebuild ? rebuild(rebuilt) : rebuilt;
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
|
|
577
|
+
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
|
|
578
|
+
* `Schema` children are skipped in shallow mode.
|
|
579
|
+
*/
|
|
580
|
+
function transformChildren(node, options, recurse) {
|
|
581
|
+
if (node.kind === "Schema" && !recurse) return node;
|
|
582
|
+
const keys = visitorKeysByKind[node.kind];
|
|
583
|
+
if (!keys) return node;
|
|
584
|
+
const record = node;
|
|
585
|
+
const childOptions = {
|
|
586
|
+
...options,
|
|
587
|
+
parent: node
|
|
588
|
+
};
|
|
589
|
+
let updates;
|
|
590
|
+
for (const key of keys) {
|
|
591
|
+
if (!(key in record)) continue;
|
|
592
|
+
const value = record[key];
|
|
593
|
+
if (Array.isArray(value)) {
|
|
594
|
+
let changed = false;
|
|
595
|
+
const mapped = value.map((item) => {
|
|
596
|
+
if (!isNode(item)) return item;
|
|
597
|
+
const next = transform(item, childOptions);
|
|
598
|
+
if (next !== item) changed = true;
|
|
599
|
+
return next;
|
|
600
|
+
});
|
|
601
|
+
if (changed) (updates ??= {})[key] = mapped;
|
|
602
|
+
} else if (isNode(value)) {
|
|
603
|
+
const next = transform(value, childOptions);
|
|
604
|
+
if (next !== value) (updates ??= {})[key] = next;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
return updates ? {
|
|
608
|
+
...node,
|
|
609
|
+
...updates
|
|
610
|
+
} : node;
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Lazy depth-first collection pass. Yields every non-null value returned by
|
|
614
|
+
* the visitor callbacks. Use `collect` for the eager array form.
|
|
615
|
+
*
|
|
616
|
+
* @example Collect every operationId
|
|
617
|
+
* ```ts
|
|
618
|
+
* const ids: string[] = []
|
|
619
|
+
* for (const id of collectLazy<string>(root, {
|
|
620
|
+
* operation(node) {
|
|
621
|
+
* return node.operationId
|
|
622
|
+
* },
|
|
623
|
+
* })) {
|
|
624
|
+
* ids.push(id)
|
|
625
|
+
* }
|
|
626
|
+
* ```
|
|
627
|
+
*/
|
|
628
|
+
function* collectLazy(node, options) {
|
|
629
|
+
const { depth, parent, ...visitor } = options;
|
|
630
|
+
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
631
|
+
const v = applyVisitor(node, visitor, parent);
|
|
632
|
+
if (v != null) yield v;
|
|
633
|
+
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
|
|
634
|
+
...options,
|
|
635
|
+
parent: node
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
640
|
+
* callbacks return into an array.
|
|
641
|
+
*
|
|
642
|
+
* @example Collect every operationId
|
|
643
|
+
* ```ts
|
|
644
|
+
* const ids = collect<string>(root, {
|
|
645
|
+
* operation(node) {
|
|
646
|
+
* return node.operationId
|
|
647
|
+
* },
|
|
648
|
+
* })
|
|
649
|
+
* ```
|
|
650
|
+
*/
|
|
651
|
+
function collect(node, options) {
|
|
652
|
+
return Array.from(collectLazy(node, options));
|
|
653
|
+
}
|
|
654
|
+
//#endregion
|
|
257
655
|
//#region src/dedupe.ts
|
|
258
656
|
/**
|
|
259
657
|
* Builds the shared `ref` replacement for a duplicate occurrence, carrying the
|
|
260
658
|
* usage-slot and documentation fields that are not part of the canonical type.
|
|
261
659
|
*/
|
|
262
660
|
function createRefNode(node, canonical) {
|
|
263
|
-
return
|
|
661
|
+
return require_factory.createSchema({
|
|
264
662
|
type: "ref",
|
|
265
663
|
name: canonical.name,
|
|
266
664
|
ref: canonical.ref,
|
|
@@ -278,7 +676,7 @@ function applyDedupe(node, plan, skipRootMatch = false) {
|
|
|
278
676
|
const { canonicalBySignature, aliasNames } = plan;
|
|
279
677
|
if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node;
|
|
280
678
|
const root = node;
|
|
281
|
-
return
|
|
679
|
+
return transform(node, { schema(schemaNode) {
|
|
282
680
|
if (schemaNode.type === "ref") {
|
|
283
681
|
const target = schemaNode.ref ? require_utils.extractRefName(schemaNode.ref) : schemaNode.name;
|
|
284
682
|
const canonical = target ? aliasNames.get(target) : void 0;
|
|
@@ -346,7 +744,7 @@ function buildDedupePlan(roots, options) {
|
|
|
346
744
|
}
|
|
347
745
|
for (const root of roots) {
|
|
348
746
|
if (root.kind === "Schema") topLevelNodes.add(root);
|
|
349
|
-
for (const schemaNode of
|
|
747
|
+
for (const schemaNode of collectLazy(root, { schema: (node) => node })) record(schemaNode);
|
|
350
748
|
}
|
|
351
749
|
const canonicalBySignature = /* @__PURE__ */ new Map();
|
|
352
750
|
const aliasNames = /* @__PURE__ */ new Map();
|
|
@@ -502,13 +900,13 @@ function setDiscriminatorEnum({ node, propertyName, values, enumName }) {
|
|
|
502
900
|
const objectNode = require_utils.narrowSchema(node, "object");
|
|
503
901
|
if (!objectNode?.properties?.length) return node;
|
|
504
902
|
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node;
|
|
505
|
-
return
|
|
903
|
+
return require_factory.createSchema({
|
|
506
904
|
...objectNode,
|
|
507
905
|
properties: objectNode.properties.map((prop) => {
|
|
508
906
|
if (prop.name !== propertyName) return prop;
|
|
509
|
-
return
|
|
907
|
+
return require_factory.createProperty({
|
|
510
908
|
...prop,
|
|
511
|
-
schema:
|
|
909
|
+
schema: require_factory.createSchema({
|
|
512
910
|
type: "enum",
|
|
513
911
|
primitive: "string",
|
|
514
912
|
enumValues: values,
|
|
@@ -538,7 +936,7 @@ function* mergeAdjacentObjectsLazy(members) {
|
|
|
538
936
|
if (objectMember && !objectMember.name && acc !== void 0) {
|
|
539
937
|
const accObject = require_utils.narrowSchema(acc, "object");
|
|
540
938
|
if (accObject && !accObject.name) {
|
|
541
|
-
acc =
|
|
939
|
+
acc = require_factory.createSchema({
|
|
542
940
|
...accObject,
|
|
543
941
|
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
|
|
544
942
|
});
|
|
@@ -563,7 +961,7 @@ function* mergeAdjacentObjectsLazy(members) {
|
|
|
563
961
|
* ```
|
|
564
962
|
*/
|
|
565
963
|
function simplifyUnion(members) {
|
|
566
|
-
const scalarPrimitives = new Set(members.filter((member) =>
|
|
964
|
+
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
|
|
567
965
|
if (!scalarPrimitives.size) return members;
|
|
568
966
|
return members.filter((member) => {
|
|
569
967
|
const enumNode = require_utils.narrowSchema(member, "enum");
|
|
@@ -590,55 +988,56 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
|
590
988
|
}
|
|
591
989
|
//#endregion
|
|
592
990
|
exports.applyDedupe = applyDedupe;
|
|
593
|
-
exports.arrowFunctionDef =
|
|
594
|
-
exports.breakDef =
|
|
991
|
+
exports.arrowFunctionDef = require_factory.arrowFunctionDef;
|
|
992
|
+
exports.breakDef = require_factory.breakDef;
|
|
595
993
|
exports.buildDedupePlan = buildDedupePlan;
|
|
596
|
-
exports.collect =
|
|
597
|
-
exports.constDef =
|
|
598
|
-
exports.contentDef =
|
|
994
|
+
exports.collect = collect;
|
|
995
|
+
exports.constDef = require_factory.constDef;
|
|
996
|
+
exports.contentDef = require_factory.contentDef;
|
|
599
997
|
exports.createPrinterFactory = createPrinterFactory;
|
|
600
|
-
exports.defineNode =
|
|
998
|
+
exports.defineNode = require_factory.defineNode;
|
|
601
999
|
exports.definePrinter = definePrinter;
|
|
602
1000
|
exports.defineSchemaDialect = defineSchemaDialect;
|
|
603
|
-
exports.exportDef =
|
|
604
|
-
exports.extractStringsFromNodes =
|
|
1001
|
+
exports.exportDef = require_factory.exportDef;
|
|
1002
|
+
exports.extractStringsFromNodes = require_factory.extractStringsFromNodes;
|
|
605
1003
|
Object.defineProperty(exports, "factory", {
|
|
606
1004
|
enumerable: true,
|
|
607
1005
|
get: function() {
|
|
608
1006
|
return require_factory.factory_exports;
|
|
609
1007
|
}
|
|
610
1008
|
});
|
|
611
|
-
exports.fileDef =
|
|
612
|
-
exports.functionDef =
|
|
613
|
-
exports.functionParameterDef =
|
|
614
|
-
exports.functionParametersDef =
|
|
615
|
-
exports.httpMethods =
|
|
616
|
-
exports.importDef =
|
|
617
|
-
exports.indexedAccessTypeDef =
|
|
618
|
-
exports.inputDef =
|
|
1009
|
+
exports.fileDef = require_factory.fileDef;
|
|
1010
|
+
exports.functionDef = require_factory.functionDef;
|
|
1011
|
+
exports.functionParameterDef = require_factory.functionParameterDef;
|
|
1012
|
+
exports.functionParametersDef = require_factory.functionParametersDef;
|
|
1013
|
+
exports.httpMethods = httpMethods;
|
|
1014
|
+
exports.importDef = require_factory.importDef;
|
|
1015
|
+
exports.indexedAccessTypeDef = require_factory.indexedAccessTypeDef;
|
|
1016
|
+
exports.inputDef = require_factory.inputDef;
|
|
619
1017
|
exports.isHttpOperationNode = require_utils.isHttpOperationNode;
|
|
620
|
-
exports.jsxDef =
|
|
1018
|
+
exports.jsxDef = require_factory.jsxDef;
|
|
621
1019
|
exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy;
|
|
622
1020
|
exports.narrowSchema = require_utils.narrowSchema;
|
|
623
|
-
exports.
|
|
624
|
-
exports.
|
|
625
|
-
exports.
|
|
626
|
-
exports.
|
|
627
|
-
exports.
|
|
628
|
-
exports.
|
|
629
|
-
exports.
|
|
630
|
-
exports.
|
|
631
|
-
exports.
|
|
1021
|
+
exports.nodeDefs = nodeDefs;
|
|
1022
|
+
exports.objectBindingPatternDef = require_factory.objectBindingPatternDef;
|
|
1023
|
+
exports.operationDef = require_factory.operationDef;
|
|
1024
|
+
exports.outputDef = require_factory.outputDef;
|
|
1025
|
+
exports.parameterDef = require_factory.parameterDef;
|
|
1026
|
+
exports.propertyDef = require_factory.propertyDef;
|
|
1027
|
+
exports.requestBodyDef = require_factory.requestBodyDef;
|
|
1028
|
+
exports.responseDef = require_factory.responseDef;
|
|
1029
|
+
exports.schemaDef = require_factory.schemaDef;
|
|
1030
|
+
exports.schemaTypes = schemaTypes;
|
|
632
1031
|
exports.setDiscriminatorEnum = setDiscriminatorEnum;
|
|
633
1032
|
exports.setEnumName = setEnumName;
|
|
634
1033
|
exports.signatureOf = signatureOf;
|
|
635
1034
|
exports.simplifyUnion = simplifyUnion;
|
|
636
|
-
exports.sourceDef =
|
|
637
|
-
exports.syncOptionality =
|
|
638
|
-
exports.textDef =
|
|
639
|
-
exports.transform =
|
|
640
|
-
exports.typeDef =
|
|
641
|
-
exports.typeLiteralDef =
|
|
642
|
-
exports.walk =
|
|
1035
|
+
exports.sourceDef = require_factory.sourceDef;
|
|
1036
|
+
exports.syncOptionality = require_factory.syncOptionality;
|
|
1037
|
+
exports.textDef = require_factory.textDef;
|
|
1038
|
+
exports.transform = transform;
|
|
1039
|
+
exports.typeDef = require_factory.typeDef;
|
|
1040
|
+
exports.typeLiteralDef = require_factory.typeLiteralDef;
|
|
1041
|
+
exports.walk = walk;
|
|
643
1042
|
|
|
644
1043
|
//# sourceMappingURL=index.cjs.map
|