@@ -2,6 +2,7 @@ import { Readable } from "stream";
22import HTTPClient from "./http" ;
33import * as Types from "./types" ;
44import { JSONParseError } from "./exceptions" ;
5+ import { AxiosResponse } from "axios" ;
56
67function toArray < T > ( maybeArr : T | T [ ] ) : T [ ] {
78 return Array . isArray ( maybeArr ) ? maybeArr : [ maybeArr ] ;
@@ -27,40 +28,33 @@ export default class Client {
2728 }
2829
2930 this . config = config ;
30- this . http = new HTTPClient (
31- process . env . API_BASE_URL || "https://api.line.me/v2/bot/" ,
32- {
31+ this . http = new HTTPClient ( {
32+ baseURL : process . env . API_BASE_URL || "https://api.line.me/v2/bot/" ,
33+ defaultHeaders : {
3334 Authorization : "Bearer " + this . config . channelAccessToken ,
3435 } ,
35- ) ;
36- }
37-
38- private setLineRequestId ( response : object , lineRequestId : string ) : boolean {
39- return Reflect . defineProperty ( response , "getLineRequestId" , {
40- enumerable : false ,
41- value : ( ) : string => {
42- return lineRequestId ;
43- } ,
36+ responseParser : this . parseHTTPResponse . bind ( this ) ,
4437 } ) ;
4538 }
4639
47- private async postMessagingAPI (
48- url : string ,
49- body ?: any ,
50- ) : Promise < Types . MessageAPIResponseBase > {
51- const res = await this . http . postJson ( url , body ) ;
52- // header names are lower-cased
53- // https://nodejs.org/api/http.html#http_message_headers
54- this . setLineRequestId ( res . data , res . headers [ "x-line-request-id" ] ) ;
55- return res . data as Types . MessageAPIResponseBase ;
40+ private parseHTTPResponse ( response : AxiosResponse ) {
41+ const { LINE_REQUEST_ID_HTTP_HEADER_NAME } = Types ;
42+ let resBody = {
43+ ...response . data ,
44+ } ;
45+ if ( response . headers [ LINE_REQUEST_ID_HTTP_HEADER_NAME ] ) {
46+ resBody [ LINE_REQUEST_ID_HTTP_HEADER_NAME ] =
47+ response . headers [ LINE_REQUEST_ID_HTTP_HEADER_NAME ] ;
48+ }
49+ return resBody ;
5650 }
5751
5852 public pushMessage (
5953 to : string ,
6054 messages : Types . Message | Types . Message [ ] ,
6155 notificationDisabled : boolean = false ,
6256 ) : Promise < Types . MessageAPIResponseBase > {
63- return this . postMessagingAPI ( "/message/push" , {
57+ return this . http . post ( "/message/push" , {
6458 messages : toArray ( messages ) ,
6559 to,
6660 notificationDisabled,
@@ -72,7 +66,7 @@ export default class Client {
7266 messages : Types . Message | Types . Message [ ] ,
7367 notificationDisabled : boolean = false ,
7468 ) : Promise < Types . MessageAPIResponseBase > {
75- return this . postMessagingAPI ( "/message/reply" , {
69+ return this . http . post ( "/message/reply" , {
7670 messages : toArray ( messages ) ,
7771 replyToken,
7872 notificationDisabled,
@@ -84,7 +78,7 @@ export default class Client {
8478 messages : Types . Message | Types . Message [ ] ,
8579 notificationDisabled : boolean = false ,
8680 ) : Promise < Types . MessageAPIResponseBase > {
87- return this . postMessagingAPI ( "/message/multicast" , {
81+ return this . http . post ( "/message/multicast" , {
8882 messages : toArray ( messages ) ,
8983 to,
9084 notificationDisabled,
@@ -94,7 +88,7 @@ export default class Client {
9488 public async broadcast (
9589 messages : Types . Message | Types . Message [ ] ,
9690 notificationDisabled : boolean = false ,
97- ) : Promise < any > {
91+ ) : Promise < Types . MessageAPIResponseBase > {
9892 return this . http . post ( "/message/broadcast" , {
9993 messages : toArray ( messages ) ,
10094 notificationDisabled,
0 commit comments