@kubb/plugin-zod 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 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,
@@ -1550,6 +1550,27 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1550
1550
  direction
1551
1551
  })] });
1552
1552
  }
1553
+ function buildResponseUnion({ responses, name, fallbackUnknown }) {
1554
+ if (new Set(responses.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
1555
+ name: resolver.name(schemaName),
1556
+ path: ""
1557
+ })).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(name)) return null;
1558
+ const members = responses.map((res) => kubb_kit.ast.factory.createSchema({
1559
+ type: "ref",
1560
+ name: resolver.response.status(node, res.statusCode)
1561
+ }));
1562
+ if (fallbackUnknown && members.length === 0) return renderSchemaEntry({
1563
+ schema: kubb_kit.ast.factory.createSchema({ type: "unknown" }),
1564
+ name
1565
+ });
1566
+ return renderSchemaEntry({
1567
+ schema: members.length === 1 ? members[0] : kubb_kit.ast.factory.createSchema({
1568
+ type: "union",
1569
+ members
1570
+ }),
1571
+ name
1572
+ });
1573
+ }
1553
1574
  const paramSchemas = params.map((param) => renderSchemaEntry({
1554
1575
  schema: param.schema,
1555
1576
  name: resolver.param.name(node, param),
@@ -1567,47 +1588,17 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1567
1588
  });
1568
1589
  const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
1569
1590
  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;
1591
+ const responseUnionSchema = responsesWithSchema.length > 0 ? buildResponseUnion({
1592
+ responses: successResponsesWithSchema,
1593
+ name: resolver.response.response(node),
1594
+ fallbackUnknown: true
1595
+ }) : null;
1592
1596
  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;
1597
+ const errorUnionSchema = errorResponsesWithSchema.length > 0 ? buildResponseUnion({
1598
+ responses: errorResponsesWithSchema,
1599
+ name: resolver.response.error(node),
1600
+ fallbackUnknown: false
1601
+ }) : null;
1611
1602
  const requestBodyContent = node.requestBody?.content ?? [];
1612
1603
  const requestSchema = (() => {
1613
1604
  if (requestBodyContent.length === 0) return null;