@kubb/plugin-zod 5.0.0-beta.85 → 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.d.ts CHANGED
@@ -454,7 +454,7 @@ declare const pluginZodName = "plugin-zod";
454
454
  * import { pluginZod } from '@kubb/plugin-zod'
455
455
  *
456
456
  * export default defineConfig({
457
- * input: { path: './petStore.yaml' },
457
+ * input: './petStore.yaml',
458
458
  * output: { path: './src/gen' },
459
459
  * plugins: [
460
460
  * pluginTs(),
package/dist/index.js CHANGED
@@ -141,7 +141,7 @@ const reservedWords = /* @__PURE__ */ new Set([
141
141
  */
142
142
  function isValidVarName(name) {
143
143
  if (!name || reservedWords.has(name)) return false;
144
- return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
144
+ return isIdentifier(name);
145
145
  }
146
146
  /**
147
147
  * Returns `name` when it's a syntactically valid JavaScript variable name,
@@ -428,6 +428,24 @@ function getSuccessResponses(responses) {
428
428
  return responses.filter((response) => isSuccessStatusCode(response.statusCode));
429
429
  }
430
430
  //#endregion
431
+ //#region ../../internals/shared/src/adapter.ts
432
+ /**
433
+ * Narrows the generic `Adapter` from a generator context to the OpenAPI adapter,
434
+ * so OAS-only options (`dateType`, `nameMapping`) and the parsed `document` are typed.
435
+ *
436
+ * Throws when a non-OAS adapter is configured, turning a silently wrong cast into a
437
+ * clear, actionable error at the point of use.
438
+ *
439
+ * @example
440
+ * ```ts
441
+ * const { dateType } = getOasAdapter(ctx.adapter).options
442
+ * ```
443
+ */
444
+ function getOasAdapter(adapter) {
445
+ if (adapter.name !== "oas") throw new Error(`Expected the OpenAPI adapter (adapterOas), but received "${adapter.name}". Configure \`adapter: adapterOas()\` in your Kubb config.`);
446
+ return adapter;
447
+ }
448
+ //#endregion
431
449
  //#region ../../internals/shared/src/group.ts
432
450
  /**
433
451
  * Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
@@ -1272,16 +1290,14 @@ function getStdPrinters(resolver, params) {
1272
1290
  output: cached.output,
1273
1291
  input: cached.input
1274
1292
  };
1275
- const base = {
1276
- ...params,
1277
- resolver
1278
- };
1279
1293
  const output = printerZod({
1280
- ...base,
1294
+ ...params,
1295
+ resolver,
1281
1296
  direction: "output"
1282
1297
  });
1283
1298
  const input = printerZod({
1284
- ...base,
1299
+ ...params,
1300
+ resolver,
1285
1301
  direction: "input"
1286
1302
  });
1287
1303
  zodPrinterCache.set(resolver, {
@@ -1325,7 +1341,7 @@ const zodGenerator = defineGenerator({
1325
1341
  schema(node, ctx) {
1326
1342
  const { adapter, config, resolver, root } = ctx;
1327
1343
  const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
1328
- const dateType = adapter.options.dateType;
1344
+ const dateType = getOasAdapter(adapter).options.dateType;
1329
1345
  if (!node.name) return;
1330
1346
  const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
1331
1347
  const cyclicSchemas = new Set(ctx.meta.circularNames);
@@ -1369,7 +1385,7 @@ const zodGenerator = defineGenerator({
1369
1385
  })
1370
1386
  };
1371
1387
  const inferTypeName = inferred ? resolver.schema.typeName(node.name) : null;
1372
- const nameMapping = adapter.options.nameMapping;
1388
+ const nameMapping = getOasAdapter(adapter).options.nameMapping;
1373
1389
  const stdPrinters = mini ? null : getStdPrinters(resolver, {
1374
1390
  coercion,
1375
1391
  guidType,
@@ -1442,7 +1458,7 @@ const zodGenerator = defineGenerator({
1442
1458
  if (!ast.isHttpOperationNode(node)) return null;
1443
1459
  const { adapter, config, resolver, root } = ctx;
1444
1460
  const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
1445
- const dateType = adapter.options.dateType;
1461
+ const dateType = getOasAdapter(adapter).options.dateType;
1446
1462
  const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
1447
1463
  const params = caseParams(node.parameters, "camelcase");
1448
1464
  const meta = { file: resolver.file({
@@ -1455,7 +1471,7 @@ const zodGenerator = defineGenerator({
1455
1471
  group: group ?? void 0
1456
1472
  }) };
1457
1473
  const cyclicSchemas = new Set(ctx.meta.circularNames);
1458
- const nameMapping = adapter.options.nameMapping;
1474
+ const nameMapping = getOasAdapter(adapter).options.nameMapping;
1459
1475
  function renderSchemaEntry({ schema, name, keysToOmit, direction = "output" }) {
1460
1476
  if (!schema) return null;
1461
1477
  const inferTypeName = inferred ? resolver.schema.type(name) : null;
@@ -1540,6 +1556,27 @@ const zodGenerator = defineGenerator({
1540
1556
  direction
1541
1557
  })] });
1542
1558
  }
1559
+ function buildResponseUnion({ responses, name, fallbackUnknown }) {
1560
+ if (new Set(responses.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
1561
+ name: resolver.name(schemaName),
1562
+ path: ""
1563
+ })).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(name)) return null;
1564
+ const members = responses.map((res) => ast.factory.createSchema({
1565
+ type: "ref",
1566
+ name: resolver.response.status(node, res.statusCode)
1567
+ }));
1568
+ if (fallbackUnknown && members.length === 0) return renderSchemaEntry({
1569
+ schema: ast.factory.createSchema({ type: "unknown" }),
1570
+ name
1571
+ });
1572
+ return renderSchemaEntry({
1573
+ schema: members.length === 1 ? members[0] : ast.factory.createSchema({
1574
+ type: "union",
1575
+ members
1576
+ }),
1577
+ name
1578
+ });
1579
+ }
1543
1580
  const paramSchemas = params.map((param) => renderSchemaEntry({
1544
1581
  schema: param.schema,
1545
1582
  name: resolver.param.name(node, param),
@@ -1557,47 +1594,17 @@ const zodGenerator = defineGenerator({
1557
1594
  });
1558
1595
  const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
1559
1596
  const successResponsesWithSchema = getSuccessResponses(responsesWithSchema);
1560
- const responseUnionSchema = responsesWithSchema.length > 0 ? (() => {
1561
- const responseUnionName = resolver.response.response(node);
1562
- if (new Set(successResponsesWithSchema.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
1563
- name: resolver.name(schemaName),
1564
- path: ""
1565
- })).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(responseUnionName)) return null;
1566
- const members = successResponsesWithSchema.map((res) => ast.factory.createSchema({
1567
- type: "ref",
1568
- name: resolver.response.status(node, res.statusCode)
1569
- }));
1570
- if (members.length === 0) return renderSchemaEntry({
1571
- schema: ast.factory.createSchema({ type: "unknown" }),
1572
- name: responseUnionName
1573
- });
1574
- return renderSchemaEntry({
1575
- schema: members.length === 1 ? members[0] : ast.factory.createSchema({
1576
- type: "union",
1577
- members
1578
- }),
1579
- name: responseUnionName
1580
- });
1581
- })() : null;
1597
+ const responseUnionSchema = responsesWithSchema.length > 0 ? buildResponseUnion({
1598
+ responses: successResponsesWithSchema,
1599
+ name: resolver.response.response(node),
1600
+ fallbackUnknown: true
1601
+ }) : null;
1582
1602
  const errorResponsesWithSchema = responsesWithSchema.filter((res) => !isSuccessStatusCode(res.statusCode));
1583
- const errorUnionSchema = errorResponsesWithSchema.length > 0 ? (() => {
1584
- const errorUnionName = resolver.response.error(node);
1585
- if (new Set(errorResponsesWithSchema.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
1586
- name: resolver.name(schemaName),
1587
- path: ""
1588
- })).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(errorUnionName)) return null;
1589
- const members = errorResponsesWithSchema.map((res) => ast.factory.createSchema({
1590
- type: "ref",
1591
- name: resolver.response.status(node, res.statusCode)
1592
- }));
1593
- return renderSchemaEntry({
1594
- schema: members.length === 1 ? members[0] : ast.factory.createSchema({
1595
- type: "union",
1596
- members
1597
- }),
1598
- name: errorUnionName
1599
- });
1600
- })() : null;
1603
+ const errorUnionSchema = errorResponsesWithSchema.length > 0 ? buildResponseUnion({
1604
+ responses: errorResponsesWithSchema,
1605
+ name: resolver.response.error(node),
1606
+ fallbackUnknown: false
1607
+ }) : null;
1601
1608
  const requestBodyContent = node.requestBody?.content ?? [];
1602
1609
  const requestSchema = (() => {
1603
1610
  if (requestBodyContent.length === 0) return null;
@@ -1746,7 +1753,7 @@ const pluginZodName = "plugin-zod";
1746
1753
  * import { pluginZod } from '@kubb/plugin-zod'
1747
1754
  *
1748
1755
  * export default defineConfig({
1749
- * input: { path: './petStore.yaml' },
1756
+ * input: './petStore.yaml',
1750
1757
  * output: { path: './src/gen' },
1751
1758
  * plugins: [
1752
1759
  * pluginTs(),