김수민

commit

Showing 96 changed files with 24618 additions and 1 deletions
......@@ -2,7 +2,6 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="5c08ae96-2f87-46c1-b81d-f30e494ce252" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app.js" beforeDir="false" afterPath="$PROJECT_DIR$/app.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/package-lock.json" beforeDir="false" afterPath="$PROJECT_DIR$/package-lock.json" afterDir="false" />
......@@ -58,6 +57,7 @@
<workItem from="1573898557106" duration="89000" />
<workItem from="1573899005304" duration="7410000" />
<workItem from="1574071758382" duration="6285000" />
<workItem from="1574323490142" duration="7902000" />
</task>
<servers />
</component>
......
......@@ -105,10 +105,41 @@ function handleEvent(event) {
result.text = objBody.message.result.translatedText;
console.log(result.text);
//번역된 문장 보내기
let audio_options={
'Text': result.text,
'OutputFormat': 'mp3',
'VoiceId':'Seoyeon'
};
Polly.synthesizeSpeech(audio_options, (err, data) => {
console.log("check");
if (err) {
throw err;
} else if (data) {
if (data.AudioStream instanceof Buffer) {
fs.writeFile("./speech.mp3", data.AudioStream, function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
})
}
}
});
request.post(audio_options, function (error,response,body) {
if(!error && response.statusCode == 200){
for_audio_client.replyAudio(event.replyToken,{
originalContentUrl: 'https://panguin.ml/speech.mp3',
duration: 240000
}).then(resolve).catch(reject);
}
});
client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
}
});
}
// 메시지의 언어가 영어 또는 한국어가 아닐 경우
else{
......
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/../loose-envify/cli.js" "$@"
ret=$?
else
node "$basedir/../loose-envify/cli.js" "$@"
ret=$?
fi
exit $ret
@IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\loose-envify\cli.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\loose-envify\cli.js" %*
)
\ No newline at end of file
The MIT License (MIT)
Copyright (c) 2017-present Yoctol (github.com/Yoctol/messaging-apis)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# axios-error
> An axios error wrapper that aim to provide clear error message to the user
## Installation
```sh
npm i --save axios-error
```
or
```sh
yarn add axios-error
```
<br />
## Usage
```js
const AxiosError = require('axios-error');
// You can construct it from error throw by axios
const error = new AxiosError(errorThrowByAxios);
// Or with custom error message
const error = new AxiosError(message, errorThrowByAxios);
// Or construct it from axios config, axios request and axios response
const error = new AxiosError(message, { config, request, response });
```
Directly `console.log` on the error instance will return formatted message. If you'd like to get the axios `request`, `response`, or `config`, you can still get them via those keys on the error instance.
```js
console.log(error); // formatted error message
console.log(error.stack); // error stack trace
console.log(error.config); // axios request config
console.log(error.request); // HTTP request
console.log(error.response); // HTTP response
```
"use strict";var _util = _interopRequireDefault(require("util"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
function indent(str) {
return str.
split('\n').
map(s => s ? ` ${s}` : '').
join('\n');
}
function json(data) {
return JSON.stringify(data, null, 2);
}
module.exports = class AxiosError extends Error {
constructor(messageOrErr, _err = {}) {
let err;
if (messageOrErr instanceof Error) {
super(messageOrErr.message);
err = messageOrErr;
} else {
super(messageOrErr);
err = _err;
}
const { config, request, response } = err;
this.config = config;
this.request = request;
this.response = response;
if (response && response.status) {
this.status = response.status;
}
}
// TODO: remove inspect until we drop node < 6.6
inspect(...args) {
return this[_util.default.inspect.custom](...args);
}
[_util.default.inspect.custom]() {
let requestMessage = '';
if (this.config) {
let { data } = this.config;
try {
data = JSON.parse(data);
} catch (_) {} // eslint-disable-line
let requestData = '';
if (this.config.data) {
requestData = `
Request Data -
${indent(json(data))}`;
}
requestMessage = `
Request -
${this.config.method.toUpperCase()} ${this.config.url}
${requestData}`;
}
let responseMessage = '';
if (this.response) {
let responseData;
if (this.response.data) {
responseData = `
Response Data -
${indent(json(this.response.data))}`;
}
responseMessage = `
Response -
${this.response.status} ${this.response.statusText}
${responseData}`;
}
return `
${this.stack}
Error Message -
${this.message}
${requestMessage}
${responseMessage}
`;
}};
\ No newline at end of file
{
"_from": "axios-error@^0.8.1",
"_id": "axios-error@0.8.1",
"_inBundle": false,
"_integrity": "sha512-4YIf0FK2aO8HHVAQXvVFK2oPQSsOh3P1WQjkwQwO3oFjq4vO3S3YSyiRzyebBIR4NmcHaMJ6W5m0iHuisG7lTA==",
"_location": "/axios-error",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "axios-error@^0.8.1",
"name": "axios-error",
"escapedName": "axios-error",
"rawSpec": "^0.8.1",
"saveSpec": null,
"fetchSpec": "^0.8.1"
},
"_requiredBy": [
"/messaging-api-line"
],
"_resolved": "https://registry.npmjs.org/axios-error/-/axios-error-0.8.1.tgz",
"_shasum": "4d9c21370465c290d07192e7f3eacfff49c256ae",
"_spec": "axios-error@^0.8.1",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\messaging-api-line",
"bugs": {
"url": "https://github.com/Yoctol/messaging-apis/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "An axios error wrapper that aim to provide clear error message to the user",
"devDependencies": {
"axios": "^0.19.0",
"axios-mock-adapter": "^1.17.0"
},
"engines": {
"node": ">=8"
},
"gitHead": "622f5664634c26e752e4e54bd32dbc773271390f",
"homepage": "https://github.com/Yoctol/messaging-apis#readme",
"keywords": [
"axios",
"error",
"http"
],
"license": "MIT",
"main": "lib/index.js",
"name": "axios-error",
"repository": {
"type": "git",
"url": "git+https://github.com/Yoctol/messaging-apis.git"
},
"version": "0.8.1"
}
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`should support error without axios data 1`] = `
"
Error: boom....
at Object.<anonymous> (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:16:19)
at Generator.throw (<anonymous>)
at step (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:336)
at /Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:535
at <anonymous>
Error Message -
custom error
"
`;
exports[`should work 1`] = `
"
Error: boom....
at Object.<anonymous> (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:16:19)
at Generator.throw (<anonymous>)
at step (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:336)
at /Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:535
at <anonymous>
Error Message -
boom....
Request -
POST /
Request Data -
{
\\"x\\": 1
}
Response -
400 Bad Request
Response Data -
{
\\"error_status\\": \\"boom....\\"
}
"
`;
exports[`should work with construct using error instance only 1`] = `
"
Error: boom....
at Object.<anonymous> (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:16:19)
at Generator.throw (<anonymous>)
at step (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:336)
at /Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:535
at <anonymous>
Error Message -
Request failed with status code 400
Request -
POST /
Request Data -
{
\\"x\\": 1
}
Response -
400 Bad Request
Response Data -
{
\\"error_status\\": \\"boom....\\"
}
"
`;
exports[`should work with undefined response 1`] = `
"
Error: boom....
at Object.<anonymous> (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:16:19)
at Generator.throw (<anonymous>)
at step (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:336)
at /Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:535
at <anonymous>
Error Message -
read ECONNRESET
Request -
POST /
Request Data -
{
\\"x\\": 1
}
"
`;
import util from 'util';
import MockAdapter from 'axios-mock-adapter';
import axios from 'axios';
import AxiosError from '..';
const mock = new MockAdapter(axios);
mock.onAny().reply(400, {
error_status: 'boom....',
});
const stack = `Error: boom....
at Object.<anonymous> (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:16:19)
at Generator.throw (<anonymous>)
at step (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:336)
at /Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:535
at <anonymous>
`;
it('should work', async () => {
try {
await axios.post('/', { x: 1 });
} catch (err) {
// overwrite because axios-mock-adapter set it to undefined
err.response.statusText = 'Bad Request';
const error = new AxiosError(err.response.data.error_status, err);
// overwrite stack to test it
error.stack = stack;
expect(error[util.inspect.custom]()).toMatchSnapshot();
}
});
it('should set `.status` property', async () => {
try {
await axios.post('/', { x: 1 });
} catch (err) {
// overwrite because axios-mock-adapter set it to undefined
err.response.statusText = 'Bad Request';
const error = new AxiosError(err);
expect(error.status).toBe(400);
}
});
it('should work with construct using error instance only', async () => {
try {
await axios.post('/', { x: 1 });
} catch (err) {
// overwrite because axios-mock-adapter set it to undefined
err.response.statusText = 'Bad Request';
const error = new AxiosError(err);
// overwrite stack to test it
error.stack = stack;
expect(error[util.inspect.custom]()).toMatchSnapshot();
}
});
it('should work with undefined response', async () => {
try {
await axios.post('/', { x: 1 });
} catch (err) {
// overwrite to undefined
// https://github.com/Yoctol/bottender/issues/246
err.response = undefined;
const error = new AxiosError('read ECONNRESET', err);
// overwrite stack to test it
error.stack = stack;
expect(error[util.inspect.custom]()).toMatchSnapshot();
}
});
it('should support error without axios data', () => {
const error = new AxiosError('custom error');
error.stack = stack;
expect(error[util.inspect.custom]()).toMatchSnapshot();
});
import util from 'util';
function indent(str) {
return str
.split('\n')
.map(s => (s ? ` ${s}` : ''))
.join('\n');
}
function json(data) {
return JSON.stringify(data, null, 2);
}
module.exports = class AxiosError extends Error {
constructor(messageOrErr, _err = {}) {
let err;
if (messageOrErr instanceof Error) {
super(messageOrErr.message);
err = messageOrErr;
} else {
super(messageOrErr);
err = _err;
}
const { config, request, response } = err;
this.config = config;
this.request = request;
this.response = response;
if (response && response.status) {
this.status = response.status;
}
}
// TODO: remove inspect until we drop node < 6.6
inspect(...args) {
return this[util.inspect.custom](...args);
}
[util.inspect.custom]() {
let requestMessage = '';
if (this.config) {
let { data } = this.config;
try {
data = JSON.parse(data);
} catch (_) {} // eslint-disable-line
let requestData = '';
if (this.config.data) {
requestData = `
Request Data -
${indent(json(data))}`;
}
requestMessage = `
Request -
${this.config.method.toUpperCase()} ${this.config.url}
${requestData}`;
}
let responseMessage = '';
if (this.response) {
let responseData;
if (this.response.data) {
responseData = `
Response Data -
${indent(json(this.response.data))}`;
}
responseMessage = `
Response -
${this.response.status} ${this.response.statusText}
${responseData}`;
}
return `
${this.stack}
Error Message -
${this.message}
${requestMessage}
${responseMessage}
`;
}
};
/// <reference types="node"/>
declare namespace imageType {
type ImageType =
| 'jpg'
| 'png'
| 'gif'
| 'webp'
| 'flif'
| 'cr2'
| 'tif'
| 'bmp'
| 'jxr'
| 'psd'
| 'ico'
| 'bpg'
| 'jp2'
| 'jpm'
| 'jpx'
| 'heic'
| 'cur'
| 'dcm';
interface ImageTypeResult {
/**
One of the supported [file types](https://github.com/sindresorhus/image-type#supported-file-types).
*/
ext: ImageType;
/**
The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
*/
mime: string;
}
}
declare const imageType: {
/**
Detect the image type of a `Buffer`/`Uint8Array`.
@param input - Input to examine to determine the file type. It only needs the first `.minimumBytes` bytes.
@example
```
import readChunk = require('read-chunk');
import imageType = require('image-type');
const buffer = readChunk.sync('unicorn.png', 0, 12);
imageType(buffer);
//=> {ext: 'png', mime: 'image/png'}
// Or from a remote location:
import * as http from 'http';
const url = 'https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';
http.get(url, response => {
response.on('readable', () => {
const chunk = response.read(imageType.minimumBytes);
response.destroy();
console.log(imageType(chunk));
//=> {ext: 'gif', mime: 'image/gif'}
});
});
```
*/
(input: Buffer | Uint8Array): imageType.ImageTypeResult | null;
/**
The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hard-code it.
*/
readonly minimumBytes: number;
// TODO: Remove this for the next major release
default: typeof imageType;
};
export = imageType;
'use strict';
const fileType = require('file-type');
const imageExts = new Set([
'jpg',
'png',
'gif',
'webp',
'flif',
'cr2',
'tif',
'bmp',
'jxr',
'psd',
'ico',
'bpg',
'jp2',
'jpm',
'jpx',
'heic',
'cur',
'dcm'
]);
const imageType = input => {
const ret = fileType(input);
return imageExts.has(ret && ret.ext) ? ret : null;
};
module.exports = imageType;
// TODO: Remove this for the next major release
module.exports.default = imageType;
Object.defineProperty(imageType, 'minimumBytes', {value: fileType.minimumBytes});
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/// <reference types="node"/>
import {Readable as ReadableStream} from 'stream';
declare namespace fileType {
type FileType =
| 'jpg'
| 'png'
| 'gif'
| 'webp'
| 'flif'
| 'cr2'
| 'tif'
| 'bmp'
| 'jxr'
| 'psd'
| 'zip'
| 'tar'
| 'rar'
| 'gz'
| 'bz2'
| '7z'
| 'dmg'
| 'mp4'
| 'm4v'
| 'mid'
| 'mkv'
| 'webm'
| 'mov'
| 'avi'
| 'wmv'
| 'mpg'
| 'mp2'
| 'mp3'
| 'm4a'
| 'ogg'
| 'opus'
| 'flac'
| 'wav'
| 'qcp'
| 'amr'
| 'pdf'
| 'epub'
| 'mobi'
| 'exe'
| 'swf'
| 'rtf'
| 'woff'
| 'woff2'
| 'eot'
| 'ttf'
| 'otf'
| 'ico'
| 'flv'
| 'ps'
| 'xz'
| 'sqlite'
| 'nes'
| 'crx'
| 'xpi'
| 'cab'
| 'deb'
| 'ar'
| 'rpm'
| 'Z'
| 'lz'
| 'msi'
| 'mxf'
| 'mts'
| 'wasm'
| 'blend'
| 'bpg'
| 'docx'
| 'pptx'
| 'xlsx'
| '3gp'
| 'jp2'
| 'jpm'
| 'jpx'
| 'mj2'
| 'aif'
| 'odt'
| 'ods'
| 'odp'
| 'xml'
| 'heic'
| 'cur'
| 'ktx'
| 'ape'
| 'wv'
| 'asf'
| 'wma'
| 'wmv'
| 'dcm'
| 'mpc'
| 'ics'
| 'glb'
| 'pcap';
interface FileTypeResult {
/**
One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
*/
ext: FileType;
/**
The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
*/
mime: string;
}
type ReadableStreamWithFileType = ReadableStream & {
readonly fileType: FileTypeResult | null;
};
}
declare const fileType: {
/**
Detect the file type of a `Buffer`/`Uint8Array`/`ArrayBuffer`. The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
@param buffer - It only needs the first `.minimumBytes` bytes. The exception is detection of `docx`, `pptx`, and `xlsx` which potentially requires reading the whole file.
@returns An object with the detected file type and MIME type or `null` when there was no match.
@example
```
import readChunk = require('read-chunk');
import fileType = require('file-type');
const buffer = readChunk.sync('unicorn.png', 0, fileType.minimumBytes);
fileType(buffer);
//=> {ext: 'png', mime: 'image/png'}
// Or from a remote location:
import * as http from 'http';
const url = 'https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';
http.get(url, response => {
response.on('readable', () => {
const chunk = response.read(fileType.minimumBytes);
response.destroy();
console.log(fileType(chunk));
//=> {ext: 'gif', mime: 'image/gif'}
});
});
```
*/
(buffer: Buffer | Uint8Array | ArrayBuffer): fileType.FileTypeResult | null;
/**
The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hard-code it.
*/
readonly minimumBytes: number;
/**
Detect the file type of a readable stream.
@param readableStream - A readable stream containing a file to examine, see: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable).
@returns A `Promise` which resolves to the original readable stream argument, but with an added `fileType` property, which is an object like the one returned from `fileType()`.
@example
```
import * as fs from 'fs';
import * as crypto from 'crypto';
import fileType = require('file-type');
(async () => {
const read = fs.createReadStream('encrypted.enc');
const decipher = crypto.createDecipheriv(alg, key, iv);
const stream = await fileType.stream(read.pipe(decipher));
console.log(stream.fileType);
//=> {ext: 'mov', mime: 'video/quicktime'}
const write = fs.createWriteStream(`decrypted.${stream.fileType.ext}`);
stream.pipe(write);
})();
```
*/
readonly stream: (
readableStream: ReadableStream
) => Promise<fileType.ReadableStreamWithFileType>;
// TODO: Remove this for the next major release
readonly default: typeof fileType;
};
export = fileType;
'use strict';
const toBytes = s => [...s].map(c => c.charCodeAt(0));
const xpiZipFilename = toBytes('META-INF/mozilla.rsa');
const oxmlContentTypes = toBytes('[Content_Types].xml');
const oxmlRels = toBytes('_rels/.rels');
function readUInt64LE(buf, offset = 0) {
let n = buf[offset];
let mul = 1;
let i = 0;
while (++i < 8) {
mul *= 0x100;
n += buf[offset + i] * mul;
}
return n;
}
const fileType = input => {
if (!(input instanceof Uint8Array || input instanceof ArrayBuffer || Buffer.isBuffer(input))) {
throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\` or \`ArrayBuffer\`, got \`${typeof input}\``);
}
const buf = input instanceof Uint8Array ? input : new Uint8Array(input);
if (!(buf && buf.length > 1)) {
return null;
}
const check = (header, options) => {
options = Object.assign({
offset: 0
}, options);
for (let i = 0; i < header.length; i++) {
// If a bitmask is set
if (options.mask) {
// If header doesn't equal `buf` with bits masked off
if (header[i] !== (options.mask[i] & buf[i + options.offset])) {
return false;
}
} else if (header[i] !== buf[i + options.offset]) {
return false;
}
}
return true;
};
const checkString = (header, options) => check(toBytes(header), options);
if (check([0xFF, 0xD8, 0xFF])) {
return {
ext: 'jpg',
mime: 'image/jpeg'
};
}
if (check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
return {
ext: 'png',
mime: 'image/png'
};
}
if (check([0x47, 0x49, 0x46])) {
return {
ext: 'gif',
mime: 'image/gif'
};
}
if (check([0x57, 0x45, 0x42, 0x50], {offset: 8})) {
return {
ext: 'webp',
mime: 'image/webp'
};
}
if (check([0x46, 0x4C, 0x49, 0x46])) {
return {
ext: 'flif',
mime: 'image/flif'
};
}
// Needs to be before `tif` check
if (
(check([0x49, 0x49, 0x2A, 0x0]) || check([0x4D, 0x4D, 0x0, 0x2A])) &&
check([0x43, 0x52], {offset: 8})
) {
return {
ext: 'cr2',
mime: 'image/x-canon-cr2'
};
}
if (
check([0x49, 0x49, 0x2A, 0x0]) ||
check([0x4D, 0x4D, 0x0, 0x2A])
) {
return {
ext: 'tif',
mime: 'image/tiff'
};
}
if (check([0x42, 0x4D])) {
return {
ext: 'bmp',
mime: 'image/bmp'
};
}
if (check([0x49, 0x49, 0xBC])) {
return {
ext: 'jxr',
mime: 'image/vnd.ms-photo'
};
}
if (check([0x38, 0x42, 0x50, 0x53])) {
return {
ext: 'psd',
mime: 'image/vnd.adobe.photoshop'
};
}
// Zip-based file formats
// Need to be before the `zip` check
if (check([0x50, 0x4B, 0x3, 0x4])) {
if (
check([0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x65, 0x70, 0x75, 0x62, 0x2B, 0x7A, 0x69, 0x70], {offset: 30})
) {
return {
ext: 'epub',
mime: 'application/epub+zip'
};
}
// Assumes signed `.xpi` from addons.mozilla.org
if (check(xpiZipFilename, {offset: 30})) {
return {
ext: 'xpi',
mime: 'application/x-xpinstall'
};
}
if (checkString('mimetypeapplication/vnd.oasis.opendocument.text', {offset: 30})) {
return {
ext: 'odt',
mime: 'application/vnd.oasis.opendocument.text'
};
}
if (checkString('mimetypeapplication/vnd.oasis.opendocument.spreadsheet', {offset: 30})) {
return {
ext: 'ods',
mime: 'application/vnd.oasis.opendocument.spreadsheet'
};
}
if (checkString('mimetypeapplication/vnd.oasis.opendocument.presentation', {offset: 30})) {
return {
ext: 'odp',
mime: 'application/vnd.oasis.opendocument.presentation'
};
}
// The docx, xlsx and pptx file types extend the Office Open XML file format:
// https://en.wikipedia.org/wiki/Office_Open_XML_file_formats
// We look for:
// - one entry named '[Content_Types].xml' or '_rels/.rels',
// - one entry indicating specific type of file.
// MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it.
const findNextZipHeaderIndex = (arr, startAt = 0) => arr.findIndex((el, i, arr) => i >= startAt && arr[i] === 0x50 && arr[i + 1] === 0x4B && arr[i + 2] === 0x3 && arr[i + 3] === 0x4);
let zipHeaderIndex = 0; // The first zip header was already found at index 0
let oxmlFound = false;
let type = null;
do {
const offset = zipHeaderIndex + 30;
if (!oxmlFound) {
oxmlFound = (check(oxmlContentTypes, {offset}) || check(oxmlRels, {offset}));
}
if (!type) {
if (checkString('word/', {offset})) {
type = {
ext: 'docx',
mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
};
} else if (checkString('ppt/', {offset})) {
type = {
ext: 'pptx',
mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
};
} else if (checkString('xl/', {offset})) {
type = {
ext: 'xlsx',
mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
};
}
}
if (oxmlFound && type) {
return type;
}
zipHeaderIndex = findNextZipHeaderIndex(buf, offset);
} while (zipHeaderIndex >= 0);
// No more zip parts available in the buffer, but maybe we are almost certain about the type?
if (type) {
return type;
}
}
if (
check([0x50, 0x4B]) &&
(buf[2] === 0x3 || buf[2] === 0x5 || buf[2] === 0x7) &&
(buf[3] === 0x4 || buf[3] === 0x6 || buf[3] === 0x8)
) {
return {
ext: 'zip',
mime: 'application/zip'
};
}
if (check([0x75, 0x73, 0x74, 0x61, 0x72], {offset: 257})) {
return {
ext: 'tar',
mime: 'application/x-tar'
};
}
if (
check([0x52, 0x61, 0x72, 0x21, 0x1A, 0x7]) &&
(buf[6] === 0x0 || buf[6] === 0x1)
) {
return {
ext: 'rar',
mime: 'application/x-rar-compressed'
};
}
if (check([0x1F, 0x8B, 0x8])) {
return {
ext: 'gz',
mime: 'application/gzip'
};
}
if (check([0x42, 0x5A, 0x68])) {
return {
ext: 'bz2',
mime: 'application/x-bzip2'
};
}
if (check([0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])) {
return {
ext: '7z',
mime: 'application/x-7z-compressed'
};
}
if (check([0x78, 0x01])) {
return {
ext: 'dmg',
mime: 'application/x-apple-diskimage'
};
}
if (check([0x33, 0x67, 0x70, 0x35]) || // 3gp5
(
check([0x0, 0x0, 0x0]) && check([0x66, 0x74, 0x79, 0x70], {offset: 4}) &&
(
check([0x6D, 0x70, 0x34, 0x31], {offset: 8}) || // MP41
check([0x6D, 0x70, 0x34, 0x32], {offset: 8}) || // MP42
check([0x69, 0x73, 0x6F, 0x6D], {offset: 8}) || // ISOM
check([0x69, 0x73, 0x6F, 0x32], {offset: 8}) || // ISO2
check([0x6D, 0x6D, 0x70, 0x34], {offset: 8}) || // MMP4
check([0x4D, 0x34, 0x56], {offset: 8}) || // M4V
check([0x64, 0x61, 0x73, 0x68], {offset: 8}) // DASH
)
)) {
return {
ext: 'mp4',
mime: 'video/mp4'
};
}
if (check([0x4D, 0x54, 0x68, 0x64])) {
return {
ext: 'mid',
mime: 'audio/midi'
};
}
// https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska
if (check([0x1A, 0x45, 0xDF, 0xA3])) {
const sliced = buf.subarray(4, 4 + 4096);
const idPos = sliced.findIndex((el, i, arr) => arr[i] === 0x42 && arr[i + 1] === 0x82);
if (idPos !== -1) {
const docTypePos = idPos + 3;
const findDocType = type => [...type].every((c, i) => sliced[docTypePos + i] === c.charCodeAt(0));
if (findDocType('matroska')) {
return {
ext: 'mkv',
mime: 'video/x-matroska'
};
}
if (findDocType('webm')) {
return {
ext: 'webm',
mime: 'video/webm'
};
}
}
}
if (check([0x0, 0x0, 0x0, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20]) ||
check([0x66, 0x72, 0x65, 0x65], {offset: 4}) || // Type: `free`
check([0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20], {offset: 4}) ||
check([0x6D, 0x64, 0x61, 0x74], {offset: 4}) || // MJPEG
check([0x6D, 0x6F, 0x6F, 0x76], {offset: 4}) || // Type: `moov`
check([0x77, 0x69, 0x64, 0x65], {offset: 4})) {
return {
ext: 'mov',
mime: 'video/quicktime'
};
}
// RIFF file format which might be AVI, WAV, QCP, etc
if (check([0x52, 0x49, 0x46, 0x46])) {
if (check([0x41, 0x56, 0x49], {offset: 8})) {
return {
ext: 'avi',
mime: 'video/vnd.avi'
};
}
if (check([0x57, 0x41, 0x56, 0x45], {offset: 8})) {
return {
ext: 'wav',
mime: 'audio/vnd.wave'
};
}
// QLCM, QCP file
if (check([0x51, 0x4C, 0x43, 0x4D], {offset: 8})) {
return {
ext: 'qcp',
mime: 'audio/qcelp'
};
}
}
// ASF_Header_Object first 80 bytes
if (check([0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9])) {
// Search for header should be in first 1KB of file.
let offset = 30;
do {
const objectSize = readUInt64LE(buf, offset + 16);
if (check([0x91, 0x07, 0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65], {offset})) {
// Sync on Stream-Properties-Object (B7DC0791-A9B7-11CF-8EE6-00C00C205365)
if (check([0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B], {offset: offset + 24})) {
// Found audio:
return {
ext: 'wma',
mime: 'audio/x-ms-wma'
};
}
if (check([0xC0, 0xEF, 0x19, 0xBC, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B], {offset: offset + 24})) {
// Found video:
return {
ext: 'wmv',
mime: 'video/x-ms-asf'
};
}
break;
}
offset += objectSize;
} while (offset + 24 <= buf.length);
// Default to ASF generic extension
return {
ext: 'asf',
mime: 'application/vnd.ms-asf'
};
}
if (
check([0x0, 0x0, 0x1, 0xBA]) ||
check([0x0, 0x0, 0x1, 0xB3])
) {
return {
ext: 'mpg',
mime: 'video/mpeg'
};
}
if (check([0x66, 0x74, 0x79, 0x70, 0x33, 0x67], {offset: 4})) {
return {
ext: '3gp',
mime: 'video/3gpp'
};
}
// Check for MPEG header at different starting offsets
for (let start = 0; start < 2 && start < (buf.length - 16); start++) {
if (
check([0x49, 0x44, 0x33], {offset: start}) || // ID3 header
check([0xFF, 0xE2], {offset: start, mask: [0xFF, 0xE2]}) // MPEG 1 or 2 Layer 3 header
) {
return {
ext: 'mp3',
mime: 'audio/mpeg'
};
}
if (
check([0xFF, 0xE4], {offset: start, mask: [0xFF, 0xE4]}) // MPEG 1 or 2 Layer 2 header
) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}
if (
check([0xFF, 0xF8], {offset: start, mask: [0xFF, 0xFC]}) // MPEG 2 layer 0 using ADTS
) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}
if (
check([0xFF, 0xF0], {offset: start, mask: [0xFF, 0xFC]}) // MPEG 4 layer 0 using ADTS
) {
return {
ext: 'mp4',
mime: 'audio/mpeg'
};
}
}
if (
check([0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x41], {offset: 4})
) {
return { // MPEG-4 layer 3 (audio)
ext: 'm4a',
mime: 'audio/mp4' // RFC 4337
};
}
// Needs to be before `ogg` check
if (check([0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64], {offset: 28})) {
return {
ext: 'opus',
mime: 'audio/opus'
};
}
// If 'OggS' in first bytes, then OGG container
if (check([0x4F, 0x67, 0x67, 0x53])) {
// This is a OGG container
// If ' theora' in header.
if (check([0x80, 0x74, 0x68, 0x65, 0x6F, 0x72, 0x61], {offset: 28})) {
return {
ext: 'ogv',
mime: 'video/ogg'
};
}
// If '\x01video' in header.
if (check([0x01, 0x76, 0x69, 0x64, 0x65, 0x6F, 0x00], {offset: 28})) {
return {
ext: 'ogm',
mime: 'video/ogg'
};
}
// If ' FLAC' in header https://xiph.org/flac/faq.html
if (check([0x7F, 0x46, 0x4C, 0x41, 0x43], {offset: 28})) {
return {
ext: 'oga',
mime: 'audio/ogg'
};
}
// 'Speex ' in header https://en.wikipedia.org/wiki/Speex
if (check([0x53, 0x70, 0x65, 0x65, 0x78, 0x20, 0x20], {offset: 28})) {
return {
ext: 'spx',
mime: 'audio/ogg'
};
}
// If '\x01vorbis' in header
if (check([0x01, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73], {offset: 28})) {
return {
ext: 'ogg',
mime: 'audio/ogg'
};
}
// Default OGG container https://www.iana.org/assignments/media-types/application/ogg
return {
ext: 'ogx',
mime: 'application/ogg'
};
}
if (check([0x66, 0x4C, 0x61, 0x43])) {
return {
ext: 'flac',
mime: 'audio/x-flac'
};
}
if (check([0x4D, 0x41, 0x43, 0x20])) { // 'MAC '
return {
ext: 'ape',
mime: 'audio/ape'
};
}
if (check([0x77, 0x76, 0x70, 0x6B])) { // 'wvpk'
return {
ext: 'wv',
mime: 'audio/wavpack'
};
}
if (check([0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A])) {
return {
ext: 'amr',
mime: 'audio/amr'
};
}
if (check([0x25, 0x50, 0x44, 0x46])) {
return {
ext: 'pdf',
mime: 'application/pdf'
};
}
if (check([0x4D, 0x5A])) {
return {
ext: 'exe',
mime: 'application/x-msdownload'
};
}
if (
(buf[0] === 0x43 || buf[0] === 0x46) &&
check([0x57, 0x53], {offset: 1})
) {
return {
ext: 'swf',
mime: 'application/x-shockwave-flash'
};
}
if (check([0x7B, 0x5C, 0x72, 0x74, 0x66])) {
return {
ext: 'rtf',
mime: 'application/rtf'
};
}
if (check([0x00, 0x61, 0x73, 0x6D])) {
return {
ext: 'wasm',
mime: 'application/wasm'
};
}
if (
check([0x77, 0x4F, 0x46, 0x46]) &&
(
check([0x00, 0x01, 0x00, 0x00], {offset: 4}) ||
check([0x4F, 0x54, 0x54, 0x4F], {offset: 4})
)
) {
return {
ext: 'woff',
mime: 'font/woff'
};
}
if (
check([0x77, 0x4F, 0x46, 0x32]) &&
(
check([0x00, 0x01, 0x00, 0x00], {offset: 4}) ||
check([0x4F, 0x54, 0x54, 0x4F], {offset: 4})
)
) {
return {
ext: 'woff2',
mime: 'font/woff2'
};
}
if (
check([0x4C, 0x50], {offset: 34}) &&
(
check([0x00, 0x00, 0x01], {offset: 8}) ||
check([0x01, 0x00, 0x02], {offset: 8}) ||
check([0x02, 0x00, 0x02], {offset: 8})
)
) {
return {
ext: 'eot',
mime: 'application/vnd.ms-fontobject'
};
}
if (check([0x00, 0x01, 0x00, 0x00, 0x00])) {
return {
ext: 'ttf',
mime: 'font/ttf'
};
}
if (check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {
return {
ext: 'otf',
mime: 'font/otf'
};
}
if (check([0x00, 0x00, 0x01, 0x00])) {
return {
ext: 'ico',
mime: 'image/x-icon'
};
}
if (check([0x00, 0x00, 0x02, 0x00])) {
return {
ext: 'cur',
mime: 'image/x-icon'
};
}
if (check([0x46, 0x4C, 0x56, 0x01])) {
return {
ext: 'flv',
mime: 'video/x-flv'
};
}
if (check([0x25, 0x21])) {
return {
ext: 'ps',
mime: 'application/postscript'
};
}
if (check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) {
return {
ext: 'xz',
mime: 'application/x-xz'
};
}
if (check([0x53, 0x51, 0x4C, 0x69])) {
return {
ext: 'sqlite',
mime: 'application/x-sqlite3'
};
}
if (check([0x4E, 0x45, 0x53, 0x1A])) {
return {
ext: 'nes',
mime: 'application/x-nintendo-nes-rom'
};
}
if (check([0x43, 0x72, 0x32, 0x34])) {
return {
ext: 'crx',
mime: 'application/x-google-chrome-extension'
};
}
if (
check([0x4D, 0x53, 0x43, 0x46]) ||
check([0x49, 0x53, 0x63, 0x28])
) {
return {
ext: 'cab',
mime: 'application/vnd.ms-cab-compressed'
};
}
// Needs to be before `ar` check
if (check([0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, 0x0A, 0x64, 0x65, 0x62, 0x69, 0x61, 0x6E, 0x2D, 0x62, 0x69, 0x6E, 0x61, 0x72, 0x79])) {
return {
ext: 'deb',
mime: 'application/x-deb'
};
}
if (check([0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E])) {
return {
ext: 'ar',
mime: 'application/x-unix-archive'
};
}
if (check([0xED, 0xAB, 0xEE, 0xDB])) {
return {
ext: 'rpm',
mime: 'application/x-rpm'
};
}
if (
check([0x1F, 0xA0]) ||
check([0x1F, 0x9D])
) {
return {
ext: 'Z',
mime: 'application/x-compress'
};
}
if (check([0x4C, 0x5A, 0x49, 0x50])) {
return {
ext: 'lz',
mime: 'application/x-lzip'
};
}
if (check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1])) {
return {
ext: 'msi',
mime: 'application/x-msi'
};
}
if (check([0x06, 0x0E, 0x2B, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0D, 0x01, 0x02, 0x01, 0x01, 0x02])) {
return {
ext: 'mxf',
mime: 'application/mxf'
};
}
if (check([0x47], {offset: 4}) && (check([0x47], {offset: 192}) || check([0x47], {offset: 196}))) {
return {
ext: 'mts',
mime: 'video/mp2t'
};
}
if (check([0x42, 0x4C, 0x45, 0x4E, 0x44, 0x45, 0x52])) {
return {
ext: 'blend',
mime: 'application/x-blender'
};
}
if (check([0x42, 0x50, 0x47, 0xFB])) {
return {
ext: 'bpg',
mime: 'image/bpg'
};
}
if (check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) {
// JPEG-2000 family
if (check([0x6A, 0x70, 0x32, 0x20], {offset: 20})) {
return {
ext: 'jp2',
mime: 'image/jp2'
};
}
if (check([0x6A, 0x70, 0x78, 0x20], {offset: 20})) {
return {
ext: 'jpx',
mime: 'image/jpx'
};
}
if (check([0x6A, 0x70, 0x6D, 0x20], {offset: 20})) {
return {
ext: 'jpm',
mime: 'image/jpm'
};
}
if (check([0x6D, 0x6A, 0x70, 0x32], {offset: 20})) {
return {
ext: 'mj2',
mime: 'image/mj2'
};
}
}
if (check([0x46, 0x4F, 0x52, 0x4D])) {
return {
ext: 'aif',
mime: 'audio/aiff'
};
}
if (checkString('<?xml ')) {
return {
ext: 'xml',
mime: 'application/xml'
};
}
if (check([0x42, 0x4F, 0x4F, 0x4B, 0x4D, 0x4F, 0x42, 0x49], {offset: 60})) {
return {
ext: 'mobi',
mime: 'application/x-mobipocket-ebook'
};
}
// File Type Box (https://en.wikipedia.org/wiki/ISO_base_media_file_format)
if (check([0x66, 0x74, 0x79, 0x70], {offset: 4})) {
if (check([0x6D, 0x69, 0x66, 0x31], {offset: 8})) {
return {
ext: 'heic',
mime: 'image/heif'
};
}
if (check([0x6D, 0x73, 0x66, 0x31], {offset: 8})) {
return {
ext: 'heic',
mime: 'image/heif-sequence'
};
}
if (check([0x68, 0x65, 0x69, 0x63], {offset: 8}) || check([0x68, 0x65, 0x69, 0x78], {offset: 8})) {
return {
ext: 'heic',
mime: 'image/heic'
};
}
if (check([0x68, 0x65, 0x76, 0x63], {offset: 8}) || check([0x68, 0x65, 0x76, 0x78], {offset: 8})) {
return {
ext: 'heic',
mime: 'image/heic-sequence'
};
}
}
if (check([0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A])) {
return {
ext: 'ktx',
mime: 'image/ktx'
};
}
if (check([0x44, 0x49, 0x43, 0x4D], {offset: 128})) {
return {
ext: 'dcm',
mime: 'application/dicom'
};
}
// Musepack, SV7
if (check([0x4D, 0x50, 0x2B])) {
return {
ext: 'mpc',
mime: 'audio/x-musepack'
};
}
// Musepack, SV8
if (check([0x4D, 0x50, 0x43, 0x4B])) {
return {
ext: 'mpc',
mime: 'audio/x-musepack'
};
}
if (check([0x42, 0x45, 0x47, 0x49, 0x4E, 0x3A])) {
return {
ext: 'ics',
mime: 'text/calendar'
};
}
if (check([0x67, 0x6C, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00])) {
return {
ext: 'glb',
mime: 'model/gltf-binary'
};
}
if (check([0xD4, 0xC3, 0xB2, 0xA1]) || check([0xA1, 0xB2, 0xC3, 0xD4])) {
return {
ext: 'pcap',
mime: 'application/vnd.tcpdump.pcap'
};
}
return null;
};
module.exports = fileType;
// TODO: Remove this for the next major release
module.exports.default = fileType;
Object.defineProperty(fileType, 'minimumBytes', {value: 4100});
module.exports.stream = readableStream => new Promise((resolve, reject) => {
// Using `eval` to work around issues when bundling with Webpack
const stream = eval('require')('stream'); // eslint-disable-line no-eval
readableStream.once('readable', () => {
const pass = new stream.PassThrough();
const chunk = readableStream.read(module.exports.minimumBytes) || readableStream.read();
try {
pass.fileType = fileType(chunk);
} catch (error) {
reject(error);
}
readableStream.unshift(chunk);
if (stream.pipeline) {
resolve(stream.pipeline(readableStream, pass, () => {}));
} else {
resolve(readableStream.pipe(pass));
}
});
});
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"_from": "file-type@^10.10.0",
"_id": "file-type@10.11.0",
"_inBundle": false,
"_integrity": "sha512-uzk64HRpUZyTGZtVuvrjP0FYxzQrBf4rojot6J65YMEbwBLB0CWm0CLojVpwpmFmxcE/lkvYICgfcGozbBq6rw==",
"_location": "/image-type/file-type",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "file-type@^10.10.0",
"name": "file-type",
"escapedName": "file-type",
"rawSpec": "^10.10.0",
"saveSpec": null,
"fetchSpec": "^10.10.0"
},
"_requiredBy": [
"/image-type"
],
"_resolved": "https://registry.npmjs.org/file-type/-/file-type-10.11.0.tgz",
"_shasum": "2961d09e4675b9fb9a3ee6b69e9cd23f43fd1890",
"_spec": "file-type@^10.10.0",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\image-type",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"bugs": {
"url": "https://github.com/sindresorhus/file-type/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer",
"devDependencies": {
"@types/node": "^11.12.2",
"ava": "^1.4.1",
"pify": "^4.0.1",
"read-chunk": "^3.2.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
},
"engines": {
"node": ">=6"
},
"files": [
"index.js",
"index.d.ts"
],
"homepage": "https://github.com/sindresorhus/file-type#readme",
"keywords": [
"mime",
"file",
"type",
"archive",
"image",
"img",
"pic",
"picture",
"flash",
"photo",
"video",
"detect",
"check",
"is",
"exif",
"exe",
"binary",
"buffer",
"uint8array",
"jpg",
"png",
"gif",
"webp",
"flif",
"cr2",
"tif",
"bmp",
"jxr",
"psd",
"zip",
"tar",
"rar",
"gz",
"bz2",
"7z",
"dmg",
"mp4",
"m4v",
"mid",
"mkv",
"webm",
"mov",
"avi",
"mpg",
"mp2",
"mp3",
"m4a",
"ogg",
"opus",
"flac",
"wav",
"amr",
"pdf",
"epub",
"mobi",
"swf",
"rtf",
"woff",
"woff2",
"eot",
"ttf",
"otf",
"ico",
"flv",
"ps",
"xz",
"sqlite",
"xpi",
"cab",
"deb",
"ar",
"rpm",
"Z",
"lz",
"msi",
"mxf",
"mts",
"wasm",
"webassembly",
"blend",
"bpg",
"docx",
"pptx",
"xlsx",
"3gp",
"jp2",
"jpm",
"jpx",
"mj2",
"aif",
"odt",
"ods",
"odp",
"xml",
"heic",
"wma",
"ics",
"glb",
"pcap"
],
"license": "MIT",
"name": "file-type",
"repository": {
"type": "git",
"url": "git+https://github.com/sindresorhus/file-type.git"
},
"scripts": {
"test": "xo && ava && tsd"
},
"version": "10.11.0"
}
# file-type [![Build Status](https://travis-ci.org/sindresorhus/file-type.svg?branch=master)](https://travis-ci.org/sindresorhus/file-type)
> Detect the file type of a Buffer/Uint8Array/ArrayBuffer
The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
## Install
```
$ npm install file-type
```
<a href="https://www.patreon.com/sindresorhus">
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
</a>
## Usage
##### Node.js
```js
const readChunk = require('read-chunk');
const fileType = require('file-type');
const buffer = readChunk.sync('unicorn.png', 0, fileType.minimumBytes);
fileType(buffer);
//=> {ext: 'png', mime: 'image/png'}
```
Or from a remote location:
```js
const http = require('http');
const fileType = require('file-type');
const url = 'https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';
http.get(url, response => {
response.on('readable', () => {
const chunk = response.read(fileType.minimumBytes);
response.destroy();
console.log(fileType(chunk));
//=> {ext: 'gif', mime: 'image/gif'}
});
});
```
Or from a stream:
```js
const fs = require('fs');
const crypto = require('crypto');
const fileType = require('file-type');
(async () => {
const read = fs.createReadStream('encrypted.enc');
const decipher = crypto.createDecipheriv(alg, key, iv);
const stream = await fileType.stream(read.pipe(decipher));
console.log(stream.fileType);
//=> {ext: 'mov', mime: 'video/quicktime'}
const write = fs.createWriteStream(`decrypted.${stream.fileType.ext}`);
stream.pipe(write);
})();
```
##### Browser
```js
const xhr = new XMLHttpRequest();
xhr.open('GET', 'unicorn.png');
xhr.responseType = 'arraybuffer';
xhr.onload = () => {
fileType(new Uint8Array(this.response));
//=> {ext: 'png', mime: 'image/png'}
};
xhr.send();
```
## API
### fileType(input)
Returns an `Object` with:
- `ext` - One of the [supported file types](#supported-file-types)
- `mime` - The [MIME type](https://en.wikipedia.org/wiki/Internet_media_type)
Or `null` when there is no match.
#### input
Type: `Buffer | Uint8Array | ArrayBuffer`
It only needs the first `.minimumBytes` bytes. The exception is detection of `docx`, `pptx`, and `xlsx` which potentially requires reading the whole file.
### fileType.minimumBytes
Type: `number`
The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hardcode it.
### fileType.stream(readableStream)
Detect the file type of a readable stream.
Returns a `Promise` which resolves to the original readable stream argument, but with an added `fileType` property, which is an object like the one returned from `fileType()`.
*Note:* This method is only for Node.js.
#### readableStream
Type: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable)
## Supported file types
- [`jpg`](https://en.wikipedia.org/wiki/JPEG)
- [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics)
- [`gif`](https://en.wikipedia.org/wiki/GIF)
- [`webp`](https://en.wikipedia.org/wiki/WebP)
- [`flif`](https://en.wikipedia.org/wiki/Free_Lossless_Image_Format)
- [`cr2`](https://fileinfo.com/extension/cr2)
- [`tif`](https://en.wikipedia.org/wiki/Tagged_Image_File_Format)
- [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format)
- [`jxr`](https://en.wikipedia.org/wiki/JPEG_XR)
- [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format)
- [`zip`](https://en.wikipedia.org/wiki/Zip_(file_format))
- [`tar`](https://en.wikipedia.org/wiki/Tar_(computing)#File_format)
- [`rar`](https://en.wikipedia.org/wiki/RAR_(file_format))
- [`gz`](https://en.wikipedia.org/wiki/Gzip)
- [`bz2`](https://en.wikipedia.org/wiki/Bzip2)
- [`7z`](https://en.wikipedia.org/wiki/7z)
- [`dmg`](https://en.wikipedia.org/wiki/Apple_Disk_Image)
- [`mp4`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#Filename_extensions)
- [`m4v`](https://en.wikipedia.org/wiki/M4V)
- [`mid`](https://en.wikipedia.org/wiki/MIDI)
- [`mkv`](https://en.wikipedia.org/wiki/Matroska)
- [`webm`](https://en.wikipedia.org/wiki/WebM)
- [`mov`](https://en.wikipedia.org/wiki/QuickTime_File_Format)
- [`avi`](https://en.wikipedia.org/wiki/Audio_Video_Interleave)
- [`wmv`](https://en.wikipedia.org/wiki/Windows_Media_Video)
- [`mpg`](https://en.wikipedia.org/wiki/MPEG-1)
- [`mp2`](https://en.wikipedia.org/wiki/MPEG-1_Audio_Layer_II)
- [`mp3`](https://en.wikipedia.org/wiki/MP3)
- [`m4a`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#.MP4_versus_.M4A)
- [`ogg`](https://en.wikipedia.org/wiki/Ogg)
- [`opus`](https://en.wikipedia.org/wiki/Opus_(audio_format))
- [`flac`](https://en.wikipedia.org/wiki/FLAC)
- [`wav`](https://en.wikipedia.org/wiki/WAV)
- [`qcp`](https://en.wikipedia.org/wiki/QCP)
- [`amr`](https://en.wikipedia.org/wiki/Adaptive_Multi-Rate_audio_codec)
- [`pdf`](https://en.wikipedia.org/wiki/Portable_Document_Format)
- [`epub`](https://en.wikipedia.org/wiki/EPUB)
- [`mobi`](https://en.wikipedia.org/wiki/Mobipocket) - Mobipocket
- [`exe`](https://en.wikipedia.org/wiki/.exe)
- [`swf`](https://en.wikipedia.org/wiki/SWF)
- [`rtf`](https://en.wikipedia.org/wiki/Rich_Text_Format)
- [`woff`](https://en.wikipedia.org/wiki/Web_Open_Font_Format)
- [`woff2`](https://en.wikipedia.org/wiki/Web_Open_Font_Format)
- [`eot`](https://en.wikipedia.org/wiki/Embedded_OpenType)
- [`ttf`](https://en.wikipedia.org/wiki/TrueType)
- [`otf`](https://en.wikipedia.org/wiki/OpenType)
- [`ico`](https://en.wikipedia.org/wiki/ICO_(file_format))
- [`flv`](https://en.wikipedia.org/wiki/Flash_Video)
- [`ps`](https://en.wikipedia.org/wiki/Postscript)
- [`xz`](https://en.wikipedia.org/wiki/Xz)
- [`sqlite`](https://www.sqlite.org/fileformat2.html)
- [`nes`](https://fileinfo.com/extension/nes)
- [`crx`](https://developer.chrome.com/extensions/crx)
- [`xpi`](https://en.wikipedia.org/wiki/XPInstall)
- [`cab`](https://en.wikipedia.org/wiki/Cabinet_(file_format))
- [`deb`](https://en.wikipedia.org/wiki/Deb_(file_format))
- [`ar`](https://en.wikipedia.org/wiki/Ar_(Unix))
- [`rpm`](https://fileinfo.com/extension/rpm)
- [`Z`](https://fileinfo.com/extension/z)
- [`lz`](https://en.wikipedia.org/wiki/Lzip)
- [`msi`](https://en.wikipedia.org/wiki/Windows_Installer)
- [`mxf`](https://en.wikipedia.org/wiki/Material_Exchange_Format)
- [`mts`](https://en.wikipedia.org/wiki/.m2ts)
- [`wasm`](https://en.wikipedia.org/wiki/WebAssembly)
- [`blend`](https://wiki.blender.org/index.php/Dev:Source/Architecture/File_Format)
- [`bpg`](https://bellard.org/bpg/)
- [`docx`](https://en.wikipedia.org/wiki/Office_Open_XML)
- [`pptx`](https://en.wikipedia.org/wiki/Office_Open_XML)
- [`xlsx`](https://en.wikipedia.org/wiki/Office_Open_XML)
- [`3gp`](https://en.wikipedia.org/wiki/3GP_and_3G2)
- [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`jpm`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`jpx`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`mj2`](https://en.wikipedia.org/wiki/Motion_JPEG_2000) - Motion JPEG 2000
- [`aif`](https://en.wikipedia.org/wiki/Audio_Interchange_File_Format)
- [`odt`](https://en.wikipedia.org/wiki/OpenDocument) - OpenDocument for word processing
- [`ods`](https://en.wikipedia.org/wiki/OpenDocument) - OpenDocument for spreadsheets
- [`odp`](https://en.wikipedia.org/wiki/OpenDocument) - OpenDocument for presentations
- [`xml`](https://en.wikipedia.org/wiki/XML)
- [`heic`](https://nokiatech.github.io/heif/technical.html)
- [`cur`](https://en.wikipedia.org/wiki/ICO_(file_format))
- [`ktx`](https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/)
- [`ape`](https://en.wikipedia.org/wiki/Monkey%27s_Audio) - Monkey's Audio
- [`wv`](https://en.wikipedia.org/wiki/WavPack) - WavPack
- [`asf`](https://en.wikipedia.org/wiki/Advanced_Systems_Format) - Advanced Systems Format
- [`wma`](https://en.wikipedia.org/wiki/Windows_Media_Audio) - Windows Media Audio
- [`wmv`](https://en.wikipedia.org/wiki/Windows_Media_Video) - Windows Media Video
- [`dcm`](https://en.wikipedia.org/wiki/DICOM#Data_format) - DICOM Image File
- [`mpc`](https://en.wikipedia.org/wiki/Musepack) - Musepack (SV7 & SV8)
- [`ics`](https://en.wikipedia.org/wiki/ICalendar#Data_format) - iCalendar
- [`glb`](https://github.com/KhronosGroup/glTF) - GL Transmission Format
- [`pcap`](https://wiki.wireshark.org/Development/LibpcapFileFormat) - Libpcap File Format
*SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).*
*Pull request welcome for additional commonly used file types.*
## Related
- [file-type-cli](https://github.com/sindresorhus/file-type-cli) - CLI for this module
## Created by
- [Sindre Sorhus](https://github.com/sindresorhus)
- [Mikael Finstad](https://github.com/mifi)
## License
MIT
{
"_from": "image-type@^4.1.0",
"_id": "image-type@4.1.0",
"_inBundle": false,
"_integrity": "sha512-CFJMJ8QK8lJvRlTCEgarL4ro6hfDQKif2HjSvYCdQZESaIPV4v9imrf7BQHK+sQeTeNeMpWciR9hyC/g8ybXEg==",
"_location": "/image-type",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "image-type@^4.1.0",
"name": "image-type",
"escapedName": "image-type",
"rawSpec": "^4.1.0",
"saveSpec": null,
"fetchSpec": "^4.1.0"
},
"_requiredBy": [
"/messaging-api-line"
],
"_resolved": "https://registry.npmjs.org/image-type/-/image-type-4.1.0.tgz",
"_shasum": "72a88d64ff5021371ed67b9a466442100be57cd1",
"_spec": "image-type@^4.1.0",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\messaging-api-line",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"bugs": {
"url": "https://github.com/sindresorhus/image-type/issues"
},
"bundleDependencies": false,
"dependencies": {
"file-type": "^10.10.0"
},
"deprecated": false,
"description": "Detect the image type of a Buffer/Uint8Array",
"devDependencies": {
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"read-chunk": "^3.2.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"engines": {
"node": ">=6"
},
"files": [
"index.js",
"index.d.ts"
],
"homepage": "https://github.com/sindresorhus/image-type#readme",
"keywords": [
"image",
"img",
"pic",
"picture",
"photo",
"type",
"detect",
"check",
"is",
"exif",
"binary",
"buffer",
"uint8array",
"png",
"jpg",
"jpeg",
"gif",
"webp",
"tif",
"bmp",
"jxr",
"psd",
"mime"
],
"license": "MIT",
"name": "image-type",
"repository": {
"type": "git",
"url": "git+https://github.com/sindresorhus/image-type.git"
},
"scripts": {
"test": "xo && ava && tsd"
},
"version": "4.1.0"
}
# image-type [![Build Status](https://travis-ci.org/sindresorhus/image-type.svg?branch=master)](https://travis-ci.org/sindresorhus/image-type)
> Detect the image type of a Buffer/Uint8Array
See the [`file-type`](https://github.com/sindresorhus/file-type) module for more file types and a CLI.
## Install
```
$ npm install image-type
```
## Usage
##### Node.js
```js
const readChunk = require('read-chunk');
const imageType = require('image-type');
const buffer = readChunk.sync('unicorn.png', 0, 12);
imageType(buffer);
//=> {ext: 'png', mime: 'image/png'}
```
Or from a remote location:
```js
const http = require('http');
const imageType = require('image-type');
const url = 'https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';
http.get(url, response => {
response.on('readable', () => {
const chunk = response.read(imageType.minimumBytes);
response.destroy();
console.log(imageType(chunk));
//=> {ext: 'gif', mime: 'image/gif'}
});
});
```
##### Browser
```js
const xhr = new XMLHttpRequest();
xhr.open('GET', 'unicorn.png');
xhr.responseType = 'arraybuffer';
xhr.onload = () => {
imageType(new Uint8Array(this.response));
//=> {ext: 'png', mime: 'image/png'}
};
xhr.send();
```
## API
### imageType(input)
Returns an `Object` with:
- `ext` - One of the [supported file types](#supported-file-types)
- `mime` - The [MIME type](http://en.wikipedia.org/wiki/Internet_media_type)
Or `null` when there is no match.
#### input
Type: `Buffer | Uint8Array`
It only needs the first `.minimumBytes` bytes.
### imageType.minimumBytes
Type: `number`
The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hardcode it.
## Supported file types
- [`jpg`](https://en.wikipedia.org/wiki/JPEG)
- [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics)
- [`gif`](https://en.wikipedia.org/wiki/GIF)
- [`webp`](https://en.wikipedia.org/wiki/WebP)
- [`flif`](https://en.wikipedia.org/wiki/Free_Lossless_Image_Format)
- [`cr2`](https://fileinfo.com/extension/cr2)
- [`tif`](https://en.wikipedia.org/wiki/Tagged_Image_File_Format)
- [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format)
- [`jxr`](https://en.wikipedia.org/wiki/JPEG_XR)
- [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format)
- [`ico`](https://en.wikipedia.org/wiki/ICO_(file_format))
- [`bpg`](https://bellard.org/bpg/)
- [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`jpm`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`jpx`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`heic`](https://nokiatech.github.io/heif/technical.html)
- [`cur`](https://en.wikipedia.org/wiki/ICO_(file_format))
- [`dcm`](https://en.wikipedia.org/wiki/DICOM#Data_format) - DICOM Image File
*SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).*
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
2.2.4 / 2018-03-13
==================
* Use flow strict mode (i.e. `@flow strict`).
2.2.3 / 2018-02-19
==================
* Change license from BSD+Patents to MIT.
2.2.2 / 2016-11-15
==================
* Add LICENSE file.
* Misc housekeeping.
2.2.1 / 2016-03-09
==================
* Use `NODE_ENV` variable instead of `__DEV__` to cache `process.env.NODE_ENV`.
2.2.0 / 2015-11-17
==================
* Use `error.name` instead of `Invariant Violation`.
2.1.3 / 2015-11-17
==================
* Remove `@provideModule` pragma.
2.1.2 / 2015-10-27
==================
* Fix license.
2.1.1 / 2015-09-20
==================
* Use correct SPDX license.
* Test "browser.js" using browserify.
* Switch from "envify" to "loose-envify".
2.1.0 / 2015-06-03
==================
* Add "envify" as a dependency.
* Fixed license field in "package.json".
2.0.0 / 2015-02-21
==================
* Switch to using the "browser" field. There are now browser and server versions that respect the "format" in production.
1.0.2 / 2014-09-24
==================
* Added tests, npmignore and gitignore.
* Clarifications in README.
1.0.1 / 2014-09-24
==================
* Actually include 'invariant.js'.
1.0.0 / 2014-09-24
==================
* Initial release.
MIT License
Copyright (c) 2013-present, Facebook, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# invariant
[![Build Status](https://travis-ci.org/zertosh/invariant.svg?branch=master)](https://travis-ci.org/zertosh/invariant)
A mirror of Facebook's `invariant` (e.g. [React](https://github.com/facebook/react/blob/v0.13.3/src/vendor/core/invariant.js), [flux](https://github.com/facebook/flux/blob/2.0.2/src/invariant.js)).
A way to provide descriptive errors in development but generic errors in production.
## Install
With [npm](http://npmjs.org) do:
```sh
npm install invariant
```
## `invariant(condition, message)`
```js
var invariant = require('invariant');
invariant(someTruthyVal, 'This will not throw');
// No errors
invariant(someFalseyVal, 'This will throw an error with this message');
// Error: Invariant Violation: This will throw an error with this message
```
**Note:** When `process.env.NODE_ENV` is not `production`, the message is required. If omitted, `invariant` will throw regardless of the truthiness of the condition. When `process.env.NODE_ENV` is `production`, the message is optional – so they can be minified away.
### Browser
When used with [browserify](https://github.com/substack/node-browserify), it'll use `browser.js` (instead of `invariant.js`) and the [envify](https://github.com/hughsk/envify) transform will inline the value of `process.env.NODE_ENV`.
### Node
The node version is optimized around the performance implications of accessing `process.env`. The value of `process.env.NODE_ENV` is cached, and repeatedly used instead of reading `process.env`. See [Server rendering is slower with npm react #812](https://github.com/facebook/react/issues/812)
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* Use invariant() to assert state which your program assumes to be true.
*
* Provide sprintf-style format (only %s is supported) and arguments
* to provide information about what broke and what you were
* expecting.
*
* The invariant message will be stripped in production, but the invariant
* will remain to ensure logic does not differ in production.
*/
var invariant = function(condition, format, a, b, c, d, e, f) {
if (process.env.NODE_ENV !== 'production') {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
}
if (!condition) {
var error;
if (format === undefined) {
error = new Error(
'Minified exception occurred; use the non-minified dev environment ' +
'for the full error message and additional helpful warnings.'
);
} else {
var args = [a, b, c, d, e, f];
var argIndex = 0;
error = new Error(
format.replace(/%s/g, function() { return args[argIndex++]; })
);
error.name = 'Invariant Violation';
}
error.framesToPop = 1; // we don't care about invariant's own frame
throw error;
}
};
module.exports = invariant;
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* Use invariant() to assert state which your program assumes to be true.
*
* Provide sprintf-style format (only %s is supported) and arguments
* to provide information about what broke and what you were
* expecting.
*
* The invariant message will be stripped in production, but the invariant
* will remain to ensure logic does not differ in production.
*/
var NODE_ENV = process.env.NODE_ENV;
var invariant = function(condition, format, a, b, c, d, e, f) {
if (NODE_ENV !== 'production') {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
}
if (!condition) {
var error;
if (format === undefined) {
error = new Error(
'Minified exception occurred; use the non-minified dev environment ' +
'for the full error message and additional helpful warnings.'
);
} else {
var args = [a, b, c, d, e, f];
var argIndex = 0;
error = new Error(
format.replace(/%s/g, function() { return args[argIndex++]; })
);
error.name = 'Invariant Violation';
}
error.framesToPop = 1; // we don't care about invariant's own frame
throw error;
}
};
module.exports = invariant;
/* @flow strict */
declare module.exports: (
condition: any,
format?: string,
...args: Array<any>
) => void;
{
"_from": "invariant@^2.2.4",
"_id": "invariant@2.2.4",
"_inBundle": false,
"_integrity": "sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA==",
"_location": "/invariant",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "invariant@^2.2.4",
"name": "invariant",
"escapedName": "invariant",
"rawSpec": "^2.2.4",
"saveSpec": null,
"fetchSpec": "^2.2.4"
},
"_requiredBy": [
"/messaging-api-line"
],
"_resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.4.tgz",
"_shasum": "610f3c92c9359ce1db616e538008d23ff35158e6",
"_spec": "invariant@^2.2.4",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\messaging-api-line",
"author": {
"name": "Andres Suarez",
"email": "zertosh@gmail.com"
},
"browser": "browser.js",
"browserify": {
"transform": [
"loose-envify"
]
},
"bugs": {
"url": "https://github.com/zertosh/invariant/issues"
},
"bundleDependencies": false,
"dependencies": {
"loose-envify": "^1.0.0"
},
"deprecated": false,
"description": "invariant",
"devDependencies": {
"browserify": "^11.0.1",
"flow-bin": "^0.67.1",
"tap": "^1.4.0"
},
"files": [
"browser.js",
"invariant.js",
"invariant.js.flow"
],
"homepage": "https://github.com/zertosh/invariant#readme",
"keywords": [
"test",
"invariant"
],
"license": "MIT",
"main": "invariant.js",
"name": "invariant",
"repository": {
"type": "git",
"url": "git+https://github.com/zertosh/invariant.git"
},
"scripts": {
"test": "NODE_ENV=production tap test/*.js && NODE_ENV=development tap test/*.js"
},
"version": "2.2.4"
}
### Version 4.0.0 (2018-01-28) ###
- Added: Support for ES2018. The only change needed was recognizing the `s`
regex flag.
- Changed: _All_ tokens returned by the `matchToToken` function now have a
`closed` property. It is set to `undefined` for the tokens where “closed”
doesn’t make sense. This means that all tokens objects have the same shape,
which might improve performance.
These are the breaking changes:
- `'/a/s'.match(jsTokens)` no longer returns `['/', 'a', '/', 's']`, but
`['/a/s']`. (There are of course other variations of this.)
- Code that rely on some token objects not having the `closed` property could
now behave differently.
### Version 3.0.2 (2017-06-28) ###
- No code changes. Just updates to the readme.
### Version 3.0.1 (2017-01-30) ###
- Fixed: ES2015 unicode escapes with more than 6 hex digits are now matched
correctly.
### Version 3.0.0 (2017-01-11) ###
This release contains one breaking change, that should [improve performance in
V8][v8-perf]:
> So how can you, as a JavaScript developer, ensure that your RegExps are fast?
> If you are not interested in hooking into RegExp internals, make sure that
> neither the RegExp instance, nor its prototype is modified in order to get the
> best performance:
>
> ```js
> var re = /./g;
> re.exec(''); // Fast path.
> re.new_property = 'slow';
> ```
This module used to export a single regex, with `.matchToToken` bolted
on, just like in the above example. This release changes the exports of
the module to avoid this issue.
Before:
```js
import jsTokens from "js-tokens"
// or:
var jsTokens = require("js-tokens")
var matchToToken = jsTokens.matchToToken
```
After:
```js
import jsTokens, {matchToToken} from "js-tokens"
// or:
var jsTokens = require("js-tokens").default
var matchToToken = require("js-tokens").matchToToken
```
[v8-perf]: http://v8project.blogspot.se/2017/01/speeding-up-v8-regular-expressions.html
### Version 2.0.0 (2016-06-19) ###
- Added: Support for ES2016. In other words, support for the `**` exponentiation
operator.
These are the breaking changes:
- `'**'.match(jsTokens)` no longer returns `['*', '*']`, but `['**']`.
- `'**='.match(jsTokens)` no longer returns `['*', '*=']`, but `['**=']`.
### Version 1.0.3 (2016-03-27) ###
- Improved: Made the regex ever so slightly smaller.
- Updated: The readme.
### Version 1.0.2 (2015-10-18) ###
- Improved: Limited npm package contents for a smaller download. Thanks to
@zertosh!
### Version 1.0.1 (2015-06-20) ###
- Fixed: Declared an undeclared variable.
### Version 1.0.0 (2015-02-26) ###
- Changed: Merged the 'operator' and 'punctuation' types into 'punctuator'. That
type is now equivalent to the Punctuator token in the ECMAScript
specification. (Backwards-incompatible change.)
- Fixed: A `-` followed by a number is now correctly matched as a punctuator
followed by a number. It used to be matched as just a number, but there is no
such thing as negative number literals. (Possibly backwards-incompatible
change.)
### Version 0.4.1 (2015-02-21) ###
- Added: Support for the regex `u` flag.
### Version 0.4.0 (2015-02-21) ###
- Improved: `jsTokens.matchToToken` performance.
- Added: Support for octal and binary number literals.
- Added: Support for template strings.
### Version 0.3.1 (2015-01-06) ###
- Fixed: Support for unicode spaces. They used to be allowed in names (which is
very confusing), and some unicode newlines were wrongly allowed in strings and
regexes.
### Version 0.3.0 (2014-12-19) ###
- Changed: The `jsTokens.names` array has been replaced with the
`jsTokens.matchToToken` function. The capturing groups of `jsTokens` are no
longer part of the public API; instead use said function. See this [gist] for
an example. (Backwards-incompatible change.)
- Changed: The empty string is now considered an “invalid” token, instead an
“empty” token (its own group). (Backwards-incompatible change.)
- Removed: component support. (Backwards-incompatible change.)
[gist]: https://gist.github.com/lydell/be49dbf80c382c473004
### Version 0.2.0 (2014-06-19) ###
- Changed: Match ES6 function arrows (`=>`) as an operator, instead of its own
category (“functionArrow”), for simplicity. (Backwards-incompatible change.)
- Added: ES6 splats (`...`) are now matched as an operator (instead of three
punctuations). (Backwards-incompatible change.)
### Version 0.1.0 (2014-03-08) ###
- Initial release.
The MIT License (MIT)
Copyright (c) 2014, 2015, 2016, 2017, 2018 Simon Lydell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Overview [![Build Status](https://travis-ci.org/lydell/js-tokens.svg?branch=master)](https://travis-ci.org/lydell/js-tokens)
========
A regex that tokenizes JavaScript.
```js
var jsTokens = require("js-tokens").default
var jsString = "var foo=opts.foo;\n..."
jsString.match(jsTokens)
// ["var", " ", "foo", "=", "opts", ".", "foo", ";", "\n", ...]
```
Installation
============
`npm install js-tokens`
```js
import jsTokens from "js-tokens"
// or:
var jsTokens = require("js-tokens").default
```
Usage
=====
### `jsTokens` ###
A regex with the `g` flag that matches JavaScript tokens.
The regex _always_ matches, even invalid JavaScript and the empty string.
The next match is always directly after the previous.
### `var token = matchToToken(match)` ###
```js
import {matchToToken} from "js-tokens"
// or:
var matchToToken = require("js-tokens").matchToToken
```
Takes a `match` returned by `jsTokens.exec(string)`, and returns a `{type:
String, value: String}` object. The following types are available:
- string
- comment
- regex
- number
- name
- punctuator
- whitespace
- invalid
Multi-line comments and strings also have a `closed` property indicating if the
token was closed or not (see below).
Comments and strings both come in several flavors. To distinguish them, check if
the token starts with `//`, `/*`, `'`, `"` or `` ` ``.
Names are ECMAScript IdentifierNames, that is, including both identifiers and
keywords. You may use [is-keyword-js] to tell them apart.
Whitespace includes both line terminators and other whitespace.
[is-keyword-js]: https://github.com/crissdev/is-keyword-js
ECMAScript support
==================
The intention is to always support the latest ECMAScript version whose feature
set has been finalized.
If adding support for a newer version requires changes, a new version with a
major verion bump will be released.
Currently, ECMAScript 2018 is supported.
Invalid code handling
=====================
Unterminated strings are still matched as strings. JavaScript strings cannot
contain (unescaped) newlines, so unterminated strings simply end at the end of
the line. Unterminated template strings can contain unescaped newlines, though,
so they go on to the end of input.
Unterminated multi-line comments are also still matched as comments. They
simply go on to the end of the input.
Unterminated regex literals are likely matched as division and whatever is
inside the regex.
Invalid ASCII characters have their own capturing group.
Invalid non-ASCII characters are treated as names, to simplify the matching of
names (except unicode spaces which are treated as whitespace). Note: See also
the [ES2018](#es2018) section.
Regex literals may contain invalid regex syntax. They are still matched as
regex literals. They may also contain repeated regex flags, to keep the regex
simple.
Strings may contain invalid escape sequences.
Limitations
===========
Tokenizing JavaScript using regexes—in fact, _one single regex_—won’t be
perfect. But that’s not the point either.
You may compare jsTokens with [esprima] by using `esprima-compare.js`.
See `npm run esprima-compare`!
[esprima]: http://esprima.org/
### Template string interpolation ###
Template strings are matched as single tokens, from the starting `` ` `` to the
ending `` ` ``, including interpolations (whose tokens are not matched
individually).
Matching template string interpolations requires recursive balancing of `{` and
`}`—something that JavaScript regexes cannot do. Only one level of nesting is
supported.
### Division and regex literals collision ###
Consider this example:
```js
var g = 9.82
var number = bar / 2/g
var regex = / 2/g
```
A human can easily understand that in the `number` line we’re dealing with
division, and in the `regex` line we’re dealing with a regex literal. How come?
Because humans can look at the whole code to put the `/` characters in context.
A JavaScript regex cannot. It only sees forwards. (Well, ES2018 regexes can also
look backwards. See the [ES2018](#es2018) section).
When the `jsTokens` regex scans throught the above, it will see the following
at the end of both the `number` and `regex` rows:
```js
/ 2/g
```
It is then impossible to know if that is a regex literal, or part of an
expression dealing with division.
Here is a similar case:
```js
foo /= 2/g
foo(/= 2/g)
```
The first line divides the `foo` variable with `2/g`. The second line calls the
`foo` function with the regex literal `/= 2/g`. Again, since `jsTokens` only
sees forwards, it cannot tell the two cases apart.
There are some cases where we _can_ tell division and regex literals apart,
though.
First off, we have the simple cases where there’s only one slash in the line:
```js
var foo = 2/g
foo /= 2
```
Regex literals cannot contain newlines, so the above cases are correctly
identified as division. Things are only problematic when there are more than
one non-comment slash in a single line.
Secondly, not every character is a valid regex flag.
```js
var number = bar / 2/e
```
The above example is also correctly identified as division, because `e` is not a
valid regex flag. I initially wanted to future-proof by allowing `[a-zA-Z]*`
(any letter) as flags, but it is not worth it since it increases the amount of
ambigous cases. So only the standard `g`, `m`, `i`, `y` and `u` flags are
allowed. This means that the above example will be identified as division as
long as you don’t rename the `e` variable to some permutation of `gmiyus` 1 to 6
characters long.
Lastly, we can look _forward_ for information.
- If the token following what looks like a regex literal is not valid after a
regex literal, but is valid in a division expression, then the regex literal
is treated as division instead. For example, a flagless regex cannot be
followed by a string, number or name, but all of those three can be the
denominator of a division.
- Generally, if what looks like a regex literal is followed by an operator, the
regex literal is treated as division instead. This is because regexes are
seldomly used with operators (such as `+`, `*`, `&&` and `==`), but division
could likely be part of such an expression.
Please consult the regex source and the test cases for precise information on
when regex or division is matched (should you need to know). In short, you
could sum it up as:
If the end of a statement looks like a regex literal (even if it isn’t), it
will be treated as one. Otherwise it should work as expected (if you write sane
code).
### ES2018 ###
ES2018 added some nice regex improvements to the language.
- [Unicode property escapes] should allow telling names and invalid non-ASCII
characters apart without blowing up the regex size.
- [Lookbehind assertions] should allow matching telling division and regex
literals apart in more cases.
- [Named capture groups] might simplify some things.
These things would be nice to do, but are not critical. They probably have to
wait until the oldest maintained Node.js LTS release supports those features.
[Unicode property escapes]: http://2ality.com/2017/07/regexp-unicode-property-escapes.html
[Lookbehind assertions]: http://2ality.com/2017/05/regexp-lookbehind-assertions.html
[Named capture groups]: http://2ality.com/2017/05/regexp-named-capture-groups.html
License
=======
[MIT](LICENSE).
// Copyright 2014, 2015, 2016, 2017, 2018 Simon Lydell
// License: MIT. (See LICENSE.)
Object.defineProperty(exports, "__esModule", {
value: true
})
// This regex comes from regex.coffee, and is inserted here by generate-index.js
// (run `npm run build`).
exports.default = /((['"])(?:(?!\2|\\).|\\(?:\r\n|[\s\S]))*(\2)?|`(?:[^`\\$]|\\[\s\S]|\$(?!\{)|\$\{(?:[^{}]|\{[^}]*\}?)*\}?)*(`)?)|(\/\/.*)|(\/\*(?:[^*]|\*(?!\/))*(\*\/)?)|(\/(?!\*)(?:\[(?:(?![\]\\]).|\\.)*\]|(?![\/\]\\]).|\\.)+\/(?:(?!\s*(?:\b|[\u0080-\uFFFF$\\'"~({]|[+\-!](?!=)|\.?\d))|[gmiyus]{1,6}\b(?![\u0080-\uFFFF$\\]|\s*(?:[+\-*%&|^<>!=?({]|\/(?![\/*])))))|(0[xX][\da-fA-F]+|0[oO][0-7]+|0[bB][01]+|(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?)|((?!\d)(?:(?!\s)[$\w\u0080-\uFFFF]|\\u[\da-fA-F]{4}|\\u\{[\da-fA-F]+\})+)|(--|\+\+|&&|\|\||=>|\.{3}|(?:[+\-\/%&|^]|\*{1,2}|<{1,2}|>{1,3}|!=?|={1,2})=?|[?~.,:;[\](){}])|(\s+)|(^$|[\s\S])/g
exports.matchToToken = function(match) {
var token = {type: "invalid", value: match[0], closed: undefined}
if (match[ 1]) token.type = "string" , token.closed = !!(match[3] || match[4])
else if (match[ 5]) token.type = "comment"
else if (match[ 6]) token.type = "comment", token.closed = !!match[7]
else if (match[ 8]) token.type = "regex"
else if (match[ 9]) token.type = "number"
else if (match[10]) token.type = "name"
else if (match[11]) token.type = "punctuator"
else if (match[12]) token.type = "whitespace"
return token
}
{
"_from": "js-tokens@^3.0.0 || ^4.0.0",
"_id": "js-tokens@4.0.0",
"_inBundle": false,
"_integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
"_location": "/js-tokens",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "js-tokens@^3.0.0 || ^4.0.0",
"name": "js-tokens",
"escapedName": "js-tokens",
"rawSpec": "^3.0.0 || ^4.0.0",
"saveSpec": null,
"fetchSpec": "^3.0.0 || ^4.0.0"
},
"_requiredBy": [
"/loose-envify"
],
"_resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
"_shasum": "19203fb59991df98e3a287050d4647cdeaf32499",
"_spec": "js-tokens@^3.0.0 || ^4.0.0",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\loose-envify",
"author": {
"name": "Simon Lydell"
},
"bugs": {
"url": "https://github.com/lydell/js-tokens/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "A regex that tokenizes JavaScript.",
"devDependencies": {
"coffeescript": "2.1.1",
"esprima": "4.0.0",
"everything.js": "1.0.3",
"mocha": "5.0.0"
},
"files": [
"index.js"
],
"homepage": "https://github.com/lydell/js-tokens#readme",
"keywords": [
"JavaScript",
"js",
"token",
"tokenize",
"regex"
],
"license": "MIT",
"name": "js-tokens",
"repository": {
"type": "git",
"url": "git+https://github.com/lydell/js-tokens.git"
},
"scripts": {
"build": "node generate-index.js",
"dev": "npm run build && npm test",
"esprima-compare": "node esprima-compare ./index.js everything.js/es5.js",
"test": "mocha --ui tdd"
},
"version": "4.0.0"
}
Copyright jQuery Foundation and other contributors <https://jquery.org/>
Based on Underscore.js, copyright Jeremy Ashkenas,
DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/>
This software consists of voluntary contributions made by many
individuals. For exact contribution history, see the revision history
available at https://github.com/lodash/lodash
The following license applies to all parts of this software except as
documented below:
====
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
====
Copyright and related rights for sample code are waived via CC0. Sample
code is defined as all source code displayed within the prose of the
documentation.
CC0: http://creativecommons.org/publicdomain/zero/1.0/
====
Files located in the node_modules and vendor directories are externally
maintained libraries used by this software which have their own
licenses; we recommend you read them, as their terms may differ from the
terms above.
# lodash.omit v4.5.0
The [lodash](https://lodash.com/) method `_.omit` exported as a [Node.js](https://nodejs.org/) module.
## Installation
Using npm:
```bash
$ {sudo -H} npm i -g npm
$ npm i --save lodash.omit
```
In Node.js:
```js
var omit = require('lodash.omit');
```
See the [documentation](https://lodash.com/docs#omit) or [package source](https://github.com/lodash/lodash/blob/4.5.0-npm-packages/lodash.omit) for more details.
/**
* lodash (Custom Build) <https://lodash.com/>
* Build: `lodash modularize exports="npm" -o ./`
* Copyright jQuery Foundation and other contributors <https://jquery.org/>
* Released under MIT license <https://lodash.com/license>
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
*/
/** Used as the size to enable large array optimizations. */
var LARGE_ARRAY_SIZE = 200;
/** Used to stand-in for `undefined` hash values. */
var HASH_UNDEFINED = '__lodash_hash_undefined__';
/** Used as references for various `Number` constants. */
var INFINITY = 1 / 0,
MAX_SAFE_INTEGER = 9007199254740991;
/** `Object#toString` result references. */
var argsTag = '[object Arguments]',
funcTag = '[object Function]',
genTag = '[object GeneratorFunction]',
symbolTag = '[object Symbol]';
/**
* Used to match `RegExp`
* [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
*/
var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
/** Used to detect host constructors (Safari). */
var reIsHostCtor = /^\[object .+?Constructor\]$/;
/** Used to detect unsigned integer values. */
var reIsUint = /^(?:0|[1-9]\d*)$/;
/** Detect free variable `global` from Node.js. */
var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
/** Detect free variable `self`. */
var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
/** Used as a reference to the global object. */
var root = freeGlobal || freeSelf || Function('return this')();
/**
* A faster alternative to `Function#apply`, this function invokes `func`
* with the `this` binding of `thisArg` and the arguments of `args`.
*
* @private
* @param {Function} func The function to invoke.
* @param {*} thisArg The `this` binding of `func`.
* @param {Array} args The arguments to invoke `func` with.
* @returns {*} Returns the result of `func`.
*/
function apply(func, thisArg, args) {
switch (args.length) {
case 0: return func.call(thisArg);
case 1: return func.call(thisArg, args[0]);
case 2: return func.call(thisArg, args[0], args[1]);
case 3: return func.call(thisArg, args[0], args[1], args[2]);
}
return func.apply(thisArg, args);
}
/**
* A specialized version of `_.includes` for arrays without support for
* specifying an index to search from.
*
* @private
* @param {Array} [array] The array to inspect.
* @param {*} target The value to search for.
* @returns {boolean} Returns `true` if `target` is found, else `false`.
*/
function arrayIncludes(array, value) {
var length = array ? array.length : 0;
return !!length && baseIndexOf(array, value, 0) > -1;
}
/**
* This function is like `arrayIncludes` except that it accepts a comparator.
*
* @private
* @param {Array} [array] The array to inspect.
* @param {*} target The value to search for.
* @param {Function} comparator The comparator invoked per element.
* @returns {boolean} Returns `true` if `target` is found, else `false`.
*/
function arrayIncludesWith(array, value, comparator) {
var index = -1,
length = array ? array.length : 0;
while (++index < length) {
if (comparator(value, array[index])) {
return true;
}
}
return false;
}
/**
* A specialized version of `_.map` for arrays without support for iteratee
* shorthands.
*
* @private
* @param {Array} [array] The array to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Array} Returns the new mapped array.
*/
function arrayMap(array, iteratee) {
var index = -1,
length = array ? array.length : 0,
result = Array(length);
while (++index < length) {
result[index] = iteratee(array[index], index, array);
}
return result;
}
/**
* Appends the elements of `values` to `array`.
*
* @private
* @param {Array} array The array to modify.
* @param {Array} values The values to append.
* @returns {Array} Returns `array`.
*/
function arrayPush(array, values) {
var index = -1,
length = values.length,
offset = array.length;
while (++index < length) {
array[offset + index] = values[index];
}
return array;
}
/**
* The base implementation of `_.findIndex` and `_.findLastIndex` without
* support for iteratee shorthands.
*
* @private
* @param {Array} array The array to inspect.
* @param {Function} predicate The function invoked per iteration.
* @param {number} fromIndex The index to search from.
* @param {boolean} [fromRight] Specify iterating from right to left.
* @returns {number} Returns the index of the matched value, else `-1`.
*/
function baseFindIndex(array, predicate, fromIndex, fromRight) {
var length = array.length,
index = fromIndex + (fromRight ? 1 : -1);
while ((fromRight ? index-- : ++index < length)) {
if (predicate(array[index], index, array)) {
return index;
}
}
return -1;
}
/**
* The base implementation of `_.indexOf` without `fromIndex` bounds checks.
*
* @private
* @param {Array} array The array to inspect.
* @param {*} value The value to search for.
* @param {number} fromIndex The index to search from.
* @returns {number} Returns the index of the matched value, else `-1`.
*/
function baseIndexOf(array, value, fromIndex) {
if (value !== value) {
return baseFindIndex(array, baseIsNaN, fromIndex);
}
var index = fromIndex - 1,
length = array.length;
while (++index < length) {
if (array[index] === value) {
return index;
}
}
return -1;
}
/**
* The base implementation of `_.isNaN` without support for number objects.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
*/
function baseIsNaN(value) {
return value !== value;
}
/**
* The base implementation of `_.times` without support for iteratee shorthands
* or max array length checks.
*
* @private
* @param {number} n The number of times to invoke `iteratee`.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Array} Returns the array of results.
*/
function baseTimes(n, iteratee) {
var index = -1,
result = Array(n);
while (++index < n) {
result[index] = iteratee(index);
}
return result;
}
/**
* The base implementation of `_.unary` without support for storing metadata.
*
* @private
* @param {Function} func The function to cap arguments for.
* @returns {Function} Returns the new capped function.
*/
function baseUnary(func) {
return function(value) {
return func(value);
};
}
/**
* Checks if a cache value for `key` exists.
*
* @private
* @param {Object} cache The cache to query.
* @param {string} key The key of the entry to check.
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
*/
function cacheHas(cache, key) {
return cache.has(key);
}
/**
* Gets the value at `key` of `object`.
*
* @private
* @param {Object} [object] The object to query.
* @param {string} key The key of the property to get.
* @returns {*} Returns the property value.
*/
function getValue(object, key) {
return object == null ? undefined : object[key];
}
/**
* Checks if `value` is a host object in IE < 9.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a host object, else `false`.
*/
function isHostObject(value) {
// Many host objects are `Object` objects that can coerce to strings
// despite having improperly defined `toString` methods.
var result = false;
if (value != null && typeof value.toString != 'function') {
try {
result = !!(value + '');
} catch (e) {}
}
return result;
}
/**
* Creates a unary function that invokes `func` with its argument transformed.
*
* @private
* @param {Function} func The function to wrap.
* @param {Function} transform The argument transform.
* @returns {Function} Returns the new function.
*/
function overArg(func, transform) {
return function(arg) {
return func(transform(arg));
};
}
/** Used for built-in method references. */
var arrayProto = Array.prototype,
funcProto = Function.prototype,
objectProto = Object.prototype;
/** Used to detect overreaching core-js shims. */
var coreJsData = root['__core-js_shared__'];
/** Used to detect methods masquerading as native. */
var maskSrcKey = (function() {
var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || '');
return uid ? ('Symbol(src)_1.' + uid) : '';
}());
/** Used to resolve the decompiled source of functions. */
var funcToString = funcProto.toString;
/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;
/**
* Used to resolve the
* [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
* of values.
*/
var objectToString = objectProto.toString;
/** Used to detect if a method is native. */
var reIsNative = RegExp('^' +
funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
.replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
);
/** Built-in value references. */
var Symbol = root.Symbol,
getPrototype = overArg(Object.getPrototypeOf, Object),
propertyIsEnumerable = objectProto.propertyIsEnumerable,
splice = arrayProto.splice,
spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined;
/* Built-in method references for those with the same name as other `lodash` methods. */
var nativeGetSymbols = Object.getOwnPropertySymbols,
nativeMax = Math.max;
/* Built-in method references that are verified to be native. */
var Map = getNative(root, 'Map'),
nativeCreate = getNative(Object, 'create');
/**
* Creates a hash object.
*
* @private
* @constructor
* @param {Array} [entries] The key-value pairs to cache.
*/
function Hash(entries) {
var index = -1,
length = entries ? entries.length : 0;
this.clear();
while (++index < length) {
var entry = entries[index];
this.set(entry[0], entry[1]);
}
}
/**
* Removes all key-value entries from the hash.
*
* @private
* @name clear
* @memberOf Hash
*/
function hashClear() {
this.__data__ = nativeCreate ? nativeCreate(null) : {};
}
/**
* Removes `key` and its value from the hash.
*
* @private
* @name delete
* @memberOf Hash
* @param {Object} hash The hash to modify.
* @param {string} key The key of the value to remove.
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
*/
function hashDelete(key) {
return this.has(key) && delete this.__data__[key];
}
/**
* Gets the hash value for `key`.
*
* @private
* @name get
* @memberOf Hash
* @param {string} key The key of the value to get.
* @returns {*} Returns the entry value.
*/
function hashGet(key) {
var data = this.__data__;
if (nativeCreate) {
var result = data[key];
return result === HASH_UNDEFINED ? undefined : result;
}
return hasOwnProperty.call(data, key) ? data[key] : undefined;
}
/**
* Checks if a hash value for `key` exists.
*
* @private
* @name has
* @memberOf Hash
* @param {string} key The key of the entry to check.
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
*/
function hashHas(key) {
var data = this.__data__;
return nativeCreate ? data[key] !== undefined : hasOwnProperty.call(data, key);
}
/**
* Sets the hash `key` to `value`.
*
* @private
* @name set
* @memberOf Hash
* @param {string} key The key of the value to set.
* @param {*} value The value to set.
* @returns {Object} Returns the hash instance.
*/
function hashSet(key, value) {
var data = this.__data__;
data[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value;
return this;
}
// Add methods to `Hash`.
Hash.prototype.clear = hashClear;
Hash.prototype['delete'] = hashDelete;
Hash.prototype.get = hashGet;
Hash.prototype.has = hashHas;
Hash.prototype.set = hashSet;
/**
* Creates an list cache object.
*
* @private
* @constructor
* @param {Array} [entries] The key-value pairs to cache.
*/
function ListCache(entries) {
var index = -1,
length = entries ? entries.length : 0;
this.clear();
while (++index < length) {
var entry = entries[index];
this.set(entry[0], entry[1]);
}
}
/**
* Removes all key-value entries from the list cache.
*
* @private
* @name clear
* @memberOf ListCache
*/
function listCacheClear() {
this.__data__ = [];
}
/**
* Removes `key` and its value from the list cache.
*
* @private
* @name delete
* @memberOf ListCache
* @param {string} key The key of the value to remove.
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
*/
function listCacheDelete(key) {
var data = this.__data__,
index = assocIndexOf(data, key);
if (index < 0) {
return false;
}
var lastIndex = data.length - 1;
if (index == lastIndex) {
data.pop();
} else {
splice.call(data, index, 1);
}
return true;
}
/**
* Gets the list cache value for `key`.
*
* @private
* @name get
* @memberOf ListCache
* @param {string} key The key of the value to get.
* @returns {*} Returns the entry value.
*/
function listCacheGet(key) {
var data = this.__data__,
index = assocIndexOf(data, key);
return index < 0 ? undefined : data[index][1];
}
/**
* Checks if a list cache value for `key` exists.
*
* @private
* @name has
* @memberOf ListCache
* @param {string} key The key of the entry to check.
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
*/
function listCacheHas(key) {
return assocIndexOf(this.__data__, key) > -1;
}
/**
* Sets the list cache `key` to `value`.
*
* @private
* @name set
* @memberOf ListCache
* @param {string} key The key of the value to set.
* @param {*} value The value to set.
* @returns {Object} Returns the list cache instance.
*/
function listCacheSet(key, value) {
var data = this.__data__,
index = assocIndexOf(data, key);
if (index < 0) {
data.push([key, value]);
} else {
data[index][1] = value;
}
return this;
}
// Add methods to `ListCache`.
ListCache.prototype.clear = listCacheClear;
ListCache.prototype['delete'] = listCacheDelete;
ListCache.prototype.get = listCacheGet;
ListCache.prototype.has = listCacheHas;
ListCache.prototype.set = listCacheSet;
/**
* Creates a map cache object to store key-value pairs.
*
* @private
* @constructor
* @param {Array} [entries] The key-value pairs to cache.
*/
function MapCache(entries) {
var index = -1,
length = entries ? entries.length : 0;
this.clear();
while (++index < length) {
var entry = entries[index];
this.set(entry[0], entry[1]);
}
}
/**
* Removes all key-value entries from the map.
*
* @private
* @name clear
* @memberOf MapCache
*/
function mapCacheClear() {
this.__data__ = {
'hash': new Hash,
'map': new (Map || ListCache),
'string': new Hash
};
}
/**
* Removes `key` and its value from the map.
*
* @private
* @name delete
* @memberOf MapCache
* @param {string} key The key of the value to remove.
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
*/
function mapCacheDelete(key) {
return getMapData(this, key)['delete'](key);
}
/**
* Gets the map value for `key`.
*
* @private
* @name get
* @memberOf MapCache
* @param {string} key The key of the value to get.
* @returns {*} Returns the entry value.
*/
function mapCacheGet(key) {
return getMapData(this, key).get(key);
}
/**
* Checks if a map value for `key` exists.
*
* @private
* @name has
* @memberOf MapCache
* @param {string} key The key of the entry to check.
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
*/
function mapCacheHas(key) {
return getMapData(this, key).has(key);
}
/**
* Sets the map `key` to `value`.
*
* @private
* @name set
* @memberOf MapCache
* @param {string} key The key of the value to set.
* @param {*} value The value to set.
* @returns {Object} Returns the map cache instance.
*/
function mapCacheSet(key, value) {
getMapData(this, key).set(key, value);
return this;
}
// Add methods to `MapCache`.
MapCache.prototype.clear = mapCacheClear;
MapCache.prototype['delete'] = mapCacheDelete;
MapCache.prototype.get = mapCacheGet;
MapCache.prototype.has = mapCacheHas;
MapCache.prototype.set = mapCacheSet;
/**
*
* Creates an array cache object to store unique values.
*
* @private
* @constructor
* @param {Array} [values] The values to cache.
*/
function SetCache(values) {
var index = -1,
length = values ? values.length : 0;
this.__data__ = new MapCache;
while (++index < length) {
this.add(values[index]);
}
}
/**
* Adds `value` to the array cache.
*
* @private
* @name add
* @memberOf SetCache
* @alias push
* @param {*} value The value to cache.
* @returns {Object} Returns the cache instance.
*/
function setCacheAdd(value) {
this.__data__.set(value, HASH_UNDEFINED);
return this;
}
/**
* Checks if `value` is in the array cache.
*
* @private
* @name has
* @memberOf SetCache
* @param {*} value The value to search for.
* @returns {number} Returns `true` if `value` is found, else `false`.
*/
function setCacheHas(value) {
return this.__data__.has(value);
}
// Add methods to `SetCache`.
SetCache.prototype.add = SetCache.prototype.push = setCacheAdd;
SetCache.prototype.has = setCacheHas;
/**
* Creates an array of the enumerable property names of the array-like `value`.
*
* @private
* @param {*} value The value to query.
* @param {boolean} inherited Specify returning inherited property names.
* @returns {Array} Returns the array of property names.
*/
function arrayLikeKeys(value, inherited) {
// Safari 8.1 makes `arguments.callee` enumerable in strict mode.
// Safari 9 makes `arguments.length` enumerable in strict mode.
var result = (isArray(value) || isArguments(value))
? baseTimes(value.length, String)
: [];
var length = result.length,
skipIndexes = !!length;
for (var key in value) {
if ((inherited || hasOwnProperty.call(value, key)) &&
!(skipIndexes && (key == 'length' || isIndex(key, length)))) {
result.push(key);
}
}
return result;
}
/**
* Gets the index at which the `key` is found in `array` of key-value pairs.
*
* @private
* @param {Array} array The array to inspect.
* @param {*} key The key to search for.
* @returns {number} Returns the index of the matched value, else `-1`.
*/
function assocIndexOf(array, key) {
var length = array.length;
while (length--) {
if (eq(array[length][0], key)) {
return length;
}
}
return -1;
}
/**
* The base implementation of methods like `_.difference` without support
* for excluding multiple arrays or iteratee shorthands.
*
* @private
* @param {Array} array The array to inspect.
* @param {Array} values The values to exclude.
* @param {Function} [iteratee] The iteratee invoked per element.
* @param {Function} [comparator] The comparator invoked per element.
* @returns {Array} Returns the new array of filtered values.
*/
function baseDifference(array, values, iteratee, comparator) {
var index = -1,
includes = arrayIncludes,
isCommon = true,
length = array.length,
result = [],
valuesLength = values.length;
if (!length) {
return result;
}
if (iteratee) {
values = arrayMap(values, baseUnary(iteratee));
}
if (comparator) {
includes = arrayIncludesWith;
isCommon = false;
}
else if (values.length >= LARGE_ARRAY_SIZE) {
includes = cacheHas;
isCommon = false;
values = new SetCache(values);
}
outer:
while (++index < length) {
var value = array[index],
computed = iteratee ? iteratee(value) : value;
value = (comparator || value !== 0) ? value : 0;
if (isCommon && computed === computed) {
var valuesIndex = valuesLength;
while (valuesIndex--) {
if (values[valuesIndex] === computed) {
continue outer;
}
}
result.push(value);
}
else if (!includes(values, computed, comparator)) {
result.push(value);
}
}
return result;
}
/**
* The base implementation of `_.flatten` with support for restricting flattening.
*
* @private
* @param {Array} array The array to flatten.
* @param {number} depth The maximum recursion depth.
* @param {boolean} [predicate=isFlattenable] The function invoked per iteration.
* @param {boolean} [isStrict] Restrict to values that pass `predicate` checks.
* @param {Array} [result=[]] The initial result value.
* @returns {Array} Returns the new flattened array.
*/
function baseFlatten(array, depth, predicate, isStrict, result) {
var index = -1,
length = array.length;
predicate || (predicate = isFlattenable);
result || (result = []);
while (++index < length) {
var value = array[index];
if (depth > 0 && predicate(value)) {
if (depth > 1) {
// Recursively flatten arrays (susceptible to call stack limits).
baseFlatten(value, depth - 1, predicate, isStrict, result);
} else {
arrayPush(result, value);
}
} else if (!isStrict) {
result[result.length] = value;
}
}
return result;
}
/**
* The base implementation of `getAllKeys` and `getAllKeysIn` which uses
* `keysFunc` and `symbolsFunc` to get the enumerable property names and
* symbols of `object`.
*
* @private
* @param {Object} object The object to query.
* @param {Function} keysFunc The function to get the keys of `object`.
* @param {Function} symbolsFunc The function to get the symbols of `object`.
* @returns {Array} Returns the array of property names and symbols.
*/
function baseGetAllKeys(object, keysFunc, symbolsFunc) {
var result = keysFunc(object);
return isArray(object) ? result : arrayPush(result, symbolsFunc(object));
}
/**
* The base implementation of `_.isNative` without bad shim checks.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a native function,
* else `false`.
*/
function baseIsNative(value) {
if (!isObject(value) || isMasked(value)) {
return false;
}
var pattern = (isFunction(value) || isHostObject(value)) ? reIsNative : reIsHostCtor;
return pattern.test(toSource(value));
}
/**
* The base implementation of `_.keysIn` which doesn't treat sparse arrays as dense.
*
* @private
* @param {Object} object The object to query.
* @returns {Array} Returns the array of property names.
*/
function baseKeysIn(object) {
if (!isObject(object)) {
return nativeKeysIn(object);
}
var isProto = isPrototype(object),
result = [];
for (var key in object) {
if (!(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
result.push(key);
}
}
return result;
}
/**
* The base implementation of `_.pick` without support for individual
* property identifiers.
*
* @private
* @param {Object} object The source object.
* @param {string[]} props The property identifiers to pick.
* @returns {Object} Returns the new object.
*/
function basePick(object, props) {
object = Object(object);
return basePickBy(object, props, function(value, key) {
return key in object;
});
}
/**
* The base implementation of `_.pickBy` without support for iteratee shorthands.
*
* @private
* @param {Object} object The source object.
* @param {string[]} props The property identifiers to pick from.
* @param {Function} predicate The function invoked per property.
* @returns {Object} Returns the new object.
*/
function basePickBy(object, props, predicate) {
var index = -1,
length = props.length,
result = {};
while (++index < length) {
var key = props[index],
value = object[key];
if (predicate(value, key)) {
result[key] = value;
}
}
return result;
}
/**
* The base implementation of `_.rest` which doesn't validate or coerce arguments.
*
* @private
* @param {Function} func The function to apply a rest parameter to.
* @param {number} [start=func.length-1] The start position of the rest parameter.
* @returns {Function} Returns the new function.
*/
function baseRest(func, start) {
start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
return function() {
var args = arguments,
index = -1,
length = nativeMax(args.length - start, 0),
array = Array(length);
while (++index < length) {
array[index] = args[start + index];
}
index = -1;
var otherArgs = Array(start + 1);
while (++index < start) {
otherArgs[index] = args[index];
}
otherArgs[start] = array;
return apply(func, this, otherArgs);
};
}
/**
* Creates an array of own and inherited enumerable property names and
* symbols of `object`.
*
* @private
* @param {Object} object The object to query.
* @returns {Array} Returns the array of property names and symbols.
*/
function getAllKeysIn(object) {
return baseGetAllKeys(object, keysIn, getSymbolsIn);
}
/**
* Gets the data for `map`.
*
* @private
* @param {Object} map The map to query.
* @param {string} key The reference key.
* @returns {*} Returns the map data.
*/
function getMapData(map, key) {
var data = map.__data__;
return isKeyable(key)
? data[typeof key == 'string' ? 'string' : 'hash']
: data.map;
}
/**
* Gets the native function at `key` of `object`.
*
* @private
* @param {Object} object The object to query.
* @param {string} key The key of the method to get.
* @returns {*} Returns the function if it's native, else `undefined`.
*/
function getNative(object, key) {
var value = getValue(object, key);
return baseIsNative(value) ? value : undefined;
}
/**
* Creates an array of the own enumerable symbol properties of `object`.
*
* @private
* @param {Object} object The object to query.
* @returns {Array} Returns the array of symbols.
*/
var getSymbols = nativeGetSymbols ? overArg(nativeGetSymbols, Object) : stubArray;
/**
* Creates an array of the own and inherited enumerable symbol properties
* of `object`.
*
* @private
* @param {Object} object The object to query.
* @returns {Array} Returns the array of symbols.
*/
var getSymbolsIn = !nativeGetSymbols ? stubArray : function(object) {
var result = [];
while (object) {
arrayPush(result, getSymbols(object));
object = getPrototype(object);
}
return result;
};
/**
* Checks if `value` is a flattenable `arguments` object or array.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is flattenable, else `false`.
*/
function isFlattenable(value) {
return isArray(value) || isArguments(value) ||
!!(spreadableSymbol && value && value[spreadableSymbol]);
}
/**
* Checks if `value` is a valid array-like index.
*
* @private
* @param {*} value The value to check.
* @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
* @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
*/
function isIndex(value, length) {
length = length == null ? MAX_SAFE_INTEGER : length;
return !!length &&
(typeof value == 'number' || reIsUint.test(value)) &&
(value > -1 && value % 1 == 0 && value < length);
}
/**
* Checks if `value` is suitable for use as unique object key.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is suitable, else `false`.
*/
function isKeyable(value) {
var type = typeof value;
return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean')
? (value !== '__proto__')
: (value === null);
}
/**
* Checks if `func` has its source masked.
*
* @private
* @param {Function} func The function to check.
* @returns {boolean} Returns `true` if `func` is masked, else `false`.
*/
function isMasked(func) {
return !!maskSrcKey && (maskSrcKey in func);
}
/**
* Checks if `value` is likely a prototype object.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
*/
function isPrototype(value) {
var Ctor = value && value.constructor,
proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;
return value === proto;
}
/**
* This function is like
* [`Object.keys`](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)
* except that it includes inherited enumerable properties.
*
* @private
* @param {Object} object The object to query.
* @returns {Array} Returns the array of property names.
*/
function nativeKeysIn(object) {
var result = [];
if (object != null) {
for (var key in Object(object)) {
result.push(key);
}
}
return result;
}
/**
* Converts `value` to a string key if it's not a string or symbol.
*
* @private
* @param {*} value The value to inspect.
* @returns {string|symbol} Returns the key.
*/
function toKey(value) {
if (typeof value == 'string' || isSymbol(value)) {
return value;
}
var result = (value + '');
return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
}
/**
* Converts `func` to its source code.
*
* @private
* @param {Function} func The function to process.
* @returns {string} Returns the source code.
*/
function toSource(func) {
if (func != null) {
try {
return funcToString.call(func);
} catch (e) {}
try {
return (func + '');
} catch (e) {}
}
return '';
}
/**
* Performs a
* [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
* comparison between two values to determine if they are equivalent.
*
* @static
* @memberOf _
* @since 4.0.0
* @category Lang
* @param {*} value The value to compare.
* @param {*} other The other value to compare.
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
* @example
*
* var object = { 'a': 1 };
* var other = { 'a': 1 };
*
* _.eq(object, object);
* // => true
*
* _.eq(object, other);
* // => false
*
* _.eq('a', 'a');
* // => true
*
* _.eq('a', Object('a'));
* // => false
*
* _.eq(NaN, NaN);
* // => true
*/
function eq(value, other) {
return value === other || (value !== value && other !== other);
}
/**
* Checks if `value` is likely an `arguments` object.
*
* @static
* @memberOf _
* @since 0.1.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is an `arguments` object,
* else `false`.
* @example
*
* _.isArguments(function() { return arguments; }());
* // => true
*
* _.isArguments([1, 2, 3]);
* // => false
*/
function isArguments(value) {
// Safari 8.1 makes `arguments.callee` enumerable in strict mode.
return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
(!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
}
/**
* Checks if `value` is classified as an `Array` object.
*
* @static
* @memberOf _
* @since 0.1.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is an array, else `false`.
* @example
*
* _.isArray([1, 2, 3]);
* // => true
*
* _.isArray(document.body.children);
* // => false
*
* _.isArray('abc');
* // => false
*
* _.isArray(_.noop);
* // => false
*/
var isArray = Array.isArray;
/**
* Checks if `value` is array-like. A value is considered array-like if it's
* not a function and has a `value.length` that's an integer greater than or
* equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
*
* @static
* @memberOf _
* @since 4.0.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is array-like, else `false`.
* @example
*
* _.isArrayLike([1, 2, 3]);
* // => true
*
* _.isArrayLike(document.body.children);
* // => true
*
* _.isArrayLike('abc');
* // => true
*
* _.isArrayLike(_.noop);
* // => false
*/
function isArrayLike(value) {
return value != null && isLength(value.length) && !isFunction(value);
}
/**
* This method is like `_.isArrayLike` except that it also checks if `value`
* is an object.
*
* @static
* @memberOf _
* @since 4.0.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is an array-like object,
* else `false`.
* @example
*
* _.isArrayLikeObject([1, 2, 3]);
* // => true
*
* _.isArrayLikeObject(document.body.children);
* // => true
*
* _.isArrayLikeObject('abc');
* // => false
*
* _.isArrayLikeObject(_.noop);
* // => false
*/
function isArrayLikeObject(value) {
return isObjectLike(value) && isArrayLike(value);
}
/**
* Checks if `value` is classified as a `Function` object.
*
* @static
* @memberOf _
* @since 0.1.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a function, else `false`.
* @example
*
* _.isFunction(_);
* // => true
*
* _.isFunction(/abc/);
* // => false
*/
function isFunction(value) {
// The use of `Object#toString` avoids issues with the `typeof` operator
// in Safari 8-9 which returns 'object' for typed array and other constructors.
var tag = isObject(value) ? objectToString.call(value) : '';
return tag == funcTag || tag == genTag;
}
/**
* Checks if `value` is a valid array-like length.
*
* **Note:** This method is loosely based on
* [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
*
* @static
* @memberOf _
* @since 4.0.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
* @example
*
* _.isLength(3);
* // => true
*
* _.isLength(Number.MIN_VALUE);
* // => false
*
* _.isLength(Infinity);
* // => false
*
* _.isLength('3');
* // => false
*/
function isLength(value) {
return typeof value == 'number' &&
value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
}
/**
* Checks if `value` is the
* [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
* of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
*
* @static
* @memberOf _
* @since 0.1.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is an object, else `false`.
* @example
*
* _.isObject({});
* // => true
*
* _.isObject([1, 2, 3]);
* // => true
*
* _.isObject(_.noop);
* // => true
*
* _.isObject(null);
* // => false
*/
function isObject(value) {
var type = typeof value;
return !!value && (type == 'object' || type == 'function');
}
/**
* Checks if `value` is object-like. A value is object-like if it's not `null`
* and has a `typeof` result of "object".
*
* @static
* @memberOf _
* @since 4.0.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
* @example
*
* _.isObjectLike({});
* // => true
*
* _.isObjectLike([1, 2, 3]);
* // => true
*
* _.isObjectLike(_.noop);
* // => false
*
* _.isObjectLike(null);
* // => false
*/
function isObjectLike(value) {
return !!value && typeof value == 'object';
}
/**
* Checks if `value` is classified as a `Symbol` primitive or object.
*
* @static
* @memberOf _
* @since 4.0.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
* @example
*
* _.isSymbol(Symbol.iterator);
* // => true
*
* _.isSymbol('abc');
* // => false
*/
function isSymbol(value) {
return typeof value == 'symbol' ||
(isObjectLike(value) && objectToString.call(value) == symbolTag);
}
/**
* Creates an array of the own and inherited enumerable property names of `object`.
*
* **Note:** Non-object values are coerced to objects.
*
* @static
* @memberOf _
* @since 3.0.0
* @category Object
* @param {Object} object The object to query.
* @returns {Array} Returns the array of property names.
* @example
*
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* _.keysIn(new Foo);
* // => ['a', 'b', 'c'] (iteration order is not guaranteed)
*/
function keysIn(object) {
return isArrayLike(object) ? arrayLikeKeys(object, true) : baseKeysIn(object);
}
/**
* The opposite of `_.pick`; this method creates an object composed of the
* own and inherited enumerable string keyed properties of `object` that are
* not omitted.
*
* @static
* @since 0.1.0
* @memberOf _
* @category Object
* @param {Object} object The source object.
* @param {...(string|string[])} [props] The property identifiers to omit.
* @returns {Object} Returns the new object.
* @example
*
* var object = { 'a': 1, 'b': '2', 'c': 3 };
*
* _.omit(object, ['a', 'c']);
* // => { 'b': '2' }
*/
var omit = baseRest(function(object, props) {
if (object == null) {
return {};
}
props = arrayMap(baseFlatten(props, 1), toKey);
return basePick(object, baseDifference(getAllKeysIn(object), props));
});
/**
* This method returns a new empty array.
*
* @static
* @memberOf _
* @since 4.13.0
* @category Util
* @returns {Array} Returns the new empty array.
* @example
*
* var arrays = _.times(2, _.stubArray);
*
* console.log(arrays);
* // => [[], []]
*
* console.log(arrays[0] === arrays[1]);
* // => false
*/
function stubArray() {
return [];
}
module.exports = omit;
{
"_from": "lodash.omit@^4.5.0",
"_id": "lodash.omit@4.5.0",
"_inBundle": false,
"_integrity": "sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA=",
"_location": "/lodash.omit",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "lodash.omit@^4.5.0",
"name": "lodash.omit",
"escapedName": "lodash.omit",
"rawSpec": "^4.5.0",
"saveSpec": null,
"fetchSpec": "^4.5.0"
},
"_requiredBy": [
"/messaging-api-line"
],
"_resolved": "https://registry.npmjs.org/lodash.omit/-/lodash.omit-4.5.0.tgz",
"_shasum": "6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60",
"_spec": "lodash.omit@^4.5.0",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\messaging-api-line",
"author": {
"name": "John-David Dalton",
"email": "john.david.dalton@gmail.com",
"url": "http://allyoucanleet.com/"
},
"bugs": {
"url": "https://github.com/lodash/lodash/issues"
},
"bundleDependencies": false,
"contributors": [
{
"name": "John-David Dalton",
"email": "john.david.dalton@gmail.com",
"url": "http://allyoucanleet.com/"
},
{
"name": "Blaine Bublitz",
"email": "blaine.bublitz@gmail.com",
"url": "https://github.com/phated"
},
{
"name": "Mathias Bynens",
"email": "mathias@qiwi.be",
"url": "https://mathiasbynens.be/"
}
],
"deprecated": false,
"description": "The lodash method `_.omit` exported as a module.",
"homepage": "https://lodash.com/",
"icon": "https://lodash.com/icon.svg",
"keywords": [
"lodash-modularized",
"omit"
],
"license": "MIT",
"name": "lodash.omit",
"repository": {
"type": "git",
"url": "git+https://github.com/lodash/lodash.git"
},
"scripts": {
"test": "echo \"See https://travis-ci.org/lodash/lodash-cli for testing details.\""
},
"version": "4.5.0"
}
The MIT License (MIT)
Copyright (c) 2015 Andres Suarez <zertosh@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# loose-envify
[![Build Status](https://travis-ci.org/zertosh/loose-envify.svg?branch=master)](https://travis-ci.org/zertosh/loose-envify)
Fast (and loose) selective `process.env` replacer using [js-tokens](https://github.com/lydell/js-tokens) instead of an AST. Works just like [envify](https://github.com/hughsk/envify) but much faster.
## Gotchas
* Doesn't handle broken syntax.
* Doesn't look inside embedded expressions in template strings.
- **this won't work:**
```js
console.log(`the current env is ${process.env.NODE_ENV}`);
```
* Doesn't replace oddly-spaced or oddly-commented expressions.
- **this won't work:**
```js
console.log(process./*won't*/env./*work*/NODE_ENV);
```
## Usage/Options
loose-envify has the exact same interface as [envify](https://github.com/hughsk/envify), including the CLI.
## Benchmark
```
envify:
$ for i in {1..5}; do node bench/bench.js 'envify'; done
708ms
727ms
791ms
719ms
720ms
loose-envify:
$ for i in {1..5}; do node bench/bench.js '../'; done
51ms
52ms
52ms
52ms
52ms
```
#!/usr/bin/env node
'use strict';
var looseEnvify = require('./');
var fs = require('fs');
if (process.argv[2]) {
fs.createReadStream(process.argv[2], {encoding: 'utf8'})
.pipe(looseEnvify(process.argv[2]))
.pipe(process.stdout);
} else {
process.stdin.resume()
process.stdin
.pipe(looseEnvify(__filename))
.pipe(process.stdout);
}
// envify compatibility
'use strict';
module.exports = require('./loose-envify');
'use strict';
module.exports = require('./loose-envify')(process.env);
'use strict';
var stream = require('stream');
var util = require('util');
var replace = require('./replace');
var jsonExtRe = /\.json$/;
module.exports = function(rootEnv) {
rootEnv = rootEnv || process.env;
return function (file, trOpts) {
if (jsonExtRe.test(file)) {
return stream.PassThrough();
}
var envs = trOpts ? [rootEnv, trOpts] : [rootEnv];
return new LooseEnvify(envs);
};
};
function LooseEnvify(envs) {
stream.Transform.call(this);
this._data = '';
this._envs = envs;
}
util.inherits(LooseEnvify, stream.Transform);
LooseEnvify.prototype._transform = function(buf, enc, cb) {
this._data += buf;
cb();
};
LooseEnvify.prototype._flush = function(cb) {
var replaced = replace(this._data, this._envs);
this.push(replaced);
cb();
};
{
"_from": "loose-envify@^1.0.0",
"_id": "loose-envify@1.4.0",
"_inBundle": false,
"_integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==",
"_location": "/loose-envify",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "loose-envify@^1.0.0",
"name": "loose-envify",
"escapedName": "loose-envify",
"rawSpec": "^1.0.0",
"saveSpec": null,
"fetchSpec": "^1.0.0"
},
"_requiredBy": [
"/invariant"
],
"_resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
"_shasum": "71ee51fa7be4caec1a63839f7e682d8132d30caf",
"_spec": "loose-envify@^1.0.0",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\invariant",
"author": {
"name": "Andres Suarez",
"email": "zertosh@gmail.com"
},
"bin": {
"loose-envify": "cli.js"
},
"bugs": {
"url": "https://github.com/zertosh/loose-envify/issues"
},
"bundleDependencies": false,
"dependencies": {
"js-tokens": "^3.0.0 || ^4.0.0"
},
"deprecated": false,
"description": "Fast (and loose) selective `process.env` replacer using js-tokens instead of an AST",
"devDependencies": {
"browserify": "^13.1.1",
"envify": "^3.4.0",
"tap": "^8.0.0"
},
"homepage": "https://github.com/zertosh/loose-envify",
"keywords": [
"environment",
"variables",
"browserify",
"browserify-transform",
"transform",
"source",
"configuration"
],
"license": "MIT",
"main": "index.js",
"name": "loose-envify",
"repository": {
"type": "git",
"url": "git://github.com/zertosh/loose-envify.git"
},
"scripts": {
"test": "tap test/*.js"
},
"version": "1.4.0"
}
'use strict';
var jsTokens = require('js-tokens').default;
var processEnvRe = /\bprocess\.env\.[_$a-zA-Z][$\w]+\b/;
var spaceOrCommentRe = /^(?:\s|\/[/*])/;
function replace(src, envs) {
if (!processEnvRe.test(src)) {
return src;
}
var out = [];
var purge = envs.some(function(env) {
return env._ && env._.indexOf('purge') !== -1;
});
jsTokens.lastIndex = 0
var parts = src.match(jsTokens);
for (var i = 0; i < parts.length; i++) {
if (parts[i ] === 'process' &&
parts[i + 1] === '.' &&
parts[i + 2] === 'env' &&
parts[i + 3] === '.') {
var prevCodeToken = getAdjacentCodeToken(-1, parts, i);
var nextCodeToken = getAdjacentCodeToken(1, parts, i + 4);
var replacement = getReplacementString(envs, parts[i + 4], purge);
if (prevCodeToken !== '.' &&
nextCodeToken !== '.' &&
nextCodeToken !== '=' &&
typeof replacement === 'string') {
out.push(replacement);
i += 4;
continue;
}
}
out.push(parts[i]);
}
return out.join('');
}
function getAdjacentCodeToken(dir, parts, i) {
while (true) {
var part = parts[i += dir];
if (!spaceOrCommentRe.test(part)) {
return part;
}
}
}
function getReplacementString(envs, name, purge) {
for (var j = 0; j < envs.length; j++) {
var env = envs[j];
if (typeof env[name] !== 'undefined') {
return JSON.stringify(env[name]);
}
}
if (purge) {
return 'undefined';
}
}
module.exports = replace;
The MIT License (MIT)
Copyright (c) 2017-present Yoctol (github.com/Yoctol/messaging-apis)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
This diff could not be displayed because it is too large.
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;
var _invariant = _interopRequireDefault(require("invariant"));
var _lodash = _interopRequireDefault(require("lodash.omit"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function ownKeys(object, enumerableOnly) {var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {return Object.getOwnPropertyDescriptor(object, sym).enumerable;});keys.push.apply(keys, symbols);}return keys;}function _objectSpread(target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {ownKeys(source, true).forEach(function (key) {_defineProperty(target, key, source[key]);});} else if (Object.getOwnPropertyDescriptors) {Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));} else {ownKeys(source).forEach(function (key) {Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));});}}return target;}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}
function createText(text, options = {}) {
return _objectSpread({
type: 'text',
text },
(0, _lodash.default)(options, 'accessToken'));
}
function createImage(
contentUrlOrImage,
previewUrlOrOptions)
{
if (previewUrlOrOptions) {
if (
typeof contentUrlOrImage === 'object' &&
typeof previewUrlOrOptions === 'object')
{
const image = contentUrlOrImage;
const options = previewUrlOrOptions;
return _objectSpread({
type: 'image',
originalContentUrl: image.originalContentUrl,
previewImageUrl: image.previewImageUrl || image.originalContentUrl },
(0, _lodash.default)(options, 'accessToken'));
}
if (
typeof contentUrlOrImage === 'string' &&
typeof previewUrlOrOptions === 'string')
{
return {
type: 'image',
originalContentUrl: contentUrlOrImage,
previewImageUrl: previewUrlOrOptions };
}
} else {
if (typeof contentUrlOrImage === 'object') {
const image = contentUrlOrImage;
return {
type: 'image',
originalContentUrl: image.originalContentUrl,
previewImageUrl: image.previewImageUrl || image.originalContentUrl };
}
if (typeof contentUrlOrImage === 'string') {
return {
type: 'image',
originalContentUrl: contentUrlOrImage,
previewImageUrl: contentUrlOrImage };
}
}
(0, _invariant.default)(false, 'Line#createImage: Wrong type of arguments.');
}
function createVideo(
contentUrlOrVideo,
previewImageUrlOrOptions)
{
if (
typeof contentUrlOrVideo === 'string' &&
typeof previewImageUrlOrOptions === 'string')
{
return {
type: 'video',
originalContentUrl: contentUrlOrVideo,
previewImageUrl: previewImageUrlOrOptions };
}
if (
typeof contentUrlOrVideo === 'object' && (
!previewImageUrlOrOptions || typeof previewImageUrlOrOptions === 'object'))
{
const video = contentUrlOrVideo;
const options = previewImageUrlOrOptions || {};
return _objectSpread({
type: 'video',
originalContentUrl: video.originalContentUrl,
previewImageUrl: video.previewImageUrl },
(0, _lodash.default)(options, 'accessToken'));
}
(0, _invariant.default)(false, 'Line#createVideo: Wrong type of arguments.');
}
function createAudio(
contentUrlOrAudio,
durationOrOptions)
{
if (
typeof contentUrlOrAudio === 'string' &&
typeof durationOrOptions === 'number')
{
return {
type: 'audio',
originalContentUrl: contentUrlOrAudio,
duration: durationOrOptions };
}
if (
typeof contentUrlOrAudio === 'object' && (
!durationOrOptions || typeof durationOrOptions === 'object'))
{
const audio = contentUrlOrAudio;
const options = durationOrOptions || {};
return _objectSpread({
type: 'audio',
originalContentUrl: audio.originalContentUrl,
duration: audio.duration },
(0, _lodash.default)(options, 'accessToken'));
}
(0, _invariant.default)(false, 'Line#createAudio: Wrong type of arguments.');
}
function createLocation(
{ title, address, latitude, longitude },
options = {})
{
return _objectSpread({
type: 'location',
title,
address,
latitude,
longitude },
(0, _lodash.default)(options, 'accessToken'));
}
function createSticker(
packageIdOrSticker,
stickerIdOrOptions)
{
if (
typeof packageIdOrSticker === 'string' &&
typeof stickerIdOrOptions === 'string')
{
return {
type: 'sticker',
packageId: packageIdOrSticker,
stickerId: stickerIdOrOptions };
}
if (
typeof packageIdOrSticker === 'object' && (
!stickerIdOrOptions || typeof stickerIdOrOptions === 'object'))
{
const sticker = packageIdOrSticker;
const options = stickerIdOrOptions || {};
return _objectSpread({
type: 'sticker',
packageId: sticker.packageId,
stickerId: sticker.stickerId },
(0, _lodash.default)(options, 'accessToken'));
}
(0, _invariant.default)(false, 'Line#createSticker: Wrong type of arguments.');
}
function createImagemap(
altText,
{
baseUrl,
baseSize,
baseHeight,
baseWidth,
video,
actions },
options = {})
{
return _objectSpread({
type: 'imagemap',
baseUrl,
altText,
baseSize: baseSize || {
height: baseHeight,
width: baseWidth },
video,
actions },
(0, _lodash.default)(options, 'accessToken'));
}
function createTemplate(
altText,
template,
options = {})
{
return _objectSpread({
type: 'template',
altText,
template },
(0, _lodash.default)(options, 'accessToken'));
}
function createButtonTemplate(
altText,
{
thumbnailImageUrl,
imageAspectRatio,
imageSize,
imageBackgroundColor,
title,
text,
defaultAction,
actions },
options = {})
{
return createTemplate(
altText,
{
type: 'buttons',
thumbnailImageUrl,
imageAspectRatio,
imageSize,
imageBackgroundColor,
title,
text,
defaultAction,
actions },
(0, _lodash.default)(options, 'accessToken'));
}
function createConfirmTemplate(
altText,
{
text,
actions },
options = {})
{
return createTemplate(
altText,
{
type: 'confirm',
text,
actions },
(0, _lodash.default)(options, 'accessToken'));
}
function createCarouselTemplate(
altText,
columns,
{
imageAspectRatio,
imageSize,
quickReply } =
{})
{
return createTemplate(
altText,
{
type: 'carousel',
columns,
imageAspectRatio,
imageSize },
{ quickReply });
}
function createImageCarouselTemplate(
altText,
columns,
options = {})
{
return createTemplate(
altText,
{
type: 'image_carousel',
columns },
(0, _lodash.default)(options, 'accessToken'));
}
function createFlex(
altText,
contents,
options = {})
{
return _objectSpread({
type: 'flex',
altText,
contents },
(0, _lodash.default)(options, 'accessToken'));
}
const Line = {
createText,
createImage,
createVideo,
createAudio,
createLocation,
createSticker,
createImagemap,
createTemplate,
createButtonsTemplate: createButtonTemplate,
createButtonTemplate,
createConfirmTemplate,
createCarouselTemplate,
createImageCarouselTemplate,
createFlex };var _default =
Line;exports.default = _default;
\ No newline at end of file
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;
var _axiosError = _interopRequireDefault(require("axios-error"));
var _axios = _interopRequireDefault(require("axios"));
var _debug = _interopRequireDefault(require("debug"));
var _imageType = _interopRequireDefault(require("image-type"));
var _invariant = _interopRequireDefault(require("invariant"));
var _lodash = _interopRequireDefault(require("lodash.omit"));
var _urlJoin = _interopRequireDefault(require("url-join"));
var _Line = _interopRequireDefault(require("./Line"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _objectWithoutProperties(source, excluded) {if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];}}return target;}function _objectWithoutPropertiesLoose(source, excluded) {if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];}return target;}function ownKeys(object, enumerableOnly) {var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {return Object.getOwnPropertyDescriptor(object, sym).enumerable;});keys.push.apply(keys, symbols);}return keys;}function _objectSpread(target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {ownKeys(source, true).forEach(function (key) {_defineProperty(target, key, source[key]);});} else if (Object.getOwnPropertyDescriptors) {Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));} else {ownKeys(source).forEach(function (key) {Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));});}}return target;}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}
function handleError(err) {
if (err.response && err.response.data) {
const { message, details } = err.response.data;
let msg = `LINE API - ${message}`;
if (details && details.length > 0) {
details.forEach(detail => {
msg += `\n- ${detail.property}: ${detail.message}`;
});
}
throw new _axiosError.default(msg, err);
}
throw new _axiosError.default(err.message, err);
}
const debugRequest = (0, _debug.default)('messaging-api-line');
function onRequest({ method, url, body }) {
debugRequest(`${method} ${url}`);
if (body) {
debugRequest('Outgoing request body:');
debugRequest(JSON.stringify(body, null, 2));
}
}
class LineClient {
static connect(
accessTokenOrConfig,
channelSecret)
{
return new LineClient(accessTokenOrConfig, channelSecret);
}
constructor(
accessTokenOrConfig,
channelSecret)
{_defineProperty(this, "_accessToken", void 0);_defineProperty(this, "_channelSecret", void 0);_defineProperty(this, "_onRequest", void 0);_defineProperty(this, "_axios", void 0);
let origin;
if (accessTokenOrConfig && typeof accessTokenOrConfig === 'object') {
const config = accessTokenOrConfig;
this._accessToken = config.accessToken;
this._channelSecret = config.channelSecret;
this._onRequest = config.onRequest || onRequest;
origin = config.origin;
} else {
this._accessToken = accessTokenOrConfig;
this._channelSecret = channelSecret;
this._onRequest = onRequest;
}
this._axios = _axios.default.create({
baseURL: `${origin || 'https://api.line.me'}/`,
headers: {
Authorization: `Bearer ${this._accessToken}`,
'Content-Type': 'application/json' } });
this._axios.interceptors.request.use(config => {
this._onRequest({
method: config.method,
url: (0, _urlJoin.default)(config.baseURL, config.url),
headers: _objectSpread({},
config.headers.common, {},
config.headers[config.method], {},
(0, _lodash.default)(config.headers, [
'common',
'get',
'post',
'put',
'patch',
'delete',
'head'])),
body: config.data });
return config;
});
}
get axios() {
return this._axios;
}
get accessToken() {
return this._accessToken;
}
_send(
type,
target,
...args)
{
if (type === 'push') {
return this.push(target, ...args);
}
if (type === 'multicast') {
return this.multicast(target, ...args);
}
return this.reply(target, ...args);
}
_sendText(
type,
target,
text,
options)
{
return this._send(
type,
target,
[_Line.default.createText(text, options || {})],
options);
}
_sendImage(
type,
target,
contentUrlOrImage,
previewUrlOrOptions)
{
return this._send(
type,
target,
[_Line.default.createImage(contentUrlOrImage, previewUrlOrOptions)],
typeof previewUrlOrOptions === 'string' ? undefined : previewUrlOrOptions);
}
_sendVideo(
type,
target,
contentUrlOrVideo,
previewUrlOrOptions)
{
return this._send(
type,
target,
[_Line.default.createVideo(contentUrlOrVideo, previewUrlOrOptions || {})],
typeof previewUrlOrOptions === 'string' ? undefined : previewUrlOrOptions);
}
_sendAudio(
type,
target,
contentUrlOrAudio,
durationOrOptions)
{
return this._send(
type,
target,
[_Line.default.createAudio(contentUrlOrAudio, durationOrOptions || {})],
typeof durationOrOptions === 'number' ? undefined : durationOrOptions);
}
_sendLocation(
type,
target,
{ title, address, latitude, longitude },
options)
{
return this._send(
type,
target,
[
_Line.default.createLocation(
{
title,
address,
latitude,
longitude },
options || {})],
options);
}
_sendSticker(
type,
target,
packageIdOrSticker,
stickerIdOrOptions)
{
return this._send(
type,
target,
[_Line.default.createSticker(packageIdOrSticker, stickerIdOrOptions || {})],
typeof stickerIdOrOptions === 'string' ? undefined : stickerIdOrOptions);
}
/**
* Imagemap Message
*
* https://developers.line.me/en/docs/messaging-api/reference/#imagemap-message
*/
_sendImagemap(
type,
target,
altText,
{
baseUrl,
baseSize,
baseHeight,
baseWidth,
video,
actions },
options)
{
return this._send(
type,
target,
[
_Line.default.createImagemap(
altText,
{
baseUrl,
baseSize,
baseHeight,
baseWidth,
video,
actions },
options || {})],
options);
}
/**
* Flex Message
*
* https://developers.line.me/en/docs/messaging-api/reference/#flex-message
*/
_sendFlex(
type,
target,
altText,
contents,
options)
{
return this._send(
type,
target,
[_Line.default.createFlex(altText, contents, options || {})],
options);
}
/**
* Template Messages
*
* https://developers.line.me/en/docs/messaging-api/reference/#template-messages
*/
_sendTemplate(
type,
target,
altText,
template,
options)
{
return this._send(
type,
target,
[_Line.default.createTemplate(altText, template, options || {})],
options);
}
_sendButtonTemplate(
type,
target,
altText,
{
thumbnailImageUrl,
imageAspectRatio,
imageSize,
imageBackgroundColor,
title,
text,
defaultAction,
actions },
options)
{
return this._send(
type,
target,
[
_Line.default.createButtonTemplate(
altText,
{
thumbnailImageUrl,
imageAspectRatio,
imageSize,
imageBackgroundColor,
title,
text,
defaultAction,
actions },
options || {})],
options);
}
_sendConfirmTemplate(
type,
target,
altText,
{
text,
actions },
options)
{
return this._send(
type,
target,
[
_Line.default.createConfirmTemplate(
altText,
{
text,
actions },
options || {})],
options);
}
_sendCarouselTemplate(
type,
target,
altText,
columns,
_ref =
{})
{let { imageAspectRatio, imageSize } = _ref,options = _objectWithoutProperties(_ref, ["imageAspectRatio", "imageSize"]);
return this._send(
type,
target,
[
_Line.default.createCarouselTemplate(altText, columns, _objectSpread({
imageAspectRatio,
imageSize },
options))],
options);
}
_sendImageCarouselTemplate(
type,
target,
altText,
columns,
options)
{
return this._send(
type,
target,
[_Line.default.createImageCarouselTemplate(altText, columns, options || {})],
options);
}
/**
* Reply Message
*
* https://developers.line.me/en/docs/messaging-api/reference/#send-reply-message
*/
replyRawBody(
body,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
'/v2/bot/message/reply',
body,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
reply(
replyToken,
messages,
options = {})
{
return this.replyRawBody({ replyToken, messages }, options);
}
/**
* Push Message
*
* https://developers.line.me/en/docs/messaging-api/reference/#send-push-message
*/
pushRawBody(
body,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
'/v2/bot/message/push',
body,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
push(
to,
messages,
options = {})
{
return this.pushRawBody({ to, messages }, options);
}
/**
* Multicast
*
* https://developers.line.me/en/docs/messaging-api/reference/#send-multicast-messages
*/
multicastRawBody(
body,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
'/v2/bot/message/multicast',
body,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
multicast(
to,
messages,
options = {})
{
return this.multicastRawBody({ to, messages }, options);
}
/**
* Content
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-content
*/
retrieveMessageContent(
messageId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
get(`/v2/bot/message/${messageId}/content`, _objectSpread({
responseType: 'arraybuffer' },
customAccessToken ?
{ headers: { Authorization: `Bearer ${customAccessToken}` } } :
undefined)).
then(res => res.data, handleError);
}
/**
* Get User Profile
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-profile
* displayName, userId, pictureUrl, statusMessage
*/
getUserProfile(
userId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
get(
`/v2/bot/profile/${userId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError).
catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
/**
* Get Group Member Profile
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-group-member-profile
*/
getGroupMemberProfile(
groupId,
userId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
get(
`/v2/bot/group/${groupId}/member/${userId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
/**
* Get Room Member Profile
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-room-member-profile
*/
getRoomMemberProfile(
roomId,
userId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
get(
`/v2/bot/room/${roomId}/member/${userId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
/**
* Get Group Member IDs
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-group-member-user-ids
*/
getGroupMemberIds(
groupId,
start,
{ accessToken: customAccessToken } = {})
{
return this._axios.
get(
`/v2/bot/group/${groupId}/members/ids${start ? `?start=${start}` : ''}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
async getAllGroupMemberIds(
groupId,
options = {})
{
let allMemberIds = [];
let continuationToken;
do {
// eslint-disable-next-line no-await-in-loop
const { memberIds, next } = await this.getGroupMemberIds(
groupId,
continuationToken,
options);
allMemberIds = allMemberIds.concat(memberIds);
continuationToken = next;
} while (continuationToken);
return allMemberIds;
}
/**
* Get Room Member IDs
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-room-member-user-ids
*/
getRoomMemberIds(
roomId,
start,
{ accessToken: customAccessToken } = {})
{
return this._axios.
get(
`/v2/bot/room/${roomId}/members/ids${start ? `?start=${start}` : ''}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
async getAllRoomMemberIds(
roomId,
options = {})
{
let allMemberIds = [];
let continuationToken;
do {
// eslint-disable-next-line no-await-in-loop
const { memberIds, next } = await this.getRoomMemberIds(
roomId,
continuationToken,
options);
allMemberIds = allMemberIds.concat(memberIds);
continuationToken = next;
} while (continuationToken);
return allMemberIds;
}
/**
* Leave Group
*
* https://developers.line.me/en/docs/messaging-api/reference/#leave-group
*/
leaveGroup(
groupId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
`/v2/bot/group/${groupId}/leave`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
/**
* Leave Room
*
* https://developers.line.me/en/docs/messaging-api/reference/#leave-room
*/
leaveRoom(
roomId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
`/v2/bot/room/${roomId}/leave`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
/**
* Rich Menu
*
* https://developers.line.me/en/docs/messaging-api/reference/#rich-menu
*/
getRichMenuList({
accessToken: customAccessToken } =
{}) {
return this._axios.
get(
'/v2/bot/richmenu/list',
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data.richmenus, handleError);
}
getRichMenu(
richMenuId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
get(
`/v2/bot/richmenu/${richMenuId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data).
catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
createRichMenu(
richMenu,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
'/v2/bot/richmenu',
richMenu,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
deleteRichMenu(
richMenuId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
delete(
`/v2/bot/richmenu/${richMenuId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
getLinkedRichMenu(
userId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
get(
`/v2/bot/user/${userId}/richmenu`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data).
catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
linkRichMenu(
userId,
richMenuId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
`/v2/bot/user/${userId}/richmenu/${richMenuId}`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
unlinkRichMenu(
userId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
delete(
`/v2/bot/user/${userId}/richmenu`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
getDefaultRichMenu({
accessToken: customAccessToken } =
{}) {
return this._axios.
get(
`/v2/bot/user/all/richmenu`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data).
catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
setDefaultRichMenu(
richMenuId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
`/v2/bot/user/all/richmenu/${richMenuId}`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
deleteDefaultRichMenu({
accessToken: customAccessToken } =
{}) {
return this._axios.
delete(
`/v2/bot/user/all/richmenu`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
/**
* - Images must have one of the following resolutions: 2500x1686, 2500x843.
* - You cannot replace an image attached to a rich menu.
* To update your rich menu image, create a new rich menu object and upload another image.
*/
uploadRichMenuImage(
richMenuId,
image,
{ accessToken: customAccessToken } = {})
{
const type = (0, _imageType.default)(image);
(0, _invariant.default)(
type && (type.mime === 'image/jpeg' || type.mime === 'image/png'),
'Image must be `image/jpeg` or `image/png`');
return this._axios.
post(`/v2/bot/richmenu/${richMenuId}/content`, image, {
headers: customAccessToken ?
{
'Content-Type': type.mime,
Authorization: `Bearer ${customAccessToken}` } :
{
'Content-Type': type.mime } }).
then(res => res.data, handleError);
}
downloadRichMenuImage(
richMenuId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
get(
`/v2/bot/richmenu/${richMenuId}/content`,
customAccessToken ?
{
responseType: 'arraybuffer',
headers: {
Authorization: `Bearer ${customAccessToken}` } } :
{
responseType: 'arraybuffer' }).
then(res => Buffer.from(res.data)).
catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
/**
* Account link
*
* https://developers.line.me/en/docs/messaging-api/reference/#account-link
*/
issueLinkToken(
userId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
`/v2/bot/user/${userId}/linkToken`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
/**
* LINE Front-end Framework (LIFF)
*
* https://developers.line.me/en/docs/liff/reference/#add-liff-app
*/
getLiffAppList({
accessToken: customAccessToken } =
{})
{
return this._axios.
get(
'/liff/v1/apps',
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data.apps, handleError);
}
createLiffApp(
view,
{ accessToken: customAccessToken } = {})
{
return this._axios.
post(
'/liff/v1/apps',
view,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
updateLiffApp(
liffId,
view,
{ accessToken: customAccessToken } = {})
{
return this._axios.
put(
`/liff/v1/apps/${liffId}/view`,
view,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}
deleteLiffApp(
liffId,
{ accessToken: customAccessToken } = {})
{
return this._axios.
delete(
`/liff/v1/apps/${liffId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` } }).
then(res => res.data, handleError);
}}exports.default = LineClient;
const sendTypes = ['reply', 'push', 'multicast'];
const messageTypes =
[
{ name: 'Text' },
{ name: 'Image' },
{ name: 'Video' },
{ name: 'Audio' },
{ name: 'Location' },
{ name: 'Sticker' },
{ name: 'Imagemap' },
{ name: 'Flex' },
{ name: 'Template' },
{ name: 'ButtonTemplate', aliases: ['ButtonsTemplate'] },
{ name: 'ConfirmTemplate' },
{ name: 'CarouselTemplate' },
{ name: 'ImageCarouselTemplate' }];
messageTypes.forEach(({ name, aliases }) => {
sendTypes.forEach(sendType => {
[name].concat(aliases || []).forEach(type => {
Object.defineProperty(LineClient.prototype, `${sendType}${type}`, {
enumerable: false,
configurable: true,
writable: true,
value(target, ...args) {
return this[`_send${name}`](sendType, target, ...args);
} });
});
});
});
\ No newline at end of file
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;
var _querystring = _interopRequireDefault(require("querystring"));
var _axiosError = _interopRequireDefault(require("axios-error"));
var _axios = _interopRequireDefault(require("axios"));
var _invariant = _interopRequireDefault(require("invariant"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function ownKeys(object, enumerableOnly) {var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {return Object.getOwnPropertyDescriptor(object, sym).enumerable;});keys.push.apply(keys, symbols);}return keys;}function _objectSpread(target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {ownKeys(source, true).forEach(function (key) {_defineProperty(target, key, source[key]);});} else if (Object.getOwnPropertyDescriptors) {Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));} else {ownKeys(source).forEach(function (key) {Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));});}}return target;}function _objectWithoutProperties(source, excluded) {if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];}}return target;}function _objectWithoutPropertiesLoose(source, excluded) {if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];}return target;}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}
function handleError(err) {
if (err.response && err.response.data) {
const { returnCode, returnMessage } = err.response.data;
const msg = `LINE PAY API - ${returnCode} ${returnMessage}`;
throw new _axiosError.default(msg, err);
}
throw new _axiosError.default(err.message, err);
}
function throwWhenNotSuccess(res) {
if (res.data.returnCode !== '0000') {
const { returnCode, returnMessage } = res.data;
const msg = `LINE PAY API - ${returnCode} ${returnMessage}`;
throw new _axiosError.default(msg);
}
return res.data.info;
}
class LinePay {
static connect(config) {
return new LinePay(config);
}
constructor({
channelId,
channelSecret,
sandbox = false,
origin })
{_defineProperty(this, "_axios", void 0);
const linePayOrigin = sandbox ?
'https://sandbox-api-pay.line.me' :
'https://api-pay.line.me';
this._axios = _axios.default.create({
baseURL: `${origin || linePayOrigin}/v2/`,
headers: {
'Content-Type': 'application/json',
'X-LINE-ChannelId': channelId,
'X-LINE-ChannelSecret': channelSecret } });
}
get axios() {
return this._axios;
}
getPayments({
transactionId,
orderId } =
{}) {
(0, _invariant.default)(
transactionId || orderId,
'getPayments: One of `transactionId` or `orderId` must be provided');
const query = {};
if (transactionId) {
query.transactionId = transactionId;
}
if (orderId) {
query.orderId = orderId;
}
return this._axios.
get(`/payments?${_querystring.default.stringify(query)}`).
then(throwWhenNotSuccess, handleError);
}
getAuthorizations({
transactionId,
orderId } =
{}) {
(0, _invariant.default)(
transactionId || orderId,
'getAuthorizations: One of `transactionId` or `orderId` must be provided');
const query = {};
if (transactionId) {
query.transactionId = transactionId;
}
if (orderId) {
query.orderId = orderId;
}
return this._axios.
get(`/payments/authorizations?${_querystring.default.stringify(query)}`).
then(throwWhenNotSuccess, handleError);
}
reserve(_ref)
{let { productName, amount, currency, confirmUrl, orderId } = _ref,options = _objectWithoutProperties(_ref, ["productName", "amount", "currency", "confirmUrl", "orderId"]);
return this._axios.
post('/payments/request', _objectSpread({
productName,
amount,
currency,
confirmUrl,
orderId },
options)).
then(throwWhenNotSuccess, handleError);
}
confirm(
transactionId,
{
amount,
currency })
{
return this._axios.
post(`/payments/${transactionId}/confirm`, {
amount,
currency }).
then(throwWhenNotSuccess, handleError);
}
capture(
transactionId,
{
amount,
currency })
{
return this._axios.
post(`/payments/authorizations/${transactionId}/capture`, {
amount,
currency }).
then(throwWhenNotSuccess, handleError);
}
void(transactionId) {
return this._axios.
post(`/payments/authorizations/${transactionId}/void`).
then(throwWhenNotSuccess, handleError);
}
refund(transactionId, options = {}) {
return this._axios.
post(`/payments/${transactionId}/refund`, options).
then(throwWhenNotSuccess, handleError);
}}exports.default = LinePay;
\ No newline at end of file
"use strict";
\ No newline at end of file
"use strict";Object.defineProperty(exports, "__esModule", { value: true });Object.defineProperty(exports, "Line", { enumerable: true, get: function () {return _Line.default;} });exports.default = void 0;
var _Line = _interopRequireDefault(require("./Line"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var _default =
{ Line: _Line.default };exports.default = _default;
\ No newline at end of file
"use strict";Object.defineProperty(exports, "__esModule", { value: true });Object.defineProperty(exports, "Line", { enumerable: true, get: function () {return _Line.default;} });Object.defineProperty(exports, "LineClient", { enumerable: true, get: function () {return _LineClient.default;} });Object.defineProperty(exports, "LinePay", { enumerable: true, get: function () {return _LinePay.default;} });exports.default = void 0;
var _Line = _interopRequireDefault(require("./Line"));
var _LineClient = _interopRequireDefault(require("./LineClient"));
var _LinePay = _interopRequireDefault(require("./LinePay"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var _default =
{ Line: _Line.default, LineClient: _LineClient.default, LinePay: _LinePay.default };exports.default = _default;
\ No newline at end of file
3.1.0 / 2017-09-26
==================
* Add `DEBUG_HIDE_DATE` env var (#486)
* Remove ReDoS regexp in %o formatter (#504)
* Remove "component" from package.json
* Remove `component.json`
* Ignore package-lock.json
* Examples: fix colors printout
* Fix: browser detection
* Fix: spelling mistake (#496, @EdwardBetts)
3.0.1 / 2017-08-24
==================
* Fix: Disable colors in Edge and Internet Explorer (#489)
3.0.0 / 2017-08-08
==================
* Breaking: Remove DEBUG_FD (#406)
* Breaking: Use `Date#toISOString()` instead to `Date#toUTCString()` when output is not a TTY (#418)
* Breaking: Make millisecond timer namespace specific and allow 'always enabled' output (#408)
* Addition: document `enabled` flag (#465)
* Addition: add 256 colors mode (#481)
* Addition: `enabled()` updates existing debug instances, add `destroy()` function (#440)
* Update: component: update "ms" to v2.0.0
* Update: separate the Node and Browser tests in Travis-CI
* Update: refactor Readme, fixed documentation, added "Namespace Colors" section, redid screenshots
* Update: separate Node.js and web browser examples for organization
* Update: update "browserify" to v14.4.0
* Fix: fix Readme typo (#473)
2.6.9 / 2017-09-22
==================
* remove ReDoS regexp in %o formatter (#504)
2.6.8 / 2017-05-18
==================
* Fix: Check for undefined on browser globals (#462, @marbemac)
2.6.7 / 2017-05-16
==================
* Fix: Update ms to 2.0.0 to fix regular expression denial of service vulnerability (#458, @hubdotcom)
* Fix: Inline extend function in node implementation (#452, @dougwilson)
* Docs: Fix typo (#455, @msasad)
2.6.5 / 2017-04-27
==================
* Fix: null reference check on window.documentElement.style.WebkitAppearance (#447, @thebigredgeek)
* Misc: clean up browser reference checks (#447, @thebigredgeek)
* Misc: add npm-debug.log to .gitignore (@thebigredgeek)
2.6.4 / 2017-04-20
==================
* Fix: bug that would occur if process.env.DEBUG is a non-string value. (#444, @LucianBuzzo)
* Chore: ignore bower.json in npm installations. (#437, @joaovieira)
* Misc: update "ms" to v0.7.3 (@tootallnate)
2.6.3 / 2017-03-13
==================
* Fix: Electron reference to `process.env.DEBUG` (#431, @paulcbetts)
* Docs: Changelog fix (@thebigredgeek)
2.6.2 / 2017-03-10
==================
* Fix: DEBUG_MAX_ARRAY_LENGTH (#420, @slavaGanzin)
* Docs: Add backers and sponsors from Open Collective (#422, @piamancini)
* Docs: Add Slackin invite badge (@tootallnate)
2.6.1 / 2017-02-10
==================
* Fix: Module's `export default` syntax fix for IE8 `Expected identifier` error
* Fix: Whitelist DEBUG_FD for values 1 and 2 only (#415, @pi0)
* Fix: IE8 "Expected identifier" error (#414, @vgoma)
* Fix: Namespaces would not disable once enabled (#409, @musikov)
2.6.0 / 2016-12-28
==================
* Fix: added better null pointer checks for browser useColors (@thebigredgeek)
* Improvement: removed explicit `window.debug` export (#404, @tootallnate)
* Improvement: deprecated `DEBUG_FD` environment variable (#405, @tootallnate)
2.5.2 / 2016-12-25
==================
* Fix: reference error on window within webworkers (#393, @KlausTrainer)
* Docs: fixed README typo (#391, @lurch)
* Docs: added notice about v3 api discussion (@thebigredgeek)
2.5.1 / 2016-12-20
==================
* Fix: babel-core compatibility
2.5.0 / 2016-12-20
==================
* Fix: wrong reference in bower file (@thebigredgeek)
* Fix: webworker compatibility (@thebigredgeek)
* Fix: output formatting issue (#388, @kribblo)
* Fix: babel-loader compatibility (#383, @escwald)
* Misc: removed built asset from repo and publications (@thebigredgeek)
* Misc: moved source files to /src (#378, @yamikuronue)
* Test: added karma integration and replaced babel with browserify for browser tests (#378, @yamikuronue)
* Test: coveralls integration (#378, @yamikuronue)
* Docs: simplified language in the opening paragraph (#373, @yamikuronue)
2.4.5 / 2016-12-17
==================
* Fix: `navigator` undefined in Rhino (#376, @jochenberger)
* Fix: custom log function (#379, @hsiliev)
* Improvement: bit of cleanup + linting fixes (@thebigredgeek)
* Improvement: rm non-maintainted `dist/` dir (#375, @freewil)
* Docs: simplified language in the opening paragraph. (#373, @yamikuronue)
2.4.4 / 2016-12-14
==================
* Fix: work around debug being loaded in preload scripts for electron (#368, @paulcbetts)
2.4.3 / 2016-12-14
==================
* Fix: navigation.userAgent error for react native (#364, @escwald)
2.4.2 / 2016-12-14
==================
* Fix: browser colors (#367, @tootallnate)
* Misc: travis ci integration (@thebigredgeek)
* Misc: added linting and testing boilerplate with sanity check (@thebigredgeek)
2.4.1 / 2016-12-13
==================
* Fix: typo that broke the package (#356)
2.4.0 / 2016-12-13
==================
* Fix: bower.json references unbuilt src entry point (#342, @justmatt)
* Fix: revert "handle regex special characters" (@tootallnate)
* Feature: configurable util.inspect()`options for NodeJS (#327, @tootallnate)
* Feature: %O`(big O) pretty-prints objects (#322, @tootallnate)
* Improvement: allow colors in workers (#335, @botverse)
* Improvement: use same color for same namespace. (#338, @lchenay)
2.3.3 / 2016-11-09
==================
* Fix: Catch `JSON.stringify()` errors (#195, Jovan Alleyne)
* Fix: Returning `localStorage` saved values (#331, Levi Thomason)
* Improvement: Don't create an empty object when no `process` (Nathan Rajlich)
2.3.2 / 2016-11-09
==================
* Fix: be super-safe in index.js as well (@TooTallNate)
* Fix: should check whether process exists (Tom Newby)
2.3.1 / 2016-11-09
==================
* Fix: Added electron compatibility (#324, @paulcbetts)
* Improvement: Added performance optimizations (@tootallnate)
* Readme: Corrected PowerShell environment variable example (#252, @gimre)
* Misc: Removed yarn lock file from source control (#321, @fengmk2)
2.3.0 / 2016-11-07
==================
* Fix: Consistent placement of ms diff at end of output (#215, @gorangajic)
* Fix: Escaping of regex special characters in namespace strings (#250, @zacronos)
* Fix: Fixed bug causing crash on react-native (#282, @vkarpov15)
* Feature: Enabled ES6+ compatible import via default export (#212 @bucaran)
* Feature: Added %O formatter to reflect Chrome's console.log capability (#279, @oncletom)
* Package: Update "ms" to 0.7.2 (#315, @DevSide)
* Package: removed superfluous version property from bower.json (#207 @kkirsche)
* Readme: fix USE_COLORS to DEBUG_COLORS
* Readme: Doc fixes for format string sugar (#269, @mlucool)
* Readme: Updated docs for DEBUG_FD and DEBUG_COLORS environment variables (#232, @mattlyons0)
* Readme: doc fixes for PowerShell (#271 #243, @exoticknight @unreadable)
* Readme: better docs for browser support (#224, @matthewmueller)
* Tooling: Added yarn integration for development (#317, @thebigredgeek)
* Misc: Renamed History.md to CHANGELOG.md (@thebigredgeek)
* Misc: Added license file (#226 #274, @CantemoInternal @sdaitzman)
* Misc: Updated contributors (@thebigredgeek)
2.2.0 / 2015-05-09
==================
* package: update "ms" to v0.7.1 (#202, @dougwilson)
* README: add logging to file example (#193, @DanielOchoa)
* README: fixed a typo (#191, @amir-s)
* browser: expose `storage` (#190, @stephenmathieson)
* Makefile: add a `distclean` target (#189, @stephenmathieson)
2.1.3 / 2015-03-13
==================
* Updated stdout/stderr example (#186)
* Updated example/stdout.js to match debug current behaviour
* Renamed example/stderr.js to stdout.js
* Update Readme.md (#184)
* replace high intensity foreground color for bold (#182, #183)
2.1.2 / 2015-03-01
==================
* dist: recompile
* update "ms" to v0.7.0
* package: update "browserify" to v9.0.3
* component: fix "ms.js" repo location
* changed bower package name
* updated documentation about using debug in a browser
* fix: security error on safari (#167, #168, @yields)
2.1.1 / 2014-12-29
==================
* browser: use `typeof` to check for `console` existence
* browser: check for `console.log` truthiness (fix IE 8/9)
* browser: add support for Chrome apps
* Readme: added Windows usage remarks
* Add `bower.json` to properly support bower install
2.1.0 / 2014-10-15
==================
* node: implement `DEBUG_FD` env variable support
* package: update "browserify" to v6.1.0
* package: add "license" field to package.json (#135, @panuhorsmalahti)
2.0.0 / 2014-09-01
==================
* package: update "browserify" to v5.11.0
* node: use stderr rather than stdout for logging (#29, @stephenmathieson)
1.0.4 / 2014-07-15
==================
* dist: recompile
* example: remove `console.info()` log usage
* example: add "Content-Type" UTF-8 header to browser example
* browser: place %c marker after the space character
* browser: reset the "content" color via `color: inherit`
* browser: add colors support for Firefox >= v31
* debug: prefer an instance `log()` function over the global one (#119)
* Readme: update documentation about styled console logs for FF v31 (#116, @wryk)
1.0.3 / 2014-07-09
==================
* Add support for multiple wildcards in namespaces (#122, @seegno)
* browser: fix lint
1.0.2 / 2014-06-10
==================
* browser: update color palette (#113, @gscottolson)
* common: make console logging function configurable (#108, @timoxley)
* node: fix %o colors on old node <= 0.8.x
* Makefile: find node path using shell/which (#109, @timoxley)
1.0.1 / 2014-06-06
==================
* browser: use `removeItem()` to clear localStorage
* browser, node: don't set DEBUG if namespaces is undefined (#107, @leedm777)
* package: add "contributors" section
* node: fix comment typo
* README: list authors
1.0.0 / 2014-06-04
==================
* make ms diff be global, not be scope
* debug: ignore empty strings in enable()
* node: make DEBUG_COLORS able to disable coloring
* *: export the `colors` array
* npmignore: don't publish the `dist` dir
* Makefile: refactor to use browserify
* package: add "browserify" as a dev dependency
* Readme: add Web Inspector Colors section
* node: reset terminal color for the debug content
* node: map "%o" to `util.inspect()`
* browser: map "%j" to `JSON.stringify()`
* debug: add custom "formatters"
* debug: use "ms" module for humanizing the diff
* Readme: add "bash" syntax highlighting
* browser: add Firebug color support
* browser: add colors for WebKit browsers
* node: apply log to `console`
* rewrite: abstract common logic for Node & browsers
* add .jshintrc file
0.8.1 / 2014-04-14
==================
* package: re-add the "component" section
0.8.0 / 2014-03-30
==================
* add `enable()` method for nodejs. Closes #27
* change from stderr to stdout
* remove unnecessary index.js file
0.7.4 / 2013-11-13
==================
* remove "browserify" key from package.json (fixes something in browserify)
0.7.3 / 2013-10-30
==================
* fix: catch localStorage security error when cookies are blocked (Chrome)
* add debug(err) support. Closes #46
* add .browser prop to package.json. Closes #42
0.7.2 / 2013-02-06
==================
* fix package.json
* fix: Mobile Safari (private mode) is broken with debug
* fix: Use unicode to send escape character to shell instead of octal to work with strict mode javascript
0.7.1 / 2013-02-05
==================
* add repository URL to package.json
* add DEBUG_COLORED to force colored output
* add browserify support
* fix component. Closes #24
0.7.0 / 2012-05-04
==================
* Added .component to package.json
* Added debug.component.js build
0.6.0 / 2012-03-16
==================
* Added support for "-" prefix in DEBUG [Vinay Pulim]
* Added `.enabled` flag to the node version [TooTallNate]
0.5.0 / 2012-02-02
==================
* Added: humanize diffs. Closes #8
* Added `debug.disable()` to the CS variant
* Removed padding. Closes #10
* Fixed: persist client-side variant again. Closes #9
0.4.0 / 2012-02-01
==================
* Added browser variant support for older browsers [TooTallNate]
* Added `debug.enable('project:*')` to browser variant [TooTallNate]
* Added padding to diff (moved it to the right)
0.3.0 / 2012-01-26
==================
* Added millisecond diff when isatty, otherwise UTC string
0.2.0 / 2012-01-22
==================
* Added wildcard support
0.1.0 / 2011-12-02
==================
* Added: remove colors unless stderr isatty [TooTallNate]
0.0.1 / 2010-01-03
==================
* Initial release
(The MIT License)
Copyright (c) 2014 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the 'Software'), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# debug
[![Build Status](https://travis-ci.org/visionmedia/debug.svg?branch=master)](https://travis-ci.org/visionmedia/debug) [![Coverage Status](https://coveralls.io/repos/github/visionmedia/debug/badge.svg?branch=master)](https://coveralls.io/github/visionmedia/debug?branch=master) [![Slack](https://visionmedia-community-slackin.now.sh/badge.svg)](https://visionmedia-community-slackin.now.sh/) [![OpenCollective](https://opencollective.com/debug/backers/badge.svg)](#backers)
[![OpenCollective](https://opencollective.com/debug/sponsors/badge.svg)](#sponsors)
<img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
A tiny JavaScript debugging utility modelled after Node.js core's debugging
technique. Works in Node.js and web browsers.
## Installation
```bash
$ npm install debug
```
## Usage
`debug` exposes a function; simply pass this function the name of your module, and it will return a decorated version of `console.error` for you to pass debug statements to. This will allow you to toggle the debug output for different parts of your module as well as the module as a whole.
Example [_app.js_](./examples/node/app.js):
```js
var debug = require('debug')('http')
, http = require('http')
, name = 'My App';
// fake app
debug('booting %o', name);
http.createServer(function(req, res){
debug(req.method + ' ' + req.url);
res.end('hello\n');
}).listen(3000, function(){
debug('listening');
});
// fake worker of some kind
require('./worker');
```
Example [_worker.js_](./examples/node/worker.js):
```js
var a = require('debug')('worker:a')
, b = require('debug')('worker:b');
function work() {
a('doing lots of uninteresting work');
setTimeout(work, Math.random() * 1000);
}
work();
function workb() {
b('doing some work');
setTimeout(workb, Math.random() * 2000);
}
workb();
```
The `DEBUG` environment variable is then used to enable these based on space or
comma-delimited names.
Here are some examples:
<img width="647" alt="screen shot 2017-08-08 at 12 53 04 pm" src="https://user-images.githubusercontent.com/71256/29091703-a6302cdc-7c38-11e7-8304-7c0b3bc600cd.png">
<img width="647" alt="screen shot 2017-08-08 at 12 53 38 pm" src="https://user-images.githubusercontent.com/71256/29091700-a62a6888-7c38-11e7-800b-db911291ca2b.png">
<img width="647" alt="screen shot 2017-08-08 at 12 53 25 pm" src="https://user-images.githubusercontent.com/71256/29091701-a62ea114-7c38-11e7-826a-2692bedca740.png">
#### Windows command prompt notes
##### CMD
On Windows the environment variable is set using the `set` command.
```cmd
set DEBUG=*,-not_this
```
Example:
```cmd
set DEBUG=* & node app.js
```
##### PowerShell (VS Code default)
PowerShell uses different syntax to set environment variables.
```cmd
$env:DEBUG = "*,-not_this"
```
Example:
```cmd
$env:DEBUG='app';node app.js
```
Then, run the program to be debugged as usual.
npm script example:
```js
"windowsDebug": "@powershell -Command $env:DEBUG='*';node app.js",
```
## Namespace Colors
Every debug instance has a color generated for it based on its namespace name.
This helps when visually parsing the debug output to identify which debug instance
a debug line belongs to.
#### Node.js
In Node.js, colors are enabled when stderr is a TTY. You also _should_ install
the [`supports-color`](https://npmjs.org/supports-color) module alongside debug,
otherwise debug will only use a small handful of basic colors.
<img width="521" src="https://user-images.githubusercontent.com/71256/29092181-47f6a9e6-7c3a-11e7-9a14-1928d8a711cd.png">
#### Web Browser
Colors are also enabled on "Web Inspectors" that understand the `%c` formatting
option. These are WebKit web inspectors, Firefox ([since version
31](https://hacks.mozilla.org/2014/05/editable-box-model-multiple-selection-sublime-text-keys-much-more-firefox-developer-tools-episode-31/))
and the Firebug plugin for Firefox (any version).
<img width="524" src="https://user-images.githubusercontent.com/71256/29092033-b65f9f2e-7c39-11e7-8e32-f6f0d8e865c1.png">
## Millisecond diff
When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls.
<img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
When stdout is not a TTY, `Date#toISOString()` is used, making it more useful for logging the debug information as shown below:
<img width="647" src="https://user-images.githubusercontent.com/71256/29091956-6bd78372-7c39-11e7-8c55-c948396d6edd.png">
## Conventions
If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser". If you append a "*" to the end of your name, it will always be enabled regardless of the setting of the DEBUG environment variable. You can then use it for normal output as well as debug output.
## Wildcards
The `*` character may be used as a wildcard. Suppose for example your library has
debuggers named "connect:bodyParser", "connect:compress", "connect:session",
instead of listing all three with
`DEBUG=connect:bodyParser,connect:compress,connect:session`, you may simply do
`DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.
You can also exclude specific debuggers by prefixing them with a "-" character.
For example, `DEBUG=*,-connect:*` would include all debuggers except those
starting with "connect:".
## Environment Variables
When running through Node.js, you can set a few environment variables that will
change the behavior of the debug logging:
| Name | Purpose |
|-----------|-------------------------------------------------|
| `DEBUG` | Enables/disables specific debugging namespaces. |
| `DEBUG_HIDE_DATE` | Hide date from debug output (non-TTY). |
| `DEBUG_COLORS`| Whether or not to use colors in the debug output. |
| `DEBUG_DEPTH` | Object inspection depth. |
| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
__Note:__ The environment variables beginning with `DEBUG_` end up being
converted into an Options object that gets used with `%o`/`%O` formatters.
See the Node.js documentation for
[`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options)
for the complete list.
## Formatters
Debug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting.
Below are the officially supported formatters:
| Formatter | Representation |
|-----------|----------------|
| `%O` | Pretty-print an Object on multiple lines. |
| `%o` | Pretty-print an Object all on a single line. |
| `%s` | String. |
| `%d` | Number (both integer and float). |
| `%j` | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
| `%%` | Single percent sign ('%'). This does not consume an argument. |
### Custom formatters
You can add custom formatters by extending the `debug.formatters` object.
For example, if you wanted to add support for rendering a Buffer as hex with
`%h`, you could do something like:
```js
const createDebug = require('debug')
createDebug.formatters.h = (v) => {
return v.toString('hex')
}
// …elsewhere
const debug = createDebug('foo')
debug('this is hex: %h', new Buffer('hello world'))
// foo this is hex: 68656c6c6f20776f726c6421 +0ms
```
## Browser Support
You can build a browser-ready script using [browserify](https://github.com/substack/node-browserify),
or just use the [browserify-as-a-service](https://wzrd.in/) [build](https://wzrd.in/standalone/debug@latest),
if you don't want to build it yourself.
Debug's enable state is currently persisted by `localStorage`.
Consider the situation shown below where you have `worker:a` and `worker:b`,
and wish to debug both. You can enable this using `localStorage.debug`:
```js
localStorage.debug = 'worker:*'
```
And then refresh the page.
```js
a = debug('worker:a');
b = debug('worker:b');
setInterval(function(){
a('doing some work');
}, 1000);
setInterval(function(){
b('doing some work');
}, 1200);
```
## Output streams
By default `debug` will log to stderr, however this can be configured per-namespace by overriding the `log` method:
Example [_stdout.js_](./examples/node/stdout.js):
```js
var debug = require('debug');
var error = debug('app:error');
// by default stderr is used
error('goes to stderr!');
var log = debug('app:log');
// set this namespace to log via console.log
log.log = console.log.bind(console); // don't forget to bind to console!
log('goes to stdout');
error('still goes to stderr!');
// set all output to go via console.info
// overrides all per-namespace log settings
debug.log = console.info.bind(console);
error('now goes to stdout via console.info');
log('still goes to stdout, but via console.info now');
```
## Extend
You can simply extend debugger
```js
const log = require('debug')('auth');
//creates new debug instance with extended namespace
const logSign = log.extend('sign');
const logLogin = log.extend('login');
log('hello'); // auth hello
logSign('hello'); //auth:sign hello
logLogin('hello'); //auth:login hello
```
## Set dynamically
You can also enable debug dynamically by calling the `enable()` method :
```js
let debug = require('debug');
console.log(1, debug.enabled('test'));
debug.enable('test');
console.log(2, debug.enabled('test'));
debug.disable();
console.log(3, debug.enabled('test'));
```
print :
```
1 false
2 true
3 false
```
Usage :
`enable(namespaces)`
`namespaces` can include modes separated by a colon and wildcards.
Note that calling `enable()` completely overrides previously set DEBUG variable :
```
$ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(dbg.enabled("foo"))'
=> false
```
`disable()`
Will disable all namespaces. The functions returns the namespaces currently
enabled (and skipped). This can be useful if you want to disable debugging
temporarily without knowing what was enabled to begin with.
For example:
```js
let debug = require('debug');
debug.enable('foo:*,-foo:bar');
let namespaces = debug.disable();
debug.enable(namespaces);
```
Note: There is no guarantee that the string will be identical to the initial
enable string, but semantically they will be identical.
## Checking whether a debug target is enabled
After you've created a debug instance, you can determine whether or not it is
enabled by checking the `enabled` property:
```javascript
const debug = require('debug')('http');
if (debug.enabled) {
// do stuff...
}
```
You can also manually toggle this property to force the debug instance to be
enabled or disabled.
## Authors
- TJ Holowaychuk
- Nathan Rajlich
- Andrew Rhyne
## Backers
Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/debug#backer)]
<a href="https://opencollective.com/debug/backer/0/website" target="_blank"><img src="https://opencollective.com/debug/backer/0/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/1/website" target="_blank"><img src="https://opencollective.com/debug/backer/1/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/2/website" target="_blank"><img src="https://opencollective.com/debug/backer/2/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/3/website" target="_blank"><img src="https://opencollective.com/debug/backer/3/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/4/website" target="_blank"><img src="https://opencollective.com/debug/backer/4/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/5/website" target="_blank"><img src="https://opencollective.com/debug/backer/5/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/6/website" target="_blank"><img src="https://opencollective.com/debug/backer/6/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/7/website" target="_blank"><img src="https://opencollective.com/debug/backer/7/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/8/website" target="_blank"><img src="https://opencollective.com/debug/backer/8/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/9/website" target="_blank"><img src="https://opencollective.com/debug/backer/9/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/10/website" target="_blank"><img src="https://opencollective.com/debug/backer/10/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/11/website" target="_blank"><img src="https://opencollective.com/debug/backer/11/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/12/website" target="_blank"><img src="https://opencollective.com/debug/backer/12/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/13/website" target="_blank"><img src="https://opencollective.com/debug/backer/13/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/14/website" target="_blank"><img src="https://opencollective.com/debug/backer/14/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/15/website" target="_blank"><img src="https://opencollective.com/debug/backer/15/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/16/website" target="_blank"><img src="https://opencollective.com/debug/backer/16/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/17/website" target="_blank"><img src="https://opencollective.com/debug/backer/17/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/18/website" target="_blank"><img src="https://opencollective.com/debug/backer/18/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/19/website" target="_blank"><img src="https://opencollective.com/debug/backer/19/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/20/website" target="_blank"><img src="https://opencollective.com/debug/backer/20/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/21/website" target="_blank"><img src="https://opencollective.com/debug/backer/21/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/22/website" target="_blank"><img src="https://opencollective.com/debug/backer/22/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/23/website" target="_blank"><img src="https://opencollective.com/debug/backer/23/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/24/website" target="_blank"><img src="https://opencollective.com/debug/backer/24/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/25/website" target="_blank"><img src="https://opencollective.com/debug/backer/25/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/26/website" target="_blank"><img src="https://opencollective.com/debug/backer/26/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/27/website" target="_blank"><img src="https://opencollective.com/debug/backer/27/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/28/website" target="_blank"><img src="https://opencollective.com/debug/backer/28/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/29/website" target="_blank"><img src="https://opencollective.com/debug/backer/29/avatar.svg"></a>
## Sponsors
Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/debug#sponsor)]
<a href="https://opencollective.com/debug/sponsor/0/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/0/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/1/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/1/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/2/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/2/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/3/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/3/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/4/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/4/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/5/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/5/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/6/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/6/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/7/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/7/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/8/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/8/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/9/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/9/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/10/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/10/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/11/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/11/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/12/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/12/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/13/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/13/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/14/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/14/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/15/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/15/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/16/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/16/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/17/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/17/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/18/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/18/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/19/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/19/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/20/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/20/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/21/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/21/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/22/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/22/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/23/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/23/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/24/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/24/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/25/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/25/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/26/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/26/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/27/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/27/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/28/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/28/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/29/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/29/avatar.svg"></a>
## License
(The MIT License)
Copyright (c) 2014-2017 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"use strict";
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
(function (f) {
if ((typeof exports === "undefined" ? "undefined" : _typeof(exports)) === "object" && typeof module !== "undefined") {
module.exports = f();
} else if (typeof define === "function" && define.amd) {
define([], f);
} else {
var g;
if (typeof window !== "undefined") {
g = window;
} else if (typeof global !== "undefined") {
g = global;
} else if (typeof self !== "undefined") {
g = self;
} else {
g = this;
}
g.debug = f();
}
})(function () {
var define, module, exports;
return function () {
function r(e, n, t) {
function o(i, f) {
if (!n[i]) {
if (!e[i]) {
var c = "function" == typeof require && require;
if (!f && c) return c(i, !0);
if (u) return u(i, !0);
var a = new Error("Cannot find module '" + i + "'");
throw a.code = "MODULE_NOT_FOUND", a;
}
var p = n[i] = {
exports: {}
};
e[i][0].call(p.exports, function (r) {
var n = e[i][1][r];
return o(n || r);
}, p, p.exports, r, e, n, t);
}
return n[i].exports;
}
for (var u = "function" == typeof require && require, i = 0; i < t.length; i++) {
o(t[i]);
}
return o;
}
return r;
}()({
1: [function (require, module, exports) {
/**
* Helpers.
*/
var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var w = d * 7;
var y = d * 365.25;
/**
* Parse or format the given `val`.
*
* Options:
*
* - `long` verbose formatting [false]
*
* @param {String|Number} val
* @param {Object} [options]
* @throws {Error} throw an error if val is not a non-empty string or a number
* @return {String|Number}
* @api public
*/
module.exports = function (val, options) {
options = options || {};
var type = _typeof(val);
if (type === 'string' && val.length > 0) {
return parse(val);
} else if (type === 'number' && isNaN(val) === false) {
return options.long ? fmtLong(val) : fmtShort(val);
}
throw new Error('val is not a non-empty string or a valid number. val=' + JSON.stringify(val));
};
/**
* Parse the given `str` and return milliseconds.
*
* @param {String} str
* @return {Number}
* @api private
*/
function parse(str) {
str = String(str);
if (str.length > 100) {
return;
}
var match = /^((?:\d+)?\-?\d?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(str);
if (!match) {
return;
}
var n = parseFloat(match[1]);
var type = (match[2] || 'ms').toLowerCase();
switch (type) {
case 'years':
case 'year':
case 'yrs':
case 'yr':
case 'y':
return n * y;
case 'weeks':
case 'week':
case 'w':
return n * w;
case 'days':
case 'day':
case 'd':
return n * d;
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
return n * h;
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
return n * m;
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
return n * s;
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'ms':
return n;
default:
return undefined;
}
}
/**
* Short format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function fmtShort(ms) {
var msAbs = Math.abs(ms);
if (msAbs >= d) {
return Math.round(ms / d) + 'd';
}
if (msAbs >= h) {
return Math.round(ms / h) + 'h';
}
if (msAbs >= m) {
return Math.round(ms / m) + 'm';
}
if (msAbs >= s) {
return Math.round(ms / s) + 's';
}
return ms + 'ms';
}
/**
* Long format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function fmtLong(ms) {
var msAbs = Math.abs(ms);
if (msAbs >= d) {
return plural(ms, msAbs, d, 'day');
}
if (msAbs >= h) {
return plural(ms, msAbs, h, 'hour');
}
if (msAbs >= m) {
return plural(ms, msAbs, m, 'minute');
}
if (msAbs >= s) {
return plural(ms, msAbs, s, 'second');
}
return ms + ' ms';
}
/**
* Pluralization helper.
*/
function plural(ms, msAbs, n, name) {
var isPlural = msAbs >= n * 1.5;
return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
}
}, {}],
2: [function (require, module, exports) {
// shim for using process in browser
var process = module.exports = {}; // cached from whatever global is present so that test runners that stub it
// don't break things. But we need to wrap it in a try catch in case it is
// wrapped in strict mode code which doesn't define any globals. It's inside a
// function because try/catches deoptimize in certain engines.
var cachedSetTimeout;
var cachedClearTimeout;
function defaultSetTimout() {
throw new Error('setTimeout has not been defined');
}
function defaultClearTimeout() {
throw new Error('clearTimeout has not been defined');
}
(function () {
try {
if (typeof setTimeout === 'function') {
cachedSetTimeout = setTimeout;
} else {
cachedSetTimeout = defaultSetTimout;
}
} catch (e) {
cachedSetTimeout = defaultSetTimout;
}
try {
if (typeof clearTimeout === 'function') {
cachedClearTimeout = clearTimeout;
} else {
cachedClearTimeout = defaultClearTimeout;
}
} catch (e) {
cachedClearTimeout = defaultClearTimeout;
}
})();
function runTimeout(fun) {
if (cachedSetTimeout === setTimeout) {
//normal enviroments in sane situations
return setTimeout(fun, 0);
} // if setTimeout wasn't available but was latter defined
if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
cachedSetTimeout = setTimeout;
return setTimeout(fun, 0);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedSetTimeout(fun, 0);
} catch (e) {
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedSetTimeout.call(null, fun, 0);
} catch (e) {
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
return cachedSetTimeout.call(this, fun, 0);
}
}
}
function runClearTimeout(marker) {
if (cachedClearTimeout === clearTimeout) {
//normal enviroments in sane situations
return clearTimeout(marker);
} // if clearTimeout wasn't available but was latter defined
if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
cachedClearTimeout = clearTimeout;
return clearTimeout(marker);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedClearTimeout(marker);
} catch (e) {
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedClearTimeout.call(null, marker);
} catch (e) {
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
// Some versions of I.E. have different rules for clearTimeout vs setTimeout
return cachedClearTimeout.call(this, marker);
}
}
}
var queue = [];
var draining = false;
var currentQueue;
var queueIndex = -1;
function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
}
draining = false;
if (currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
}
if (queue.length) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
}
var timeout = runTimeout(cleanUpNextTick);
draining = true;
var len = queue.length;
while (len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
runClearTimeout(timeout);
}
process.nextTick = function (fun) {
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
runTimeout(drainQueue);
}
}; // v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.version = ''; // empty string to avoid regexp issues
process.versions = {};
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.prependListener = noop;
process.prependOnceListener = noop;
process.listeners = function (name) {
return [];
};
process.binding = function (name) {
throw new Error('process.binding is not supported');
};
process.cwd = function () {
return '/';
};
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
process.umask = function () {
return 0;
};
}, {}],
3: [function (require, module, exports) {
/**
* This is the common logic for both the Node.js and web browser
* implementations of `debug()`.
*/
function setup(env) {
createDebug.debug = createDebug;
createDebug.default = createDebug;
createDebug.coerce = coerce;
createDebug.disable = disable;
createDebug.enable = enable;
createDebug.enabled = enabled;
createDebug.humanize = require('ms');
Object.keys(env).forEach(function (key) {
createDebug[key] = env[key];
});
/**
* Active `debug` instances.
*/
createDebug.instances = [];
/**
* The currently active debug mode names, and names to skip.
*/
createDebug.names = [];
createDebug.skips = [];
/**
* Map of special "%n" handling functions, for the debug "format" argument.
*
* Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
*/
createDebug.formatters = {};
/**
* Selects a color for a debug namespace
* @param {String} namespace The namespace string for the for the debug instance to be colored
* @return {Number|String} An ANSI color code for the given namespace
* @api private
*/
function selectColor(namespace) {
var hash = 0;
for (var i = 0; i < namespace.length; i++) {
hash = (hash << 5) - hash + namespace.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
}
createDebug.selectColor = selectColor;
/**
* Create a debugger with the given `namespace`.
*
* @param {String} namespace
* @return {Function}
* @api public
*/
function createDebug(namespace) {
var prevTime;
function debug() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
// Disabled?
if (!debug.enabled) {
return;
}
var self = debug; // Set `diff` timestamp
var curr = Number(new Date());
var ms = curr - (prevTime || curr);
self.diff = ms;
self.prev = prevTime;
self.curr = curr;
prevTime = curr;
args[0] = createDebug.coerce(args[0]);
if (typeof args[0] !== 'string') {
// Anything else let's inspect with %O
args.unshift('%O');
} // Apply any `formatters` transformations
var index = 0;
args[0] = args[0].replace(/%([a-zA-Z%])/g, function (match, format) {
// If we encounter an escaped % then don't increase the array index
if (match === '%%') {
return match;
}
index++;
var formatter = createDebug.formatters[format];
if (typeof formatter === 'function') {
var val = args[index];
match = formatter.call(self, val); // Now we need to remove `args[index]` since it's inlined in the `format`
args.splice(index, 1);
index--;
}
return match;
}); // Apply env-specific formatting (colors, etc.)
createDebug.formatArgs.call(self, args);
var logFn = self.log || createDebug.log;
logFn.apply(self, args);
}
debug.namespace = namespace;
debug.enabled = createDebug.enabled(namespace);
debug.useColors = createDebug.useColors();
debug.color = selectColor(namespace);
debug.destroy = destroy;
debug.extend = extend; // Debug.formatArgs = formatArgs;
// debug.rawLog = rawLog;
// env-specific initialization logic for debug instances
if (typeof createDebug.init === 'function') {
createDebug.init(debug);
}
createDebug.instances.push(debug);
return debug;
}
function destroy() {
var index = createDebug.instances.indexOf(this);
if (index !== -1) {
createDebug.instances.splice(index, 1);
return true;
}
return false;
}
function extend(namespace, delimiter) {
var newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
newDebug.log = this.log;
return newDebug;
}
/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
*
* @param {String} namespaces
* @api public
*/
function enable(namespaces) {
createDebug.save(namespaces);
createDebug.names = [];
createDebug.skips = [];
var i;
var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
var len = split.length;
for (i = 0; i < len; i++) {
if (!split[i]) {
// ignore empty strings
continue;
}
namespaces = split[i].replace(/\*/g, '.*?');
if (namespaces[0] === '-') {
createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
} else {
createDebug.names.push(new RegExp('^' + namespaces + '$'));
}
}
for (i = 0; i < createDebug.instances.length; i++) {
var instance = createDebug.instances[i];
instance.enabled = createDebug.enabled(instance.namespace);
}
}
/**
* Disable debug output.
*
* @return {String} namespaces
* @api public
*/
function disable() {
var namespaces = [].concat(_toConsumableArray(createDebug.names.map(toNamespace)), _toConsumableArray(createDebug.skips.map(toNamespace).map(function (namespace) {
return '-' + namespace;
}))).join(',');
createDebug.enable('');
return namespaces;
}
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
function enabled(name) {
if (name[name.length - 1] === '*') {
return true;
}
var i;
var len;
for (i = 0, len = createDebug.skips.length; i < len; i++) {
if (createDebug.skips[i].test(name)) {
return false;
}
}
for (i = 0, len = createDebug.names.length; i < len; i++) {
if (createDebug.names[i].test(name)) {
return true;
}
}
return false;
}
/**
* Convert regexp to namespace
*
* @param {RegExp} regxep
* @return {String} namespace
* @api private
*/
function toNamespace(regexp) {
return regexp.toString().substring(2, regexp.toString().length - 2).replace(/\.\*\?$/, '*');
}
/**
* Coerce `val`.
*
* @param {Mixed} val
* @return {Mixed}
* @api private
*/
function coerce(val) {
if (val instanceof Error) {
return val.stack || val.message;
}
return val;
}
createDebug.enable(createDebug.load());
return createDebug;
}
module.exports = setup;
}, {
"ms": 1
}],
4: [function (require, module, exports) {
(function (process) {
/* eslint-env browser */
/**
* This is the web browser implementation of `debug()`.
*/
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.storage = localstorage();
/**
* Colors.
*/
exports.colors = ['#0000CC', '#0000FF', '#0033CC', '#0033FF', '#0066CC', '#0066FF', '#0099CC', '#0099FF', '#00CC00', '#00CC33', '#00CC66', '#00CC99', '#00CCCC', '#00CCFF', '#3300CC', '#3300FF', '#3333CC', '#3333FF', '#3366CC', '#3366FF', '#3399CC', '#3399FF', '#33CC00', '#33CC33', '#33CC66', '#33CC99', '#33CCCC', '#33CCFF', '#6600CC', '#6600FF', '#6633CC', '#6633FF', '#66CC00', '#66CC33', '#9900CC', '#9900FF', '#9933CC', '#9933FF', '#99CC00', '#99CC33', '#CC0000', '#CC0033', '#CC0066', '#CC0099', '#CC00CC', '#CC00FF', '#CC3300', '#CC3333', '#CC3366', '#CC3399', '#CC33CC', '#CC33FF', '#CC6600', '#CC6633', '#CC9900', '#CC9933', '#CCCC00', '#CCCC33', '#FF0000', '#FF0033', '#FF0066', '#FF0099', '#FF00CC', '#FF00FF', '#FF3300', '#FF3333', '#FF3366', '#FF3399', '#FF33CC', '#FF33FF', '#FF6600', '#FF6633', '#FF9900', '#FF9933', '#FFCC00', '#FFCC33'];
/**
* Currently only WebKit-based Web Inspectors, Firefox >= v31,
* and the Firebug extension (any Firefox version) are known
* to support "%c" CSS customizations.
*
* TODO: add a `localStorage` variable to explicitly enable/disable colors
*/
// eslint-disable-next-line complexity
function useColors() {
// NB: In an Electron preload script, document will be defined but not fully
// initialized. Since we know we're in Chrome, we'll just detect this case
// explicitly
if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
return true;
} // Internet Explorer and Edge do not support colors.
if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
return false;
} // Is webkit? http://stackoverflow.com/a/16459606/376773
// document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
return typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance || // Is firebug? http://stackoverflow.com/a/398120/376773
typeof window !== 'undefined' && window.console && (window.console.firebug || window.console.exception && window.console.table) || // Is firefox >= v31?
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31 || // Double check webkit in userAgent just in case we are in a worker
typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/);
}
/**
* Colorize log arguments if enabled.
*
* @api public
*/
function formatArgs(args) {
args[0] = (this.useColors ? '%c' : '') + this.namespace + (this.useColors ? ' %c' : ' ') + args[0] + (this.useColors ? '%c ' : ' ') + '+' + module.exports.humanize(this.diff);
if (!this.useColors) {
return;
}
var c = 'color: ' + this.color;
args.splice(1, 0, c, 'color: inherit'); // The final "%c" is somewhat tricky, because there could be other
// arguments passed either before or after the %c, so we need to
// figure out the correct index to insert the CSS into
var index = 0;
var lastC = 0;
args[0].replace(/%[a-zA-Z%]/g, function (match) {
if (match === '%%') {
return;
}
index++;
if (match === '%c') {
// We only are interested in the *last* %c
// (the user may have provided their own)
lastC = index;
}
});
args.splice(lastC, 0, c);
}
/**
* Invokes `console.log()` when available.
* No-op when `console.log` is not a "function".
*
* @api public
*/
function log() {
var _console;
// This hackery is required for IE8/9, where
// the `console.log` function doesn't have 'apply'
return (typeof console === "undefined" ? "undefined" : _typeof(console)) === 'object' && console.log && (_console = console).log.apply(_console, arguments);
}
/**
* Save `namespaces`.
*
* @param {String} namespaces
* @api private
*/
function save(namespaces) {
try {
if (namespaces) {
exports.storage.setItem('debug', namespaces);
} else {
exports.storage.removeItem('debug');
}
} catch (error) {// Swallow
// XXX (@Qix-) should we be logging these?
}
}
/**
* Load `namespaces`.
*
* @return {String} returns the previously persisted debug modes
* @api private
*/
function load() {
var r;
try {
r = exports.storage.getItem('debug');
} catch (error) {} // Swallow
// XXX (@Qix-) should we be logging these?
// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
if (!r && typeof process !== 'undefined' && 'env' in process) {
r = process.env.DEBUG;
}
return r;
}
/**
* Localstorage attempts to return the localstorage.
*
* This is necessary because safari throws
* when a user disables cookies/localstorage
* and you attempt to access it.
*
* @return {LocalStorage}
* @api private
*/
function localstorage() {
try {
// TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
// The Browser also has localStorage in the global context.
return localStorage;
} catch (error) {// Swallow
// XXX (@Qix-) should we be logging these?
}
}
module.exports = require('./common')(exports);
var formatters = module.exports.formatters;
/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/
formatters.j = function (v) {
try {
return JSON.stringify(v);
} catch (error) {
return '[UnexpectedJSONParseError]: ' + error.message;
}
};
}).call(this, require('_process'));
}, {
"./common": 3,
"_process": 2
}]
}, {}, [4])(4);
});
{
"_from": "debug@^4.1.1",
"_id": "debug@4.1.1",
"_inBundle": false,
"_integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
"_location": "/messaging-api-line/debug",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "debug@^4.1.1",
"name": "debug",
"escapedName": "debug",
"rawSpec": "^4.1.1",
"saveSpec": null,
"fetchSpec": "^4.1.1"
},
"_requiredBy": [
"/messaging-api-line"
],
"_resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
"_shasum": "3b72260255109c6b589cee050f1d516139664791",
"_spec": "debug@^4.1.1",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\messaging-api-line",
"author": {
"name": "TJ Holowaychuk",
"email": "tj@vision-media.ca"
},
"browser": "./src/browser.js",
"bugs": {
"url": "https://github.com/visionmedia/debug/issues"
},
"bundleDependencies": false,
"contributors": [
{
"name": "Nathan Rajlich",
"email": "nathan@tootallnate.net",
"url": "http://n8.io"
},
{
"name": "Andrew Rhyne",
"email": "rhyneandrew@gmail.com"
}
],
"dependencies": {
"ms": "^2.1.1"
},
"deprecated": false,
"description": "small debugging utility",
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"browserify": "14.4.0",
"chai": "^3.5.0",
"concurrently": "^3.1.0",
"coveralls": "^3.0.2",
"istanbul": "^0.4.5",
"karma": "^3.0.0",
"karma-chai": "^0.1.0",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.2",
"mocha": "^5.2.0",
"mocha-lcov-reporter": "^1.2.0",
"rimraf": "^2.5.4",
"xo": "^0.23.0"
},
"files": [
"src",
"dist/debug.js",
"LICENSE",
"README.md"
],
"homepage": "https://github.com/visionmedia/debug#readme",
"keywords": [
"debug",
"log",
"debugger"
],
"license": "MIT",
"main": "./src/index.js",
"name": "debug",
"repository": {
"type": "git",
"url": "git://github.com/visionmedia/debug.git"
},
"scripts": {
"build": "npm run build:debug && npm run build:test",
"build:debug": "babel -o dist/debug.js dist/debug.es6.js > dist/debug.js",
"build:test": "babel -d dist test.js",
"clean": "rimraf dist coverage",
"lint": "xo",
"prebuild:debug": "mkdir -p dist && browserify --standalone debug -o dist/debug.es6.js .",
"pretest:browser": "npm run build",
"test": "npm run test:node && npm run test:browser",
"test:browser": "karma start --single-run",
"test:coverage": "cat ./coverage/lcov.info | coveralls",
"test:node": "istanbul cover _mocha -- test.js"
},
"unpkg": "./dist/debug.js",
"version": "4.1.1"
}
/* eslint-env browser */
/**
* This is the web browser implementation of `debug()`.
*/
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.storage = localstorage();
/**
* Colors.
*/
exports.colors = [
'#0000CC',
'#0000FF',
'#0033CC',
'#0033FF',
'#0066CC',
'#0066FF',
'#0099CC',
'#0099FF',
'#00CC00',
'#00CC33',
'#00CC66',
'#00CC99',
'#00CCCC',
'#00CCFF',
'#3300CC',
'#3300FF',
'#3333CC',
'#3333FF',
'#3366CC',
'#3366FF',
'#3399CC',
'#3399FF',
'#33CC00',
'#33CC33',
'#33CC66',
'#33CC99',
'#33CCCC',
'#33CCFF',
'#6600CC',
'#6600FF',
'#6633CC',
'#6633FF',
'#66CC00',
'#66CC33',
'#9900CC',
'#9900FF',
'#9933CC',
'#9933FF',
'#99CC00',
'#99CC33',
'#CC0000',
'#CC0033',
'#CC0066',
'#CC0099',
'#CC00CC',
'#CC00FF',
'#CC3300',
'#CC3333',
'#CC3366',
'#CC3399',
'#CC33CC',
'#CC33FF',
'#CC6600',
'#CC6633',
'#CC9900',
'#CC9933',
'#CCCC00',
'#CCCC33',
'#FF0000',
'#FF0033',
'#FF0066',
'#FF0099',
'#FF00CC',
'#FF00FF',
'#FF3300',
'#FF3333',
'#FF3366',
'#FF3399',
'#FF33CC',
'#FF33FF',
'#FF6600',
'#FF6633',
'#FF9900',
'#FF9933',
'#FFCC00',
'#FFCC33'
];
/**
* Currently only WebKit-based Web Inspectors, Firefox >= v31,
* and the Firebug extension (any Firefox version) are known
* to support "%c" CSS customizations.
*
* TODO: add a `localStorage` variable to explicitly enable/disable colors
*/
// eslint-disable-next-line complexity
function useColors() {
// NB: In an Electron preload script, document will be defined but not fully
// initialized. Since we know we're in Chrome, we'll just detect this case
// explicitly
if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
return true;
}
// Internet Explorer and Edge do not support colors.
if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
return false;
}
// Is webkit? http://stackoverflow.com/a/16459606/376773
// document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
// Is firebug? http://stackoverflow.com/a/398120/376773
(typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
// Is firefox >= v31?
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
(typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
// Double check webkit in userAgent just in case we are in a worker
(typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
}
/**
* Colorize log arguments if enabled.
*
* @api public
*/
function formatArgs(args) {
args[0] = (this.useColors ? '%c' : '') +
this.namespace +
(this.useColors ? ' %c' : ' ') +
args[0] +
(this.useColors ? '%c ' : ' ') +
'+' + module.exports.humanize(this.diff);
if (!this.useColors) {
return;
}
const c = 'color: ' + this.color;
args.splice(1, 0, c, 'color: inherit');
// The final "%c" is somewhat tricky, because there could be other
// arguments passed either before or after the %c, so we need to
// figure out the correct index to insert the CSS into
let index = 0;
let lastC = 0;
args[0].replace(/%[a-zA-Z%]/g, match => {
if (match === '%%') {
return;
}
index++;
if (match === '%c') {
// We only are interested in the *last* %c
// (the user may have provided their own)
lastC = index;
}
});
args.splice(lastC, 0, c);
}
/**
* Invokes `console.log()` when available.
* No-op when `console.log` is not a "function".
*
* @api public
*/
function log(...args) {
// This hackery is required for IE8/9, where
// the `console.log` function doesn't have 'apply'
return typeof console === 'object' &&
console.log &&
console.log(...args);
}
/**
* Save `namespaces`.
*
* @param {String} namespaces
* @api private
*/
function save(namespaces) {
try {
if (namespaces) {
exports.storage.setItem('debug', namespaces);
} else {
exports.storage.removeItem('debug');
}
} catch (error) {
// Swallow
// XXX (@Qix-) should we be logging these?
}
}
/**
* Load `namespaces`.
*
* @return {String} returns the previously persisted debug modes
* @api private
*/
function load() {
let r;
try {
r = exports.storage.getItem('debug');
} catch (error) {
// Swallow
// XXX (@Qix-) should we be logging these?
}
// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
if (!r && typeof process !== 'undefined' && 'env' in process) {
r = process.env.DEBUG;
}
return r;
}
/**
* Localstorage attempts to return the localstorage.
*
* This is necessary because safari throws
* when a user disables cookies/localstorage
* and you attempt to access it.
*
* @return {LocalStorage}
* @api private
*/
function localstorage() {
try {
// TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
// The Browser also has localStorage in the global context.
return localStorage;
} catch (error) {
// Swallow
// XXX (@Qix-) should we be logging these?
}
}
module.exports = require('./common')(exports);
const {formatters} = module.exports;
/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/
formatters.j = function (v) {
try {
return JSON.stringify(v);
} catch (error) {
return '[UnexpectedJSONParseError]: ' + error.message;
}
};
/**
* This is the common logic for both the Node.js and web browser
* implementations of `debug()`.
*/
function setup(env) {
createDebug.debug = createDebug;
createDebug.default = createDebug;
createDebug.coerce = coerce;
createDebug.disable = disable;
createDebug.enable = enable;
createDebug.enabled = enabled;
createDebug.humanize = require('ms');
Object.keys(env).forEach(key => {
createDebug[key] = env[key];
});
/**
* Active `debug` instances.
*/
createDebug.instances = [];
/**
* The currently active debug mode names, and names to skip.
*/
createDebug.names = [];
createDebug.skips = [];
/**
* Map of special "%n" handling functions, for the debug "format" argument.
*
* Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
*/
createDebug.formatters = {};
/**
* Selects a color for a debug namespace
* @param {String} namespace The namespace string for the for the debug instance to be colored
* @return {Number|String} An ANSI color code for the given namespace
* @api private
*/
function selectColor(namespace) {
let hash = 0;
for (let i = 0; i < namespace.length; i++) {
hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
}
createDebug.selectColor = selectColor;
/**
* Create a debugger with the given `namespace`.
*
* @param {String} namespace
* @return {Function}
* @api public
*/
function createDebug(namespace) {
let prevTime;
function debug(...args) {
// Disabled?
if (!debug.enabled) {
return;
}
const self = debug;
// Set `diff` timestamp
const curr = Number(new Date());
const ms = curr - (prevTime || curr);
self.diff = ms;
self.prev = prevTime;
self.curr = curr;
prevTime = curr;
args[0] = createDebug.coerce(args[0]);
if (typeof args[0] !== 'string') {
// Anything else let's inspect with %O
args.unshift('%O');
}
// Apply any `formatters` transformations
let index = 0;
args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
// If we encounter an escaped % then don't increase the array index
if (match === '%%') {
return match;
}
index++;
const formatter = createDebug.formatters[format];
if (typeof formatter === 'function') {
const val = args[index];
match = formatter.call(self, val);
// Now we need to remove `args[index]` since it's inlined in the `format`
args.splice(index, 1);
index--;
}
return match;
});
// Apply env-specific formatting (colors, etc.)
createDebug.formatArgs.call(self, args);
const logFn = self.log || createDebug.log;
logFn.apply(self, args);
}
debug.namespace = namespace;
debug.enabled = createDebug.enabled(namespace);
debug.useColors = createDebug.useColors();
debug.color = selectColor(namespace);
debug.destroy = destroy;
debug.extend = extend;
// Debug.formatArgs = formatArgs;
// debug.rawLog = rawLog;
// env-specific initialization logic for debug instances
if (typeof createDebug.init === 'function') {
createDebug.init(debug);
}
createDebug.instances.push(debug);
return debug;
}
function destroy() {
const index = createDebug.instances.indexOf(this);
if (index !== -1) {
createDebug.instances.splice(index, 1);
return true;
}
return false;
}
function extend(namespace, delimiter) {
const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
newDebug.log = this.log;
return newDebug;
}
/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
*
* @param {String} namespaces
* @api public
*/
function enable(namespaces) {
createDebug.save(namespaces);
createDebug.names = [];
createDebug.skips = [];
let i;
const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
const len = split.length;
for (i = 0; i < len; i++) {
if (!split[i]) {
// ignore empty strings
continue;
}
namespaces = split[i].replace(/\*/g, '.*?');
if (namespaces[0] === '-') {
createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
} else {
createDebug.names.push(new RegExp('^' + namespaces + '$'));
}
}
for (i = 0; i < createDebug.instances.length; i++) {
const instance = createDebug.instances[i];
instance.enabled = createDebug.enabled(instance.namespace);
}
}
/**
* Disable debug output.
*
* @return {String} namespaces
* @api public
*/
function disable() {
const namespaces = [
...createDebug.names.map(toNamespace),
...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace)
].join(',');
createDebug.enable('');
return namespaces;
}
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
function enabled(name) {
if (name[name.length - 1] === '*') {
return true;
}
let i;
let len;
for (i = 0, len = createDebug.skips.length; i < len; i++) {
if (createDebug.skips[i].test(name)) {
return false;
}
}
for (i = 0, len = createDebug.names.length; i < len; i++) {
if (createDebug.names[i].test(name)) {
return true;
}
}
return false;
}
/**
* Convert regexp to namespace
*
* @param {RegExp} regxep
* @return {String} namespace
* @api private
*/
function toNamespace(regexp) {
return regexp.toString()
.substring(2, regexp.toString().length - 2)
.replace(/\.\*\?$/, '*');
}
/**
* Coerce `val`.
*
* @param {Mixed} val
* @return {Mixed}
* @api private
*/
function coerce(val) {
if (val instanceof Error) {
return val.stack || val.message;
}
return val;
}
createDebug.enable(createDebug.load());
return createDebug;
}
module.exports = setup;
/**
* Detect Electron renderer / nwjs process, which is node, but we should
* treat as a browser.
*/
if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
module.exports = require('./browser.js');
} else {
module.exports = require('./node.js');
}
/**
* Module dependencies.
*/
const tty = require('tty');
const util = require('util');
/**
* This is the Node.js implementation of `debug()`.
*/
exports.init = init;
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
/**
* Colors.
*/
exports.colors = [6, 2, 3, 4, 5, 1];
try {
// Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
// eslint-disable-next-line import/no-extraneous-dependencies
const supportsColor = require('supports-color');
if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
exports.colors = [
20,
21,
26,
27,
32,
33,
38,
39,
40,
41,
42,
43,
44,
45,
56,
57,
62,
63,
68,
69,
74,
75,
76,
77,
78,
79,
80,
81,
92,
93,
98,
99,
112,
113,
128,
129,
134,
135,
148,
149,
160,
161,
162,
163,
164,
165,
166,
167,
168,
169,
170,
171,
172,
173,
178,
179,
184,
185,
196,
197,
198,
199,
200,
201,
202,
203,
204,
205,
206,
207,
208,
209,
214,
215,
220,
221
];
}
} catch (error) {
// Swallow - we only care if `supports-color` is available; it doesn't have to be.
}
/**
* Build up the default `inspectOpts` object from the environment variables.
*
* $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
*/
exports.inspectOpts = Object.keys(process.env).filter(key => {
return /^debug_/i.test(key);
}).reduce((obj, key) => {
// Camel-case
const prop = key
.substring(6)
.toLowerCase()
.replace(/_([a-z])/g, (_, k) => {
return k.toUpperCase();
});
// Coerce string value into JS value
let val = process.env[key];
if (/^(yes|on|true|enabled)$/i.test(val)) {
val = true;
} else if (/^(no|off|false|disabled)$/i.test(val)) {
val = false;
} else if (val === 'null') {
val = null;
} else {
val = Number(val);
}
obj[prop] = val;
return obj;
}, {});
/**
* Is stdout a TTY? Colored output is enabled when `true`.
*/
function useColors() {
return 'colors' in exports.inspectOpts ?
Boolean(exports.inspectOpts.colors) :
tty.isatty(process.stderr.fd);
}
/**
* Adds ANSI color escape codes if enabled.
*
* @api public
*/
function formatArgs(args) {
const {namespace: name, useColors} = this;
if (useColors) {
const c = this.color;
const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
const prefix = ` ${colorCode};1m${name} \u001B[0m`;
args[0] = prefix + args[0].split('\n').join('\n' + prefix);
args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m');
} else {
args[0] = getDate() + name + ' ' + args[0];
}
}
function getDate() {
if (exports.inspectOpts.hideDate) {
return '';
}
return new Date().toISOString() + ' ';
}
/**
* Invokes `util.format()` with the specified arguments and writes to stderr.
*/
function log(...args) {
return process.stderr.write(util.format(...args) + '\n');
}
/**
* Save `namespaces`.
*
* @param {String} namespaces
* @api private
*/
function save(namespaces) {
if (namespaces) {
process.env.DEBUG = namespaces;
} else {
// If you set a process.env field to null or undefined, it gets cast to the
// string 'null' or 'undefined'. Just delete instead.
delete process.env.DEBUG;
}
}
/**
* Load `namespaces`.
*
* @return {String} returns the previously persisted debug modes
* @api private
*/
function load() {
return process.env.DEBUG;
}
/**
* Init logic for `debug` instances.
*
* Create a new `inspectOpts` object in case `useColors` is set
* differently for a particular `debug` instance.
*/
function init(debug) {
debug.inspectOpts = {};
const keys = Object.keys(exports.inspectOpts);
for (let i = 0; i < keys.length; i++) {
debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
}
}
module.exports = require('./common')(exports);
const {formatters} = module.exports;
/**
* Map %o to `util.inspect()`, all on a single line.
*/
formatters.o = function (v) {
this.inspectOpts.colors = this.useColors;
return util.inspect(v, this.inspectOpts)
.replace(/\s*\n\s*/g, ' ');
};
/**
* Map %O to `util.inspect()`, allowing multiple lines if needed.
*/
formatters.O = function (v) {
this.inspectOpts.colors = this.useColors;
return util.inspect(v, this.inspectOpts);
};
/**
* Helpers.
*/
var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var w = d * 7;
var y = d * 365.25;
/**
* Parse or format the given `val`.
*
* Options:
*
* - `long` verbose formatting [false]
*
* @param {String|Number} val
* @param {Object} [options]
* @throws {Error} throw an error if val is not a non-empty string or a number
* @return {String|Number}
* @api public
*/
module.exports = function(val, options) {
options = options || {};
var type = typeof val;
if (type === 'string' && val.length > 0) {
return parse(val);
} else if (type === 'number' && isFinite(val)) {
return options.long ? fmtLong(val) : fmtShort(val);
}
throw new Error(
'val is not a non-empty string or a valid number. val=' +
JSON.stringify(val)
);
};
/**
* Parse the given `str` and return milliseconds.
*
* @param {String} str
* @return {Number}
* @api private
*/
function parse(str) {
str = String(str);
if (str.length > 100) {
return;
}
var match = /^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
str
);
if (!match) {
return;
}
var n = parseFloat(match[1]);
var type = (match[2] || 'ms').toLowerCase();
switch (type) {
case 'years':
case 'year':
case 'yrs':
case 'yr':
case 'y':
return n * y;
case 'weeks':
case 'week':
case 'w':
return n * w;
case 'days':
case 'day':
case 'd':
return n * d;
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
return n * h;
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
return n * m;
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
return n * s;
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'ms':
return n;
default:
return undefined;
}
}
/**
* Short format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function fmtShort(ms) {
var msAbs = Math.abs(ms);
if (msAbs >= d) {
return Math.round(ms / d) + 'd';
}
if (msAbs >= h) {
return Math.round(ms / h) + 'h';
}
if (msAbs >= m) {
return Math.round(ms / m) + 'm';
}
if (msAbs >= s) {
return Math.round(ms / s) + 's';
}
return ms + 'ms';
}
/**
* Long format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function fmtLong(ms) {
var msAbs = Math.abs(ms);
if (msAbs >= d) {
return plural(ms, msAbs, d, 'day');
}
if (msAbs >= h) {
return plural(ms, msAbs, h, 'hour');
}
if (msAbs >= m) {
return plural(ms, msAbs, m, 'minute');
}
if (msAbs >= s) {
return plural(ms, msAbs, s, 'second');
}
return ms + ' ms';
}
/**
* Pluralization helper.
*/
function plural(ms, msAbs, n, name) {
var isPlural = msAbs >= n * 1.5;
return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
}
The MIT License (MIT)
Copyright (c) 2016 Zeit, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
{
"_from": "ms@^2.1.1",
"_id": "ms@2.1.2",
"_inBundle": false,
"_integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"_location": "/messaging-api-line/ms",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "ms@^2.1.1",
"name": "ms",
"escapedName": "ms",
"rawSpec": "^2.1.1",
"saveSpec": null,
"fetchSpec": "^2.1.1"
},
"_requiredBy": [
"/messaging-api-line/debug"
],
"_resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"_shasum": "d09d1f357b443f493382a8eb3ccd183872ae6009",
"_spec": "ms@^2.1.1",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\messaging-api-line\\node_modules\\debug",
"bugs": {
"url": "https://github.com/zeit/ms/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Tiny millisecond conversion utility",
"devDependencies": {
"eslint": "4.12.1",
"expect.js": "0.3.1",
"husky": "0.14.3",
"lint-staged": "5.0.0",
"mocha": "4.0.1"
},
"eslintConfig": {
"extends": "eslint:recommended",
"env": {
"node": true,
"es6": true
}
},
"files": [
"index.js"
],
"homepage": "https://github.com/zeit/ms#readme",
"license": "MIT",
"lint-staged": {
"*.js": [
"npm run lint",
"prettier --single-quote --write",
"git add"
]
},
"main": "./index",
"name": "ms",
"repository": {
"type": "git",
"url": "git+https://github.com/zeit/ms.git"
},
"scripts": {
"lint": "eslint lib/* bin/*",
"precommit": "lint-staged",
"test": "mocha tests.js"
},
"version": "2.1.2"
}
# ms
[![Build Status](https://travis-ci.org/zeit/ms.svg?branch=master)](https://travis-ci.org/zeit/ms)
[![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/zeit)
Use this package to easily convert various time formats to milliseconds.
## Examples
```js
ms('2 days') // 172800000
ms('1d') // 86400000
ms('10h') // 36000000
ms('2.5 hrs') // 9000000
ms('2h') // 7200000
ms('1m') // 60000
ms('5s') // 5000
ms('1y') // 31557600000
ms('100') // 100
ms('-3 days') // -259200000
ms('-1h') // -3600000
ms('-200') // -200
```
### Convert from Milliseconds
```js
ms(60000) // "1m"
ms(2 * 60000) // "2m"
ms(-3 * 60000) // "-3m"
ms(ms('10 hours')) // "10h"
```
### Time Format Written-Out
```js
ms(60000, { long: true }) // "1 minute"
ms(2 * 60000, { long: true }) // "2 minutes"
ms(-3 * 60000, { long: true }) // "-3 minutes"
ms(ms('10 hours'), { long: true }) // "10 hours"
```
## Features
- Works both in [Node.js](https://nodejs.org) and in the browser
- If a number is supplied to `ms`, a string with a unit is returned
- If a string that contains the number is supplied, it returns it as a number (e.g.: it returns `100` for `'100'`)
- If you pass a string with a number and a valid unit, the number of equivalent milliseconds is returned
## Related Packages
- [ms.macro](https://github.com/knpwrs/ms.macro) - Run `ms` as a macro at build-time.
## Caught a Bug?
1. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device
2. Link the package to the global module directory: `npm link`
3. Within the module you want to test your local development instance of ms, just link it to the dependencies: `npm link ms`. Instead of the default one from npm, Node.js will now use your clone of ms!
As always, you can run the tests using: `npm test`
{
"_from": "messaging-api-line",
"_id": "messaging-api-line@0.8.3",
"_inBundle": false,
"_integrity": "sha512-EQRqW9UMtfQPw/PfnkFue7eSXZlxX2Ke5frFGhl6UHUW5Qfi4KPpvAqf2VTaU/cU7rGhGnZWuNbjoxkCWDyT8A==",
"_location": "/messaging-api-line",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "messaging-api-line",
"name": "messaging-api-line",
"escapedName": "messaging-api-line",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/messaging-api-line/-/messaging-api-line-0.8.3.tgz",
"_shasum": "53344ba1e88bc91da5c03df5618e32a395e176e8",
"_spec": "messaging-api-line",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT",
"browser": "lib/browser.js",
"bugs": {
"url": "https://github.com/Yoctol/messaging-apis/issues"
},
"bundleDependencies": false,
"dependencies": {
"axios": "^0.19.0",
"axios-error": "^0.8.1",
"debug": "^4.1.1",
"image-type": "^4.1.0",
"invariant": "^2.2.4",
"lodash.omit": "^4.5.0",
"url-join": "^4.0.1"
},
"deprecated": false,
"description": "Messaging API client for LINE",
"devDependencies": {
"axios-mock-adapter": "^1.17.0"
},
"engines": {
"node": ">=8"
},
"gitHead": "874202b421e7b302d84d9b039fbfc30b39a77ee3",
"homepage": "https://github.com/Yoctol/messaging-apis#readme",
"keywords": [
"bot",
"chatbot",
"line",
"messaging-apis"
],
"license": "MIT",
"main": "lib/index.js",
"name": "messaging-api-line",
"repository": {
"type": "git",
"url": "git+https://github.com/Yoctol/messaging-apis.git"
},
"version": "0.8.3"
}
/* @flow */
import invariant from 'invariant';
import omit from 'lodash.omit';
import {
type AudioMessage,
type ButtonsTemplate,
type CarouselTemplate,
type ColumnObject,
type ConfirmTemplate,
type FlexContainer,
type FlexMessage,
type ImageCarouselColumnObject,
type ImageCarouselTemplate,
type ImageMapAction,
type ImageMapMessage,
type ImageMapVideo,
type ImageMessage,
type Location,
type LocationMessage,
type MessageOptions,
type QuickReply,
type StickerMessage,
type Template,
type TemplateAction,
type TemplateMessage,
type TextMessage,
type VideoMessage,
} from './LineTypes';
function createText(text: string, options: MessageOptions = {}): TextMessage {
return {
type: 'text',
text,
...omit(options, 'accessToken'),
};
}
function createImage(
contentUrlOrImage: string | Object,
previewUrlOrOptions?: string | MessageOptions
): ImageMessage {
if (previewUrlOrOptions) {
if (
typeof contentUrlOrImage === 'object' &&
typeof previewUrlOrOptions === 'object'
) {
const image = contentUrlOrImage;
const options = previewUrlOrOptions;
return {
type: 'image',
originalContentUrl: image.originalContentUrl,
previewImageUrl: image.previewImageUrl || image.originalContentUrl,
...omit(options, 'accessToken'),
};
}
if (
typeof contentUrlOrImage === 'string' &&
typeof previewUrlOrOptions === 'string'
) {
return {
type: 'image',
originalContentUrl: contentUrlOrImage,
previewImageUrl: previewUrlOrOptions,
};
}
} else {
if (typeof contentUrlOrImage === 'object') {
const image = contentUrlOrImage;
return {
type: 'image',
originalContentUrl: image.originalContentUrl,
previewImageUrl: image.previewImageUrl || image.originalContentUrl,
};
}
if (typeof contentUrlOrImage === 'string') {
return {
type: 'image',
originalContentUrl: contentUrlOrImage,
previewImageUrl: contentUrlOrImage,
};
}
}
invariant(false, 'Line#createImage: Wrong type of arguments.');
}
function createVideo(
contentUrlOrVideo: string | Object,
previewImageUrlOrOptions?: string | MessageOptions
): VideoMessage {
if (
typeof contentUrlOrVideo === 'string' &&
typeof previewImageUrlOrOptions === 'string'
) {
return {
type: 'video',
originalContentUrl: contentUrlOrVideo,
previewImageUrl: previewImageUrlOrOptions,
};
}
if (
typeof contentUrlOrVideo === 'object' &&
(!previewImageUrlOrOptions || typeof previewImageUrlOrOptions === 'object')
) {
const video = contentUrlOrVideo;
const options = previewImageUrlOrOptions || {};
return {
type: 'video',
originalContentUrl: video.originalContentUrl,
previewImageUrl: video.previewImageUrl,
...omit(options, 'accessToken'),
};
}
invariant(false, 'Line#createVideo: Wrong type of arguments.');
}
function createAudio(
contentUrlOrAudio: string | Object,
durationOrOptions: number | MessageOptions
): AudioMessage {
if (
typeof contentUrlOrAudio === 'string' &&
typeof durationOrOptions === 'number'
) {
return {
type: 'audio',
originalContentUrl: contentUrlOrAudio,
duration: durationOrOptions,
};
}
if (
typeof contentUrlOrAudio === 'object' &&
(!durationOrOptions || typeof durationOrOptions === 'object')
) {
const audio = contentUrlOrAudio;
const options = durationOrOptions || {};
return {
type: 'audio',
originalContentUrl: audio.originalContentUrl,
duration: audio.duration,
...omit(options, 'accessToken'),
};
}
invariant(false, 'Line#createAudio: Wrong type of arguments.');
}
function createLocation(
{ title, address, latitude, longitude }: Location,
options?: MessageOptions = {}
): LocationMessage {
return {
type: 'location',
title,
address,
latitude,
longitude,
...omit(options, 'accessToken'),
};
}
function createSticker(
packageIdOrSticker: string | Object,
stickerIdOrOptions: string | MessageOptions
): StickerMessage {
if (
typeof packageIdOrSticker === 'string' &&
typeof stickerIdOrOptions === 'string'
) {
return {
type: 'sticker',
packageId: packageIdOrSticker,
stickerId: stickerIdOrOptions,
};
}
if (
typeof packageIdOrSticker === 'object' &&
(!stickerIdOrOptions || typeof stickerIdOrOptions === 'object')
) {
const sticker = packageIdOrSticker;
const options = stickerIdOrOptions || {};
return {
type: 'sticker',
packageId: sticker.packageId,
stickerId: sticker.stickerId,
...omit(options, 'accessToken'),
};
}
invariant(false, 'Line#createSticker: Wrong type of arguments.');
}
function createImagemap(
altText: string,
{
baseUrl,
baseSize,
baseHeight,
baseWidth,
video,
actions,
}: {
baseUrl: string,
baseSize: {
height: number,
width: number,
},
baseHeight: number,
baseWidth: number,
video?: ImageMapVideo,
actions: Array<ImageMapAction>,
},
options?: MessageOptions = {}
): ImageMapMessage {
return {
type: 'imagemap',
baseUrl,
altText,
baseSize: baseSize || {
height: baseHeight,
width: baseWidth,
},
video,
actions,
...omit(options, 'accessToken'),
};
}
function createTemplate(
altText: string,
template: Template,
options?: MessageOptions = {}
): TemplateMessage<any> {
return {
type: 'template',
altText,
template,
...omit(options, 'accessToken'),
};
}
function createButtonTemplate(
altText: string,
{
thumbnailImageUrl,
imageAspectRatio,
imageSize,
imageBackgroundColor,
title,
text,
defaultAction,
actions,
}: {
thumbnailImageUrl?: string,
imageAspectRatio?: 'rectangle' | 'square',
imageSize?: 'cover' | 'contain',
imageBackgroundColor?: string,
title?: string,
text: string,
defaultAction?: TemplateAction,
actions: Array<TemplateAction>,
},
options: MessageOptions = {}
): TemplateMessage<ButtonsTemplate> {
return createTemplate(
altText,
{
type: 'buttons',
thumbnailImageUrl,
imageAspectRatio,
imageSize,
imageBackgroundColor,
title,
text,
defaultAction,
actions,
},
omit(options, 'accessToken')
);
}
function createConfirmTemplate(
altText: string,
{
text,
actions,
}: {
text: string,
actions: Array<TemplateAction>,
},
options?: MessageOptions = {}
): TemplateMessage<ConfirmTemplate> {
return createTemplate(
altText,
{
type: 'confirm',
text,
actions,
},
omit(options, 'accessToken')
);
}
function createCarouselTemplate(
altText: string,
columns: Array<ColumnObject>,
{
imageAspectRatio,
imageSize,
quickReply,
}: {
imageAspectRatio?: 'rectangle' | 'square',
imageSize?: 'cover' | 'contain',
quickReply?: QuickReply,
} = {}
): TemplateMessage<CarouselTemplate> {
return createTemplate(
altText,
{
type: 'carousel',
columns,
imageAspectRatio,
imageSize,
},
{ quickReply }
);
}
function createImageCarouselTemplate(
altText: string,
columns: Array<ImageCarouselColumnObject>,
options?: MessageOptions = {}
): TemplateMessage<ImageCarouselTemplate> {
return createTemplate(
altText,
{
type: 'image_carousel',
columns,
},
omit(options, 'accessToken')
);
}
function createFlex(
altText: string,
contents: FlexContainer,
options?: MessageOptions = {}
): FlexMessage {
return {
type: 'flex',
altText,
contents,
...omit(options, 'accessToken'),
};
}
const Line = {
createText,
createImage,
createVideo,
createAudio,
createLocation,
createSticker,
createImagemap,
createTemplate,
createButtonsTemplate: createButtonTemplate,
createButtonTemplate,
createConfirmTemplate,
createCarouselTemplate,
createImageCarouselTemplate,
createFlex,
};
export default Line;
/* @flow */
import AxiosError from 'axios-error';
import axios from 'axios';
import debug from 'debug';
import imageType from 'image-type';
import invariant from 'invariant';
import omit from 'lodash.omit';
import urlJoin from 'url-join';
import Line from './Line';
import {
type ColumnObject,
type FlexContainer,
type ImageCarouselColumnObject,
type ImageMapAction,
type ImageMapVideo,
type LiffView,
type Location,
type Message,
type MessageOptions,
type MutationSuccessResponse,
type ReplyToken,
type RichMenu,
type SendTarget,
type SendType,
type Template,
type TemplateAction,
type User,
type UserId,
} from './LineTypes';
type Axios = {
get: Function,
post: Function,
put: Function,
path: Function,
delete: Function,
};
type ClientConfig = {
accessToken: string,
channelSecret: string,
origin?: string,
onRequest?: Function,
};
function handleError(err) {
if (err.response && err.response.data) {
const { message, details } = err.response.data;
let msg = `LINE API - ${message}`;
if (details && details.length > 0) {
details.forEach(detail => {
msg += `\n- ${detail.property}: ${detail.message}`;
});
}
throw new AxiosError(msg, err);
}
throw new AxiosError(err.message, err);
}
const debugRequest = debug('messaging-api-line');
function onRequest({ method, url, body }) {
debugRequest(`${method} ${url}`);
if (body) {
debugRequest('Outgoing request body:');
debugRequest(JSON.stringify(body, null, 2));
}
}
export default class LineClient {
static connect(
accessTokenOrConfig: string | ClientConfig,
channelSecret: string
): LineClient {
return new LineClient(accessTokenOrConfig, channelSecret);
}
_accessToken: string;
_channelSecret: string;
_onRequest: Function;
_axios: Axios;
constructor(
accessTokenOrConfig: string | ClientConfig,
channelSecret: string
) {
let origin;
if (accessTokenOrConfig && typeof accessTokenOrConfig === 'object') {
const config = accessTokenOrConfig;
this._accessToken = config.accessToken;
this._channelSecret = config.channelSecret;
this._onRequest = config.onRequest || onRequest;
origin = config.origin;
} else {
this._accessToken = accessTokenOrConfig;
this._channelSecret = channelSecret;
this._onRequest = onRequest;
}
this._axios = axios.create({
baseURL: `${origin || 'https://api.line.me'}/`,
headers: {
Authorization: `Bearer ${this._accessToken}`,
'Content-Type': 'application/json',
},
});
this._axios.interceptors.request.use(config => {
this._onRequest({
method: config.method,
url: urlJoin(config.baseURL, config.url),
headers: {
...config.headers.common,
...config.headers[config.method],
...omit(config.headers, [
'common',
'get',
'post',
'put',
'patch',
'delete',
'head',
]),
},
body: config.data,
});
return config;
});
}
get axios(): Axios {
return this._axios;
}
get accessToken(): string {
return this._accessToken;
}
_send(
type: SendType,
target: SendTarget,
...args: Array<any>
): Promise<MutationSuccessResponse> {
if (type === 'push') {
return this.push(((target: any): UserId), ...args);
}
if (type === 'multicast') {
return this.multicast(((target: any): Array<UserId>), ...args);
}
return this.reply(((target: any): ReplyToken), ...args);
}
_sendText(
type: SendType,
target: SendTarget,
text: string,
options?: MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[Line.createText(text, options || {})],
options
);
}
_sendImage(
type: SendType,
target: SendTarget,
contentUrlOrImage: string | Object,
previewUrlOrOptions?: string | MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[Line.createImage(contentUrlOrImage, previewUrlOrOptions)],
typeof previewUrlOrOptions === 'string' ? undefined : previewUrlOrOptions
);
}
_sendVideo(
type: SendType,
target: SendTarget,
contentUrlOrVideo: string | Object,
previewUrlOrOptions?: string | MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[Line.createVideo(contentUrlOrVideo, previewUrlOrOptions || {})],
typeof previewUrlOrOptions === 'string' ? undefined : previewUrlOrOptions
);
}
_sendAudio(
type: SendType,
target: SendTarget,
contentUrlOrAudio: string | Object,
durationOrOptions?: number | MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[Line.createAudio(contentUrlOrAudio, durationOrOptions || {})],
typeof durationOrOptions === 'number' ? undefined : durationOrOptions
);
}
_sendLocation(
type: SendType,
target: SendTarget,
{ title, address, latitude, longitude }: Location,
options?: MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[
Line.createLocation(
{
title,
address,
latitude,
longitude,
},
options || {}
),
],
options
);
}
_sendSticker(
type: SendType,
target: SendTarget,
packageIdOrSticker: string | Object,
stickerIdOrOptions?: string | MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[Line.createSticker(packageIdOrSticker, stickerIdOrOptions || {})],
typeof stickerIdOrOptions === 'string' ? undefined : stickerIdOrOptions
);
}
/**
* Imagemap Message
*
* https://developers.line.me/en/docs/messaging-api/reference/#imagemap-message
*/
_sendImagemap(
type: SendType,
target: SendTarget,
altText: string,
{
baseUrl,
baseSize,
baseHeight,
baseWidth,
video,
actions,
}: {
baseUrl: string,
baseSize: {
height: number,
width: number,
},
baseHeight: number,
baseWidth: number,
video?: ImageMapVideo,
actions: Array<ImageMapAction>,
},
options?: MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[
Line.createImagemap(
altText,
{
baseUrl,
baseSize,
baseHeight,
baseWidth,
video,
actions,
},
options || {}
),
],
options
);
}
/**
* Flex Message
*
* https://developers.line.me/en/docs/messaging-api/reference/#flex-message
*/
_sendFlex(
type: SendType,
target: SendTarget,
altText: string,
contents: FlexContainer,
options?: MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[Line.createFlex(altText, contents, options || {})],
options
);
}
/**
* Template Messages
*
* https://developers.line.me/en/docs/messaging-api/reference/#template-messages
*/
_sendTemplate(
type: SendType,
target: SendTarget,
altText: string,
template: Template,
options?: MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[Line.createTemplate(altText, template, options || {})],
options
);
}
_sendButtonTemplate(
type: SendType,
target: SendTarget,
altText: string,
{
thumbnailImageUrl,
imageAspectRatio,
imageSize,
imageBackgroundColor,
title,
text,
defaultAction,
actions,
}: {
thumbnailImageUrl?: string,
imageAspectRatio?: 'rectangle' | 'square',
imageSize?: 'cover' | 'contain',
imageBackgroundColor?: string,
title?: string,
text: string,
defaultAction?: TemplateAction,
actions: Array<TemplateAction>,
},
options?: MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[
Line.createButtonTemplate(
altText,
{
thumbnailImageUrl,
imageAspectRatio,
imageSize,
imageBackgroundColor,
title,
text,
defaultAction,
actions,
},
options || {}
),
],
options
);
}
_sendConfirmTemplate(
type: SendType,
target: SendTarget,
altText: string,
{
text,
actions,
}: {
text: string,
actions: Array<TemplateAction>,
},
options?: MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[
Line.createConfirmTemplate(
altText,
{
text,
actions,
},
options || {}
),
],
options
);
}
_sendCarouselTemplate(
type: SendType,
target: SendTarget,
altText: string,
columns: Array<ColumnObject>,
{
imageAspectRatio,
imageSize,
...options
}: {
imageAspectRatio?: 'rectangle' | 'square',
imageSize?: 'cover' | 'contain',
options?: MessageOptions,
} = {}
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[
Line.createCarouselTemplate(altText, columns, {
imageAspectRatio,
imageSize,
...options,
}),
],
options
);
}
_sendImageCarouselTemplate(
type: SendType,
target: SendTarget,
altText: string,
columns: Array<ImageCarouselColumnObject>,
options?: MessageOptions
): Promise<MutationSuccessResponse> {
return this._send(
type,
target,
[Line.createImageCarouselTemplate(altText, columns, options || {})],
options
);
}
/**
* Reply Message
*
* https://developers.line.me/en/docs/messaging-api/reference/#send-reply-message
*/
replyRawBody(
body: {
replyToken: ReplyToken,
messages: Array<Message>,
},
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<MutationSuccessResponse> {
return this._axios
.post(
'/v2/bot/message/reply',
body,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
reply(
replyToken: ReplyToken,
messages: Array<Message>,
options?: Object = {}
): Promise<MutationSuccessResponse> {
return this.replyRawBody({ replyToken, messages }, options);
}
/**
* Push Message
*
* https://developers.line.me/en/docs/messaging-api/reference/#send-push-message
*/
pushRawBody(
body: {
to: string,
messages: Array<Message>,
},
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<MutationSuccessResponse> {
return this._axios
.post(
'/v2/bot/message/push',
body,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
push(
to: string,
messages: Array<Message>,
options?: Object = {}
): Promise<MutationSuccessResponse> {
return this.pushRawBody({ to, messages }, options);
}
/**
* Multicast
*
* https://developers.line.me/en/docs/messaging-api/reference/#send-multicast-messages
*/
multicastRawBody(
body: {
to: Array<UserId>,
messages: Array<Message>,
},
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<MutationSuccessResponse> {
return this._axios
.post(
'/v2/bot/message/multicast',
body,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
multicast(
to: Array<UserId>,
messages: Array<Message>,
options?: Object = {}
): Promise<MutationSuccessResponse> {
return this.multicastRawBody({ to, messages }, options);
}
/**
* Content
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-content
*/
retrieveMessageContent(
messageId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<Buffer> {
return this._axios
.get(`/v2/bot/message/${messageId}/content`, {
responseType: 'arraybuffer',
...(customAccessToken
? { headers: { Authorization: `Bearer ${customAccessToken}` } }
: undefined),
})
.then(res => res.data, handleError);
}
/**
* Get User Profile
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-profile
* displayName, userId, pictureUrl, statusMessage
*/
getUserProfile(
userId: UserId,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<User> {
return this._axios
.get(
`/v2/bot/profile/${userId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError)
.catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
/**
* Get Group Member Profile
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-group-member-profile
*/
getGroupMemberProfile(
groupId: string,
userId: UserId,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.get(
`/v2/bot/group/${groupId}/member/${userId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
/**
* Get Room Member Profile
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-room-member-profile
*/
getRoomMemberProfile(
roomId: string,
userId: UserId,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.get(
`/v2/bot/room/${roomId}/member/${userId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
/**
* Get Group Member IDs
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-group-member-user-ids
*/
getGroupMemberIds(
groupId: string,
start?: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<{ memberIds: Array<string>, next?: ?string }> {
return this._axios
.get(
`/v2/bot/group/${groupId}/members/ids${start ? `?start=${start}` : ''}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
async getAllGroupMemberIds(
groupId: string,
options?: Object = {}
): Promise<Array<string>> {
let allMemberIds: Array<string> = [];
let continuationToken;
do {
// eslint-disable-next-line no-await-in-loop
const { memberIds, next } = await this.getGroupMemberIds(
groupId,
continuationToken,
options
);
allMemberIds = allMemberIds.concat(memberIds);
continuationToken = next;
} while (continuationToken);
return allMemberIds;
}
/**
* Get Room Member IDs
*
* https://developers.line.me/en/docs/messaging-api/reference/#get-room-member-user-ids
*/
getRoomMemberIds(
roomId: string,
start?: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<{ memberIds: Array<string>, next?: ?string }> {
return this._axios
.get(
`/v2/bot/room/${roomId}/members/ids${start ? `?start=${start}` : ''}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
async getAllRoomMemberIds(
roomId: string,
options?: Object = {}
): Promise<Array<string>> {
let allMemberIds: Array<string> = [];
let continuationToken;
do {
// eslint-disable-next-line no-await-in-loop
const { memberIds, next } = await this.getRoomMemberIds(
roomId,
continuationToken,
options
);
allMemberIds = allMemberIds.concat(memberIds);
continuationToken = next;
} while (continuationToken);
return allMemberIds;
}
/**
* Leave Group
*
* https://developers.line.me/en/docs/messaging-api/reference/#leave-group
*/
leaveGroup(
groupId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<MutationSuccessResponse> {
return this._axios
.post(
`/v2/bot/group/${groupId}/leave`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
/**
* Leave Room
*
* https://developers.line.me/en/docs/messaging-api/reference/#leave-room
*/
leaveRoom(
roomId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<MutationSuccessResponse> {
return this._axios
.post(
`/v2/bot/room/${roomId}/leave`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
/**
* Rich Menu
*
* https://developers.line.me/en/docs/messaging-api/reference/#rich-menu
*/
getRichMenuList({
accessToken: customAccessToken,
}: { accessToken?: string } = {}) {
return this._axios
.get(
'/v2/bot/richmenu/list',
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data.richmenus, handleError);
}
getRichMenu(
richMenuId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.get(
`/v2/bot/richmenu/${richMenuId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data)
.catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
createRichMenu(
richMenu: RichMenu,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.post(
'/v2/bot/richmenu',
richMenu,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
deleteRichMenu(
richMenuId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.delete(
`/v2/bot/richmenu/${richMenuId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
getLinkedRichMenu(
userId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.get(
`/v2/bot/user/${userId}/richmenu`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data)
.catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
linkRichMenu(
userId: string,
richMenuId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.post(
`/v2/bot/user/${userId}/richmenu/${richMenuId}`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
unlinkRichMenu(
userId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.delete(
`/v2/bot/user/${userId}/richmenu`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
getDefaultRichMenu({
accessToken: customAccessToken,
}: { accessToken?: string } = {}) {
return this._axios
.get(
`/v2/bot/user/all/richmenu`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data)
.catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
setDefaultRichMenu(
richMenuId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.post(
`/v2/bot/user/all/richmenu/${richMenuId}`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
deleteDefaultRichMenu({
accessToken: customAccessToken,
}: { accessToken?: string } = {}) {
return this._axios
.delete(
`/v2/bot/user/all/richmenu`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
/**
* - Images must have one of the following resolutions: 2500x1686, 2500x843.
* - You cannot replace an image attached to a rich menu.
* To update your rich menu image, create a new rich menu object and upload another image.
*/
uploadRichMenuImage(
richMenuId: string,
image: Buffer,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
const type = imageType(image);
invariant(
type && (type.mime === 'image/jpeg' || type.mime === 'image/png'),
'Image must be `image/jpeg` or `image/png`'
);
return this._axios
.post(`/v2/bot/richmenu/${richMenuId}/content`, image, {
headers: customAccessToken
? {
'Content-Type': type.mime,
Authorization: `Bearer ${customAccessToken}`,
}
: {
'Content-Type': type.mime,
},
})
.then(res => res.data, handleError);
}
downloadRichMenuImage(
richMenuId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
) {
return this._axios
.get(
`/v2/bot/richmenu/${richMenuId}/content`,
customAccessToken
? {
responseType: 'arraybuffer',
headers: {
Authorization: `Bearer ${customAccessToken}`,
},
}
: {
responseType: 'arraybuffer',
}
)
.then(res => Buffer.from(res.data))
.catch(err => {
if (err.response && err.response.status === 404) {
return null;
}
handleError(err);
});
}
/**
* Account link
*
* https://developers.line.me/en/docs/messaging-api/reference/#account-link
*/
issueLinkToken(
userId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<{ issueToken: string }> {
return this._axios
.post(
`/v2/bot/user/${userId}/linkToken`,
null,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
/**
* LINE Front-end Framework (LIFF)
*
* https://developers.line.me/en/docs/liff/reference/#add-liff-app
*/
getLiffAppList({
accessToken: customAccessToken,
}: { accessToken?: string } = {}): Promise<{
liffId: string,
view: LiffView,
}> {
return this._axios
.get(
'/liff/v1/apps',
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data.apps, handleError);
}
createLiffApp(
view: LiffView,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<{ liffId: string }> {
return this._axios
.post(
'/liff/v1/apps',
view,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
updateLiffApp(
liffId: string,
view: LiffView,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<void> {
return this._axios
.put(
`/liff/v1/apps/${liffId}/view`,
view,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
deleteLiffApp(
liffId: string,
{ accessToken: customAccessToken }: { accessToken?: string } = {}
): Promise<void> {
return this._axios
.delete(
`/liff/v1/apps/${liffId}`,
customAccessToken && {
headers: { Authorization: `Bearer ${customAccessToken}` },
}
)
.then(res => res.data, handleError);
}
}
const sendTypes = ['reply', 'push', 'multicast'];
const messageTypes: Array<{
name: string,
aliases?: Array<string>,
}> = [
{ name: 'Text' },
{ name: 'Image' },
{ name: 'Video' },
{ name: 'Audio' },
{ name: 'Location' },
{ name: 'Sticker' },
{ name: 'Imagemap' },
{ name: 'Flex' },
{ name: 'Template' },
{ name: 'ButtonTemplate', aliases: ['ButtonsTemplate'] },
{ name: 'ConfirmTemplate' },
{ name: 'CarouselTemplate' },
{ name: 'ImageCarouselTemplate' },
];
messageTypes.forEach(({ name, aliases }) => {
sendTypes.forEach(sendType => {
[name].concat(aliases || []).forEach(type => {
Object.defineProperty(LineClient.prototype, `${sendType}${type}`, {
enumerable: false,
configurable: true,
writable: true,
value(target: SendTarget, ...args) {
return this[`_send${name}`](sendType, target, ...args);
},
});
});
});
});
/* @flow */
import querystring from 'querystring';
import AxiosError from 'axios-error';
import axios from 'axios';
import invariant from 'invariant';
type Axios = {
get: Function,
post: Function,
put: Function,
path: Function,
delete: Function,
};
type LinePayConfig = {
channelId: string,
channelSecret: string,
sandbox: boolean,
origin?: string,
};
type LinePayCurrency = 'USD' | 'JPY' | 'TWD' | 'THB';
function handleError(err) {
if (err.response && err.response.data) {
const { returnCode, returnMessage } = err.response.data;
const msg = `LINE PAY API - ${returnCode} ${returnMessage}`;
throw new AxiosError(msg, err);
}
throw new AxiosError(err.message, err);
}
function throwWhenNotSuccess(res) {
if (res.data.returnCode !== '0000') {
const { returnCode, returnMessage } = res.data;
const msg = `LINE PAY API - ${returnCode} ${returnMessage}`;
throw new AxiosError(msg);
}
return res.data.info;
}
export default class LinePay {
static connect(config: LinePayConfig): LinePay {
return new LinePay(config);
}
_axios: Axios;
constructor({
channelId,
channelSecret,
sandbox = false,
origin,
}: LinePayConfig) {
const linePayOrigin = sandbox
? 'https://sandbox-api-pay.line.me'
: 'https://api-pay.line.me';
this._axios = axios.create({
baseURL: `${origin || linePayOrigin}/v2/`,
headers: {
'Content-Type': 'application/json',
'X-LINE-ChannelId': channelId,
'X-LINE-ChannelSecret': channelSecret,
},
});
}
get axios(): Axios {
return this._axios;
}
getPayments({
transactionId,
orderId,
}: {
transactionId: string,
orderId: string,
} = {}) {
invariant(
transactionId || orderId,
'getPayments: One of `transactionId` or `orderId` must be provided'
);
const query = {};
if (transactionId) {
query.transactionId = transactionId;
}
if (orderId) {
query.orderId = orderId;
}
return this._axios
.get(`/payments?${querystring.stringify(query)}`)
.then(throwWhenNotSuccess, handleError);
}
getAuthorizations({
transactionId,
orderId,
}: {
transactionId: string,
orderId: string,
} = {}) {
invariant(
transactionId || orderId,
'getAuthorizations: One of `transactionId` or `orderId` must be provided'
);
const query = {};
if (transactionId) {
query.transactionId = transactionId;
}
if (orderId) {
query.orderId = orderId;
}
return this._axios
.get(`/payments/authorizations?${querystring.stringify(query)}`)
.then(throwWhenNotSuccess, handleError);
}
reserve({
productName,
amount,
currency,
confirmUrl,
orderId,
...options
}: {
productName: string,
amount: number,
currency: LinePayCurrency,
confirmUrl: string,
orderId: string,
productImageUrl?: string,
mid?: string,
oneTimeKey?: string,
confirmUrlType?: 'CLIENT' | 'SERVER',
checkConfirmUrlBrowser?: boolean,
cancelUrl?: string,
packageName?: string,
deliveryPlacePhone?: string,
payType?: 'NORMAL' | 'PREAPPROVED',
langCd?: 'ja' | 'ko' | 'en' | 'zh-Hans' | 'zh-Hant' | 'th',
capture?: boolean,
extras?: Object,
}) {
return this._axios
.post('/payments/request', {
productName,
amount,
currency,
confirmUrl,
orderId,
...options,
})
.then(throwWhenNotSuccess, handleError);
}
confirm(
transactionId: string,
{
amount,
currency,
}: {
amount: number,
currency: LinePayCurrency,
}
) {
return this._axios
.post(`/payments/${transactionId}/confirm`, {
amount,
currency,
})
.then(throwWhenNotSuccess, handleError);
}
capture(
transactionId: string,
{
amount,
currency,
}: {
amount: number,
currency: LinePayCurrency,
}
) {
return this._axios
.post(`/payments/authorizations/${transactionId}/capture`, {
amount,
currency,
})
.then(throwWhenNotSuccess, handleError);
}
void(transactionId: string) {
return this._axios
.post(`/payments/authorizations/${transactionId}/void`)
.then(throwWhenNotSuccess, handleError);
}
refund(transactionId: string, options?: { refundAmount?: number } = {}) {
return this._axios
.post(`/payments/${transactionId}/refund`, options)
.then(throwWhenNotSuccess, handleError);
}
}
/* @flow */
export type SendType = 'reply' | 'push' | 'multicast';
export type ReplyToken = string;
export type UserId = string;
export type SendTarget = ReplyToken | UserId | Array<UserId>;
export type User = {
displayName: string,
userId: string,
pictureUrl: string,
statusMessage: string,
};
export type TextMessage = {
type: 'text',
text: string,
};
export type ImageMessage = {
type: 'image',
originalContentUrl: string,
previewImageUrl: string,
};
export type ImageMapVideo = {
originalContentUrl: string,
previewImageUrl: string,
area: {
x: number,
y: number,
width: number,
height: number,
},
externalLink: {
linkUri: string,
label: string,
},
};
export type ImageMapAction = {
type: string,
linkUri: string,
area: {
x: number,
y: number,
width: number,
height: number,
},
};
export type ImageMapMessage = {
type: 'imagemap',
baseUrl: string,
altText: string,
baseSize: {
height: number,
width: number,
},
actions: Array<ImageMapAction>,
};
export type VideoMessage = {
type: 'video',
originalContentUrl: string,
previewImageUrl: string,
};
export type AudioMessage = {
type: 'audio',
originalContentUrl: string,
duration: number,
};
export type Location = {
title: string,
address: string,
latitude: number,
longitude: number,
};
export type LocationMessage = {
type: 'location',
title: string,
address: string,
latitude: number,
longitude: number,
};
export type StickerMessage = {
type: 'sticker',
packageId: string,
stickerId: string,
};
export type PostbackAction = {
type: 'postback',
label?: string,
data: string,
text?: string,
displayText?: string,
};
export type MessageAction = {
type: 'message',
label?: string,
text: string,
};
export type URIAction = {
type: 'uri',
label?: string,
uri: string,
};
export type DatetimePickerAction = {
type: 'datetimepicker',
label?: string,
data: string,
mode: string,
initial?: string,
max?: string,
min?: string,
};
export type CameraAction = {
type: 'camera',
label: string,
};
export type CameraRollAction = {
type: 'cameraRoll',
label: string,
};
export type LocationAction = {
type: 'location',
label: string,
};
export type TemplateAction =
| PostbackAction
| MessageAction
| URIAction
| DatetimePickerAction;
export type QuickReplyAction =
| PostbackAction
| MessageAction
| DatetimePickerAction
| CameraAction
| CameraRollAction
| LocationAction;
export type QuickReply = {
items: Array<{
type: 'action',
imageUrl?: string,
action: QuickReplyAction,
}>,
};
export type MessageOptions = {
quickReply?: QuickReply,
};
export type TemplateMessage<Template> = {
type: 'template',
altText: string,
template: Template,
};
export type ButtonsTemplate = {
type: 'buttons',
thumbnailImageUrl?: string,
title?: string,
text: string,
defaultAction?: TemplateAction,
actions: Array<TemplateAction>,
};
export type ConfirmTemplate = {
type: 'confirm',
text: string,
actions: Array<TemplateAction>,
};
export type ColumnObject = {
thumbnailImageUrl?: string,
title?: string,
text: string,
defaultAction?: TemplateAction,
actions: Array<TemplateAction>,
};
export type CarouselTemplate = {
type: 'carousel',
columns: Array<ColumnObject>,
};
export type ImageCarouselColumnObject = {
imageUrl: string,
action: TemplateAction,
};
export type ImageCarouselTemplate = {
type: 'image_carousel',
columns: Array<ImageCarouselColumnObject>,
};
export type Template =
| ButtonsTemplate
| ConfirmTemplate
| CarouselTemplate
| ImageCarouselTemplate;
type Size = 'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
type FlexBlockStyle = {
backgroundColor?: string,
separator?: boolean,
separatorColor?: string,
};
type FlexBubbleStyle = {
header?: FlexBlockStyle,
hero?: FlexBlockStyle,
body?: FlexBlockStyle,
footer?: FlexBlockStyle,
};
type FlexButton = {
type: 'button',
action: TemplateAction,
flex?: number,
margin?: Size,
height?: 'sm' | 'md',
style?: 'link' | 'primary' | 'secondary',
color?: string,
gravity?: string,
};
type FlexFiller = {
type: 'filler',
};
type FlexIcon = {
type: 'icon',
url: string,
margin?: Size,
size?:
| 'xxs'
| 'xs'
| 'sm'
| 'md'
| 'lg'
| 'xl'
| 'xxl'
| '3xl'
| '4xl'
| '5xl',
asprctRatio?: '1:1' | '2:1' | '3:1',
};
type FlexImage = {
type: 'image',
url: string,
flex?: number,
margin?: Size,
align?: 'start' | 'end' | 'center',
gravity?: 'top' | 'bottom' | 'center',
size?:
| 'xxs'
| 'xs'
| 'sm'
| 'md'
| 'lg'
| 'xl'
| 'xxl'
| '3xl'
| '4xl'
| '5xl'
| 'full',
aspectRatio?:
| '1:1'
| '1.51:1'
| '1.91:1'
| '4:3'
| '16:9'
| '20:13'
| '2:1'
| '3:1'
| '3:4'
| '9:16'
| '1:2'
| '1:3',
aspectMode?: 'cover' | 'fit',
backgroundColor?: string,
action?: TemplateAction,
};
type FlexSeparator = {
type: 'separator',
margin?: Size,
color?: string,
};
type FlexSpacer = {
type: 'spacer',
size: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl',
};
type FlexText = {
type: 'text',
text: string,
flex?: number,
margin?: Size,
size?:
| 'xxs'
| 'xs'
| 'sm'
| 'md'
| 'lg'
| 'xl'
| 'xxl'
| '3xl'
| '4xl'
| '5xl',
align?: 'start' | 'end' | 'center',
gravity?: 'top' | 'bottom' | 'center',
wrap?: boolean,
maxLines?: number,
weight?: 'regular' | 'bold',
color?: string,
action?: TemplateAction,
};
type FlexBoxContent =
// content
| FlexButton
| FlexIcon
| FlexImage
| FlexText
// layout
| FlexFiller
| FlexSeparator
| FlexSpacer;
type FlexBox = {
type: 'box',
layout: 'horizontal' | 'vertical' | 'baseline',
contents: Array<FlexBox | FlexBoxContent>,
flex?: number,
spacing?: Size,
margin?: Size,
action?: TemplateAction,
};
type FlexBubbleContainer = {
type: 'bubble',
direction?: 'ltr' | 'rtl',
header?: FlexBox,
hero?: FlexImage,
body?: FlexBox,
footer?: FlexBox,
styles?: FlexBubbleStyle,
};
type FlexCarouselContainer = {
type: 'carousel',
contents: Array<FlexBubbleContainer>,
};
export type FlexContainer = FlexBubbleContainer | FlexCarouselContainer;
export type FlexMessage = {
type: 'flex',
altText: string,
contents: FlexContainer,
};
export type Message =
| TextMessage
| ImageMessage
| ImageMapMessage
| VideoMessage
| AudioMessage
| LocationMessage
| StickerMessage
| TemplateMessage<Template>
| FlexMessage;
type Area = {
bounds: {
x: number,
y: number,
width: number,
height: number,
},
action: {
type: string,
data: string,
},
};
export type RichMenu = {
size: {
width: 2500,
height: 1686 | 843,
},
selected: boolean,
name: string,
chatBarText: string,
areas: Array<Area>,
};
export type LiffView = {
type: 'compact' | 'tall' | 'full',
url: string,
};
export type MutationSuccessResponse = {};
import invariant from 'invariant';
import Line from '../Line';
jest.mock('invariant');
const quickReplyOptions = {
quickReply: {
items: [
{
type: 'action',
action: {
type: 'cameraRoll',
label: 'Send photo',
},
},
{
type: 'action',
action: {
type: 'camera',
label: 'Open camera',
},
},
],
},
};
describe('#createText', () => {
it('should return text message object', () => {
expect(Line.createText('t')).toEqual({ type: 'text', text: 't' });
expect(Line.createText('t', quickReplyOptions)).toEqual({
type: 'text',
text: 't',
...quickReplyOptions,
});
});
});
describe('#createImage', () => {
it('should return image message object', () => {
expect(Line.createImage('http://example.com/img1.jpg')).toEqual({
type: 'image',
originalContentUrl: 'http://example.com/img1.jpg',
previewImageUrl: 'http://example.com/img1.jpg',
});
expect(
Line.createImage(
'http://example.com/img1.jpg',
'http://example.com/img2.jpg'
)
).toEqual({
type: 'image',
originalContentUrl: 'http://example.com/img1.jpg',
previewImageUrl: 'http://example.com/img2.jpg',
});
});
it('should work with object', () => {
expect(
Line.createImage({
originalContentUrl: 'http://example.com/img1.jpg',
})
).toEqual({
type: 'image',
originalContentUrl: 'http://example.com/img1.jpg',
previewImageUrl: 'http://example.com/img1.jpg',
});
expect(
Line.createImage({
originalContentUrl: 'http://example.com/img1.jpg',
previewImageUrl: 'http://example.com/img2.jpg',
})
).toEqual({
type: 'image',
originalContentUrl: 'http://example.com/img1.jpg',
previewImageUrl: 'http://example.com/img2.jpg',
});
expect(
Line.createImage(
{
originalContentUrl: 'http://example.com/img1.jpg',
},
quickReplyOptions
)
).toEqual({
type: 'image',
originalContentUrl: 'http://example.com/img1.jpg',
previewImageUrl: 'http://example.com/img1.jpg',
...quickReplyOptions,
});
expect(
Line.createImage(
{
originalContentUrl: 'http://example.com/img1.jpg',
previewImageUrl: 'http://example.com/img2.jpg',
},
quickReplyOptions
)
).toEqual({
type: 'image',
originalContentUrl: 'http://example.com/img1.jpg',
previewImageUrl: 'http://example.com/img2.jpg',
...quickReplyOptions,
});
});
it('call invariant if wrong argument type #1', () => {
Line.createImage('http://example.com/img1.jpg', {});
expect(invariant).toBeCalled();
});
it('call invariant if wrong argument type #2', () => {
Line.createImage(123);
expect(invariant).toBeCalled();
});
});
describe('#createVideo', () => {
it('should return video message object', () => {
expect(
Line.createVideo(
'http://example.com/video.mp4',
'http://example.com/img.jpg'
)
).toEqual({
type: 'video',
originalContentUrl: 'http://example.com/video.mp4',
previewImageUrl: 'http://example.com/img.jpg',
});
});
it('should work with object', () => {
expect(
Line.createVideo({
originalContentUrl: 'http://example.com/video.mp4',
previewImageUrl: 'http://example.com/img.jpg',
})
).toEqual({
type: 'video',
originalContentUrl: 'http://example.com/video.mp4',
previewImageUrl: 'http://example.com/img.jpg',
});
expect(
Line.createVideo(
{
originalContentUrl: 'http://example.com/video.mp4',
previewImageUrl: 'http://example.com/img.jpg',
},
quickReplyOptions
)
).toEqual({
type: 'video',
originalContentUrl: 'http://example.com/video.mp4',
previewImageUrl: 'http://example.com/img.jpg',
...quickReplyOptions,
});
});
it('call invariant if wrong argument type', () => {
Line.createVideo('http://example.com/video.mp4', {});
expect(invariant).toBeCalled();
});
});
describe('#createAudio', () => {
it('should return audio message object', () => {
expect(Line.createAudio('http://example.com/audio.mp3', 240000)).toEqual({
type: 'audio',
originalContentUrl: 'http://example.com/audio.mp3',
duration: 240000,
});
});
it('should work with object', () => {
expect(
Line.createAudio({
originalContentUrl: 'http://example.com/audio.mp3',
duration: 240000,
})
).toEqual({
type: 'audio',
originalContentUrl: 'http://example.com/audio.mp3',
duration: 240000,
});
expect(
Line.createAudio(
{
originalContentUrl: 'http://example.com/audio.mp3',
duration: 240000,
},
quickReplyOptions
)
).toEqual({
type: 'audio',
originalContentUrl: 'http://example.com/audio.mp3',
duration: 240000,
...quickReplyOptions,
});
});
it('call invariant if wrong argument type', () => {
Line.createAudio('http://example.com/audio.mp3', {});
expect(invariant).toBeCalled();
});
});
describe('#createLocation', () => {
it('should return location message object', () => {
expect(
Line.createLocation({
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
})
).toEqual({
type: 'location',
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
});
expect(
Line.createLocation(
{
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
quickReplyOptions
)
).toEqual({
type: 'location',
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
...quickReplyOptions,
});
});
});
describe('#createSticker', () => {
it('should return sticker message object', () => {
expect(Line.createSticker('1', '1')).toEqual({
type: 'sticker',
packageId: '1',
stickerId: '1',
});
});
it('should work with object', () => {
expect(
Line.createSticker({
packageId: '1',
stickerId: '1',
})
).toEqual({
type: 'sticker',
packageId: '1',
stickerId: '1',
});
expect(
Line.createSticker(
{
packageId: '1',
stickerId: '1',
},
quickReplyOptions
)
).toEqual({
type: 'sticker',
packageId: '1',
stickerId: '1',
...quickReplyOptions,
});
});
it('call invariant if wrong argument type', () => {
Line.createSticker('1', {});
expect(invariant).toBeCalled();
});
});
describe('#createImagemap', () => {
it('should return imagemap message object', () => {
expect(
Line.createImagemap('this is an imagemap', {
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
width: 1040,
height: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
})
).toEqual({
type: 'imagemap',
altText: 'this is an imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
width: 1040,
height: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
});
expect(
Line.createImagemap(
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
width: 1040,
height: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
quickReplyOptions
)
).toEqual({
type: 'imagemap',
altText: 'this is an imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
width: 1040,
height: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
...quickReplyOptions,
});
});
});
describe('#createImagemap', () => {
it('should return imagemap message object', () => {
expect(
Line.createImagemap('this is an imagemap', {
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
width: 1040,
height: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
})
).toEqual({
type: 'imagemap',
altText: 'this is an imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
width: 1040,
height: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
});
expect(
Line.createImagemap(
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
width: 1040,
height: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
quickReplyOptions
)
).toEqual({
type: 'imagemap',
altText: 'this is an imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
width: 1040,
height: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
...quickReplyOptions,
});
});
});
describe('#createTemplate', () => {
it('should return template message object', () => {
expect(
Line.createTemplate('this is a buttons template', {
type: 'buttons',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
})
).toEqual({
type: 'template',
altText: 'this is a buttons template',
template: {
type: 'buttons',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
});
expect(
Line.createTemplate(
'this is a buttons template',
{
type: 'buttons',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
quickReplyOptions
)
).toEqual({
type: 'template',
altText: 'this is a buttons template',
template: {
type: 'buttons',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
...quickReplyOptions,
});
});
});
describe('#createTemplate', () => {
it('should return template message object', () => {
expect(
Line.createTemplate('this is a confirm template', {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
})
).toEqual({
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
});
expect(
Line.createTemplate(
'this is a confirm template',
{
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
quickReplyOptions
)
).toEqual({
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
...quickReplyOptions,
});
});
});
describe('#createButtonTemplate', () => {
it('should return buttons template message object', () => {
expect(
Line.createButtonTemplate('this is a buttons template', {
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
})
).toEqual({
type: 'template',
altText: 'this is a buttons template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
});
expect(
Line.createButtonTemplate(
'this is a buttons template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
quickReplyOptions
)
).toEqual({
type: 'template',
altText: 'this is a buttons template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
...quickReplyOptions,
});
});
it('should support createButtonsTemplate alias', () => {
expect(
Line.createButtonsTemplate('this is a buttons template', {
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
})
).toEqual({
type: 'template',
altText: 'this is a buttons template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
});
});
});
describe('#createConfirmTemplate', () => {
it('should return confirm template message object', () => {
expect(
Line.createConfirmTemplate('this is a confirm template', {
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
})
).toEqual({
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
});
expect(
Line.createConfirmTemplate(
'this is a confirm template',
{
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
quickReplyOptions
)
).toEqual({
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
...quickReplyOptions,
});
});
});
describe('#createCarouselTemplate', () => {
it('should return carousel template message object', () => {
expect(
Line.createCarouselTemplate('this is a carousel template', [
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
])
).toEqual({
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
columns: [
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
});
expect(
Line.createCarouselTemplate(
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
quickReplyOptions
)
).toEqual({
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
columns: [
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
...quickReplyOptions,
});
});
});
describe('#createImageCarouselTemplate', () => {
it('should return image carousel template message object', () => {
expect(
Line.createImageCarouselTemplate('this is a image carousel template', [
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
])
).toEqual({
type: 'template',
altText: 'this is a image carousel template',
template: {
type: 'image_carousel',
columns: [
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
},
});
expect(
Line.createImageCarouselTemplate(
'this is a image carousel template',
[
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
quickReplyOptions
)
).toEqual({
type: 'template',
altText: 'this is a image carousel template',
template: {
type: 'image_carousel',
columns: [
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
},
...quickReplyOptions,
});
});
});
describe('#createFlex', () => {
it('should return flex message object', () => {
expect(
Line.createFlex('this is a flex message', {
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
})
).toEqual({
type: 'flex',
altText: 'this is a flex message',
contents: {
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
});
expect(
Line.createFlex(
'this is a flex message',
{
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
quickReplyOptions
)
).toEqual({
type: 'flex',
altText: 'this is a flex message',
contents: {
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
...quickReplyOptions,
});
});
});
import MockAdapter from 'axios-mock-adapter';
import LineClient from '../LineClient';
const ACCESS_TOKEN = '1234567890';
const CHANNEL_SECRET = 'so-secret';
describe('connect', () => {
let axios;
let _create;
beforeEach(() => {
axios = require('axios'); // eslint-disable-line global-require
_create = axios.create;
});
afterEach(() => {
axios.create = _create;
});
describe('create axios with Line API', () => {
it('with args', () => {
axios.create = jest.fn().mockReturnValue({
interceptors: {
request: {
use: jest.fn(),
},
},
});
LineClient.connect(ACCESS_TOKEN, CHANNEL_SECRET);
expect(axios.create).toBeCalledWith({
baseURL: 'https://api.line.me/',
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
});
});
it('with config', () => {
axios.create = jest.fn().mockReturnValue({
interceptors: {
request: {
use: jest.fn(),
},
},
});
LineClient.connect({
accessToken: ACCESS_TOKEN,
channelSecret: CHANNEL_SECRET,
});
expect(axios.create).toBeCalledWith({
baseURL: 'https://api.line.me/',
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
});
});
});
it('support origin', () => {
axios.create = jest.fn().mockReturnValue({
interceptors: {
request: {
use: jest.fn(),
},
},
});
LineClient.connect({
accessToken: ACCESS_TOKEN,
channelSecret: CHANNEL_SECRET,
origin: 'https://mydummytestserver.com',
});
expect(axios.create).toBeCalledWith({
baseURL: 'https://mydummytestserver.com/',
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
});
});
});
describe('constructor', () => {
let axios;
let _create;
beforeEach(() => {
axios = require('axios'); // eslint-disable-line global-require
_create = axios.create;
});
afterEach(() => {
axios.create = _create;
});
describe('create axios with Line API', () => {
it('with args', () => {
axios.create = jest.fn().mockReturnValue({
interceptors: {
request: {
use: jest.fn(),
},
},
});
new LineClient(ACCESS_TOKEN, CHANNEL_SECRET); // eslint-disable-line no-new
expect(axios.create).toBeCalledWith({
baseURL: 'https://api.line.me/',
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
});
});
it('with config', () => {
axios.create = jest.fn().mockReturnValue({
interceptors: {
request: {
use: jest.fn(),
},
},
});
// eslint-disable-next-line no-new
new LineClient({
accessToken: ACCESS_TOKEN,
channelSecret: CHANNEL_SECRET,
});
expect(axios.create).toBeCalledWith({
baseURL: 'https://api.line.me/',
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
});
});
});
it('support origin', () => {
axios.create = jest.fn().mockReturnValue({
interceptors: {
request: {
use: jest.fn(),
},
},
});
// eslint-disable-next-line no-new
new LineClient({
accessToken: ACCESS_TOKEN,
channelSecret: CHANNEL_SECRET,
origin: 'https://mydummytestserver.com',
});
expect(axios.create).toBeCalledWith({
baseURL: 'https://mydummytestserver.com/',
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
});
});
});
describe('#axios', () => {
it('should return underlying http client', () => {
let client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
expect(client.axios.get).toBeDefined();
expect(client.axios.post).toBeDefined();
expect(client.axios.put).toBeDefined();
expect(client.axios.delete).toBeDefined();
client = new LineClient({
accessToken: ACCESS_TOKEN,
channelSecret: CHANNEL_SECRET,
});
expect(client.axios.get).toBeDefined();
expect(client.axios.post).toBeDefined();
expect(client.axios.put).toBeDefined();
expect(client.axios.delete).toBeDefined();
});
});
describe('#accessToken', () => {
it('should return underlying access token', () => {
let client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
expect(client.accessToken).toBe(ACCESS_TOKEN);
client = new LineClient({
accessToken: ACCESS_TOKEN,
channelSecret: CHANNEL_SECRET,
});
expect(client.accessToken).toBe(ACCESS_TOKEN);
});
});
describe('#onRequest', () => {
it('should call onRequest when calling any API', async () => {
const onRequest = jest.fn();
const client = new LineClient({
accessToken: ACCESS_TOKEN,
channelSecret: CHANNEL_SECRET,
onRequest,
});
const mock = new MockAdapter(client.axios);
mock.onPost('/path').reply(200, {});
await client.axios.post('/path', { x: 1 });
expect(onRequest).toBeCalledWith({
method: 'post',
url: 'https://api.line.me/path',
body: {
x: 1,
},
headers: {
Authorization: 'Bearer 1234567890',
'Content-Type': 'application/json',
Accept: 'application/json, text/plain, */*',
},
});
});
});
describe('Client instance', () => {
it('prototype should be defined', () => {
const sendTypes = ['reply', 'push', 'multicast'];
const messageTypes = [
'Text',
'Image',
'Video',
'Audio',
'Location',
'Sticker',
'Imagemap',
'Template',
'ButtonTemplate',
'ConfirmTemplate',
'CarouselTemplate',
'ImageCarouselTemplate',
];
let client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
sendTypes.forEach(sendType => {
messageTypes.forEach(messageType => {
expect(client[`${sendType}${messageType}`]).toBeDefined();
});
});
client = new LineClient({
accessToken: ACCESS_TOKEN,
channelSecret: CHANNEL_SECRET,
});
sendTypes.forEach(sendType => {
messageTypes.forEach(messageType => {
expect(client[`${sendType}${messageType}`]).toBeDefined();
});
});
});
});
import MockAdapter from 'axios-mock-adapter';
import LineClient from '../LineClient';
const CUSTOM_ACCESS_TOKEN = '555555555';
const ACCESS_TOKEN = '1234567890';
const CHANNEL_SECRET = 'so-secret';
const createMock = ({ customAccessToken } = {}) => {
const client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
const mock = new MockAdapter(client.axios);
const headers = {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
Authorization: `Bearer ${customAccessToken || ACCESS_TOKEN}`,
};
return { client, mock, headers };
};
describe('LINE Front-end Framework', () => {
describe('#getLiffAppList', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
apps: [
{
liffId: 'liff-12345',
view: {
type: 'full',
url: 'https://example.com/myservice',
},
},
{
liffId: 'liff-67890',
view: {
type: 'tall',
url: 'https://example.com/myservice2',
},
},
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual('https://api.line.me/liff/v1/apps');
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getLiffAppList();
expect(res).toEqual([
{
liffId: 'liff-12345',
view: {
type: 'full',
url: 'https://example.com/myservice',
},
},
{
liffId: 'liff-67890',
view: {
type: 'tall',
url: 'https://example.com/myservice2',
},
},
]);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
apps: [
{
liffId: 'liff-12345',
view: {
type: 'full',
url: 'https://example.com/myservice',
},
},
{
liffId: 'liff-67890',
view: {
type: 'tall',
url: 'https://example.com/myservice2',
},
},
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual('https://api.line.me/liff/v1/apps');
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getLiffAppList({
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual([
{
liffId: 'liff-12345',
view: {
type: 'full',
url: 'https://example.com/myservice',
},
},
{
liffId: 'liff-67890',
view: {
type: 'tall',
url: 'https://example.com/myservice2',
},
},
]);
});
});
describe('#createLiffApp', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
liffId: 'liff-12345',
};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/liff/v1/apps');
expect(JSON.parse(config.data)).toEqual({
type: 'tall',
url: 'https://example.com/myservice',
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.createLiffApp({
type: 'tall',
url: 'https://example.com/myservice',
});
expect(res).toEqual({
liffId: 'liff-12345',
});
});
it('should work with custom access token', async () => {
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
liffId: 'liff-12345',
};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/liff/v1/apps');
expect(JSON.parse(config.data)).toEqual({
type: 'tall',
url: 'https://example.com/myservice',
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.createLiffApp(
{
type: 'tall',
url: 'https://example.com/myservice',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual({
liffId: 'liff-12345',
});
});
});
describe('#updateLiffApp', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPut().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/liff/v1/apps/liff-12345/view'
);
expect(JSON.parse(config.data)).toEqual({
type: 'tall',
url: 'https://example.com/myservice',
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.updateLiffApp('liff-12345', {
type: 'tall',
url: 'https://example.com/myservice',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPut().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/liff/v1/apps/liff-12345/view'
);
expect(JSON.parse(config.data)).toEqual({
type: 'tall',
url: 'https://example.com/myservice',
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.updateLiffApp(
'liff-12345',
{
type: 'tall',
url: 'https://example.com/myservice',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#deleteLiffApp', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onDelete().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/liff/v1/apps/liff-12345'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.deleteLiffApp('liff-12345');
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onDelete().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/liff/v1/apps/liff-12345'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.deleteLiffApp('liff-12345', {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
});
import fs from 'fs';
import path from 'path';
import MockAdapter from 'axios-mock-adapter';
import LineClient from '../LineClient';
const CUSTOM_ACCESS_TOKEN = '555555555';
const ACCESS_TOKEN = '1234567890';
const CHANNEL_SECRET = 'so-secret';
const createMock = ({ customAccessToken } = {}) => {
const client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
const mock = new MockAdapter(client.axios);
const headers = {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
Authorization: `Bearer ${customAccessToken || ACCESS_TOKEN}`,
};
return { client, mock, headers };
};
describe('Rich Menu', () => {
describe('#getRichMenuList', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
richmenus: [
{
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=buy&itemid=123',
},
},
],
},
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/richmenu/list');
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getRichMenuList();
expect(res).toEqual([
{
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=buy&itemid=123',
},
},
],
},
]);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
richmenus: [
{
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=buy&itemid=123',
},
},
],
},
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/richmenu/list');
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getRichMenuList({
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual([
{
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=buy&itemid=123',
},
},
],
},
]);
});
});
describe('#getRichMenu', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=buy&itemid=123',
},
},
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/richmenu/richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getRichMenu(
'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=buy&itemid=123',
},
},
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/richmenu/richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getRichMenu(
'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
it('should return null when no rich menu found', async () => {
const { client, mock } = createMock();
mock.onGet().reply(404, {
message: 'richmenu not found',
details: [],
});
const res = await client.getRichMenu(
'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(res).toEqual(null);
});
});
describe('#createRichMenu', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
};
const richMenuObject = {
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=buy&itemid=123',
},
},
],
};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/richmenu');
expect(JSON.parse(config.data)).toEqual(richMenuObject);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.createRichMenu(richMenuObject);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
};
const richMenuObject = {
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=buy&itemid=123',
},
},
],
};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/richmenu');
expect(JSON.parse(config.data)).toEqual(richMenuObject);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.createRichMenu(richMenuObject, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#deleteRichMenu', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onDelete().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/richmenu/1');
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.deleteRichMenu('1');
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onDelete().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/richmenu/1');
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.deleteRichMenu('1', {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#getLinkedRichMenu', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/1/richmenu'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getLinkedRichMenu('1');
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/1/richmenu'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getLinkedRichMenu('1', {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
it('should return null when no rich menu found', async () => {
const { client, mock } = createMock();
mock.onGet().reply(404, {
message: 'the user has no richmenu',
details: [],
});
const res = await client.getLinkedRichMenu(
'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(res).toEqual(null);
});
});
describe('#linkRichMenu', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/1/richmenu/2'
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.linkRichMenu('1', '2');
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/1/richmenu/2'
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.linkRichMenu('1', '2', {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#unlinkRichMenu', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onDelete().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/1/richmenu'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.unlinkRichMenu('1');
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onDelete().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/1/richmenu'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.unlinkRichMenu('1', {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#uploadRichMenuImage', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
const buffer = await new Promise((resolve, reject) => {
fs.readFile(path.join(__dirname, 'fixture.png'), (err, buf) => {
if (err) {
reject(err);
} else {
resolve(buf);
}
});
});
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/richmenu/1/content'
);
expect(config.data).toEqual(buffer);
expect(config.headers).toEqual({
...headers,
'Content-Type': 'image/png',
});
return [200, reply];
});
const res = await client.uploadRichMenuImage('1', buffer);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
const buffer = await new Promise((resolve, reject) => {
fs.readFile(path.join(__dirname, 'fixture.png'), (err, buf) => {
if (err) {
reject(err);
} else {
resolve(buf);
}
});
});
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/richmenu/1/content'
);
expect(config.data).toEqual(buffer);
expect(config.headers).toEqual({
...headers,
'Content-Type': 'image/png',
});
return [200, reply];
});
const res = await client.uploadRichMenuImage('1', buffer, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
it('should throw error when ', async () => {
expect.assertions(1);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/richmenu/1/content'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
let error;
try {
await client.uploadRichMenuImage('1', Buffer.from('a content buffer'));
} catch (err) {
error = err;
}
expect(error.message).toMatch(/image\/(jpeg|png)/);
});
});
describe('#downloadRichMenuImage', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = Buffer.from('a content buffer');
mock.onGet().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/richmenu/1/content'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.downloadRichMenuImage('1');
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = Buffer.from('a content buffer');
mock.onGet().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/richmenu/1/content'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.downloadRichMenuImage('1', {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
it('should return null when no rich menu image found', async () => {
const { client, mock } = createMock();
mock.onGet().reply(404, Buffer.from('{"message":"Not found"}'));
const res = await client.downloadRichMenuImage(
'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(res).toEqual(null);
});
});
describe('#getDefaultRichMenu', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/all/richmenu'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getDefaultRichMenu();
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
richMenuId: 'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/all/richmenu'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getDefaultRichMenu({
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
it('should return null when no default rich menu found', async () => {
const { client, mock } = createMock();
mock.onGet().reply(404, {
message: 'no default richmenu',
details: [],
});
const res = await client.getDefaultRichMenu(
'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(res).toEqual(null);
});
});
describe('#setDefaultRichMenu', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/all/richmenu/richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.setDefaultRichMenu(
'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/all/richmenu/richmenu-8dfdfc571eca39c0ffcd1f799519c5b5'
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.setDefaultRichMenu(
'richmenu-8dfdfc571eca39c0ffcd1f799519c5b5',
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#deleteDefaultRichMenu', () => {
it('should call api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onDelete().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/all/richmenu'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.deleteDefaultRichMenu();
expect(res).toEqual(reply);
});
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onDelete().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/user/all/richmenu'
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.deleteDefaultRichMenu({
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
import MockAdapter from 'axios-mock-adapter';
import LineClient from '../LineClient';
const RECIPIENT_ID = '1QAZ2WSX';
const CUSTOM_ACCESS_TOKEN = '555555555';
const ACCESS_TOKEN = '1234567890';
const CHANNEL_SECRET = 'so-secret';
const createMock = ({ customAccessToken } = {}) => {
const client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
const mock = new MockAdapter(client.axios);
const headers = {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
Authorization: `Bearer ${customAccessToken || ACCESS_TOKEN}`,
};
return { client, mock, headers };
};
describe('Multicast', () => {
describe('#multicastRawBody', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastRawBody({
to: [RECIPIENT_ID],
messages: [
{
type: 'text',
text: 'Hello!',
},
],
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastRawBody(
{
to: [RECIPIENT_ID],
messages: [
{
type: 'text',
text: 'Hello!',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicast', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicast(
[RECIPIENT_ID],
[
{
type: 'text',
text: 'Hello!',
},
]
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicast(
[RECIPIENT_ID],
[
{
type: 'text',
text: 'Hello!',
},
],
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicastText', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastText([RECIPIENT_ID], 'Hello!');
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastText([RECIPIENT_ID], 'Hello!', {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#multicastImage', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImage(
[RECIPIENT_ID],
'https://example.com/original.jpg',
'https://example.com/preview.jpg'
);
expect(res).toEqual(reply);
});
it('should use contentUrl as fallback', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/original.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImage(
[RECIPIENT_ID],
'https://example.com/original.jpg'
);
expect(res).toEqual(reply);
});
it('should call multicast api with object image arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImage([RECIPIENT_ID], {
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImage(
[RECIPIENT_ID],
{
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicastVideo', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'video',
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastVideo(
[RECIPIENT_ID],
'https://example.com/original.mp4',
'https://example.com/preview.jpg'
);
expect(res).toEqual(reply);
});
it('should call multicast api with object video arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'video',
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastVideo([RECIPIENT_ID], {
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'video',
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastVideo(
[RECIPIENT_ID],
{
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicastAudio', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'audio',
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastAudio(
[RECIPIENT_ID],
'https://example.com/original.m4a',
240000
);
expect(res).toEqual(reply);
});
it('should call multicast api with object audio arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'audio',
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastAudio([RECIPIENT_ID], {
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'audio',
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastAudio(
[RECIPIENT_ID],
{
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
{
accessToken: CUSTOM_ACCESS_TOKEN,
}
);
expect(res).toEqual(reply);
});
});
describe('#multicastLocation', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'location',
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastLocation([RECIPIENT_ID], {
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'location',
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastLocation(
[RECIPIENT_ID],
{
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicastSticker', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'sticker',
packageId: '1',
stickerId: '1',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastSticker([RECIPIENT_ID], '1', '1');
expect(res).toEqual(reply);
});
it('should call multicast api with object sticker arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'sticker',
packageId: '1',
stickerId: '1',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastSticker([RECIPIENT_ID], {
packageId: '1',
stickerId: '1',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'sticker',
packageId: '1',
stickerId: '1',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastSticker(
[RECIPIENT_ID],
{
packageId: '1',
stickerId: '1',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicastImagemap', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImagemap(
[RECIPIENT_ID],
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseHeight: 1040,
baseWidth: 1040,
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImagemap(
[RECIPIENT_ID],
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseHeight: 1040,
baseWidth: 1040,
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
it('should support baseSize argument', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImagemap(
[RECIPIENT_ID],
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
}
);
expect(res).toEqual(reply);
});
it('should support video', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
video: {
originalContentUrl: 'https://example.com/video.mp4',
previewImageUrl: 'https://example.com/video_preview.jpg',
area: {
x: 0,
y: 0,
width: 1040,
height: 585,
},
externalLink: {
linkUri: 'https://example.com/see_more.html',
label: 'See More',
},
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImagemap(
[RECIPIENT_ID],
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
height: 1040,
width: 1040,
},
video: {
originalContentUrl: 'https://example.com/video.mp4',
previewImageUrl: 'https://example.com/video_preview.jpg',
area: {
x: 0,
y: 0,
width: 1040,
height: 585,
},
externalLink: {
linkUri: 'https://example.com/see_more.html',
label: 'See More',
},
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
}
);
expect(res).toEqual(reply);
});
});
describe('#multicastTemplate', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastTemplate(
[RECIPIENT_ID],
'this is a template',
{
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastTemplate(
[RECIPIENT_ID],
'this is a template',
{
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicastButtonTemplate', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastButtonTemplate(
[RECIPIENT_ID],
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastButtonTemplate(
[RECIPIENT_ID],
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicastConfirmTemplate', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastConfirmTemplate(
[RECIPIENT_ID],
'this is a confirm template',
{
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastConfirmTemplate(
[RECIPIENT_ID],
'this is a confirm template',
{
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicastCarouselTemplate', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
columns: [
{
thumbnailImageUrl:
'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl:
'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastCarouselTemplate(
[RECIPIENT_ID],
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
{
imageAspectRatio: 'rectangle',
imageSize: 'cover',
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
columns: [
{
thumbnailImageUrl:
'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl:
'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastCarouselTemplate(
[RECIPIENT_ID],
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
{
imageAspectRatio: 'rectangle',
imageSize: 'cover',
accessToken: CUSTOM_ACCESS_TOKEN,
}
);
expect(res).toEqual(reply);
});
it('should work without option', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
columns: [
{
thumbnailImageUrl:
'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl:
'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastCarouselTemplate(
[RECIPIENT_ID],
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
]
);
expect(res).toEqual(reply);
});
});
describe('#multicastImageCarouselTemplate', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is an image carousel template',
template: {
type: 'image_carousel',
columns: [
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImageCarouselTemplate(
[RECIPIENT_ID],
'this is an image carousel template',
[
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
]
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'template',
altText: 'this is an image carousel template',
template: {
type: 'image_carousel',
columns: [
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastImageCarouselTemplate(
[RECIPIENT_ID],
'this is an image carousel template',
[
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#multicastFlex', () => {
it('should call multicast api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'flex',
altText: 'this is a flex message',
contents: {
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastFlex(
[RECIPIENT_ID],
'this is a flex message',
{
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
'https://api.line.me/v2/bot/message/multicast'
);
expect(JSON.parse(config.data)).toEqual({
to: [RECIPIENT_ID],
messages: [
{
type: 'flex',
altText: 'this is a flex message',
contents: {
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.multicastFlex(
[RECIPIENT_ID],
'this is a flex message',
{
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
});
import MockAdapter from 'axios-mock-adapter';
import LineClient from '../LineClient';
const RECIPIENT_ID = '1QAZ2WSX';
const CUSTOM_ACCESS_TOKEN = '555555555';
const ACCESS_TOKEN = '1234567890';
const CHANNEL_SECRET = 'so-secret';
const createMock = ({ customAccessToken } = {}) => {
const client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
const mock = new MockAdapter(client.axios);
const headers = {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
Authorization: `Bearer ${customAccessToken || ACCESS_TOKEN}`,
};
return { client, mock, headers };
};
describe('Push Message', () => {
describe('#pushRawBody', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushRawBody({
to: RECIPIENT_ID,
messages: [
{
type: 'text',
text: 'Hello!',
},
],
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushRawBody(
{
to: RECIPIENT_ID,
messages: [
{
type: 'text',
text: 'Hello!',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#push', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.push(RECIPIENT_ID, [
{
type: 'text',
text: 'Hello!',
},
]);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.push(
RECIPIENT_ID,
[
{
type: 'text',
text: 'Hello!',
},
],
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushText', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushText(RECIPIENT_ID, 'Hello!');
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushText(RECIPIENT_ID, 'Hello!', {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#pushImage', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImage(
RECIPIENT_ID,
'https://example.com/original.jpg',
'https://example.com/preview.jpg'
);
expect(res).toEqual(reply);
});
it('should use contentUrl as fallback', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/original.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImage(
RECIPIENT_ID,
'https://example.com/original.jpg'
);
expect(res).toEqual(reply);
});
it('should call push api with object image arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImage(RECIPIENT_ID, {
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImage(
RECIPIENT_ID,
{
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushVideo', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'video',
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushVideo(
RECIPIENT_ID,
'https://example.com/original.mp4',
'https://example.com/preview.jpg'
);
expect(res).toEqual(reply);
});
it('should call push api with object video arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'video',
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushVideo(RECIPIENT_ID, {
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'video',
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushVideo(
RECIPIENT_ID,
{
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushAudio', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'audio',
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushAudio(
RECIPIENT_ID,
'https://example.com/original.m4a',
240000
);
expect(res).toEqual(reply);
});
it('should call push api with object audio arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'audio',
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushAudio(RECIPIENT_ID, {
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'audio',
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushAudio(
RECIPIENT_ID,
{
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushLocation', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'location',
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushLocation(RECIPIENT_ID, {
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'location',
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushLocation(
RECIPIENT_ID,
{
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushSticker', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'sticker',
packageId: '1',
stickerId: '1',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushSticker(RECIPIENT_ID, '1', '1');
expect(res).toEqual(reply);
});
it('should call push api with object sticker arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'sticker',
packageId: '1',
stickerId: '1',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushSticker(RECIPIENT_ID, {
packageId: '1',
stickerId: '1',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'sticker',
packageId: '1',
stickerId: '1',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushSticker(
RECIPIENT_ID,
{
packageId: '1',
stickerId: '1',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushImagemap', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImagemap(
RECIPIENT_ID,
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseHeight: 1040,
baseWidth: 1040,
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImagemap(
RECIPIENT_ID,
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseHeight: 1040,
baseWidth: 1040,
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
it('should support baseSize argument', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImagemap(
RECIPIENT_ID,
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
}
);
expect(res).toEqual(reply);
});
it('should support video', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
video: {
originalContentUrl: 'https://example.com/video.mp4',
previewImageUrl: 'https://example.com/video_preview.jpg',
area: {
x: 0,
y: 0,
width: 1040,
height: 585,
},
externalLink: {
linkUri: 'https://example.com/see_more.html',
label: 'See More',
},
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImagemap(
RECIPIENT_ID,
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
height: 1040,
width: 1040,
},
video: {
originalContentUrl: 'https://example.com/video.mp4',
previewImageUrl: 'https://example.com/video_preview.jpg',
area: {
x: 0,
y: 0,
width: 1040,
height: 585,
},
externalLink: {
linkUri: 'https://example.com/see_more.html',
label: 'See More',
},
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
}
);
expect(res).toEqual(reply);
});
});
describe('#pushTemplate', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushTemplate(
RECIPIENT_ID,
'this is a template',
{
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushTemplate(
RECIPIENT_ID,
'this is a template',
{
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushButtonTemplate', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushButtonTemplate(
RECIPIENT_ID,
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushButtonTemplate(
RECIPIENT_ID,
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
it('should support pushButtonsTemplate alias', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushButtonsTemplate(
RECIPIENT_ID,
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
}
);
expect(res).toEqual(reply);
});
it('pushButtonsTemplate alias should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushButtonsTemplate(
RECIPIENT_ID,
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushConfirmTemplate', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushConfirmTemplate(
RECIPIENT_ID,
'this is a confirm template',
{
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushConfirmTemplate(
RECIPIENT_ID,
'this is a confirm template',
{
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushCarouselTemplate', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
columns: [
{
thumbnailImageUrl:
'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl:
'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushCarouselTemplate(
RECIPIENT_ID,
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
{
imageAspectRatio: 'rectangle',
imageSize: 'cover',
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
columns: [
{
thumbnailImageUrl:
'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl:
'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushCarouselTemplate(
RECIPIENT_ID,
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
{
imageAspectRatio: 'rectangle',
imageSize: 'cover',
accessToken: CUSTOM_ACCESS_TOKEN,
}
);
expect(res).toEqual(reply);
});
it('should work without option', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
columns: [
{
thumbnailImageUrl:
'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl:
'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushCarouselTemplate(
RECIPIENT_ID,
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
]
);
expect(res).toEqual(reply);
});
});
describe('#pushImageCarouselTemplate', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is an image carousel template',
template: {
type: 'image_carousel',
columns: [
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImageCarouselTemplate(
RECIPIENT_ID,
'this is an image carousel template',
[
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
]
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'template',
altText: 'this is an image carousel template',
template: {
type: 'image_carousel',
columns: [
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushImageCarouselTemplate(
RECIPIENT_ID,
'this is an image carousel template',
[
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#pushFlex', () => {
it('should call push api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'flex',
altText: 'this is a flex message',
contents: {
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushFlex(
RECIPIENT_ID,
'this is a flex message',
{
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/push');
expect(JSON.parse(config.data)).toEqual({
to: RECIPIENT_ID,
messages: [
{
type: 'flex',
altText: 'this is a flex message',
contents: {
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.pushFlex(
RECIPIENT_ID,
'this is a flex message',
{
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
});
import MockAdapter from 'axios-mock-adapter';
import LineClient from '../LineClient';
const REPLY_TOKEN = 'nHuyWiB7yP5Zw52FIkcQobQuGDXCTA';
const CUSTOM_ACCESS_TOKEN = '555555555';
const ACCESS_TOKEN = '1234567890';
const CHANNEL_SECRET = 'so-secret';
const createMock = ({ customAccessToken } = {}) => {
const client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
const mock = new MockAdapter(client.axios);
const headers = {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
Authorization: `Bearer ${customAccessToken || ACCESS_TOKEN}`,
};
return { client, mock, headers };
};
describe('Reply Message', () => {
describe('#replyRawBody', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyRawBody({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'text',
text: 'Hello!',
},
],
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyRawBody(
{
replyToken: REPLY_TOKEN,
messages: [
{
type: 'text',
text: 'Hello!',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#reply', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.reply(REPLY_TOKEN, [
{
type: 'text',
text: 'Hello!',
},
]);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.reply(
REPLY_TOKEN,
[
{
type: 'text',
text: 'Hello!',
},
],
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replyText', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyText(REPLY_TOKEN, 'Hello!');
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [{ type: 'text', text: 'Hello!' }],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyText(REPLY_TOKEN, 'Hello!', {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#replyImage', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImage(
REPLY_TOKEN,
'https://example.com/original.jpg',
'https://example.com/preview.jpg'
);
expect(res).toEqual(reply);
});
it('should use contentUrl as fallback', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/original.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImage(
REPLY_TOKEN,
'https://example.com/original.jpg'
);
expect(res).toEqual(reply);
});
it('should call reply api with object image arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImage(REPLY_TOKEN, {
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'image',
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImage(
REPLY_TOKEN,
{
originalContentUrl: 'https://example.com/original.jpg',
previewImageUrl: 'https://example.com/preview.jpg',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replyVideo', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'video',
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyVideo(
REPLY_TOKEN,
'https://example.com/original.mp4',
'https://example.com/preview.jpg'
);
expect(res).toEqual(reply);
});
it('should call reply api with object video arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'video',
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyVideo(REPLY_TOKEN, {
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'video',
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyVideo(
REPLY_TOKEN,
{
originalContentUrl: 'https://example.com/original.mp4',
previewImageUrl: 'https://example.com/preview.jpg',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replyAudio', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'audio',
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyAudio(
REPLY_TOKEN,
'https://example.com/original.m4a',
240000
);
expect(res).toEqual(reply);
});
it('should call reply api with object audio arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'audio',
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyAudio(REPLY_TOKEN, {
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'audio',
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyAudio(
REPLY_TOKEN,
{
originalContentUrl: 'https://example.com/original.m4a',
duration: 240000,
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replyLocation', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'location',
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyLocation(REPLY_TOKEN, {
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'location',
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyLocation(
REPLY_TOKEN,
{
title: 'my location',
address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
latitude: 35.65910807942215,
longitude: 139.70372892916203,
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replySticker', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'sticker',
packageId: '1',
stickerId: '1',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replySticker(REPLY_TOKEN, '1', '1');
expect(res).toEqual(reply);
});
it('should call reply api with object sticker arg', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'sticker',
packageId: '1',
stickerId: '1',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replySticker(REPLY_TOKEN, {
packageId: '1',
stickerId: '1',
});
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'sticker',
packageId: '1',
stickerId: '1',
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replySticker(
REPLY_TOKEN,
{
packageId: '1',
stickerId: '1',
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replyImagemap', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImagemap(
REPLY_TOKEN,
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseHeight: 1040,
baseWidth: 1040,
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImagemap(
REPLY_TOKEN,
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseHeight: 1040,
baseWidth: 1040,
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
it('should support baseSize argument', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImagemap(
REPLY_TOKEN,
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
height: 1040,
width: 1040,
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
}
);
expect(res).toEqual(reply);
});
it('should support video', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'imagemap',
baseUrl: 'https://example.com/bot/images/rm001',
altText: 'this is an imagemap',
baseSize: {
height: 1040,
width: 1040,
},
video: {
originalContentUrl: 'https://example.com/video.mp4',
previewImageUrl: 'https://example.com/video_preview.jpg',
area: {
x: 0,
y: 0,
width: 1040,
height: 585,
},
externalLink: {
linkUri: 'https://example.com/see_more.html',
label: 'See More',
},
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImagemap(
REPLY_TOKEN,
'this is an imagemap',
{
baseUrl: 'https://example.com/bot/images/rm001',
baseSize: {
height: 1040,
width: 1040,
},
video: {
originalContentUrl: 'https://example.com/video.mp4',
previewImageUrl: 'https://example.com/video_preview.jpg',
area: {
x: 0,
y: 0,
width: 1040,
height: 585,
},
externalLink: {
linkUri: 'https://example.com/see_more.html',
label: 'See More',
},
},
actions: [
{
type: 'uri',
linkUri: 'https://example.com/',
area: {
x: 0,
y: 0,
width: 520,
height: 1040,
},
},
{
type: 'message',
text: 'hello',
area: {
x: 520,
y: 0,
width: 520,
height: 1040,
},
},
],
}
);
expect(res).toEqual(reply);
});
});
describe('#replyTemplate', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyTemplate(
REPLY_TOKEN,
'this is a template',
{
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyTemplate(
REPLY_TOKEN,
'this is a template',
{
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replyButtonTemplate', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyButtonTemplate(
REPLY_TOKEN,
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyButtonTemplate(
REPLY_TOKEN,
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
defaultAction: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
it('should support replyButtonsTemplate alias', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyButtonsTemplate(
REPLY_TOKEN,
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
}
);
expect(res).toEqual(reply);
});
it('replyButtonsTemplate alias should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a template',
template: {
type: 'buttons',
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyButtonsTemplate(
REPLY_TOKEN,
'this is a template',
{
thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
imageBackgroundColor: '#FFFFFF',
title: 'Menu',
text: 'Please select',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=123',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=123',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/123',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replyConfirmTemplate', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyConfirmTemplate(
REPLY_TOKEN,
'this is a confirm template',
{
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyConfirmTemplate(
REPLY_TOKEN,
'this is a confirm template',
{
text: 'Are you sure?',
actions: [
{
type: 'message',
label: 'Yes',
text: 'yes',
},
{
type: 'message',
label: 'No',
text: 'no',
},
],
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replyCarouselTemplate', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
columns: [
{
thumbnailImageUrl:
'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl:
'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyCarouselTemplate(
REPLY_TOKEN,
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
{
imageAspectRatio: 'rectangle',
imageSize: 'cover',
}
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
imageAspectRatio: 'rectangle',
imageSize: 'cover',
columns: [
{
thumbnailImageUrl:
'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl:
'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyCarouselTemplate(
REPLY_TOKEN,
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
{
accessToken: CUSTOM_ACCESS_TOKEN,
imageAspectRatio: 'rectangle',
imageSize: 'cover',
}
);
expect(res).toEqual(reply);
});
it('should work without option', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is a carousel template',
template: {
type: 'carousel',
columns: [
{
thumbnailImageUrl:
'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl:
'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyCarouselTemplate(
REPLY_TOKEN,
'this is a carousel template',
[
{
thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=111',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/111',
},
],
},
{
thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
title: 'this is menu',
text: 'description',
actions: [
{
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=222',
},
{
type: 'postback',
label: 'Add to cart',
data: 'action=add&itemid=222',
},
{
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
],
},
]
);
expect(res).toEqual(reply);
});
});
describe('#replyImageCarouselTemplate', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is an image carousel template',
template: {
type: 'image_carousel',
columns: [
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImageCarouselTemplate(
REPLY_TOKEN,
'this is an image carousel template',
[
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
]
);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'template',
altText: 'this is an image carousel template',
template: {
type: 'image_carousel',
columns: [
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyImageCarouselTemplate(
REPLY_TOKEN,
'this is an image carousel template',
[
{
imageUrl: 'https://example.com/bot/images/item1.jpg',
action: {
type: 'postback',
label: 'Buy',
data: 'action=buy&itemid=111',
},
},
{
imageUrl: 'https://example.com/bot/images/item2.jpg',
action: {
type: 'message',
label: 'Yes',
text: 'yes',
},
},
{
imageUrl: 'https://example.com/bot/images/item3.jpg',
action: {
type: 'uri',
label: 'View detail',
uri: 'http://example.com/page/222',
},
},
],
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
describe('#replyFlex', () => {
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'flex',
altText: 'this is a flex message',
contents: {
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyFlex(
REPLY_TOKEN,
'this is a flex message',
{
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
}
);
expect(res).toEqual(reply);
});
it('should call reply api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual('https://api.line.me/v2/bot/message/reply');
expect(JSON.parse(config.data)).toEqual({
replyToken: REPLY_TOKEN,
messages: [
{
type: 'flex',
altText: 'this is a flex message',
contents: {
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
},
],
});
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.replyFlex(
REPLY_TOKEN,
'this is a flex message',
{
type: 'bubble',
header: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Header text',
},
],
},
hero: {
type: 'image',
url: 'https://example.com/flex/images/image.jpg',
},
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Body text',
},
],
},
footer: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
text: 'Footer text',
},
],
},
styles: {
comment: 'See the example of a bubble style object',
},
},
{ accessToken: CUSTOM_ACCESS_TOKEN }
);
expect(res).toEqual(reply);
});
});
});
import MockAdapter from 'axios-mock-adapter';
import LineClient from '../LineClient';
const RECIPIENT_ID = '1QAZ2WSX';
const GROUP_ID = 'G1QAZ2WSX';
const ROOM_ID = 'R1QAZ2WSX';
const CUSTOM_ACCESS_TOKEN = '555555555';
const ACCESS_TOKEN = '1234567890';
const CHANNEL_SECRET = 'so-secret';
const createMock = ({ customAccessToken } = {}) => {
const client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
const mock = new MockAdapter(client.axios);
const headers = {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
Authorization: `Bearer ${customAccessToken || ACCESS_TOKEN}`,
};
return { client, mock, headers };
};
describe('Group/Room Member', () => {
describe('#getGroupMemberProfile', () => {
it('should response group member profile', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
displayName: 'LINE taro',
userId: RECIPIENT_ID,
pictureUrl: 'http://obs.line-apps.com/...',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/member/${RECIPIENT_ID}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getGroupMemberProfile(GROUP_ID, RECIPIENT_ID);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
displayName: 'LINE taro',
userId: RECIPIENT_ID,
pictureUrl: 'http://obs.line-apps.com/...',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/member/${RECIPIENT_ID}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getGroupMemberProfile(GROUP_ID, RECIPIENT_ID, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#getRoomMemberProfile', () => {
it('should response room member profile', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
displayName: 'LINE taro',
userId: RECIPIENT_ID,
pictureUrl: 'http://obs.line-apps.com/...',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/member/${RECIPIENT_ID}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getRoomMemberProfile(ROOM_ID, RECIPIENT_ID);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
displayName: 'LINE taro',
userId: RECIPIENT_ID,
pictureUrl: 'http://obs.line-apps.com/...',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/member/${RECIPIENT_ID}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getRoomMemberProfile(ROOM_ID, RECIPIENT_ID, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#getGroupMemberIds', () => {
it('should response group member ids', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
memberIds: [
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/members/ids`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getGroupMemberIds(GROUP_ID);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
memberIds: [
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/members/ids`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getGroupMemberIds(GROUP_ID, null, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
it('should call api with provided continuationToken', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
memberIds: [
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
],
};
const continuationToken = 'TOKEN';
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/members/ids?start=${continuationToken}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getGroupMemberIds(GROUP_ID, continuationToken);
expect(res).toEqual(reply);
});
});
describe('#getAllGroupMemberIds', () => {
it('should fetch all member ids until it is finished', async () => {
expect.assertions(7);
const { client, mock, headers } = createMock();
const continuationToken = 'TOKEN';
const reply1 = {
memberIds: [
'Uxxxxxxxxxxxxxx..1',
'Uxxxxxxxxxxxxxx..2',
'Uxxxxxxxxxxxxxx..3',
],
next: continuationToken,
};
const reply2 = {
memberIds: [
'Uxxxxxxxxxxxxxx..4',
'Uxxxxxxxxxxxxxx..5',
'Uxxxxxxxxxxxxxx..6',
],
};
mock
.onGet(`/v2/bot/group/${GROUP_ID}/members/ids`)
.replyOnce(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/members/ids`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply1];
})
.onGet(
`/v2/bot/group/${GROUP_ID}/members/ids?start=${continuationToken}`
)
.replyOnce(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/members/ids?start=${continuationToken}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply2];
});
const res = await client.getAllGroupMemberIds(GROUP_ID);
expect(res).toEqual([
'Uxxxxxxxxxxxxxx..1',
'Uxxxxxxxxxxxxxx..2',
'Uxxxxxxxxxxxxxx..3',
'Uxxxxxxxxxxxxxx..4',
'Uxxxxxxxxxxxxxx..5',
'Uxxxxxxxxxxxxxx..6',
]);
});
it('should work with custom access token', async () => {
expect.assertions(7);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const continuationToken = 'TOKEN';
const reply1 = {
memberIds: [
'Uxxxxxxxxxxxxxx..1',
'Uxxxxxxxxxxxxxx..2',
'Uxxxxxxxxxxxxxx..3',
],
next: continuationToken,
};
const reply2 = {
memberIds: [
'Uxxxxxxxxxxxxxx..4',
'Uxxxxxxxxxxxxxx..5',
'Uxxxxxxxxxxxxxx..6',
],
};
mock
.onGet(`/v2/bot/group/${GROUP_ID}/members/ids`)
.replyOnce(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/members/ids`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply1];
})
.onGet(
`/v2/bot/group/${GROUP_ID}/members/ids?start=${continuationToken}`
)
.replyOnce(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/members/ids?start=${continuationToken}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply2];
});
const res = await client.getAllGroupMemberIds(GROUP_ID, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual([
'Uxxxxxxxxxxxxxx..1',
'Uxxxxxxxxxxxxxx..2',
'Uxxxxxxxxxxxxxx..3',
'Uxxxxxxxxxxxxxx..4',
'Uxxxxxxxxxxxxxx..5',
'Uxxxxxxxxxxxxxx..6',
]);
});
});
describe('#getRoomMemberIds', () => {
it('should response room member ids', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
memberIds: [
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/members/ids`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getRoomMemberIds(ROOM_ID);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
memberIds: [
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
],
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/members/ids`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getRoomMemberIds(ROOM_ID, null, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
it('should call api with provided continuationToken', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
memberIds: [
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
'Uxxxxxxxxxxxxxx...',
],
};
const continuationToken = 'TOKEN';
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/members/ids?start=${continuationToken}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getRoomMemberIds(ROOM_ID, continuationToken);
expect(res).toEqual(reply);
});
});
describe('#getAllRoomMemberIds', () => {
it('should fetch all member ids until it is finished', async () => {
expect.assertions(7);
const { client, mock, headers } = createMock();
const continuationToken = 'TOKEN';
const reply1 = {
memberIds: [
'Uxxxxxxxxxxxxxx..1',
'Uxxxxxxxxxxxxxx..2',
'Uxxxxxxxxxxxxxx..3',
],
next: continuationToken,
};
const reply2 = {
memberIds: [
'Uxxxxxxxxxxxxxx..4',
'Uxxxxxxxxxxxxxx..5',
'Uxxxxxxxxxxxxxx..6',
],
};
mock
.onGet()
.replyOnce(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/members/ids`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply1];
})
.onGet()
.replyOnce(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/members/ids?start=${continuationToken}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply2];
});
const res = await client.getAllRoomMemberIds(ROOM_ID);
expect(res).toEqual([
'Uxxxxxxxxxxxxxx..1',
'Uxxxxxxxxxxxxxx..2',
'Uxxxxxxxxxxxxxx..3',
'Uxxxxxxxxxxxxxx..4',
'Uxxxxxxxxxxxxxx..5',
'Uxxxxxxxxxxxxxx..6',
]);
});
it('should work with custom access token', async () => {
expect.assertions(7);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const continuationToken = 'TOKEN';
const reply1 = {
memberIds: [
'Uxxxxxxxxxxxxxx..1',
'Uxxxxxxxxxxxxxx..2',
'Uxxxxxxxxxxxxxx..3',
],
next: continuationToken,
};
const reply2 = {
memberIds: [
'Uxxxxxxxxxxxxxx..4',
'Uxxxxxxxxxxxxxx..5',
'Uxxxxxxxxxxxxxx..6',
],
};
mock
.onGet()
.replyOnce(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/members/ids`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply1];
})
.onGet()
.replyOnce(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/members/ids?start=${continuationToken}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply2];
});
const res = await client.getAllRoomMemberIds(ROOM_ID, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual([
'Uxxxxxxxxxxxxxx..1',
'Uxxxxxxxxxxxxxx..2',
'Uxxxxxxxxxxxxxx..3',
'Uxxxxxxxxxxxxxx..4',
'Uxxxxxxxxxxxxxx..5',
'Uxxxxxxxxxxxxxx..6',
]);
});
});
});
describe('Leave', () => {
describe('#leaveGroup', () => {
it('should call leave api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/leave`
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.leaveGroup(GROUP_ID);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/group/${GROUP_ID}/leave`
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.leaveGroup(GROUP_ID, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
describe('#leaveRoom', () => {
it('should call leave api', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/leave`
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.leaveRoom(ROOM_ID);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {};
mock.onPost().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/room/${ROOM_ID}/leave`
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.leaveRoom(ROOM_ID, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
});
import MockAdapter from 'axios-mock-adapter';
import LineClient from '../LineClient';
const RECIPIENT_ID = '1QAZ2WSX';
const REPLY_TOKEN = 'nHuyWiB7yP5Zw52FIkcQobQuGDXCTA';
const CUSTOM_ACCESS_TOKEN = '555555555';
const ACCESS_TOKEN = '1234567890';
const CHANNEL_SECRET = 'so-secret';
const createMock = ({ customAccessToken } = {}) => {
const client = new LineClient(ACCESS_TOKEN, CHANNEL_SECRET);
const mock = new MockAdapter(client.axios);
const headers = {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
Authorization: `Bearer ${customAccessToken || ACCESS_TOKEN}`,
};
return { client, mock, headers };
};
describe('Content', () => {
describe('#retrieveMessageContent', () => {
it('should call retrieveMessageContent api', async () => {
const { client, mock } = createMock();
const reply = Buffer.from('a content buffer');
const MESSAGE_ID = '1234567890';
mock.onGet(`/v2/bot/message/${MESSAGE_ID}/content`).reply(200, reply);
const res = await client.retrieveMessageContent(MESSAGE_ID);
expect(res).toEqual(reply);
});
});
});
describe('Profile', () => {
describe('#getUserProfile', () => {
it('should response user profile', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
displayName: 'LINE taro',
userId: RECIPIENT_ID,
pictureUrl: 'http://obs.line-apps.com/...',
statusMessage: 'Hello, LINE!',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/profile/${RECIPIENT_ID}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getUserProfile(RECIPIENT_ID);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
displayName: 'LINE taro',
userId: RECIPIENT_ID,
pictureUrl: 'http://obs.line-apps.com/...',
statusMessage: 'Hello, LINE!',
};
mock.onGet().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/profile/${RECIPIENT_ID}`
);
expect(config.data).toEqual(undefined);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.getUserProfile(RECIPIENT_ID, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
it('should return null when no user found', async () => {
const { client, mock } = createMock();
mock.onGet().reply(404, {
message: 'Not found',
});
const res = await client.getUserProfile(RECIPIENT_ID);
expect(res).toEqual(null);
});
});
});
describe('Account link', () => {
describe('#issueLinkToken', () => {
it('should response data with link token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock();
const reply = {
linkToken: 'NMZTNuVrPTqlr2IF8Bnymkb7rXfYv5EY',
};
mock.onPost().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/user/${RECIPIENT_ID}/linkToken`
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.issueLinkToken(RECIPIENT_ID);
expect(res).toEqual(reply);
});
it('should work with custom access token', async () => {
expect.assertions(4);
const { client, mock, headers } = createMock({
customAccessToken: CUSTOM_ACCESS_TOKEN,
});
const reply = {
linkToken: 'NMZTNuVrPTqlr2IF8Bnymkb7rXfYv5EY',
};
mock.onPost().reply(config => {
expect(config.url).toEqual(
`https://api.line.me/v2/bot/user/${RECIPIENT_ID}/linkToken`
);
expect(config.data).toEqual(null);
expect(config.headers).toEqual(headers);
return [200, reply];
});
const res = await client.issueLinkToken(RECIPIENT_ID, {
accessToken: CUSTOM_ACCESS_TOKEN,
});
expect(res).toEqual(reply);
});
});
});
describe('Error', () => {
it('should format correctly when no details', async () => {
const { client, mock } = createMock();
const reply = {
message: 'The request body has 2 error(s)',
};
mock.onAny().reply(400, reply);
let error;
try {
await client.replyText(REPLY_TOKEN, 'Hello!');
} catch (err) {
error = err;
}
expect(error.message).toEqual('LINE API - The request body has 2 error(s)');
});
it('should format correctly when details exist', async () => {
const { client, mock } = createMock();
const reply = {
message: 'The request body has 2 error(s)',
details: [
{ message: 'May not be empty', property: 'messages[0].text' },
{
message:
'Must be one of the following values: [text, image, video, audio, location, sticker, template, imagemap]',
property: 'messages[1].type',
},
],
};
mock.onAny().reply(400, reply);
let error;
try {
await client.replyText(REPLY_TOKEN, 'Hello!');
} catch (err) {
error = err;
}
expect(error.message).toEqual(`LINE API - The request body has 2 error(s)
- messages[0].text: May not be empty
- messages[1].type: Must be one of the following values: [text, image, video, audio, location, sticker, template, imagemap]`);
});
});
import MockAdapter from 'axios-mock-adapter';
import LinePay from '../LinePay';
const CHANNEL_ID = '1234567890';
const CHANNEL_SECRET = 'so-secret';
const createMock = () => {
const client = new LinePay({
channelId: CHANNEL_ID,
channelSecret: CHANNEL_SECRET,
});
const mock = new MockAdapter(client.axios);
return { client, mock };
};
describe('connect', () => {
let axios;
let _create;
beforeEach(() => {
axios = require('axios'); // eslint-disable-line global-require
_create = axios.create;
});
afterEach(() => {
axios.create = _create;
});
it('create axios with LINE PAY API', () => {
axios.create = jest.fn();
LinePay.connect({
channelId: CHANNEL_ID,
channelSecret: CHANNEL_SECRET,
});
expect(axios.create).toBeCalledWith({
baseURL: 'https://api-pay.line.me/v2/',
headers: {
'Content-Type': 'application/json',
'X-LINE-ChannelId': CHANNEL_ID,
'X-LINE-ChannelSecret': CHANNEL_SECRET,
},
});
});
});
describe('constructor', () => {
let axios;
let _create;
beforeEach(() => {
axios = require('axios'); // eslint-disable-line global-require
_create = axios.create;
});
afterEach(() => {
axios.create = _create;
});
it('create axios with LINE PAY API', () => {
axios.create = jest.fn();
// eslint-disable-next-line no-new
new LinePay({
channelId: CHANNEL_ID,
channelSecret: CHANNEL_SECRET,
});
expect(axios.create).toBeCalledWith({
baseURL: 'https://api-pay.line.me/v2/',
headers: {
'Content-Type': 'application/json',
'X-LINE-ChannelId': CHANNEL_ID,
'X-LINE-ChannelSecret': CHANNEL_SECRET,
},
});
});
it('support sandbox', () => {
axios.create = jest.fn();
// eslint-disable-next-line no-new
new LinePay({
channelId: CHANNEL_ID,
channelSecret: CHANNEL_SECRET,
sandbox: true,
});
expect(axios.create).toBeCalledWith({
baseURL: 'https://sandbox-api-pay.line.me/v2/',
headers: {
'Content-Type': 'application/json',
'X-LINE-ChannelId': CHANNEL_ID,
'X-LINE-ChannelSecret': CHANNEL_SECRET,
},
});
});
it('support origin', () => {
axios.create = jest.fn();
// eslint-disable-next-line no-new
new LinePay({
channelId: CHANNEL_ID,
channelSecret: CHANNEL_SECRET,
origin: 'https://mydummytestserver.com',
});
expect(axios.create).toBeCalledWith({
baseURL: 'https://mydummytestserver.com/v2/',
headers: {
'Content-Type': 'application/json',
'X-LINE-ChannelId': CHANNEL_ID,
'X-LINE-ChannelSecret': CHANNEL_SECRET,
},
});
});
});
describe('#axios', () => {
it('should return underlying http client', () => {
const client = new LinePay({
channelId: CHANNEL_ID,
channelSecret: CHANNEL_SECRET,
});
expect(client.axios.get).toBeDefined();
expect(client.axios.post).toBeDefined();
expect(client.axios.put).toBeDefined();
expect(client.axios.delete).toBeDefined();
});
});
describe('#getPayments', () => {
const reply = {
returnCode: '0000',
returnMessage: 'success',
info: [
{
transactionId: 1020140728100001997,
transactionDate: '2014-07-28T09:48:43Z',
transactionType: 'PARTIAL_REFUND',
amount: -5,
productName: '',
currency: 'USD',
orderId: '20140101123123123',
originalTransactionId: 1020140728100001999,
},
],
};
it('should work with transactionId and orderId', async () => {
const { client, mock } = createMock();
mock
.onGet('/payments?transactionId=20140101123123123&orderId=1002045572')
.reply(200, reply);
const result = await client.getPayments({
transactionId: '20140101123123123',
orderId: '1002045572',
});
expect(result).toEqual(reply.info);
});
it('should work with only transactionId', async () => {
const { client, mock } = createMock();
mock.onGet('/payments?transactionId=20140101123123123').reply(200, reply);
const result = await client.getPayments({
transactionId: '20140101123123123',
});
expect(result).toEqual(reply.info);
});
it('should work with only orderId', async () => {
const { client, mock } = createMock();
mock.onGet('/payments?orderId=1002045572').reply(200, reply);
const result = await client.getPayments({
orderId: '1002045572',
});
expect(result).toEqual(reply.info);
});
it('should throw without any id', async () => {
const { client } = createMock();
await expect(() => client.getPayments()).toThrow(
/One of `transactionId` or `orderId` must be provided/
);
});
it('should throw when not success', async () => {
const { client, mock } = createMock();
mock.onGet('/payments?orderId=1002045572').reply(200, {
returnCode: '1104',
returnMessage: 'merchant not found',
});
return expect(
client.getPayments({
orderId: '1002045572',
})
).rejects.toThrow('LINE PAY API - 1104 merchant not found');
});
});
describe('#getAuthorizations', () => {
const reply = {
returnCode: '0000',
returnMessage: 'success',
info: [
{
transactionId: 201612312312333401,
transactionDate: '2014-07-28T09:48:43Z',
transactionType: 'PAYMENT',
payInfo: [
{
method: 'BALANCE',
amount: 10,
},
{
method: 'DISCOUNT',
amount: 10,
},
],
productName: 'tes production',
currency: 'USD',
orderId: '20140101123123123',
payStatus: 'AUTHORIZATION',
authorizationExpireDate: '2014-07-28T09:48:43Z',
},
],
};
it('should work with transactionId and orderId', async () => {
const { client, mock } = createMock();
mock
.onGet(
'/payments/authorizations?transactionId=20140101123123123&orderId=1002045572'
)
.reply(200, reply);
const result = await client.getAuthorizations({
transactionId: '20140101123123123',
orderId: '1002045572',
});
expect(result).toEqual(reply.info);
});
it('should work with only transactionId', async () => {
const { client, mock } = createMock();
mock
.onGet('/payments/authorizations?transactionId=20140101123123123')
.reply(200, reply);
const result = await client.getAuthorizations({
transactionId: '20140101123123123',
});
expect(result).toEqual(reply.info);
});
it('should work with only orderId', async () => {
const { client, mock } = createMock();
mock.onGet('/payments/authorizations?orderId=1002045572').reply(200, reply);
const result = await client.getAuthorizations({
orderId: '1002045572',
});
expect(result).toEqual(reply.info);
});
it('should throw without any id', async () => {
const { client } = createMock();
await expect(() => client.getAuthorizations()).toThrow(
/One of `transactionId` or `orderId` must be provided/
);
});
it('should throw when not success', async () => {
const { client, mock } = createMock();
mock.onGet('/payments/authorizations?orderId=1002045572').reply(200, {
returnCode: '1104',
returnMessage: 'merchant not found',
});
return expect(
client.getAuthorizations({
orderId: '1002045572',
})
).rejects.toThrow('LINE PAY API - 1104 merchant not found');
});
});
describe('#reserve', () => {
it('should call reserve api', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '0000',
returnMessage: 'OK',
info: {
transactionId: 123123123123,
paymentUrl: {
web: 'http://web-pay.line.me/web/wait?transactionReserveId=blahblah',
app: 'line://pay/payment/blahblah',
},
paymentAccessToken: '187568751124',
},
};
mock
.onPost('/payments/request', {
productName: 'test product',
productImageUrl: 'http://testst.com',
amount: 10,
currency: 'USD',
mid: 'os89dufgoiw8yer9021384rdfeq',
orderId: '20140101123456789',
confirmUrl:
'naversearchapp://inappbrowser?url=http%3A%2F%2FtestMall.com%2FcheckResult.nhn%3ForderId%3D20140101123456789',
cancelUrl:
'naversearchapp://inappbrowser?url=http%3A%2F%2FtestMall.com%2ForderSheet.nhn%3ForderId%3D20140101123456789',
capture: 'true',
confirmUrlType: 'CLIENT',
extras: {
addFriends: [
{
type: 'LINE_AT',
idList: ['@aaa', '@bbb'],
},
],
branchName: 'test_branch_1',
},
})
.reply(200, reply);
const result = await client.reserve({
productName: 'test product',
productImageUrl: 'http://testst.com',
amount: 10,
currency: 'USD',
mid: 'os89dufgoiw8yer9021384rdfeq',
orderId: '20140101123456789',
confirmUrl:
'naversearchapp://inappbrowser?url=http%3A%2F%2FtestMall.com%2FcheckResult.nhn%3ForderId%3D20140101123456789',
cancelUrl:
'naversearchapp://inappbrowser?url=http%3A%2F%2FtestMall.com%2ForderSheet.nhn%3ForderId%3D20140101123456789',
capture: 'true',
confirmUrlType: 'CLIENT',
extras: {
addFriends: [
{
type: 'LINE_AT',
idList: ['@aaa', '@bbb'],
},
],
branchName: 'test_branch_1',
},
});
expect(result).toEqual({
transactionId: 123123123123,
paymentUrl: {
web: 'http://web-pay.line.me/web/wait?transactionReserveId=blahblah',
app: 'line://pay/payment/blahblah',
},
paymentAccessToken: '187568751124',
});
});
it('should throw when not success', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '1104',
returnMessage: 'merchant not found',
info: {
transactionId: 123123123123,
paymentUrl: {
web: 'http://web-pay.line.me/web/wait?transactionReserveId=blahblah',
app: 'line://pay/payment/blahblah',
},
paymentAccessToken: '187568751124',
},
};
mock
.onPost('/payments/request', {
productName: 'test product',
productImageUrl: 'http://testst.com',
amount: 10,
currency: 'USD',
mid: 'os89dufgoiw8yer9021384rdfeq',
orderId: '20140101123456789',
confirmUrl:
'naversearchapp://inappbrowser?url=http%3A%2F%2FtestMall.com%2FcheckResult.nhn%3ForderId%3D20140101123456789',
cancelUrl:
'naversearchapp://inappbrowser?url=http%3A%2F%2FtestMall.com%2ForderSheet.nhn%3ForderId%3D20140101123456789',
capture: 'true',
confirmUrlType: 'CLIENT',
extras: {
addFriends: [
{
type: 'LINE_AT',
idList: ['@aaa', '@bbb'],
},
],
branchName: 'test_branch_1',
},
})
.reply(200, reply);
return expect(
client.reserve({
productName: 'test product',
productImageUrl: 'http://testst.com',
amount: 10,
currency: 'USD',
mid: 'os89dufgoiw8yer9021384rdfeq',
orderId: '20140101123456789',
confirmUrl:
'naversearchapp://inappbrowser?url=http%3A%2F%2FtestMall.com%2FcheckResult.nhn%3ForderId%3D20140101123456789',
cancelUrl:
'naversearchapp://inappbrowser?url=http%3A%2F%2FtestMall.com%2ForderSheet.nhn%3ForderId%3D20140101123456789',
capture: 'true',
confirmUrlType: 'CLIENT',
extras: {
addFriends: [
{
type: 'LINE_AT',
idList: ['@aaa', '@bbb'],
},
],
branchName: 'test_branch_1',
},
})
).rejects.toThrow('LINE PAY API - 1104 merchant not found');
});
});
describe('#confirm', () => {
it('should call confirm api', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '0000',
returnMessage: 'OK',
info: {
orderId: 'order_210124213',
transactionId: 20140101123123123,
payInfo: [
{
method: 'BALANCE',
amount: 10,
},
{
method: 'DISCOUNT',
amount: 10,
},
],
},
};
mock
.onPost('/payments/sdhqiwouehrafdasrqoi123as/confirm', {
amount: 1000,
currency: 'TWD',
})
.reply(200, reply);
const result = await client.confirm('sdhqiwouehrafdasrqoi123as', {
amount: 1000,
currency: 'TWD',
});
expect(result).toEqual({
orderId: 'order_210124213',
transactionId: 20140101123123123,
payInfo: [
{
method: 'BALANCE',
amount: 10,
},
{
method: 'DISCOUNT',
amount: 10,
},
],
});
});
it('should throw when not success', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '1104',
returnMessage: 'merchant not found',
info: {
orderId: 'order_210124213',
transactionId: 20140101123123123,
payInfo: [
{
method: 'BALANCE',
amount: 10,
},
{
method: 'DISCOUNT',
amount: 10,
},
],
},
};
mock
.onPost('/payments/sdhqiwouehrafdasrqoi123as/confirm', {
amount: 1000,
currency: 'TWD',
})
.reply(200, reply);
return expect(
client.confirm('sdhqiwouehrafdasrqoi123as', {
amount: 1000,
currency: 'TWD',
})
).rejects.toThrow('LINE PAY API - 1104 merchant not found');
});
});
describe('#capture', () => {
it('should call capture api', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '0000',
returnMessage: 'OK',
info: {
transactionId: 20140101123123123,
orderId: 'order_210124213',
payInfo: [
{
method: 'BALANCE',
amount: 10,
},
{
method: 'DISCOUNT',
amount: 10,
},
],
},
};
mock
.onPost('/payments/authorizations/sdhqiwouehrafdasrqoi123as/capture', {
amount: 1000,
currency: 'TWD',
})
.reply(200, reply);
const result = await client.capture('sdhqiwouehrafdasrqoi123as', {
amount: 1000,
currency: 'TWD',
});
expect(result).toEqual({
transactionId: 20140101123123123,
orderId: 'order_210124213',
payInfo: [
{
method: 'BALANCE',
amount: 10,
},
{
method: 'DISCOUNT',
amount: 10,
},
],
});
});
it('should throw when not success', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '1104',
returnMessage: 'merchant not found',
info: {
transactionId: 20140101123123123,
orderId: 'order_210124213',
payInfo: [
{
method: 'BALANCE',
amount: 10,
},
{
method: 'DISCOUNT',
amount: 10,
},
],
},
};
mock
.onPost('/payments/authorizations/sdhqiwouehrafdasrqoi123as/capture', {
amount: 1000,
currency: 'TWD',
})
.reply(200, reply);
return expect(
client.capture('sdhqiwouehrafdasrqoi123as', {
amount: 1000,
currency: 'TWD',
})
).rejects.toThrow('LINE PAY API - 1104 merchant not found');
});
});
describe('#void', () => {
it('should call void api', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '0000',
returnMessage: 'OK',
};
mock
.onPost('/payments/authorizations/sdhqiwouehrafdasrqoi123as/void')
.reply(200, reply);
const result = await client.void('sdhqiwouehrafdasrqoi123as');
expect(result).toBeUndefined();
});
it('should throw when not success', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '1104',
returnMessage: 'merchant not found',
};
mock
.onPost('/payments/authorizations/sdhqiwouehrafdasrqoi123as/void')
.reply(200, reply);
return expect(client.void('sdhqiwouehrafdasrqoi123as')).rejects.toThrow(
'LINE PAY API - 1104 merchant not found'
);
});
});
describe('#refund', () => {
it('should call refund api', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '0000',
returnMessage: 'success',
info: {
refundTransactionId: 123123123123,
refundTransactionDate: '2014-01-01T06:17:41Z',
},
};
mock.onPost('/payments/sdhqiwouehrafdasrqoi123as/refund').reply(200, reply);
const result = await client.refund('sdhqiwouehrafdasrqoi123as', {
refundAmount: 500,
});
expect(result).toEqual({
refundTransactionId: 123123123123,
refundTransactionDate: '2014-01-01T06:17:41Z',
});
});
it('should throw when not success', async () => {
const { client, mock } = createMock();
const reply = {
returnCode: '1104',
returnMessage: 'merchant not found',
info: {
refundTransactionId: 123123123123,
refundTransactionDate: '2014-01-01T06:17:41Z',
},
};
mock.onPost('/payments/sdhqiwouehrafdasrqoi123as/refund').reply(200, reply);
return expect(
client.refund('sdhqiwouehrafdasrqoi123as', {
refundAmount: 500,
})
).rejects.toThrow('LINE PAY API - 1104 merchant not found');
});
});
import { Line } from '../browser';
it('should export api correctly', () => {
expect(Line).toBeDefined();
});
import { Line, LineClient, LinePay } from '..';
it('should export api correctly', () => {
expect(Line).toBeDefined();
expect(LineClient).toBeDefined();
expect(LinePay).toBeDefined();
});
/* @flow */
import Line from './Line';
export { Line };
export default { Line };
/* @flow */
import Line from './Line';
import LineClient from './LineClient';
import LinePay from './LinePay';
export { Line, LineClient, LinePay };
export default { Line, LineClient, LinePay };
language: node_js
node_js:
- "5"
- "4"
- "0.12"
\ No newline at end of file
## 4.0.0 - 2018-02-02
- Ignore empty string arguments and throw an exception for non-string. Closes #36, #18 ([da05242f381bfe1ae09d00b708cfdbdb93c1a85d](https://github.com/jfromaniello/url-join/commit/da05242f381bfe1ae09d00b708cfdbdb93c1a85d)), closes [#36](https://github.com/jfromaniello/url-join/issues/36) [#18](https://github.com/jfromaniello/url-join/issues/18)
## 3.0.0 - 2018-01-12
- add new test ([d65d7c1696cb53b53ceabadf1a77917196967b4c](https://github.com/jfromaniello/url-join/commit/d65d7c1696cb53b53ceabadf1a77917196967b4c))
- Fixed to handle the colon in non-protocol separation role in the first part. ([9212db75f805031a9cc06120b5dd08a6cdd805e4](https://github.com/jfromaniello/url-join/commit/9212db75f805031a9cc06120b5dd08a6cdd805e4))
## 2.0.5 - 2018-01-10
- revert to previous behavior #30 ([b6943343af7bd723cbca266388e84e036543577d](https://github.com/jfromaniello/url-join/commit/b6943343af7bd723cbca266388e84e036543577d)), closes [#30](https://github.com/jfromaniello/url-join/issues/30)
## 2.0.4 - 2018-01-10
- fix bower.json ([9677895a4afe51d8a1d670980bc6fede71252e9a](https://github.com/jfromaniello/url-join/commit/9677895a4afe51d8a1d670980bc6fede71252e9a))
## 2.0.3 - 2018-01-09
- 2.0.3 ([7b7806b21cf81a3476e39ddb8a6f51272a276186](https://github.com/jfromaniello/url-join/commit/7b7806b21cf81a3476e39ddb8a6f51272a276186))
- Added a test for simple paths for issue #21 ([be99b10a707b4d22aac015d19eb087fff46d4270](https://github.com/jfromaniello/url-join/commit/be99b10a707b4d22aac015d19eb087fff46d4270)), closes [#21](https://github.com/jfromaniello/url-join/issues/21)
- Added some new tests for cases that fail. ([f1afbd62c3149476a9ef099ba523e85fb4839732](https://github.com/jfromaniello/url-join/commit/f1afbd62c3149476a9ef099ba523e85fb4839732))
- Passes all the tests with these changes. ([8cde667f400fa83efc7ed5c2437c7cb25c7d7600](https://github.com/jfromaniello/url-join/commit/8cde667f400fa83efc7ed5c2437c7cb25c7d7600))
- The protocol slashes should be normalized also when the protocol is not alone in the first argument. ([0ce1239c60f7bbb625d4ccbf1fcf044f37488bd8](https://github.com/jfromaniello/url-join/commit/0ce1239c60f7bbb625d4ccbf1fcf044f37488bd8))
## 2.0.2 - 2017-05-18
- fix: remove consecutives slashes ([33639364ef186e257b8424620017b9d1ba225539](https://github.com/jfromaniello/url-join/commit/33639364ef186e257b8424620017b9d1ba225539))
## 2.0.1 - 2017-04-12
- update mocha and bower.json ([ebd3665028b2408d405f9a31f8479e91c4ef52c1](https://github.com/jfromaniello/url-join/commit/ebd3665028b2408d405f9a31f8479e91c4ef52c1))
- feat: add test ([46d3387141e5d2f751da699e02d57fc36bfe37a8](https://github.com/jfromaniello/url-join/commit/46d3387141e5d2f751da699e02d57fc36bfe37a8))
- fix: ignore encoded url when removing consecusive slashes ([711add4e8af8fc97390adef14b9a4722cac5e70a](https://github.com/jfromaniello/url-join/commit/711add4e8af8fc97390adef14b9a4722cac5e70a))
## 2.0.0 - 2017-04-11
- Add a LICENSE file ([ffd3b2253470cee648152c55dd51c1bf4e688a60](https://github.com/jfromaniello/url-join/commit/ffd3b2253470cee648152c55dd51c1bf4e688a60))
- change copyright year ([9f67671dd8ab23b4d2da6ae775efdf66d594eac3](https://github.com/jfromaniello/url-join/commit/9f67671dd8ab23b4d2da6ae775efdf66d594eac3))
- refactor: use local startsWith function ([a1e1214644cd187f2584b79b4241ac3b8c9b9f1b](https://github.com/jfromaniello/url-join/commit/a1e1214644cd187f2584b79b4241ac3b8c9b9f1b))
- fix: split logic for files ([d7053a99aa40b0c2f4802819f7e0643be8889ac4](https://github.com/jfromaniello/url-join/commit/d7053a99aa40b0c2f4802819f7e0643be8889ac4))
- feat: add file protocol support ([48ebe0d84e8e2eca3a02fe5e3259cdd294e519dc](https://github.com/jfromaniello/url-join/commit/48ebe0d84e8e2eca3a02fe5e3259cdd294e519dc))
## 1.1.0 - 2016-04-05
- add .travis.yml ([c75e7507f72fd4be101b64bb44539fd249842cc0](https://github.com/jfromaniello/url-join/commit/c75e7507f72fd4be101b64bb44539fd249842cc0))
- added new syntax to allow options, fixed #! urls ([b8e5d8372c55187cdd9c6fa5e02830f76858347e](https://github.com/jfromaniello/url-join/commit/b8e5d8372c55187cdd9c6fa5e02830f76858347e))
- added travis, updated version in bower.json ([5a58405d89298e693e8f97a74b14324d83a8a87a](https://github.com/jfromaniello/url-join/commit/5a58405d89298e693e8f97a74b14324d83a8a87a))
- fixed query string handling, closes #9, closes #4 ([e190fe28282287204dbe7877979f18b4570042f9](https://github.com/jfromaniello/url-join/commit/e190fe28282287204dbe7877979f18b4570042f9)), closes [#9](https://github.com/jfromaniello/url-join/issues/9) [#4](https://github.com/jfromaniello/url-join/issues/4)
## 1.0.0 - 2016-03-23
## 0.1.0 - 2016-03-23
- 0.1.0 ([2db128d268dfd531f1af6c9bd0543458387e94cd](https://github.com/jfromaniello/url-join/commit/2db128d268dfd531f1af6c9bd0543458387e94cd))
- add support for AMD and windows['url-join'] ([b02169596877a1e6cd518f1b0d711f38c721fb02](https://github.com/jfromaniello/url-join/commit/b02169596877a1e6cd518f1b0d711f38c721fb02))
- added comments, fixed leading // ([3f72b6ea6fa84c4b254d0c656815a5df6b89a10a](https://github.com/jfromaniello/url-join/commit/3f72b6ea6fa84c4b254d0c656815a5df6b89a10a))
- added test for leading // ([baac627b2052e1d9b5c05e48c8dc6a05a80e08fa](https://github.com/jfromaniello/url-join/commit/baac627b2052e1d9b5c05e48c8dc6a05a80e08fa))
- bower init ([650dcfe72eee854108dd0832963553eae5ede7c5](https://github.com/jfromaniello/url-join/commit/650dcfe72eee854108dd0832963553eae5ede7c5))
- initial ([af68a208966de3d4be757c9d0f4a918c6dfa360e](https://github.com/jfromaniello/url-join/commit/af68a208966de3d4be757c9d0f4a918c6dfa360e))
- minor ([dde2dc6815f9a0476d7aade1d6848cbc5f3a14a4](https://github.com/jfromaniello/url-join/commit/dde2dc6815f9a0476d7aade1d6848cbc5f3a14a4))
- minor ([4d9d8ee16591da2092739a172145f968f71598dc](https://github.com/jfromaniello/url-join/commit/4d9d8ee16591da2092739a172145f968f71598dc))
- minor ([9ed0161497ee7d7d1b4b04d1735483a6216fe2c6](https://github.com/jfromaniello/url-join/commit/9ed0161497ee7d7d1b4b04d1735483a6216fe2c6))
- simplify normalize function ([d6886a362828eacc028c6167b9ae0efd8b2fbfc8](https://github.com/jfromaniello/url-join/commit/d6886a362828eacc028c6167b9ae0efd8b2fbfc8))
MIT License
Copyright (c) 2015 José F. Romaniello
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Join all arguments together and normalize the resulting url.
## Install
~~~
npm install url-join
~~~
## Usage
~~~javascript
var urljoin = require('url-join');
var fullUrl = urljoin('http://www.google.com', 'a', '/b/cd', '?foo=123');
console.log(fullUrl);
~~~
Prints:
~~~
'http://www.google.com/a/b/cd?foo=123'
~~~
## Browser and AMD
It also works in the browser, you can either include ```lib/url-join.js``` in your page:
~~~html
<script src="url-join.js"></script>
<script type="text/javascript">
urljoin('http://blabla.com', 'foo?a=1')
</script>
~~~
Or using an AMD module system like requirejs:
~~~javascript
define(['path/url-join.js'], function (urljoin) {
urljoin('http://blabla.com', 'foo?a=1');
});
~~~
## License
MIT
#!/usr/bin/env node
var changelog = require('conventional-changelog');
var semver_regex = /\bv?(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?\b/ig;
const commitPartial = ` - {{header}}
{{~!-- commit hash --}} {{#if @root.linkReferences}}([{{hash}}]({{#if @root.host}}{{@root.host}}/{{/if}}{{#if @root.owner}}{{@root.owner}}/{{/if}}{{@root.repository}}/{{@root.commit}}/{{hash}})){{else}}{{hash~}}{{/if}}
{{~!-- commit references --}}{{#if references}}, closes{{~#each references}} {{#if @root.linkReferences}}[{{#if this.owner}}{{this.owner}}/{{/if}}{{this.repository}}#{{this.issue}}]({{#if @root.host}}{{@root.host}}/{{/if}}{{#if this.repository}}{{#if this.owner}}{{this.owner}}/{{/if}}{{this.repository}}{{else}}{{#if @root.owner}}{{@root.owner}}/{{/if}}{{@root.repository}}{{/if}}/{{@root.issue}}/{{this.issue}}){{else}}{{#if this.owner}}{{this.owner}}/{{/if}}{{this.repository}}#{{this.issue}}{{/if}}{{/each}}{{/if}}
`;
const headerPartial = `## {{version}}{{#if title}} "{{title}}"{{/if}}{{#if date}} - {{date}}{{/if}}
`;
changelog({
releaseCount: 19,
// preset: 'jshint'
}, null, null, null, {
transform: function (commit) {
if (commit.header && semver_regex.exec(commit.header)) {
return null;
}
return commit;
},
commitPartial: commitPartial,
headerPartial: headerPartial
}).pipe(process.stdout);
(function (name, context, definition) {
if (typeof module !== 'undefined' && module.exports) module.exports = definition();
else if (typeof define === 'function' && define.amd) define(definition);
else context[name] = definition();
})('urljoin', this, function () {
function normalize (strArray) {
var resultArray = [];
if (strArray.length === 0) { return ''; }
if (typeof strArray[0] !== 'string') {
throw new TypeError('Url must be a string. Received ' + strArray[0]);
}
// If the first part is a plain protocol, we combine it with the next part.
if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) {
var first = strArray.shift();
strArray[0] = first + strArray[0];
}
// There must be two or three slashes in the file protocol, two slashes in anything else.
if (strArray[0].match(/^file:\/\/\//)) {
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///');
} else {
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://');
}
for (var i = 0; i < strArray.length; i++) {
var component = strArray[i];
if (typeof component !== 'string') {
throw new TypeError('Url must be a string. Received ' + component);
}
if (component === '') { continue; }
if (i > 0) {
// Removing the starting slashes for each component but the first.
component = component.replace(/^[\/]+/, '');
}
if (i < strArray.length - 1) {
// Removing the ending slashes for each component but the last.
component = component.replace(/[\/]+$/, '');
} else {
// For the last component we will combine multiple slashes to a single one.
component = component.replace(/[\/]+$/, '/');
}
resultArray.push(component);
}
var str = resultArray.join('/');
// Each input component is now separated by a single slash except the possible first plain protocol part.
// remove trailing slash before parameters or hash
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
// replace ? in parameters with &
var parts = str.split('?');
str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&');
return str;
}
return function () {
var input;
if (typeof arguments[0] === 'object') {
input = arguments[0];
} else {
input = [].slice.call(arguments);
}
return normalize(input);
};
});
{
"_from": "url-join@^4.0.1",
"_id": "url-join@4.0.1",
"_inBundle": false,
"_integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==",
"_location": "/url-join",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "url-join@^4.0.1",
"name": "url-join",
"escapedName": "url-join",
"rawSpec": "^4.0.1",
"saveSpec": null,
"fetchSpec": "^4.0.1"
},
"_requiredBy": [
"/messaging-api-line"
],
"_resolved": "https://registry.npmjs.org/url-join/-/url-join-4.0.1.tgz",
"_shasum": "b642e21a2646808ffa178c4c5fda39844e12cde7",
"_spec": "url-join@^4.0.1",
"_where": "C:\\Users\\김수민\\Desktop\\ostt\\LINEBOT\\node_modules\\messaging-api-line",
"author": {
"name": "José F. Romaniello",
"email": "jfromaniello@gmail.com",
"url": "http://joseoncode.com"
},
"bugs": {
"url": "https://github.com/jfromaniello/url-join/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Join urls and normalize as in path.join.",
"devDependencies": {
"conventional-changelog": "^1.1.10",
"mocha": "^3.2.0",
"should": "~1.2.1"
},
"homepage": "https://github.com/jfromaniello/url-join#readme",
"keywords": [
"url",
"join"
],
"license": "MIT",
"main": "lib/url-join.js",
"name": "url-join",
"repository": {
"type": "git",
"url": "git://github.com/jfromaniello/url-join.git"
},
"scripts": {
"test": "mocha --require should"
},
"version": "4.0.1"
}
var urljoin = require('../lib/url-join');
var assert = require('assert');
describe('url join', function () {
it('should work for simple case', function () {
urljoin('http://www.google.com/', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should work for simple case with new syntax', function () {
urljoin(['http://www.google.com/', 'foo/bar', '?test=123'])
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should work for hashbang urls', function () {
urljoin(['http://www.google.com', '#!', 'foo/bar', '?test=123'])
.should.eql('http://www.google.com/#!/foo/bar?test=123');
});
it('should be able to join protocol', function () {
urljoin('http:', 'www.google.com/', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should be able to join protocol with slashes', function () {
urljoin('http://', 'www.google.com/', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should remove extra slashes', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should not remove extra slashes in an encoded URL', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?url=http%3A//Ftest.com')
.should.eql('http://www.google.com/foo/bar?url=http%3A//Ftest.com');
urljoin('http://a.com/23d04b3/', '/b/c.html')
.should.eql('http://a.com/23d04b3/b/c.html')
.should.not.eql('http://a.com/23d04b3//b/c.html');
});
it('should support anchors in urls', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '#faaaaa')
.should.eql('http://www.google.com/foo/bar?test=123#faaaaa');
});
it('should support protocol-relative urls', function () {
urljoin('//www.google.com', 'foo/bar', '?test=123')
.should.eql('//www.google.com/foo/bar?test=123')
});
it('should support file protocol urls', function () {
urljoin('file:/', 'android_asset', 'foo/bar')
.should.eql('file://android_asset/foo/bar')
urljoin('file:', '/android_asset', 'foo/bar')
.should.eql('file://android_asset/foo/bar')
});
it('should support absolute file protocol urls', function () {
urljoin('file:', '///android_asset', 'foo/bar')
.should.eql('file:///android_asset/foo/bar')
urljoin('file:///', 'android_asset', 'foo/bar')
.should.eql('file:///android_asset/foo/bar')
urljoin('file:///', '//android_asset', 'foo/bar')
.should.eql('file:///android_asset/foo/bar')
urljoin('file:///android_asset', 'foo/bar')
.should.eql('file:///android_asset/foo/bar')
});
it('should merge multiple query params properly', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?key=456')
.should.eql('http://www.google.com/foo/bar?test=123&key=456');
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?boom=value', '&key=456')
.should.eql('http://www.google.com/foo/bar?test=123&boom=value&key=456');
urljoin('http://example.org/x', '?a=1', '?b=2', '?c=3', '?d=4')
.should.eql('http://example.org/x?a=1&b=2&c=3&d=4');
});
it('should merge slashes in paths correctly', function () {
urljoin('http://example.org', 'a//', 'b//', 'A//', 'B//')
.should.eql('http://example.org/a/b/A/B/');
});
it('should merge colons in paths correctly', function () {
urljoin('http://example.org/', ':foo:', 'bar')
.should.eql('http://example.org/:foo:/bar');
});
it('should merge just a simple path without URL correctly', function() {
urljoin('/', 'test')
.should.eql('/test');
});
it('should fail with segments that are not string', function() {
assert.throws(() => urljoin(true),
/Url must be a string. Received true/);
assert.throws(() => urljoin('http://blabla.com/', 1),
/Url must be a string. Received 1/);
assert.throws(() => urljoin('http://blabla.com/', undefined, 'test'),
/Url must be a string. Received undefined/);
assert.throws(() => urljoin('http://blabla.com/', null, 'test'),
/Url must be a string. Received null/);
assert.throws(() => urljoin('http://blabla.com/', { foo: 123 }, 'test'),
/Url must be a string. Received \[object Object\]/);
});
it('should merge a path with colon properly', function(){
urljoin('/users/:userId', '/cars/:carId')
.should.eql('/users/:userId/cars/:carId');
});
it('should merge slashes in protocol correctly', function () {
urljoin('http://example.org', 'a')
.should.eql('http://example.org/a');
urljoin('http:', '//example.org', 'a')
.should.eql('http://example.org/a');
urljoin('http:///example.org', 'a')
.should.eql('http://example.org/a');
urljoin('file:///example.org', 'a')
.should.eql('file:///example.org/a');
urljoin('file:example.org', 'a')
.should.eql('file://example.org/a');
urljoin('file:/', 'example.org', 'a')
.should.eql('file://example.org/a');
urljoin('file:', '/example.org', 'a')
.should.eql('file://example.org/a');
urljoin('file:', '//example.org', 'a')
.should.eql('file://example.org/a');
});
it('should skip empty strings', function() {
urljoin('http://foobar.com', '', 'test')
.should.eql('http://foobar.com/test');
urljoin('', 'http://foobar.com', '', 'test')
.should.eql('http://foobar.com/test');
});
it('should return an empty string if no arguments are supplied', function() {
urljoin().should.eql('');
});
});
......@@ -230,6 +230,11 @@
"is-buffer": "^2.0.2"
}
},
"axios-error": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/axios-error/-/axios-error-0.8.1.tgz",
"integrity": "sha512-4YIf0FK2aO8HHVAQXvVFK2oPQSsOh3P1WQjkwQwO3oFjq4vO3S3YSyiRzyebBIR4NmcHaMJ6W5m0iHuisG7lTA=="
},
"base64-js": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz",
......@@ -778,11 +783,34 @@
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz",
"integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg=="
},
"image-type": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/image-type/-/image-type-4.1.0.tgz",
"integrity": "sha512-CFJMJ8QK8lJvRlTCEgarL4ro6hfDQKif2HjSvYCdQZESaIPV4v9imrf7BQHK+sQeTeNeMpWciR9hyC/g8ybXEg==",
"requires": {
"file-type": "^10.10.0"
},
"dependencies": {
"file-type": {
"version": "10.11.0",
"resolved": "https://registry.npmjs.org/file-type/-/file-type-10.11.0.tgz",
"integrity": "sha512-uzk64HRpUZyTGZtVuvrjP0FYxzQrBf4rojot6J65YMEbwBLB0CWm0CLojVpwpmFmxcE/lkvYICgfcGozbBq6rw=="
}
}
},
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
},
"invariant": {
"version": "2.2.4",
"resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.4.tgz",
"integrity": "sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA==",
"requires": {
"loose-envify": "^1.0.0"
}
},
"ipaddr.js": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.8.0.tgz",
......@@ -849,6 +877,11 @@
"resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz",
"integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc="
},
"js-tokens": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="
},
"jsbn": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz",
......@@ -922,11 +955,24 @@
"resolved": "https://registry.npmjs.org/lodash.has/-/lodash.has-4.5.2.tgz",
"integrity": "sha1-0Z9NwQlQWMzL4rDN9O4P5Ko3yGI="
},
"lodash.omit": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.omit/-/lodash.omit-4.5.0.tgz",
"integrity": "sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA="
},
"long": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA=="
},
"loose-envify": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
"integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==",
"requires": {
"js-tokens": "^3.0.0 || ^4.0.0"
}
},
"lru-cache": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz",
......@@ -945,6 +991,35 @@
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
"integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E="
},
"messaging-api-line": {
"version": "0.8.3",
"resolved": "https://registry.npmjs.org/messaging-api-line/-/messaging-api-line-0.8.3.tgz",
"integrity": "sha512-EQRqW9UMtfQPw/PfnkFue7eSXZlxX2Ke5frFGhl6UHUW5Qfi4KPpvAqf2VTaU/cU7rGhGnZWuNbjoxkCWDyT8A==",
"requires": {
"axios": "^0.19.0",
"axios-error": "^0.8.1",
"debug": "^4.1.1",
"image-type": "^4.1.0",
"invariant": "^2.2.4",
"lodash.omit": "^4.5.0",
"url-join": "^4.0.1"
},
"dependencies": {
"debug": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
"integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
"requires": {
"ms": "^2.1.1"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}
}
},
"methods": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
......@@ -1368,6 +1443,11 @@
}
}
},
"url-join": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/url-join/-/url-join-4.0.1.tgz",
"integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA=="
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
......
......@@ -15,6 +15,7 @@
"aws-sdk": "^2.573.0",
"dotenv": "^8.2.0",
"express": "^4.16.4",
"messaging-api-line": "^0.8.3",
"request": "^2.88.0"
}
}
......