@kubb/ast 5.0.0-beta.59 → 5.0.0-beta.60
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/dist/factory-Cl8Z7mcc.cjs +299 -0
- package/dist/factory-Cl8Z7mcc.cjs.map +1 -0
- package/dist/factory.cjs +26 -25
- package/dist/index.cjs +44 -548
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +9 -9
- package/dist/index.js +1 -1
- package/dist/{factory-BmcGBdeg.cjs → response-DS5S3IG4.cjs} +155 -348
- package/dist/response-DS5S3IG4.cjs.map +1 -0
- package/dist/utils-D83JA6Xx.cjs +1645 -0
- package/dist/utils-D83JA6Xx.cjs.map +1 -0
- package/dist/{utils-SdZU0F3H.js → utils-Dj_KoXMv.js} +55 -2
- package/dist/utils-Dj_KoXMv.js.map +1 -0
- package/dist/utils.cjs +28 -127
- package/dist/utils.d.ts +91 -2
- package/dist/utils.js +2 -2
- package/package.json +1 -2
- package/src/utils/index.ts +3 -1
- package/src/utils/schemaTraversal.ts +86 -0
- package/dist/casing-BE2R1RXg.cjs +0 -88
- package/dist/casing-BE2R1RXg.cjs.map +0 -1
- package/dist/factory-BmcGBdeg.cjs.map +0 -1
- package/dist/utils-BCtRXfhI.cjs +0 -275
- package/dist/utils-BCtRXfhI.cjs.map +0 -1
- package/dist/utils-SdZU0F3H.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,157 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
require("./
|
|
3
|
-
const
|
|
4
|
-
const
|
|
2
|
+
const require_response = require("./response-DS5S3IG4.cjs");
|
|
3
|
+
const require_utils = require("./utils-D83JA6Xx.cjs");
|
|
4
|
+
const require_factory = require("./factory-Cl8Z7mcc.cjs");
|
|
5
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
|
|
155
6
|
//#region src/signature.ts
|
|
156
7
|
/**
|
|
157
8
|
* The flags shared by every node kind that affect its type: `primitive`, `format`, `nullable`.
|
|
@@ -404,261 +255,13 @@ function signatureOf(node) {
|
|
|
404
255
|
return signature;
|
|
405
256
|
}
|
|
406
257
|
//#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
|
|
655
258
|
//#region src/dedupe.ts
|
|
656
259
|
/**
|
|
657
260
|
* Builds the shared `ref` replacement for a duplicate occurrence, carrying the
|
|
658
261
|
* usage-slot and documentation fields that are not part of the canonical type.
|
|
659
262
|
*/
|
|
660
263
|
function createRefNode(node, canonical) {
|
|
661
|
-
return
|
|
264
|
+
return require_response.createSchema({
|
|
662
265
|
type: "ref",
|
|
663
266
|
name: canonical.name,
|
|
664
267
|
ref: canonical.ref,
|
|
@@ -676,7 +279,7 @@ function applyDedupe(node, plan, skipRootMatch = false) {
|
|
|
676
279
|
const { canonicalBySignature, aliasNames } = plan;
|
|
677
280
|
if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node;
|
|
678
281
|
const root = node;
|
|
679
|
-
return transform(node, { schema(schemaNode) {
|
|
282
|
+
return require_utils.transform(node, { schema(schemaNode) {
|
|
680
283
|
if (schemaNode.type === "ref") {
|
|
681
284
|
const target = schemaNode.ref ? require_utils.extractRefName(schemaNode.ref) : schemaNode.name;
|
|
682
285
|
const canonical = target ? aliasNames.get(target) : void 0;
|
|
@@ -744,7 +347,7 @@ function buildDedupePlan(roots, options) {
|
|
|
744
347
|
}
|
|
745
348
|
for (const root of roots) {
|
|
746
349
|
if (root.kind === "Schema") topLevelNodes.add(root);
|
|
747
|
-
for (const schemaNode of collectLazy(root, { schema: (node) => node })) record(schemaNode);
|
|
350
|
+
for (const schemaNode of require_utils.collectLazy(root, { schema: (node) => node })) record(schemaNode);
|
|
748
351
|
}
|
|
749
352
|
const canonicalBySignature = /* @__PURE__ */ new Map();
|
|
750
353
|
const aliasNames = /* @__PURE__ */ new Map();
|
|
@@ -880,164 +483,57 @@ function createPrinterFactory(getKey) {
|
|
|
880
483
|
};
|
|
881
484
|
}
|
|
882
485
|
//#endregion
|
|
883
|
-
//#region src/transformers.ts
|
|
884
|
-
/**
|
|
885
|
-
* Replaces a discriminator property's schema with a string enum of allowed values.
|
|
886
|
-
*
|
|
887
|
-
* If `node` is not an object schema, or if the property does not exist, the input
|
|
888
|
-
* node is returned as-is.
|
|
889
|
-
*
|
|
890
|
-
* @example
|
|
891
|
-
* ```ts
|
|
892
|
-
* const schema = createSchema({
|
|
893
|
-
* type: 'object',
|
|
894
|
-
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
|
|
895
|
-
* })
|
|
896
|
-
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
|
|
897
|
-
* ```
|
|
898
|
-
*/
|
|
899
|
-
function setDiscriminatorEnum({ node, propertyName, values, enumName }) {
|
|
900
|
-
const objectNode = require_utils.narrowSchema(node, "object");
|
|
901
|
-
if (!objectNode?.properties?.length) return node;
|
|
902
|
-
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node;
|
|
903
|
-
return require_factory.createSchema({
|
|
904
|
-
...objectNode,
|
|
905
|
-
properties: objectNode.properties.map((prop) => {
|
|
906
|
-
if (prop.name !== propertyName) return prop;
|
|
907
|
-
return require_factory.createProperty({
|
|
908
|
-
...prop,
|
|
909
|
-
schema: require_factory.createSchema({
|
|
910
|
-
type: "enum",
|
|
911
|
-
primitive: "string",
|
|
912
|
-
enumValues: values,
|
|
913
|
-
name: enumName,
|
|
914
|
-
readOnly: prop.schema.readOnly,
|
|
915
|
-
writeOnly: prop.schema.writeOnly
|
|
916
|
-
})
|
|
917
|
-
});
|
|
918
|
-
})
|
|
919
|
-
});
|
|
920
|
-
}
|
|
921
|
-
/**
|
|
922
|
-
* Merges adjacent anonymous object members into a single anonymous object member.
|
|
923
|
-
*
|
|
924
|
-
* @example
|
|
925
|
-
* ```ts
|
|
926
|
-
* const merged = mergeAdjacentObjects([
|
|
927
|
-
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
|
|
928
|
-
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
|
|
929
|
-
* ])
|
|
930
|
-
* ```
|
|
931
|
-
*/
|
|
932
|
-
function* mergeAdjacentObjectsLazy(members) {
|
|
933
|
-
let acc;
|
|
934
|
-
for (const member of members) {
|
|
935
|
-
const objectMember = require_utils.narrowSchema(member, "object");
|
|
936
|
-
if (objectMember && !objectMember.name && acc !== void 0) {
|
|
937
|
-
const accObject = require_utils.narrowSchema(acc, "object");
|
|
938
|
-
if (accObject && !accObject.name) {
|
|
939
|
-
acc = require_factory.createSchema({
|
|
940
|
-
...accObject,
|
|
941
|
-
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
|
|
942
|
-
});
|
|
943
|
-
continue;
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
if (acc !== void 0) yield acc;
|
|
947
|
-
acc = member;
|
|
948
|
-
}
|
|
949
|
-
if (acc !== void 0) yield acc;
|
|
950
|
-
}
|
|
951
|
-
/**
|
|
952
|
-
* Removes enum members that are covered by broader scalar primitives in the same union.
|
|
953
|
-
*
|
|
954
|
-
* @example
|
|
955
|
-
* ```ts
|
|
956
|
-
* const simplified = simplifyUnion([
|
|
957
|
-
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
|
|
958
|
-
* createSchema({ type: 'string' }),
|
|
959
|
-
* ])
|
|
960
|
-
* // keeps only string member
|
|
961
|
-
* ```
|
|
962
|
-
*/
|
|
963
|
-
function simplifyUnion(members) {
|
|
964
|
-
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
|
|
965
|
-
if (!scalarPrimitives.size) return members;
|
|
966
|
-
return members.filter((member) => {
|
|
967
|
-
const enumNode = require_utils.narrowSchema(member, "enum");
|
|
968
|
-
if (!enumNode) return true;
|
|
969
|
-
const primitive = enumNode.primitive;
|
|
970
|
-
if (!primitive) return true;
|
|
971
|
-
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
|
|
972
|
-
if (scalarPrimitives.has(primitive)) return false;
|
|
973
|
-
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
|
|
974
|
-
return true;
|
|
975
|
-
});
|
|
976
|
-
}
|
|
977
|
-
function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
978
|
-
const enumNode = require_utils.narrowSchema(propNode, "enum");
|
|
979
|
-
if (enumNode?.primitive === "boolean") return {
|
|
980
|
-
...propNode,
|
|
981
|
-
name: null
|
|
982
|
-
};
|
|
983
|
-
if (enumNode) return {
|
|
984
|
-
...propNode,
|
|
985
|
-
name: require_utils.enumPropName(parentName, propName, enumSuffix)
|
|
986
|
-
};
|
|
987
|
-
return propNode;
|
|
988
|
-
}
|
|
989
|
-
//#endregion
|
|
990
486
|
exports.applyDedupe = applyDedupe;
|
|
991
|
-
exports.arrowFunctionDef =
|
|
992
|
-
exports.breakDef =
|
|
487
|
+
exports.arrowFunctionDef = require_response.arrowFunctionDef;
|
|
488
|
+
exports.breakDef = require_response.breakDef;
|
|
993
489
|
exports.buildDedupePlan = buildDedupePlan;
|
|
994
|
-
exports.collect = collect;
|
|
995
|
-
exports.constDef =
|
|
996
|
-
exports.contentDef =
|
|
490
|
+
exports.collect = require_utils.collect;
|
|
491
|
+
exports.constDef = require_response.constDef;
|
|
492
|
+
exports.contentDef = require_response.contentDef;
|
|
997
493
|
exports.createPrinterFactory = createPrinterFactory;
|
|
998
|
-
exports.defineNode =
|
|
494
|
+
exports.defineNode = require_response.defineNode;
|
|
999
495
|
exports.definePrinter = definePrinter;
|
|
1000
496
|
exports.defineSchemaDialect = defineSchemaDialect;
|
|
1001
|
-
exports.exportDef =
|
|
1002
|
-
exports.extractStringsFromNodes =
|
|
497
|
+
exports.exportDef = require_response.exportDef;
|
|
498
|
+
exports.extractStringsFromNodes = require_response.extractStringsFromNodes;
|
|
1003
499
|
Object.defineProperty(exports, "factory", {
|
|
1004
500
|
enumerable: true,
|
|
1005
501
|
get: function() {
|
|
1006
502
|
return require_factory.factory_exports;
|
|
1007
503
|
}
|
|
1008
504
|
});
|
|
1009
|
-
exports.fileDef =
|
|
1010
|
-
exports.functionDef =
|
|
1011
|
-
exports.functionParameterDef =
|
|
1012
|
-
exports.functionParametersDef =
|
|
1013
|
-
exports.httpMethods = httpMethods;
|
|
1014
|
-
exports.importDef =
|
|
1015
|
-
exports.indexedAccessTypeDef =
|
|
1016
|
-
exports.inputDef =
|
|
505
|
+
exports.fileDef = require_response.fileDef;
|
|
506
|
+
exports.functionDef = require_response.functionDef;
|
|
507
|
+
exports.functionParameterDef = require_response.functionParameterDef;
|
|
508
|
+
exports.functionParametersDef = require_response.functionParametersDef;
|
|
509
|
+
exports.httpMethods = require_utils.httpMethods;
|
|
510
|
+
exports.importDef = require_response.importDef;
|
|
511
|
+
exports.indexedAccessTypeDef = require_response.indexedAccessTypeDef;
|
|
512
|
+
exports.inputDef = require_response.inputDef;
|
|
1017
513
|
exports.isHttpOperationNode = require_utils.isHttpOperationNode;
|
|
1018
|
-
exports.jsxDef =
|
|
1019
|
-
exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy;
|
|
514
|
+
exports.jsxDef = require_response.jsxDef;
|
|
515
|
+
exports.mergeAdjacentObjectsLazy = require_utils.mergeAdjacentObjectsLazy;
|
|
1020
516
|
exports.narrowSchema = require_utils.narrowSchema;
|
|
1021
|
-
exports.nodeDefs = nodeDefs;
|
|
1022
|
-
exports.objectBindingPatternDef =
|
|
1023
|
-
exports.operationDef =
|
|
1024
|
-
exports.outputDef =
|
|
1025
|
-
exports.parameterDef =
|
|
1026
|
-
exports.propertyDef =
|
|
1027
|
-
exports.requestBodyDef =
|
|
1028
|
-
exports.responseDef =
|
|
1029
|
-
exports.schemaDef =
|
|
1030
|
-
exports.schemaTypes = schemaTypes;
|
|
1031
|
-
exports.setDiscriminatorEnum = setDiscriminatorEnum;
|
|
1032
|
-
exports.setEnumName = setEnumName;
|
|
517
|
+
exports.nodeDefs = require_utils.nodeDefs;
|
|
518
|
+
exports.objectBindingPatternDef = require_response.objectBindingPatternDef;
|
|
519
|
+
exports.operationDef = require_response.operationDef;
|
|
520
|
+
exports.outputDef = require_response.outputDef;
|
|
521
|
+
exports.parameterDef = require_response.parameterDef;
|
|
522
|
+
exports.propertyDef = require_response.propertyDef;
|
|
523
|
+
exports.requestBodyDef = require_response.requestBodyDef;
|
|
524
|
+
exports.responseDef = require_response.responseDef;
|
|
525
|
+
exports.schemaDef = require_response.schemaDef;
|
|
526
|
+
exports.schemaTypes = require_utils.schemaTypes;
|
|
527
|
+
exports.setDiscriminatorEnum = require_utils.setDiscriminatorEnum;
|
|
528
|
+
exports.setEnumName = require_utils.setEnumName;
|
|
1033
529
|
exports.signatureOf = signatureOf;
|
|
1034
|
-
exports.simplifyUnion = simplifyUnion;
|
|
1035
|
-
exports.sourceDef =
|
|
1036
|
-
exports.syncOptionality =
|
|
1037
|
-
exports.textDef =
|
|
1038
|
-
exports.transform = transform;
|
|
1039
|
-
exports.typeDef =
|
|
1040
|
-
exports.typeLiteralDef =
|
|
1041
|
-
exports.walk = walk;
|
|
530
|
+
exports.simplifyUnion = require_utils.simplifyUnion;
|
|
531
|
+
exports.sourceDef = require_response.sourceDef;
|
|
532
|
+
exports.syncOptionality = require_response.syncOptionality;
|
|
533
|
+
exports.textDef = require_response.textDef;
|
|
534
|
+
exports.transform = require_utils.transform;
|
|
535
|
+
exports.typeDef = require_response.typeDef;
|
|
536
|
+
exports.typeLiteralDef = require_response.typeLiteralDef;
|
|
537
|
+
exports.walk = require_utils.walk;
|
|
1042
538
|
|
|
1043
539
|
//# sourceMappingURL=index.cjs.map
|