Skip to content

Commit 0c7293e

Browse files
authored
Simplify the http-fetch.ts (#730)
Before implement WithHttpInfo methods, simplify the http-fetch.ts
1 parent d522eea commit 0c7293e

File tree

15 files changed

+557
-342
lines changed

15 files changed

+557
-342
lines changed

generator/src/main/resources/line-bot-sdk-nodejs-generator/api-single.pebble

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as Types from "../../types";
1111
import {ensureJSON} from "../../utils";
1212
import {Readable} from "stream";
1313

14-
import HTTPFetchClient from "../../http-fetch";
14+
import HTTPFetchClient, { convertResponseToReadable } from "../../http-fetch";
1515

1616
// ===============================================
1717
// This file is autogenerated - Please do not edit
@@ -39,7 +39,6 @@ export class {{operations.classname}} {
3939
Authorization: "Bearer " + config.channelAccessToken,
4040
{% endif -%}
4141
},
42-
responseParser: this.parseHTTPResponse.bind(this),
4342
baseURL: config.baseURL,
4443
});
4544
}

generator/src/main/resources/line-bot-sdk-nodejs-generator/apiBody/multipart.pebble

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
form.append("{{param.paramName}}", String({{param.paramName}}));
88
{% endif -%}
99
{% endfor %}
10-
const res = this.httpClient.{{op.httpMethod|lower}}{% if op.hasFormParams %}Form{% endif %}Multipart{% if op.returnType %}<{{ op.returnType }}>{% endif %}(
10+
const res = await this.httpClient.{{op.httpMethod|lower}}{% if op.hasFormParams %}Form{% endif %}Multipart(
1111
"{{op.path}}"
1212
{% for param in op.pathParams -%}
1313
.replace("{{ "{" + param.paramName + "}" }}", String({{ param.paramName }}))
1414
{% endfor %},
1515
form,
1616
);
17-
return ensureJSON(res);
17+
const result = (await this.parseHTTPResponse(res)) as {% if op.returnType %}{{ op.returnType }}{% else %}Types.MessageAPIResponseBase{% endif %};
18+
return ensureJSON(result);

generator/src/main/resources/line-bot-sdk-nodejs-generator/apiBody/normal.pebble

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{# @pebvariable name="op" type="org.openapitools.codegen.CodegenOperation" #}
22

33
{% if op.isResponseFile %}
4-
return this.httpClient.{{op.httpMethod|lower}}Stream("{{op.path}}"
4+
const response = await this.httpClient.{{op.httpMethod|lower}}("{{op.path}}"
55
{% for param in op.pathParams %}
66
.replace('{' + "{{param.baseName}}" + '}', String({{param.paramName}}))
77
{% endfor %}
88
);
9+
return convertResponseToReadable(response);
910
{% else %}
1011
{% if op.hasBodyParam %}const params = {{op.bodyParam.paramName}};
1112
{% elseif op.hasFormParams %}const formParams = {
@@ -31,7 +32,7 @@
3132
};
3233
{% endif %}
3334

34-
const res = this.httpClient.{{op.httpMethod|lower}}{% if op.hasFormParams %}Form{% endif %}{% if op.isMultipart %}Multipart{% endif %}{% if op.hasBodyParam and op.bodyParam.isFile %}BinaryContent{% endif %}{% if op.returnType %}<{{ op.returnType }}>{% endif %}(
35+
const res = await this.httpClient.{{op.httpMethod|lower}}{% if op.hasFormParams %}Form{% endif %}{% if op.isMultipart %}Multipart{% endif %}{% if op.hasBodyParam and op.bodyParam.isFile %}BinaryContent{% endif %}(
3536
"{{ op.path }}"
3637
{% for param in op.pathParams %}
3738
.replace("{{ "{" + param.paramName + "}" }}", String({{ param.paramName }}))
@@ -42,5 +43,6 @@
4243
{% elseif op.hasQueryParams %}queryParams,{% endif %}
4344
{% if op.hasHeaderParams %}{headers: headerParams},{% endif %}
4445
);
45-
return ensureJSON(res);
46+
const result = (await this.parseHTTPResponse(res)) as {% if op.returnType %}{{ op.returnType }}{% else %}Types.MessageAPIResponseBase{% endif %};
47+
return ensureJSON(result);
4648
{% endif %}

lib/channel-access-token/api/channelAccessTokenClient.ts

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as Types from "../../types";
2222
import { ensureJSON } from "../../utils";
2323
import { Readable } from "stream";
2424

25-
import HTTPFetchClient from "../../http-fetch";
25+
import HTTPFetchClient, { convertResponseToReadable } from "../../http-fetch";
2626

2727
// ===============================================
2828
// This file is autogenerated - Please do not edit
@@ -42,7 +42,6 @@ export class ChannelAccessTokenClient {
4242
}
4343
this.httpClient = new HTTPFetchClient({
4444
defaultHeaders: {},
45-
responseParser: this.parseHTTPResponse.bind(this),
4645
baseURL: config.baseURL,
4746
});
4847
}
@@ -76,11 +75,14 @@ export class ChannelAccessTokenClient {
7675
clientAssertion: clientAssertion,
7776
};
7877

79-
const res = this.httpClient.get<ChannelAccessTokenKeyIdsResponse>(
78+
const res = await this.httpClient.get(
8079
"/oauth2/v2.1/tokens/kid",
8180
queryParams,
8281
);
83-
return ensureJSON(res);
82+
const result = (await this.parseHTTPResponse(
83+
res,
84+
)) as ChannelAccessTokenKeyIdsResponse;
85+
return ensureJSON(result);
8486
}
8587
/**
8688
* Issue short-lived channel access token
@@ -106,12 +108,14 @@ export class ChannelAccessTokenClient {
106108
}
107109
});
108110

109-
const res =
110-
this.httpClient.postForm<IssueShortLivedChannelAccessTokenResponse>(
111-
"/v2/oauth/accessToken",
112-
formParams,
113-
);
114-
return ensureJSON(res);
111+
const res = await this.httpClient.postForm(
112+
"/v2/oauth/accessToken",
113+
formParams,
114+
);
115+
const result = (await this.parseHTTPResponse(
116+
res,
117+
)) as IssueShortLivedChannelAccessTokenResponse;
118+
return ensureJSON(result);
115119
}
116120
/**
117121
* Issues a channel access token that allows you to specify a desired expiration date. This method lets you use JWT assertion for authentication.
@@ -137,11 +141,14 @@ export class ChannelAccessTokenClient {
137141
}
138142
});
139143

140-
const res = this.httpClient.postForm<IssueChannelAccessTokenResponse>(
144+
const res = await this.httpClient.postForm(
141145
"/oauth2/v2.1/token",
142146
formParams,
143147
);
144-
return ensureJSON(res);
148+
const result = (await this.parseHTTPResponse(
149+
res,
150+
)) as IssueChannelAccessTokenResponse;
151+
return ensureJSON(result);
145152
}
146153
/**
147154
* Issues a new stateless channel access token, which doesn\'t have max active token limit unlike the other token types. The newly issued token is only valid for 15 minutes but can not be revoked until it naturally expires.
@@ -173,12 +180,11 @@ export class ChannelAccessTokenClient {
173180
}
174181
});
175182

176-
const res =
177-
this.httpClient.postForm<IssueStatelessChannelAccessTokenResponse>(
178-
"/oauth2/v3/token",
179-
formParams,
180-
);
181-
return ensureJSON(res);
183+
const res = await this.httpClient.postForm("/oauth2/v3/token", formParams);
184+
const result = (await this.parseHTTPResponse(
185+
res,
186+
)) as IssueStatelessChannelAccessTokenResponse;
187+
return ensureJSON(result);
182188
}
183189
/**
184190
* Revoke short-lived or long-lived channel access token
@@ -198,8 +204,11 @@ export class ChannelAccessTokenClient {
198204
}
199205
});
200206

201-
const res = this.httpClient.postForm("/v2/oauth/revoke", formParams);
202-
return ensureJSON(res);
207+
const res = await this.httpClient.postForm("/v2/oauth/revoke", formParams);
208+
const result = (await this.parseHTTPResponse(
209+
res,
210+
)) as Types.MessageAPIResponseBase;
211+
return ensureJSON(result);
203212
}
204213
/**
205214
* Revoke channel access token v2.1
@@ -225,8 +234,14 @@ export class ChannelAccessTokenClient {
225234
}
226235
});
227236

228-
const res = this.httpClient.postForm("/oauth2/v2.1/revoke", formParams);
229-
return ensureJSON(res);
237+
const res = await this.httpClient.postForm(
238+
"/oauth2/v2.1/revoke",
239+
formParams,
240+
);
241+
const result = (await this.parseHTTPResponse(
242+
res,
243+
)) as Types.MessageAPIResponseBase;
244+
return ensureJSON(result);
230245
}
231246
/**
232247
* Verify the validity of short-lived and long-lived channel access tokens
@@ -246,11 +261,11 @@ export class ChannelAccessTokenClient {
246261
}
247262
});
248263

249-
const res = this.httpClient.postForm<VerifyChannelAccessTokenResponse>(
250-
"/v2/oauth/verify",
251-
formParams,
252-
);
253-
return ensureJSON(res);
264+
const res = await this.httpClient.postForm("/v2/oauth/verify", formParams);
265+
const result = (await this.parseHTTPResponse(
266+
res,
267+
)) as VerifyChannelAccessTokenResponse;
268+
return ensureJSON(result);
254269
}
255270
/**
256271
* You can verify whether a Channel access token with a user-specified expiration (Channel Access Token v2.1) is valid.
@@ -265,10 +280,10 @@ export class ChannelAccessTokenClient {
265280
accessToken: accessToken,
266281
};
267282

268-
const res = this.httpClient.get<VerifyChannelAccessTokenResponse>(
269-
"/oauth2/v2.1/verify",
270-
queryParams,
271-
);
272-
return ensureJSON(res);
283+
const res = await this.httpClient.get("/oauth2/v2.1/verify", queryParams);
284+
const result = (await this.parseHTTPResponse(
285+
res,
286+
)) as VerifyChannelAccessTokenResponse;
287+
return ensureJSON(result);
273288
}
274289
}

0 commit comments

Comments
 (0)