swagger-typescript-api 13.5.0 → 13.6.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.
@@ -174,7 +174,7 @@ var ComponentTypeNameResolver = class extends NameResolver {
174
174
  //#endregion
175
175
  //#region package.json
176
176
  var name = "swagger-typescript-api";
177
- var version = "13.5.0";
177
+ var version = "13.6.0";
178
178
  var description = "Generate the API client for Fetch or Axios from an OpenAPI Specification";
179
179
  //#endregion
180
180
  //#region src/constants.ts
@@ -1412,7 +1412,7 @@ var SchemaUtils = class {
1412
1412
  this.schemaComponentsMap = schemaComponentsMap;
1413
1413
  this.typeNameFormatter = typeNameFormatter;
1414
1414
  }
1415
- isBinaryLikeContentMediaType = (contentMediaType) => {
1415
+ isBinaryLikeMimeType = (contentMediaType) => {
1416
1416
  if (typeof contentMediaType !== "string" || !contentMediaType) return false;
1417
1417
  const mediaType = contentMediaType.split(";")[0]?.trim().toLowerCase();
1418
1418
  if (!mediaType) return false;
@@ -1423,10 +1423,13 @@ var SchemaUtils = class {
1423
1423
  */
1424
1424
  if (mediaType.startsWith("text/")) return false;
1425
1425
  if (mediaType.includes("json") || mediaType.includes("+json")) return false;
1426
+ /** application/vnd.* binary types first: names like "openxmlformats" / "spreadsheetml" contain "xml" but are binary. */
1427
+ 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";
1426
1428
  if (mediaType.includes("xml") || mediaType.includes("+xml")) return false;
1427
1429
  if (mediaType === "application/x-www-form-urlencoded") return false;
1428
- if (mediaType === "application/javascript" || mediaType === "application/ecmascript" || mediaType === "application/graphql" || mediaType === "application/yaml" || mediaType === "application/x-yaml") return false;
1429
- return mediaType === "application/octet-stream" || mediaType.startsWith("image/") || mediaType.startsWith("audio/") || mediaType.startsWith("video/") || mediaType.startsWith("font/") || mediaType.startsWith("model/") || mediaType.startsWith("application/");
1430
+ if (mediaType === "application/javascript" || mediaType === "application/ecmascript" || mediaType === "application/graphql" || mediaType === "application/yaml" || mediaType === "application/x-yaml" || mediaType === "application/jwt") return false;
1431
+ 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";
1432
+ return mediaType.startsWith("image/") || mediaType.startsWith("audio/") || mediaType.startsWith("video/") || mediaType.startsWith("font/") || mediaType.startsWith("model/");
1430
1433
  };
1431
1434
  getRequiredProperties = (schema) => {
1432
1435
  return uniq(schema && Array.isArray(schema.required) && schema.required || []);
@@ -1536,7 +1539,7 @@ var SchemaUtils = class {
1536
1539
  else {
1537
1540
  const primitiveType = this.getSchemaPrimitiveType(schema);
1538
1541
  if (primitiveType == null) return this.config.Ts.Keyword.Any;
1539
- 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]);
1542
+ 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]);
1540
1543
  else {
1541
1544
  const typeAlias = get(this.config.primitiveTypes, [primitiveType, schema.format]) || get(this.config.primitiveTypes, [primitiveType, "$default"]) || this.config.primitiveTypes[primitiveType];
1542
1545
  if (typeof typeAlias === "function") resultType = typeAlias(schema, this);
@@ -1798,6 +1801,8 @@ var SchemaRoutes = class {
1798
1801
  if (contentTypes.some((contentType) => contentType.startsWith("text/"))) return CONTENT_KIND.TEXT;
1799
1802
  return CONTENT_KIND.OTHER;
1800
1803
  };
1804
+ /** True when response produces only binary media types (e.g. file download). */
1805
+ isBinaryOnlyContentTypes = (contentTypes) => !!contentTypes?.length && contentTypes.every((ct) => this.schemaUtils.isBinaryLikeMimeType(ct));
1801
1806
  isSuccessStatus = (status) => this.config.defaultResponseAsSuccess && status === "default" || +status >= this.config.successResponseStatusRange[0] && +status <= this.config.successResponseStatusRange[1] || status === "2xx";
1802
1807
  getSchemaFromRequestType = (requestInfo) => {
1803
1808
  const content = get(requestInfo, "content");
@@ -1877,9 +1882,18 @@ var SchemaRoutes = class {
1877
1882
  return acc;
1878
1883
  }, []);
1879
1884
  };
1880
- getResponseBodyInfo = (routeInfo, parsedSchemas, resolvedSwaggerSchema) => {
1885
+ getResponseBodyInfo = (routeInfo, parsedSchemas, resolvedSwaggerSchema, pathName, method) => {
1881
1886
  const { produces, operationId, responses } = routeInfo;
1882
1887
  const contentTypes = this.getContentTypes(responses, [...produces || [], routeInfo["x-accepts"]]);
1888
+ const successStatus = Object.keys(responses || {}).find((s) => this.isSuccessStatus(s));
1889
+ const successResponseContent = successStatus && responses?.[successStatus];
1890
+ const successContentTypes = successResponseContent?.content && typeof successResponseContent.content === "object" ? Object.keys(successResponseContent.content) : null;
1891
+ const originalProduces = pathName && method ? resolvedSwaggerSchema.getOriginalProduces(pathName, method) ?? get(resolvedSwaggerSchema.originalSchema, [
1892
+ "paths",
1893
+ pathName,
1894
+ method,
1895
+ "produces"
1896
+ ]) : void 0;
1883
1897
  const responseInfos = this.getRequestInfoTypes({
1884
1898
  requestInfos: responses,
1885
1899
  parsedSchemas,
@@ -1897,13 +1911,14 @@ var SchemaRoutes = class {
1897
1911
  }));
1898
1912
  return `headers: { ${Object.entries(headerTypes).map(([k, v]) => `"${k}": ${v}`).join(",")} },`;
1899
1913
  };
1914
+ const typesToCheck = (Array.isArray(originalProduces) && originalProduces.length > 0 ? originalProduces : null) ?? (produces?.length ? produces : null) ?? (successContentTypes?.length ? successContentTypes : null) ?? contentTypes;
1900
1915
  return {
1901
1916
  contentTypes,
1902
1917
  responses: responseInfos,
1903
1918
  links,
1904
1919
  success: {
1905
1920
  schema: successResponse,
1906
- type: successResponse?.type || this.config.Ts.Keyword.Any
1921
+ type: this.isBinaryOnlyContentTypes(typesToCheck) ? this.config.Ts.Keyword.Blob : successResponse?.type || this.config.Ts.Keyword.Any
1907
1922
  },
1908
1923
  error: {
1909
1924
  schemas: errorResponses,
@@ -2103,7 +2118,7 @@ var SchemaRoutes = class {
2103
2118
  description: pathArgSchema.description
2104
2119
  }));
2105
2120
  const pathArgsNames = pathArgs.map((arg) => arg.name);
2106
- const responseBodyInfo = this.getResponseBodyInfo(routeInfo, parsedSchemas, resolvedSwaggerSchema);
2121
+ const responseBodyInfo = this.getResponseBodyInfo(routeInfo, parsedSchemas, resolvedSwaggerSchema, rawRouteName, method);
2107
2122
  const rawRouteInfo = {
2108
2123
  ...otherInfo,
2109
2124
  pathArgs,
@@ -2403,13 +2418,18 @@ var ResolvedSwaggerSchema = class ResolvedSwaggerSchema {
2403
2418
  if (resolved == null) return null;
2404
2419
  return this.absolutizeLocalRefs(resolved, this.stripHash(externalPath));
2405
2420
  }
2406
- constructor(config, usageSchema, originalSchema, resolvers) {
2421
+ originalProducesByRoute = Object.create(null);
2422
+ constructor(config, usageSchema, originalSchema, resolvers, originalProducesByRoute) {
2407
2423
  this.config = config;
2408
2424
  this.usageSchema = usageSchema;
2409
2425
  this.originalSchema = originalSchema;
2410
2426
  this.resolvers = resolvers;
2411
2427
  this.usageSchema = usageSchema;
2412
2428
  this.originalSchema = originalSchema;
2429
+ if (originalProducesByRoute) this.originalProducesByRoute = originalProducesByRoute;
2430
+ }
2431
+ getOriginalProduces(pathName, method) {
2432
+ return this.originalProducesByRoute[pathName]?.[method];
2413
2433
  }
2414
2434
  getRefDetails(ref) {
2415
2435
  const normalizedRef = this.normalizeRef(ref);
@@ -2577,7 +2597,7 @@ var ResolvedSwaggerSchema = class ResolvedSwaggerSchema {
2577
2597
  }
2578
2598
  return null;
2579
2599
  }
2580
- static async create(config, usageSchema, originalSchema) {
2600
+ static async create(config, usageSchema, originalSchema, originalProducesByRoute) {
2581
2601
  const resolvers = [];
2582
2602
  const options = {
2583
2603
  continueOnError: true,
@@ -2610,7 +2630,7 @@ var ResolvedSwaggerSchema = class ResolvedSwaggerSchema {
2610
2630
  } catch (e) {
2611
2631
  consola.debug(e);
2612
2632
  }
2613
- const resolvedSwaggerSchema = new ResolvedSwaggerSchema(config, usageSchema, originalSchema, resolvers);
2633
+ const resolvedSwaggerSchema = new ResolvedSwaggerSchema(config, usageSchema, originalSchema, resolvers, originalProducesByRoute);
2614
2634
  await resolvedSwaggerSchema.warmUpRemoteSchemasCache();
2615
2635
  return resolvedSwaggerSchema;
2616
2636
  }
@@ -2655,12 +2675,13 @@ var SwaggerSchemaResolver = class {
2655
2675
  const swaggerSchemaObject = this.processSwaggerSchemaFile(swaggerSchemaFile);
2656
2676
  swaggerSchemas = await this.convertSwaggerObject(swaggerSchemaObject, { patch });
2657
2677
  }
2658
- this.fixSwaggerSchemas(swaggerSchemas);
2659
- return ResolvedSwaggerSchema.create(this.config, swaggerSchemas.usageSchema, swaggerSchemas.originalSchema);
2678
+ const originalProducesByRoute = this.fixSwaggerSchemas(swaggerSchemas);
2679
+ return await ResolvedSwaggerSchema.create(this.config, swaggerSchemas.usageSchema, swaggerSchemas.originalSchema, originalProducesByRoute);
2660
2680
  }
2661
2681
  convertSwaggerObject(swaggerSchema, converterOptions) {
2662
2682
  return new Promise((resolve) => {
2663
2683
  const result = structuredClone(swaggerSchema);
2684
+ const originalSchemaForProduces = structuredClone(swaggerSchema);
2664
2685
  result.info = merge({
2665
2686
  title: "No title",
2666
2687
  version: ""
@@ -2679,12 +2700,14 @@ var SwaggerSchemaResolver = class {
2679
2700
  this.config.update({ convertedFromSwagger2: true });
2680
2701
  resolve({
2681
2702
  usageSchema: parsedSwaggerSchema,
2682
- originalSchema: result
2703
+ originalSchema: result,
2704
+ originalSchemaForProduces
2683
2705
  });
2684
2706
  });
2685
2707
  } else resolve({
2686
2708
  usageSchema: result,
2687
- originalSchema: structuredClone(result)
2709
+ originalSchema: structuredClone(result),
2710
+ originalSchemaForProduces
2688
2711
  });
2689
2712
  });
2690
2713
  }
@@ -2725,13 +2748,17 @@ var SwaggerSchemaResolver = class {
2725
2748
  if (typeof objectSchema.$ref === "string") objectSchema.$ref = this.normalizeRefValue(objectSchema.$ref);
2726
2749
  for (const value of Object.values(objectSchema)) this.normalizeRefsInSchema(value);
2727
2750
  }
2728
- fixSwaggerSchemas({ usageSchema, originalSchema }) {
2751
+ fixSwaggerSchemas({ usageSchema, originalSchema, originalSchemaForProduces }) {
2729
2752
  this.normalizeRefsInSchema(usageSchema);
2730
2753
  this.normalizeRefsInSchema(originalSchema);
2731
2754
  const usagePaths = get(usageSchema, "paths") || {};
2732
- const originalPaths = get(originalSchema, "paths") || {};
2755
+ const schemaForProduces = originalSchemaForProduces ?? originalSchema;
2756
+ const originalPaths = get(schemaForProduces, "paths") || {};
2757
+ const basePath = schemaForProduces.basePath?.replace(/\/$/, "") || "";
2758
+ const originalProducesByRoute = Object.create(null);
2733
2759
  for (const [route, usagePathObject] of Object.entries(usagePaths)) {
2734
- const originalPathObject = get(originalPaths, route) || {};
2760
+ const routeWithoutBase = basePath && route.startsWith(basePath) ? route.slice(basePath.length) || "/" : route;
2761
+ const originalPathObject = get(originalPaths, route) || get(originalPaths, routeWithoutBase.startsWith("/") ? routeWithoutBase : `/${routeWithoutBase}`) || {};
2735
2762
  for (const [methodName, usageRouteInfo] of Object.entries(usagePathObject)) {
2736
2763
  const originalRouteInfo = get(originalPathObject, methodName) || {};
2737
2764
  const usageRouteParams = get(usageRouteInfo, "parameters") || [];
@@ -2739,11 +2766,18 @@ var SwaggerSchemaResolver = class {
2739
2766
  const usageAsOpenapiv2 = usageRouteInfo;
2740
2767
  if (typeof usageRouteInfo === "object") {
2741
2768
  usageAsOpenapiv2.consumes = uniq(compact([...usageAsOpenapiv2.consumes || [], ...originalRouteInfo.consumes || []]));
2742
- usageAsOpenapiv2.produces = uniq(compact([...usageAsOpenapiv2.produces || [], ...originalRouteInfo.produces || []]));
2769
+ let mergedProduces = uniq(compact([...usageAsOpenapiv2.produces || [], ...originalRouteInfo.produces || []]));
2770
+ if (mergedProduces.length === 0) mergedProduces = uniq(compact(get(schemaForProduces, "produces") || []));
2771
+ usageAsOpenapiv2.produces = mergedProduces;
2772
+ if (mergedProduces.length > 0) {
2773
+ if (!originalProducesByRoute[route]) originalProducesByRoute[route] = Object.create(null);
2774
+ originalProducesByRoute[route][methodName] = mergedProduces;
2775
+ }
2743
2776
  }
2744
2777
  for (const originalRouteParam of originalRouteParams) if (!usageRouteParams.find((param) => originalRouteParam.in === param.in && originalRouteParam.name === param.name)) usageRouteParams.push(originalRouteParam);
2745
2778
  }
2746
2779
  }
2780
+ return originalProducesByRoute;
2747
2781
  }
2748
2782
  };
2749
2783
  //#endregion
@@ -3433,4 +3467,4 @@ async function generateApi(config) {
3433
3467
  //#endregion
3434
3468
  export { SCHEMA_TYPES as a, constants_exports as c, version as d, RequestContentKind as i, description as l, generateTemplates as n, CodeGenConfig as o, TemplatesGenConfig as r, HTTP_CLIENT as s, generateApi as t, name as u };
3435
3469
 
3436
- //# sourceMappingURL=src-eXqnNGnl.mjs.map
3470
+ //# sourceMappingURL=src-sIP2rLyu.mjs.map