Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change data api's domain to api-data.line.me #178

Merged
merged 3 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 64 additions & 42 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ function ensureJSON<T>(raw: T): T {
}

type ChatType = "group" | "room";
const API_HOST: string = process.env.API_BASE_URL || "https://api.line.me/v2/";
const BOT_BASE_URL: string = process.env.API_BASE_URL || `${API_HOST}bot/`;
const OAUTH_BASE_URL = `${API_HOST}oauth/`;
import {
MESSAGING_API_PREFIX,
DATA_API_PREFIX,
OAUTH_BASE_PREFIX,
} from "./endpoints";

export default class Client {
public config: Types.ClientConfig;
Expand All @@ -32,7 +34,6 @@ export default class Client {

this.config = config;
this.http = new HTTPClient({
baseURL: BOT_BASE_URL,
defaultHeaders: {
Authorization: "Bearer " + this.config.channelAccessToken,
},
Expand All @@ -57,7 +58,7 @@ export default class Client {
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<Types.MessageAPIResponseBase> {
return this.http.post("/message/push", {
return this.http.post(`${MESSAGING_API_PREFIX}/message/push`, {
messages: toArray(messages),
to,
notificationDisabled,
Expand All @@ -69,7 +70,7 @@ export default class Client {
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<Types.MessageAPIResponseBase> {
return this.http.post("/message/reply", {
return this.http.post(`${MESSAGING_API_PREFIX}/message/reply`, {
messages: toArray(messages),
replyToken,
notificationDisabled,
Expand All @@ -81,7 +82,7 @@ export default class Client {
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<Types.MessageAPIResponseBase> {
return this.http.post("/message/multicast", {
return this.http.post(`${MESSAGING_API_PREFIX}/message/multicast`, {
messages: toArray(messages),
to,
notificationDisabled,
Expand All @@ -92,14 +93,16 @@ export default class Client {
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<Types.MessageAPIResponseBase> {
return this.http.post("/message/broadcast", {
return this.http.post(`${MESSAGING_API_PREFIX}/message/broadcast`, {
messages: toArray(messages),
notificationDisabled,
});
}

public async getProfile(userId: string): Promise<Types.Profile> {
const profile = await this.http.get<Types.Profile>(`/profile/${userId}`);
const profile = await this.http.get<Types.Profile>(
`${MESSAGING_API_PREFIX}/profile/${userId}`,
);
return ensureJSON(profile);
}

Expand All @@ -109,7 +112,7 @@ export default class Client {
userId: string,
): Promise<Types.Profile> {
const profile = await this.http.get<Types.Profile>(
`/${chatType}/${chatId}/member/${userId}`,
`${MESSAGING_API_PREFIX}/${chatType}/${chatId}/member/${userId}`,
);
return ensureJSON(profile);
}
Expand Down Expand Up @@ -137,7 +140,7 @@ export default class Client {
let start: string;
do {
const res = await this.http.get<{ memberIds: string[]; next?: string }>(
`/${chatType}/${chatId}/members/ids`,
`${MESSAGING_API_PREFIX}/${chatType}/${chatId}/members/ids`,
start ? { start } : null,
);
ensureJSON(res);
Expand All @@ -157,11 +160,15 @@ export default class Client {
}

public async getMessageContent(messageId: string): Promise<Readable> {
return this.http.getStream(`/message/${messageId}/content`);
return this.http.getStream(
`${DATA_API_PREFIX}/message/${messageId}/content`,
);
}

private leaveChat(chatType: ChatType, chatId: string): Promise<any> {
return this.http.post(`/${chatType}/${chatId}/leave`);
return this.http.post(
`${MESSAGING_API_PREFIX}/${chatType}/${chatId}/leave`,
);
}

public async leaveGroup(groupId: string): Promise<any> {
Expand All @@ -176,41 +183,48 @@ export default class Client {
richMenuId: string,
): Promise<Types.RichMenuResponse> {
const res = await this.http.get<Types.RichMenuResponse>(
`/richmenu/${richMenuId}`,
`${MESSAGING_API_PREFIX}/richmenu/${richMenuId}`,
);
return ensureJSON(res);
}

public async createRichMenu(richMenu: Types.RichMenu): Promise<string> {
const res = await this.http.post<any>("/richmenu", richMenu);
const res = await this.http.post<any>(
`${MESSAGING_API_PREFIX}/richmenu`,
richMenu,
);
return ensureJSON(res).richMenuId;
}

public async deleteRichMenu(richMenuId: string): Promise<any> {
return this.http.delete(`/richmenu/${richMenuId}`);
return this.http.delete(`${MESSAGING_API_PREFIX}/richmenu/${richMenuId}`);
}

public async getRichMenuIdOfUser(userId: string): Promise<string> {
const res = await this.http.get<any>(`/user/${userId}/richmenu`);
const res = await this.http.get<any>(
`${MESSAGING_API_PREFIX}/user/${userId}/richmenu`,
);
return ensureJSON(res).richMenuId;
}

public async linkRichMenuToUser(
userId: string,
richMenuId: string,
): Promise<any> {
return this.http.post(`/user/${userId}/richmenu/${richMenuId}`);
return this.http.post(
`${MESSAGING_API_PREFIX}/user/${userId}/richmenu/${richMenuId}`,
);
}

public async unlinkRichMenuFromUser(userId: string): Promise<any> {
return this.http.delete(`/user/${userId}/richmenu`);
return this.http.delete(`${MESSAGING_API_PREFIX}/user/${userId}/richmenu`);
}

public async linkRichMenuToMultipleUsers(
richMenuId: string,
userIds: string[],
): Promise<any> {
return this.http.post("/richmenu/bulk/link", {
return this.http.post(`${MESSAGING_API_PREFIX}/richmenu/bulk/link`, {
richMenuId,
userIds,
});
Expand All @@ -219,13 +233,15 @@ export default class Client {
public async unlinkRichMenusFromMultipleUsers(
userIds: string[],
): Promise<any> {
return this.http.post("/richmenu/bulk/unlink", {
return this.http.post(`${MESSAGING_API_PREFIX}/richmenu/bulk/unlink`, {
userIds,
});
}

public async getRichMenuImage(richMenuId: string): Promise<Readable> {
return this.http.getStream(`/richmenu/${richMenuId}/content`);
return this.http.getStream(
`${DATA_API_PREFIX}/richmenu/${richMenuId}/content`,
);
}

public async setRichMenuImage(
Expand All @@ -234,40 +250,48 @@ export default class Client {
contentType?: string,
): Promise<any> {
return this.http.postBinary(
`/richmenu/${richMenuId}/content`,
`${DATA_API_PREFIX}/richmenu/${richMenuId}/content`,
data,
contentType,
);
}

public async getRichMenuList(): Promise<Array<Types.RichMenuResponse>> {
const res = await this.http.get<any>(`/richmenu/list`);
const res = await this.http.get<any>(
`${MESSAGING_API_PREFIX}/richmenu/list`,
);
return ensureJSON(res).richmenus;
}

public async setDefaultRichMenu(richMenuId: string): Promise<{}> {
return this.http.post(`/user/all/richmenu/${richMenuId}`);
return this.http.post(
`${MESSAGING_API_PREFIX}/user/all/richmenu/${richMenuId}`,
);
}

public async getDefaultRichMenuId(): Promise<string> {
const res = await this.http.get<any>("/user/all/richmenu");
const res = await this.http.get<any>(
`${MESSAGING_API_PREFIX}/user/all/richmenu`,
);
return ensureJSON(res).richMenuId;
}

public async deleteDefaultRichMenu(): Promise<{}> {
return this.http.delete("/user/all/richmenu");
return this.http.delete(`${MESSAGING_API_PREFIX}/user/all/richmenu`);
}

public async getLinkToken(userId: string): Promise<string> {
const res = await this.http.post<any>(`/user/${userId}/linkToken`);
const res = await this.http.post<any>(
`${MESSAGING_API_PREFIX}/user/${userId}/linkToken`,
);
return ensureJSON(res).linkToken;
}

public async getNumberOfSentReplyMessages(
date: string,
): Promise<Types.NumberOfMessagesSentResponse> {
const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
`/message/delivery/reply?date=${date}`,
`${MESSAGING_API_PREFIX}/message/delivery/reply?date=${date}`,
);
return ensureJSON(res);
}
Expand All @@ -276,7 +300,7 @@ export default class Client {
date: string,
): Promise<Types.NumberOfMessagesSentResponse> {
const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
`/message/delivery/push?date=${date}`,
`${MESSAGING_API_PREFIX}/message/delivery/push?date=${date}`,
);
return ensureJSON(res);
}
Expand All @@ -285,7 +309,7 @@ export default class Client {
date: string,
): Promise<Types.NumberOfMessagesSentResponse> {
const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
`/message/delivery/multicast?date=${date}`,
`${MESSAGING_API_PREFIX}/message/delivery/multicast?date=${date}`,
);
return ensureJSON(res);
}
Expand All @@ -294,7 +318,7 @@ export default class Client {
Types.TargetLimitForAdditionalMessages
> {
const res = await this.http.get<Types.TargetLimitForAdditionalMessages>(
"/message/quota",
`${MESSAGING_API_PREFIX}/message/quota`,
);
return ensureJSON(res);
}
Expand All @@ -303,7 +327,7 @@ export default class Client {
Types.NumberOfMessagesSentThisMonth
> {
const res = await this.http.get<Types.NumberOfMessagesSentThisMonth>(
"/message/quota/consumption",
`${MESSAGING_API_PREFIX}/message/quota/consumption`,
);
return ensureJSON(res);
}
Expand All @@ -312,7 +336,7 @@ export default class Client {
date: string,
): Promise<Types.NumberOfMessagesSentResponse> {
const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
`/message/delivery/broadcast?date=${date}`,
`${MESSAGING_API_PREFIX}/message/delivery/broadcast?date=${date}`,
);
return ensureJSON(res);
}
Expand All @@ -321,7 +345,7 @@ export default class Client {
date: string,
): Promise<Types.NumberOfMessageDeliveriesResponse> {
const res = await this.http.get<Types.NumberOfMessageDeliveriesResponse>(
`/insight/message/delivery?date=${date}`,
`${MESSAGING_API_PREFIX}/insight/message/delivery?date=${date}`,
);
return ensureJSON(res);
}
Expand All @@ -330,14 +354,14 @@ export default class Client {
date: string,
): Promise<Types.NumberOfFollowersResponse> {
const res = await this.http.get<Types.NumberOfFollowersResponse>(
`/insight/followers?date=${date}`,
`${MESSAGING_API_PREFIX}/insight/followers?date=${date}`,
);
return ensureJSON(res);
}

public async getFriendDemographics(): Promise<Types.FriendDemographics> {
const res = await this.http.get<Types.FriendDemographics>(
`/insight/demographic`,
`${MESSAGING_API_PREFIX}/insight/demographic`,
);
return ensureJSON(res);
}
Expand All @@ -347,9 +371,7 @@ export class OAuth {
private http: HTTPClient;

constructor() {
this.http = new HTTPClient({
baseURL: OAUTH_BASE_URL,
});
this.http = new HTTPClient();
}

public issueAccessToken(
Expand All @@ -360,14 +382,14 @@ export class OAuth {
expires_in: number;
token_type: "Bearer";
}> {
return this.http.postForm("/accessToken", {
return this.http.postForm(`${OAUTH_BASE_PREFIX}/accessToken`, {
grant_type: "client_credentials",
client_id,
client_secret,
});
}

public revokeAccessToken(access_token: string): Promise<{}> {
return this.http.postForm("/revoke", { access_token });
return this.http.postForm(`${OAUTH_BASE_PREFIX}/revoke`, { access_token });
}
}
18 changes: 18 additions & 0 deletions lib/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let MESSAGING_API_PREFIX: string,
DATA_API_PREFIX: string,
OAUTH_BASE_PREFIX: string;

if (process.env.NODE_ENV == "test") {
const TEST_SERVER = `http://localhost:1234`;
process.env.TEST_PORT = "1234";

MESSAGING_API_PREFIX = TEST_SERVER;
DATA_API_PREFIX = TEST_SERVER;
OAUTH_BASE_PREFIX = `${TEST_SERVER}/oauth`;
} else {
MESSAGING_API_PREFIX = `https://api.line.me/v2/bot`;
DATA_API_PREFIX = `https://api-data.line.me/v2/bot`;
OAUTH_BASE_PREFIX = `https://api.line.me/v2/oauth`;
}

export { MESSAGING_API_PREFIX, DATA_API_PREFIX, OAUTH_BASE_PREFIX };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"scripts": {
"pretest": "npm run build",
"test": "API_BASE_URL=http://localhost:1234/ TEST_PORT=1234 TS_NODE_CACHE=0 nyc mocha",
"test": "NODE_ENV=test TS_NODE_CACHE=0 nyc mocha",
"prettier": "prettier --parser typescript --trailing-comma all \"{lib,test}/**/*.ts\"",
"format": "npm run prettier -- --write",
"format:check": "npm run prettier -- -l",
Expand Down