Skip to content

Commit 283ab8a

Browse files
committed
move checkJSON into client.ts
1 parent 5ad57c2 commit 283ab8a

File tree

3 files changed

+48
-56
lines changed

3 files changed

+48
-56
lines changed

lib/client.ts

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { get, post, stream, del, postBinary } from "./http";
33
import * as Types from "./types";
44
import * as URL from "./urls";
55
import { toArray, detectContentType } from "./util";
6+
import { JSONParseError } from "./exceptions";
67

78
export default class Client {
89
public config: Types.ClientConfig;
@@ -22,7 +23,7 @@ export default class Client {
2223
return this.post(URL.push, {
2324
messages: toArray(messages),
2425
to,
25-
});
26+
}) /*.then(this.checkJSON)*/;
2627
}
2728

2829
public replyMessage(
@@ -32,7 +33,7 @@ export default class Client {
3233
return this.post(URL.reply, {
3334
messages: toArray(messages),
3435
replyToken,
35-
});
36+
}).then(this.checkJSON);
3637
}
3738

3839
public multicast(
@@ -42,52 +43,58 @@ export default class Client {
4243
return this.post(URL.multicast, {
4344
messages: toArray(messages),
4445
to,
45-
});
46+
}).then(this.checkJSON);
4647
}
4748

4849
public getProfile(userId: string): Promise<Types.Profile> {
49-
return this.get(URL.profile(userId));
50+
return this.get(URL.profile(userId)).then(this.checkJSON);
5051
}
5152

5253
public getGroupMemberProfile(
5354
groupId: string,
5455
userId: string,
5556
): Promise<Types.Profile> {
56-
return this.get(URL.groupMemberProfile(groupId, userId));
57+
return this.get(URL.groupMemberProfile(groupId, userId)).then(
58+
this.checkJSON,
59+
);
5760
}
5861

5962
public getRoomMemberProfile(
6063
roomId: string,
6164
userId: string,
6265
): Promise<Types.Profile> {
63-
return this.get(URL.roomMemberProfile(roomId, userId));
66+
return this.get(URL.roomMemberProfile(roomId, userId)).then(this.checkJSON);
6467
}
6568

6669
public getGroupMemberIds(groupId: string): Promise<string[]> {
6770
const load = (start?: string): Promise<string[]> =>
68-
this.get(
69-
URL.groupMemberIds(groupId, start),
70-
).then((res: { memberIds: string[]; next?: string }) => {
71-
if (!res.next) {
72-
return res.memberIds;
73-
}
74-
75-
return load(res.next).then(extraIds => res.memberIds.concat(extraIds));
76-
});
71+
this.get(URL.groupMemberIds(groupId, start))
72+
.then(this.checkJSON)
73+
.then((res: { memberIds: string[]; next?: string }) => {
74+
if (!res.next) {
75+
return res.memberIds;
76+
}
77+
78+
return load(res.next).then(extraIds =>
79+
res.memberIds.concat(extraIds),
80+
);
81+
});
7782
return load();
7883
}
7984

8085
public getRoomMemberIds(roomId: string): Promise<string[]> {
8186
const load = (start?: string): Promise<string[]> =>
82-
this.get(
83-
URL.roomMemberIds(roomId, start),
84-
).then((res: { memberIds: string[]; next?: string }) => {
85-
if (!res.next) {
86-
return res.memberIds;
87-
}
88-
89-
return load(res.next).then(extraIds => res.memberIds.concat(extraIds));
90-
});
87+
this.get(URL.roomMemberIds(roomId, start))
88+
.then(this.checkJSON)
89+
.then((res: { memberIds: string[]; next?: string }) => {
90+
if (!res.next) {
91+
return res.memberIds;
92+
}
93+
94+
return load(res.next).then(extraIds =>
95+
res.memberIds.concat(extraIds),
96+
);
97+
});
9198
return load();
9299
}
93100

@@ -96,32 +103,31 @@ export default class Client {
96103
}
97104

98105
public leaveGroup(groupId: string): Promise<any> {
99-
return this.post(URL.leaveGroup(groupId));
106+
return this.post(URL.leaveGroup(groupId)).then(this.checkJSON);
100107
}
101108

102109
public leaveRoom(roomId: string): Promise<any> {
103-
return this.post(URL.leaveRoom(roomId));
110+
return this.post(URL.leaveRoom(roomId)).then(this.checkJSON);
104111
}
105112

106113
public getRichMenu(
107114
richMenuId: string,
108115
): Promise<Types.RichMenuId & Types.RichMenu> {
109-
return this.get(URL.richMenu(richMenuId));
116+
return this.get(URL.richMenu(richMenuId)).then(this.checkJSON);
110117
}
111118

112119
public createRichMenu(richMenu: Types.RichMenu): Promise<Types.RichMenuId> {
113-
return this.post(URL.richMenu(), richMenu);
120+
return this.post(URL.richMenu(), richMenu).then(this.checkJSON);
114121
}
115122

116123
public deleteRichMenu(richMenuId: string): Promise<any> {
117124
return this.delete(URL.richMenu(richMenuId));
118125
}
119126

120127
public getUserRichMenuIds(userId: string): Promise<Types.RichMenuId> {
121-
return this.get(URL.userRichMenu(userId));
128+
return this.get(URL.userRichMenu(userId)).then(this.checkJSON);
122129
}
123130

124-
// TODO: change return type to appropriate one
125131
public linkRichMenuToUser(userId: string, richMenuId: string): Promise<any> {
126132
return this.post(URL.userRichMenu(userId, richMenuId));
127133
}
@@ -146,7 +152,7 @@ export default class Client {
146152
}
147153

148154
public getRichMenuList(): Promise<Array<Types.RichMenuId & Types.RichMenu>> {
149-
return this.get(URL.richMenuList());
155+
return this.get(URL.richMenuList()).then(this.checkJSON);
150156
}
151157

152158
private authHeader(): { [key: string]: string } {
@@ -176,4 +182,12 @@ export default class Client {
176182
private stream(url: string): Promise<Readable> {
177183
return stream(url, this.authHeader());
178184
}
185+
186+
private checkJSON(raw: any): any {
187+
if (typeof raw === "object") {
188+
return raw;
189+
} else {
190+
throw new JSONParseError("Failed to parse response body as JSON", raw);
191+
}
192+
}
179193
}

lib/http.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
import axios, { AxiosError } from "axios";
22
import { detectContentType } from "./util";
33
import { Readable } from "stream";
4-
import {
5-
HTTPError,
6-
JSONParseError,
7-
ReadError,
8-
RequestError,
9-
} from "./exceptions";
4+
import { HTTPError, ReadError, RequestError } from "./exceptions";
105

116
const pkg = require("../package.json");
127

13-
function checkJSON(raw: any): any {
14-
if (typeof raw === "object") {
15-
return raw;
16-
} else {
17-
throw new JSONParseError("Failed to parse response body as JSON", raw);
18-
}
19-
}
20-
218
function wrapError(err: AxiosError) {
229
if (err.response) {
2310
throw new HTTPError(
@@ -51,7 +38,7 @@ export function get(url: string, headers: any): Promise<any> {
5138

5239
return axios
5340
.get(url, { headers })
54-
.then(res => checkJSON(res.data))
41+
.then(res => res.data)
5542
.catch(wrapError);
5643
}
5744

@@ -60,7 +47,7 @@ export function post(url: string, headers: any, data?: any): Promise<any> {
6047
headers["User-Agent"] = userAgent;
6148
return axios
6249
.post(url, data, { headers })
63-
.then(res => checkJSON(res.data))
50+
.then(res => res.data)
6451
.catch(wrapError);
6552
}
6653

test/http.spec.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,6 @@ describe("http", () => {
110110
});
111111
});
112112

113-
it("fail to parse json", () => {
114-
return get(`${TEST_URL}/text`, {})
115-
.then(() => ok(false))
116-
.catch(err => {
117-
ok(err instanceof JSONParseError);
118-
equal(err.raw, "i am not jason");
119-
});
120-
});
121-
122113
it("fail with 404", () => {
123114
return get(`${TEST_URL}/404`, {})
124115
.then(() => ok(false))

0 commit comments

Comments
 (0)