@kubb/plugin-ts 5.0.0 → 5.0.1
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 +166 -155
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +166 -155
- package/dist/index.js.map +1 -1
- package/package.json +17 -5
package/dist/index.cjs
CHANGED
|
@@ -950,6 +950,115 @@ function Enum({ node, enum: enumOptions, resolver }) {
|
|
|
950
950
|
})] });
|
|
951
951
|
}
|
|
952
952
|
//#endregion
|
|
953
|
+
//#region src/utils.ts
|
|
954
|
+
/**
|
|
955
|
+
* Tells whether a `const` (single-value enum) should render as a bare literal type (`'active'`)
|
|
956
|
+
* rather than a named enum reference or a runtime enum declaration.
|
|
957
|
+
*
|
|
958
|
+
* The parser folds `const` into a single-value enum node. The adapter decides which schemas are
|
|
959
|
+
* named enums and lists them in `enumSchemaNames`, and references to those names get suffixed (for
|
|
960
|
+
* example `StatusKey`). A const renders as a literal only when the adapter has not registered it as
|
|
961
|
+
* a named enum, which keeps the declaration and its references in sync across adapter versions.
|
|
962
|
+
*/
|
|
963
|
+
function isInlineConstEnum(node, enumSchemaNames) {
|
|
964
|
+
return (node.namedEnumValues ?? node.enumValues ?? []).length === 1 && !(node.name && enumSchemaNames?.has(node.name));
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* Collects JSDoc annotation strings for a schema node.
|
|
968
|
+
*
|
|
969
|
+
* Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
|
|
970
|
+
* Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
|
|
971
|
+
|
|
972
|
+
*/
|
|
973
|
+
function isSchemaOptional(schema) {
|
|
974
|
+
return Boolean("optional" in schema && schema.optional || "nullish" in schema && schema.nullish);
|
|
975
|
+
}
|
|
976
|
+
function formatExample(value) {
|
|
977
|
+
if (value === null || typeof value !== "object") return String(value);
|
|
978
|
+
return (JSON.stringify(value) ?? String(value)).replaceAll("*/", "*\\/");
|
|
979
|
+
}
|
|
980
|
+
function buildPropertyJSDocComments(schema, optional) {
|
|
981
|
+
const meta = (0, kubb_kit.syncSchemaRef)(schema);
|
|
982
|
+
const isArray = meta?.primitive === "array";
|
|
983
|
+
const hasDescription = meta && "description" in meta && meta.description;
|
|
984
|
+
const formatComment = meta && "format" in meta && meta.format ? hasDescription ? [" ", `Format: \`${meta.format}\``] : ["@description", `Format: \`${meta.format}\``] : [];
|
|
985
|
+
const exampleValues = meta?.examples ?? [];
|
|
986
|
+
const comments = [
|
|
987
|
+
hasDescription ? `@description ${jsStringEscape(meta.description)}` : null,
|
|
988
|
+
...formatComment,
|
|
989
|
+
meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
|
|
990
|
+
!isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
|
|
991
|
+
!isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
|
|
992
|
+
meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
|
|
993
|
+
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,
|
|
994
|
+
...exampleValues.map((example) => `@example ${formatExample(example)}`)
|
|
995
|
+
].filter(Boolean);
|
|
996
|
+
const typeTag = comments.length && meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, optional ?? isSchemaOptional(schema) ? " | undefined" : null].filter(Boolean).join("") : null;
|
|
997
|
+
return [...comments, typeTag].filter(Boolean);
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Builds the object schema for a group of parameters sharing one `in` location (path, query, or
|
|
1001
|
+
* header), embedding each param's own schema (and JSDoc) directly rather than referencing a
|
|
1002
|
+
* separate per-param type — the group itself is the only type these params get exported as.
|
|
1003
|
+
*/
|
|
1004
|
+
function buildParams({ params }) {
|
|
1005
|
+
return kubb_kit.ast.factory.createSchema({
|
|
1006
|
+
type: "object",
|
|
1007
|
+
properties: params.map((param) => kubb_kit.ast.factory.createProperty({
|
|
1008
|
+
name: param.name,
|
|
1009
|
+
required: param.required,
|
|
1010
|
+
schema: kubb_kit.ast.factory.createSchema({
|
|
1011
|
+
...param.schema,
|
|
1012
|
+
optional: !param.required
|
|
1013
|
+
})
|
|
1014
|
+
}))
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
function buildResponseUnion(node, { resolver }) {
|
|
1018
|
+
const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
|
|
1019
|
+
if (responsesWithSchema.length === 0) return null;
|
|
1020
|
+
return kubb_kit.ast.factory.createSchema({
|
|
1021
|
+
type: "union",
|
|
1022
|
+
members: responsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
|
|
1023
|
+
type: "ref",
|
|
1024
|
+
name: resolver.response.status(node, res.statusCode)
|
|
1025
|
+
}))
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
//#endregion
|
|
1029
|
+
//#region src/components/Type.tsx
|
|
1030
|
+
function Type({ name, node, printer, enum: enumOptions, resolver }) {
|
|
1031
|
+
const enumSchemaNodes = kubb_kit.ast.collectSync(node, { schema(n) {
|
|
1032
|
+
const enumNode = kubb_kit.ast.narrowSchema(n, kubb_kit.ast.schemaTypes.enum);
|
|
1033
|
+
if (enumNode?.name && !isInlineConstEnum(enumNode, printer.options.enumSchemaNames)) return enumNode;
|
|
1034
|
+
} });
|
|
1035
|
+
const output = printer.print(node);
|
|
1036
|
+
if (!output) return;
|
|
1037
|
+
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
1038
|
+
return {
|
|
1039
|
+
node,
|
|
1040
|
+
...getEnumNames({
|
|
1041
|
+
node,
|
|
1042
|
+
enum: enumOptions,
|
|
1043
|
+
resolver
|
|
1044
|
+
})
|
|
1045
|
+
};
|
|
1046
|
+
});
|
|
1047
|
+
const shouldExportEnums = enumOptions.type !== "inlineLiteral";
|
|
1048
|
+
const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
|
|
1049
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Enum, {
|
|
1050
|
+
node,
|
|
1051
|
+
enum: enumOptions,
|
|
1052
|
+
resolver
|
|
1053
|
+
}, node.name)), shouldExportType && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
1054
|
+
name,
|
|
1055
|
+
isTypeOnly: true,
|
|
1056
|
+
isExportable: true,
|
|
1057
|
+
isIndexable: true,
|
|
1058
|
+
children: output
|
|
1059
|
+
})] });
|
|
1060
|
+
}
|
|
1061
|
+
//#endregion
|
|
953
1062
|
//#region ../../internals/shared/src/params.ts
|
|
954
1063
|
/**
|
|
955
1064
|
* Drops parameters that share the same name, keeping the first.
|
|
@@ -1093,6 +1202,62 @@ function buildOptionsSchema(node, resolver) {
|
|
|
1093
1202
|
})]
|
|
1094
1203
|
});
|
|
1095
1204
|
}
|
|
1205
|
+
/**
|
|
1206
|
+
* The schema a status occupies in the `<Name>Responses` record. A status that documents several
|
|
1207
|
+
* content types becomes a `{ contentType; data }` union so the runtime can surface the negotiated type
|
|
1208
|
+
* on `result.parsed`, while the standalone `<Name>StatusNNN` alias stays the plain body union that the
|
|
1209
|
+
* query hooks and `result.data` use.
|
|
1210
|
+
*/
|
|
1211
|
+
function buildResponseRecordEntry(node, res, resolver) {
|
|
1212
|
+
const statusName = resolver.response.status(node, res.statusCode);
|
|
1213
|
+
const variants = (res.content ?? []).filter((entry) => entry.schema);
|
|
1214
|
+
if (variants.length <= 1) return kubb_kit.ast.factory.createSchema({
|
|
1215
|
+
type: "ref",
|
|
1216
|
+
name: statusName
|
|
1217
|
+
});
|
|
1218
|
+
return kubb_kit.ast.factory.createSchema({
|
|
1219
|
+
type: "union",
|
|
1220
|
+
members: resolveContentTypeVariants(variants, statusName).map((variant) => kubb_kit.ast.factory.createSchema({
|
|
1221
|
+
type: "object",
|
|
1222
|
+
primitive: "object",
|
|
1223
|
+
properties: [kubb_kit.ast.factory.createProperty({
|
|
1224
|
+
name: "contentType",
|
|
1225
|
+
required: true,
|
|
1226
|
+
schema: kubb_kit.ast.factory.createSchema({
|
|
1227
|
+
type: "enum",
|
|
1228
|
+
enumValues: [variant.contentType]
|
|
1229
|
+
})
|
|
1230
|
+
}), kubb_kit.ast.factory.createProperty({
|
|
1231
|
+
name: "data",
|
|
1232
|
+
required: true,
|
|
1233
|
+
schema: kubb_kit.ast.factory.createSchema({
|
|
1234
|
+
type: "ref",
|
|
1235
|
+
name: variant.name
|
|
1236
|
+
})
|
|
1237
|
+
})]
|
|
1238
|
+
}))
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Builds the per-status `<Name>Responses` record for an operation, referencing the already-resolved
|
|
1243
|
+
* `<Name>StatusNNN` names. Shared by `@kubb/plugin-ts`'s `Responses` type and `@kubb/plugin-zod`'s
|
|
1244
|
+
* inferred responses schema, so both emit the same shape from the same inputs.
|
|
1245
|
+
*
|
|
1246
|
+
* Always emits the keyed record, even when an operation declares no responses. An operation with no
|
|
1247
|
+
* responses renders as an empty object, which keeps every consumer's import (for example the axios
|
|
1248
|
+
* SDK's `RequestResult<XResponses>`) resolvable instead of pointing at a missing export.
|
|
1249
|
+
*/
|
|
1250
|
+
function buildResponses(node, resolver) {
|
|
1251
|
+
return kubb_kit.ast.factory.createSchema({
|
|
1252
|
+
type: "object",
|
|
1253
|
+
primitive: "object",
|
|
1254
|
+
properties: node.responses.map((res) => kubb_kit.ast.factory.createProperty({
|
|
1255
|
+
name: String(res.statusCode),
|
|
1256
|
+
required: true,
|
|
1257
|
+
schema: buildResponseRecordEntry(node, res, resolver)
|
|
1258
|
+
}))
|
|
1259
|
+
});
|
|
1260
|
+
}
|
|
1096
1261
|
//#endregion
|
|
1097
1262
|
//#region ../../internals/shared/src/resolver.ts
|
|
1098
1263
|
/**
|
|
@@ -1243,160 +1408,6 @@ function mapSchemaItems(node, transform) {
|
|
|
1243
1408
|
}));
|
|
1244
1409
|
}
|
|
1245
1410
|
//#endregion
|
|
1246
|
-
//#region src/utils.ts
|
|
1247
|
-
/**
|
|
1248
|
-
* Tells whether a `const` (single-value enum) should render as a bare literal type (`'active'`)
|
|
1249
|
-
* rather than a named enum reference or a runtime enum declaration.
|
|
1250
|
-
*
|
|
1251
|
-
* The parser folds `const` into a single-value enum node. The adapter decides which schemas are
|
|
1252
|
-
* named enums and lists them in `enumSchemaNames`, and references to those names get suffixed (for
|
|
1253
|
-
* example `StatusKey`). A const renders as a literal only when the adapter has not registered it as
|
|
1254
|
-
* a named enum, which keeps the declaration and its references in sync across adapter versions.
|
|
1255
|
-
*/
|
|
1256
|
-
function isInlineConstEnum(node, enumSchemaNames) {
|
|
1257
|
-
return (node.namedEnumValues ?? node.enumValues ?? []).length === 1 && !(node.name && enumSchemaNames?.has(node.name));
|
|
1258
|
-
}
|
|
1259
|
-
/**
|
|
1260
|
-
* Collects JSDoc annotation strings for a schema node.
|
|
1261
|
-
*
|
|
1262
|
-
* Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
|
|
1263
|
-
* Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
|
|
1264
|
-
|
|
1265
|
-
*/
|
|
1266
|
-
function isSchemaOptional(schema) {
|
|
1267
|
-
return Boolean("optional" in schema && schema.optional || "nullish" in schema && schema.nullish);
|
|
1268
|
-
}
|
|
1269
|
-
function formatExample(value) {
|
|
1270
|
-
if (value === null || typeof value !== "object") return String(value);
|
|
1271
|
-
return (JSON.stringify(value) ?? String(value)).replaceAll("*/", "*\\/");
|
|
1272
|
-
}
|
|
1273
|
-
function buildPropertyJSDocComments(schema, optional) {
|
|
1274
|
-
const meta = (0, kubb_kit.syncSchemaRef)(schema);
|
|
1275
|
-
const isArray = meta?.primitive === "array";
|
|
1276
|
-
const hasDescription = meta && "description" in meta && meta.description;
|
|
1277
|
-
const formatComment = meta && "format" in meta && meta.format ? hasDescription ? [" ", `Format: \`${meta.format}\``] : ["@description", `Format: \`${meta.format}\``] : [];
|
|
1278
|
-
const exampleValues = meta?.examples ?? [];
|
|
1279
|
-
const comments = [
|
|
1280
|
-
hasDescription ? `@description ${jsStringEscape(meta.description)}` : null,
|
|
1281
|
-
...formatComment,
|
|
1282
|
-
meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
|
|
1283
|
-
!isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
|
|
1284
|
-
!isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
|
|
1285
|
-
meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
|
|
1286
|
-
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,
|
|
1287
|
-
...exampleValues.map((example) => `@example ${formatExample(example)}`)
|
|
1288
|
-
].filter(Boolean);
|
|
1289
|
-
const typeTag = comments.length && meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, optional ?? isSchemaOptional(schema) ? " | undefined" : null].filter(Boolean).join("") : null;
|
|
1290
|
-
return [...comments, typeTag].filter(Boolean);
|
|
1291
|
-
}
|
|
1292
|
-
/**
|
|
1293
|
-
* Builds the object schema for a group of parameters sharing one `in` location (path, query, or
|
|
1294
|
-
* header), embedding each param's own schema (and JSDoc) directly rather than referencing a
|
|
1295
|
-
* separate per-param type — the group itself is the only type these params get exported as.
|
|
1296
|
-
*/
|
|
1297
|
-
function buildParams({ params }) {
|
|
1298
|
-
return kubb_kit.ast.factory.createSchema({
|
|
1299
|
-
type: "object",
|
|
1300
|
-
properties: params.map((param) => kubb_kit.ast.factory.createProperty({
|
|
1301
|
-
name: param.name,
|
|
1302
|
-
required: param.required,
|
|
1303
|
-
schema: kubb_kit.ast.factory.createSchema({
|
|
1304
|
-
...param.schema,
|
|
1305
|
-
optional: !param.required
|
|
1306
|
-
})
|
|
1307
|
-
}))
|
|
1308
|
-
});
|
|
1309
|
-
}
|
|
1310
|
-
/**
|
|
1311
|
-
* The schema a status occupies in the `<Name>Responses` record. A status that documents several
|
|
1312
|
-
* content types becomes a `{ contentType; data }` union so the runtime can surface the negotiated type
|
|
1313
|
-
* on `result.parsed`, while the standalone `<Name>StatusNNN` alias stays the plain body union that the
|
|
1314
|
-
* query hooks and `result.data` use.
|
|
1315
|
-
*/
|
|
1316
|
-
function buildResponseRecordEntry(node, res, resolver) {
|
|
1317
|
-
const statusName = resolver.response.status(node, res.statusCode);
|
|
1318
|
-
const variants = (res.content ?? []).filter((entry) => entry.schema);
|
|
1319
|
-
if (variants.length <= 1) return kubb_kit.ast.factory.createSchema({
|
|
1320
|
-
type: "ref",
|
|
1321
|
-
name: statusName
|
|
1322
|
-
});
|
|
1323
|
-
return kubb_kit.ast.factory.createSchema({
|
|
1324
|
-
type: "union",
|
|
1325
|
-
members: resolveContentTypeVariants(variants, statusName).map((variant) => kubb_kit.ast.factory.createSchema({
|
|
1326
|
-
type: "object",
|
|
1327
|
-
properties: [kubb_kit.ast.factory.createProperty({
|
|
1328
|
-
name: "contentType",
|
|
1329
|
-
required: true,
|
|
1330
|
-
schema: kubb_kit.ast.factory.createSchema({
|
|
1331
|
-
type: "enum",
|
|
1332
|
-
enumValues: [variant.contentType]
|
|
1333
|
-
})
|
|
1334
|
-
}), kubb_kit.ast.factory.createProperty({
|
|
1335
|
-
name: "data",
|
|
1336
|
-
required: true,
|
|
1337
|
-
schema: kubb_kit.ast.factory.createSchema({
|
|
1338
|
-
type: "ref",
|
|
1339
|
-
name: variant.name
|
|
1340
|
-
})
|
|
1341
|
-
})]
|
|
1342
|
-
}))
|
|
1343
|
-
});
|
|
1344
|
-
}
|
|
1345
|
-
function buildResponses(node, { resolver }) {
|
|
1346
|
-
return kubb_kit.ast.factory.createSchema({
|
|
1347
|
-
type: "object",
|
|
1348
|
-
properties: node.responses.map((res) => kubb_kit.ast.factory.createProperty({
|
|
1349
|
-
name: String(res.statusCode),
|
|
1350
|
-
required: true,
|
|
1351
|
-
schema: buildResponseRecordEntry(node, res, resolver)
|
|
1352
|
-
}))
|
|
1353
|
-
});
|
|
1354
|
-
}
|
|
1355
|
-
function buildResponseUnion(node, { resolver }) {
|
|
1356
|
-
const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
|
|
1357
|
-
if (responsesWithSchema.length === 0) return null;
|
|
1358
|
-
return kubb_kit.ast.factory.createSchema({
|
|
1359
|
-
type: "union",
|
|
1360
|
-
members: responsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
|
|
1361
|
-
type: "ref",
|
|
1362
|
-
name: resolver.response.status(node, res.statusCode)
|
|
1363
|
-
}))
|
|
1364
|
-
});
|
|
1365
|
-
}
|
|
1366
|
-
//#endregion
|
|
1367
|
-
//#region src/components/Type.tsx
|
|
1368
|
-
function Type({ name, node, printer, enum: enumOptions, resolver }) {
|
|
1369
|
-
const enumSchemaNodes = kubb_kit.ast.collectSync(node, { schema(n) {
|
|
1370
|
-
const enumNode = kubb_kit.ast.narrowSchema(n, kubb_kit.ast.schemaTypes.enum);
|
|
1371
|
-
if (enumNode?.name && !isInlineConstEnum(enumNode, printer.options.enumSchemaNames)) return enumNode;
|
|
1372
|
-
} });
|
|
1373
|
-
const output = printer.print(node);
|
|
1374
|
-
if (!output) return;
|
|
1375
|
-
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
1376
|
-
return {
|
|
1377
|
-
node,
|
|
1378
|
-
...getEnumNames({
|
|
1379
|
-
node,
|
|
1380
|
-
enum: enumOptions,
|
|
1381
|
-
resolver
|
|
1382
|
-
})
|
|
1383
|
-
};
|
|
1384
|
-
});
|
|
1385
|
-
const shouldExportEnums = enumOptions.type !== "inlineLiteral";
|
|
1386
|
-
const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
|
|
1387
|
-
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Enum, {
|
|
1388
|
-
node,
|
|
1389
|
-
enum: enumOptions,
|
|
1390
|
-
resolver
|
|
1391
|
-
}, node.name)), shouldExportType && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
1392
|
-
name,
|
|
1393
|
-
isTypeOnly: true,
|
|
1394
|
-
isExportable: true,
|
|
1395
|
-
isIndexable: true,
|
|
1396
|
-
children: output
|
|
1397
|
-
})] });
|
|
1398
|
-
}
|
|
1399
|
-
//#endregion
|
|
1400
1411
|
//#region src/printers/printerTs.ts
|
|
1401
1412
|
const { isNonNullable } = factory_exports;
|
|
1402
1413
|
/**
|
|
@@ -1790,7 +1801,7 @@ const typeGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1790
1801
|
name: resolver.response.options(node)
|
|
1791
1802
|
});
|
|
1792
1803
|
const responsesType = renderSchemaType({
|
|
1793
|
-
schema: buildResponses(node,
|
|
1804
|
+
schema: buildResponses(node, resolver),
|
|
1794
1805
|
name: resolver.response.responses(node)
|
|
1795
1806
|
});
|
|
1796
1807
|
function buildResponseType() {
|