@line/bot-sdk
Advanced tools
Comparing version 6.7.0 to 6.7.1
@@ -8,3 +8,3 @@ "use strict"; | ||
} | ||
function checkJSON(raw) { | ||
function ensureJSON(raw) { | ||
if (typeof raw === "object") { | ||
@@ -27,3 +27,3 @@ return raw; | ||
} | ||
pushMessage(to, messages) { | ||
async pushMessage(to, messages) { | ||
return this.http.post("/message/push", { | ||
@@ -34,3 +34,3 @@ messages: toArray(messages), | ||
} | ||
replyMessage(replyToken, messages) { | ||
async replyMessage(replyToken, messages) { | ||
return this.http.post("/message/reply", { | ||
@@ -41,3 +41,3 @@ messages: toArray(messages), | ||
} | ||
multicast(to, messages) { | ||
async multicast(to, messages) { | ||
return this.http.post("/message/multicast", { | ||
@@ -48,35 +48,34 @@ messages: toArray(messages), | ||
} | ||
getProfile(userId) { | ||
return this.http.get(`/profile/${userId}`).then(checkJSON); | ||
async getProfile(userId) { | ||
const profile = await this.http.get(`/profile/${userId}`); | ||
return ensureJSON(profile); | ||
} | ||
getChatMemberProfile(chatType, chatId, userId) { | ||
return this.http | ||
.get(`/${chatType}/${chatId}/member/${userId}`) | ||
.then(checkJSON); | ||
async getChatMemberProfile(chatType, chatId, userId) { | ||
const profile = await this.http.get(`/${chatType}/${chatId}/member/${userId}`); | ||
return ensureJSON(profile); | ||
} | ||
getGroupMemberProfile(groupId, userId) { | ||
async getGroupMemberProfile(groupId, userId) { | ||
return this.getChatMemberProfile("group", groupId, userId); | ||
} | ||
getRoomMemberProfile(roomId, userId) { | ||
async getRoomMemberProfile(roomId, userId) { | ||
return this.getChatMemberProfile("room", roomId, userId); | ||
} | ||
getChatMemberIds(chatType, chatId) { | ||
const load = (start) => this.http | ||
.get(`/${chatType}/${chatId}/members/ids`, start ? { start } : null) | ||
.then(checkJSON) | ||
.then((res) => { | ||
if (!res.next) { | ||
return res.memberIds; | ||
} | ||
return load(res.next).then(extraIds => res.memberIds.concat(extraIds)); | ||
}); | ||
return load(); | ||
async getChatMemberIds(chatType, chatId) { | ||
let memberIds = []; | ||
let start; | ||
do { | ||
const res = await this.http.get(`/${chatType}/${chatId}/members/ids`, start ? { start } : null); | ||
ensureJSON(res); | ||
memberIds = memberIds.concat(res.memberIds); | ||
start = res.next; | ||
} while (start); | ||
return memberIds; | ||
} | ||
getGroupMemberIds(groupId) { | ||
async getGroupMemberIds(groupId) { | ||
return this.getChatMemberIds("group", groupId); | ||
} | ||
getRoomMemberIds(roomId) { | ||
async getRoomMemberIds(roomId) { | ||
return this.getChatMemberIds("room", roomId); | ||
} | ||
getMessageContent(messageId) { | ||
async getMessageContent(messageId) { | ||
return this.http.getStream(`/message/${messageId}/content`); | ||
@@ -87,35 +86,30 @@ } | ||
} | ||
leaveGroup(groupId) { | ||
async leaveGroup(groupId) { | ||
return this.leaveChat("group", groupId); | ||
} | ||
leaveRoom(roomId) { | ||
async leaveRoom(roomId) { | ||
return this.leaveChat("room", roomId); | ||
} | ||
getRichMenu(richMenuId) { | ||
return this.http | ||
.get(`/richmenu/${richMenuId}`) | ||
.then(checkJSON); | ||
async getRichMenu(richMenuId) { | ||
const res = await this.http.get(`/richmenu/${richMenuId}`); | ||
return ensureJSON(res); | ||
} | ||
createRichMenu(richMenu) { | ||
return this.http | ||
.post("/richmenu", richMenu) | ||
.then(checkJSON) | ||
.then(res => res.richMenuId); | ||
async createRichMenu(richMenu) { | ||
const res = await this.http.post("/richmenu", richMenu); | ||
return ensureJSON(res).richMenuId; | ||
} | ||
deleteRichMenu(richMenuId) { | ||
async deleteRichMenu(richMenuId) { | ||
return this.http.delete(`/richmenu/${richMenuId}`); | ||
} | ||
getRichMenuIdOfUser(userId) { | ||
return this.http | ||
.get(`/user/${userId}/richmenu`) | ||
.then(checkJSON) | ||
.then(res => res.richMenuId); | ||
async getRichMenuIdOfUser(userId) { | ||
const res = await this.http.get(`/user/${userId}/richmenu`); | ||
return ensureJSON(res).richMenuId; | ||
} | ||
linkRichMenuToUser(userId, richMenuId) { | ||
async linkRichMenuToUser(userId, richMenuId) { | ||
return this.http.post(`/user/${userId}/richmenu/${richMenuId}`); | ||
} | ||
unlinkRichMenuFromUser(userId) { | ||
async unlinkRichMenuFromUser(userId) { | ||
return this.http.delete(`/user/${userId}/richmenu`); | ||
} | ||
linkRichMenuToMultipleUsers(richMenuId, userIds) { | ||
async linkRichMenuToMultipleUsers(richMenuId, userIds) { | ||
return this.http.post("/richmenu/bulk/link", { | ||
@@ -126,3 +120,3 @@ richMenuId, | ||
} | ||
unlinkRichMenusFromMultipleUsers(userIds) { | ||
async unlinkRichMenusFromMultipleUsers(userIds) { | ||
return this.http.post("/richmenu/bulk/unlink", { | ||
@@ -132,48 +126,39 @@ userIds, | ||
} | ||
getRichMenuImage(richMenuId) { | ||
async getRichMenuImage(richMenuId) { | ||
return this.http.getStream(`/richmenu/${richMenuId}/content`); | ||
} | ||
setRichMenuImage(richMenuId, data, contentType) { | ||
async setRichMenuImage(richMenuId, data, contentType) { | ||
return this.http.postBinary(`/richmenu/${richMenuId}/content`, data, contentType); | ||
} | ||
getRichMenuList() { | ||
return this.http | ||
.get(`/richmenu/list`) | ||
.then(checkJSON) | ||
.then(res => res.richmenus); | ||
async getRichMenuList() { | ||
const res = await this.http.get(`/richmenu/list`); | ||
return ensureJSON(res).richmenus; | ||
} | ||
setDefaultRichMenu(richMenuId) { | ||
async setDefaultRichMenu(richMenuId) { | ||
return this.http.post(`/user/all/richmenu/${richMenuId}`); | ||
} | ||
getDefaultRichMenuId() { | ||
return this.http | ||
.get("/user/all/richmenu") | ||
.then(checkJSON) | ||
.then(res => res.richMenuId); | ||
async getDefaultRichMenuId() { | ||
const res = await this.http.get("/user/all/richmenu"); | ||
return ensureJSON(res).richMenuId; | ||
} | ||
deleteDefaultRichMenu() { | ||
async deleteDefaultRichMenu() { | ||
return this.http.delete("/user/all/richmenu"); | ||
} | ||
getLinkToken(userId) { | ||
return this.http | ||
.post(`/user/${userId}/linkToken`) | ||
.then(checkJSON) | ||
.then(res => res.linkToken); | ||
async getLinkToken(userId) { | ||
const res = await this.http.post(`/user/${userId}/linkToken`); | ||
return ensureJSON(res).linkToken; | ||
} | ||
getNumberOfSentReplyMessages(date) { | ||
return this.http | ||
.get(`/message/delivery/reply?date=${date}`) | ||
.then(checkJSON); | ||
async getNumberOfSentReplyMessages(date) { | ||
const res = await this.http.get(`/message/delivery/reply?date=${date}`); | ||
return ensureJSON(res); | ||
} | ||
getNumberOfSentPushMessages(date) { | ||
return this.http | ||
.get(`/message/delivery/push?date=${date}`) | ||
.then(checkJSON); | ||
async getNumberOfSentPushMessages(date) { | ||
const res = await this.http.get(`/message/delivery/push?date=${date}`); | ||
return ensureJSON(res); | ||
} | ||
getNumberOfSentMulticastMessages(date) { | ||
return this.http | ||
.get(`/message/delivery/multicast?date=${date}`) | ||
.then(checkJSON); | ||
async getNumberOfSentMulticastMessages(date) { | ||
const res = await this.http.get(`/message/delivery/multicast?date=${date}`); | ||
return ensureJSON(res); | ||
} | ||
} | ||
exports.default = Client; |
@@ -8,3 +8,3 @@ /// <reference types="node" /> | ||
getStream(url: string, params?: any): Promise<Readable>; | ||
post<T>(url: string, data?: any): Promise<T>; | ||
post<T>(url: string, body?: any): Promise<T>; | ||
postBinary<T>(url: string, data: Buffer | Readable, contentType?: string): Promise<T>; | ||
@@ -11,0 +11,0 @@ delete<T>(url: string, params?: any): Promise<T>; |
@@ -18,23 +18,26 @@ "use strict"; | ||
} | ||
get(url, params) { | ||
return this.instance.get(url, { params }).then(res => res.data); | ||
async get(url, params) { | ||
const res = await this.instance.get(url, { params }); | ||
return res.data; | ||
} | ||
getStream(url, params) { | ||
return this.instance | ||
.get(url, { params, responseType: "stream" }) | ||
.then(res => res.data); | ||
async getStream(url, params) { | ||
const res = await this.instance.get(url, { | ||
params, | ||
responseType: "stream", | ||
}); | ||
return res.data; | ||
} | ||
post(url, data) { | ||
return this.instance | ||
.post(url, data, { headers: { "Content-Type": "application/json" } }) | ||
.then(res => res.data); | ||
async post(url, body) { | ||
const res = await this.instance.post(url, body, { | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
return res.data; | ||
} | ||
postBinary(url, data, contentType) { | ||
let getBuffer; | ||
if (Buffer.isBuffer(data)) { | ||
getBuffer = Promise.resolve(data); | ||
} | ||
else { | ||
getBuffer = new Promise((resolve, reject) => { | ||
if (data instanceof stream_1.Readable) { | ||
async postBinary(url, data, contentType) { | ||
const buffer = await (async () => { | ||
if (Buffer.isBuffer(data)) { | ||
return data; | ||
} | ||
else if (data instanceof stream_1.Readable) { | ||
return new Promise((resolve, reject) => { | ||
const buffers = []; | ||
@@ -48,21 +51,19 @@ let size = 0; | ||
data.on("error", reject); | ||
} | ||
else { | ||
reject(new Error("invalid data type for postBinary")); | ||
} | ||
}); | ||
} | ||
return getBuffer.then(data => { | ||
return this.instance | ||
.post(url, data, { | ||
headers: { | ||
"Content-Type": contentType || fileType(data).mime, | ||
"Content-Length": data.length, | ||
}, | ||
}) | ||
.then(res => res.data); | ||
}); | ||
} | ||
else { | ||
throw new Error("invalid data type for postBinary"); | ||
} | ||
})(); | ||
const res = await this.instance.post(url, buffer, { | ||
headers: { | ||
"Content-Type": contentType || fileType(buffer).mime, | ||
"Content-Length": buffer.length, | ||
}, | ||
}); | ||
return res.data; | ||
} | ||
delete(url, params) { | ||
return this.instance.delete(url, { params }).then(res => res.data); | ||
async delete(url, params) { | ||
const res = await this.instance.delete(url, { params }); | ||
return res.data; | ||
} | ||
@@ -69,0 +70,0 @@ wrapError(err) { |
@@ -14,3 +14,3 @@ "use strict"; | ||
const secret = config.channelSecret; | ||
return (req, res, next) => { | ||
return async (req, res, next) => { | ||
// header names are lower-cased | ||
@@ -23,32 +23,29 @@ // https://nodejs.org/api/http.html#http_message_headers | ||
} | ||
let getBody; | ||
if (isValidBody(req.rawBody)) { | ||
// rawBody is provided in Google Cloud Functions and others | ||
getBody = Promise.resolve(req.rawBody); | ||
const body = await (async () => { | ||
if (isValidBody(req.rawBody)) { | ||
// rawBody is provided in Google Cloud Functions and others | ||
return req.rawBody; | ||
} | ||
else if (isValidBody(req.body)) { | ||
return req.body; | ||
} | ||
else { | ||
// body may not be parsed yet, parse it to a buffer | ||
return new Promise(resolve => body_parser_1.raw({ type: "*/*" })(req, res, () => resolve(req.body))); | ||
} | ||
})(); | ||
if (!validate_signature_1.default(body, secret, signature)) { | ||
next(new exceptions_1.SignatureValidationFailed("signature validation failed", signature)); | ||
return; | ||
} | ||
else if (isValidBody(req.body)) { | ||
getBody = Promise.resolve(req.body); | ||
const strBody = Buffer.isBuffer(body) ? body.toString() : body; | ||
try { | ||
req.body = JSON.parse(strBody); | ||
next(); | ||
} | ||
else { | ||
// body may not be parsed yet, parse it to a buffer | ||
getBody = new Promise(resolve => { | ||
body_parser_1.raw({ type: "*/*" })(req, res, () => resolve(req.body)); | ||
}); | ||
catch (err) { | ||
next(new exceptions_1.JSONParseError(err.message, strBody)); | ||
} | ||
getBody.then(body => { | ||
if (!validate_signature_1.default(body, secret, signature)) { | ||
next(new exceptions_1.SignatureValidationFailed("signature validation failed", signature)); | ||
return; | ||
} | ||
const strBody = Buffer.isBuffer(body) ? body.toString() : body; | ||
try { | ||
req.body = JSON.parse(strBody); | ||
next(); | ||
} | ||
catch (err) { | ||
next(new exceptions_1.JSONParseError(err.message, strBody)); | ||
} | ||
}); | ||
}; | ||
} | ||
exports.default = middleware; |
@@ -10,3 +10,3 @@ import { Readable } from "stream"; | ||
function checkJSON<T>(raw: T): T { | ||
function ensureJSON<T>(raw: T): T { | ||
if (typeof raw === "object") { | ||
@@ -39,3 +39,3 @@ return raw; | ||
public pushMessage( | ||
public async pushMessage( | ||
to: string, | ||
@@ -50,3 +50,3 @@ messages: Types.Message | Types.Message[], | ||
public replyMessage( | ||
public async replyMessage( | ||
replyToken: string, | ||
@@ -61,3 +61,3 @@ messages: Types.Message | Types.Message[], | ||
public multicast( | ||
public async multicast( | ||
to: string[], | ||
@@ -72,7 +72,8 @@ messages: Types.Message | Types.Message[], | ||
public getProfile(userId: string): Promise<Types.Profile> { | ||
return this.http.get<Types.Profile>(`/profile/${userId}`).then(checkJSON); | ||
public async getProfile(userId: string): Promise<Types.Profile> { | ||
const profile = await this.http.get<Types.Profile>(`/profile/${userId}`); | ||
return ensureJSON(profile); | ||
} | ||
private getChatMemberProfile( | ||
private async getChatMemberProfile( | ||
chatType: ChatType, | ||
@@ -82,8 +83,9 @@ chatId: string, | ||
): Promise<Types.Profile> { | ||
return this.http | ||
.get<Types.Profile>(`/${chatType}/${chatId}/member/${userId}`) | ||
.then(checkJSON); | ||
const profile = await this.http.get<Types.Profile>( | ||
`/${chatType}/${chatId}/member/${userId}`, | ||
); | ||
return ensureJSON(profile); | ||
} | ||
public getGroupMemberProfile( | ||
public async getGroupMemberProfile( | ||
groupId: string, | ||
@@ -95,3 +97,3 @@ userId: string, | ||
public getRoomMemberProfile( | ||
public async getRoomMemberProfile( | ||
roomId: string, | ||
@@ -103,31 +105,31 @@ userId: string, | ||
private getChatMemberIds( | ||
private async getChatMemberIds( | ||
chatType: ChatType, | ||
chatId: string, | ||
): Promise<string[]> { | ||
const load = (start?: string): Promise<string[]> => | ||
this.http | ||
.get(`/${chatType}/${chatId}/members/ids`, start ? { start } : null) | ||
.then(checkJSON) | ||
.then((res: { memberIds: string[]; next?: string }) => { | ||
if (!res.next) { | ||
return res.memberIds; | ||
} | ||
let memberIds: string[] = []; | ||
return load(res.next).then(extraIds => | ||
res.memberIds.concat(extraIds), | ||
); | ||
}); | ||
return load(); | ||
let start: string; | ||
do { | ||
const res = await this.http.get<{ memberIds: string[]; next?: string }>( | ||
`/${chatType}/${chatId}/members/ids`, | ||
start ? { start } : null, | ||
); | ||
ensureJSON(res); | ||
memberIds = memberIds.concat(res.memberIds); | ||
start = res.next; | ||
} while (start); | ||
return memberIds; | ||
} | ||
public getGroupMemberIds(groupId: string): Promise<string[]> { | ||
public async getGroupMemberIds(groupId: string): Promise<string[]> { | ||
return this.getChatMemberIds("group", groupId); | ||
} | ||
public getRoomMemberIds(roomId: string): Promise<string[]> { | ||
public async getRoomMemberIds(roomId: string): Promise<string[]> { | ||
return this.getChatMemberIds("room", roomId); | ||
} | ||
public getMessageContent(messageId: string): Promise<Readable> { | ||
public async getMessageContent(messageId: string): Promise<Readable> { | ||
return this.http.getStream(`/message/${messageId}/content`); | ||
@@ -140,43 +142,45 @@ } | ||
public leaveGroup(groupId: string): Promise<any> { | ||
public async leaveGroup(groupId: string): Promise<any> { | ||
return this.leaveChat("group", groupId); | ||
} | ||
public leaveRoom(roomId: string): Promise<any> { | ||
public async leaveRoom(roomId: string): Promise<any> { | ||
return this.leaveChat("room", roomId); | ||
} | ||
public getRichMenu(richMenuId: string): Promise<Types.RichMenuResponse> { | ||
return this.http | ||
.get<Types.RichMenuResponse>(`/richmenu/${richMenuId}`) | ||
.then(checkJSON); | ||
public async getRichMenu( | ||
richMenuId: string, | ||
): Promise<Types.RichMenuResponse> { | ||
const res = await this.http.get<Types.RichMenuResponse>( | ||
`/richmenu/${richMenuId}`, | ||
); | ||
return ensureJSON(res); | ||
} | ||
public createRichMenu(richMenu: Types.RichMenu): Promise<string> { | ||
return this.http | ||
.post<any>("/richmenu", richMenu) | ||
.then(checkJSON) | ||
.then(res => res.richMenuId); | ||
public async createRichMenu(richMenu: Types.RichMenu): Promise<string> { | ||
const res = await this.http.post<any>("/richmenu", richMenu); | ||
return ensureJSON(res).richMenuId; | ||
} | ||
public deleteRichMenu(richMenuId: string): Promise<any> { | ||
public async deleteRichMenu(richMenuId: string): Promise<any> { | ||
return this.http.delete(`/richmenu/${richMenuId}`); | ||
} | ||
public getRichMenuIdOfUser(userId: string): Promise<string> { | ||
return this.http | ||
.get<any>(`/user/${userId}/richmenu`) | ||
.then(checkJSON) | ||
.then(res => res.richMenuId); | ||
public async getRichMenuIdOfUser(userId: string): Promise<string> { | ||
const res = await this.http.get<any>(`/user/${userId}/richmenu`); | ||
return ensureJSON(res).richMenuId; | ||
} | ||
public linkRichMenuToUser(userId: string, richMenuId: string): Promise<any> { | ||
public async linkRichMenuToUser( | ||
userId: string, | ||
richMenuId: string, | ||
): Promise<any> { | ||
return this.http.post(`/user/${userId}/richmenu/${richMenuId}`); | ||
} | ||
public unlinkRichMenuFromUser(userId: string): Promise<any> { | ||
public async unlinkRichMenuFromUser(userId: string): Promise<any> { | ||
return this.http.delete(`/user/${userId}/richmenu`); | ||
} | ||
public linkRichMenuToMultipleUsers( | ||
public async linkRichMenuToMultipleUsers( | ||
richMenuId: string, | ||
@@ -191,3 +195,5 @@ userIds: string[], | ||
public unlinkRichMenusFromMultipleUsers(userIds: string[]): Promise<any> { | ||
public async unlinkRichMenusFromMultipleUsers( | ||
userIds: string[], | ||
): Promise<any> { | ||
return this.http.post("/richmenu/bulk/unlink", { | ||
@@ -198,7 +204,7 @@ userIds, | ||
public getRichMenuImage(richMenuId: string): Promise<Readable> { | ||
public async getRichMenuImage(richMenuId: string): Promise<Readable> { | ||
return this.http.getStream(`/richmenu/${richMenuId}/content`); | ||
} | ||
public setRichMenuImage( | ||
public async setRichMenuImage( | ||
richMenuId: string, | ||
@@ -215,60 +221,51 @@ data: Buffer | Readable, | ||
public getRichMenuList(): Promise<Array<Types.RichMenuResponse>> { | ||
return this.http | ||
.get<any>(`/richmenu/list`) | ||
.then(checkJSON) | ||
.then(res => res.richmenus); | ||
public async getRichMenuList(): Promise<Array<Types.RichMenuResponse>> { | ||
const res = await this.http.get<any>(`/richmenu/list`); | ||
return ensureJSON(res).richmenus; | ||
} | ||
public setDefaultRichMenu(richMenuId: string): Promise<{}> { | ||
public async setDefaultRichMenu(richMenuId: string): Promise<{}> { | ||
return this.http.post(`/user/all/richmenu/${richMenuId}`); | ||
} | ||
public getDefaultRichMenuId(): Promise<string> { | ||
return this.http | ||
.get<any>("/user/all/richmenu") | ||
.then(checkJSON) | ||
.then(res => res.richMenuId); | ||
public async getDefaultRichMenuId(): Promise<string> { | ||
const res = await this.http.get<any>("/user/all/richmenu"); | ||
return ensureJSON(res).richMenuId; | ||
} | ||
public deleteDefaultRichMenu(): Promise<{}> { | ||
public async deleteDefaultRichMenu(): Promise<{}> { | ||
return this.http.delete("/user/all/richmenu"); | ||
} | ||
public getLinkToken(userId: string): Promise<string> { | ||
return this.http | ||
.post<any>(`/user/${userId}/linkToken`) | ||
.then(checkJSON) | ||
.then(res => res.linkToken); | ||
public async getLinkToken(userId: string): Promise<string> { | ||
const res = await this.http.post<any>(`/user/${userId}/linkToken`); | ||
return ensureJSON(res).linkToken; | ||
} | ||
public getNumberOfSentReplyMessages( | ||
public async getNumberOfSentReplyMessages( | ||
date: string, | ||
): Promise<Types.NumberOfMessagesSentResponse> { | ||
return this.http | ||
.get<Types.NumberOfMessagesSentResponse>( | ||
`/message/delivery/reply?date=${date}`, | ||
) | ||
.then(checkJSON); | ||
const res = await this.http.get<Types.NumberOfMessagesSentResponse>( | ||
`/message/delivery/reply?date=${date}`, | ||
); | ||
return ensureJSON(res); | ||
} | ||
public getNumberOfSentPushMessages( | ||
public async getNumberOfSentPushMessages( | ||
date: string, | ||
): Promise<Types.NumberOfMessagesSentResponse> { | ||
return this.http | ||
.get<Types.NumberOfMessagesSentResponse>( | ||
`/message/delivery/push?date=${date}`, | ||
) | ||
.then(checkJSON); | ||
const res = await this.http.get<Types.NumberOfMessagesSentResponse>( | ||
`/message/delivery/push?date=${date}`, | ||
); | ||
return ensureJSON(res); | ||
} | ||
public getNumberOfSentMulticastMessages( | ||
public async getNumberOfSentMulticastMessages( | ||
date: string, | ||
): Promise<Types.NumberOfMessagesSentResponse> { | ||
return this.http | ||
.get<Types.NumberOfMessagesSentResponse>( | ||
`/message/delivery/multicast?date=${date}`, | ||
) | ||
.then(checkJSON); | ||
const res = await this.http.get<Types.NumberOfMessagesSentResponse>( | ||
`/message/delivery/multicast?date=${date}`, | ||
); | ||
return ensureJSON(res); | ||
} | ||
} |
@@ -25,19 +25,23 @@ import axios, { AxiosInstance, AxiosError } from "axios"; | ||
public get<T>(url: string, params?: any): Promise<T> { | ||
return this.instance.get(url, { params }).then(res => res.data); | ||
public async get<T>(url: string, params?: any): Promise<T> { | ||
const res = await this.instance.get(url, { params }); | ||
return res.data; | ||
} | ||
public getStream(url: string, params?: any): Promise<Readable> { | ||
return this.instance | ||
.get(url, { params, responseType: "stream" }) | ||
.then(res => res.data as Readable); | ||
public async getStream(url: string, params?: any): Promise<Readable> { | ||
const res = await this.instance.get(url, { | ||
params, | ||
responseType: "stream", | ||
}); | ||
return res.data as Readable; | ||
} | ||
public post<T>(url: string, data?: any): Promise<T> { | ||
return this.instance | ||
.post(url, data, { headers: { "Content-Type": "application/json" } }) | ||
.then(res => res.data); | ||
public async post<T>(url: string, body?: any): Promise<T> { | ||
const res = await this.instance.post(url, body, { | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
return res.data; | ||
} | ||
public postBinary<T>( | ||
public async postBinary<T>( | ||
url: string, | ||
@@ -47,9 +51,7 @@ data: Buffer | Readable, | ||
): Promise<T> { | ||
let getBuffer: Promise<Buffer>; | ||
if (Buffer.isBuffer(data)) { | ||
getBuffer = Promise.resolve(data); | ||
} else { | ||
getBuffer = new Promise((resolve, reject) => { | ||
if (data instanceof Readable) { | ||
const buffer = await (async (): Promise<Buffer> => { | ||
if (Buffer.isBuffer(data)) { | ||
return data; | ||
} else if (data instanceof Readable) { | ||
return new Promise<Buffer>((resolve, reject) => { | ||
const buffers: Buffer[] = []; | ||
@@ -63,22 +65,21 @@ let size = 0; | ||
data.on("error", reject); | ||
} else { | ||
reject(new Error("invalid data type for postBinary")); | ||
} | ||
}); | ||
} | ||
}); | ||
} else { | ||
throw new Error("invalid data type for postBinary"); | ||
} | ||
})(); | ||
return getBuffer.then(data => { | ||
return this.instance | ||
.post(url, data, { | ||
headers: { | ||
"Content-Type": contentType || fileType(data).mime, | ||
"Content-Length": data.length, | ||
}, | ||
}) | ||
.then(res => res.data); | ||
const res = await this.instance.post(url, buffer, { | ||
headers: { | ||
"Content-Type": contentType || fileType(buffer).mime, | ||
"Content-Length": buffer.length, | ||
}, | ||
}); | ||
return res.data; | ||
} | ||
public delete<T>(url: string, params?: any): Promise<T> { | ||
return this.instance.delete(url, { params }).then(res => res.data); | ||
public async delete<T>(url: string, params?: any): Promise<T> { | ||
const res = await this.instance.delete(url, { params }); | ||
return res.data; | ||
} | ||
@@ -85,0 +86,0 @@ |
@@ -28,3 +28,3 @@ import { raw } from "body-parser"; | ||
return (req, res, next) => { | ||
return async (req, res, next) => { | ||
// header names are lower-cased | ||
@@ -39,36 +39,32 @@ // https://nodejs.org/api/http.html#http_message_headers | ||
let getBody: Promise<string | Buffer>; | ||
if (isValidBody((req as any).rawBody)) { | ||
// rawBody is provided in Google Cloud Functions and others | ||
getBody = Promise.resolve((req as any).rawBody); | ||
} else if (isValidBody(req.body)) { | ||
getBody = Promise.resolve(req.body); | ||
} else { | ||
// body may not be parsed yet, parse it to a buffer | ||
getBody = new Promise(resolve => { | ||
raw({ type: "*/*" })(req as any, res as any, () => resolve(req.body)); | ||
}); | ||
} | ||
getBody.then(body => { | ||
if (!validateSignature(body, secret, signature)) { | ||
next( | ||
new SignatureValidationFailed( | ||
"signature validation failed", | ||
signature, | ||
), | ||
const body = await (async (): Promise<string | Buffer> => { | ||
if (isValidBody((req as any).rawBody)) { | ||
// rawBody is provided in Google Cloud Functions and others | ||
return (req as any).rawBody; | ||
} else if (isValidBody(req.body)) { | ||
return req.body; | ||
} else { | ||
// body may not be parsed yet, parse it to a buffer | ||
return new Promise<Buffer>(resolve => | ||
raw({ type: "*/*" })(req as any, res as any, () => resolve(req.body)), | ||
); | ||
return; | ||
} | ||
})(); | ||
const strBody = Buffer.isBuffer(body) ? body.toString() : body; | ||
if (!validateSignature(body, secret, signature)) { | ||
next( | ||
new SignatureValidationFailed("signature validation failed", signature), | ||
); | ||
return; | ||
} | ||
try { | ||
req.body = JSON.parse(strBody); | ||
next(); | ||
} catch (err) { | ||
next(new JSONParseError(err.message, strBody)); | ||
} | ||
}); | ||
const strBody = Buffer.isBuffer(body) ? body.toString() : body; | ||
try { | ||
req.body = JSON.parse(strBody); | ||
next(); | ||
} catch (err) { | ||
next(new JSONParseError(err.message, strBody)); | ||
} | ||
}; | ||
} |
{ | ||
"name": "@line/bot-sdk", | ||
"version": "6.7.0", | ||
"version": "6.7.1", | ||
"description": "Node.js SDK for LINE Messaging API", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
@@ -8,0 +8,0 @@ "main": "dist/index.js", |
@@ -1,2 +0,2 @@ | ||
# line-bot-sdk-nodejs | ||
# LINE Messaging API SDK for nodejs | ||
@@ -6,8 +6,21 @@ [![Travis CI](https://travis-ci.org/line/line-bot-sdk-nodejs.svg?branch=master)](https://travis-ci.org/line/line-bot-sdk-nodejs) | ||
Node.js SDK for LINE Messaging API | ||
## Getting Started | ||
## Introduction | ||
The LINE Messaging API SDK for nodejs makes it easy to develop bots using LINE Messaging API, and you can create a sample bot within minutes. | ||
### Install | ||
## Documentation | ||
See the official API documentation for more information | ||
- English: https://developers.line.biz/en/docs/messaging-api/overview/ | ||
- Japanese: https://developers.line.biz/ja/docs/messaging-api/overview/ | ||
line-bot-sdk-nodejs documentation: https://line.github.io/line-bot-sdk-nodejs/#getting-started | ||
## Requirements | ||
* **Node.js** 8 or higher | ||
## Installation | ||
Using [npm](https://www.npmjs.com/): | ||
@@ -19,22 +32,16 @@ | ||
### Documentation | ||
## Help and media | ||
FAQ: https://developers.line.biz/en/faq/ | ||
For guide, API reference, and other information, please refer to | ||
the [documentation](https://line.github.io/line-bot-sdk-nodejs/). | ||
Community Q&A: https://www.line-community.me/questions | ||
### LINE Messaging API References | ||
News: https://developers.line.biz/en/news/ | ||
Here are links to official references for LINE Messaging API. It is recommended | ||
reading them beforehand. | ||
Twitter: @LINE_DEV | ||
* LINE API Reference [EN](https://developers.line.me/en/docs/messaging-api/reference/) [JA](https://developers.line.me/ja/docs/messaging-api/reference/) | ||
* LINE Developers - Messaging API | ||
* [Overview](https://developers.line.me/messaging-api/overview) | ||
* [Getting started](https://developers.line.me/messaging-api/getting-started) | ||
* [Joining groups and rooms](https://developers.line.me/messaging-api/joining-groups-and-rooms) | ||
## Versioning | ||
This project respects semantic versioning | ||
## Requirements | ||
See http://semver.org/ | ||
* **Node.js** 6 or higher | ||
## Contributing | ||
@@ -45,3 +52,16 @@ | ||
## License | ||
[Apache License Version 2.0](LICENSE) | ||
``` | ||
Copyright (C) 2016 LINE Corp. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
154060
65
4055
1