Skip to content

Commit c79d65c

Browse files
author
Hyunje Jun
authored
Refactorings (#94)
* Upgrade TypeScript to 3.1.6 * Remove unused imports * Import package.json as a TypeScript module * Refactor HTTP client usage Made HTTP client a class and simplify its usage in client module. Also removed lib/urls, because it's only used in client module and adds complexity.
1 parent 7abf887 commit c79d65c

File tree

10 files changed

+253
-307
lines changed

10 files changed

+253
-307
lines changed

lib/client.ts

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { Readable } from "stream";
2-
import { get, post, stream, del, postBinary } from "./http";
2+
import HTTPClient from "./http";
33
import * as Types from "./types";
4-
import * as URL from "./urls";
54
import { JSONParseError } from "./exceptions";
65

76
function toArray<T>(maybeArr: T | T[]): T[] {
87
return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
98
}
109

11-
function checkJSON(raw: any): any {
10+
function checkJSON<T>(raw: T): T {
1211
if (typeof raw === "object") {
1312
return raw;
1413
} else {
@@ -18,20 +17,27 @@ function checkJSON(raw: any): any {
1817

1918
export default class Client {
2019
public config: Types.ClientConfig;
20+
private http: HTTPClient;
2121

2222
constructor(config: Types.ClientConfig) {
2323
if (!config.channelAccessToken) {
2424
throw new Error("no channel access token");
2525
}
2626

2727
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+
);
2834
}
2935

3036
public pushMessage(
3137
to: string,
3238
messages: Types.Message | Types.Message[],
3339
): Promise<any> {
34-
return this.post(URL.push, {
40+
return this.http.post("/message/push", {
3541
messages: toArray(messages),
3642
to,
3743
});
@@ -41,7 +47,7 @@ export default class Client {
4147
replyToken: string,
4248
messages: Types.Message | Types.Message[],
4349
): Promise<any> {
44-
return this.post(URL.reply, {
50+
return this.http.post("/message/reply", {
4551
messages: toArray(messages),
4652
replyToken,
4753
});
@@ -51,33 +57,38 @@ export default class Client {
5157
to: string[],
5258
messages: Types.Message | Types.Message[],
5359
): Promise<any> {
54-
return this.post(URL.multicast, {
60+
return this.http.post("/message/multicast", {
5561
messages: toArray(messages),
5662
to,
5763
});
5864
}
5965

6066
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);
6268
}
6369

6470
public getGroupMemberProfile(
6571
groupId: string,
6672
userId: string,
6773
): 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);
6977
}
7078

7179
public getRoomMemberProfile(
7280
roomId: string,
7381
userId: string,
7482
): 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);
7686
}
7787

7888
public getGroupMemberIds(groupId: string): Promise<string[]> {
7989
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)
8192
.then(checkJSON)
8293
.then((res: { memberIds: string[]; next?: string }) => {
8394
if (!res.next) {
@@ -93,7 +104,8 @@ export default class Client {
93104

94105
public getRoomMemberIds(roomId: string): Promise<string[]> {
95106
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)
97109
.then(checkJSON)
98110
.then((res: { memberIds: string[]; next?: string }) => {
99111
if (!res.next) {
@@ -108,102 +120,84 @@ export default class Client {
108120
}
109121

110122
public getMessageContent(messageId: string): Promise<Readable> {
111-
return this.stream(URL.content(messageId));
123+
return this.http.getStream(`/message/${messageId}/content`);
112124
}
113125

114126
public leaveGroup(groupId: string): Promise<any> {
115-
return this.post(URL.leaveGroup(groupId));
127+
return this.http.post(`/group/${groupId}/leave`);
116128
}
117129

118130
public leaveRoom(roomId: string): Promise<any> {
119-
return this.post(URL.leaveRoom(roomId));
131+
return this.http.post(`/room/${roomId}/leave`);
120132
}
121133

122134
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);
124138
}
125139

126140
public createRichMenu(richMenu: Types.RichMenu): Promise<string> {
127-
return this.post(URL.richMenu(), richMenu)
141+
return this.http
142+
.post<any>("/richmenu", richMenu)
128143
.then(checkJSON)
129144
.then(res => res.richMenuId);
130145
}
131146

132147
public deleteRichMenu(richMenuId: string): Promise<any> {
133-
return this.delete(URL.richMenu(richMenuId));
148+
return this.http.delete(`/richmenu/${richMenuId}`);
134149
}
135150

136151
public getRichMenuIdOfUser(userId: string): Promise<string> {
137-
return this.get(URL.userRichMenu(userId))
152+
return this.http
153+
.get<any>(`/user/${userId}/richmenu`)
138154
.then(checkJSON)
139155
.then(res => res.richMenuId);
140156
}
141157

142158
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}`);
144160
}
145161

146162
public unlinkRichMenuFromUser(userId: string): Promise<any> {
147-
return this.delete(URL.userRichMenu(userId));
163+
return this.http.delete(`/user/${userId}/richmenu`);
148164
}
149165

150166
public getRichMenuImage(richMenuId: string): Promise<Readable> {
151-
return this.stream(URL.richMenuContent(richMenuId));
167+
return this.http.getStream(`/richmenu/${richMenuId}/content`);
152168
}
153169

154170
public setRichMenuImage(
155171
richMenuId: string,
156172
data: Buffer | Readable,
157173
contentType?: string,
158174
): 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+
);
160180
}
161181

162182
public getRichMenuList(): Promise<Array<Types.RichMenuResponse>> {
163-
return this.get(URL.richMenuList())
183+
return this.http
184+
.get<any>(`/richmenu/list`)
164185
.then(checkJSON)
165186
.then(res => res.richmenus);
166187
}
167188

168189
public setDefaultRichMenu(richMenuId: string): Promise<{}> {
169-
return this.post(URL.defaultRichMenu(richMenuId));
190+
return this.http.post(`/user/all/richmenu/${richMenuId}`);
170191
}
171192

172193
public getDefaultRichMenuId(): Promise<string> {
173-
return this.get(URL.defaultRichMenu())
194+
return this.http
195+
.get<any>("/user/all/richmenu")
174196
.then(checkJSON)
175197
.then(res => res.richMenuId);
176198
}
177199

178200
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");
208202
}
209203
}

0 commit comments

Comments
 (0)