@kubb/ast 5.0.0-beta.17 → 5.0.0-beta.19
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 +33 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -35
- package/dist/index.js +33 -25
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/nodes/schema.ts +4 -0
- package/src/utils.ts +41 -10
- package/src/visitor.ts +7 -36
package/dist/index.d.ts
CHANGED
|
@@ -1120,6 +1120,10 @@ type SchemaNodeBase = BaseNode & {
|
|
|
1120
1120
|
* For example, this is `'string'` for a `uuid` schema.
|
|
1121
1121
|
*/
|
|
1122
1122
|
primitive?: PrimitiveSchemaType;
|
|
1123
|
+
/**
|
|
1124
|
+
* Schema `format` value.
|
|
1125
|
+
*/
|
|
1126
|
+
format?: string;
|
|
1123
1127
|
};
|
|
1124
1128
|
/**
|
|
1125
1129
|
* Object schema with ordered properties.
|
|
@@ -3225,13 +3229,6 @@ declare function syncSchemaRef(node: SchemaNode): SchemaNode;
|
|
|
3225
3229
|
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
|
|
3226
3230
|
*/
|
|
3227
3231
|
declare function isStringType(node: SchemaNode): boolean;
|
|
3228
|
-
/**
|
|
3229
|
-
* Applies casing rules to parameter names and returns a new parameter array.
|
|
3230
|
-
*
|
|
3231
|
-
* Use this before passing parameters to schema builders so output property keys match
|
|
3232
|
-
* the desired casing while preserving `OperationNode.parameters` for other consumers.
|
|
3233
|
-
* The input array is not mutated. When `casing` is not set, the original array is returned unchanged.
|
|
3234
|
-
*/
|
|
3235
3232
|
declare function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
|
|
3236
3233
|
/**
|
|
3237
3234
|
* Creates a single-property object schema used as a discriminator literal.
|
|
@@ -3403,34 +3400,6 @@ declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): st
|
|
|
3403
3400
|
*/
|
|
3404
3401
|
declare function resolveRefName(node: SchemaNode | undefined): string | undefined;
|
|
3405
3402
|
declare function collectReferencedSchemaNames(node: SchemaNode | undefined, out?: Set<string>): Set<string>;
|
|
3406
|
-
/**
|
|
3407
|
-
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
3408
|
-
*
|
|
3409
|
-
* An operation uses a schema when any of its parameters, request body content, or responses
|
|
3410
|
-
* reference it — directly or indirectly through other named schemas.
|
|
3411
|
-
* The walk is iterative and safe against reference cycles.
|
|
3412
|
-
*
|
|
3413
|
-
* Use this together with `include` filters to determine which schemas from `components/schemas`
|
|
3414
|
-
* are reachable from the allowed operations, so that schemas used only by excluded operations
|
|
3415
|
-
* are not generated.
|
|
3416
|
-
*
|
|
3417
|
-
* @example Only generate schemas referenced by included operations
|
|
3418
|
-
* ```ts
|
|
3419
|
-
* const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
|
|
3420
|
-
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
3421
|
-
*
|
|
3422
|
-
* for (const schema of inputNode.schemas) {
|
|
3423
|
-
* if (schema.name && !allowed.has(schema.name)) continue
|
|
3424
|
-
* // … generate schema
|
|
3425
|
-
* }
|
|
3426
|
-
* ```
|
|
3427
|
-
*
|
|
3428
|
-
* @example Check whether a specific schema is needed
|
|
3429
|
-
* ```ts
|
|
3430
|
-
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
3431
|
-
* allowed.has('OrderStatus') // false when no included operation references OrderStatus
|
|
3432
|
-
* ```
|
|
3433
|
-
*/
|
|
3434
3403
|
declare function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string>;
|
|
3435
3404
|
/**
|
|
3436
3405
|
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
package/dist/index.js
CHANGED
|
@@ -600,9 +600,7 @@ function transform(node, options) {
|
|
|
600
600
|
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
601
601
|
switch (node.kind) {
|
|
602
602
|
case "Input": {
|
|
603
|
-
|
|
604
|
-
const replaced = visitor.input?.(input, { parent });
|
|
605
|
-
if (replaced) input = replaced;
|
|
603
|
+
const input = visitor.input?.(node, { parent }) ?? node;
|
|
606
604
|
return {
|
|
607
605
|
...input,
|
|
608
606
|
schemas: input.schemas.map((s) => transform(s, {
|
|
@@ -615,16 +613,9 @@ function transform(node, options) {
|
|
|
615
613
|
}))
|
|
616
614
|
};
|
|
617
615
|
}
|
|
618
|
-
case "Output": {
|
|
619
|
-
let output = node;
|
|
620
|
-
const replaced = visitor.output?.(output, { parent });
|
|
621
|
-
if (replaced) output = replaced;
|
|
622
|
-
return output;
|
|
623
|
-
}
|
|
616
|
+
case "Output": return visitor.output?.(node, { parent }) ?? node;
|
|
624
617
|
case "Operation": {
|
|
625
|
-
|
|
626
|
-
const replaced = visitor.operation?.(op, { parent });
|
|
627
|
-
if (replaced) op = replaced;
|
|
618
|
+
const op = visitor.operation?.(node, { parent }) ?? node;
|
|
628
619
|
return {
|
|
629
620
|
...op,
|
|
630
621
|
parameters: op.parameters.map((p) => transform(p, {
|
|
@@ -648,9 +639,7 @@ function transform(node, options) {
|
|
|
648
639
|
};
|
|
649
640
|
}
|
|
650
641
|
case "Schema": {
|
|
651
|
-
|
|
652
|
-
const replaced = visitor.schema?.(schema, { parent });
|
|
653
|
-
if (replaced) schema = replaced;
|
|
642
|
+
const schema = visitor.schema?.(node, { parent }) ?? node;
|
|
654
643
|
const childOptions = {
|
|
655
644
|
...options,
|
|
656
645
|
parent: schema
|
|
@@ -664,9 +653,7 @@ function transform(node, options) {
|
|
|
664
653
|
};
|
|
665
654
|
}
|
|
666
655
|
case "Property": {
|
|
667
|
-
|
|
668
|
-
const replaced = visitor.property?.(prop, { parent });
|
|
669
|
-
if (replaced) prop = replaced;
|
|
656
|
+
const prop = visitor.property?.(node, { parent }) ?? node;
|
|
670
657
|
return createProperty({
|
|
671
658
|
...prop,
|
|
672
659
|
schema: transform(prop.schema, {
|
|
@@ -676,9 +663,7 @@ function transform(node, options) {
|
|
|
676
663
|
});
|
|
677
664
|
}
|
|
678
665
|
case "Parameter": {
|
|
679
|
-
|
|
680
|
-
const replaced = visitor.parameter?.(param, { parent });
|
|
681
|
-
if (replaced) param = replaced;
|
|
666
|
+
const param = visitor.parameter?.(node, { parent }) ?? node;
|
|
682
667
|
return createParameter({
|
|
683
668
|
...param,
|
|
684
669
|
schema: transform(param.schema, {
|
|
@@ -688,9 +673,7 @@ function transform(node, options) {
|
|
|
688
673
|
});
|
|
689
674
|
}
|
|
690
675
|
case "Response": {
|
|
691
|
-
|
|
692
|
-
const replaced = visitor.response?.(response, { parent });
|
|
693
|
-
if (replaced) response = replaced;
|
|
676
|
+
const response = visitor.response?.(node, { parent }) ?? node;
|
|
694
677
|
return {
|
|
695
678
|
...response,
|
|
696
679
|
schema: transform(response.schema, {
|
|
@@ -817,15 +800,25 @@ function isStringType(node) {
|
|
|
817
800
|
* the desired casing while preserving `OperationNode.parameters` for other consumers.
|
|
818
801
|
* The input array is not mutated. When `casing` is not set, the original array is returned unchanged.
|
|
819
802
|
*/
|
|
803
|
+
const caseParamsCache = /* @__PURE__ */ new WeakMap();
|
|
820
804
|
function caseParams(params, casing) {
|
|
821
805
|
if (!casing) return params;
|
|
822
|
-
|
|
806
|
+
let byParams = caseParamsCache.get(params);
|
|
807
|
+
if (!byParams) {
|
|
808
|
+
byParams = /* @__PURE__ */ new Map();
|
|
809
|
+
caseParamsCache.set(params, byParams);
|
|
810
|
+
}
|
|
811
|
+
const cached = byParams.get(casing);
|
|
812
|
+
if (cached) return cached;
|
|
813
|
+
const result = params.map((param) => {
|
|
823
814
|
const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name;
|
|
824
815
|
return {
|
|
825
816
|
...param,
|
|
826
817
|
name: transformed
|
|
827
818
|
};
|
|
828
819
|
});
|
|
820
|
+
byParams.set(casing, result);
|
|
821
|
+
return result;
|
|
829
822
|
}
|
|
830
823
|
/**
|
|
831
824
|
* Creates a single-property object schema used as a discriminator literal.
|
|
@@ -1310,7 +1303,15 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
|
1310
1303
|
* allowed.has('OrderStatus') // false when no included operation references OrderStatus
|
|
1311
1304
|
* ```
|
|
1312
1305
|
*/
|
|
1306
|
+
const usedSchemaNamesCache = /* @__PURE__ */ new WeakMap();
|
|
1313
1307
|
function collectUsedSchemaNames(operations, schemas) {
|
|
1308
|
+
let byOps = usedSchemaNamesCache.get(operations);
|
|
1309
|
+
if (!byOps) {
|
|
1310
|
+
byOps = /* @__PURE__ */ new WeakMap();
|
|
1311
|
+
usedSchemaNamesCache.set(operations, byOps);
|
|
1312
|
+
}
|
|
1313
|
+
const cached = byOps.get(schemas);
|
|
1314
|
+
if (cached) return cached;
|
|
1314
1315
|
const schemaMap = /* @__PURE__ */ new Map();
|
|
1315
1316
|
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
|
|
1316
1317
|
const result = /* @__PURE__ */ new Set();
|
|
@@ -1326,8 +1327,11 @@ function collectUsedSchemaNames(operations, schemas) {
|
|
|
1326
1327
|
depth: "shallow",
|
|
1327
1328
|
schema: (node) => node
|
|
1328
1329
|
})) visitSchema(schema);
|
|
1330
|
+
byOps.set(schemas, result);
|
|
1329
1331
|
return result;
|
|
1330
1332
|
}
|
|
1333
|
+
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
|
|
1334
|
+
const circularSchemaCache = /* @__PURE__ */ new WeakMap();
|
|
1331
1335
|
/**
|
|
1332
1336
|
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
1333
1337
|
*
|
|
@@ -1338,6 +1342,9 @@ function collectUsedSchemaNames(operations, schemas) {
|
|
|
1338
1342
|
* @note Call this once on the full schema graph, then use `containsCircularRef()` to check individual schemas.
|
|
1339
1343
|
*/
|
|
1340
1344
|
function findCircularSchemas(schemas) {
|
|
1345
|
+
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
|
|
1346
|
+
const cached = circularSchemaCache.get(schemas);
|
|
1347
|
+
if (cached) return cached;
|
|
1341
1348
|
const graph = /* @__PURE__ */ new Map();
|
|
1342
1349
|
for (const schema of schemas) {
|
|
1343
1350
|
if (!schema.name) continue;
|
|
@@ -1359,6 +1366,7 @@ function findCircularSchemas(schemas) {
|
|
|
1359
1366
|
if (next) for (const r of next) stack.push(r);
|
|
1360
1367
|
}
|
|
1361
1368
|
}
|
|
1369
|
+
circularSchemaCache.set(schemas, circular);
|
|
1362
1370
|
return circular;
|
|
1363
1371
|
}
|
|
1364
1372
|
/**
|