@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.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,
@@ -1540,6 +1540,27 @@ const zodGenerator = defineGenerator({
1540
1540
  direction
1541
1541
  })] });
1542
1542
  }
1543
+ function buildResponseUnion({ responses, name, fallbackUnknown }) {
1544
+ if (new Set(responses.flatMap((res) => (res.content ?? []).flatMap((entry) => entry.schema ? adapter.getImports(entry.schema, (schemaName) => ({
1545
+ name: resolver.name(schemaName),
1546
+ path: ""
1547
+ })).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(name)) return null;
1548
+ const members = responses.map((res) => ast.factory.createSchema({
1549
+ type: "ref",
1550
+ name: resolver.response.status(node, res.statusCode)
1551
+ }));
1552
+ if (fallbackUnknown && members.length === 0) return renderSchemaEntry({
1553
+ schema: ast.factory.createSchema({ type: "unknown" }),
1554
+ name
1555
+ });
1556
+ return renderSchemaEntry({
1557
+ schema: members.length === 1 ? members[0] : ast.factory.createSchema({
1558
+ type: "union",
1559
+ members
1560
+ }),
1561
+ name
1562
+ });
1563
+ }
1543
1564
  const paramSchemas = params.map((param) => renderSchemaEntry({
1544
1565
  schema: param.schema,
1545
1566
  name: resolver.param.name(node, param),
@@ -1557,47 +1578,17 @@ const zodGenerator = defineGenerator({
1557
1578
  });
1558
1579
  const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
1559
1580
  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;
1581
+ const responseUnionSchema = responsesWithSchema.length > 0 ? buildResponseUnion({
1582
+ responses: successResponsesWithSchema,
1583
+ name: resolver.response.response(node),
1584
+ fallbackUnknown: true
1585
+ }) : null;
1582
1586
  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;
1587
+ const errorUnionSchema = errorResponsesWithSchema.length > 0 ? buildResponseUnion({
1588
+ responses: errorResponsesWithSchema,
1589
+ name: resolver.response.error(node),
1590
+ fallbackUnknown: false
1591
+ }) : null;
1601
1592
  const requestBodyContent = node.requestBody?.content ?? [];
1602
1593
  const requestSchema = (() => {
1603
1594
  if (requestBodyContent.length === 0) return null;