Skip to content

Commit bdfb4e5

Browse files
author
Hyunje Jun
authored
Merge pull request #20 from line/axios
Use axios instead of got
2 parents 131e296 + e546445 commit bdfb4e5

File tree

11 files changed

+136
-94
lines changed

11 files changed

+136
-94
lines changed

docs/pages/api-reference/client.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Client {
1515
replyMessage(replyToken: string, messages: Message | Message[]): Promise<{}>
1616
multicast(to: string[], messages: Message | Message[]): Promise<{}>
1717
getProfile(userId: string): Promise<Profile>
18-
getMessageContent(messageId: string): ReadableStream
18+
getMessageContent(messageId: string): Promise<ReadableStream>
1919
leaveGroup(groupId: string): Promise<{}>
2020
leaveRoom(roomId: string): Promise<{}>
2121
}
@@ -100,25 +100,27 @@ client.getProfile('user_id').then((profile) => {
100100
});
101101
```
102102

103-
### `getMessageContent(messageId: string): ReadableStream`
103+
### `getMessageContent(messageId: string): Promise<ReadableStream>`
104104

105105
It corresponds to the [Content](https://devdocs.line.me/en/#content) API.
106106

107107
The argument is an ID of media messages, such as image, video, and audio. The ID
108108
can be retrieved from a message object of a message event.
109109

110-
Please beware that what it returns is not a promise, but a [readable stream](https://nodejs.org/dist/latest/docs/api/stream.html#stream_readable_streams).
110+
Please beware that what it returns is promise of [readable stream](https://nodejs.org/dist/latest/docs/api/stream.html#stream_readable_streams).
111111
You can pipe the stream into a file, an HTTP response, etc.
112112

113113
``` js
114-
const stream = client.getMessageContent('message_id')
115-
116-
stream.on('data', (chunk) => {
117-
...
118-
})
119-
stream.on('error', (err) => {
120-
...
121-
})
114+
client.getMessageContent('message_id')
115+
.then((stream) => {
116+
stream.on('data', (chunk) => {
117+
...
118+
})
119+
stream.on('error', (err) => {
120+
...
121+
})
122+
stream.pipe(...)
123+
})
122124
```
123125

124126
### `leaveGroup(groupId: string): Promise<{}>`

docs/pages/api-reference/exceptions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SignatureValidationFailed extends Error {
2828
}
2929

3030
class JSONParseError extends Error {
31-
public raw: string;
31+
public raw: any;
3232
}
3333

3434
class RequestError extends Error {

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
}

0 commit comments

Comments
 (0)