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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger-typescript-api",
3
- "version": "13.10.0",
3
+ "version": "13.11.0",
4
4
  "description": "Generate the API client for Fetch or Axios from an OpenAPI Specification",
5
5
  "homepage": "https://github.com/acacode/swagger-typescript-api",
6
6
  "bugs": "https://github.com/acacode/swagger-typescript-api/issues",
@@ -46,7 +46,7 @@
46
46
  "dependencies": {
47
47
  "@apidevtools/swagger-parser": "12.1.0",
48
48
  "@biomejs/js-api": "4.0.0",
49
- "@biomejs/wasm-nodejs": "2.4.15",
49
+ "@biomejs/wasm-nodejs": "2.4.16",
50
50
  "@types/swagger-schema-official": "^2.0.25",
51
51
  "c12": "^3.3.3",
52
52
  "citty": "^0.2.1",
@@ -63,7 +63,7 @@
63
63
  "yummies": "7.19.4"
64
64
  },
65
65
  "devDependencies": {
66
- "@biomejs/biome": "2.4.15",
66
+ "@biomejs/biome": "2.4.16",
67
67
  "@changesets/changelog-github": "0.7.0",
68
68
  "@changesets/cli": "2.31.0",
69
69
  "@tsconfig/node20": "20.1.9",
@@ -0,0 +1,18 @@
1
+ <%
2
+ const { config } = it;
3
+ const isUnion = config.enumStyle === "union";
4
+
5
+ // camelCase keys (Json, FormData, …) are used by http-client templates as CT.Json, CT.FormData, …
6
+ // They mirror the ContentType property names so the access pattern is identical regardless of enumStyle.
7
+ const v = {
8
+ Json: isUnion ? '"application/json"' : "ContentType.Json",
9
+ JsonApi: isUnion ? '"application/vnd.api+json"' : "ContentType.JsonApi",
10
+ FormData: isUnion ? '"multipart/form-data"' : "ContentType.FormData",
11
+ UrlEncoded: isUnion ? '"application/x-www-form-urlencoded"' : "ContentType.UrlEncoded",
12
+ Text: isUnion ? '"text/plain"' : "ContentType.Text",
13
+ };
14
+
15
+ // UPPER_SNAKE aliases (JSON, FORM_DATA, …) are used by procedure-call templates as
16
+ // requestContentKind["JSON"], matching the CONTENT_KIND constants produced by schema-routes.ts.
17
+ return { ...v, JSON: v.Json, JSON_API: v.JsonApi, FORM_DATA: v.FormData, URL_ENCODED: v.UrlEncoded, TEXT: v.Text };
18
+ %>
@@ -1,5 +1,6 @@
1
1
  <%
2
2
  const { apiConfig, generateResponses, config } = it;
3
+ const CT = includeFile("@base/content-type-accessors", { config });
3
4
  %>
4
5
 
5
6
  import type { AxiosInstance, AxiosRequestConfig, HeadersDefaults, ResponseType, AxiosResponse } from "axios";
@@ -39,6 +40,8 @@ export const ContentType = {
39
40
  Text: "text/plain",
40
41
  } as const;
41
42
  export type ContentType = (typeof ContentType)[keyof typeof ContentType];
43
+ <% } else if (config.enumStyle === "union") { %>
44
+ export type ContentType = "application/json" | "application/vnd.api+json" | "multipart/form-data" | "application/x-www-form-urlencoded" | "text/plain";
42
45
  <% } else { %>
43
46
  export enum ContentType {
44
47
  Json = "application/json",
@@ -127,11 +130,11 @@ export class HttpClient<SecurityDataType = unknown> {
127
130
  const requestParams = this.mergeRequestParams(params, secureParams);
128
131
  const responseFormat = (format || this.format) || undefined;
129
132
 
130
- if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
133
+ if (type === <%~ CT.FormData %> && body && body !== null && typeof body === "object") {
131
134
  body = this.createFormData(body as Record<string, unknown>);
132
135
  }
133
136
 
134
- if (type === ContentType.Text && body && body !== null && typeof body !== "string") {
137
+ if (type === <%~ CT.Text %> && body && body !== null && typeof body !== "string") {
135
138
  body = JSON.stringify(body);
136
139
  }
137
140
 
@@ -1,5 +1,6 @@
1
1
  <%
2
2
  const { apiConfig, generateResponses, config } = it;
3
+ const CT = includeFile("@base/content-type-accessors", { config });
3
4
  %>
4
5
 
5
6
  export type QueryParamsType = Record<string | number, any>;
@@ -50,6 +51,8 @@ export const ContentType = {
50
51
  Text: "text/plain",
51
52
  } as const;
52
53
  export type ContentType = (typeof ContentType)[keyof typeof ContentType];
54
+ <% } else if (config.enumStyle === "union") { %>
55
+ export type ContentType = "application/json" | "application/vnd.api+json" | "multipart/form-data" | "application/x-www-form-urlencoded" | "text/plain";
53
56
  <% } else { %>
54
57
  export enum ContentType {
55
58
  Json = "application/json",
@@ -114,10 +117,10 @@ export class HttpClient<SecurityDataType = unknown> {
114
117
  }
115
118
 
116
119
  private contentFormatters: Record<ContentType, (input: any) => any> = {
117
- [ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
118
- [ContentType.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
119
- [ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
120
- [ContentType.FormData]: (input: any) => {
120
+ [<%~ CT.Json %>]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
121
+ [<%~ CT.JsonApi %>]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
122
+ [<%~ CT.Text %>]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
123
+ [<%~ CT.FormData %>]: (input: any) => {
121
124
  if (input instanceof FormData) {
122
125
  return input;
123
126
  }
@@ -135,7 +138,7 @@ export class HttpClient<SecurityDataType = unknown> {
135
138
  return formData;
136
139
  }, new FormData());
137
140
  },
138
- [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
141
+ [<%~ CT.UrlEncoded %>]: (input: any) => this.toQueryString(input),
139
142
  }
140
143
 
141
144
  protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams {
@@ -192,7 +195,7 @@ export class HttpClient<SecurityDataType = unknown> {
192
195
  const secureParams = ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData)) || {};
193
196
  const requestParams = this.mergeRequestParams(params, secureParams);
194
197
  const queryString = query && this.toQueryString(query);
195
- const payloadFormatter = this.contentFormatters[type || ContentType.Json];
198
+ const payloadFormatter = this.contentFormatters[type || <%~ CT.Json %>];
196
199
  const responseFormat = format || requestParams.format;
197
200
 
198
201
  return this.customFetch(
@@ -201,7 +204,7 @@ export class HttpClient<SecurityDataType = unknown> {
201
204
  ...requestParams,
202
205
  headers: {
203
206
  ...(requestParams.headers || {}),
204
- ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
207
+ ...(type && type !== <%~ CT.FormData %> ? { "Content-Type": type } : {}),
205
208
  },
206
209
  signal: (cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal) || null,
207
210
  body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
@@ -53,13 +53,7 @@ const wrapperArgs = _
53
53
  .join(', ')
54
54
 
55
55
  // RequestParams["type"]
56
- const requestContentKind = {
57
- "JSON": "ContentType.Json",
58
- "JSON_API": "ContentType.JsonApi",
59
- "URL_ENCODED": "ContentType.UrlEncoded",
60
- "FORM_DATA": "ContentType.FormData",
61
- "TEXT": "ContentType.Text",
62
- }
56
+ const requestContentKind = includeFile("@base/content-type-accessors", { config });
63
57
  // RequestParams["format"]
64
58
  const responseContentKind = {
65
59
  "JSON": '"json"',
@@ -8,7 +8,11 @@ const dataContracts = _.map(modelTypes, "name");
8
8
 
9
9
  <% if (config.httpClientType === config.constants.HTTP_CLIENT.AXIOS) { %> import type { AxiosRequestConfig, AxiosResponse } from "axios"; <% } %>
10
10
 
11
+ <% if (config.enumStyle === "union") { %>
12
+ import { HttpClient, RequestParams, type ContentType, HttpResponse } from "./<%~ config.fileNames.httpClient %>";
13
+ <% } else { %>
11
14
  import { HttpClient, RequestParams, ContentType, HttpResponse } from "./<%~ config.fileNames.httpClient %>";
15
+ <% } %>
12
16
  <% if (dataContracts.length) { %>
13
17
  import { <%~ dataContracts.join(", ") %> } from "./<%~ config.fileNames.dataContracts %>"
14
18
  <% } %>
@@ -53,13 +53,7 @@ const wrapperArgs = _
53
53
  .join(', ')
54
54
 
55
55
  // RequestParams["type"]
56
- const requestContentKind = {
57
- "JSON": "ContentType.Json",
58
- "JSON_API": "ContentType.JsonApi",
59
- "URL_ENCODED": "ContentType.UrlEncoded",
60
- "FORM_DATA": "ContentType.FormData",
61
- "TEXT": "ContentType.Text",
62
- }
56
+ const requestContentKind = includeFile("@base/content-type-accessors", { config });
63
57
  // RequestParams["format"]
64
58
  const responseContentKind = {
65
59
  "JSON": '"json"',