Skip to content

Commit ffbb736

Browse files
author
Hyunje Jun
committed
Delete util.ts and inline methods
This also fixes postBinary issue with stream.
1 parent 7516f23 commit ffbb736

File tree

4 files changed

+45
-79
lines changed

4 files changed

+45
-79
lines changed

lib/client.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ import { Readable } from "stream";
22
import { get, post, stream, del, postBinary } from "./http";
33
import * as Types from "./types";
44
import * as URL from "./urls";
5-
import { toArray, detectContentType } from "./util";
65
import { JSONParseError } from "./exceptions";
76

7+
function toArray<T>(maybeArr: T | T[]): T[] {
8+
return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
9+
}
10+
11+
function checkJSON(raw: any): any {
12+
if (typeof raw === "object") {
13+
return raw;
14+
} else {
15+
throw new JSONParseError("Failed to parse response body as JSON", raw);
16+
}
17+
}
18+
819
export default class Client {
920
public config: Types.ClientConfig;
1021

@@ -47,29 +58,27 @@ export default class Client {
4758
}
4859

4960
public getProfile(userId: string): Promise<Types.Profile> {
50-
return this.get(URL.profile(userId)).then(this.checkJSON);
61+
return this.get(URL.profile(userId)).then(checkJSON);
5162
}
5263

5364
public getGroupMemberProfile(
5465
groupId: string,
5566
userId: string,
5667
): Promise<Types.Profile> {
57-
return this.get(URL.groupMemberProfile(groupId, userId)).then(
58-
this.checkJSON,
59-
);
68+
return this.get(URL.groupMemberProfile(groupId, userId)).then(checkJSON);
6069
}
6170

6271
public getRoomMemberProfile(
6372
roomId: string,
6473
userId: string,
6574
): Promise<Types.Profile> {
66-
return this.get(URL.roomMemberProfile(roomId, userId)).then(this.checkJSON);
75+
return this.get(URL.roomMemberProfile(roomId, userId)).then(checkJSON);
6776
}
6877

6978
public getGroupMemberIds(groupId: string): Promise<string[]> {
7079
const load = (start?: string): Promise<string[]> =>
7180
this.get(URL.groupMemberIds(groupId, start))
72-
.then(this.checkJSON)
81+
.then(checkJSON)
7382
.then((res: { memberIds: string[]; next?: string }) => {
7483
if (!res.next) {
7584
return res.memberIds;
@@ -85,7 +94,7 @@ export default class Client {
8594
public getRoomMemberIds(roomId: string): Promise<string[]> {
8695
const load = (start?: string): Promise<string[]> =>
8796
this.get(URL.roomMemberIds(roomId, start))
88-
.then(this.checkJSON)
97+
.then(checkJSON)
8998
.then((res: { memberIds: string[]; next?: string }) => {
9099
if (!res.next) {
91100
return res.memberIds;
@@ -113,19 +122,19 @@ export default class Client {
113122
public getRichMenu(
114123
richMenuId: string,
115124
): Promise<Types.RichMenuId & Types.RichMenu> {
116-
return this.get(URL.richMenu(richMenuId)).then(this.checkJSON);
125+
return this.get(URL.richMenu(richMenuId)).then(checkJSON);
117126
}
118127

119128
public createRichMenu(richMenu: Types.RichMenu): Promise<Types.RichMenuId> {
120-
return this.post(URL.richMenu(), richMenu).then(this.checkJSON);
129+
return this.post(URL.richMenu(), richMenu).then(checkJSON);
121130
}
122131

123132
public deleteRichMenu(richMenuId: string): Promise<any> {
124133
return this.delete(URL.richMenu(richMenuId));
125134
}
126135

127136
public getUserRichMenuIds(userId: string): Promise<Types.RichMenuId> {
128-
return this.get(URL.userRichMenu(userId)).then(this.checkJSON);
137+
return this.get(URL.userRichMenu(userId)).then(checkJSON);
129138
}
130139

131140
public linkRichMenuToUser(userId: string, richMenuId: string): Promise<any> {
@@ -152,7 +161,7 @@ export default class Client {
152161
}
153162

154163
public getRichMenuList(): Promise<Array<Types.RichMenuId & Types.RichMenu>> {
155-
return this.get(URL.richMenuList()).then(this.checkJSON);
164+
return this.get(URL.richMenuList()).then(checkJSON);
156165
}
157166

158167
private authHeader(): { [key: string]: string } {
@@ -182,12 +191,4 @@ export default class Client {
182191
private stream(url: string): Promise<Readable> {
183192
return stream(url, this.authHeader());
184193
}
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-
}
193194
}

lib/http.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import axios, { AxiosError } from "axios";
2-
import { detectContentType } from "./util";
3-
import { Readable } from "stream";
2+
import { Readable, PassThrough } from "stream";
43
import { HTTPError, ReadError, RequestError } from "./exceptions";
4+
import * as fileType from "file-type";
5+
6+
const fileTypeStream = require("file-type-stream").default;
57

68
const pkg = require("../package.json");
79

@@ -57,9 +59,26 @@ export function postBinary(
5759
data: Buffer | Readable,
5860
contentType?: string,
5961
): Promise<any> {
60-
return new Promise(resolve => {
61-
return resolve(contentType ? contentType : detectContentType(data));
62-
}).then((contentType: string) => {
62+
let contentTypeGetter;
63+
if (contentType) {
64+
contentTypeGetter = Promise.resolve(contentType);
65+
} else if (Buffer.isBuffer(data)) {
66+
contentTypeGetter = Promise.resolve(fileType(data).mime);
67+
} else {
68+
contentTypeGetter = new Promise(resolve => {
69+
if (data instanceof Readable) {
70+
const passThrough = new PassThrough();
71+
data
72+
.pipe(fileTypeStream((result: any) => resolve(result.mime)))
73+
.pipe(passThrough);
74+
data = passThrough;
75+
} else {
76+
throw new Error("invalid data type for postBinary");
77+
}
78+
});
79+
}
80+
81+
return contentTypeGetter.then((contentType: string) => {
6382
headers["Content-Type"] = contentType;
6483
headers["User-Agent"] = userAgent;
6584
return axios

lib/util.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/util.spec.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)