@kubb/ast 5.0.0-alpha.15 → 5.0.0-alpha.16
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/index.cjs +88 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +27 -2
- package/dist/index.js +84 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/{visitor-UlWOe-In.d.ts → visitor-YMltBj6w.d.ts} +11 -2
- package/package.json +1 -1
- package/src/constants.ts +5 -0
- package/src/index.ts +3 -2
- package/src/refs.ts +8 -0
- package/src/transforms.ts +114 -0
package/dist/index.cjs
CHANGED
|
@@ -44,6 +44,16 @@ const schemaTypes = {
|
|
|
44
44
|
blob: "blob",
|
|
45
45
|
never: "never"
|
|
46
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* Scalar primitive schema types used for union member simplification.
|
|
49
|
+
*/
|
|
50
|
+
const SCALAR_PRIMITIVE_TYPES = new Set([
|
|
51
|
+
"string",
|
|
52
|
+
"number",
|
|
53
|
+
"integer",
|
|
54
|
+
"bigint",
|
|
55
|
+
"boolean"
|
|
56
|
+
]);
|
|
47
57
|
const httpMethods = {
|
|
48
58
|
get: "GET",
|
|
49
59
|
post: "POST",
|
|
@@ -490,6 +500,13 @@ const isFunctionParametersNode = isKind("FunctionParameters");
|
|
|
490
500
|
//#endregion
|
|
491
501
|
//#region src/refs.ts
|
|
492
502
|
/**
|
|
503
|
+
* Extracts the final segment from a reference string.
|
|
504
|
+
* Falls back to the original string when no slash exists.
|
|
505
|
+
*/
|
|
506
|
+
function extractRefName(ref) {
|
|
507
|
+
return ref.split("/").at(-1) ?? ref;
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
493
510
|
* Indexes named schemas from `root.schemas` by name. Unnamed schemas are skipped.
|
|
494
511
|
*/
|
|
495
512
|
function buildRefMap(root) {
|
|
@@ -510,6 +527,72 @@ function refMapToObject(refMap) {
|
|
|
510
527
|
return Object.fromEntries(refMap);
|
|
511
528
|
}
|
|
512
529
|
//#endregion
|
|
530
|
+
//#region src/transforms.ts
|
|
531
|
+
/**
|
|
532
|
+
* Replaces the discriminator property's schema inside an object node with
|
|
533
|
+
* an enum of the provided values.
|
|
534
|
+
*/
|
|
535
|
+
function applyDiscriminatorEnum({ node, propertyName, values, enumName }) {
|
|
536
|
+
const objectNode = narrowSchema(node, "object");
|
|
537
|
+
if (!objectNode?.properties?.length) return node;
|
|
538
|
+
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node;
|
|
539
|
+
return createSchema({
|
|
540
|
+
...objectNode,
|
|
541
|
+
properties: objectNode.properties.map((prop) => {
|
|
542
|
+
if (prop.name !== propertyName) return prop;
|
|
543
|
+
return createProperty({
|
|
544
|
+
...prop,
|
|
545
|
+
schema: createSchema({
|
|
546
|
+
type: "enum",
|
|
547
|
+
primitive: "string",
|
|
548
|
+
enumValues: values,
|
|
549
|
+
name: enumName,
|
|
550
|
+
readOnly: prop.schema.readOnly,
|
|
551
|
+
writeOnly: prop.schema.writeOnly
|
|
552
|
+
})
|
|
553
|
+
});
|
|
554
|
+
})
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Merges adjacent anonymous object members into a single anonymous object.
|
|
559
|
+
*/
|
|
560
|
+
function mergeAdjacentAnonymousObjects(members) {
|
|
561
|
+
return members.reduce((acc, member) => {
|
|
562
|
+
const objectMember = narrowSchema(member, "object");
|
|
563
|
+
if (objectMember && !objectMember.name) {
|
|
564
|
+
const previous = acc.at(-1);
|
|
565
|
+
const previousObject = previous ? narrowSchema(previous, "object") : void 0;
|
|
566
|
+
if (previousObject && !previousObject.name) {
|
|
567
|
+
acc[acc.length - 1] = createSchema({
|
|
568
|
+
...previousObject,
|
|
569
|
+
properties: [...previousObject.properties ?? [], ...objectMember.properties ?? []]
|
|
570
|
+
});
|
|
571
|
+
return acc;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
acc.push(member);
|
|
575
|
+
return acc;
|
|
576
|
+
}, []);
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Removes enum members subsumed by broader scalar members in the same union.
|
|
580
|
+
*/
|
|
581
|
+
function simplifyUnionMembers(members) {
|
|
582
|
+
const scalarPrimitives = new Set(members.filter((member) => SCALAR_PRIMITIVE_TYPES.has(member.type)).map((m) => m.type));
|
|
583
|
+
if (!scalarPrimitives.size) return members;
|
|
584
|
+
return members.filter((member) => {
|
|
585
|
+
const enumNode = narrowSchema(member, "enum");
|
|
586
|
+
if (!enumNode) return true;
|
|
587
|
+
const primitive = enumNode.primitive;
|
|
588
|
+
if (!primitive) return true;
|
|
589
|
+
if (!enumNode.enumType) return true;
|
|
590
|
+
if (scalarPrimitives.has(primitive)) return false;
|
|
591
|
+
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
|
|
592
|
+
return true;
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
//#endregion
|
|
513
596
|
//#region ../../internals/utils/dist/index.js
|
|
514
597
|
/**
|
|
515
598
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -1067,6 +1150,8 @@ function collect(node, options) {
|
|
|
1067
1150
|
return results;
|
|
1068
1151
|
}
|
|
1069
1152
|
//#endregion
|
|
1153
|
+
exports.SCALAR_PRIMITIVE_TYPES = SCALAR_PRIMITIVE_TYPES;
|
|
1154
|
+
exports.applyDiscriminatorEnum = applyDiscriminatorEnum;
|
|
1070
1155
|
exports.applyParamsCasing = applyParamsCasing;
|
|
1071
1156
|
exports.buildRefMap = buildRefMap;
|
|
1072
1157
|
exports.collect = collect;
|
|
@@ -1081,6 +1166,7 @@ exports.createResponse = createResponse;
|
|
|
1081
1166
|
exports.createRoot = createRoot;
|
|
1082
1167
|
exports.createSchema = createSchema;
|
|
1083
1168
|
exports.definePrinter = definePrinter;
|
|
1169
|
+
exports.extractRefName = extractRefName;
|
|
1084
1170
|
exports.functionPrinter = functionPrinter;
|
|
1085
1171
|
exports.httpMethods = httpMethods;
|
|
1086
1172
|
exports.isFunctionParameterNode = isFunctionParameterNode;
|
|
@@ -1094,11 +1180,13 @@ exports.isResponseNode = isResponseNode;
|
|
|
1094
1180
|
exports.isRootNode = isRootNode;
|
|
1095
1181
|
exports.isSchemaNode = isSchemaNode;
|
|
1096
1182
|
exports.mediaTypes = mediaTypes;
|
|
1183
|
+
exports.mergeAdjacentAnonymousObjects = mergeAdjacentAnonymousObjects;
|
|
1097
1184
|
exports.narrowSchema = narrowSchema;
|
|
1098
1185
|
exports.nodeKinds = nodeKinds;
|
|
1099
1186
|
exports.refMapToObject = refMapToObject;
|
|
1100
1187
|
exports.resolveRef = resolveRef;
|
|
1101
1188
|
exports.schemaTypes = schemaTypes;
|
|
1189
|
+
exports.simplifyUnionMembers = simplifyUnionMembers;
|
|
1102
1190
|
exports.syncPropertySchema = syncPropertySchema;
|
|
1103
1191
|
exports.transform = transform;
|
|
1104
1192
|
exports.walk = walk;
|