1
1
import { Readable } from "stream" ;
2
- import { get , post , stream , del , postBinary } from "./http" ;
2
+ import HTTPClient from "./http" ;
3
3
import * as Types from "./types" ;
4
- import * as URL from "./urls" ;
5
4
import { JSONParseError } from "./exceptions" ;
6
5
7
6
function toArray < T > ( maybeArr : T | T [ ] ) : T [ ] {
8
7
return Array . isArray ( maybeArr ) ? maybeArr : [ maybeArr ] ;
9
8
}
10
9
11
- function checkJSON ( raw : any ) : any {
10
+ function checkJSON < T > ( raw : T ) : T {
12
11
if ( typeof raw === "object" ) {
13
12
return raw ;
14
13
} else {
@@ -18,20 +17,27 @@ function checkJSON(raw: any): any {
18
17
19
18
export default class Client {
20
19
public config : Types . ClientConfig ;
20
+ private http : HTTPClient ;
21
21
22
22
constructor ( config : Types . ClientConfig ) {
23
23
if ( ! config . channelAccessToken ) {
24
24
throw new Error ( "no channel access token" ) ;
25
25
}
26
26
27
27
this . config = config ;
28
+ this . http = new HTTPClient (
29
+ process . env . API_BASE_URL || "https://api.line.me/v2/bot/" ,
30
+ {
31
+ Authorization : "Bearer " + this . config . channelAccessToken ,
32
+ } ,
33
+ ) ;
28
34
}
29
35
30
36
public pushMessage (
31
37
to : string ,
32
38
messages : Types . Message | Types . Message [ ] ,
33
39
) : Promise < any > {
34
- return this . post ( URL . push , {
40
+ return this . http . post ( "/message/ push" , {
35
41
messages : toArray ( messages ) ,
36
42
to,
37
43
} ) ;
@@ -41,7 +47,7 @@ export default class Client {
41
47
replyToken : string ,
42
48
messages : Types . Message | Types . Message [ ] ,
43
49
) : Promise < any > {
44
- return this . post ( URL . reply , {
50
+ return this . http . post ( "/message/ reply" , {
45
51
messages : toArray ( messages ) ,
46
52
replyToken,
47
53
} ) ;
@@ -51,33 +57,38 @@ export default class Client {
51
57
to : string [ ] ,
52
58
messages : Types . Message | Types . Message [ ] ,
53
59
) : Promise < any > {
54
- return this . post ( URL . multicast , {
60
+ return this . http . post ( "/message/ multicast" , {
55
61
messages : toArray ( messages ) ,
56
62
to,
57
63
} ) ;
58
64
}
59
65
60
66
public getProfile ( userId : string ) : Promise < Types . Profile > {
61
- return this . get ( URL . profile ( userId ) ) . then ( checkJSON ) ;
67
+ return this . http . get < Types . Profile > ( `/profile/ ${ userId } ` ) . then ( checkJSON ) ;
62
68
}
63
69
64
70
public getGroupMemberProfile (
65
71
groupId : string ,
66
72
userId : string ,
67
73
) : Promise < Types . Profile > {
68
- return this . get ( URL . groupMemberProfile ( groupId , userId ) ) . then ( checkJSON ) ;
74
+ return this . http
75
+ . get < Types . Profile > ( `/group/${ groupId } /member/${ userId } ` )
76
+ . then ( checkJSON ) ;
69
77
}
70
78
71
79
public getRoomMemberProfile (
72
80
roomId : string ,
73
81
userId : string ,
74
82
) : Promise < Types . Profile > {
75
- return this . get ( URL . roomMemberProfile ( roomId , userId ) ) . then ( checkJSON ) ;
83
+ return this . http
84
+ . get < Types . Profile > ( `/room/${ roomId } /member/${ userId } ` )
85
+ . then ( checkJSON ) ;
76
86
}
77
87
78
88
public getGroupMemberIds ( groupId : string ) : Promise < string [ ] > {
79
89
const load = ( start ?: string ) : Promise < string [ ] > =>
80
- this . get ( URL . groupMemberIds ( groupId , start ) )
90
+ this . http
91
+ . get ( `/group/${ groupId } /members/ids` , start ? { start } : null )
81
92
. then ( checkJSON )
82
93
. then ( ( res : { memberIds : string [ ] ; next ?: string } ) => {
83
94
if ( ! res . next ) {
@@ -93,7 +104,8 @@ export default class Client {
93
104
94
105
public getRoomMemberIds ( roomId : string ) : Promise < string [ ] > {
95
106
const load = ( start ?: string ) : Promise < string [ ] > =>
96
- this . get ( URL . roomMemberIds ( roomId , start ) )
107
+ this . http
108
+ . get ( `/room/${ roomId } /members/ids` , start ? { start } : null )
97
109
. then ( checkJSON )
98
110
. then ( ( res : { memberIds : string [ ] ; next ?: string } ) => {
99
111
if ( ! res . next ) {
@@ -108,102 +120,84 @@ export default class Client {
108
120
}
109
121
110
122
public getMessageContent ( messageId : string ) : Promise < Readable > {
111
- return this . stream ( URL . content ( messageId ) ) ;
123
+ return this . http . getStream ( `/message/ ${ messageId } /content` ) ;
112
124
}
113
125
114
126
public leaveGroup ( groupId : string ) : Promise < any > {
115
- return this . post ( URL . leaveGroup ( groupId ) ) ;
127
+ return this . http . post ( `/group/ ${ groupId } /leave` ) ;
116
128
}
117
129
118
130
public leaveRoom ( roomId : string ) : Promise < any > {
119
- return this . post ( URL . leaveRoom ( roomId ) ) ;
131
+ return this . http . post ( `/room/ ${ roomId } /leave` ) ;
120
132
}
121
133
122
134
public getRichMenu ( richMenuId : string ) : Promise < Types . RichMenuResponse > {
123
- return this . get ( URL . richMenu ( richMenuId ) ) . then ( checkJSON ) ;
135
+ return this . http
136
+ . get < Types . RichMenuResponse > ( `/richmenu/${ richMenuId } ` )
137
+ . then ( checkJSON ) ;
124
138
}
125
139
126
140
public createRichMenu ( richMenu : Types . RichMenu ) : Promise < string > {
127
- return this . post ( URL . richMenu ( ) , richMenu )
141
+ return this . http
142
+ . post < any > ( "/richmenu" , richMenu )
128
143
. then ( checkJSON )
129
144
. then ( res => res . richMenuId ) ;
130
145
}
131
146
132
147
public deleteRichMenu ( richMenuId : string ) : Promise < any > {
133
- return this . delete ( URL . richMenu ( richMenuId ) ) ;
148
+ return this . http . delete ( `/richmenu/ ${ richMenuId } ` ) ;
134
149
}
135
150
136
151
public getRichMenuIdOfUser ( userId : string ) : Promise < string > {
137
- return this . get ( URL . userRichMenu ( userId ) )
152
+ return this . http
153
+ . get < any > ( `/user/${ userId } /richmenu` )
138
154
. then ( checkJSON )
139
155
. then ( res => res . richMenuId ) ;
140
156
}
141
157
142
158
public linkRichMenuToUser ( userId : string , richMenuId : string ) : Promise < any > {
143
- return this . post ( URL . userRichMenu ( userId , richMenuId ) ) ;
159
+ return this . http . post ( `/user/ ${ userId } /richmenu/ ${ richMenuId } ` ) ;
144
160
}
145
161
146
162
public unlinkRichMenuFromUser ( userId : string ) : Promise < any > {
147
- return this . delete ( URL . userRichMenu ( userId ) ) ;
163
+ return this . http . delete ( `/user/ ${ userId } /richmenu` ) ;
148
164
}
149
165
150
166
public getRichMenuImage ( richMenuId : string ) : Promise < Readable > {
151
- return this . stream ( URL . richMenuContent ( richMenuId ) ) ;
167
+ return this . http . getStream ( `/richmenu/ ${ richMenuId } /content` ) ;
152
168
}
153
169
154
170
public setRichMenuImage (
155
171
richMenuId : string ,
156
172
data : Buffer | Readable ,
157
173
contentType ?: string ,
158
174
) : Promise < any > {
159
- return this . postBinary ( URL . richMenuContent ( richMenuId ) , data , contentType ) ;
175
+ return this . http . postBinary (
176
+ `/richmenu/${ richMenuId } /content` ,
177
+ data ,
178
+ contentType ,
179
+ ) ;
160
180
}
161
181
162
182
public getRichMenuList ( ) : Promise < Array < Types . RichMenuResponse > > {
163
- return this . get ( URL . richMenuList ( ) )
183
+ return this . http
184
+ . get < any > ( `/richmenu/list` )
164
185
. then ( checkJSON )
165
186
. then ( res => res . richmenus ) ;
166
187
}
167
188
168
189
public setDefaultRichMenu ( richMenuId : string ) : Promise < { } > {
169
- return this . post ( URL . defaultRichMenu ( richMenuId ) ) ;
190
+ return this . http . post ( `/user/all/richmenu/ ${ richMenuId } ` ) ;
170
191
}
171
192
172
193
public getDefaultRichMenuId ( ) : Promise < string > {
173
- return this . get ( URL . defaultRichMenu ( ) )
194
+ return this . http
195
+ . get < any > ( "/user/all/richmenu" )
174
196
. then ( checkJSON )
175
197
. then ( res => res . richMenuId ) ;
176
198
}
177
199
178
200
public deleteDefaultRichMenu ( ) : Promise < { } > {
179
- return this . delete ( URL . defaultRichMenu ( ) ) ;
180
- }
181
-
182
- private authHeader ( ) : { [ key : string ] : string } {
183
- return { Authorization : "Bearer " + this . config . channelAccessToken } ;
184
- }
185
-
186
- private delete ( url : string ) : Promise < any > {
187
- return del ( url , this . authHeader ( ) ) ;
188
- }
189
-
190
- private get ( url : string ) : Promise < any > {
191
- return get ( url , this . authHeader ( ) ) ;
192
- }
193
-
194
- private post ( url : string , body ?: any ) : Promise < any > {
195
- return post ( url , this . authHeader ( ) , body ) ;
196
- }
197
-
198
- private postBinary (
199
- url : string ,
200
- data : Buffer | Readable ,
201
- contentType ?: string ,
202
- ) {
203
- return postBinary ( url , this . authHeader ( ) , data , contentType ) ;
204
- }
205
-
206
- private stream ( url : string ) : Promise < Readable > {
207
- return stream ( url , this . authHeader ( ) ) ;
201
+ return this . http . delete ( "/user/all/richmenu" ) ;
208
202
}
209
203
}
0 commit comments