Skip to content

Commit 40efa18

Browse files
committed
fix: --union-enums option names fix to --generate-union-enums
1 parent 868bdae commit 40efa18

File tree

4 files changed

+245
-7
lines changed

4 files changed

+245
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Options:
2626
some swagger schemas use "default" response status code as success response type by default. (default: false)
2727
-r, --responses generate additional information about request responses
2828
also add typings for bad responses (default: false)
29-
--union-enums generate all "enum" types as union types (T1 | T2 | TN) (default: false)
29+
--generate-union-enums generate all "enum" types as union types (T1 | T2 | TN) (default: false)
3030
--add-readonly generate readonly properties (default: false)
3131
--route-types generate type definitions for API routes (default: false)
3232
--[no-]client generate an API class (default: true)

index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,6 @@ const generateCommand = defineCommand({
281281
description: "data contract name suffix",
282282
default: codeGenBaseConfig.typeSuffix,
283283
},
284-
"union-enums": {
285-
type: "boolean",
286-
description: 'generate all "enum" types as union types (T1 | T2 | TN)',
287-
default: codeGenBaseConfig.generateUnionEnums,
288-
},
289284
"unwrap-response-data": {
290285
type: "boolean",
291286
description: "unwrap the data item from the response",

tests/spec/unionEnums/__snapshots__/basic.test.ts.snap

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,248 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`basic > --generate-union-enums 1`] = `
4+
"/* eslint-disable */
5+
/* tslint:disable */
6+
// @ts-nocheck
7+
/*
8+
* ---------------------------------------------------------------
9+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
10+
* ## ##
11+
* ## AUTHOR: acacode ##
12+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
13+
* ---------------------------------------------------------------
14+
*/
15+
16+
export type StringEnum = "String1" | "String2" | "String3" | "String4";
17+
18+
export type NumberEnum = 1 | 2 | 3 | 4;
19+
20+
export type BooleanEnum = true | false;
21+
22+
/**
23+
* FooBar
24+
* @format int32
25+
*/
26+
export type IntEnumWithNames = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
27+
28+
export type QueryParamsType = Record<string | number, any>;
29+
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
30+
31+
export interface FullRequestParams extends Omit<RequestInit, "body"> {
32+
/** set parameter to \`true\` for call \`securityWorker\` for this request */
33+
secure?: boolean;
34+
/** request path */
35+
path: string;
36+
/** content type of request body */
37+
type?: ContentType;
38+
/** query params */
39+
query?: QueryParamsType;
40+
/** format of response (i.e. response.json() -> format: "json") */
41+
format?: ResponseFormat;
42+
/** request body */
43+
body?: unknown;
44+
/** base url */
45+
baseUrl?: string;
46+
/** request cancellation token */
47+
cancelToken?: CancelToken;
48+
}
49+
50+
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
51+
52+
export interface ApiConfig<SecurityDataType = unknown> {
53+
baseUrl?: string;
54+
baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">;
55+
securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void;
56+
customFetch?: typeof fetch;
57+
}
58+
59+
export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
60+
data: D;
61+
error: E;
62+
}
63+
64+
type CancelToken = Symbol | string | number;
65+
66+
export enum ContentType {
67+
Json = "application/json",
68+
FormData = "multipart/form-data",
69+
UrlEncoded = "application/x-www-form-urlencoded",
70+
Text = "text/plain",
71+
}
72+
73+
export class HttpClient<SecurityDataType = unknown> {
74+
public baseUrl: string = "http://localhost:8080/api/v1";
75+
private securityData: SecurityDataType | null = null;
76+
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
77+
private abortControllers = new Map<CancelToken, AbortController>();
78+
private customFetch = (...fetchParams: Parameters<typeof fetch>) => fetch(...fetchParams);
79+
80+
private baseApiParams: RequestParams = {
81+
credentials: "same-origin",
82+
headers: {},
83+
redirect: "follow",
84+
referrerPolicy: "no-referrer",
85+
};
86+
87+
constructor(apiConfig: ApiConfig<SecurityDataType> = {}) {
88+
Object.assign(this, apiConfig);
89+
}
90+
91+
public setSecurityData = (data: SecurityDataType | null) => {
92+
this.securityData = data;
93+
};
94+
95+
protected encodeQueryParam(key: string, value: any) {
96+
const encodedKey = encodeURIComponent(key);
97+
return \`\${encodedKey}=\${encodeURIComponent(typeof value === "number" ? value : \`\${value}\`)}\`;
98+
}
99+
100+
protected addQueryParam(query: QueryParamsType, key: string) {
101+
return this.encodeQueryParam(key, query[key]);
102+
}
103+
104+
protected addArrayQueryParam(query: QueryParamsType, key: string) {
105+
const value = query[key];
106+
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
107+
}
108+
109+
protected toQueryString(rawQuery?: QueryParamsType): string {
110+
const query = rawQuery || {};
111+
const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]);
112+
return keys
113+
.map((key) => (Array.isArray(query[key]) ? this.addArrayQueryParam(query, key) : this.addQueryParam(query, key)))
114+
.join("&");
115+
}
116+
117+
protected addQueryParams(rawQuery?: QueryParamsType): string {
118+
const queryString = this.toQueryString(rawQuery);
119+
return queryString ? \`?\${queryString}\` : "";
120+
}
121+
122+
private contentFormatters: Record<ContentType, (input: any) => any> = {
123+
[ContentType.Json]: (input: any) =>
124+
input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
125+
[ContentType.Text]: (input: any) => (input !== null && typeof input !== "string" ? JSON.stringify(input) : input),
126+
[ContentType.FormData]: (input: any) =>
127+
Object.keys(input || {}).reduce((formData, key) => {
128+
const property = input[key];
129+
formData.append(
130+
key,
131+
property instanceof Blob
132+
? property
133+
: typeof property === "object" && property !== null
134+
? JSON.stringify(property)
135+
: \`\${property}\`,
136+
);
137+
return formData;
138+
}, new FormData()),
139+
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
140+
};
141+
142+
protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams {
143+
return {
144+
...this.baseApiParams,
145+
...params1,
146+
...(params2 || {}),
147+
headers: {
148+
...(this.baseApiParams.headers || {}),
149+
...(params1.headers || {}),
150+
...((params2 && params2.headers) || {}),
151+
},
152+
};
153+
}
154+
155+
protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => {
156+
if (this.abortControllers.has(cancelToken)) {
157+
const abortController = this.abortControllers.get(cancelToken);
158+
if (abortController) {
159+
return abortController.signal;
160+
}
161+
return void 0;
162+
}
163+
164+
const abortController = new AbortController();
165+
this.abortControllers.set(cancelToken, abortController);
166+
return abortController.signal;
167+
};
168+
169+
public abortRequest = (cancelToken: CancelToken) => {
170+
const abortController = this.abortControllers.get(cancelToken);
171+
172+
if (abortController) {
173+
abortController.abort();
174+
this.abortControllers.delete(cancelToken);
175+
}
176+
};
177+
178+
public request = async <T = any, E = any>({
179+
body,
180+
secure,
181+
path,
182+
type,
183+
query,
184+
format,
185+
baseUrl,
186+
cancelToken,
187+
...params
188+
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
189+
const secureParams =
190+
((typeof secure === "boolean" ? secure : this.baseApiParams.secure) &&
191+
this.securityWorker &&
192+
(await this.securityWorker(this.securityData))) ||
193+
{};
194+
const requestParams = this.mergeRequestParams(params, secureParams);
195+
const queryString = query && this.toQueryString(query);
196+
const payloadFormatter = this.contentFormatters[type || ContentType.Json];
197+
const responseFormat = format || requestParams.format;
198+
199+
return this.customFetch(\`\${baseUrl || this.baseUrl || ""}\${path}\${queryString ? \`?\${queryString}\` : ""}\`, {
200+
...requestParams,
201+
headers: {
202+
...(requestParams.headers || {}),
203+
...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
204+
},
205+
signal: (cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal) || null,
206+
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
207+
}).then(async (response) => {
208+
const r = response.clone() as HttpResponse<T, E>;
209+
r.data = null as unknown as T;
210+
r.error = null as unknown as E;
211+
212+
const data = !responseFormat
213+
? r
214+
: await response[responseFormat]()
215+
.then((data) => {
216+
if (r.ok) {
217+
r.data = data;
218+
} else {
219+
r.error = data;
220+
}
221+
return r;
222+
})
223+
.catch((e) => {
224+
r.error = e;
225+
return r;
226+
});
227+
228+
if (cancelToken) {
229+
this.abortControllers.delete(cancelToken);
230+
}
231+
232+
if (!response.ok) throw data;
233+
return data;
234+
});
235+
};
236+
}
237+
238+
/**
239+
* @title No title
240+
* @baseUrl http://localhost:8080/api/v1
241+
*/
242+
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {}
243+
"
244+
`;
245+
3246
exports[`basic > --union-enums 1`] = `
4247
"/* eslint-disable */
5248
/* tslint:disable */

tests/spec/unionEnums/basic.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("basic", async () => {
1717
await fs.rm(tmpdir, { recursive: true });
1818
});
1919

20-
test("--union-enums", async () => {
20+
test("--generate-union-enums", async () => {
2121
await generateApi({
2222
fileName: "schema",
2323
input: path.resolve(import.meta.dirname, "schema.json"),

0 commit comments

Comments
 (0)