Skip to content

Commit d18af23

Browse files
Junkawasako
authored andcommitted
Node version up and use async await (#139)
* Drop Node.js 6 and add support for Node 12 Please refer to the Node.js LTS schedule. https://nodejs.org/en/about/releases/ * Use async-await Resolve #126. * Use async-await in tests * Do not compile async-await into ES2015 * Upgrade npm version for Travis jobs To support Node.js 12.
1 parent aa3f047 commit d18af23

File tree

10 files changed

+570
-626
lines changed

10 files changed

+570
-626
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: node_js
22
node_js:
3-
- 6
43
- 8
54
- 10
5+
- 12
66
before_install:
7-
- npm i -g npm@6.4.1
7+
- npm i -g npm@6.9.0
88
install:
99
- npm ci

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ reading them beforehand.
3333

3434
## Requirements
3535

36-
* **Node.js** 6 or higher
36+
* **Node.js** 8 or higher
3737

3838
## Contributing
3939

lib/client.ts

Lines changed: 87 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function toArray<T>(maybeArr: T | T[]): T[] {
77
return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
88
}
99

10-
function checkJSON<T>(raw: T): T {
10+
function ensureJSON<T>(raw: T): T {
1111
if (typeof raw === "object") {
1212
return raw;
1313
} else {
@@ -35,7 +35,7 @@ export default class Client {
3535
);
3636
}
3737

38-
public pushMessage(
38+
public async pushMessage(
3939
to: string,
4040
messages: Types.Message | Types.Message[],
4141
): Promise<any> {
@@ -45,7 +45,7 @@ export default class Client {
4545
});
4646
}
4747

48-
public replyMessage(
48+
public async replyMessage(
4949
replyToken: string,
5050
messages: Types.Message | Types.Message[],
5151
): Promise<any> {
@@ -55,7 +55,7 @@ export default class Client {
5555
});
5656
}
5757

58-
public multicast(
58+
public async multicast(
5959
to: string[],
6060
messages: Types.Message | Types.Message[],
6161
): Promise<any> {
@@ -65,111 +65,115 @@ export default class Client {
6565
});
6666
}
6767

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);
7071
}
7172

72-
private getChatMemberProfile(
73+
private async getChatMemberProfile(
7374
chatType: ChatType,
7475
chatId: string,
7576
userId: string,
7677
): 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);
8082
}
8183

82-
public getGroupMemberProfile(
84+
public async getGroupMemberProfile(
8385
groupId: string,
8486
userId: string,
8587
): Promise<Types.Profile> {
8688
return this.getChatMemberProfile("group", groupId, userId);
8789
}
8890

89-
public getRoomMemberProfile(
91+
public async getRoomMemberProfile(
9092
roomId: string,
9193
userId: string,
9294
): Promise<Types.Profile> {
9395
return this.getChatMemberProfile("room", roomId, userId);
9496
}
9597

96-
private getChatMemberIds(
98+
private async getChatMemberIds(
9799
chatType: ChatType,
98100
chatId: string,
99101
): 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[]> {
117119
return this.getChatMemberIds("group", groupId);
118120
}
119121

120-
public getRoomMemberIds(roomId: string): Promise<string[]> {
122+
public async getRoomMemberIds(roomId: string): Promise<string[]> {
121123
return this.getChatMemberIds("room", roomId);
122124
}
123125

124-
public getMessageContent(messageId: string): Promise<Readable> {
126+
public async getMessageContent(messageId: string): Promise<Readable> {
125127
return this.http.getStream(`/message/${messageId}/content`);
126128
}
127129

128130
private leaveChat(chatType: ChatType, chatId: string): Promise<any> {
129131
return this.http.post(`/${chatType}/${chatId}/leave`);
130132
}
131133

132-
public leaveGroup(groupId: string): Promise<any> {
134+
public async leaveGroup(groupId: string): Promise<any> {
133135
return this.leaveChat("group", groupId);
134136
}
135137

136-
public leaveRoom(roomId: string): Promise<any> {
138+
public async leaveRoom(roomId: string): Promise<any> {
137139
return this.leaveChat("room", roomId);
138140
}
139141

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);
144149
}
145150

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;
151154
}
152155

153-
public deleteRichMenu(richMenuId: string): Promise<any> {
156+
public async deleteRichMenu(richMenuId: string): Promise<any> {
154157
return this.http.delete(`/richmenu/${richMenuId}`);
155158
}
156159

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;
162163
}
163164

164-
public linkRichMenuToUser(userId: string, richMenuId: string): Promise<any> {
165+
public async linkRichMenuToUser(
166+
userId: string,
167+
richMenuId: string,
168+
): Promise<any> {
165169
return this.http.post(`/user/${userId}/richmenu/${richMenuId}`);
166170
}
167171

168-
public unlinkRichMenuFromUser(userId: string): Promise<any> {
172+
public async unlinkRichMenuFromUser(userId: string): Promise<any> {
169173
return this.http.delete(`/user/${userId}/richmenu`);
170174
}
171175

172-
public linkRichMenuToMultipleUsers(
176+
public async linkRichMenuToMultipleUsers(
173177
richMenuId: string,
174178
userIds: string[],
175179
): Promise<any> {
@@ -179,17 +183,19 @@ export default class Client {
179183
});
180184
}
181185

182-
public unlinkRichMenusFromMultipleUsers(userIds: string[]): Promise<any> {
186+
public async unlinkRichMenusFromMultipleUsers(
187+
userIds: string[],
188+
): Promise<any> {
183189
return this.http.post("/richmenu/bulk/unlink", {
184190
userIds,
185191
});
186192
}
187193

188-
public getRichMenuImage(richMenuId: string): Promise<Readable> {
194+
public async getRichMenuImage(richMenuId: string): Promise<Readable> {
189195
return this.http.getStream(`/richmenu/${richMenuId}/content`);
190196
}
191197

192-
public setRichMenuImage(
198+
public async setRichMenuImage(
193199
richMenuId: string,
194200
data: Buffer | Readable,
195201
contentType?: string,
@@ -201,62 +207,53 @@ export default class Client {
201207
);
202208
}
203209

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;
209213
}
210214

211-
public setDefaultRichMenu(richMenuId: string): Promise<{}> {
215+
public async setDefaultRichMenu(richMenuId: string): Promise<{}> {
212216
return this.http.post(`/user/all/richmenu/${richMenuId}`);
213217
}
214218

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;
220222
}
221223

222-
public deleteDefaultRichMenu(): Promise<{}> {
224+
public async deleteDefaultRichMenu(): Promise<{}> {
223225
return this.http.delete("/user/all/richmenu");
224226
}
225227

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;
231231
}
232232

233-
public getNumberOfSentReplyMessages(
233+
public async getNumberOfSentReplyMessages(
234234
date: string,
235235
): 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);
241240
}
242241

243-
public getNumberOfSentPushMessages(
242+
public async getNumberOfSentPushMessages(
244243
date: string,
245244
): 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);
251249
}
252250

253-
public getNumberOfSentMulticastMessages(
251+
public async getNumberOfSentMulticastMessages(
254252
date: string,
255253
): 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);
261258
}
262259
}

0 commit comments

Comments
 (0)