Skip to content

Commit e7cfb60

Browse files
committed
Remove indirection between API methods and native fetch method in generated code
1 parent b379601 commit e7cfb60

File tree

13 files changed

+211
-506
lines changed

13 files changed

+211
-506
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
This is a plugin to [OpenApi Generator](https://openapi-generator.tech/) which generates Typescript client libraries from openapi specifications using the native `fetch` method. The generated code has no dependencies.
1616

17-
The output contains both interfaces and classes for each `tag` in the input specification, for example:
17+
The output contains both interfaces and classes for each `tag` in the input specification, [for example](https://github.com/jhannes/openapi-generator-typescript-fetch-api/blob/main/snapshotTests/snapshot/petstore/api.ts):
1818

1919
```typescript
2020
/**
@@ -48,12 +48,15 @@ export class PetApi extends BaseAPI implements PetApiInterface {
4848
petDto?: PetDto;
4949
security: petstore_auth;
5050
}): Promise<void> {
51-
return await this.POST(
51+
return await this.fetch(
5252
this.basePath + "/pet",
53-
JSON.stringify(params.petDto),
5453
{
55-
...params.security?.headers(),
56-
"Content-Type": "application/json",
54+
method: "POST",
55+
body: JSON.stringify(params.petDto),
56+
headers: {
57+
...params.security?.headers(),
58+
"Content-Type": "application/json",
59+
}
5760
}
5861
);
5962
}
@@ -78,7 +81,7 @@ try {
7881

7982
### Generated test support code
8083

81-
To facilitate testing, the code ships which interface test stubs and test generators. For example:
84+
To facilitate testing, the code ships which interface test stubs and test generators. [For example](https://github.com/jhannes/openapi-generator-typescript-fetch-api/blob/main/snapshotTests/snapshot/petstore/test/modelTest.ts):
8285

8386
```typescript
8487
export function mockPetApi(operations: {

snapshotTests/snapshot/example/api.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,14 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
7171
pathParams: { storeId: string };
7272
petDto?: PetDto;
7373
}): Promise<void> {
74-
return await this.POST(
74+
return await this.fetch(
7575
this.url("/{storeId}/pets", params.pathParams),
76-
JSON.stringify(params.petDto),
7776
{
78-
"Content-Type": "application/json",
77+
method: "POST",
78+
body: JSON.stringify(params.petDto),
79+
headers: {
80+
"Content-Type": "application/json",
81+
}
7982
}
8083
);
8184
}
@@ -88,11 +91,14 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
8891
pathParams: { petId: string };
8992
formParams: { name: string; status: string; }
9093
}): Promise<void> {
91-
return await this.POST(
94+
return await this.fetch(
9295
this.url("/pets/{petId}", params.pathParams),
93-
this.formData(params.formParams),
9496
{
95-
"Content-Type": "application/x-www-form-urlencoded",
97+
method: "POST",
98+
body: this.formData(params.formParams),
99+
headers: {
100+
"Content-Type": "application/x-www-form-urlencoded",
101+
}
96102
}
97103
);
98104
}
@@ -105,12 +111,10 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
105111
pathParams: { storeId: string };
106112
queryParams?: { status?: Array<string>, tags?: Array<string>, bornAfter?: Date, };
107113
}): Promise<PetDto> {
108-
return await this.GET(
114+
return await this.fetch(
109115
this.url("/{storeId}/pets", params.pathParams, params?.queryParams, {
110116
status: { delimiter: " " },
111-
}),
112-
undefined,
113-
{}
117+
})
114118
);
115119
}
116120
}

snapshotTests/snapshot/example/base.ts

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,63 +21,7 @@ export class BaseAPI {
2121
mode?: RequestMode
2222
} | undefined) {}
2323

24-
protected async GET(
25-
url: string,
26-
requestBody: undefined,
27-
headers: Record<string, string|undefined> = {}
28-
): Promise<any> {
29-
return await this.fetch(url, { headers: this.removeEmpty(headers) });
30-
}
31-
32-
protected async PUT(
33-
url: string,
34-
body?: BodyInit,
35-
headers: Record<string, string|undefined> = {}
36-
): Promise<any> {
37-
return await this.fetch(url, {
38-
method: "PUT",
39-
headers: this.removeEmpty(headers),
40-
body,
41-
});
42-
}
43-
44-
protected async POST(
45-
url: string,
46-
body?: BodyInit,
47-
headers: Record<string, string|undefined> = {}
48-
): Promise<any> {
49-
return await this.fetch(url, {
50-
method: "POST",
51-
headers: this.removeEmpty(headers),
52-
body,
53-
});
54-
}
55-
56-
protected async PATCH(
57-
url: string,
58-
body?: BodyInit,
59-
headers: Record<string, string|undefined> = {}
60-
): Promise<any> {
61-
return await this.fetch(url, {
62-
method: "PATCH",
63-
headers: this.removeEmpty(headers),
64-
body,
65-
});
66-
}
67-
68-
protected async DELETE(
69-
url: string,
70-
body?: BodyInit,
71-
headers: Record<string, string|undefined> = {}
72-
): Promise<void> {
73-
return await this.fetch(url, {
74-
method: "DELETE",
75-
headers: this.removeEmpty(headers),
76-
body,
77-
});
78-
}
79-
80-
protected async fetch(url: string, options: RequestInit): Promise<any> {
24+
protected async fetch(url: string, options: RequestInit = {}): Promise<any> {
8125
const result = await fetch(url, {
8226
credentials: this.requestOptions?.credentials || "same-origin",
8327
mode: this.requestOptions?.mode,

snapshotTests/snapshot/infectionTracker/api.ts

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export interface ApplicationApis {
3333
export interface CaseWorkersApiInterface {
3434
/**
3535
*
36-
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
3736
* @throws {HttpError}
3837
* @memberof CaseWorkersApi
3938
*/
@@ -55,14 +54,11 @@ export interface CaseWorkersApiInterface {
5554
export class CaseWorkersApi extends BaseAPI implements CaseWorkersApiInterface {
5655
/**
5756
*
58-
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
5957
* @throws {HttpError}
6058
*/
6159
public async listCaseWorkers(): Promise<CaseWorkerDto> {
62-
return await this.GET(
63-
this.basePath + "/api/caseWorkers",
64-
undefined,
65-
{}
60+
return await this.fetch(
61+
this.basePath + "/api/caseWorkers"
6662
);
6763
}
6864
/**
@@ -73,11 +69,14 @@ export class CaseWorkersApi extends BaseAPI implements CaseWorkersApiInterface {
7369
public async registerCaseWorker(params: {
7470
caseWorkerDto?: CaseWorkerDto;
7571
}): Promise<void> {
76-
return await this.POST(
72+
return await this.fetch(
7773
this.basePath + "/api/caseWorkers",
78-
JSON.stringify(params.caseWorkerDto),
7974
{
80-
"Content-Type": "application/json",
75+
method: "POST",
76+
body: JSON.stringify(params.caseWorkerDto),
77+
headers: {
78+
"Content-Type": "application/json",
79+
}
8180
}
8281
);
8382
}
@@ -97,7 +96,6 @@ export interface CasesApiInterface {
9796
}): Promise<InfectionDto>;
9897
/**
9998
*
100-
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
10199
* @throws {HttpError}
102100
* @memberof CasesApi
103101
*/
@@ -135,22 +133,17 @@ export class CasesApi extends BaseAPI implements CasesApiInterface {
135133
public async getCaseDetails(params: {
136134
pathParams: { caseId: string };
137135
}): Promise<InfectionDto> {
138-
return await this.GET(
139-
this.url("/api/cases/{caseId}", params.pathParams),
140-
undefined,
141-
{}
136+
return await this.fetch(
137+
this.url("/api/cases/{caseId}", params.pathParams)
142138
);
143139
}
144140
/**
145141
*
146-
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
147142
* @throws {HttpError}
148143
*/
149144
public async listCases(): Promise<InfectionDto> {
150-
return await this.GET(
151-
this.basePath + "/api/cases",
152-
undefined,
153-
{}
145+
return await this.fetch(
146+
this.basePath + "/api/cases"
154147
);
155148
}
156149
/**
@@ -161,11 +154,14 @@ export class CasesApi extends BaseAPI implements CasesApiInterface {
161154
public async newCase(params: {
162155
infectionInformationDto?: InfectionInformationDto;
163156
}): Promise<void> {
164-
return await this.POST(
157+
return await this.fetch(
165158
this.basePath + "/api/cases",
166-
JSON.stringify(params.infectionInformationDto),
167159
{
168-
"Content-Type": "application/json",
160+
method: "POST",
161+
body: JSON.stringify(params.infectionInformationDto),
162+
headers: {
163+
"Content-Type": "application/json",
164+
}
169165
}
170166
);
171167
}
@@ -178,11 +174,14 @@ export class CasesApi extends BaseAPI implements CasesApiInterface {
178174
pathParams: { caseId: string };
179175
exposureDto?: ExposureDto;
180176
}): Promise<void> {
181-
return await this.POST(
177+
return await this.fetch(
182178
this.url("/api/cases/{caseId}/exposures", params.pathParams),
183-
JSON.stringify(params.exposureDto),
184179
{
185-
"Content-Type": "application/json",
180+
method: "POST",
181+
body: JSON.stringify(params.exposureDto),
182+
headers: {
183+
"Content-Type": "application/json",
184+
}
186185
}
187186
);
188187
}
@@ -193,7 +192,6 @@ export class CasesApi extends BaseAPI implements CasesApiInterface {
193192
export interface ExposuresApiInterface {
194193
/**
195194
*
196-
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
197195
* @throws {HttpError}
198196
* @memberof ExposuresApi
199197
*/
@@ -216,14 +214,11 @@ export interface ExposuresApiInterface {
216214
export class ExposuresApi extends BaseAPI implements ExposuresApiInterface {
217215
/**
218216
*
219-
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
220217
* @throws {HttpError}
221218
*/
222219
public async listExposures(): Promise<ExposureDto> {
223-
return await this.GET(
224-
this.basePath + "/api/exposures",
225-
undefined,
226-
{}
220+
return await this.fetch(
221+
this.basePath + "/api/exposures"
227222
);
228223
}
229224
/**
@@ -235,11 +230,14 @@ export class ExposuresApi extends BaseAPI implements ExposuresApiInterface {
235230
pathParams: { exposureId: string };
236231
exposureDto?: ExposureDto;
237232
}): Promise<void> {
238-
return await this.PUT(
233+
return await this.fetch(
239234
this.url("/api/exposures/{exposureId}", params.pathParams),
240-
JSON.stringify(params.exposureDto),
241235
{
242-
"Content-Type": "application/json",
236+
method: "PUT",
237+
body: JSON.stringify(params.exposureDto),
238+
headers: {
239+
"Content-Type": "application/json",
240+
}
243241
}
244242
);
245243
}

snapshotTests/snapshot/infectionTracker/base.ts

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,63 +21,7 @@ export class BaseAPI {
2121
mode?: RequestMode
2222
} | undefined) {}
2323

24-
protected async GET(
25-
url: string,
26-
requestBody: undefined,
27-
headers: Record<string, string|undefined> = {}
28-
): Promise<any> {
29-
return await this.fetch(url, { headers: this.removeEmpty(headers) });
30-
}
31-
32-
protected async PUT(
33-
url: string,
34-
body?: BodyInit,
35-
headers: Record<string, string|undefined> = {}
36-
): Promise<any> {
37-
return await this.fetch(url, {
38-
method: "PUT",
39-
headers: this.removeEmpty(headers),
40-
body,
41-
});
42-
}
43-
44-
protected async POST(
45-
url: string,
46-
body?: BodyInit,
47-
headers: Record<string, string|undefined> = {}
48-
): Promise<any> {
49-
return await this.fetch(url, {
50-
method: "POST",
51-
headers: this.removeEmpty(headers),
52-
body,
53-
});
54-
}
55-
56-
protected async PATCH(
57-
url: string,
58-
body?: BodyInit,
59-
headers: Record<string, string|undefined> = {}
60-
): Promise<any> {
61-
return await this.fetch(url, {
62-
method: "PATCH",
63-
headers: this.removeEmpty(headers),
64-
body,
65-
});
66-
}
67-
68-
protected async DELETE(
69-
url: string,
70-
body?: BodyInit,
71-
headers: Record<string, string|undefined> = {}
72-
): Promise<void> {
73-
return await this.fetch(url, {
74-
method: "DELETE",
75-
headers: this.removeEmpty(headers),
76-
body,
77-
});
78-
}
79-
80-
protected async fetch(url: string, options: RequestInit): Promise<any> {
24+
protected async fetch(url: string, options: RequestInit = {}): Promise<any> {
8125
const result = await fetch(url, {
8226
credentials: this.requestOptions?.credentials || "same-origin",
8327
mode: this.requestOptions?.mode,

0 commit comments

Comments
 (0)