@kubb/plugin-ts 5.0.0-beta.22 → 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 }) {
@@ -984,7 +984,8 @@ const printerTs = _kubb_core.ast.definePrinter((options) => {
984
984
  const meta = _kubb_core.ast.syncSchemaRef(node);
985
985
  if (!name) {
986
986
  const withNullable = meta.nullable ? createUnionDeclaration({ nodes: [transformed, keywordTypeNodes.null] }) : transformed;
987
- return (0, _kubb_parser_ts.safePrint)((meta.nullish || meta.optional) && addsUndefined ? createUnionDeclaration({ nodes: [withNullable, keywordTypeNodes.undefined] }) : withNullable);
987
+ const result = (meta.nullish || meta.optional) && addsUndefined ? createUnionDeclaration({ nodes: [withNullable, keywordTypeNodes.undefined] }) : withNullable;
988
+ return _kubb_parser_ts.parserTs.print(result);
988
989
  }
989
990
  const inner = (() => {
990
991
  const omitted = keysToOmit?.length ? createOmitDeclaration({
@@ -995,17 +996,17 @@ const printerTs = _kubb_core.ast.definePrinter((options) => {
995
996
  const withNullable = meta.nullable ? createUnionDeclaration({ nodes: [omitted, keywordTypeNodes.null] }) : omitted;
996
997
  return meta.nullish || meta.optional ? createUnionDeclaration({ nodes: [withNullable, keywordTypeNodes.undefined] }) : withNullable;
997
998
  })();
998
- const useTypeGeneration = syntaxType === "type" || inner.kind === syntaxKind.union || !!keysToOmit?.length;
999
- return (0, _kubb_parser_ts.safePrint)(createTypeDeclaration({
999
+ const typeNode = createTypeDeclaration({
1000
1000
  name,
1001
1001
  isExportable: true,
1002
1002
  type: inner,
1003
- syntax: useTypeGeneration ? "type" : "interface",
1003
+ syntax: syntaxType === "type" || inner.kind === syntaxKind.union || !!keysToOmit?.length ? "type" : "interface",
1004
1004
  comments: buildPropertyJSDocComments({
1005
1005
  ...meta,
1006
1006
  description
1007
1007
  })
1008
- }));
1008
+ });
1009
+ return _kubb_parser_ts.parserTs.print(typeNode);
1009
1010
  }
1010
1011
  };
1011
1012
  });
@@ -1024,6 +1025,12 @@ function getPerContentTypeName(dataName, suffix) {
1024
1025
  if (dataName.endsWith("Data")) return suffix.endsWith("Data") ? dataName.slice(0, -4) + suffix : `${dataName.slice(0, -4)}${suffix}Data`;
1025
1026
  return dataName + suffix;
1026
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
+ */
1027
1034
  const typeGenerator = (0, _kubb_core.defineGenerator)({
1028
1035
  name: "typescript",
1029
1036
  renderer: _kubb_renderer_jsx.jsxRendererSync,
@@ -1045,7 +1052,7 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1045
1052
  }, {
1046
1053
  root,
1047
1054
  output,
1048
- group
1055
+ group: group ?? void 0
1049
1056
  }).path
1050
1057
  }));
1051
1058
  const isEnumSchema = !!_kubb_core.ast.narrowSchema(node, _kubb_core.ast.schemaTypes.enum);
@@ -1057,7 +1064,7 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1057
1064
  }, {
1058
1065
  root,
1059
1066
  output,
1060
- group
1067
+ group: group ?? void 0
1061
1068
  })
1062
1069
  };
1063
1070
  const schemaPrinter = printerTs({
@@ -1117,7 +1124,7 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1117
1124
  }, {
1118
1125
  root,
1119
1126
  output,
1120
- group
1127
+ group: group ?? void 0
1121
1128
  }) };
1122
1129
  const enumSchemaNames = new Set(ctx.meta.enumNames);
1123
1130
  function resolveImportName(schemaName) {
@@ -1134,7 +1141,7 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1134
1141
  }, {
1135
1142
  root,
1136
1143
  output,
1137
- group
1144
+ group: group ?? void 0
1138
1145
  }).path
1139
1146
  }));
1140
1147
  const schemaPrinter = printerTs({
@@ -1280,20 +1287,21 @@ const typeGenerator = (0, _kubb_core.defineGenerator)({
1280
1287
  //#endregion
1281
1288
  //#region src/resolvers/resolverTs.ts
1282
1289
  /**
1283
- * Resolver for `@kubb/plugin-ts` that provides the default naming and path-resolution
1284
- * helpers used by the plugin. Import this in other plugins to resolve the exact names and
1285
- * 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.
1286
1294
  *
1287
- * The `default` method is automatically injected by `defineResolver` it uses `camelCase`
1288
- * 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.
1289
1297
  *
1290
- * @example
1298
+ * @example Resolve a type and file name
1291
1299
  * ```ts
1292
- * import { resolver } from '@kubb/plugin-ts'
1300
+ * import { resolverTs } from '@kubb/plugin-ts'
1293
1301
  *
1294
- * resolver.default('list pets', 'type') // 'ListPets'
1295
- * resolver.resolveName('list pets status 200') // 'ListPetsStatus200'
1296
- * 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'
1297
1305
  * ```
1298
1306
  */
1299
1307
  const resolverTs = (0, _kubb_core.defineResolver)(() => {
@@ -1344,22 +1352,31 @@ const resolverTs = (0, _kubb_core.defineResolver)(() => {
1344
1352
  //#endregion
1345
1353
  //#region src/plugin.ts
1346
1354
  /**
1347
- * 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.
1348
1357
  */
1349
1358
  const pluginTsName = "plugin-ts";
1350
1359
  /**
1351
- * The `@kubb/plugin-ts` plugin factory.
1352
- *
1353
- * Generates TypeScript type declarations from an OpenAPI/AST `RootNode`.
1354
- * Walks schemas and operations, delegates rendering to the active generators,
1355
- * 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.
1356
1364
  *
1357
1365
  * @example
1358
1366
  * ```ts
1359
- * import pluginTs from '@kubb/plugin-ts'
1367
+ * import { defineConfig } from 'kubb'
1368
+ * import { pluginTs } from '@kubb/plugin-ts'
1360
1369
  *
1361
1370
  * export default defineConfig({
1362
- * 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
+ * ],
1363
1380
  * })
1364
1381
  * ```
1365
1382
  */
@@ -1374,7 +1391,7 @@ const pluginTs = (0, _kubb_core.definePlugin)((options) => {
1374
1391
  if (group.type === "path") return `${ctx.group.split("/")[1]}`;
1375
1392
  return `${camelCase(ctx.group)}Controller`;
1376
1393
  }
1377
- } : void 0;
1394
+ } : null;
1378
1395
  return {
1379
1396
  name: pluginTsName,
1380
1397
  options,