Skip to content

Commit bae2eef

Browse files
authored
Merge pull request #325 from /issues/324/main
Call afterMiddleware before throwing error
2 parents 2a94af0 + ea64532 commit bae2eef

File tree

5 files changed

+175
-65
lines changed

5 files changed

+175
-65
lines changed

e2e/multipart/clients/ts/api_client.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface MiddlewareContext {
2424
endpoint: string;
2525
request: unknown;
2626
response?: unknown;
27+
responseBody?: string;
2728
baseURL: string;
2829
headers: {[key: string]: string};
2930
options: {[key: string]: any};
@@ -181,11 +182,14 @@ class ParamClient {
181182
}
182183
);
183184

185+
const responseText = await resp.text();
186+
context.responseBody = responseText;
187+
184188
if (Math.floor(resp.status / 100) !== 2) {
185-
const responseText = await resp.text();
189+
await this.callMiddleware(this.afterMiddleware, context);
186190
throw new ApiError(resp, responseText);
187191
}
188-
const res = (await resp.json()) as ParamPostBResponse;
192+
const res = JSON.parse(responseText) as ParamPostBResponse;
189193
context.response = res;
190194
await this.callMiddleware(this.afterMiddleware, context);
191195
return res;
@@ -393,11 +397,14 @@ export class APIClient {
393397
}
394398
);
395399

400+
const responseText = await resp.text();
401+
context.responseBody = responseText;
402+
396403
if (Math.floor(resp.status / 100) !== 2) {
397-
const responseText = await resp.text();
404+
await this.callMiddleware(this.afterMiddleware, context);
398405
throw new ApiError(resp, responseText);
399406
}
400-
const res = (await resp.json()) as PostAResponse;
407+
const res = JSON.parse(responseText) as PostAResponse;
401408
context.response = res;
402409
await this.callMiddleware(this.afterMiddleware, context);
403410
return res;

pkg/client/typescript/templates/api.ts.tmpl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface MiddlewareContext {
2222
endpoint: string;
2323
request: unknown;
2424
response?: unknown;
25+
responseBody?: string;
2526
baseURL: string;
2627
headers: {[key: string]: string};
2728
options: {[key: string]: any};
@@ -220,12 +221,15 @@ class {{ $elem.Name }} {
220221
}
221222
);
222223

224+
const responseText = await resp.text();
225+
context.responseBody = responseText;
226+
223227
if (Math.floor(resp.status / 100) !== 2) {
224-
const responseText = await resp.text();
228+
await this.callMiddleware(this.afterMiddleware, context);
225229
throw new ApiError(resp, responseText);
226230
}
227231
{{- if eq .HasResFields true }}
228-
const res = (await resp.json()) as {{ $method.ResponseType }};
232+
const res = JSON.parse(responseText) as {{ $method.ResponseType }};
229233
{{- else }}
230234
await resp.text();
231235
const res = {} as {{ $method.ResponseType }};
@@ -465,13 +469,16 @@ export class APIClient {
465469
}
466470
);
467471

472+
const responseText = await resp.text();
473+
context.responseBody = responseText;
474+
468475
if (Math.floor(resp.status / 100) !== 2) {
469-
const responseText = await resp.text();
476+
await this.callMiddleware(this.afterMiddleware, context);
470477
throw new ApiError(resp, responseText);
471478
}
472479

473480
{{- if eq .HasResFields true }}
474-
const res = (await resp.json()) as {{ $method.ResponseType }};
481+
const res = JSON.parse(responseText) as {{ $method.ResponseType }};
475482
{{- else }}
476483
await resp.text();
477484
const res = {} as {{ $method.ResponseType }};

pkg/client/typescript/testdata/api_client.ts

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export interface MiddlewareContext {
6868
endpoint: string;
6969
request: unknown;
7070
response?: unknown;
71+
responseBody?: string;
7172
baseURL: string;
7273
headers: {[key: string]: string};
7374
options: {[key: string]: any};
@@ -248,11 +249,14 @@ class ServiceClient {
248249
}
249250
);
250251

252+
const responseText = await resp.text();
253+
context.responseBody = responseText;
254+
251255
if (Math.floor(resp.status / 100) !== 2) {
252-
const responseText = await resp.text();
256+
await this.callMiddleware(this.afterMiddleware, context);
253257
throw new ApiError(resp, responseText);
254258
}
255-
const res = (await resp.json()) as ServiceGetArticleResponse;
259+
const res = JSON.parse(responseText) as ServiceGetArticleResponse;
256260
context.response = res;
257261
await this.callMiddleware(this.afterMiddleware, context);
258262
return res;
@@ -396,11 +400,14 @@ class ServiceGroupsClient {
396400
}
397401
);
398402

403+
const responseText = await resp.text();
404+
context.responseBody = responseText;
405+
399406
if (Math.floor(resp.status / 100) !== 2) {
400-
const responseText = await resp.text();
407+
await this.callMiddleware(this.afterMiddleware, context);
401408
throw new ApiError(resp, responseText);
402409
}
403-
const res = (await resp.json()) as ServiceGroupsGetGroupsResponse;
410+
const res = JSON.parse(responseText) as ServiceGroupsGetGroupsResponse;
404411
context.response = res;
405412
await this.callMiddleware(this.afterMiddleware, context);
406413
return res;
@@ -587,11 +594,14 @@ class ServiceStaticPageClient {
587594
}
588595
);
589596

597+
const responseText = await resp.text();
598+
context.responseBody = responseText;
599+
590600
if (Math.floor(resp.status / 100) !== 2) {
591-
const responseText = await resp.text();
601+
await this.callMiddleware(this.afterMiddleware, context);
592602
throw new ApiError(resp, responseText);
593603
}
594-
const res = (await resp.json()) as ServiceStaticPageGetStaticPageResponse;
604+
const res = JSON.parse(responseText) as ServiceStaticPageGetStaticPageResponse;
595605
context.response = res;
596606
await this.callMiddleware(this.afterMiddleware, context);
597607
return res;
@@ -788,8 +798,11 @@ class ServiceUser2Client {
788798
}
789799
);
790800

801+
const responseText = await resp.text();
802+
context.responseBody = responseText;
803+
791804
if (Math.floor(resp.status / 100) !== 2) {
792-
const responseText = await resp.text();
805+
await this.callMiddleware(this.afterMiddleware, context);
793806
throw new ApiError(resp, responseText);
794807
}
795808
await resp.text();
@@ -869,11 +882,14 @@ class ServiceUser2Client {
869882
}
870883
);
871884

885+
const responseText = await resp.text();
886+
context.responseBody = responseText;
887+
872888
if (Math.floor(resp.status / 100) !== 2) {
873-
const responseText = await resp.text();
889+
await this.callMiddleware(this.afterMiddleware, context);
874890
throw new ApiError(resp, responseText);
875891
}
876-
const res = (await resp.json()) as ServiceUser2GetUserResponse;
892+
const res = JSON.parse(responseText) as ServiceUser2GetUserResponse;
877893
context.response = res;
878894
await this.callMiddleware(this.afterMiddleware, context);
879895
return res;
@@ -965,11 +981,14 @@ class ServiceUser2Client {
965981
}
966982
);
967983

984+
const responseText = await resp.text();
985+
context.responseBody = responseText;
986+
968987
if (Math.floor(resp.status / 100) !== 2) {
969-
const responseText = await resp.text();
988+
await this.callMiddleware(this.afterMiddleware, context);
970989
throw new ApiError(resp, responseText);
971990
}
972-
const res = (await resp.json()) as ServiceUser2PostUpdateUserNameResponse;
991+
const res = JSON.parse(responseText) as ServiceUser2PostUpdateUserNameResponse;
973992
context.response = res;
974993
await this.callMiddleware(this.afterMiddleware, context);
975994
return res;
@@ -1047,11 +1066,14 @@ class ServiceUser2Client {
10471066
}
10481067
);
10491068

1069+
const responseText = await resp.text();
1070+
context.responseBody = responseText;
1071+
10501072
if (Math.floor(resp.status / 100) !== 2) {
1051-
const responseText = await resp.text();
1073+
await this.callMiddleware(this.afterMiddleware, context);
10521074
throw new ApiError(resp, responseText);
10531075
}
1054-
const res = (await resp.json()) as ServiceUser2PostUpdateUserPasswordResponse;
1076+
const res = JSON.parse(responseText) as ServiceUser2PostUpdateUserPasswordResponse;
10551077
context.response = res;
10561078
await this.callMiddleware(this.afterMiddleware, context);
10571079
return res;
@@ -1192,11 +1214,14 @@ class ServiceUser2UserIDClient {
11921214
}
11931215
);
11941216

1217+
const responseText = await resp.text();
1218+
context.responseBody = responseText;
1219+
11951220
if (Math.floor(resp.status / 100) !== 2) {
1196-
const responseText = await resp.text();
1221+
await this.callMiddleware(this.afterMiddleware, context);
11971222
throw new ApiError(resp, responseText);
11981223
}
1199-
const res = (await resp.json()) as ServiceUser2UserIDGetUserJobGetResponse;
1224+
const res = JSON.parse(responseText) as ServiceUser2UserIDGetUserJobGetResponse;
12001225
context.response = res;
12011226
await this.callMiddleware(this.afterMiddleware, context);
12021227
return res;
@@ -1328,11 +1353,14 @@ class ServiceUser2UserIDJobIDClient {
13281353
}
13291354
);
13301355

1356+
const responseText = await resp.text();
1357+
context.responseBody = responseText;
1358+
13311359
if (Math.floor(resp.status / 100) !== 2) {
1332-
const responseText = await resp.text();
1360+
await this.callMiddleware(this.afterMiddleware, context);
13331361
throw new ApiError(resp, responseText);
13341362
}
1335-
const res = (await resp.json()) as ServiceUser2UserIDJobIDPutJobResponse;
1363+
const res = JSON.parse(responseText) as ServiceUser2UserIDJobIDPutJobResponse;
13361364
context.response = res;
13371365
await this.callMiddleware(this.afterMiddleware, context);
13381366
return res;
@@ -1465,8 +1493,11 @@ class ServiceUserClient {
14651493
}
14661494
);
14671495

1496+
const responseText = await resp.text();
1497+
context.responseBody = responseText;
1498+
14681499
if (Math.floor(resp.status / 100) !== 2) {
1469-
const responseText = await resp.text();
1500+
await this.callMiddleware(this.afterMiddleware, context);
14701501
throw new ApiError(resp, responseText);
14711502
}
14721503
await resp.text();
@@ -1548,11 +1579,14 @@ class ServiceUserClient {
15481579
}
15491580
);
15501581

1582+
const responseText = await resp.text();
1583+
context.responseBody = responseText;
1584+
15511585
if (Math.floor(resp.status / 100) !== 2) {
1552-
const responseText = await resp.text();
1586+
await this.callMiddleware(this.afterMiddleware, context);
15531587
throw new ApiError(resp, responseText);
15541588
}
1555-
const res = (await resp.json()) as ServiceUserPostUpdateUserNameResponse;
1589+
const res = JSON.parse(responseText) as ServiceUserPostUpdateUserNameResponse;
15561590
context.response = res;
15571591
await this.callMiddleware(this.afterMiddleware, context);
15581592
return res;
@@ -1630,11 +1664,14 @@ class ServiceUserClient {
16301664
}
16311665
);
16321666

1667+
const responseText = await resp.text();
1668+
context.responseBody = responseText;
1669+
16331670
if (Math.floor(resp.status / 100) !== 2) {
1634-
const responseText = await resp.text();
1671+
await this.callMiddleware(this.afterMiddleware, context);
16351672
throw new ApiError(resp, responseText);
16361673
}
1637-
const res = (await resp.json()) as ServiceUserPostUpdateUserPasswordResponse;
1674+
const res = JSON.parse(responseText) as ServiceUserPostUpdateUserPasswordResponse;
16381675
context.response = res;
16391676
await this.callMiddleware(this.afterMiddleware, context);
16401677
return res;
@@ -1825,11 +1862,14 @@ export class APIClient {
18251862
}
18261863
);
18271864

1865+
const responseText = await resp.text();
1866+
context.responseBody = responseText;
1867+
18281868
if (Math.floor(resp.status / 100) !== 2) {
1829-
const responseText = await resp.text();
1869+
await this.callMiddleware(this.afterMiddleware, context);
18301870
throw new ApiError(resp, responseText);
18311871
}
1832-
const res = (await resp.json()) as GetResponse;
1872+
const res = JSON.parse(responseText) as GetResponse;
18331873
context.response = res;
18341874
await this.callMiddleware(this.afterMiddleware, context);
18351875
return res;
@@ -1927,11 +1967,14 @@ export class APIClient {
19271967
}
19281968
);
19291969

1970+
const responseText = await resp.text();
1971+
context.responseBody = responseText;
1972+
19301973
if (Math.floor(resp.status / 100) !== 2) {
1931-
const responseText = await resp.text();
1974+
await this.callMiddleware(this.afterMiddleware, context);
19321975
throw new ApiError(resp, responseText);
19331976
}
1934-
const res = (await resp.json()) as PostCreateTableResponse;
1977+
const res = JSON.parse(responseText) as PostCreateTableResponse;
19351978
context.response = res;
19361979
await this.callMiddleware(this.afterMiddleware, context);
19371980
return res;
@@ -2015,11 +2058,14 @@ export class APIClient {
20152058
}
20162059
);
20172060

2061+
const responseText = await resp.text();
2062+
context.responseBody = responseText;
2063+
20182064
if (Math.floor(resp.status / 100) !== 2) {
2019-
const responseText = await resp.text();
2065+
await this.callMiddleware(this.afterMiddleware, context);
20202066
throw new ApiError(resp, responseText);
20212067
}
2022-
const res = (await resp.json()) as PostCreateUserResponse;
2068+
const res = JSON.parse(responseText) as PostCreateUserResponse;
20232069
context.response = res;
20242070
await this.callMiddleware(this.afterMiddleware, context);
20252071
return res;

samples/empty_root/clients/ts/api_client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface MiddlewareContext {
1919
endpoint: string;
2020
request: unknown;
2121
response?: unknown;
22+
responseBody?: string;
2223
baseURL: string;
2324
headers: {[key: string]: string};
2425
options: {[key: string]: any};
@@ -165,8 +166,11 @@ class FooBarClient {
165166
}
166167
);
167168

169+
const responseText = await resp.text();
170+
context.responseBody = responseText;
171+
168172
if (Math.floor(resp.status / 100) !== 2) {
169-
const responseText = await resp.text();
173+
await this.callMiddleware(this.afterMiddleware, context);
170174
throw new ApiError(resp, responseText);
171175
}
172176
await resp.text();

0 commit comments

Comments
 (0)