@sdk-it/typescript 0.47.4 → 0.48.1

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
@@ -496,7 +496,15 @@ var TypeScriptEmitter = class {
496
496
  return this.oneOf(schemas, required);
497
497
  }
498
498
  enum(values, required) {
499
- const enumValues = values.map((val) => typeof val === "string" ? `'${val}'` : `${val}`).join(" | ");
499
+ const enumValues = values.map((val) => {
500
+ if (typeof val === "string") {
501
+ return `'${val}'`;
502
+ }
503
+ if (val !== null && typeof val === "object") {
504
+ return constType(val);
505
+ }
506
+ return `${val}`;
507
+ }).join(" | ");
500
508
  return appendOptional(enumValues, required);
501
509
  }
502
510
  /**
@@ -547,8 +555,8 @@ var TypeScriptEmitter = class {
547
555
  if (schema.enum && Array.isArray(schema.enum)) {
548
556
  return this.enum(schema.enum, required);
549
557
  }
550
- if (schema.const) {
551
- return this.enum([schema.const], true);
558
+ if (schema.const !== void 0) {
559
+ return appendOptional(constType(schema.const), required);
552
560
  }
553
561
  const types = Array.isArray(schema.type) ? schema.type : schema.type ? [schema.type] : [];
554
562
  if (!types.length) {
@@ -572,6 +580,18 @@ var TypeScriptEmitter = class {
572
580
  function appendOptional(type, isRequired) {
573
581
  return isRequired ? type : `${type} | undefined`;
574
582
  }
583
+ function constType(value) {
584
+ if (Array.isArray(value)) {
585
+ return `[${value.map(constType).join(", ")}]`;
586
+ }
587
+ if (value !== null && typeof value === "object") {
588
+ const members = Object.entries(value).map(
589
+ ([key, item]) => `${JSON.stringify(key)}: ${constType(item)}`
590
+ );
591
+ return `{ ${members.join("; ")} }`;
592
+ }
593
+ return JSON.stringify(value);
594
+ }
575
595
 
576
596
  // packages/typescript/src/lib/folder-index.ts
577
597
  import { extname } from "node:path";
@@ -651,7 +671,7 @@ import {
651
671
  isRef as isRef2,
652
672
  parseRef as parseRef2
653
673
  } from "@sdk-it/core";
654
- import { isInlinable as isInlinable2, isRecursiveRef } from "@sdk-it/spec";
674
+ import { discriminatorOf, isInlinable as isInlinable2, isRecursiveRef } from "@sdk-it/spec";
655
675
  var ZodEmitter = class {
656
676
  #generatedRefs = /* @__PURE__ */ new Set();
657
677
  #spec;
@@ -778,21 +798,45 @@ var ZodEmitter = class {
778
798
  }
779
799
  return `${this.#toIntersection(allOfSchemas)}${appendOptional2(required)}`;
780
800
  }
781
- anyOf(schemas, required) {
782
- const anyOfSchemas = schemas.map((sub) => this.handle(sub, true));
801
+ anyOf(schema, required) {
802
+ const anyOfSchemas = (schema.anyOf ?? []).map(
803
+ (sub) => this.handle(sub, true)
804
+ );
783
805
  if (anyOfSchemas.length === 1) {
784
806
  return `${anyOfSchemas[0]}${appendOptional2(required)}`;
785
807
  }
808
+ const discriminated = this.#discriminatedUnion(schema, anyOfSchemas);
809
+ if (discriminated) {
810
+ return `${discriminated}${appendOptional2(required)}`;
811
+ }
786
812
  return `z.union([${anyOfSchemas.join(", ")}])${appendOptional2(required)}`;
787
813
  }
788
- oneOf(schemas, required) {
789
- const oneOfSchemas = schemas.map((sub) => this.handle(sub, true));
814
+ oneOf(schema, required) {
815
+ const oneOfSchemas = (schema.oneOf ?? []).map(
816
+ (sub) => this.handle(sub, true)
817
+ );
790
818
  if (oneOfSchemas.length === 1) {
791
819
  return `${oneOfSchemas[0]}${appendOptional2(required)}`;
792
820
  }
821
+ const discriminated = this.#discriminatedUnion(schema, oneOfSchemas);
822
+ if (discriminated) {
823
+ return `${discriminated}${appendOptional2(required)}`;
824
+ }
793
825
  return `z.xor([${oneOfSchemas.join(", ")}])${appendOptional2(required)}`;
794
826
  }
795
- enum(values) {
827
+ /**
828
+ * `branches` as a zod discriminated union, which reports an issue at the
829
+ * discriminator, when the spec says that validates the same values.
830
+ */
831
+ #discriminatedUnion(schema, branches) {
832
+ const property = discriminatorOf(this.#spec, schema);
833
+ return property === void 0 ? void 0 : `z.discriminatedUnion(${JSON.stringify(property)}, [${branches.join(", ")}])`;
834
+ }
835
+ enum(members) {
836
+ if (members.some((member) => member !== null && typeof member === "object")) {
837
+ return `z.union([${members.map(constSchema).join(", ")}])`;
838
+ }
839
+ const values = members.map((member) => JSON.stringify(member));
796
840
  if (values.length === 1) {
797
841
  return `z.literal(${values.join(", ")})`;
798
842
  }
@@ -905,18 +949,17 @@ var ZodEmitter = class {
905
949
  return this.allOf(schema.allOf ?? [], required);
906
950
  }
907
951
  if (schema.anyOf && Array.isArray(schema.anyOf)) {
908
- return this.anyOf(schema.anyOf ?? [], required);
952
+ return this.anyOf(schema, required);
909
953
  }
910
954
  if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length) {
911
- return this.oneOf(schema.oneOf ?? [], required);
955
+ return this.oneOf(schema, required);
912
956
  }
913
957
  if (schema.const !== void 0) {
914
- return `z.literal(${JSON.stringify(schema.const)})${this.#suffixes(JSON.stringify(schema.default), required, false)}`;
958
+ return `${constSchema(schema.const)}${this.#suffixes(JSON.stringify(schema.default), required, false)}`;
915
959
  }
916
960
  if (schema.enum && Array.isArray(schema.enum)) {
917
- const enumVals = schema.enum.map((val) => JSON.stringify(val));
918
- const defaultValue = enumVals.includes(JSON.stringify(schema.default)) ? JSON.stringify(schema.default) : void 0;
919
- return `${this.enum(enumVals)}${this.#suffixes(defaultValue, required, false)}`;
961
+ const defaultValue = schema.enum.map((member) => JSON.stringify(member)).includes(JSON.stringify(schema.default)) ? JSON.stringify(schema.default) : void 0;
962
+ return `${this.enum(schema.enum)}${this.#suffixes(defaultValue, required, false)}`;
920
963
  }
921
964
  const types = Array.isArray(schema.type) ? schema.type : schema.type ? [schema.type] : [];
922
965
  if (!types.length) {
@@ -987,6 +1030,21 @@ function regexLiteral(pattern) {
987
1030
  throw error;
988
1031
  }
989
1032
  }
1033
+ function constSchema(value) {
1034
+ if (value === null) {
1035
+ return "z.null()";
1036
+ }
1037
+ if (Array.isArray(value)) {
1038
+ return `z.tuple([${value.map(constSchema).join(", ")}])`;
1039
+ }
1040
+ if (typeof value === "object") {
1041
+ const shape = Object.entries(value).map(
1042
+ ([key, item]) => `${JSON.stringify(key)}: ${constSchema(item)}`
1043
+ );
1044
+ return `z.strictObject({ ${shape.join(", ")} })`;
1045
+ }
1046
+ return `z.literal(${JSON.stringify(value)})`;
1047
+ }
990
1048
 
991
1049
  // packages/typescript/src/lib/module-file.ts
992
1050
  import { spinalcase } from "stringcase";
@@ -1449,10 +1507,12 @@ var PropEmitter = class {
1449
1507
  #property(name, schema, required) {
1450
1508
  const docs = this.handle(schema);
1451
1509
  const rawType = docs[0].replace("**Type:** ", "").replace(" (nullable)", "|null");
1452
- const defaultVal = !isRef3(schema) && schema.default !== void 0 ? ` default: ${JSON.stringify(schema.default)}` : "";
1510
+ const fixed = isRef3(schema) ? "" : this.#fixedSummary(schema);
1453
1511
  const reqMark = required ? " required" : "";
1454
- const summary = `- \`${name}\` ${rawType}${reqMark}${defaultVal}:`;
1455
- const detailLines = docs.slice(1).filter((it) => !it.startsWith("**Default:**")).map((it) => ` ${it}`);
1512
+ const summary = `- \`${name}\` ${rawType}${reqMark}${fixed}:`;
1513
+ const detailLines = docs.slice(1).filter(
1514
+ (it) => !it.startsWith("**Value:**") && !it.startsWith("**Default:**")
1515
+ ).map((it) => ` ${it}`);
1456
1516
  return [summary, ...detailLines];
1457
1517
  }
1458
1518
  /**
@@ -1494,8 +1554,30 @@ var PropEmitter = class {
1494
1554
  });
1495
1555
  return lines;
1496
1556
  }
1497
- #anyOf(schemas) {
1498
- const lines = ["**Any of (Union):**"];
1557
+ #fixedSummary(schema) {
1558
+ if (schema.const !== void 0) {
1559
+ return ` value: ${JSON.stringify(schema.const)}`;
1560
+ }
1561
+ if (schema.default !== void 0) {
1562
+ return ` default: ${JSON.stringify(schema.default)}`;
1563
+ }
1564
+ return "";
1565
+ }
1566
+ #fixedLines(schema) {
1567
+ const lines = [];
1568
+ if (schema.const !== void 0) {
1569
+ lines.push(`**Value:** \`${JSON.stringify(schema.const)}\``);
1570
+ }
1571
+ if (schema.default !== void 0) {
1572
+ lines.push(`**Default:** \`${JSON.stringify(schema.default)}\``);
1573
+ }
1574
+ return lines;
1575
+ }
1576
+ #discriminator(schema) {
1577
+ return schema.discriminator ? [`**Discriminator:** \`${schema.discriminator.propertyName}\``] : [];
1578
+ }
1579
+ #anyOf(schema, schemas) {
1580
+ const lines = ["**Any of (Union):**", ...this.#discriminator(schema)];
1499
1581
  schemas.forEach((subSchema, index) => {
1500
1582
  lines.push(`- **Option ${index + 1}:**`);
1501
1583
  const subLines = this.handle(subSchema);
@@ -1503,8 +1585,11 @@ var PropEmitter = class {
1503
1585
  });
1504
1586
  return lines;
1505
1587
  }
1506
- #oneOf(schemas) {
1507
- const lines = ["**One of (Exclusive Union):**"];
1588
+ #oneOf(schema, schemas) {
1589
+ const lines = [
1590
+ "**One of (Exclusive Union):**",
1591
+ ...this.#discriminator(schema)
1592
+ ];
1508
1593
  schemas.forEach((subSchema, index) => {
1509
1594
  lines.push(`- **Option ${index + 1}:**`);
1510
1595
  const subLines = this.handle(subSchema);
@@ -1602,9 +1687,7 @@ var PropEmitter = class {
1602
1687
  lines.push(`**Type:** \`${type}\`${nullableSuffix}`);
1603
1688
  lines.push(...description);
1604
1689
  }
1605
- if (schema.default !== void 0) {
1606
- lines.push(`**Default:** \`${JSON.stringify(schema.default)}\``);
1607
- }
1690
+ lines.push(...this.#fixedLines(schema));
1608
1691
  return lines.filter((l) => l);
1609
1692
  }
1610
1693
  /**
@@ -1619,10 +1702,10 @@ var PropEmitter = class {
1619
1702
  return this.#allOf(schema.allOf);
1620
1703
  }
1621
1704
  if (schema.anyOf && Array.isArray(schema.anyOf)) {
1622
- return this.#anyOf(schema.anyOf);
1705
+ return this.#anyOf(schema, schema.anyOf);
1623
1706
  }
1624
1707
  if (schema.oneOf && Array.isArray(schema.oneOf)) {
1625
- return this.#oneOf(schema.oneOf);
1708
+ return this.#oneOf(schema, schema.oneOf);
1626
1709
  }
1627
1710
  if (schema.enum && Array.isArray(schema.enum)) {
1628
1711
  return this.#enum(schema);
@@ -1643,8 +1726,7 @@ var PropEmitter = class {
1643
1726
  if (types.length === 0) {
1644
1727
  const lines2 = ["**Type:** `unknown`"];
1645
1728
  if (schema.description) lines2.push(schema.description);
1646
- if (schema.default !== void 0)
1647
- lines2.push(`**Default:** \`${JSON.stringify(schema.default)}\``);
1729
+ lines2.push(...this.#fixedLines(schema));
1648
1730
  return lines2;
1649
1731
  }
1650
1732
  if (types.length === 1) {
@@ -1654,8 +1736,7 @@ var PropEmitter = class {
1654
1736
  const nullableSuffix = nullable ? " (nullable)" : "";
1655
1737
  const lines = [`**Type:** \`${typeString}\`${nullableSuffix}`];
1656
1738
  if (schema.description) lines.push(schema.description);
1657
- if (schema.default !== void 0)
1658
- lines.push(`**Default:** \`${JSON.stringify(schema.default)}\``);
1739
+ lines.push(...this.#fixedLines(schema));
1659
1740
  return lines;
1660
1741
  }
1661
1742
  /** The documentation of a request body, one block per content type. */
@@ -1876,7 +1957,7 @@ var SnippetEmitter = class {
1876
1957
  for (const [propName, propSchema] of Object.entries(properties)) {
1877
1958
  const isRequired = (schemaObj.required ?? []).includes(propName);
1878
1959
  const resolvedProp = resolveRef2(this.spec, propSchema);
1879
- if (isRequired || resolvedProp.example !== void 0 || resolvedProp.default !== void 0) {
1960
+ if (isRequired || resolvedProp.const !== void 0 || resolvedProp.example !== void 0 || resolvedProp.default !== void 0) {
1880
1961
  result[propName] = this.handle(propSchema);
1881
1962
  }
1882
1963
  }
@@ -1901,8 +1982,6 @@ var SnippetEmitter = class {
1901
1982
  return result;
1902
1983
  }
1903
1984
  string(schema) {
1904
- if (schema.example !== void 0) return String(schema.example);
1905
- if (schema.default !== void 0) return String(schema.default);
1906
1985
  switch (schema.format) {
1907
1986
  case "date-time":
1908
1987
  case "datetime":
@@ -1928,15 +2007,10 @@ var SnippetEmitter = class {
1928
2007
  case "byte":
1929
2008
  return `new Blob(['example'], { type: 'text/plain' })`;
1930
2009
  default:
1931
- if (schema.enum && schema.enum.length > 0) {
1932
- return String(schema.enum[0]);
1933
- }
1934
2010
  return schema.pattern ? `string matching ${schema.pattern}` : "example";
1935
2011
  }
1936
2012
  }
1937
2013
  number(schema) {
1938
- if (schema.example !== void 0) return Number(schema.example);
1939
- if (schema.default !== void 0) return Number(schema.default);
1940
2014
  let value;
1941
2015
  if (typeof schema.exclusiveMinimum === "number") {
1942
2016
  value = schema.exclusiveMinimum + 1;
@@ -1955,9 +2029,7 @@ var SnippetEmitter = class {
1955
2029
  }
1956
2030
  return schema.type === "integer" ? Math.floor(value) : value;
1957
2031
  }
1958
- boolean(schema) {
1959
- if (schema.example !== void 0) return Boolean(schema.example);
1960
- if (schema.default !== void 0) return Boolean(schema.default);
2032
+ boolean() {
1961
2033
  return true;
1962
2034
  }
1963
2035
  null() {
@@ -2001,6 +2073,9 @@ var SnippetEmitter = class {
2001
2073
  return this.ref(schemaOrRef.$ref);
2002
2074
  }
2003
2075
  const schema = resolveRef2(this.spec, schemaOrRef);
2076
+ if (schema.const !== void 0) {
2077
+ return schema.const;
2078
+ }
2004
2079
  if (schema.example !== void 0) {
2005
2080
  return schema.example;
2006
2081
  }
@@ -2036,7 +2111,7 @@ var SnippetEmitter = class {
2036
2111
  case "integer":
2037
2112
  return this.number(schema);
2038
2113
  case "boolean":
2039
- return this.boolean(schema);
2114
+ return this.boolean();
2040
2115
  case "object":
2041
2116
  return this.object(schema);
2042
2117
  case "array":