swagger-typescript-api 13.5.0 → 13.6.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.
@@ -213,7 +213,7 @@ var ComponentTypeNameResolver = class extends NameResolver {
213
213
  //#endregion
214
214
  //#region package.json
215
215
  var name = "swagger-typescript-api";
216
- var version = "13.5.0";
216
+ var version = "13.6.1";
217
217
  var description = "Generate the API client for Fetch or Axios from an OpenAPI Specification";
218
218
  //#endregion
219
219
  //#region src/constants.ts
@@ -1451,7 +1451,7 @@ var SchemaUtils = class {
1451
1451
  this.schemaComponentsMap = schemaComponentsMap;
1452
1452
  this.typeNameFormatter = typeNameFormatter;
1453
1453
  }
1454
- isBinaryLikeContentMediaType = (contentMediaType) => {
1454
+ isBinaryLikeMimeType = (contentMediaType) => {
1455
1455
  if (typeof contentMediaType !== "string" || !contentMediaType) return false;
1456
1456
  const mediaType = contentMediaType.split(";")[0]?.trim().toLowerCase();
1457
1457
  if (!mediaType) return false;
@@ -1462,10 +1462,13 @@ var SchemaUtils = class {
1462
1462
  */
1463
1463
  if (mediaType.startsWith("text/")) return false;
1464
1464
  if (mediaType.includes("json") || mediaType.includes("+json")) return false;
1465
+ /** application/vnd.* binary types first: names like "openxmlformats" / "spreadsheetml" contain "xml" but are binary. */
1466
+ if (mediaType.startsWith("application/vnd.")) return mediaType.endsWith(".blob") || mediaType.includes("spreadsheetml.sheet") || mediaType.startsWith("application/vnd.ms-excel") || mediaType.startsWith("application/vnd.openxmlformats-officedocument.") || mediaType === "application/vnd.rar" || mediaType.startsWith("application/vnd.oasis.opendocument.") || mediaType.startsWith("application/vnd.ms-powerpoint") || mediaType.startsWith("application/vnd.ms-fontobject") || mediaType === "application/vnd.visio" || mediaType === "application/vnd.amazon.ebook";
1465
1467
  if (mediaType.includes("xml") || mediaType.includes("+xml")) return false;
1466
1468
  if (mediaType === "application/x-www-form-urlencoded") return false;
1467
- if (mediaType === "application/javascript" || mediaType === "application/ecmascript" || mediaType === "application/graphql" || mediaType === "application/yaml" || mediaType === "application/x-yaml") return false;
1468
- return mediaType === "application/octet-stream" || mediaType.startsWith("image/") || mediaType.startsWith("audio/") || mediaType.startsWith("video/") || mediaType.startsWith("font/") || mediaType.startsWith("model/") || mediaType.startsWith("application/");
1469
+ if (mediaType === "application/javascript" || mediaType === "application/ecmascript" || mediaType === "application/graphql" || mediaType === "application/yaml" || mediaType === "application/x-yaml" || mediaType === "application/jwt") return false;
1470
+ if (mediaType.startsWith("application/")) return mediaType === "application/octet-stream" || mediaType.startsWith("application/pdf") || mediaType === "application/zip" || mediaType.startsWith("application/x-zip") || mediaType === "application/gzip" || mediaType.startsWith("application/x-gzip") || mediaType.startsWith("application/x-bzip") || mediaType === "application/x-bzip2" || mediaType.startsWith("application/x-tar") || mediaType.startsWith("application/x-rar") || mediaType.startsWith("application/x-7z") || mediaType === "application/x-binary" || mediaType === "application/java-archive" || mediaType === "application/epub+zip" || mediaType === "application/msword" || mediaType === "application/rtf" || mediaType === "application/x-abiword" || mediaType === "application/x-freearc";
1471
+ return mediaType.startsWith("image/") || mediaType.startsWith("audio/") || mediaType.startsWith("video/") || mediaType.startsWith("font/") || mediaType.startsWith("model/");
1469
1472
  };
1470
1473
  getRequiredProperties = (schema) => {
1471
1474
  return (0, es_toolkit.uniq)(schema && Array.isArray(schema.required) && schema.required || []);
@@ -1575,7 +1578,7 @@ var SchemaUtils = class {
1575
1578
  else {
1576
1579
  const primitiveType = this.getSchemaPrimitiveType(schema);
1577
1580
  if (primitiveType == null) return this.config.Ts.Keyword.Any;
1578
- if (primitiveType === this.config.Ts.Keyword.String && !schema.format && this.isBinaryLikeContentMediaType(schema.contentMediaType)) resultType = this.config.Ts.UnionType([this.config.Ts.Keyword.File, this.config.Ts.Keyword.Blob]);
1581
+ if (primitiveType === this.config.Ts.Keyword.String && !schema.format && this.isBinaryLikeMimeType(schema.contentMediaType)) resultType = this.config.Ts.UnionType([this.config.Ts.Keyword.File, this.config.Ts.Keyword.Blob]);
1579
1582
  else {
1580
1583
  const typeAlias = (0, es_toolkit_compat.get)(this.config.primitiveTypes, [primitiveType, schema.format]) || (0, es_toolkit_compat.get)(this.config.primitiveTypes, [primitiveType, "$default"]) || this.config.primitiveTypes[primitiveType];
1581
1584
  if (typeof typeAlias === "function") resultType = typeAlias(schema, this);
@@ -1837,6 +1840,8 @@ var SchemaRoutes = class {
1837
1840
  if (contentTypes.some((contentType) => contentType.startsWith("text/"))) return CONTENT_KIND.TEXT;
1838
1841
  return CONTENT_KIND.OTHER;
1839
1842
  };
1843
+ /** True when response produces only binary media types (e.g. file download). */
1844
+ isBinaryOnlyContentTypes = (contentTypes) => !!contentTypes?.length && contentTypes.every((ct) => this.schemaUtils.isBinaryLikeMimeType(ct));
1840
1845
  isSuccessStatus = (status) => this.config.defaultResponseAsSuccess && status === "default" || +status >= this.config.successResponseStatusRange[0] && +status <= this.config.successResponseStatusRange[1] || status === "2xx";
1841
1846
  getSchemaFromRequestType = (requestInfo) => {
1842
1847
  const content = (0, es_toolkit_compat.get)(requestInfo, "content");
@@ -1916,9 +1921,18 @@ var SchemaRoutes = class {
1916
1921
  return acc;
1917
1922
  }, []);
1918
1923
  };
1919
- getResponseBodyInfo = (routeInfo, parsedSchemas, resolvedSwaggerSchema) => {
1924
+ getResponseBodyInfo = (routeInfo, parsedSchemas, resolvedSwaggerSchema, pathName, method) => {
1920
1925
  const { produces, operationId, responses } = routeInfo;
1921
1926
  const contentTypes = this.getContentTypes(responses, [...produces || [], routeInfo["x-accepts"]]);
1927
+ const successStatus = Object.keys(responses || {}).find((s) => this.isSuccessStatus(s));
1928
+ const successResponseContent = successStatus && responses?.[successStatus];
1929
+ const successContentTypes = successResponseContent?.content && typeof successResponseContent.content === "object" ? Object.keys(successResponseContent.content) : null;
1930
+ const originalProduces = pathName && method ? resolvedSwaggerSchema.getOriginalProduces(pathName, method) ?? (0, es_toolkit_compat.get)(resolvedSwaggerSchema.originalSchema, [
1931
+ "paths",
1932
+ pathName,
1933
+ method,
1934
+ "produces"
1935
+ ]) : void 0;
1922
1936
  const responseInfos = this.getRequestInfoTypes({
1923
1937
  requestInfos: responses,
1924
1938
  parsedSchemas,
@@ -1936,13 +1950,14 @@ var SchemaRoutes = class {
1936
1950
  }));
1937
1951
  return `headers: { ${Object.entries(headerTypes).map(([k, v]) => `"${k}": ${v}`).join(",")} },`;
1938
1952
  };
1953
+ const typesToCheck = (Array.isArray(originalProduces) && originalProduces.length > 0 ? originalProduces : null) ?? (produces?.length ? produces : null) ?? (successContentTypes?.length ? successContentTypes : null) ?? contentTypes;
1939
1954
  return {
1940
1955
  contentTypes,
1941
1956
  responses: responseInfos,
1942
1957
  links,
1943
1958
  success: {
1944
1959
  schema: successResponse,
1945
- type: successResponse?.type || this.config.Ts.Keyword.Any
1960
+ type: this.isBinaryOnlyContentTypes(typesToCheck) ? this.config.Ts.Keyword.Blob : successResponse?.type || this.config.Ts.Keyword.Any
1946
1961
  },
1947
1962
  error: {
1948
1963
  schemas: errorResponses,
@@ -2142,7 +2157,7 @@ var SchemaRoutes = class {
2142
2157
  description: pathArgSchema.description
2143
2158
  }));
2144
2159
  const pathArgsNames = pathArgs.map((arg) => arg.name);
2145
- const responseBodyInfo = this.getResponseBodyInfo(routeInfo, parsedSchemas, resolvedSwaggerSchema);
2160
+ const responseBodyInfo = this.getResponseBodyInfo(routeInfo, parsedSchemas, resolvedSwaggerSchema, rawRouteName, method);
2146
2161
  const rawRouteInfo = {
2147
2162
  ...otherInfo,
2148
2163
  pathArgs,
@@ -2442,13 +2457,18 @@ var ResolvedSwaggerSchema = class ResolvedSwaggerSchema {
2442
2457
  if (resolved == null) return null;
2443
2458
  return this.absolutizeLocalRefs(resolved, this.stripHash(externalPath));
2444
2459
  }
2445
- constructor(config, usageSchema, originalSchema, resolvers) {
2460
+ originalProducesByRoute = Object.create(null);
2461
+ constructor(config, usageSchema, originalSchema, resolvers, originalProducesByRoute) {
2446
2462
  this.config = config;
2447
2463
  this.usageSchema = usageSchema;
2448
2464
  this.originalSchema = originalSchema;
2449
2465
  this.resolvers = resolvers;
2450
2466
  this.usageSchema = usageSchema;
2451
2467
  this.originalSchema = originalSchema;
2468
+ if (originalProducesByRoute) this.originalProducesByRoute = originalProducesByRoute;
2469
+ }
2470
+ getOriginalProduces(pathName, method) {
2471
+ return this.originalProducesByRoute[pathName]?.[method];
2452
2472
  }
2453
2473
  getRefDetails(ref) {
2454
2474
  const normalizedRef = this.normalizeRef(ref);
@@ -2616,7 +2636,7 @@ var ResolvedSwaggerSchema = class ResolvedSwaggerSchema {
2616
2636
  }
2617
2637
  return null;
2618
2638
  }
2619
- static async create(config, usageSchema, originalSchema) {
2639
+ static async create(config, usageSchema, originalSchema, originalProducesByRoute) {
2620
2640
  const resolvers = [];
2621
2641
  const options = {
2622
2642
  continueOnError: true,
@@ -2649,7 +2669,7 @@ var ResolvedSwaggerSchema = class ResolvedSwaggerSchema {
2649
2669
  } catch (e) {
2650
2670
  consola.default.debug(e);
2651
2671
  }
2652
- const resolvedSwaggerSchema = new ResolvedSwaggerSchema(config, usageSchema, originalSchema, resolvers);
2672
+ const resolvedSwaggerSchema = new ResolvedSwaggerSchema(config, usageSchema, originalSchema, resolvers, originalProducesByRoute);
2653
2673
  await resolvedSwaggerSchema.warmUpRemoteSchemasCache();
2654
2674
  return resolvedSwaggerSchema;
2655
2675
  }
@@ -2694,12 +2714,13 @@ var SwaggerSchemaResolver = class {
2694
2714
  const swaggerSchemaObject = this.processSwaggerSchemaFile(swaggerSchemaFile);
2695
2715
  swaggerSchemas = await this.convertSwaggerObject(swaggerSchemaObject, { patch });
2696
2716
  }
2697
- this.fixSwaggerSchemas(swaggerSchemas);
2698
- return ResolvedSwaggerSchema.create(this.config, swaggerSchemas.usageSchema, swaggerSchemas.originalSchema);
2717
+ const originalProducesByRoute = this.fixSwaggerSchemas(swaggerSchemas);
2718
+ return await ResolvedSwaggerSchema.create(this.config, swaggerSchemas.usageSchema, swaggerSchemas.originalSchema, originalProducesByRoute);
2699
2719
  }
2700
2720
  convertSwaggerObject(swaggerSchema, converterOptions) {
2701
2721
  return new Promise((resolve) => {
2702
2722
  const result = structuredClone(swaggerSchema);
2723
+ const originalSchemaForProduces = structuredClone(swaggerSchema);
2703
2724
  result.info = (0, es_toolkit.merge)({
2704
2725
  title: "No title",
2705
2726
  version: ""
@@ -2718,12 +2739,14 @@ var SwaggerSchemaResolver = class {
2718
2739
  this.config.update({ convertedFromSwagger2: true });
2719
2740
  resolve({
2720
2741
  usageSchema: parsedSwaggerSchema,
2721
- originalSchema: result
2742
+ originalSchema: result,
2743
+ originalSchemaForProduces
2722
2744
  });
2723
2745
  });
2724
2746
  } else resolve({
2725
2747
  usageSchema: result,
2726
- originalSchema: structuredClone(result)
2748
+ originalSchema: structuredClone(result),
2749
+ originalSchemaForProduces
2727
2750
  });
2728
2751
  });
2729
2752
  }
@@ -2764,13 +2787,18 @@ var SwaggerSchemaResolver = class {
2764
2787
  if (typeof objectSchema.$ref === "string") objectSchema.$ref = this.normalizeRefValue(objectSchema.$ref);
2765
2788
  for (const value of Object.values(objectSchema)) this.normalizeRefsInSchema(value);
2766
2789
  }
2767
- fixSwaggerSchemas({ usageSchema, originalSchema }) {
2790
+ fixSwaggerSchemas({ usageSchema, originalSchema, originalSchemaForProduces }) {
2768
2791
  this.normalizeRefsInSchema(usageSchema);
2769
2792
  this.normalizeRefsInSchema(originalSchema);
2770
2793
  const usagePaths = (0, es_toolkit_compat.get)(usageSchema, "paths") || {};
2771
- const originalPaths = (0, es_toolkit_compat.get)(originalSchema, "paths") || {};
2794
+ const schemaForProduces = originalSchemaForProduces ?? originalSchema;
2795
+ const originalPaths = (0, es_toolkit_compat.get)(schemaForProduces, "paths") || {};
2796
+ const basePath = schemaForProduces.basePath?.replace(/\/$/, "") || "";
2797
+ const originalProducesByRoute = Object.create(null);
2772
2798
  for (const [route, usagePathObject] of Object.entries(usagePaths)) {
2773
- const originalPathObject = (0, es_toolkit_compat.get)(originalPaths, route) || {};
2799
+ const routeWithoutBase = basePath && route.startsWith(basePath) ? route.slice(basePath.length) || "/" : route;
2800
+ const routeWithBase = basePath && !route.startsWith(basePath) ? `${basePath}${route.startsWith("/") ? route : `/${route}`}` : route;
2801
+ const originalPathObject = (0, es_toolkit_compat.get)(originalPaths, route) || (0, es_toolkit_compat.get)(originalPaths, routeWithoutBase.startsWith("/") ? routeWithoutBase : `/${routeWithoutBase}`) || (0, es_toolkit_compat.get)(originalPaths, routeWithBase) || {};
2774
2802
  for (const [methodName, usageRouteInfo] of Object.entries(usagePathObject)) {
2775
2803
  const originalRouteInfo = (0, es_toolkit_compat.get)(originalPathObject, methodName) || {};
2776
2804
  const usageRouteParams = (0, es_toolkit_compat.get)(usageRouteInfo, "parameters") || [];
@@ -2778,11 +2806,18 @@ var SwaggerSchemaResolver = class {
2778
2806
  const usageAsOpenapiv2 = usageRouteInfo;
2779
2807
  if (typeof usageRouteInfo === "object") {
2780
2808
  usageAsOpenapiv2.consumes = (0, es_toolkit.uniq)((0, es_toolkit.compact)([...usageAsOpenapiv2.consumes || [], ...originalRouteInfo.consumes || []]));
2781
- usageAsOpenapiv2.produces = (0, es_toolkit.uniq)((0, es_toolkit.compact)([...usageAsOpenapiv2.produces || [], ...originalRouteInfo.produces || []]));
2809
+ let mergedProduces = (0, es_toolkit.uniq)((0, es_toolkit.compact)([...usageAsOpenapiv2.produces || [], ...originalRouteInfo.produces || []]));
2810
+ if (mergedProduces.length === 0) mergedProduces = (0, es_toolkit.uniq)((0, es_toolkit.compact)((0, es_toolkit_compat.get)(schemaForProduces, "produces") || []));
2811
+ usageAsOpenapiv2.produces = mergedProduces;
2812
+ if (mergedProduces.length > 0) {
2813
+ if (!originalProducesByRoute[route]) originalProducesByRoute[route] = Object.create(null);
2814
+ originalProducesByRoute[route][methodName] = mergedProduces;
2815
+ }
2782
2816
  }
2783
2817
  for (const originalRouteParam of originalRouteParams) if (!usageRouteParams.find((param) => originalRouteParam.in === param.in && originalRouteParam.name === param.name)) usageRouteParams.push(originalRouteParam);
2784
2818
  }
2785
2819
  }
2820
+ return originalProducesByRoute;
2786
2821
  }
2787
2822
  };
2788
2823
  //#endregion
@@ -3543,4 +3578,4 @@ Object.defineProperty(exports, "version", {
3543
3578
  }
3544
3579
  });
3545
3580
 
3546
- //# sourceMappingURL=src-CIiQTFUj.cjs.map
3581
+ //# sourceMappingURL=src-DEFrSruL.cjs.map