swagger-typescript-api 13.4.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.4.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
@@ -500,6 +500,7 @@ var CodeGenConfig = class {
500
500
  string: {
501
501
  $default: this.Ts.Keyword.String,
502
502
  binary: () => this.Ts.Keyword.File,
503
+ byte: () => this.Ts.Keyword.Blob,
503
504
  file: () => this.Ts.Keyword.File,
504
505
  "date-time": () => this.Ts.Keyword.String,
505
506
  time: () => this.Ts.Keyword.String,
@@ -1411,7 +1412,7 @@ var SchemaUtils = class {
1411
1412
  this.schemaComponentsMap = schemaComponentsMap;
1412
1413
  this.typeNameFormatter = typeNameFormatter;
1413
1414
  }
1414
- isBinaryLikeContentMediaType = (contentMediaType) => {
1415
+ isBinaryLikeMimeType = (contentMediaType) => {
1415
1416
  if (typeof contentMediaType !== "string" || !contentMediaType) return false;
1416
1417
  const mediaType = contentMediaType.split(";")[0]?.trim().toLowerCase();
1417
1418
  if (!mediaType) return false;
@@ -1422,10 +1423,13 @@ var SchemaUtils = class {
1422
1423
  */
1423
1424
  if (mediaType.startsWith("text/")) return false;
1424
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";
1425
1428
  if (mediaType.includes("xml") || mediaType.includes("+xml")) return false;
1426
1429
  if (mediaType === "application/x-www-form-urlencoded") return false;
1427
- if (mediaType === "application/javascript" || mediaType === "application/ecmascript" || mediaType === "application/graphql" || mediaType === "application/yaml" || mediaType === "application/x-yaml") return false;
1428
- 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/");
1429
1433
  };
1430
1434
  getRequiredProperties = (schema) => {
1431
1435
  return uniq(schema && Array.isArray(schema.required) && schema.required || []);
@@ -1535,7 +1539,7 @@ var SchemaUtils = class {
1535
1539
  else {
1536
1540
  const primitiveType = this.getSchemaPrimitiveType(schema);
1537
1541
  if (primitiveType == null) return this.config.Ts.Keyword.Any;
1538
- 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]);
1539
1543
  else {
1540
1544
  const typeAlias = get(this.config.primitiveTypes, [primitiveType, schema.format]) || get(this.config.primitiveTypes, [primitiveType, "$default"]) || this.config.primitiveTypes[primitiveType];
1541
1545
  if (typeof typeAlias === "function") resultType = typeAlias(schema, this);
@@ -1797,6 +1801,8 @@ var SchemaRoutes = class {
1797
1801
  if (contentTypes.some((contentType) => contentType.startsWith("text/"))) return CONTENT_KIND.TEXT;
1798
1802
  return CONTENT_KIND.OTHER;
1799
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));
1800
1806
  isSuccessStatus = (status) => this.config.defaultResponseAsSuccess && status === "default" || +status >= this.config.successResponseStatusRange[0] && +status <= this.config.successResponseStatusRange[1] || status === "2xx";
1801
1807
  getSchemaFromRequestType = (requestInfo) => {
1802
1808
  const content = get(requestInfo, "content");
@@ -1876,9 +1882,18 @@ var SchemaRoutes = class {
1876
1882
  return acc;
1877
1883
  }, []);
1878
1884
  };
1879
- getResponseBodyInfo = (routeInfo, parsedSchemas, resolvedSwaggerSchema) => {
1885
+ getResponseBodyInfo = (routeInfo, parsedSchemas, resolvedSwaggerSchema, pathName, method) => {
1880
1886
  const { produces, operationId, responses } = routeInfo;
1881
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;
1882
1897
  const responseInfos = this.getRequestInfoTypes({
1883
1898
  requestInfos: responses,
1884
1899
  parsedSchemas,
@@ -1896,13 +1911,14 @@ var SchemaRoutes = class {
1896
1911
  }));
1897
1912
  return `headers: { ${Object.entries(headerTypes).map(([k, v]) => `"${k}": ${v}`).join(",")} },`;
1898
1913
  };
1914
+ const typesToCheck = (Array.isArray(originalProduces) && originalProduces.length > 0 ? originalProduces : null) ?? (produces?.length ? produces : null) ?? (successContentTypes?.length ? successContentTypes : null) ?? contentTypes;
1899
1915
  return {
1900
1916
  contentTypes,
1901
1917
  responses: responseInfos,
1902
1918
  links,
1903
1919
  success: {
1904
1920
  schema: successResponse,
1905
- 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
1906
1922
  },
1907
1923
  error: {
1908
1924
  schemas: errorResponses,
@@ -2102,7 +2118,7 @@ var SchemaRoutes = class {
2102
2118
  description: pathArgSchema.description
2103
2119
  }));
2104
2120
  const pathArgsNames = pathArgs.map((arg) => arg.name);
2105
- const responseBodyInfo = this.getResponseBodyInfo(routeInfo, parsedSchemas, resolvedSwaggerSchema);
2121
+ const responseBodyInfo = this.getResponseBodyInfo(routeInfo, parsedSchemas, resolvedSwaggerSchema, rawRouteName, method);
2106
2122
  const rawRouteInfo = {
2107
2123
  ...otherInfo,
2108
2124
  pathArgs,
@@ -2402,13 +2418,18 @@ var ResolvedSwaggerSchema = class ResolvedSwaggerSchema {
2402
2418
  if (resolved == null) return null;
2403
2419
  return this.absolutizeLocalRefs(resolved, this.stripHash(externalPath));
2404
2420
  }
2405
- constructor(config, usageSchema, originalSchema, resolvers) {
2421
+ originalProducesByRoute = Object.create(null);
2422
+ constructor(config, usageSchema, originalSchema, resolvers, originalProducesByRoute) {
2406
2423
  this.config = config;
2407
2424
  this.usageSchema = usageSchema;
2408
2425
  this.originalSchema = originalSchema;
2409
2426
  this.resolvers = resolvers;
2410
2427
  this.usageSchema = usageSchema;
2411
2428
  this.originalSchema = originalSchema;
2429
+ if (originalProducesByRoute) this.originalProducesByRoute = originalProducesByRoute;
2430
+ }
2431
+ getOriginalProduces(pathName, method) {
2432
+ return this.originalProducesByRoute[pathName]?.[method];
2412
2433
  }
2413
2434
  getRefDetails(ref) {
2414
2435
  const normalizedRef = this.normalizeRef(ref);
@@ -2576,7 +2597,7 @@ var ResolvedSwaggerSchema = class ResolvedSwaggerSchema {
2576
2597
  }
2577
2598
  return null;
2578
2599
  }
2579
- static async create(config, usageSchema, originalSchema) {
2600
+ static async create(config, usageSchema, originalSchema, originalProducesByRoute) {
2580
2601
  const resolvers = [];
2581
2602
  const options = {
2582
2603
  continueOnError: true,
@@ -2609,7 +2630,7 @@ var ResolvedSwaggerSchema = class ResolvedSwaggerSchema {
2609
2630
  } catch (e) {
2610
2631
  consola.debug(e);
2611
2632
  }
2612
- const resolvedSwaggerSchema = new ResolvedSwaggerSchema(config, usageSchema, originalSchema, resolvers);
2633
+ const resolvedSwaggerSchema = new ResolvedSwaggerSchema(config, usageSchema, originalSchema, resolvers, originalProducesByRoute);
2613
2634
  await resolvedSwaggerSchema.warmUpRemoteSchemasCache();
2614
2635
  return resolvedSwaggerSchema;
2615
2636
  }
@@ -2654,12 +2675,13 @@ var SwaggerSchemaResolver = class {
2654
2675
  const swaggerSchemaObject = this.processSwaggerSchemaFile(swaggerSchemaFile);
2655
2676
  swaggerSchemas = await this.convertSwaggerObject(swaggerSchemaObject, { patch });
2656
2677
  }
2657
- this.fixSwaggerSchemas(swaggerSchemas);
2658
- 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);
2659
2680
  }
2660
2681
  convertSwaggerObject(swaggerSchema, converterOptions) {
2661
2682
  return new Promise((resolve) => {
2662
2683
  const result = structuredClone(swaggerSchema);
2684
+ const originalSchemaForProduces = structuredClone(swaggerSchema);
2663
2685
  result.info = merge({
2664
2686
  title: "No title",
2665
2687
  version: ""
@@ -2678,12 +2700,14 @@ var SwaggerSchemaResolver = class {
2678
2700
  this.config.update({ convertedFromSwagger2: true });
2679
2701
  resolve({
2680
2702
  usageSchema: parsedSwaggerSchema,
2681
- originalSchema: result
2703
+ originalSchema: result,
2704
+ originalSchemaForProduces
2682
2705
  });
2683
2706
  });
2684
2707
  } else resolve({
2685
2708
  usageSchema: result,
2686
- originalSchema: structuredClone(result)
2709
+ originalSchema: structuredClone(result),
2710
+ originalSchemaForProduces
2687
2711
  });
2688
2712
  });
2689
2713
  }
@@ -2724,13 +2748,17 @@ var SwaggerSchemaResolver = class {
2724
2748
  if (typeof objectSchema.$ref === "string") objectSchema.$ref = this.normalizeRefValue(objectSchema.$ref);
2725
2749
  for (const value of Object.values(objectSchema)) this.normalizeRefsInSchema(value);
2726
2750
  }
2727
- fixSwaggerSchemas({ usageSchema, originalSchema }) {
2751
+ fixSwaggerSchemas({ usageSchema, originalSchema, originalSchemaForProduces }) {
2728
2752
  this.normalizeRefsInSchema(usageSchema);
2729
2753
  this.normalizeRefsInSchema(originalSchema);
2730
2754
  const usagePaths = get(usageSchema, "paths") || {};
2731
- 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);
2732
2759
  for (const [route, usagePathObject] of Object.entries(usagePaths)) {
2733
- 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}`) || {};
2734
2762
  for (const [methodName, usageRouteInfo] of Object.entries(usagePathObject)) {
2735
2763
  const originalRouteInfo = get(originalPathObject, methodName) || {};
2736
2764
  const usageRouteParams = get(usageRouteInfo, "parameters") || [];
@@ -2738,11 +2766,18 @@ var SwaggerSchemaResolver = class {
2738
2766
  const usageAsOpenapiv2 = usageRouteInfo;
2739
2767
  if (typeof usageRouteInfo === "object") {
2740
2768
  usageAsOpenapiv2.consumes = uniq(compact([...usageAsOpenapiv2.consumes || [], ...originalRouteInfo.consumes || []]));
2741
- 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
+ }
2742
2776
  }
2743
2777
  for (const originalRouteParam of originalRouteParams) if (!usageRouteParams.find((param) => originalRouteParam.in === param.in && originalRouteParam.name === param.name)) usageRouteParams.push(originalRouteParam);
2744
2778
  }
2745
2779
  }
2780
+ return originalProducesByRoute;
2746
2781
  }
2747
2782
  };
2748
2783
  //#endregion
@@ -3432,4 +3467,4 @@ async function generateApi(config) {
3432
3467
  //#endregion
3433
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 };
3434
3469
 
3435
- //# sourceMappingURL=src-BPFXd2rf.mjs.map
3470
+ //# sourceMappingURL=src-sIP2rLyu.mjs.map