Skip to content

Commit

Permalink
Merge pull request #325 from /issues/324/main
Browse files Browse the repository at this point in the history
Call afterMiddleware before throwing error
  • Loading branch information
moezakura authored Jul 7, 2022
2 parents 2a94af0 + ea64532 commit bae2eef
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 65 deletions.
15 changes: 11 additions & 4 deletions e2e/multipart/clients/ts/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface MiddlewareContext {
endpoint: string;
request: unknown;
response?: unknown;
responseBody?: string;
baseURL: string;
headers: {[key: string]: string};
options: {[key: string]: any};
Expand Down Expand Up @@ -181,11 +182,14 @@ class ParamClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ParamPostBResponse;
const res = JSON.parse(responseText) as ParamPostBResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -393,11 +397,14 @@ export class APIClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as PostAResponse;
const res = JSON.parse(responseText) as PostAResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down
15 changes: 11 additions & 4 deletions pkg/client/typescript/templates/api.ts.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface MiddlewareContext {
endpoint: string;
request: unknown;
response?: unknown;
responseBody?: string;
baseURL: string;
headers: {[key: string]: string};
options: {[key: string]: any};
Expand Down Expand Up @@ -220,12 +221,15 @@ class {{ $elem.Name }} {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
{{- if eq .HasResFields true }}
const res = (await resp.json()) as {{ $method.ResponseType }};
const res = JSON.parse(responseText) as {{ $method.ResponseType }};
{{- else }}
await resp.text();
const res = {} as {{ $method.ResponseType }};
Expand Down Expand Up @@ -465,13 +469,16 @@ export class APIClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}

{{- if eq .HasResFields true }}
const res = (await resp.json()) as {{ $method.ResponseType }};
const res = JSON.parse(responseText) as {{ $method.ResponseType }};
{{- else }}
await resp.text();
const res = {} as {{ $method.ResponseType }};
Expand Down
102 changes: 74 additions & 28 deletions pkg/client/typescript/testdata/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export interface MiddlewareContext {
endpoint: string;
request: unknown;
response?: unknown;
responseBody?: string;
baseURL: string;
headers: {[key: string]: string};
options: {[key: string]: any};
Expand Down Expand Up @@ -248,11 +249,14 @@ class ServiceClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceGetArticleResponse;
const res = JSON.parse(responseText) as ServiceGetArticleResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -396,11 +400,14 @@ class ServiceGroupsClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceGroupsGetGroupsResponse;
const res = JSON.parse(responseText) as ServiceGroupsGetGroupsResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -587,11 +594,14 @@ class ServiceStaticPageClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceStaticPageGetStaticPageResponse;
const res = JSON.parse(responseText) as ServiceStaticPageGetStaticPageResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -788,8 +798,11 @@ class ServiceUser2Client {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
await resp.text();
Expand Down Expand Up @@ -869,11 +882,14 @@ class ServiceUser2Client {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceUser2GetUserResponse;
const res = JSON.parse(responseText) as ServiceUser2GetUserResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -965,11 +981,14 @@ class ServiceUser2Client {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceUser2PostUpdateUserNameResponse;
const res = JSON.parse(responseText) as ServiceUser2PostUpdateUserNameResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -1047,11 +1066,14 @@ class ServiceUser2Client {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceUser2PostUpdateUserPasswordResponse;
const res = JSON.parse(responseText) as ServiceUser2PostUpdateUserPasswordResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -1192,11 +1214,14 @@ class ServiceUser2UserIDClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceUser2UserIDGetUserJobGetResponse;
const res = JSON.parse(responseText) as ServiceUser2UserIDGetUserJobGetResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -1328,11 +1353,14 @@ class ServiceUser2UserIDJobIDClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceUser2UserIDJobIDPutJobResponse;
const res = JSON.parse(responseText) as ServiceUser2UserIDJobIDPutJobResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -1465,8 +1493,11 @@ class ServiceUserClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
await resp.text();
Expand Down Expand Up @@ -1548,11 +1579,14 @@ class ServiceUserClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceUserPostUpdateUserNameResponse;
const res = JSON.parse(responseText) as ServiceUserPostUpdateUserNameResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -1630,11 +1664,14 @@ class ServiceUserClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as ServiceUserPostUpdateUserPasswordResponse;
const res = JSON.parse(responseText) as ServiceUserPostUpdateUserPasswordResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -1825,11 +1862,14 @@ export class APIClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as GetResponse;
const res = JSON.parse(responseText) as GetResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -1927,11 +1967,14 @@ export class APIClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as PostCreateTableResponse;
const res = JSON.parse(responseText) as PostCreateTableResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down Expand Up @@ -2015,11 +2058,14 @@ export class APIClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
const res = (await resp.json()) as PostCreateUserResponse;
const res = JSON.parse(responseText) as PostCreateUserResponse;
context.response = res;
await this.callMiddleware(this.afterMiddleware, context);
return res;
Expand Down
6 changes: 5 additions & 1 deletion samples/empty_root/clients/ts/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface MiddlewareContext {
endpoint: string;
request: unknown;
response?: unknown;
responseBody?: string;
baseURL: string;
headers: {[key: string]: string};
options: {[key: string]: any};
Expand Down Expand Up @@ -165,8 +166,11 @@ class FooBarClient {
}
);

const responseText = await resp.text();
context.responseBody = responseText;

if (Math.floor(resp.status / 100) !== 2) {
const responseText = await resp.text();
await this.callMiddleware(this.afterMiddleware, context);
throw new ApiError(resp, responseText);
}
await resp.text();
Expand Down
Loading

0 comments on commit bae2eef

Please sign in to comment.