@kubb/plugin-ts 5.0.0-beta.86 → 5.0.0-beta.87

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 CHANGED
@@ -581,7 +581,7 @@ function createInterfaceDeclaration({ modifiers, name, typeParameters, members }
581
581
  * Intelligently selects the syntax based on the type structure and attaches JSDoc comments.
582
582
  */
583
583
  function createTypeDeclaration({ syntax, isExportable, comments, name, type }) {
584
- if (syntax === "interface" && "members" in type) return appendJSDocToNode({
584
+ if (syntax === "interface" && typescript.default.isTypeLiteralNode(type)) return appendJSDocToNode({
585
585
  node: createInterfaceDeclaration({
586
586
  members: [...type.members],
587
587
  modifiers: isExportable ? [modifiers.export] : [],
@@ -680,7 +680,7 @@ function createEnumDeclaration({ type = "enum", name, typeName, enums, enumKeyCa
680
680
  }).filter((node) => node !== void 0)))];
681
681
  if (type === "enum" || type === "constEnum") return [void 0, factory.createEnumDeclaration([factory.createToken(typescript.default.SyntaxKind.ExportKeyword), type === "constEnum" ? factory.createToken(typescript.default.SyntaxKind.ConstKeyword) : void 0].filter((modifier) => modifier !== void 0), factory.createIdentifier(typeName), enums.map(([key, value, description]) => {
682
682
  let initializer = factory.createStringLiteral(value?.toString());
683
- if (Number.parseInt(value.toString(), 10) === value && isNumber(Number.parseInt(value.toString(), 10))) if (value < 0) initializer = factory.createPrefixUnaryExpression(typescript.default.SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value)));
683
+ if (Number.parseInt(value.toString(), 10) === value && isNumber(value)) if (value < 0) initializer = factory.createPrefixUnaryExpression(typescript.default.SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value)));
684
684
  else initializer = factory.createNumericLiteral(value);
685
685
  if (typeof value === "boolean") initializer = value ? factory.createTrue() : factory.createFalse();
686
686
  if (isNumber(Number.parseInt(key.toString(), 10))) {
@@ -955,13 +955,13 @@ function Enum({ node, enum: enumOptions, resolver }) {
955
955
  isExportable: true,
956
956
  isIndexable: true,
957
957
  isTypeOnly: false,
958
- children: _kubb_parser_ts.parserTs.print(nameNode)
958
+ children: (0, _kubb_parser_ts.parserTs)().print(nameNode)
959
959
  }), /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
960
960
  name: typeName,
961
961
  isIndexable: !namesMerge,
962
962
  isExportable: !namesMerge && ENUM_TYPES_WITH_RUNTIME_VALUE.has(enumOptions.type),
963
963
  isTypeOnly: ENUM_TYPES_WITH_TYPE_ONLY.has(enumOptions.type),
964
- children: _kubb_parser_ts.parserTs.print(typeNode)
964
+ children: (0, _kubb_parser_ts.parserTs)().print(typeNode)
965
965
  })] });
966
966
  }
967
967
  //#endregion
@@ -1062,6 +1062,24 @@ function getOperationParameters(node, options = {}) {
1062
1062
  };
1063
1063
  }
1064
1064
  //#endregion
1065
+ //#region ../../internals/shared/src/adapter.ts
1066
+ /**
1067
+ * Narrows the generic `Adapter` from a generator context to the OpenAPI adapter,
1068
+ * so OAS-only options (`dateType`, `nameMapping`) and the parsed `document` are typed.
1069
+ *
1070
+ * Throws when a non-OAS adapter is configured, turning a silently wrong cast into a
1071
+ * clear, actionable error at the point of use.
1072
+ *
1073
+ * @example
1074
+ * ```ts
1075
+ * const { dateType } = getOasAdapter(ctx.adapter).options
1076
+ * ```
1077
+ */
1078
+ function getOasAdapter(adapter) {
1079
+ if (adapter.name !== "oas") throw new Error(`Expected the OpenAPI adapter (adapterOas), but received "${adapter.name}". Configure \`adapter: adapterOas()\` in your Kubb config.`);
1080
+ return adapter;
1081
+ }
1082
+ //#endregion
1065
1083
  //#region ../../internals/shared/src/group.ts
1066
1084
  /**
1067
1085
  * Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
@@ -1129,7 +1147,7 @@ function buildPropertyJSDocComments(schema, optional) {
1129
1147
  !isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
1130
1148
  !isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
1131
1149
  meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
1132
- meta && "default" in meta && meta.default !== void 0 ? `@default ${"primitive" in meta && meta.primitive === "string" ? stringify(meta.default) : meta.default}` : null,
1150
+ 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,
1133
1151
  ...exampleValues.map((example) => `@example ${example}`),
1134
1152
  meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, optional ?? isSchemaOptional(schema) ? " | undefined" : null].filter(Boolean).join("") : null
1135
1153
  ].filter(Boolean);
@@ -1440,7 +1458,7 @@ const printerTs = kubb_kit.ast.createPrinter((options) => {
1440
1458
  if (!name) {
1441
1459
  const withNullable = meta.nullable ? createUnionDeclaration({ nodes: [transformed, keywordTypeNodes.null] }) : transformed;
1442
1460
  const result = (meta.nullish || meta.optional) && addsUndefined ? createUnionDeclaration({ nodes: [withNullable, keywordTypeNodes.undefined] }) : withNullable;
1443
- return _kubb_parser_ts.parserTs.print(result);
1461
+ return (0, _kubb_parser_ts.parserTs)().print(result);
1444
1462
  }
1445
1463
  const inner = (() => {
1446
1464
  const omitted = keysToOmit?.length ? createOmitDeclaration({
@@ -1461,7 +1479,7 @@ const printerTs = kubb_kit.ast.createPrinter((options) => {
1461
1479
  description
1462
1480
  })
1463
1481
  });
1464
- return _kubb_parser_ts.parserTs.print(typeNode);
1482
+ return (0, _kubb_parser_ts.parserTs)().print(typeNode);
1465
1483
  }
1466
1484
  };
1467
1485
  });
@@ -1526,7 +1544,7 @@ const typeGenerator = (0, kubb_kit.defineGenerator)({
1526
1544
  description: node.description,
1527
1545
  resolver,
1528
1546
  enumSchemaNames,
1529
- nameMapping: adapter.options.nameMapping,
1547
+ nameMapping: getOasAdapter(adapter).options.nameMapping,
1530
1548
  nodes: printer?.nodes
1531
1549
  });
1532
1550
  return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.File, {
@@ -1608,7 +1626,7 @@ const typeGenerator = (0, kubb_kit.defineGenerator)({
1608
1626
  keysToOmit,
1609
1627
  resolver,
1610
1628
  enumSchemaNames,
1611
- nameMapping: adapter.options.nameMapping,
1629
+ nameMapping: getOasAdapter(adapter).options.nameMapping,
1612
1630
  nodes: printer?.nodes
1613
1631
  });
1614
1632
  return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [imports.map((imp) => /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
@@ -1849,7 +1867,7 @@ const pluginTsName = "plugin-ts";
1849
1867
  * import { pluginTs } from '@kubb/plugin-ts'
1850
1868
  *
1851
1869
  * export default defineConfig({
1852
- * input: { path: './petStore.yaml' },
1870
+ * input: './petStore.yaml',
1853
1871
  * output: { path: './src/gen' },
1854
1872
  * plugins: [
1855
1873
  * pluginTs({
@@ -2086,14 +2104,14 @@ function renderGroupType(type, sorted, transformType) {
2086
2104
  }
2087
2105
  function printParameter(node, options) {
2088
2106
  const { mode, transformName, transformType } = options;
2089
- const isGroup = typeof node.name !== "string";
2107
+ const bindingName = node.name;
2090
2108
  if (mode === "call") {
2091
- if (isGroup) return `{ ${sortedGroupMembers(node.name, node.type).map((member) => renderBindingMember(member)).join(", ")} }`;
2092
- const name = transformName ? transformName(node.name) : node.name;
2109
+ if (typeof bindingName !== "string") return `{ ${sortedGroupMembers(bindingName, node.type).map((member) => renderBindingMember(member)).join(", ")} }`;
2110
+ const name = transformName ? transformName(bindingName) : bindingName;
2093
2111
  return node.rest ? `...${name}` : name;
2094
2112
  }
2095
- if (isGroup) {
2096
- const sorted = sortedGroupMembers(node.name, node.type);
2113
+ if (typeof bindingName !== "string") {
2114
+ const sorted = sortedGroupMembers(bindingName, node.type);
2097
2115
  const binding = `{ ${sorted.map((member) => renderBindingMember(member, transformName)).join(", ")} }`;
2098
2116
  const allOptional = sorted.every((member) => member.optional);
2099
2117
  const type = renderGroupType(node.type, sorted, transformType);
@@ -2103,7 +2121,7 @@ function printParameter(node, options) {
2103
2121
  }
2104
2122
  return node.default ? `${binding} = ${node.default}` : binding;
2105
2123
  }
2106
- const name = transformName ? transformName(node.name) : node.name;
2124
+ const name = transformName ? transformName(bindingName) : bindingName;
2107
2125
  const type = node.type ? renderType(node.type, transformType) : void 0;
2108
2126
  if (node.rest) return type ? `...${name}: ${type}` : `...${name}`;
2109
2127
  if (type) {