@kubb/plugin-ts 5.0.0-beta.85 → 5.0.0-beta.86
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 +44 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +44 -12
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -231,7 +231,7 @@ const reservedWords = /* @__PURE__ */ new Set([
|
|
|
231
231
|
*/
|
|
232
232
|
function isValidVarName(name) {
|
|
233
233
|
if (!name || reservedWords.has(name)) return false;
|
|
234
|
-
return
|
|
234
|
+
return isIdentifier(name);
|
|
235
235
|
}
|
|
236
236
|
/**
|
|
237
237
|
* Returns `name` when it's a syntactically valid JavaScript variable name,
|
|
@@ -253,6 +253,22 @@ function ensureValidVarName(name) {
|
|
|
253
253
|
if (!name || isValidVarName(name)) return name;
|
|
254
254
|
return `_${name}`;
|
|
255
255
|
}
|
|
256
|
+
/**
|
|
257
|
+
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
|
|
258
|
+
*
|
|
259
|
+
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
260
|
+
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
261
|
+
* deciding whether an object key needs quoting.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```ts
|
|
265
|
+
* isIdentifier('name') // true
|
|
266
|
+
* isIdentifier('x-total')// false
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
function isIdentifier(name) {
|
|
270
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
271
|
+
}
|
|
256
272
|
//#endregion
|
|
257
273
|
//#region ../../internals/utils/src/strings.ts
|
|
258
274
|
/**
|
|
@@ -1452,6 +1468,15 @@ const printerTs = kubb_kit.ast.createPrinter((options) => {
|
|
|
1452
1468
|
//#endregion
|
|
1453
1469
|
//#region src/generators/typeGenerator.tsx
|
|
1454
1470
|
/**
|
|
1471
|
+
* Resolves the imported type name for a referenced schema. An enum schema emitted
|
|
1472
|
+
* as a `const` object imports its suffixed key alias (e.g. `StatusKey`); every other
|
|
1473
|
+
* schema imports its plain resolved name.
|
|
1474
|
+
*/
|
|
1475
|
+
function resolveImportName({ schemaName, enumOptions, enumSchemaNames, resolver }) {
|
|
1476
|
+
if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumOptions.type) && enumOptions.typeSuffix && enumSchemaNames.has(schemaName)) return resolver.enum.keyName({ name: schemaName }, enumOptions.typeSuffix);
|
|
1477
|
+
return resolver.name(schemaName);
|
|
1478
|
+
}
|
|
1479
|
+
/**
|
|
1455
1480
|
* Built-in generator for `@kubb/plugin-ts`. Emits one TypeScript file per
|
|
1456
1481
|
* schema in the spec plus per-operation request, response, and parameter
|
|
1457
1482
|
* types. Drop-replace with a custom `Generator<PluginTs>` to change how
|
|
@@ -1465,12 +1490,13 @@ const typeGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1465
1490
|
const { adapter, config, resolver, root } = ctx;
|
|
1466
1491
|
if (!node.name) return;
|
|
1467
1492
|
const enumSchemaNames = new Set(ctx.meta.enumNames);
|
|
1468
|
-
function resolveImportName(schemaName) {
|
|
1469
|
-
if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumOptions.type) && enumOptions.typeSuffix && enumSchemaNames.has(schemaName)) return resolver.enum.keyName({ name: schemaName }, enumOptions.typeSuffix);
|
|
1470
|
-
return resolver.name(schemaName);
|
|
1471
|
-
}
|
|
1472
1493
|
const imports = adapter.getImports(node, (schemaName) => ({
|
|
1473
|
-
name: resolveImportName(
|
|
1494
|
+
name: resolveImportName({
|
|
1495
|
+
schemaName,
|
|
1496
|
+
enumOptions,
|
|
1497
|
+
enumSchemaNames,
|
|
1498
|
+
resolver
|
|
1499
|
+
}),
|
|
1474
1500
|
path: resolver.file({
|
|
1475
1501
|
name: schemaName,
|
|
1476
1502
|
extname: ".ts",
|
|
@@ -1555,14 +1581,15 @@ const typeGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1555
1581
|
group: group ?? void 0
|
|
1556
1582
|
}) };
|
|
1557
1583
|
const enumSchemaNames = new Set(ctx.meta.enumNames);
|
|
1558
|
-
function resolveImportName(schemaName) {
|
|
1559
|
-
if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumOptions.type) && enumOptions.typeSuffix && enumSchemaNames.has(schemaName)) return resolver.enum.keyName({ name: schemaName }, enumOptions.typeSuffix);
|
|
1560
|
-
return resolver.name(schemaName);
|
|
1561
|
-
}
|
|
1562
1584
|
function renderSchemaType({ schema, name, keysToOmit }) {
|
|
1563
1585
|
if (!schema) return null;
|
|
1564
1586
|
const imports = adapter.getImports(schema, (schemaName) => ({
|
|
1565
|
-
name: resolveImportName(
|
|
1587
|
+
name: resolveImportName({
|
|
1588
|
+
schemaName,
|
|
1589
|
+
enumOptions,
|
|
1590
|
+
enumSchemaNames,
|
|
1591
|
+
resolver
|
|
1592
|
+
}),
|
|
1566
1593
|
path: resolver.file({
|
|
1567
1594
|
name: schemaName,
|
|
1568
1595
|
extname: ".ts",
|
|
@@ -1691,7 +1718,12 @@ const typeGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1691
1718
|
const responseName = resolver.response.response(node);
|
|
1692
1719
|
const responsesWithSchema = node.responses.filter(hasSchema);
|
|
1693
1720
|
if (new Set(responsesWithSchema.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
|
|
1694
|
-
name: resolveImportName(
|
|
1721
|
+
name: resolveImportName({
|
|
1722
|
+
schemaName,
|
|
1723
|
+
enumOptions,
|
|
1724
|
+
enumSchemaNames,
|
|
1725
|
+
resolver
|
|
1726
|
+
}),
|
|
1695
1727
|
path: ""
|
|
1696
1728
|
})).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(responseName)) return null;
|
|
1697
1729
|
return renderSchemaType({
|