Skip to content

Commit 15c64ff

Browse files
author
Hyunje Jun
authored
Merge pull request #30 from line/refac
Refactoring
2 parents 0b8ddec + 39c559c commit 15c64ff

18 files changed

+306
-289
lines changed

.npmignore

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

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: node_js
22
node_js:
3-
- "4"
43
- "6"
54
- "7"
6-
cache: yarn
5+
- "8"

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Here are each top-level directory explained:
2525
Also, you may use the following npm scripts for development:
2626

2727
* `npm run test`: Run test suites in `test`.
28-
* `npm run lint`: Lint source code with [TSLint](https://github.com/palantir/tslint)
28+
* `npm run format`: Format source code with [Prettier](https://github.com/prettier/prettier)
29+
* `npm run format:check`: Silently run `format` and report formatting errors
2930
* `npm run build`: Build TypeScript code into JavaScript. The built files will
3031
be placed in `dist/`.
3132
* `npm run docs`: Build GitBook docs and serve a doc server

lib/client.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,30 @@ export default class Client {
1313
this.config = config;
1414
}
1515

16-
public pushMessage(to: string, messages: Line.Message | Line.Message[]): Promise<any> {
16+
public pushMessage(
17+
to: string,
18+
messages: Line.Message | Line.Message[],
19+
): Promise<any> {
1720
return this.post(URL.push, {
1821
messages: toArray(messages),
1922
to,
2023
});
2124
}
2225

23-
public replyMessage(replyToken: string, messages: Line.Message | Line.Message[]): Promise<any> {
26+
public replyMessage(
27+
replyToken: string,
28+
messages: Line.Message | Line.Message[],
29+
): Promise<any> {
2430
return this.post(URL.reply, {
2531
messages: toArray(messages),
2632
replyToken,
2733
});
2834
}
2935

30-
public multicast(to: string[], messages: Line.Message | Line.Message[]): Promise<any> {
36+
public multicast(
37+
to: string[],
38+
messages: Line.Message | Line.Message[],
39+
): Promise<any> {
3140
return this.post(URL.multicast, {
3241
messages: toArray(messages),
3342
to,
@@ -38,36 +47,44 @@ export default class Client {
3847
return this.get(URL.profile(userId));
3948
}
4049

41-
public getGroupMemberProfile(groupId: string, userId: string): Promise<Line.Profile> {
50+
public getGroupMemberProfile(
51+
groupId: string,
52+
userId: string,
53+
): Promise<Line.Profile> {
4254
return this.get(URL.groupMemberProfile(groupId, userId));
4355
}
4456

45-
public getRoomMemberProfile(roomId: string, userId: string): Promise<Line.Profile> {
57+
public getRoomMemberProfile(
58+
roomId: string,
59+
userId: string,
60+
): Promise<Line.Profile> {
4661
return this.get(URL.roomMemberProfile(roomId, userId));
4762
}
4863

4964
public getGroupMemberIds(groupId: string): Promise<string[]> {
5065
const load = (start?: string): Promise<string[]> =>
51-
this.get(URL.groupMemberIds(groupId, start))
52-
.then((res: { memberIds: string[], next?: string }) => {
66+
this.get(
67+
URL.groupMemberIds(groupId, start),
68+
).then((res: { memberIds: string[]; next?: string }) => {
5369
if (!res.next) {
5470
return res.memberIds;
5571
}
5672

57-
return load(res.next).then((extraIds) => res.memberIds.concat(extraIds));
73+
return load(res.next).then(extraIds => res.memberIds.concat(extraIds));
5874
});
5975
return load();
6076
}
6177

6278
public getRoomMemberIds(roomId: string): Promise<string[]> {
6379
const load = (start?: string): Promise<string[]> =>
64-
this.get(URL.roomMemberIds(roomId, start))
65-
.then((res: { memberIds: string[], next?: string }) => {
80+
this.get(
81+
URL.roomMemberIds(roomId, start),
82+
).then((res: { memberIds: string[]; next?: string }) => {
6683
if (!res.next) {
6784
return res.memberIds;
6885
}
6986

70-
return load(res.next).then((extraIds) => res.memberIds.concat(extraIds));
87+
return load(res.next).then(extraIds => res.memberIds.concat(extraIds));
7188
});
7289
return load();
7390
}

lib/exceptions.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
export class SignatureValidationFailed extends Error {
2-
constructor(
3-
message: string,
4-
public signature?: string,
5-
) {
2+
constructor(message: string, public signature?: string) {
63
super(message);
74
}
85
}
96

107
export class JSONParseError extends Error {
11-
constructor(
12-
message: string,
13-
public raw: any,
14-
) {
8+
constructor(message: string, public raw: any) {
159
super(message);
1610
}
1711
}
@@ -27,9 +21,7 @@ export class RequestError extends Error {
2721
}
2822

2923
export class ReadError extends Error {
30-
constructor(
31-
private originalError: Error,
32-
) {
24+
constructor(private originalError: Error) {
3325
super(originalError.message);
3426
}
3527
}

lib/http.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
RequestError,
77
} from "./exceptions";
88

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

1111
function checkJSON(raw: any): any {
1212
if (typeof raw === "object") {
@@ -25,11 +25,7 @@ function wrapError(err: AxiosError) {
2525
err,
2626
);
2727
} else if (err.code) {
28-
throw new RequestError(
29-
err.message,
30-
err.code,
31-
err,
32-
);
28+
throw new RequestError(err.message, err.code, err);
3329
} else if (err.config) {
3430
// unknown, but from axios
3531
throw new ReadError(err);
@@ -41,19 +37,22 @@ function wrapError(err: AxiosError) {
4137

4238
const userAgent = `${pkg.name}/${pkg.version}`;
4339

44-
export function stream(url: string, headers: any): Promise<NodeJS.ReadableStream> {
40+
export function stream(
41+
url: string,
42+
headers: any,
43+
): Promise<NodeJS.ReadableStream> {
4544
headers["User-Agent"] = userAgent;
4645
return axios
4746
.get(url, { headers, responseType: "stream" })
48-
.then((res) => res.data as NodeJS.ReadableStream);
47+
.then(res => res.data as NodeJS.ReadableStream);
4948
}
5049

5150
export function get(url: string, headers: any): Promise<any> {
5251
headers["User-Agent"] = userAgent;
5352

5453
return axios
5554
.get(url, { headers })
56-
.then((res) => checkJSON(res.data))
55+
.then(res => checkJSON(res.data))
5756
.catch(wrapError);
5857
}
5958

@@ -62,6 +61,6 @@ export function post(url: string, headers: any, data?: any): Promise<any> {
6261
headers["User-Agent"] = userAgent;
6362
return axios
6463
.post(url, data, { headers })
65-
.then((res) => checkJSON(res.data))
64+
.then(res => checkJSON(res.data))
6665
.catch(wrapError);
6766
}

lib/middleware.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ export type Request = http.IncomingMessage & { body: any };
77
export type Response = http.ServerResponse;
88
export type NextCallback = (err?: Error) => void;
99

10-
export type Middleware = (req: Request, res: Response, next: NextCallback) => void;
11-
12-
export default function middleware(config: Line.Config & Line.MiddlewareConfig): Middleware {
10+
export type Middleware = (
11+
req: Request,
12+
res: Response,
13+
next: NextCallback,
14+
) => void;
15+
16+
export default function middleware(
17+
config: Line.Config & Line.MiddlewareConfig,
18+
): Middleware {
1319
if (!config.channelSecret) {
1420
throw new Error("no channel secret");
1521
}
@@ -28,7 +34,12 @@ export default function middleware(config: Line.Config & Line.MiddlewareConfig):
2834

2935
const validate = (body: string | Buffer) => {
3036
if (!validateSignature(body, secret, signature)) {
31-
next(new SignatureValidationFailed("signature validation failed", signature));
37+
next(
38+
new SignatureValidationFailed(
39+
"signature validation failed",
40+
signature,
41+
),
42+
);
3243
return;
3344
}
3445

lib/urls.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as qs from "querystring";
22

3-
const baseURL: string = process.env.API_BASE_URL || "https://api.line.me/v2/bot/";
3+
const baseURL: string =
4+
process.env.API_BASE_URL || "https://api.line.me/v2/bot/";
45

56
const apiURL = (path: string, query?: object) =>
67
baseURL + path + (query ? `?${qs.stringify(query)}` : "");

lib/validate-signature.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,21 @@ function safeCompare(a: Buffer, b: Buffer): boolean {
2525
} else {
2626
let result = 0;
2727
for (let i = 0; i < a.length; i++) {
28-
result |= a[i] ^ b[i]; // tslint:disable-line no-bitwise
28+
result |= a[i] ^ b[i];
2929
}
3030
return result === 0;
3131
}
3232
}
3333

34-
export default function validateSignature(body: string | Buffer, channelSecret: string, signature: string): boolean {
34+
export default function validateSignature(
35+
body: string | Buffer,
36+
channelSecret: string,
37+
signature: string,
38+
): boolean {
3539
return safeCompare(
36-
createHmac("SHA256", channelSecret).update(body).digest(),
40+
createHmac("SHA256", channelSecret)
41+
.update(body)
42+
.digest(),
3743
s2b(signature, "base64"),
3844
);
3945
}

0 commit comments

Comments
 (0)