@kubb/ast 5.0.0-beta.75 → 5.0.0-beta.9
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 +1 -1
- package/dist/index.cjs +68 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +48 -7
- package/dist/index.js +68 -9
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/index.ts +1 -0
- package/src/utils.ts +89 -7
package/dist/index.d.ts
CHANGED
|
@@ -3230,14 +3230,14 @@ type OperationParamsResolver = {
|
|
|
3230
3230
|
* @example Individual path parameter name
|
|
3231
3231
|
* `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
|
|
3232
3232
|
*/
|
|
3233
|
-
resolveParamName(node: OperationNode, param: ParameterNode): string;
|
|
3233
|
+
resolveParamName(this: OperationParamsResolver, node: OperationNode, param: ParameterNode): string;
|
|
3234
3234
|
/**
|
|
3235
3235
|
* Resolves the request body type name.
|
|
3236
3236
|
*
|
|
3237
3237
|
* @example Request body type name
|
|
3238
3238
|
* `resolver.resolveDataName(node) // → 'CreatePetData'`
|
|
3239
3239
|
*/
|
|
3240
|
-
resolveDataName(node: OperationNode): string;
|
|
3240
|
+
resolveDataName(this: OperationParamsResolver, node: OperationNode): string;
|
|
3241
3241
|
/**
|
|
3242
3242
|
* Resolves the grouped path parameters type name.
|
|
3243
3243
|
* When the return value equals `resolveParamName`, no indexed access is emitted.
|
|
@@ -3245,7 +3245,7 @@ type OperationParamsResolver = {
|
|
|
3245
3245
|
* @example Grouped path params type name
|
|
3246
3246
|
* `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
|
|
3247
3247
|
*/
|
|
3248
|
-
resolvePathParamsName(node: OperationNode, param: ParameterNode): string;
|
|
3248
|
+
resolvePathParamsName(this: OperationParamsResolver, node: OperationNode, param: ParameterNode): string;
|
|
3249
3249
|
/**
|
|
3250
3250
|
* Resolves the grouped query parameters type name.
|
|
3251
3251
|
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
|
|
@@ -3253,7 +3253,7 @@ type OperationParamsResolver = {
|
|
|
3253
3253
|
* @example Grouped query params type name
|
|
3254
3254
|
* `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
|
|
3255
3255
|
*/
|
|
3256
|
-
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string;
|
|
3256
|
+
resolveQueryParamsName(this: OperationParamsResolver, node: OperationNode, param: ParameterNode): string;
|
|
3257
3257
|
/**
|
|
3258
3258
|
* Resolves the grouped header parameters type name.
|
|
3259
3259
|
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
|
|
@@ -3261,7 +3261,7 @@ type OperationParamsResolver = {
|
|
|
3261
3261
|
* @example Grouped header params type name
|
|
3262
3262
|
* `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
|
|
3263
3263
|
*/
|
|
3264
|
-
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string;
|
|
3264
|
+
resolveHeaderParamsName(this: OperationParamsResolver, node: OperationNode, param: ParameterNode): string;
|
|
3265
3265
|
};
|
|
3266
3266
|
/**
|
|
3267
3267
|
* Options for {@link createOperationParams}.
|
|
@@ -3377,9 +3377,50 @@ declare function resolveRefName(node: SchemaNode | undefined): string | undefine
|
|
|
3377
3377
|
* Refs are followed by name only — the resolved `node.schema` is not traversed inline.
|
|
3378
3378
|
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
|
|
3379
3379
|
*
|
|
3380
|
-
* @
|
|
3380
|
+
* @example Collect refs from a single schema
|
|
3381
|
+
* ```ts
|
|
3382
|
+
* const names = collectReferencedSchemaNames(petSchema)
|
|
3383
|
+
* // → Set { 'Category', 'Tag' }
|
|
3384
|
+
* ```
|
|
3385
|
+
*
|
|
3386
|
+
* @example Accumulate refs from multiple schemas into one set
|
|
3387
|
+
* ```ts
|
|
3388
|
+
* const out = new Set<string>()
|
|
3389
|
+
* for (const schema of schemas) {
|
|
3390
|
+
* collectReferencedSchemaNames(schema, out)
|
|
3391
|
+
* }
|
|
3392
|
+
* ```
|
|
3381
3393
|
*/
|
|
3382
3394
|
declare function collectReferencedSchemaNames(node: SchemaNode | undefined, out?: Set<string>): Set<string>;
|
|
3395
|
+
/**
|
|
3396
|
+
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
3397
|
+
*
|
|
3398
|
+
* An operation uses a schema when any of its parameters, request body content, or responses
|
|
3399
|
+
* reference it — directly or indirectly through other named schemas.
|
|
3400
|
+
* The walk is iterative and safe against reference cycles.
|
|
3401
|
+
*
|
|
3402
|
+
* Use this together with `include` filters to determine which schemas from `components/schemas`
|
|
3403
|
+
* are reachable from the allowed operations, so that schemas used only by excluded operations
|
|
3404
|
+
* are not generated.
|
|
3405
|
+
*
|
|
3406
|
+
* @example Only generate schemas referenced by included operations
|
|
3407
|
+
* ```ts
|
|
3408
|
+
* const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
|
|
3409
|
+
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
3410
|
+
*
|
|
3411
|
+
* for (const schema of inputNode.schemas) {
|
|
3412
|
+
* if (schema.name && !allowed.has(schema.name)) continue
|
|
3413
|
+
* // … generate schema
|
|
3414
|
+
* }
|
|
3415
|
+
* ```
|
|
3416
|
+
*
|
|
3417
|
+
* @example Check whether a specific schema is needed
|
|
3418
|
+
* ```ts
|
|
3419
|
+
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
3420
|
+
* allowed.has('OrderStatus') // false when no included operation references OrderStatus
|
|
3421
|
+
* ```
|
|
3422
|
+
*/
|
|
3423
|
+
declare function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string>;
|
|
3383
3424
|
/**
|
|
3384
3425
|
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
3385
3426
|
*
|
|
@@ -3406,5 +3447,5 @@ declare function containsCircularRef(node: SchemaNode | undefined, {
|
|
|
3406
3447
|
excludeName?: string;
|
|
3407
3448
|
}): boolean;
|
|
3408
3449
|
//#endregion
|
|
3409
|
-
export { type ArraySchemaNode, type ArrowFunctionNode, AsyncVisitor, type BaseNode, type BreakNode, type CodeNode, CollectOptions, CollectVisitor, type ComplexSchemaType, type ConstNode, type DateSchemaNode, type DatetimeSchemaNode, DistributiveOmit, type EnumSchemaNode, type EnumValueNode, type ExportNode, type FileNode, type FormatStringSchemaNode, type FunctionNode, type FunctionNodeType, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type HttpMethod, type HttpStatusCode, type ImportNode, InferSchema, InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type Ipv4SchemaNode, type Ipv6SchemaNode, type JSDocNode, type JsxNode, type MediaType, Node, type NodeKind, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, OperationParamsResolver, type OutputNode, type ParameterGroupNode, type ParameterLocation, type ParameterNode, type ParamsTypeNode, ParentOf, ParserOptions, type PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, type PropertyNode, RefMap, type RefSchemaNode, type ResponseNode, ScalarPrimitive, type ScalarSchemaNode, type ScalarSchemaType, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type SpecialSchemaType, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, TransformOptions, type TypeDeclarationNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, UserFileNode, Visitor, VisitorContext, VisitorDepth, WalkOptions, caseParams, childName, collect, collectImports, collectReferencedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
3450
|
+
export { type ArraySchemaNode, type ArrowFunctionNode, AsyncVisitor, type BaseNode, type BreakNode, type CodeNode, CollectOptions, CollectVisitor, type ComplexSchemaType, type ConstNode, type DateSchemaNode, type DatetimeSchemaNode, DistributiveOmit, type EnumSchemaNode, type EnumValueNode, type ExportNode, type FileNode, type FormatStringSchemaNode, type FunctionNode, type FunctionNodeType, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type HttpMethod, type HttpStatusCode, type ImportNode, InferSchema, InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type Ipv4SchemaNode, type Ipv6SchemaNode, type JSDocNode, type JsxNode, type MediaType, Node, type NodeKind, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, OperationParamsResolver, type OutputNode, type ParameterGroupNode, type ParameterLocation, type ParameterNode, type ParamsTypeNode, ParentOf, ParserOptions, type PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, type PropertyNode, RefMap, type RefSchemaNode, type ResponseNode, ScalarPrimitive, type ScalarSchemaNode, type ScalarSchemaType, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type SpecialSchemaType, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, TransformOptions, type TypeDeclarationNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, UserFileNode, Visitor, VisitorContext, VisitorDepth, WalkOptions, caseParams, childName, collect, collectImports, collectReferencedSchemaNames, collectUsedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
3410
3451
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -445,12 +445,6 @@ const isOperationNode = isKind("Operation");
|
|
|
445
445
|
* ```
|
|
446
446
|
*/
|
|
447
447
|
const isSchemaNode = isKind("Schema");
|
|
448
|
-
isKind("Property");
|
|
449
|
-
isKind("Parameter");
|
|
450
|
-
isKind("Response");
|
|
451
|
-
isKind("FunctionParameter");
|
|
452
|
-
isKind("ParameterGroup");
|
|
453
|
-
isKind("FunctionParameters");
|
|
454
448
|
//#endregion
|
|
455
449
|
//#region src/refs.ts
|
|
456
450
|
/**
|
|
@@ -1157,6 +1151,13 @@ function combineExports(exports) {
|
|
|
1157
1151
|
function combineImports(imports, exports, source) {
|
|
1158
1152
|
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
1159
1153
|
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
|
|
1154
|
+
const importNameMemo = /* @__PURE__ */ new Map();
|
|
1155
|
+
const canonicalizeName = (n) => {
|
|
1156
|
+
if (typeof n === "string") return n;
|
|
1157
|
+
const key = `${n.propertyName}:${n.name ?? ""}`;
|
|
1158
|
+
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
|
|
1159
|
+
return importNameMemo.get(key);
|
|
1160
|
+
};
|
|
1160
1161
|
const result = [];
|
|
1161
1162
|
const namedByPath = /* @__PURE__ */ new Map();
|
|
1162
1163
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -1170,7 +1171,7 @@ function combineImports(imports, exports, source) {
|
|
|
1170
1171
|
const { path, isTypeOnly } = curr;
|
|
1171
1172
|
let { name } = curr;
|
|
1172
1173
|
if (Array.isArray(name)) {
|
|
1173
|
-
name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.propertyName));
|
|
1174
|
+
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
|
|
1174
1175
|
if (!name.length) continue;
|
|
1175
1176
|
const key = pathTypeKey(path, isTypeOnly);
|
|
1176
1177
|
const existing = namedByPath.get(key);
|
|
@@ -1243,7 +1244,19 @@ function resolveRefName(node) {
|
|
|
1243
1244
|
* Refs are followed by name only — the resolved `node.schema` is not traversed inline.
|
|
1244
1245
|
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
|
|
1245
1246
|
*
|
|
1246
|
-
* @
|
|
1247
|
+
* @example Collect refs from a single schema
|
|
1248
|
+
* ```ts
|
|
1249
|
+
* const names = collectReferencedSchemaNames(petSchema)
|
|
1250
|
+
* // → Set { 'Category', 'Tag' }
|
|
1251
|
+
* ```
|
|
1252
|
+
*
|
|
1253
|
+
* @example Accumulate refs from multiple schemas into one set
|
|
1254
|
+
* ```ts
|
|
1255
|
+
* const out = new Set<string>()
|
|
1256
|
+
* for (const schema of schemas) {
|
|
1257
|
+
* collectReferencedSchemaNames(schema, out)
|
|
1258
|
+
* }
|
|
1259
|
+
* ```
|
|
1247
1260
|
*/
|
|
1248
1261
|
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
1249
1262
|
if (!node) return out;
|
|
@@ -1256,6 +1269,52 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
|
1256
1269
|
return out;
|
|
1257
1270
|
}
|
|
1258
1271
|
/**
|
|
1272
|
+
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
1273
|
+
*
|
|
1274
|
+
* An operation uses a schema when any of its parameters, request body content, or responses
|
|
1275
|
+
* reference it — directly or indirectly through other named schemas.
|
|
1276
|
+
* The walk is iterative and safe against reference cycles.
|
|
1277
|
+
*
|
|
1278
|
+
* Use this together with `include` filters to determine which schemas from `components/schemas`
|
|
1279
|
+
* are reachable from the allowed operations, so that schemas used only by excluded operations
|
|
1280
|
+
* are not generated.
|
|
1281
|
+
*
|
|
1282
|
+
* @example Only generate schemas referenced by included operations
|
|
1283
|
+
* ```ts
|
|
1284
|
+
* const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
|
|
1285
|
+
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
1286
|
+
*
|
|
1287
|
+
* for (const schema of inputNode.schemas) {
|
|
1288
|
+
* if (schema.name && !allowed.has(schema.name)) continue
|
|
1289
|
+
* // … generate schema
|
|
1290
|
+
* }
|
|
1291
|
+
* ```
|
|
1292
|
+
*
|
|
1293
|
+
* @example Check whether a specific schema is needed
|
|
1294
|
+
* ```ts
|
|
1295
|
+
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
1296
|
+
* allowed.has('OrderStatus') // false when no included operation references OrderStatus
|
|
1297
|
+
* ```
|
|
1298
|
+
*/
|
|
1299
|
+
function collectUsedSchemaNames(operations, schemas) {
|
|
1300
|
+
const schemaMap = /* @__PURE__ */ new Map();
|
|
1301
|
+
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
|
|
1302
|
+
const result = /* @__PURE__ */ new Set();
|
|
1303
|
+
function visitSchema(schema) {
|
|
1304
|
+
const directRefs = collectReferencedSchemaNames(schema);
|
|
1305
|
+
for (const name of directRefs) if (!result.has(name)) {
|
|
1306
|
+
result.add(name);
|
|
1307
|
+
const namedSchema = schemaMap.get(name);
|
|
1308
|
+
if (namedSchema) visitSchema(namedSchema);
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
for (const op of operations) for (const schema of collect(op, {
|
|
1312
|
+
depth: "shallow",
|
|
1313
|
+
schema: (node) => node
|
|
1314
|
+
})) visitSchema(schema);
|
|
1315
|
+
return result;
|
|
1316
|
+
}
|
|
1317
|
+
/**
|
|
1259
1318
|
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
1260
1319
|
*
|
|
1261
1320
|
* Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
|
|
@@ -2133,6 +2192,6 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
|
2133
2192
|
return propNode;
|
|
2134
2193
|
}
|
|
2135
2194
|
//#endregion
|
|
2136
|
-
export { caseParams, childName, collect, collectImports, collectReferencedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
2195
|
+
export { caseParams, childName, collect, collectImports, collectReferencedSchemaNames, collectUsedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
2137
2196
|
|
|
2138
2197
|
//# sourceMappingURL=index.js.map
|