@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.cjs CHANGED
@@ -151,7 +151,7 @@ const reservedWords = /* @__PURE__ */ new Set([
151
151
  */
152
152
  function isValidVarName(name) {
153
153
  if (!name || reservedWords.has(name)) return false;
154
- return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
154
+ return isIdentifier(name);
155
155
  }
156
156
  /**
157
157
  * Returns `name` when it's a syntactically valid JavaScript variable name,
@@ -438,6 +438,24 @@ function getSuccessResponses(responses) {
438
438
  return responses.filter((response) => isSuccessStatusCode(response.statusCode));
439
439
  }
440
440
  //#endregion
441
+ //#region ../../internals/shared/src/adapter.ts
442
+ /**
443
+ * Narrows the generic `Adapter` from a generator context to the OpenAPI adapter,
444
+ * so OAS-only options (`dateType`, `nameMapping`) and the parsed `document` are typed.
445
+ *
446
+ * Throws when a non-OAS adapter is configured, turning a silently wrong cast into a
447
+ * clear, actionable error at the point of use.
448
+ *
449
+ * @example
450
+ * ```ts
451
+ * const { dateType } = getOasAdapter(ctx.adapter).options
452
+ * ```
453
+ */
454
+ function getOasAdapter(adapter) {
455
+ if (adapter.name !== "oas") throw new Error(`Expected the OpenAPI adapter (adapterOas), but received "${adapter.name}". Configure \`adapter: adapterOas()\` in your Kubb config.`);
456
+ return adapter;
457
+ }
458
+ //#endregion
441
459
  //#region ../../internals/shared/src/group.ts
442
460
  /**
443
461
  * Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
@@ -1282,16 +1300,14 @@ function getStdPrinters(resolver, params) {
1282
1300
  output: cached.output,
1283
1301
  input: cached.input
1284
1302
  };
1285
- const base = {
1286
- ...params,
1287
- resolver
1288
- };
1289
1303
  const output = printerZod({
1290
- ...base,
1304
+ ...params,
1305
+ resolver,
1291
1306
  direction: "output"
1292
1307
  });
1293
1308
  const input = printerZod({
1294
- ...base,
1309
+ ...params,
1310
+ resolver,
1295
1311
  direction: "input"
1296
1312
  });
1297
1313
  zodPrinterCache.set(resolver, {
@@ -1335,7 +1351,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1335
1351
  schema(node, ctx) {
1336
1352
  const { adapter, config, resolver, root } = ctx;
1337
1353
  const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
1338
- const dateType = adapter.options.dateType;
1354
+ const dateType = getOasAdapter(adapter).options.dateType;
1339
1355
  if (!node.name) return;
1340
1356
  const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
1341
1357
  const cyclicSchemas = new Set(ctx.meta.circularNames);
@@ -1379,7 +1395,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1379
1395
  })
1380
1396
  };
1381
1397
  const inferTypeName = inferred ? resolver.schema.typeName(node.name) : null;
1382
- const nameMapping = adapter.options.nameMapping;
1398
+ const nameMapping = getOasAdapter(adapter).options.nameMapping;
1383
1399
  const stdPrinters = mini ? null : getStdPrinters(resolver, {
1384
1400
  coercion,
1385
1401
  guidType,
@@ -1452,7 +1468,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1452
1468
  if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
1453
1469
  const { adapter, config, resolver, root } = ctx;
1454
1470
  const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
1455
- const dateType = adapter.options.dateType;
1471
+ const dateType = getOasAdapter(adapter).options.dateType;
1456
1472
  const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
1457
1473
  const params = caseParams(node.parameters, "camelcase");
1458
1474
  const meta = { file: resolver.file({
@@ -1465,7 +1481,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1465
1481
  group: group ?? void 0
1466
1482
  }) };
1467
1483
  const cyclicSchemas = new Set(ctx.meta.circularNames);
1468
- const nameMapping = adapter.options.nameMapping;
1484
+ const nameMapping = getOasAdapter(adapter).options.nameMapping;
1469
1485
  function renderSchemaEntry({ schema, name, keysToOmit, direction = "output" }) {
1470
1486
  if (!schema) return null;
1471
1487
  const inferTypeName = inferred ? resolver.schema.type(name) : null;
@@ -1550,6 +1566,27 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1550
1566
  direction
1551
1567
  })] });
1552
1568
  }
1569
+ function buildResponseUnion({ responses, name, fallbackUnknown }) {
1570
+ if (new Set(responses.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
1571
+ name: resolver.name(schemaName),
1572
+ path: ""
1573
+ })).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(name)) return null;
1574
+ const members = responses.map((res) => kubb_kit.ast.factory.createSchema({
1575
+ type: "ref",
1576
+ name: resolver.response.status(node, res.statusCode)
1577
+ }));
1578
+ if (fallbackUnknown && members.length === 0) return renderSchemaEntry({
1579
+ schema: kubb_kit.ast.factory.createSchema({ type: "unknown" }),
1580
+ name
1581
+ });
1582
+ return renderSchemaEntry({
1583
+ schema: members.length === 1 ? members[0] : kubb_kit.ast.factory.createSchema({
1584
+ type: "union",
1585
+ members
1586
+ }),
1587
+ name
1588
+ });
1589
+ }
1553
1590
  const paramSchemas = params.map((param) => renderSchemaEntry({
1554
1591
  schema: param.schema,
1555
1592
  name: resolver.param.name(node, param),
@@ -1567,47 +1604,17 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1567
1604
  });
1568
1605
  const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
1569
1606
  const successResponsesWithSchema = getSuccessResponses(responsesWithSchema);
1570
- const responseUnionSchema = responsesWithSchema.length > 0 ? (() => {
1571
- const responseUnionName = resolver.response.response(node);
1572
- if (new Set(successResponsesWithSchema.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
1573
- name: resolver.name(schemaName),
1574
- path: ""
1575
- })).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(responseUnionName)) return null;
1576
- const members = successResponsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
1577
- type: "ref",
1578
- name: resolver.response.status(node, res.statusCode)
1579
- }));
1580
- if (members.length === 0) return renderSchemaEntry({
1581
- schema: kubb_kit.ast.factory.createSchema({ type: "unknown" }),
1582
- name: responseUnionName
1583
- });
1584
- return renderSchemaEntry({
1585
- schema: members.length === 1 ? members[0] : kubb_kit.ast.factory.createSchema({
1586
- type: "union",
1587
- members
1588
- }),
1589
- name: responseUnionName
1590
- });
1591
- })() : null;
1607
+ const responseUnionSchema = responsesWithSchema.length > 0 ? buildResponseUnion({
1608
+ responses: successResponsesWithSchema,
1609
+ name: resolver.response.response(node),
1610
+ fallbackUnknown: true
1611
+ }) : null;
1592
1612
  const errorResponsesWithSchema = responsesWithSchema.filter((res) => !isSuccessStatusCode(res.statusCode));
1593
- const errorUnionSchema = errorResponsesWithSchema.length > 0 ? (() => {
1594
- const errorUnionName = resolver.response.error(node);
1595
- if (new Set(errorResponsesWithSchema.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
1596
- name: resolver.name(schemaName),
1597
- path: ""
1598
- })).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(errorUnionName)) return null;
1599
- const members = errorResponsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
1600
- type: "ref",
1601
- name: resolver.response.status(node, res.statusCode)
1602
- }));
1603
- return renderSchemaEntry({
1604
- schema: members.length === 1 ? members[0] : kubb_kit.ast.factory.createSchema({
1605
- type: "union",
1606
- members
1607
- }),
1608
- name: errorUnionName
1609
- });
1610
- })() : null;
1613
+ const errorUnionSchema = errorResponsesWithSchema.length > 0 ? buildResponseUnion({
1614
+ responses: errorResponsesWithSchema,
1615
+ name: resolver.response.error(node),
1616
+ fallbackUnknown: false
1617
+ }) : null;
1611
1618
  const requestBodyContent = node.requestBody?.content ?? [];
1612
1619
  const requestSchema = (() => {
1613
1620
  if (requestBodyContent.length === 0) return null;
@@ -1756,7 +1763,7 @@ const pluginZodName = "plugin-zod";
1756
1763
  * import { pluginZod } from '@kubb/plugin-zod'
1757
1764
  *
1758
1765
  * export default defineConfig({
1759
- * input: { path: './petStore.yaml' },
1766
+ * input: './petStore.yaml',
1760
1767
  * output: { path: './src/gen' },
1761
1768
  * plugins: [
1762
1769
  * pluginTs(),