@@ -7,7 +7,7 @@ function toArray<T>(maybeArr: T | T[]): T[] {
7
7
return Array . isArray ( maybeArr ) ? maybeArr : [ maybeArr ] ;
8
8
}
9
9
10
- function checkJSON < T > ( raw : T ) : T {
10
+ function ensureJSON < T > ( raw : T ) : T {
11
11
if ( typeof raw === "object" ) {
12
12
return raw ;
13
13
} else {
@@ -35,7 +35,7 @@ export default class Client {
35
35
) ;
36
36
}
37
37
38
- public pushMessage (
38
+ public async pushMessage (
39
39
to : string ,
40
40
messages : Types . Message | Types . Message [ ] ,
41
41
) : Promise < any > {
@@ -45,7 +45,7 @@ export default class Client {
45
45
} ) ;
46
46
}
47
47
48
- public replyMessage (
48
+ public async replyMessage (
49
49
replyToken : string ,
50
50
messages : Types . Message | Types . Message [ ] ,
51
51
) : Promise < any > {
@@ -55,7 +55,7 @@ export default class Client {
55
55
} ) ;
56
56
}
57
57
58
- public multicast (
58
+ public async multicast (
59
59
to : string [ ] ,
60
60
messages : Types . Message | Types . Message [ ] ,
61
61
) : Promise < any > {
@@ -65,111 +65,115 @@ export default class Client {
65
65
} ) ;
66
66
}
67
67
68
- public getProfile ( userId : string ) : Promise < Types . Profile > {
69
- return this . http . get < Types . Profile > ( `/profile/${ userId } ` ) . then ( checkJSON ) ;
68
+ public async getProfile ( userId : string ) : Promise < Types . Profile > {
69
+ const profile = await this . http . get < Types . Profile > ( `/profile/${ userId } ` ) ;
70
+ return ensureJSON ( profile ) ;
70
71
}
71
72
72
- private getChatMemberProfile (
73
+ private async getChatMemberProfile (
73
74
chatType : ChatType ,
74
75
chatId : string ,
75
76
userId : string ,
76
77
) : Promise < Types . Profile > {
77
- return this . http
78
- . get < Types . Profile > ( `/${ chatType } /${ chatId } /member/${ userId } ` )
79
- . then ( checkJSON ) ;
78
+ const profile = await this . http . get < Types . Profile > (
79
+ `/${ chatType } /${ chatId } /member/${ userId } ` ,
80
+ ) ;
81
+ return ensureJSON ( profile ) ;
80
82
}
81
83
82
- public getGroupMemberProfile (
84
+ public async getGroupMemberProfile (
83
85
groupId : string ,
84
86
userId : string ,
85
87
) : Promise < Types . Profile > {
86
88
return this . getChatMemberProfile ( "group" , groupId , userId ) ;
87
89
}
88
90
89
- public getRoomMemberProfile (
91
+ public async getRoomMemberProfile (
90
92
roomId : string ,
91
93
userId : string ,
92
94
) : Promise < Types . Profile > {
93
95
return this . getChatMemberProfile ( "room" , roomId , userId ) ;
94
96
}
95
97
96
- private getChatMemberIds (
98
+ private async getChatMemberIds (
97
99
chatType : ChatType ,
98
100
chatId : string ,
99
101
) : Promise < string [ ] > {
100
- const load = ( start ? : string ) : Promise < string [ ] > =>
101
- this . http
102
- . get ( `/ ${ chatType } / ${ chatId } /members/ids` , start ? { start } : null )
103
- . then ( checkJSON )
104
- . then ( ( res : { memberIds : string [ ] ; next ?: string } ) => {
105
- if ( ! res . next ) {
106
- return res . memberIds ;
107
- }
108
-
109
- return load ( res . next ) . then ( extraIds =>
110
- res . memberIds . concat ( extraIds ) ,
111
- ) ;
112
- } ) ;
113
- return load ( ) ;
114
- }
115
-
116
- public getGroupMemberIds ( groupId : string ) : Promise < string [ ] > {
102
+ let memberIds : string [ ] = [ ] ;
103
+
104
+ let start : string ;
105
+ do {
106
+ const res = await this . http . get < { memberIds : string [ ] ; next ?: string } > (
107
+ `/ ${ chatType } / ${ chatId } /members/ids` ,
108
+ start ? { start } : null ,
109
+ ) ;
110
+ ensureJSON ( res ) ;
111
+ memberIds = memberIds . concat ( res . memberIds ) ;
112
+ start = res . next ;
113
+ } while ( start ) ;
114
+
115
+ return memberIds ;
116
+ }
117
+
118
+ public async getGroupMemberIds ( groupId : string ) : Promise < string [ ] > {
117
119
return this . getChatMemberIds ( "group" , groupId ) ;
118
120
}
119
121
120
- public getRoomMemberIds ( roomId : string ) : Promise < string [ ] > {
122
+ public async getRoomMemberIds ( roomId : string ) : Promise < string [ ] > {
121
123
return this . getChatMemberIds ( "room" , roomId ) ;
122
124
}
123
125
124
- public getMessageContent ( messageId : string ) : Promise < Readable > {
126
+ public async getMessageContent ( messageId : string ) : Promise < Readable > {
125
127
return this . http . getStream ( `/message/${ messageId } /content` ) ;
126
128
}
127
129
128
130
private leaveChat ( chatType : ChatType , chatId : string ) : Promise < any > {
129
131
return this . http . post ( `/${ chatType } /${ chatId } /leave` ) ;
130
132
}
131
133
132
- public leaveGroup ( groupId : string ) : Promise < any > {
134
+ public async leaveGroup ( groupId : string ) : Promise < any > {
133
135
return this . leaveChat ( "group" , groupId ) ;
134
136
}
135
137
136
- public leaveRoom ( roomId : string ) : Promise < any > {
138
+ public async leaveRoom ( roomId : string ) : Promise < any > {
137
139
return this . leaveChat ( "room" , roomId ) ;
138
140
}
139
141
140
- public getRichMenu ( richMenuId : string ) : Promise < Types . RichMenuResponse > {
141
- return this . http
142
- . get < Types . RichMenuResponse > ( `/richmenu/${ richMenuId } ` )
143
- . then ( checkJSON ) ;
142
+ public async getRichMenu (
143
+ richMenuId : string ,
144
+ ) : Promise < Types . RichMenuResponse > {
145
+ const res = await this . http . get < Types . RichMenuResponse > (
146
+ `/richmenu/${ richMenuId } ` ,
147
+ ) ;
148
+ return ensureJSON ( res ) ;
144
149
}
145
150
146
- public createRichMenu ( richMenu : Types . RichMenu ) : Promise < string > {
147
- return this . http
148
- . post < any > ( "/richmenu" , richMenu )
149
- . then ( checkJSON )
150
- . then ( res => res . richMenuId ) ;
151
+ public async createRichMenu ( richMenu : Types . RichMenu ) : Promise < string > {
152
+ const res = await this . http . post < any > ( "/richmenu" , richMenu ) ;
153
+ return ensureJSON ( res ) . richMenuId ;
151
154
}
152
155
153
- public deleteRichMenu ( richMenuId : string ) : Promise < any > {
156
+ public async deleteRichMenu ( richMenuId : string ) : Promise < any > {
154
157
return this . http . delete ( `/richmenu/${ richMenuId } ` ) ;
155
158
}
156
159
157
- public getRichMenuIdOfUser ( userId : string ) : Promise < string > {
158
- return this . http
159
- . get < any > ( `/user/${ userId } /richmenu` )
160
- . then ( checkJSON )
161
- . then ( res => res . richMenuId ) ;
160
+ public async getRichMenuIdOfUser ( userId : string ) : Promise < string > {
161
+ const res = await this . http . get < any > ( `/user/${ userId } /richmenu` ) ;
162
+ return ensureJSON ( res ) . richMenuId ;
162
163
}
163
164
164
- public linkRichMenuToUser ( userId : string , richMenuId : string ) : Promise < any > {
165
+ public async linkRichMenuToUser (
166
+ userId : string ,
167
+ richMenuId : string ,
168
+ ) : Promise < any > {
165
169
return this . http . post ( `/user/${ userId } /richmenu/${ richMenuId } ` ) ;
166
170
}
167
171
168
- public unlinkRichMenuFromUser ( userId : string ) : Promise < any > {
172
+ public async unlinkRichMenuFromUser ( userId : string ) : Promise < any > {
169
173
return this . http . delete ( `/user/${ userId } /richmenu` ) ;
170
174
}
171
175
172
- public linkRichMenuToMultipleUsers (
176
+ public async linkRichMenuToMultipleUsers (
173
177
richMenuId : string ,
174
178
userIds : string [ ] ,
175
179
) : Promise < any > {
@@ -179,17 +183,19 @@ export default class Client {
179
183
} ) ;
180
184
}
181
185
182
- public unlinkRichMenusFromMultipleUsers ( userIds : string [ ] ) : Promise < any > {
186
+ public async unlinkRichMenusFromMultipleUsers (
187
+ userIds : string [ ] ,
188
+ ) : Promise < any > {
183
189
return this . http . post ( "/richmenu/bulk/unlink" , {
184
190
userIds,
185
191
} ) ;
186
192
}
187
193
188
- public getRichMenuImage ( richMenuId : string ) : Promise < Readable > {
194
+ public async getRichMenuImage ( richMenuId : string ) : Promise < Readable > {
189
195
return this . http . getStream ( `/richmenu/${ richMenuId } /content` ) ;
190
196
}
191
197
192
- public setRichMenuImage (
198
+ public async setRichMenuImage (
193
199
richMenuId : string ,
194
200
data : Buffer | Readable ,
195
201
contentType ?: string ,
@@ -201,62 +207,53 @@ export default class Client {
201
207
) ;
202
208
}
203
209
204
- public getRichMenuList ( ) : Promise < Array < Types . RichMenuResponse > > {
205
- return this . http
206
- . get < any > ( `/richmenu/list` )
207
- . then ( checkJSON )
208
- . then ( res => res . richmenus ) ;
210
+ public async getRichMenuList ( ) : Promise < Array < Types . RichMenuResponse > > {
211
+ const res = await this . http . get < any > ( `/richmenu/list` ) ;
212
+ return ensureJSON ( res ) . richmenus ;
209
213
}
210
214
211
- public setDefaultRichMenu ( richMenuId : string ) : Promise < { } > {
215
+ public async setDefaultRichMenu ( richMenuId : string ) : Promise < { } > {
212
216
return this . http . post ( `/user/all/richmenu/${ richMenuId } ` ) ;
213
217
}
214
218
215
- public getDefaultRichMenuId ( ) : Promise < string > {
216
- return this . http
217
- . get < any > ( "/user/all/richmenu" )
218
- . then ( checkJSON )
219
- . then ( res => res . richMenuId ) ;
219
+ public async getDefaultRichMenuId ( ) : Promise < string > {
220
+ const res = await this . http . get < any > ( "/user/all/richmenu" ) ;
221
+ return ensureJSON ( res ) . richMenuId ;
220
222
}
221
223
222
- public deleteDefaultRichMenu ( ) : Promise < { } > {
224
+ public async deleteDefaultRichMenu ( ) : Promise < { } > {
223
225
return this . http . delete ( "/user/all/richmenu" ) ;
224
226
}
225
227
226
- public getLinkToken ( userId : string ) : Promise < string > {
227
- return this . http
228
- . post < any > ( `/user/${ userId } /linkToken` )
229
- . then ( checkJSON )
230
- . then ( res => res . linkToken ) ;
228
+ public async getLinkToken ( userId : string ) : Promise < string > {
229
+ const res = await this . http . post < any > ( `/user/${ userId } /linkToken` ) ;
230
+ return ensureJSON ( res ) . linkToken ;
231
231
}
232
232
233
- public getNumberOfSentReplyMessages (
233
+ public async getNumberOfSentReplyMessages (
234
234
date : string ,
235
235
) : Promise < Types . NumberOfMessagesSentResponse > {
236
- return this . http
237
- . get < Types . NumberOfMessagesSentResponse > (
238
- `/message/delivery/reply?date=${ date } ` ,
239
- )
240
- . then ( checkJSON ) ;
236
+ const res = await this . http . get < Types . NumberOfMessagesSentResponse > (
237
+ `/message/delivery/reply?date=${ date } ` ,
238
+ ) ;
239
+ return ensureJSON ( res ) ;
241
240
}
242
241
243
- public getNumberOfSentPushMessages (
242
+ public async getNumberOfSentPushMessages (
244
243
date : string ,
245
244
) : Promise < Types . NumberOfMessagesSentResponse > {
246
- return this . http
247
- . get < Types . NumberOfMessagesSentResponse > (
248
- `/message/delivery/push?date=${ date } ` ,
249
- )
250
- . then ( checkJSON ) ;
245
+ const res = await this . http . get < Types . NumberOfMessagesSentResponse > (
246
+ `/message/delivery/push?date=${ date } ` ,
247
+ ) ;
248
+ return ensureJSON ( res ) ;
251
249
}
252
250
253
- public getNumberOfSentMulticastMessages (
251
+ public async getNumberOfSentMulticastMessages (
254
252
date : string ,
255
253
) : Promise < Types . NumberOfMessagesSentResponse > {
256
- return this . http
257
- . get < Types . NumberOfMessagesSentResponse > (
258
- `/message/delivery/multicast?date=${ date } ` ,
259
- )
260
- . then ( checkJSON ) ;
254
+ const res = await this . http . get < Types . NumberOfMessagesSentResponse > (
255
+ `/message/delivery/multicast?date=${ date } ` ,
256
+ ) ;
257
+ return ensureJSON ( res ) ;
261
258
}
262
259
}
0 commit comments