Skip to content

Commit 03ce3c5

Browse files
committed
set appropriate return type in client.ts
1 parent ec5e3cf commit 03ce3c5

File tree

5 files changed

+35
-33
lines changed

5 files changed

+35
-33
lines changed

lib/client.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default class Client {
9191
return load();
9292
}
9393

94-
public getMessageContent(messageId: string): Promise<NodeJS.ReadableStream> {
94+
public getMessageContent(messageId: string): Promise<Readable> {
9595
return this.stream(URL.content(messageId));
9696
}
9797

@@ -103,22 +103,25 @@ export default class Client {
103103
return this.post(URL.leaveRoom(roomId));
104104
}
105105

106-
public getRichMenu(richMenuId: string): Promise<any> {
106+
public getRichMenu(
107+
richMenuId: string,
108+
): Promise<Types.RichMenuId & Types.RichMenu> {
107109
return this.get(URL.richMenu(richMenuId));
108110
}
109111

110-
public createRichMenu(richMenu: Types.RichMenu): Promise<any> {
112+
public createRichMenu(richMenu: Types.RichMenu): Promise<Types.RichMenuId> {
111113
return this.post(URL.richMenu(), richMenu);
112114
}
113115

114116
public deleteRichMenu(richMenuId: string): Promise<any> {
115117
return this.delete(URL.richMenu(richMenuId));
116118
}
117119

118-
public getUserRichMenuIds(userId: string): Promise<any> {
120+
public getUserRichMenuIds(userId: string): Promise<Types.RichMenuId> {
119121
return this.get(URL.userRichMenu(userId));
120122
}
121123

124+
// TODO: change return type to appropriate one
122125
public linkRichMenuToUser(userId: string, richMenuId: string): Promise<any> {
123126
return this.post(URL.userRichMenu(userId, richMenuId));
124127
}
@@ -130,7 +133,7 @@ export default class Client {
130133
return this.delete(URL.userRichMenu(userId, richMenuId));
131134
}
132135

133-
public getRichMenuImage(richMenuId: string): Promise<any> {
136+
public getRichMenuImage(richMenuId: string): Promise<Readable> {
134137
return this.stream(URL.richMenuContent(richMenuId));
135138
}
136139

@@ -170,7 +173,7 @@ export default class Client {
170173
return postBinary(url, this.authHeader(), data, contentType);
171174
}
172175

173-
private stream(url: string): Promise<NodeJS.ReadableStream> {
176+
private stream(url: string): Promise<Readable> {
174177
return stream(url, this.authHeader());
175178
}
176179
}

lib/http.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ function wrapError(err: AxiosError) {
3939

4040
const userAgent = `${pkg.name}/${pkg.version}`;
4141

42-
export function stream(
43-
url: string,
44-
headers: any,
45-
): Promise<NodeJS.ReadableStream> {
42+
export function stream(url: string, headers: any): Promise<Readable> {
4643
headers["User-Agent"] = userAgent;
4744
return axios
4845
.get(url, { headers, responseType: "stream" })
49-
.then(res => res.data as NodeJS.ReadableStream);
46+
.then(res => res.data as Readable);
5047
}
5148

5249
export function get(url: string, headers: any): Promise<any> {
@@ -72,24 +69,24 @@ export function postBinary(
7269
headers: any,
7370
data: Buffer | Readable,
7471
contentType?: string,
75-
): Promise<Readable> {
72+
): Promise<any> {
7673
return new Promise(resolve => {
7774
return resolve(contentType ? contentType : detectContentType(data));
7875
}).then((contentType: string) => {
7976
headers["Content-Type"] = contentType;
8077
headers["User-Agent"] = userAgent;
81-
const responseType = "stream";
8278
return axios
83-
.post(url, data, { headers, responseType })
84-
.then(res => res.data);
79+
.post(url, data, { headers, responseType: "text" })
80+
.then(res => res.data)
81+
.catch(wrapError);
8582
});
8683
}
8784

8885
export function del(url: string, headers: any): Promise<any> {
8986
headers["User-Agent"] = userAgent;
9087

9188
return axios
92-
.delete(url, { headers })
93-
.then(res => checkJSON(res.data))
89+
.delete(url, { headers, responseType: "text" })
90+
.then(res => res.data)
9491
.catch(wrapError);
9592
}

test/client.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { readFileSync } from "fs";
22
import { join } from "path";
33
import { deepEqual, equal } from "assert";
4+
import { Readable } from "stream";
45
import Client from "../lib/client";
56
import * as Types from "../lib/types";
67
import { getStreamData } from "./helpers/stream";
@@ -144,7 +145,7 @@ describe("client", () => {
144145
it("getMessageContent", () => {
145146
return client
146147
.getMessageContent("test_message_id")
147-
.then((s: NodeJS.ReadableStream) => getStreamData(s))
148+
.then((s: Readable) => getStreamData(s))
148149
.then((data: string) => {
149150
const res = JSON.parse(data);
150151
equal(res.headers.authorization, "Bearer test_channel_access_token");
@@ -226,9 +227,7 @@ describe("client", () => {
226227
const buffer = readFileSync(filepath);
227228
return client
228229
.setRichMenuImage("test_rich_menu_id", buffer)
229-
.then(s => getStreamData(s))
230-
.then((data: string) => {
231-
const res = JSON.parse(data);
230+
.then((res: any) => {
232231
equal(res.headers.authorization, "Bearer test_channel_access_token");
233232
equal(res.path, "/richmenu/test_rich_menu_id/content");
234233
equal(res.method, "POST");
@@ -238,7 +237,7 @@ describe("client", () => {
238237
it("getRichMenuImage", () => {
239238
return client
240239
.getRichMenuImage("test_rich_menu_id")
241-
.then((s: NodeJS.ReadableStream) => getStreamData(s))
240+
.then((s: Readable) => getStreamData(s))
242241
.then((data: string) => {
243242
const res = JSON.parse(data);
244243
equal(res.headers.authorization, "Bearer test_channel_access_token");

test/helpers/stream.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export function getStreamData(stream: NodeJS.ReadableStream): Promise<string> {
1+
import { Readable } from "stream";
2+
3+
export function getStreamData(stream: Readable): Promise<string> {
24
return new Promise(resolve => {
35
let result: string = "";
46
stream.on("data", (chunk: Buffer) => {

test/http.spec.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,17 @@ describe("http", () => {
9797

9898
const filepath = join(__dirname, "/helpers/LINE_Icon.png");
9999
const buffer = readFileSync(filepath);
100-
return postBinary(`${TEST_URL}/post`, testHeaders, buffer)
101-
.then(s => getStreamData(s))
102-
.then((data: string) => {
103-
const res = JSON.parse(data);
104-
equal(res.method, "POST");
105-
equal(res.path, "/post");
106-
equal(res.headers["test-header-key"], testHeaders["test-header-key"]);
107-
equal(res.headers["user-agent"], `${pkg.name}/${pkg.version}`);
108-
equal(res.headers["content-type"], "image/png");
109-
});
100+
return postBinary(
101+
`${TEST_URL}/post`,
102+
testHeaders,
103+
buffer,
104+
).then((res: any) => {
105+
equal(res.method, "POST");
106+
equal(res.path, "/post");
107+
equal(res.headers["test-header-key"], testHeaders["test-header-key"]);
108+
equal(res.headers["user-agent"], `${pkg.name}/${pkg.version}`);
109+
equal(res.headers["content-type"], "image/png");
110+
});
110111
});
111112

112113
it("fail to parse json", () => {

0 commit comments

Comments
 (0)