Skip to content

Commit c7a9b0e

Browse files
author
Hyunje Jun
committed
Replace got with axios
1 parent 8c3ea0f commit c7a9b0e

File tree

6 files changed

+68
-64
lines changed

6 files changed

+68
-64
lines changed

lib/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default class Client {
4646
return this.get(URL.roomMemberProfile(roomId, userId));
4747
}
4848

49-
public getMessageContent(messageId: string): NodeJS.ReadableStream {
49+
public getMessageContent(messageId: string): Promise<NodeJS.ReadableStream> {
5050
return this.stream(URL.content(messageId));
5151
}
5252

@@ -70,7 +70,7 @@ export default class Client {
7070
return post(url, this.authHeader(), body);
7171
}
7272

73-
private stream(url: string): NodeJS.ReadableStream {
73+
private stream(url: string): Promise<NodeJS.ReadableStream> {
7474
return stream(url, this.authHeader());
7575
}
7676
}

lib/exceptions.ts

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,46 @@
1-
import * as got from "got";
2-
31
export class SignatureValidationFailed extends Error {
4-
public signature: string;
5-
6-
constructor(msg: string, signature: string) {
7-
super(msg);
8-
this.signature = signature;
2+
constructor(
3+
message: string,
4+
public signature: string,
5+
) {
6+
super(message);
97
}
108
}
119

1210
export class JSONParseError extends Error {
13-
public raw: string;
14-
15-
constructor(msg: string, raw: string) {
16-
super(msg);
17-
this.raw = raw;
11+
constructor(
12+
message: string,
13+
public raw: any,
14+
) {
15+
super(message);
1816
}
1917
}
2018

2119
export class RequestError extends Error {
22-
public code: string;
23-
private origin: Error;
24-
25-
constructor(gotErr: RequestError) {
26-
super(gotErr.message);
27-
this.code = gotErr.code;
28-
this.origin = gotErr;
20+
constructor(
21+
message: string,
22+
public code: string,
23+
private originalError: Error,
24+
) {
25+
super(message);
2926
}
3027
}
3128

3229
export class ReadError extends Error {
33-
private origin: Error;
34-
35-
constructor(gotErr: ReadError) {
36-
super(gotErr.message);
37-
this.origin = gotErr;
30+
constructor(
31+
private originalError: Error,
32+
) {
33+
super(originalError.message);
3834
}
3935
}
4036

4137
export class HTTPError extends Error {
42-
public statusCode: number;
43-
public statusMessage: string;
44-
private origin: Error;
45-
46-
constructor(gotErr: HTTPError) {
47-
super(gotErr.message);
48-
this.statusCode = gotErr.statusCode;
49-
this.statusMessage = gotErr.statusMessage;
50-
this.origin = gotErr;
38+
constructor(
39+
message: string,
40+
public statusCode: number,
41+
public statusMessage: string,
42+
private originalError: Error,
43+
) {
44+
super(message);
5145
}
5246
}

lib/http.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as got from "got";
1+
import axios, { AxiosError } from "axios";
22
import {
33
HTTPError,
44
JSONParseError,
@@ -8,21 +8,31 @@ import {
88

99
const pkg = require("../package.json"); // tslint:disable-line no-var-requires
1010

11-
function parseJSON(raw: string): any {
12-
try {
13-
return JSON.parse(raw);
14-
} catch (err) {
15-
throw new JSONParseError(err.message, raw);
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);
1616
}
1717
}
1818

19-
function wrapError(err: Error) {
20-
if (err instanceof got.RequestError) {
21-
throw new RequestError(err as any);
22-
} else if (err instanceof got.ReadError) {
23-
throw new ReadError(err as any);
24-
} else if (err instanceof got.HTTPError) {
25-
throw new HTTPError(err as any);
19+
function wrapError(err: AxiosError) {
20+
if (err.response) {
21+
throw new HTTPError(
22+
err.message,
23+
err.response.status,
24+
err.response.statusText,
25+
err,
26+
);
27+
} else if (err.code) {
28+
throw new RequestError(
29+
err.message,
30+
err.code,
31+
err,
32+
);
33+
} else if (err.config) {
34+
// unknown, but from axios
35+
throw new ReadError(err);
2636
}
2737

2838
// otherwise, just rethrow
@@ -31,27 +41,27 @@ function wrapError(err: Error) {
3141

3242
const userAgent = `${pkg.name}/${pkg.version}`;
3343

34-
export function stream(url: string, headers: any): NodeJS.ReadableStream {
44+
export function stream(url: string, headers: any): Promise<NodeJS.ReadableStream> {
3545
headers["User-Agent"] = userAgent;
36-
return got.stream(url, { headers });
46+
return axios
47+
.get(url, { headers, responseType: "stream" })
48+
.then((res) => res.data as NodeJS.ReadableStream);
3749
}
3850

3951
export function get(url: string, headers: any): Promise<any> {
4052
headers["User-Agent"] = userAgent;
41-
return got
53+
54+
return axios
4255
.get(url, { headers })
43-
.then((res: any) => parseJSON(res.body))
56+
.then((res) => checkJSON(res.data))
4457
.catch(wrapError);
4558
}
4659

47-
export function post(url: string, headers: any, body?: any): Promise<any> {
60+
export function post(url: string, headers: any, data?: any): Promise<any> {
4861
headers["Content-Type"] = "application/json";
4962
headers["User-Agent"] = userAgent;
50-
return got
51-
.post(url, {
52-
body: JSON.stringify(body),
53-
headers,
54-
})
55-
.then((res: any) => parseJSON(res.body))
63+
return axios
64+
.post(url, data, { headers })
65+
.then((res) => checkJSON(res.data))
5666
.catch(wrapError);
5767
}

test/client.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ describe("client", () => {
7878
});
7979

8080
it("getMessageContent", () => {
81-
const s = client.getMessageContent("test_message_id");
82-
return getStreamData(s)
81+
return client.getMessageContent("test_message_id")
82+
.then((s) => getStreamData(s))
8383
.then((data) => {
8484
const res = JSON.parse(data);
8585
equal(res.headers.authorization, "Bearer test_channel_access_token");

test/http.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ describe("http", () => {
7171
"test-header-key": "Test-Header-Value",
7272
};
7373

74-
const s = stream(`${TEST_URL}/stream.txt`, testHeaders);
75-
return getStreamData(s)
74+
return stream(`${TEST_URL}/stream.txt`, testHeaders)
75+
.then((s) => getStreamData(s))
7676
.then((result) => {
7777
equal(result, "hello, stream!\n");
7878
});

test/middleware.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe("middleware", () => {
6565
});
6666

6767
it("fails on invalid JSON", () => {
68-
const auth: any = { "X-Line-Signature": "EqtMVtbumD+AWcprLA6Ty/KsHAN910X7x9EogZkXRHU=" };
68+
const auth: any = { "X-Line-Signature": "Z8YlPpm0lQOqPipiCHVbiuwIDIzRzD7w5hvHgmwEuEs=" };
6969

7070
return post(`${TEST_URL}/webhook`, auth, "i am not jason")
7171
.catch((err: HTTPError) => {

0 commit comments

Comments
 (0)