@kubb/plugin-ts 5.0.2 → 5.0.4
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 +2 -2
- package/dist/index.cjs +119 -109
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +119 -109
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -915,115 +915,6 @@ function Enum({ node, enum: enumOptions, resolver }) {
|
|
|
915
915
|
})] });
|
|
916
916
|
}
|
|
917
917
|
//#endregion
|
|
918
|
-
//#region src/utils.ts
|
|
919
|
-
/**
|
|
920
|
-
* Tells whether a `const` (single-value enum) should render as a bare literal type (`'active'`)
|
|
921
|
-
* rather than a named enum reference or a runtime enum declaration.
|
|
922
|
-
*
|
|
923
|
-
* The parser folds `const` into a single-value enum node. The adapter decides which schemas are
|
|
924
|
-
* named enums and lists them in `enumSchemaNames`, and references to those names get suffixed (for
|
|
925
|
-
* example `StatusKey`). A const renders as a literal only when the adapter has not registered it as
|
|
926
|
-
* a named enum, which keeps the declaration and its references in sync across adapter versions.
|
|
927
|
-
*/
|
|
928
|
-
function isInlineConstEnum(node, enumSchemaNames) {
|
|
929
|
-
return (node.namedEnumValues ?? node.enumValues ?? []).length === 1 && !(node.name && enumSchemaNames?.has(node.name));
|
|
930
|
-
}
|
|
931
|
-
/**
|
|
932
|
-
* Collects JSDoc annotation strings for a schema node.
|
|
933
|
-
*
|
|
934
|
-
* Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
|
|
935
|
-
* Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
|
|
936
|
-
|
|
937
|
-
*/
|
|
938
|
-
function isSchemaOptional(schema) {
|
|
939
|
-
return Boolean("optional" in schema && schema.optional || "nullish" in schema && schema.nullish);
|
|
940
|
-
}
|
|
941
|
-
function formatExample(value) {
|
|
942
|
-
if (value === null || typeof value !== "object") return String(value);
|
|
943
|
-
return (JSON.stringify(value) ?? String(value)).replaceAll("*/", "*\\/");
|
|
944
|
-
}
|
|
945
|
-
function buildPropertyJSDocComments(schema, optional) {
|
|
946
|
-
const meta = syncSchemaRef(schema);
|
|
947
|
-
const isArray = meta?.primitive === "array";
|
|
948
|
-
const hasDescription = meta && "description" in meta && meta.description;
|
|
949
|
-
const formatComment = meta && "format" in meta && meta.format ? hasDescription ? [" ", `Format: \`${meta.format}\``] : ["@description", `Format: \`${meta.format}\``] : [];
|
|
950
|
-
const exampleValues = meta?.examples ?? [];
|
|
951
|
-
const comments = [
|
|
952
|
-
hasDescription ? `@description ${jsStringEscape(meta.description)}` : null,
|
|
953
|
-
...formatComment,
|
|
954
|
-
meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
|
|
955
|
-
!isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
|
|
956
|
-
!isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
|
|
957
|
-
meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
|
|
958
|
-
meta && "default" in meta && meta.default !== void 0 ? `@default ${"primitive" in meta && meta.primitive === "string" && typeof meta.default === "string" ? stringify(meta.default) : meta.default}` : null,
|
|
959
|
-
...exampleValues.map((example) => `@example ${formatExample(example)}`)
|
|
960
|
-
].filter(Boolean);
|
|
961
|
-
const typeTag = comments.length && meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, optional ?? isSchemaOptional(schema) ? " | undefined" : null].filter(Boolean).join("") : null;
|
|
962
|
-
return [...comments, typeTag].filter(Boolean);
|
|
963
|
-
}
|
|
964
|
-
/**
|
|
965
|
-
* Builds the object schema for a group of parameters sharing one `in` location (path, query, or
|
|
966
|
-
* header), embedding each param's own schema (and JSDoc) directly rather than referencing a
|
|
967
|
-
* separate per-param type — the group itself is the only type these params get exported as.
|
|
968
|
-
*/
|
|
969
|
-
function buildParams({ params }) {
|
|
970
|
-
return ast.factory.createSchema({
|
|
971
|
-
type: "object",
|
|
972
|
-
properties: params.map((param) => ast.factory.createProperty({
|
|
973
|
-
name: param.name,
|
|
974
|
-
required: param.required,
|
|
975
|
-
schema: ast.factory.createSchema({
|
|
976
|
-
...param.schema,
|
|
977
|
-
optional: !param.required
|
|
978
|
-
})
|
|
979
|
-
}))
|
|
980
|
-
});
|
|
981
|
-
}
|
|
982
|
-
function buildResponseUnion(node, { resolver }) {
|
|
983
|
-
const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
|
|
984
|
-
if (responsesWithSchema.length === 0) return null;
|
|
985
|
-
return ast.factory.createSchema({
|
|
986
|
-
type: "union",
|
|
987
|
-
members: responsesWithSchema.map((res) => ast.factory.createSchema({
|
|
988
|
-
type: "ref",
|
|
989
|
-
name: resolver.response.status(node, res.statusCode)
|
|
990
|
-
}))
|
|
991
|
-
});
|
|
992
|
-
}
|
|
993
|
-
//#endregion
|
|
994
|
-
//#region src/components/Type.tsx
|
|
995
|
-
function Type({ name, node, printer, enum: enumOptions, resolver }) {
|
|
996
|
-
const enumSchemaNodes = ast.collectSync(node, { schema(n) {
|
|
997
|
-
const enumNode = ast.narrowSchema(n, ast.schemaTypes.enum);
|
|
998
|
-
if (enumNode?.name && !isInlineConstEnum(enumNode, printer.options.enumSchemaNames)) return enumNode;
|
|
999
|
-
} });
|
|
1000
|
-
const output = printer.print(node);
|
|
1001
|
-
if (!output) return;
|
|
1002
|
-
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
1003
|
-
return {
|
|
1004
|
-
node,
|
|
1005
|
-
...getEnumNames({
|
|
1006
|
-
node,
|
|
1007
|
-
enum: enumOptions,
|
|
1008
|
-
resolver
|
|
1009
|
-
})
|
|
1010
|
-
};
|
|
1011
|
-
});
|
|
1012
|
-
const shouldExportEnums = enumOptions.type !== "inlineLiteral";
|
|
1013
|
-
const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
|
|
1014
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ jsx(Enum, {
|
|
1015
|
-
node,
|
|
1016
|
-
enum: enumOptions,
|
|
1017
|
-
resolver
|
|
1018
|
-
}, node.name)), shouldExportType && /* @__PURE__ */ jsx(File.Source, {
|
|
1019
|
-
name,
|
|
1020
|
-
isTypeOnly: true,
|
|
1021
|
-
isExportable: true,
|
|
1022
|
-
isIndexable: true,
|
|
1023
|
-
children: output
|
|
1024
|
-
})] });
|
|
1025
|
-
}
|
|
1026
|
-
//#endregion
|
|
1027
918
|
//#region ../../internals/shared/src/params.ts
|
|
1028
919
|
/**
|
|
1029
920
|
* Drops parameters that share the same name, keeping the first.
|
|
@@ -1223,6 +1114,14 @@ function buildResponses(node, resolver) {
|
|
|
1223
1114
|
}))
|
|
1224
1115
|
});
|
|
1225
1116
|
}
|
|
1117
|
+
function getStatusCodeNumber(statusCode) {
|
|
1118
|
+
const code = Number(statusCode);
|
|
1119
|
+
return Number.isNaN(code) ? null : code;
|
|
1120
|
+
}
|
|
1121
|
+
function isSuccessStatusCode(statusCode) {
|
|
1122
|
+
const code = getStatusCodeNumber(statusCode);
|
|
1123
|
+
return code !== null && code >= 200 && code < 300;
|
|
1124
|
+
}
|
|
1226
1125
|
//#endregion
|
|
1227
1126
|
//#region ../../internals/shared/src/resolver.ts
|
|
1228
1127
|
/**
|
|
@@ -1373,6 +1272,117 @@ function mapSchemaItems(node, transform) {
|
|
|
1373
1272
|
}));
|
|
1374
1273
|
}
|
|
1375
1274
|
//#endregion
|
|
1275
|
+
//#region src/utils.ts
|
|
1276
|
+
/**
|
|
1277
|
+
* Tells whether a `const` (single-value enum) should render as a bare literal type (`'active'`)
|
|
1278
|
+
* rather than a named enum reference or a runtime enum declaration.
|
|
1279
|
+
*
|
|
1280
|
+
* The parser folds `const` into a single-value enum node. The adapter decides which schemas are
|
|
1281
|
+
* named enums and lists them in `enumSchemaNames`, and references to those names get suffixed (for
|
|
1282
|
+
* example `StatusKey`). A const renders as a literal only when the adapter has not registered it as
|
|
1283
|
+
* a named enum, which keeps the declaration and its references in sync across adapter versions.
|
|
1284
|
+
*/
|
|
1285
|
+
function isInlineConstEnum(node, enumSchemaNames) {
|
|
1286
|
+
return (node.namedEnumValues ?? node.enumValues ?? []).length === 1 && !(node.name && enumSchemaNames?.has(node.name));
|
|
1287
|
+
}
|
|
1288
|
+
/**
|
|
1289
|
+
* Collects JSDoc annotation strings for a schema node.
|
|
1290
|
+
*
|
|
1291
|
+
* Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
|
|
1292
|
+
* Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
|
|
1293
|
+
|
|
1294
|
+
*/
|
|
1295
|
+
function isSchemaOptional(schema) {
|
|
1296
|
+
return Boolean("optional" in schema && schema.optional || "nullish" in schema && schema.nullish);
|
|
1297
|
+
}
|
|
1298
|
+
function formatExample(value) {
|
|
1299
|
+
if (value === null || typeof value !== "object") return String(value);
|
|
1300
|
+
return (JSON.stringify(value) ?? String(value)).replaceAll("*/", "*\\/");
|
|
1301
|
+
}
|
|
1302
|
+
function buildPropertyJSDocComments(schema, optional) {
|
|
1303
|
+
const meta = syncSchemaRef(schema);
|
|
1304
|
+
const isArray = meta?.primitive === "array";
|
|
1305
|
+
const hasDescription = meta && "description" in meta && meta.description;
|
|
1306
|
+
const formatComment = meta && "format" in meta && meta.format ? hasDescription ? [" ", `Format: \`${meta.format}\``] : ["@description", `Format: \`${meta.format}\``] : [];
|
|
1307
|
+
const exampleValues = meta?.examples ?? [];
|
|
1308
|
+
const comments = [
|
|
1309
|
+
hasDescription ? `@description ${jsStringEscape(meta.description)}` : null,
|
|
1310
|
+
...formatComment,
|
|
1311
|
+
meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
|
|
1312
|
+
!isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
|
|
1313
|
+
!isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
|
|
1314
|
+
meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
|
|
1315
|
+
meta && "default" in meta && meta.default !== void 0 ? `@default ${"primitive" in meta && meta.primitive === "string" && typeof meta.default === "string" ? stringify(meta.default) : meta.default}` : null,
|
|
1316
|
+
...exampleValues.map((example) => `@example ${formatExample(example)}`)
|
|
1317
|
+
].filter(Boolean);
|
|
1318
|
+
const typeTag = comments.length && meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, optional ?? isSchemaOptional(schema) ? " | undefined" : null].filter(Boolean).join("") : null;
|
|
1319
|
+
return [...comments, typeTag].filter(Boolean);
|
|
1320
|
+
}
|
|
1321
|
+
/**
|
|
1322
|
+
* Builds the object schema for a group of parameters sharing one `in` location (path, query, or
|
|
1323
|
+
* header), embedding each param's own schema (and JSDoc) directly rather than referencing a
|
|
1324
|
+
* separate per-param type — the group itself is the only type these params get exported as.
|
|
1325
|
+
*/
|
|
1326
|
+
function buildParams({ params }) {
|
|
1327
|
+
return ast.factory.createSchema({
|
|
1328
|
+
type: "object",
|
|
1329
|
+
properties: params.map((param) => ast.factory.createProperty({
|
|
1330
|
+
name: param.name,
|
|
1331
|
+
required: param.required,
|
|
1332
|
+
schema: ast.factory.createSchema({
|
|
1333
|
+
...param.schema,
|
|
1334
|
+
optional: !param.required
|
|
1335
|
+
})
|
|
1336
|
+
}))
|
|
1337
|
+
});
|
|
1338
|
+
}
|
|
1339
|
+
function buildResponseUnion(node, { resolver }) {
|
|
1340
|
+
const allWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
|
|
1341
|
+
const successWithSchema = allWithSchema.filter((res) => isSuccessStatusCode(res.statusCode));
|
|
1342
|
+
const responsesWithSchema = successWithSchema.length > 0 ? successWithSchema : allWithSchema;
|
|
1343
|
+
if (responsesWithSchema.length === 0) return null;
|
|
1344
|
+
return ast.factory.createSchema({
|
|
1345
|
+
type: "union",
|
|
1346
|
+
members: responsesWithSchema.map((res) => ast.factory.createSchema({
|
|
1347
|
+
type: "ref",
|
|
1348
|
+
name: resolver.response.status(node, res.statusCode)
|
|
1349
|
+
}))
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
//#endregion
|
|
1353
|
+
//#region src/components/Type.tsx
|
|
1354
|
+
function Type({ name, node, printer, enum: enumOptions, resolver }) {
|
|
1355
|
+
const enumSchemaNodes = ast.collectSync(node, { schema(n) {
|
|
1356
|
+
const enumNode = ast.narrowSchema(n, ast.schemaTypes.enum);
|
|
1357
|
+
if (enumNode?.name && !isInlineConstEnum(enumNode, printer.options.enumSchemaNames)) return enumNode;
|
|
1358
|
+
} });
|
|
1359
|
+
const output = printer.print(node);
|
|
1360
|
+
if (!output) return;
|
|
1361
|
+
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
1362
|
+
return {
|
|
1363
|
+
node,
|
|
1364
|
+
...getEnumNames({
|
|
1365
|
+
node,
|
|
1366
|
+
enum: enumOptions,
|
|
1367
|
+
resolver
|
|
1368
|
+
})
|
|
1369
|
+
};
|
|
1370
|
+
});
|
|
1371
|
+
const shouldExportEnums = enumOptions.type !== "inlineLiteral";
|
|
1372
|
+
const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
|
|
1373
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ jsx(Enum, {
|
|
1374
|
+
node,
|
|
1375
|
+
enum: enumOptions,
|
|
1376
|
+
resolver
|
|
1377
|
+
}, node.name)), shouldExportType && /* @__PURE__ */ jsx(File.Source, {
|
|
1378
|
+
name,
|
|
1379
|
+
isTypeOnly: true,
|
|
1380
|
+
isExportable: true,
|
|
1381
|
+
isIndexable: true,
|
|
1382
|
+
children: output
|
|
1383
|
+
})] });
|
|
1384
|
+
}
|
|
1385
|
+
//#endregion
|
|
1376
1386
|
//#region src/printers/printerTs.ts
|
|
1377
1387
|
const { isNonNullable } = factory_exports;
|
|
1378
1388
|
/**
|