@kubb/plugin-ts 5.0.3 → 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 CHANGED
@@ -10,7 +10,7 @@
10
10
  [![Node][node-src]][node-href]
11
11
 
12
12
  <h4>
13
- <a href="https://kubb.dev/plugins/ts" target="_blank">Documentation</a>
13
+ <a href="https://kubb.dev/plugins/plugin-ts" target="_blank">Documentation</a>
14
14
  <span> · </span>
15
15
  <a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Report Bug</a>
16
16
  <span> · </span>
@@ -38,7 +38,7 @@ npm install @kubb/plugin-ts
38
38
 
39
39
  ## Documentation
40
40
 
41
- See the [full documentation](https://kubb.dev/plugins/ts) for configuration options and examples.
41
+ See the [full documentation](https://kubb.dev/plugins/plugin-ts) for configuration options and examples.
42
42
 
43
43
  ## Supporting Kubb
44
44
 
package/dist/index.cjs CHANGED
@@ -954,115 +954,6 @@ function Enum({ node, enum: enumOptions, resolver }) {
954
954
  })] });
955
955
  }
956
956
  //#endregion
957
- //#region src/utils.ts
958
- /**
959
- * Tells whether a `const` (single-value enum) should render as a bare literal type (`'active'`)
960
- * rather than a named enum reference or a runtime enum declaration.
961
- *
962
- * The parser folds `const` into a single-value enum node. The adapter decides which schemas are
963
- * named enums and lists them in `enumSchemaNames`, and references to those names get suffixed (for
964
- * example `StatusKey`). A const renders as a literal only when the adapter has not registered it as
965
- * a named enum, which keeps the declaration and its references in sync across adapter versions.
966
- */
967
- function isInlineConstEnum(node, enumSchemaNames) {
968
- return (node.namedEnumValues ?? node.enumValues ?? []).length === 1 && !(node.name && enumSchemaNames?.has(node.name));
969
- }
970
- /**
971
- * Collects JSDoc annotation strings for a schema node.
972
- *
973
- * Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
974
- * Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
975
-
976
- */
977
- function isSchemaOptional(schema) {
978
- return Boolean("optional" in schema && schema.optional || "nullish" in schema && schema.nullish);
979
- }
980
- function formatExample(value) {
981
- if (value === null || typeof value !== "object") return String(value);
982
- return (JSON.stringify(value) ?? String(value)).replaceAll("*/", "*\\/");
983
- }
984
- function buildPropertyJSDocComments(schema, optional) {
985
- const meta = (0, kubb_kit.syncSchemaRef)(schema);
986
- const isArray = meta?.primitive === "array";
987
- const hasDescription = meta && "description" in meta && meta.description;
988
- const formatComment = meta && "format" in meta && meta.format ? hasDescription ? [" ", `Format: \`${meta.format}\``] : ["@description", `Format: \`${meta.format}\``] : [];
989
- const exampleValues = meta?.examples ?? [];
990
- const comments = [
991
- hasDescription ? `@description ${jsStringEscape(meta.description)}` : null,
992
- ...formatComment,
993
- meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
994
- !isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
995
- !isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
996
- meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
997
- 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,
998
- ...exampleValues.map((example) => `@example ${formatExample(example)}`)
999
- ].filter(Boolean);
1000
- const typeTag = comments.length && meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, optional ?? isSchemaOptional(schema) ? " | undefined" : null].filter(Boolean).join("") : null;
1001
- return [...comments, typeTag].filter(Boolean);
1002
- }
1003
- /**
1004
- * Builds the object schema for a group of parameters sharing one `in` location (path, query, or
1005
- * header), embedding each param's own schema (and JSDoc) directly rather than referencing a
1006
- * separate per-param type — the group itself is the only type these params get exported as.
1007
- */
1008
- function buildParams({ params }) {
1009
- return kubb_kit.ast.factory.createSchema({
1010
- type: "object",
1011
- properties: params.map((param) => kubb_kit.ast.factory.createProperty({
1012
- name: param.name,
1013
- required: param.required,
1014
- schema: kubb_kit.ast.factory.createSchema({
1015
- ...param.schema,
1016
- optional: !param.required
1017
- })
1018
- }))
1019
- });
1020
- }
1021
- function buildResponseUnion(node, { resolver }) {
1022
- const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
1023
- if (responsesWithSchema.length === 0) return null;
1024
- return kubb_kit.ast.factory.createSchema({
1025
- type: "union",
1026
- members: responsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
1027
- type: "ref",
1028
- name: resolver.response.status(node, res.statusCode)
1029
- }))
1030
- });
1031
- }
1032
- //#endregion
1033
- //#region src/components/Type.tsx
1034
- function Type({ name, node, printer, enum: enumOptions, resolver }) {
1035
- const enumSchemaNodes = kubb_kit.ast.collectSync(node, { schema(n) {
1036
- const enumNode = kubb_kit.ast.narrowSchema(n, kubb_kit.ast.schemaTypes.enum);
1037
- if (enumNode?.name && !isInlineConstEnum(enumNode, printer.options.enumSchemaNames)) return enumNode;
1038
- } });
1039
- const output = printer.print(node);
1040
- if (!output) return;
1041
- const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
1042
- return {
1043
- node,
1044
- ...getEnumNames({
1045
- node,
1046
- enum: enumOptions,
1047
- resolver
1048
- })
1049
- };
1050
- });
1051
- const shouldExportEnums = enumOptions.type !== "inlineLiteral";
1052
- const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
1053
- 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, {
1054
- node,
1055
- enum: enumOptions,
1056
- resolver
1057
- }, node.name)), shouldExportType && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
1058
- name,
1059
- isTypeOnly: true,
1060
- isExportable: true,
1061
- isIndexable: true,
1062
- children: output
1063
- })] });
1064
- }
1065
- //#endregion
1066
957
  //#region ../../internals/shared/src/params.ts
1067
958
  /**
1068
959
  * Drops parameters that share the same name, keeping the first.
@@ -1262,6 +1153,14 @@ function buildResponses(node, resolver) {
1262
1153
  }))
1263
1154
  });
1264
1155
  }
1156
+ function getStatusCodeNumber(statusCode) {
1157
+ const code = Number(statusCode);
1158
+ return Number.isNaN(code) ? null : code;
1159
+ }
1160
+ function isSuccessStatusCode(statusCode) {
1161
+ const code = getStatusCodeNumber(statusCode);
1162
+ return code !== null && code >= 200 && code < 300;
1163
+ }
1265
1164
  //#endregion
1266
1165
  //#region ../../internals/shared/src/resolver.ts
1267
1166
  /**
@@ -1412,6 +1311,117 @@ function mapSchemaItems(node, transform) {
1412
1311
  }));
1413
1312
  }
1414
1313
  //#endregion
1314
+ //#region src/utils.ts
1315
+ /**
1316
+ * Tells whether a `const` (single-value enum) should render as a bare literal type (`'active'`)
1317
+ * rather than a named enum reference or a runtime enum declaration.
1318
+ *
1319
+ * The parser folds `const` into a single-value enum node. The adapter decides which schemas are
1320
+ * named enums and lists them in `enumSchemaNames`, and references to those names get suffixed (for
1321
+ * example `StatusKey`). A const renders as a literal only when the adapter has not registered it as
1322
+ * a named enum, which keeps the declaration and its references in sync across adapter versions.
1323
+ */
1324
+ function isInlineConstEnum(node, enumSchemaNames) {
1325
+ return (node.namedEnumValues ?? node.enumValues ?? []).length === 1 && !(node.name && enumSchemaNames?.has(node.name));
1326
+ }
1327
+ /**
1328
+ * Collects JSDoc annotation strings for a schema node.
1329
+ *
1330
+ * Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
1331
+ * Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
1332
+
1333
+ */
1334
+ function isSchemaOptional(schema) {
1335
+ return Boolean("optional" in schema && schema.optional || "nullish" in schema && schema.nullish);
1336
+ }
1337
+ function formatExample(value) {
1338
+ if (value === null || typeof value !== "object") return String(value);
1339
+ return (JSON.stringify(value) ?? String(value)).replaceAll("*/", "*\\/");
1340
+ }
1341
+ function buildPropertyJSDocComments(schema, optional) {
1342
+ const meta = (0, kubb_kit.syncSchemaRef)(schema);
1343
+ const isArray = meta?.primitive === "array";
1344
+ const hasDescription = meta && "description" in meta && meta.description;
1345
+ const formatComment = meta && "format" in meta && meta.format ? hasDescription ? [" ", `Format: \`${meta.format}\``] : ["@description", `Format: \`${meta.format}\``] : [];
1346
+ const exampleValues = meta?.examples ?? [];
1347
+ const comments = [
1348
+ hasDescription ? `@description ${jsStringEscape(meta.description)}` : null,
1349
+ ...formatComment,
1350
+ meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
1351
+ !isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
1352
+ !isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
1353
+ meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
1354
+ 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,
1355
+ ...exampleValues.map((example) => `@example ${formatExample(example)}`)
1356
+ ].filter(Boolean);
1357
+ const typeTag = comments.length && meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, optional ?? isSchemaOptional(schema) ? " | undefined" : null].filter(Boolean).join("") : null;
1358
+ return [...comments, typeTag].filter(Boolean);
1359
+ }
1360
+ /**
1361
+ * Builds the object schema for a group of parameters sharing one `in` location (path, query, or
1362
+ * header), embedding each param's own schema (and JSDoc) directly rather than referencing a
1363
+ * separate per-param type — the group itself is the only type these params get exported as.
1364
+ */
1365
+ function buildParams({ params }) {
1366
+ return kubb_kit.ast.factory.createSchema({
1367
+ type: "object",
1368
+ properties: params.map((param) => kubb_kit.ast.factory.createProperty({
1369
+ name: param.name,
1370
+ required: param.required,
1371
+ schema: kubb_kit.ast.factory.createSchema({
1372
+ ...param.schema,
1373
+ optional: !param.required
1374
+ })
1375
+ }))
1376
+ });
1377
+ }
1378
+ function buildResponseUnion(node, { resolver }) {
1379
+ const allWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
1380
+ const successWithSchema = allWithSchema.filter((res) => isSuccessStatusCode(res.statusCode));
1381
+ const responsesWithSchema = successWithSchema.length > 0 ? successWithSchema : allWithSchema;
1382
+ if (responsesWithSchema.length === 0) return null;
1383
+ return kubb_kit.ast.factory.createSchema({
1384
+ type: "union",
1385
+ members: responsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
1386
+ type: "ref",
1387
+ name: resolver.response.status(node, res.statusCode)
1388
+ }))
1389
+ });
1390
+ }
1391
+ //#endregion
1392
+ //#region src/components/Type.tsx
1393
+ function Type({ name, node, printer, enum: enumOptions, resolver }) {
1394
+ const enumSchemaNodes = kubb_kit.ast.collectSync(node, { schema(n) {
1395
+ const enumNode = kubb_kit.ast.narrowSchema(n, kubb_kit.ast.schemaTypes.enum);
1396
+ if (enumNode?.name && !isInlineConstEnum(enumNode, printer.options.enumSchemaNames)) return enumNode;
1397
+ } });
1398
+ const output = printer.print(node);
1399
+ if (!output) return;
1400
+ const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
1401
+ return {
1402
+ node,
1403
+ ...getEnumNames({
1404
+ node,
1405
+ enum: enumOptions,
1406
+ resolver
1407
+ })
1408
+ };
1409
+ });
1410
+ const shouldExportEnums = enumOptions.type !== "inlineLiteral";
1411
+ const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
1412
+ 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, {
1413
+ node,
1414
+ enum: enumOptions,
1415
+ resolver
1416
+ }, node.name)), shouldExportType && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
1417
+ name,
1418
+ isTypeOnly: true,
1419
+ isExportable: true,
1420
+ isIndexable: true,
1421
+ children: output
1422
+ })] });
1423
+ }
1424
+ //#endregion
1415
1425
  //#region src/printers/printerTs.ts
1416
1426
  const { isNonNullable } = factory_exports;
1417
1427
  /**