@kubb/plugin-ts 5.0.0-beta.15 → 5.0.0-beta.25

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
@@ -664,13 +664,13 @@ function Enum({ node, enumType, enumTypeSuffix, enumKeyCasing, resolver }) {
664
664
  isExportable: true,
665
665
  isIndexable: true,
666
666
  isTypeOnly: false,
667
- children: (0, _kubb_parser_ts.safePrint)(nameNode)
667
+ children: _kubb_parser_ts.parserTs.print(nameNode)
668
668
  }), /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
669
669
  name: typeName,
670
670
  isIndexable: true,
671
671
  isExportable: ENUM_TYPES_WITH_RUNTIME_VALUE.has(enumType),
672
672
  isTypeOnly: ENUM_TYPES_WITH_TYPE_ONLY.has(enumType),
673
- children: (0, _kubb_parser_ts.safePrint)(typeNode)
673
+ children: _kubb_parser_ts.parserTs.print(typeNode)
674
674
  })] });
675
675
  }
676
676
  //#endregion
@@ -733,14 +733,14 @@ function buildPropertyJSDocComments(schema) {
733
733
  const meta = _kubb_core.ast.syncSchemaRef(schema);
734
734
  const isArray = meta?.primitive === "array";
735
735
  return [
736
- meta && "description" in meta && meta.description ? `@description ${jsStringEscape(meta.description)}` : void 0,
737
- meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : void 0,
738
- !isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : void 0,
739
- !isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : void 0,
740
- meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : void 0,
741
- meta && "default" in meta && meta.default !== void 0 ? `@default ${"primitive" in meta && meta.primitive === "string" ? stringify(meta.default) : meta.default}` : void 0,
742
- meta && "example" in meta && meta.example !== void 0 ? `@example ${meta.example}` : void 0,
743
- meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, "optional" in schema && schema.optional ? " | undefined" : void 0].filter(Boolean).join("") : void 0
736
+ meta && "description" in meta && meta.description ? `@description ${jsStringEscape(meta.description)}` : null,
737
+ meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
738
+ !isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
739
+ !isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
740
+ meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
741
+ meta && "default" in meta && meta.default !== void 0 ? `@default ${"primitive" in meta && meta.primitive === "string" ? stringify(meta.default) : meta.default}` : null,
742
+ meta && "example" in meta && meta.example !== void 0 ? `@example ${meta.example}` : null,
743
+ meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, "optional" in schema && schema.optional ? " | undefined" : null].filter(Boolean).join("") : null
744
744
  ].filter(Boolean);
745
745
  }
746
746
  function buildParams(node, { params, resolver }) {
@@ -906,7 +906,7 @@ const printerTs = _kubb_core.ast.definePrinter((options) => {
906
906
  date: dateOrStringNode,
907
907
  time: dateOrStringNode,
908
908
  ref(node) {
909
- if (!node.name) return;
909
+ if (!node.name) return null;
910
910
  const refName = node.ref ? _kubb_core.ast.extractRefName(node.ref) ?? node.name : node.name;
911
911
  return createTypeReferenceNode(node.ref && ENUM_TYPES_WITH_KEY_SUFFIX.has(this.options.enumType) && this.options.enumTypeSuffix && this.options.enumSchemaNames?.has(refName) ? this.options.resolver.resolveEnumKeyName({ name: refName }, this.options.enumTypeSuffix) : node.ref ? this.options.resolver.default(refName, "type") : refName, void 0);
912
912
  },
@@ -943,16 +943,16 @@ const printerTs = _kubb_core.ast.definePrinter((options) => {
943
943
  return createIntersectionDeclaration({
944
944
  withParentheses: true,
945
945
  nodes: buildMemberNodes(node.members, this.transform)
946
- }) ?? void 0;
946
+ }) ?? null;
947
947
  },
948
948
  array(node) {
949
949
  return createArrayDeclaration({
950
950
  nodes: (node.items ?? []).map((item) => this.transform(item)).filter(isNonNullable),
951
951
  arrayType: this.options.arrayType
952
- }) ?? void 0;
952
+ }) ?? null;
953
953
  },
954
954
  tuple(node) {
955
- return buildTupleNode(node, this.transform);
955
+ return buildTupleNode(node, this.transform) ?? null;
956
956
  },
957
957
  object(node) {
958
958
  const { transform, options } = this;
@@ -979,32 +979,34 @@ const printerTs = _kubb_core.ast.definePrinter((options) => {
979
979
  },
980
980
  print(node) {
981
981
  const { name, syntaxType = "type", description, keysToOmit } = this.options;
982
- let base = this.transform(node);
983
- if (!base) return null;
982
+ const transformed = this.transform(node);
983
+ if (!transformed) return null;
984
984
  const meta = _kubb_core.ast.syncSchemaRef(node);
985
985
  if (!name) {
986
- if (meta.nullable) base = createUnionDeclaration({ nodes: [base, keywordTypeNodes.null] });
987
- if ((meta.nullish || meta.optional) && addsUndefined) base = createUnionDeclaration({ nodes: [base, keywordTypeNodes.undefined] });
988
- return (0, _kubb_parser_ts.safePrint)(base);
986
+ const withNullable = meta.nullable ? createUnionDeclaration({ nodes: [transformed, keywordTypeNodes.null] }) : transformed;
987
+ const result = (meta.nullish || meta.optional) && addsUndefined ? createUnionDeclaration({ nodes: [withNullable, keywordTypeNodes.undefined] }) : withNullable;
988
+ return _kubb_parser_ts.parserTs.print(result);
989
989
  }
990
- let inner = keysToOmit?.length ? createOmitDeclaration({
991
- keys: keysToOmit,
992
- type: base,
993
- nonNullable: true
994
- }) : base;
995
- if (meta.nullable) inner = createUnionDeclaration({ nodes: [inner, keywordTypeNodes.null] });
996
- if (meta.nullish || meta.optional) inner = createUnionDeclaration({ nodes: [inner, keywordTypeNodes.undefined] });
997
- const useTypeGeneration = syntaxType === "type" || inner.kind === syntaxKind.union || !!keysToOmit?.length;
998
- return (0, _kubb_parser_ts.safePrint)(createTypeDeclaration({
990
+ const inner = (() => {
991
+ const omitted = keysToOmit?.length ? createOmitDeclaration({
992
+ keys: keysToOmit,
993
+ type: transformed,
994
+ nonNullable: true
995
+ }) : transformed;
996
+ const withNullable = meta.nullable ? createUnionDeclaration({ nodes: [omitted, keywordTypeNodes.null] }) : omitted;
997
+ return meta.nullish || meta.optional ? createUnionDeclaration({ nodes: [withNullable, keywordTypeNodes.undefined] }) : withNullable;
998
+ })();
999
+ const typeNode = createTypeDeclaration({
999
1000
  name,
1000
1001
  isExportable: true,
1001
1002
  type: inner,
1002
- syntax: useTypeGeneration ? "type" : "interface",
1003
+ syntax: syntaxType === "type" || inner.kind === syntaxKind.union || !!keysToOmit?.length ? "type" : "interface",
1003
1004
  comments: buildPropertyJSDocComments({
1004
1005
  ...meta,
1005
1006
  description
1006
1007
  })
1007
- }));
1008
+ });
1009
+ return _kubb_parser_ts.parserTs.print(typeNode);
1008
1010
  }
1009
1011
  };
1010
1012
  });
@@ -1023,15 +1025,21 @@ function getPerContentTypeName(dataName, suffix) {
1023
1025
  if (dataName.endsWith("Data")) return suffix.endsWith("Data") ? dataName.slice(0, -4) + suffix : `${dataName.slice(0, -4)}${suffix}Data`;
1024
1026
  return dataName + suffix;
1025
1027
  }
1028
+ /**
1029
+ * Built-in generator for `@kubb/plugin-ts`. Emits one TypeScript file per
1030
+ * schema in the spec plus per-operation request, response, and parameter
1031
+ * types. Drop-replace with a custom `Generator<PluginTs>` to change how
1032
+ * TypeScript output is produced.
1033
+ */
1026
1034
  const typeGenerator = (0, _kubb_core.defineGenerator)({
1027
1035
  name: "typescript",
1028
1036
  renderer: _kubb_renderer_jsx.jsxRendererSync,
1029
1037
  schema(node, ctx) {
1030
1038
  const { enumType, enumTypeSuffix, enumKeyCasing, syntaxType, optionalType, arrayType, output, group, printer } = ctx.options;
1031
- const { adapter, config, resolver, root, inputNode } = ctx;
1039
+ const { adapter, config, resolver, root } = ctx;
1032
1040
  if (!node.name) return;
1033
1041
  const mode = ctx.getMode(output);
1034
- const enumSchemaNames = new Set(inputNode.schemas.filter((s) => _kubb_core.ast.narrowSchema(s, _kubb_core.ast.schemaTypes.enum) && s.name).map((s) => s.name));
1042
+ const enumSchemaNames = new Set(ctx.meta.enumNames);
1035
1043
  function resolveImportName(schemaName) {
1036
1044
  if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && enumTypeSuffix && enumSchemaNames.has(schemaName)) return resolver.resolveEnumKeyName({ name: schemaName }, enumTypeSuffix);
1037
1045
  return resolver.resolveTypeName(schemaName);
@@ -1044,7 +1052,7 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1044
1052
  }, {
1045
1053
  root,
1046
1054
  output,
1047
- group
1055
+ group: group ?? void 0
1048
1056
  }).path
1049
1057
  }));
1050
1058
  const isEnumSchema = !!_kubb_core.ast.narrowSchema(node, _kubb_core.ast.schemaTypes.enum);
@@ -1056,7 +1064,7 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1056
1064
  }, {
1057
1065
  root,
1058
1066
  output,
1059
- group
1067
+ group: group ?? void 0
1060
1068
  })
1061
1069
  };
1062
1070
  const schemaPrinter = printerTs({
@@ -1075,11 +1083,11 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1075
1083
  baseName: meta.file.baseName,
1076
1084
  path: meta.file.path,
1077
1085
  meta: meta.file.meta,
1078
- banner: resolver.resolveBanner(inputNode, {
1086
+ banner: resolver.resolveBanner(ctx.meta, {
1079
1087
  output,
1080
1088
  config
1081
1089
  }),
1082
- footer: resolver.resolveFooter(inputNode, {
1090
+ footer: resolver.resolveFooter(ctx.meta, {
1083
1091
  output,
1084
1092
  config
1085
1093
  }),
@@ -1105,7 +1113,7 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1105
1113
  },
1106
1114
  operation(node, ctx) {
1107
1115
  const { enumType, enumTypeSuffix, enumKeyCasing, optionalType, arrayType, syntaxType, paramsCasing, group, output, printer } = ctx.options;
1108
- const { adapter, config, resolver, root, inputNode } = ctx;
1116
+ const { adapter, config, resolver, root } = ctx;
1109
1117
  const mode = ctx.getMode(output);
1110
1118
  const params = _kubb_core.ast.caseParams(node.parameters, paramsCasing);
1111
1119
  const meta = { file: resolver.resolveFile({
@@ -1116,9 +1124,9 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1116
1124
  }, {
1117
1125
  root,
1118
1126
  output,
1119
- group
1127
+ group: group ?? void 0
1120
1128
  }) };
1121
- const enumSchemaNames = new Set(inputNode.schemas.filter((s) => _kubb_core.ast.narrowSchema(s, _kubb_core.ast.schemaTypes.enum) && s.name).map((s) => s.name));
1129
+ const enumSchemaNames = new Set(ctx.meta.enumNames);
1122
1130
  function resolveImportName(schemaName) {
1123
1131
  if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && enumTypeSuffix && enumSchemaNames.has(schemaName)) return resolver.resolveEnumKeyName({ name: schemaName }, enumTypeSuffix);
1124
1132
  return resolver.resolveTypeName(schemaName);
@@ -1133,7 +1141,7 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1133
1141
  }, {
1134
1142
  root,
1135
1143
  output,
1136
- group
1144
+ group: group ?? void 0
1137
1145
  }).path
1138
1146
  }));
1139
1147
  const schemaPrinter = printerTs({
@@ -1257,11 +1265,11 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1257
1265
  baseName: meta.file.baseName,
1258
1266
  path: meta.file.path,
1259
1267
  meta: meta.file.meta,
1260
- banner: resolver.resolveBanner(inputNode, {
1268
+ banner: resolver.resolveBanner(ctx.meta, {
1261
1269
  output,
1262
1270
  config
1263
1271
  }),
1264
- footer: resolver.resolveFooter(inputNode, {
1272
+ footer: resolver.resolveFooter(ctx.meta, {
1265
1273
  output,
1266
1274
  config
1267
1275
  }),
@@ -1279,20 +1287,21 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1279
1287
  //#endregion
1280
1288
  //#region src/resolvers/resolverTs.ts
1281
1289
  /**
1282
- * Resolver for `@kubb/plugin-ts` that provides the default naming and path-resolution
1283
- * helpers used by the plugin. Import this in other plugins to resolve the exact names and
1284
- * paths that `plugin-ts` generates without hardcoding the conventions.
1290
+ * Default resolver used by `@kubb/plugin-ts`. Decides the names and file paths
1291
+ * for every generated TypeScript type. Import this in other plugins that need
1292
+ * to reference the exact names `plugin-ts` produces without duplicating the
1293
+ * casing/file-layout rules.
1285
1294
  *
1286
- * The `default` method is automatically injected by `defineResolver` it uses `camelCase`
1287
- * for identifiers/files and `pascalCase` for type names.
1295
+ * The `default` method is supplied by `defineResolver`. It uses PascalCase for
1296
+ * type names and PascalCase-with-isFile for files.
1288
1297
  *
1289
- * @example
1298
+ * @example Resolve a type and file name
1290
1299
  * ```ts
1291
- * import { resolver } from '@kubb/plugin-ts'
1300
+ * import { resolverTs } from '@kubb/plugin-ts'
1292
1301
  *
1293
- * resolver.default('list pets', 'type') // 'ListPets'
1294
- * resolver.resolveName('list pets status 200') // 'ListPetsStatus200'
1295
- * resolver.resolvePathName('list pets', 'file') // 'listPets'
1302
+ * resolverTs.default('list pets', 'type') // 'ListPets'
1303
+ * resolverTs.resolvePathName('list pets', 'file') // 'ListPets'
1304
+ * resolverTs.resolveResponseStatusName(node, 200) // 'ListPetsStatus200'
1296
1305
  * ```
1297
1306
  */
1298
1307
  const resolverTs = (0, _kubb_core.defineResolver)(() => {
@@ -1343,22 +1352,31 @@ const resolverTs = (0, _kubb_core.defineResolver)(() => {
1343
1352
  //#endregion
1344
1353
  //#region src/plugin.ts
1345
1354
  /**
1346
- * Canonical plugin name for `@kubb/plugin-ts`, used to identify the plugin in driver lookups and warnings.
1355
+ * Canonical plugin name for `@kubb/plugin-ts`. Used for driver lookups and
1356
+ * cross-plugin dependency references.
1347
1357
  */
1348
1358
  const pluginTsName = "plugin-ts";
1349
1359
  /**
1350
- * The `@kubb/plugin-ts` plugin factory.
1351
- *
1352
- * Generates TypeScript type declarations from an OpenAPI/AST `RootNode`.
1353
- * Walks schemas and operations, delegates rendering to the active generators,
1354
- * and writes barrel files based on `output.barrelType`.
1360
+ * Generates TypeScript `type` aliases and `interface` declarations from an
1361
+ * OpenAPI spec. The foundation that every other Kubb plugin builds on:
1362
+ * clients, query hooks, mocks, and validators all reference the names this
1363
+ * plugin produces.
1355
1364
  *
1356
1365
  * @example
1357
1366
  * ```ts
1358
- * import pluginTs from '@kubb/plugin-ts'
1367
+ * import { defineConfig } from 'kubb'
1368
+ * import { pluginTs } from '@kubb/plugin-ts'
1359
1369
  *
1360
1370
  * export default defineConfig({
1361
- * plugins: [pluginTs({ output: { path: 'types' }, enumType: 'asConst' })],
1371
+ * input: { path: './petStore.yaml' },
1372
+ * output: { path: './src/gen' },
1373
+ * plugins: [
1374
+ * pluginTs({
1375
+ * output: { path: './types' },
1376
+ * enumType: 'asConst',
1377
+ * optionalType: 'questionTokenAndUndefined',
1378
+ * }),
1379
+ * ],
1362
1380
  * })
1363
1381
  * ```
1364
1382
  */
@@ -1373,7 +1391,7 @@ const pluginTs = (0, _kubb_core.definePlugin)((options) => {
1373
1391
  if (group.type === "path") return `${ctx.group.split("/")[1]}`;
1374
1392
  return `${camelCase(ctx.group)}Controller`;
1375
1393
  }
1376
- } : void 0;
1394
+ } : null;
1377
1395
  return {
1378
1396
  name: pluginTsName,
1379
1397
  options,