swagger-typescript-api 13.10.0 → 13.11.0

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.
@@ -208,7 +208,7 @@ var ComponentTypeNameResolver = class extends NameResolver {
208
208
  //#endregion
209
209
  //#region package.json
210
210
  var name = "swagger-typescript-api";
211
- var version = "13.10.0";
211
+ var version = "13.11.0";
212
212
  var description = "Generate the API client for Fetch or Axios from an OpenAPI Specification";
213
213
  //#endregion
214
214
  //#region src/constants.ts
@@ -414,6 +414,7 @@ var CodeGenConfig = class {
414
414
  enumKeyPrefix = "";
415
415
  enumKeySuffix = "";
416
416
  patch = false;
417
+ preferExistingSchemaNamesForExternalRefs = false;
417
418
  componentTypeNameResolver;
418
419
  /** name of the main exported class */
419
420
  apiClassName = "Api";
@@ -708,6 +709,18 @@ function pascalCase(value) {
708
709
  }
709
710
  //#endregion
710
711
  //#region src/schema-components-map.ts
712
+ const OPENAPI_COMPONENT_NAMES = new Set([
713
+ "schemas",
714
+ "responses",
715
+ "requestBodies",
716
+ "parameters",
717
+ "headers",
718
+ "securitySchemes",
719
+ "links",
720
+ "callbacks",
721
+ "examples",
722
+ "pathItems"
723
+ ]);
711
724
  var SchemaComponentsMap = class {
712
725
  _data = [];
713
726
  constructor(config) {
@@ -739,18 +752,49 @@ var SchemaComponentsMap = class {
739
752
  });
740
753
  return matchingComponents.length === 1 ? matchingComponents[0] : null;
741
754
  }
755
+ normalizeTypeNameFromFile(typeName) {
756
+ return typeName.replace(/\.(yaml|yml|json)$/i, "");
757
+ }
758
+ resolveComponentName(rawComponentName) {
759
+ const normalizedComponentName = rawComponentName === "definitions" ? "schemas" : rawComponentName;
760
+ return OPENAPI_COMPONENT_NAMES.has(normalizedComponentName) ? normalizedComponentName : "schemas";
761
+ }
762
+ isFileOnlyRef(ref) {
763
+ const [, rawPointer = ""] = ref.split("#");
764
+ return !rawPointer.replace(/^\/+/, "");
765
+ }
766
+ unwrapExternalComponentsDocument(ref, resolved) {
767
+ if (!this.isFileOnlyRef(ref)) return null;
768
+ const schemas = resolved.components?.schemas;
769
+ if (!schemas || typeof schemas !== "object") return null;
770
+ const schemaEntries = Object.entries(schemas).filter(([, schemaData]) => schemaData != null && typeof schemaData === "object");
771
+ if (schemaEntries.length !== 1) return null;
772
+ const [schemaName, schemaData] = schemaEntries[0];
773
+ return {
774
+ ref: `${ref}#/components/schemas/${schemaName}`,
775
+ resolved: schemaData
776
+ };
777
+ }
778
+ isRefOnlyRawTypeData(rawTypeData) {
779
+ if (!rawTypeData || typeof rawTypeData !== "object") return false;
780
+ return Object.keys(rawTypeData).length === 1 && typeof rawTypeData.$ref === "string";
781
+ }
782
+ preferExistingSchemaNameForExternalRef(typeName, refDetails) {
783
+ if (!this.config.preferExistingSchemaNamesForExternalRefs) return false;
784
+ return pascalCase(refDetails.externalOpenapiFileName || "External") === typeName;
785
+ }
742
786
  createComponentDraft($ref, rawTypeData) {
743
787
  if (yummies_type_guard.typeGuard.isObject(rawTypeData) && rawTypeData.typeName && rawTypeData.rawTypeData && rawTypeData.$ref) return rawTypeData;
744
788
  const parsed = this.parseRef($ref);
745
789
  const [, rawPointer = ""] = $ref.split("#");
746
790
  const pointerParts = (rawPointer.startsWith("/") ? rawPointer : `/${rawPointer}`).split("/").filter(Boolean);
747
- const typeName = pointerParts.at(-1) || parsed.at(-1) || "Unknown";
791
+ const typeName = this.normalizeTypeNameFromFile(pointerParts.at(-1) || parsed.at(-1) || "Unknown");
748
792
  const rawComponentName = pointerParts.at(-2) || parsed[parsed.length - 2] || "schemas";
749
793
  return {
750
794
  $ref,
751
795
  typeName,
752
796
  rawTypeData,
753
- componentName: rawComponentName === "definitions" ? "schemas" : rawComponentName,
797
+ componentName: this.resolveComponentName(rawComponentName),
754
798
  /** result from schema parser */
755
799
  typeData: null
756
800
  };
@@ -770,6 +814,20 @@ var SchemaComponentsMap = class {
770
814
  filter(...componentNames) {
771
815
  return this._data.filter((it) => componentNames.some((componentName) => it.$ref.startsWith(`#/components/${componentName}`)));
772
816
  }
817
+ resolveRefOnlyComponents() {
818
+ if (!this.config.preferExistingSchemaNamesForExternalRefs) return;
819
+ const { resolvedSwaggerSchema } = this.config;
820
+ for (const component of this._data) {
821
+ if (!this.isRefOnlyRawTypeData(component.rawTypeData)) continue;
822
+ const ref = component.rawTypeData?.$ref;
823
+ if (typeof ref !== "string") continue;
824
+ const resolved = resolvedSwaggerSchema.getRef(ref);
825
+ if (resolved == null || typeof resolved !== "object") continue;
826
+ component.rawTypeData = resolved;
827
+ component.typeData = null;
828
+ delete component.$prepared;
829
+ }
830
+ }
773
831
  get = ($ref) => {
774
832
  const localFound = this._data.find((c) => c.$ref === $ref) || this.getByLocalFragmentRef($ref) || null;
775
833
  if (localFound != null) return localFound;
@@ -778,9 +836,24 @@ var SchemaComponentsMap = class {
778
836
  const foundByRef = resolvedSwaggerSchema.getRef($ref);
779
837
  const refDetails = resolvedSwaggerSchema.getRefDetails($ref);
780
838
  if (foundByRef != null) {
781
- const componentDraft = this.createComponentDraft($ref, foundByRef);
839
+ let resolvedRef = $ref;
840
+ let resolvedTypeData = foundByRef;
841
+ const unwrappedComponentsDocument = this.unwrapExternalComponentsDocument($ref, resolvedTypeData);
842
+ if (unwrappedComponentsDocument) {
843
+ resolvedRef = unwrappedComponentsDocument.ref;
844
+ resolvedTypeData = unwrappedComponentsDocument.resolved;
845
+ }
846
+ const componentDraft = this.createComponentDraft(resolvedRef, resolvedTypeData);
782
847
  componentDraft.typeName = this.config.hooks.onFormatExternalTypeName?.(componentDraft.typeName, refDetails) || componentDraft.typeName;
783
- if (this._data.some((component) => component.typeName === componentDraft.typeName)) componentDraft.typeName = this.config.hooks.onFixDuplicateExternalTypeName?.(componentDraft.typeName, refDetails, this._data.map((it) => it.typeName)) ?? `${pascalCase(refDetails.externalOpenapiFileName || "External")}${componentDraft.typeName}`;
848
+ if (this._data.some((component) => component.typeName === componentDraft.typeName)) {
849
+ if (this.preferExistingSchemaNameForExternalRef(componentDraft.typeName, refDetails)) {
850
+ const existingComponent = this._data.find((component) => component.typeName === componentDraft.typeName);
851
+ if (existingComponent) return existingComponent;
852
+ }
853
+ componentDraft.typeName = this.config.hooks.onFixDuplicateExternalTypeName?.(componentDraft.typeName, refDetails, this._data.map((it) => it.typeName)) ?? `${pascalCase(refDetails.externalOpenapiFileName || "External")}${componentDraft.typeName}`;
854
+ }
855
+ const existingComponent = this._data.find((component) => component.componentName === componentDraft.componentName && component.typeName === componentDraft.typeName);
856
+ if (existingComponent) return existingComponent;
784
857
  return this.createComponent($ref, componentDraft);
785
858
  }
786
859
  return null;
@@ -3588,6 +3661,7 @@ var CodeGenProcess = class {
3588
3661
  ]), rawTypeData);
3589
3662
  this.schemaComponentsMap.discriminatorsFirst();
3590
3663
  this.schemaComponentsMap.enumsFirst();
3664
+ this.schemaComponentsMap.resolveRefOnlyComponents();
3591
3665
  const componentsToParse = this.schemaComponentsMap.filter((0, es_toolkit.compact)(["schemas", this.config.extractResponses && "responses"]));
3592
3666
  this.typeNameFormatter.precommit(componentsToParse.map((c) => c.typeName));
3593
3667
  const parsedSchemas = componentsToParse.map((schemaComponent) => {
@@ -3679,9 +3753,17 @@ var CodeGenProcess = class {
3679
3753
  while (processedCount < schemaComponentsCount) {
3680
3754
  modelTypes = [];
3681
3755
  processedCount = 0;
3756
+ const seenExportNames = /* @__PURE__ */ new Set();
3682
3757
  for (const component of components) if (modelTypeComponents.includes(component.componentName)) {
3683
3758
  const modelType = this.prepareModelType(component);
3684
- if (modelType) modelTypes.push(modelType);
3759
+ if (modelType) {
3760
+ if (seenExportNames.has(modelType.name)) {
3761
+ processedCount++;
3762
+ continue;
3763
+ }
3764
+ seenExportNames.add(modelType.name);
3765
+ modelTypes.push(modelType);
3766
+ }
3685
3767
  processedCount++;
3686
3768
  }
3687
3769
  schemaComponentsCount = getSchemaComponentsCount();
@@ -4053,4 +4135,4 @@ Object.defineProperty(exports, "version", {
4053
4135
  }
4054
4136
  });
4055
4137
 
4056
- //# sourceMappingURL=src-B2z6JCvN.cjs.map
4138
+ //# sourceMappingURL=src-C6CxNImi.cjs.map