es6 syntax
This commit is contained in:
parent
b430a9146f
commit
74cdccf5cf
3
.gitignore
vendored
3
.gitignore
vendored
@ -60,4 +60,7 @@ typings/
|
||||
# next.js build output
|
||||
.next
|
||||
.idea
|
||||
|
||||
package-lock.json
|
||||
out/
|
||||
dist/
|
45
README.md
45
README.md
@ -18,11 +18,11 @@ var RetailcrmBotApiClient = require('mg-api-client');
|
||||
# Usage
|
||||
#### Get users
|
||||
```javascript
|
||||
var api = new RetailcrmBotApiClient({
|
||||
const api = new RetailcrmBotApiClient({
|
||||
host: 'https://api.example.com',
|
||||
token: 'your bot token',
|
||||
apiVersion: 'v1' // optional
|
||||
}).getClient();
|
||||
}).client;
|
||||
|
||||
api.getUsers()
|
||||
.then(function (users) {
|
||||
@ -35,13 +35,13 @@ api.getUsers()
|
||||
|
||||
#### Send message
|
||||
```javascript
|
||||
var api = new RetailcrmBotApiClient({
|
||||
const api = new RetailcrmBotApiClient({
|
||||
host: 'https://api.example.com',
|
||||
token: 'your bot token',
|
||||
apiVersion: 'v1' // optional
|
||||
}).getClient();
|
||||
}).client;
|
||||
|
||||
var message = {
|
||||
let message = {
|
||||
chat_id: 1,
|
||||
content: 'Text message',
|
||||
scope: 'public',
|
||||
@ -56,3 +56,38 @@ api.sendMessage(message)
|
||||
console.log(e);
|
||||
});
|
||||
```
|
||||
#### Websocket Example
|
||||
```javascript
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const api = new RetailcrmBotApiClient({
|
||||
host: 'https://api.example.com',
|
||||
token: 'your bot token',
|
||||
apiVersion: 'v1' // optional
|
||||
}).client;
|
||||
|
||||
const wsData = api.getWebsocketData(['message_new']);
|
||||
const ws = new WebSocket(wsData.get('url'), {
|
||||
headers: wsData.get('headers')
|
||||
});
|
||||
|
||||
ws.on('message', function (content) {
|
||||
let event = JSON.parse(content);
|
||||
let data = event.data;
|
||||
|
||||
if (event.type === 'message_new' && data.message.from.type !== 'bot') {
|
||||
let message = {
|
||||
chat_id: data.message.chat_id,
|
||||
content: 'Bonjour!',
|
||||
scope: 'public',
|
||||
type: 'text'
|
||||
};
|
||||
|
||||
api.sendMessage(message).then(function (res) {
|
||||
console.log(res);
|
||||
}).catch(function (e) {
|
||||
console.log(e);
|
||||
})
|
||||
}
|
||||
});
|
||||
```
|
||||
|
31
index.js
31
index.js
@ -1,16 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
var v1 = require('./lib/v1/client');
|
||||
var request = require('./lib/request');
|
||||
import v1 from './lib/v1/client'
|
||||
import Request from './lib/request'
|
||||
|
||||
module.exports = RetailcrmBotApiClient;
|
||||
const lastApiVersion = 'v1';
|
||||
|
||||
/**
|
||||
/** Class init bot api client */
|
||||
export default class RetailcrmBotApiClient {
|
||||
/**
|
||||
* @param {Object} options
|
||||
* @throws {Error}
|
||||
* @constructor
|
||||
*/
|
||||
function RetailcrmBotApiClient(options) {
|
||||
constructor(options) {
|
||||
if (!options.host) {
|
||||
throw new Error('Url is required');
|
||||
}
|
||||
@ -23,11 +24,10 @@ function RetailcrmBotApiClient(options) {
|
||||
throw new Error('Token is required');
|
||||
}
|
||||
|
||||
var currentVersion;
|
||||
var lastApiVersion = 'v1';
|
||||
let currentVersion;
|
||||
|
||||
var clients = {
|
||||
'v1': v1.Client
|
||||
const clients = {
|
||||
'v1': v1
|
||||
};
|
||||
|
||||
if (options.apiVersion) {
|
||||
@ -36,13 +36,14 @@ function RetailcrmBotApiClient(options) {
|
||||
currentVersion = lastApiVersion;
|
||||
}
|
||||
|
||||
this._client = new clients[currentVersion](new request.Request(options));
|
||||
}
|
||||
this._client = new clients[currentVersion](new Request(options));
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get API client
|
||||
* @returns {Client}
|
||||
*/
|
||||
RetailcrmBotApiClient.prototype.getClient = function () {
|
||||
get client() {
|
||||
return this._client;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
105
lib/request.js
105
lib/request.js
@ -1,31 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
var url = require('url');
|
||||
var https = require('https');
|
||||
var querystring = require('querystring');
|
||||
import url from 'url'
|
||||
import https from 'https'
|
||||
import querystring from 'querystring'
|
||||
|
||||
exports.Request = Request;
|
||||
|
||||
/**
|
||||
export default class Request {
|
||||
/**
|
||||
* @param {Object} options
|
||||
* @constructor
|
||||
*/
|
||||
function Request(options) {
|
||||
constructor(options) {
|
||||
/**
|
||||
* @prop System host
|
||||
* @type {string}
|
||||
* @private
|
||||
*/
|
||||
this._host = url.parse(options.host).host;
|
||||
this._token = options.token;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* @prop Bot token
|
||||
* @type {*|string|string}
|
||||
* @private
|
||||
*/
|
||||
this._token = options.token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request path
|
||||
* @param {string} endpoint
|
||||
* @returns {string}
|
||||
* @private
|
||||
*/
|
||||
Request.prototype._getPath = function (endpoint) {
|
||||
_getPath(endpoint) {
|
||||
return '/api/bot/' + endpoint;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Make request
|
||||
* @param {string} endpoint
|
||||
* @param {string} method
|
||||
@ -34,32 +44,32 @@ Request.prototype._getPath = function (endpoint) {
|
||||
* @throws {Error}
|
||||
* @private
|
||||
*/
|
||||
Request.prototype._request = function (endpoint, method, data) {
|
||||
var path = this._getPath(endpoint);
|
||||
var response = '';
|
||||
_request(endpoint, method, data = {}) {
|
||||
let path = this._getPath(endpoint);
|
||||
let response = '';
|
||||
|
||||
if (method === 'GET' && data.length > 0) {
|
||||
path += '?' + querystring.stringify(data);
|
||||
}
|
||||
|
||||
var options = {
|
||||
const options = {
|
||||
host: this._host,
|
||||
method: method,
|
||||
path: path,
|
||||
headers: {
|
||||
'x-bot-token': this._token
|
||||
'X-Bot-Token': this._token
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
var request = https.request(options, function (res) {
|
||||
const request = https.request(options, function (res) {
|
||||
res.on('data', function (chunk) {
|
||||
response += chunk;
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
try {
|
||||
var result = JSON.parse(response);
|
||||
const result = JSON.parse(response);
|
||||
|
||||
if (res.statusCode < 400) {
|
||||
resolve(result);
|
||||
@ -86,76 +96,85 @@ Request.prototype._request = function (endpoint, method, data) {
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Method GET
|
||||
* @param {string} endpoint
|
||||
* @param {Object} params
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Request.prototype.get = function (endpoint, params) {
|
||||
if (params === undefined) {
|
||||
params = {};
|
||||
get(endpoint, params = {}) {
|
||||
return this._request(endpoint, 'GET', params);
|
||||
}
|
||||
|
||||
return this._request(endpoint, 'GET', params);
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Method POST
|
||||
* @param {string} endpoint
|
||||
* @param {Object} data
|
||||
* @returns {Promise}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Request.prototype.post = function (endpoint, data) {
|
||||
post(endpoint, data) {
|
||||
if (!data) {
|
||||
throw new Error('Body is not be empty');
|
||||
}
|
||||
|
||||
return this._request(endpoint, 'POST', data);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Method PATCH
|
||||
* @param {string} endpoint
|
||||
* @param {Object} data
|
||||
* @returns {Promise}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Request.prototype.patch = function (endpoint, data) {
|
||||
patch(endpoint, data) {
|
||||
if (!data) {
|
||||
throw new Error('Body is not be empty');
|
||||
}
|
||||
|
||||
return this._request(endpoint, 'PATCH', data);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Method PUT
|
||||
* @param {string} endpoint
|
||||
* @param {Object} data
|
||||
* @returns {Promise}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Request.prototype.put = function (endpoint, data) {
|
||||
put(endpoint, data) {
|
||||
if (!data) {
|
||||
throw new Error('Body is not be empty');
|
||||
}
|
||||
|
||||
return this._request(endpoint, 'PUT', data);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Method DELETE
|
||||
* @param {string} endpoint
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Request.prototype.delete = function (endpoint) {
|
||||
return this._request(endpoint, 'DELETE', {});
|
||||
};
|
||||
delete(endpoint) {
|
||||
return this._request(endpoint, 'DELETE');
|
||||
}
|
||||
|
||||
Request.prototype.getHost = function () {
|
||||
/**
|
||||
* Get api host
|
||||
* @returns {string | *}
|
||||
*/
|
||||
get host() {
|
||||
return this._host;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bot token
|
||||
* @returns {*|string}
|
||||
*/
|
||||
get token() {
|
||||
return this._token;
|
||||
}
|
||||
}
|
||||
|
183
lib/v1/client.js
183
lib/v1/client.js
@ -1,204 +1,231 @@
|
||||
'use strict';
|
||||
|
||||
exports.Client = Client;
|
||||
|
||||
/**
|
||||
* @classdesc Bot API v1 methods
|
||||
* @namespace ClientV1
|
||||
* @readonly
|
||||
*/
|
||||
export default class Client {
|
||||
/**
|
||||
* @param {Request} request
|
||||
* @constructor
|
||||
*/
|
||||
function Client(request) {
|
||||
constructor(request) {
|
||||
/**
|
||||
* @prop API version
|
||||
* @type {string}
|
||||
* @private
|
||||
*/
|
||||
this._version = 'v1';
|
||||
|
||||
/**
|
||||
* @prop Request object
|
||||
* @type {Request}
|
||||
* @private
|
||||
*/
|
||||
this._request = request;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get bots
|
||||
* @param {string} params
|
||||
* @param {Object} params
|
||||
* @since 1.0.0
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.getBots = function (params) {
|
||||
getBots(params = {}) {
|
||||
return this._request.get(this._version + '/bots', params);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get channels
|
||||
* @param {string} params
|
||||
* @param {Object} params
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.getChannels = function (params) {
|
||||
getChannels(params = {}) {
|
||||
return this._request.get(this._version + '/channels', params);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get chats
|
||||
* @param {string} params
|
||||
* @param {Object} params
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.getChats = function (params) {
|
||||
getChats(params = {}) {
|
||||
return this._request.get(this._version + '/chats', params);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get customers
|
||||
* @param {string} params
|
||||
* @param {Object} params
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.getCustomers = function (params) {
|
||||
getCustomers(params = {}) {
|
||||
return this._request.get(this._version + '/customers', params);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get dialogs
|
||||
* @param {string} params
|
||||
* @param {Object} params
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.getDialogs = function (params) {
|
||||
getDialogs(params = {}) {
|
||||
return this._request.get(this._version + '/dialogs', params);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get members
|
||||
* @param {string} params
|
||||
* @param {Object} params
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.getMembers = function (params) {
|
||||
getMembers(params = {}) {
|
||||
return this._request.get(this._version + '/members', params);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Assign dialog
|
||||
* @param {Number} dialog_id
|
||||
* @param {Object} dialog
|
||||
* @param {Number} dialog_id - Dialog identificator
|
||||
* @param {Object} dialog - Dialog object
|
||||
* @returns {Promise}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Client.prototype.assignDialog = function (dialog_id, dialog) {
|
||||
return this._request.patch(this._version + '/dialogs/'+ dialog_id + '/assign', dialog);
|
||||
};
|
||||
assignDialog(dialog_id, dialog) {
|
||||
if (!dialog_id) {
|
||||
throw new Error('Parameter `dialog_id` is required');
|
||||
}
|
||||
|
||||
/**
|
||||
return this._request.patch(this._version + '/dialogs/'+ dialog_id + '/assign', dialog);
|
||||
};
|
||||
|
||||
/**
|
||||
* Close dialog
|
||||
* @param {Number} dialog_id
|
||||
* @returns {Promise}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Client.prototype.closeDialog = function (dialog_id) {
|
||||
closeDialog(dialog_id) {
|
||||
if (!dialog_id) {
|
||||
throw new Error('dialog_id is required');
|
||||
throw new Error('Parameter `dialog_id` is required');
|
||||
}
|
||||
|
||||
return this._request.delete(this._version + '/dialogs/'+ dialog_id + '/close');
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Send message
|
||||
* @param {Object} data
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.sendMessage = function (data) {
|
||||
sendMessage(data) {
|
||||
return this._request.post(this._version + '/messages', data);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get messages
|
||||
* @param {Object} params
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.getMessages = function (params) {
|
||||
getMessages(params = {}) {
|
||||
return this._request.get(this._version + '/messages', params);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Delete message
|
||||
* @param {Number} message_id
|
||||
* @returns {Promise}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Client.prototype.deleteMessage = function (message_id) {
|
||||
deleteMessage(message_id) {
|
||||
if (!message_id) {
|
||||
throw new Error('message_id is required');
|
||||
throw new Error('Parameter `message_id` is required');
|
||||
}
|
||||
|
||||
return this._request.delete(this._version + '/messages/' + message_id);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Edit message
|
||||
* @param {Number} message_id
|
||||
* @param {Object} message
|
||||
* @returns {Promise}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Client.prototype.editMessage = function (message_id, message) {
|
||||
return this._request.patch(this._version + '/messages/' + message_id, message);
|
||||
};
|
||||
editMessage(message_id, message) {
|
||||
if (!message_id) {
|
||||
throw new Error('Parameter `message_id` is required');
|
||||
}
|
||||
|
||||
/**
|
||||
return this._request.patch(this._version + '/messages/' + message_id, message);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get bot commands
|
||||
* @param {Object} params
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.getCommands = function (params) {
|
||||
getCommands(params = {}) {
|
||||
return this._request.get(this._version + '/my/commands', params);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Edit bot command
|
||||
* @param {string} command_name
|
||||
* @param {Object} data
|
||||
* @returns {Promise}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Client.prototype.editCommand = function (command_name, data) {
|
||||
editCommand(command_name, data) {
|
||||
if (!command_name) {
|
||||
throw new Error('Parameter command name is required');
|
||||
throw new Error('Parameter `command_name` is required');
|
||||
}
|
||||
|
||||
return this._request.put(this._version + '/my/commands/' + command_name, data);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Delete bot command
|
||||
* @param {string} command_name
|
||||
* @returns {Promise}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Client.prototype.deleteCommand = function (command_name) {
|
||||
deleteCommand(command_name) {
|
||||
if (!command_name) {
|
||||
throw new Error('command_name is required');
|
||||
throw new Error('Parameter `command_name` is required');
|
||||
}
|
||||
|
||||
return this._request.delete(this._version + '/my/commands/' + command_name);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Bot information update
|
||||
* @param {Object} data
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.info = function (data) {
|
||||
info(data) {
|
||||
return this._request.patch(this._version + '/my/info', data);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get users
|
||||
* @param {Object} params
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Client.prototype.getUsers = function (params) {
|
||||
getUsers(params = {}) {
|
||||
return this._request.get(this._version + '/users', params);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Get websocket url
|
||||
* @param {array<string>} events
|
||||
* @returns {string}
|
||||
* @returns {Map}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Client.prototype.getWebsocketUrl = function (events) {
|
||||
getWebsocketData(events) {
|
||||
if (!events) {
|
||||
throw new Error('Events is required');
|
||||
}
|
||||
|
||||
var url = 'wss://' + this._request.getHost() + '/api/bot/' + this._version + '/ws?events=';
|
||||
let map = new Map();
|
||||
let url = 'wss://' + this._request.host + '/api/bot/' + this._version + '/ws?events=';
|
||||
|
||||
events.forEach(function (event) {
|
||||
url += event + ',';
|
||||
@ -206,5 +233,11 @@ Client.prototype.getWebsocketUrl = function (events) {
|
||||
|
||||
url = url.slice(0, -1);
|
||||
|
||||
return url;
|
||||
};
|
||||
map.set('url', url);
|
||||
map.set('headers', {
|
||||
'X-Bot-Token': this._request.token
|
||||
});
|
||||
|
||||
return map;
|
||||
};
|
||||
}
|
||||
|
23
package.json
23
package.json
@ -1,22 +1,37 @@
|
||||
{
|
||||
"name": "mg-api-client",
|
||||
"description": "JS client for retailCRM Bot API",
|
||||
"tags": ["API", "retailCRM", "Bot", "Chat"],
|
||||
"tags": [
|
||||
"API",
|
||||
"retailCRM",
|
||||
"Bot",
|
||||
"Chat"
|
||||
],
|
||||
"version": "1.0.1",
|
||||
"scripts": {
|
||||
"test": "./node_modules/.bin/_mocha ./tests/."
|
||||
"build": "./node_modules/.bin/rollup -c",
|
||||
"test": "./node_modules/.bin/_mocha --compilers js:@babel/register ./tests/."
|
||||
},
|
||||
"author": "retailCRM",
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"main": "./dist/index.js",
|
||||
"bugs": {
|
||||
"url": "https://github.com/retailcrm/mg-bot-api-client-js/issues"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.2.3",
|
||||
"@babel/core": "^7.3.3",
|
||||
"@babel/preset-env": "^7.3.1",
|
||||
"@babel/register": "^7.0.0",
|
||||
"babelrc-rollup": "^3.0.0",
|
||||
"chai": "^4.2.0",
|
||||
"mocha": "^5.2.0",
|
||||
"nock": "^10.0.6"
|
||||
"nock": "^10.0.6",
|
||||
"rollup": "^1.2.2",
|
||||
"rollup-plugin-babel": "^4.3.2",
|
||||
"rollup-plugin-node-builtins": "^2.1.2",
|
||||
"rollup-plugin-node-globals": "^1.4.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 4.0.0"
|
||||
|
19
rollup.config.js
Normal file
19
rollup.config.js
Normal file
@ -0,0 +1,19 @@
|
||||
import babel from 'rollup-plugin-babel';
|
||||
// import builtins from 'rollup-plugin-node-builtins';
|
||||
|
||||
let pluginOptions = [
|
||||
// builtins(),
|
||||
babel({
|
||||
exclude: 'node_modules/**',
|
||||
}),
|
||||
];
|
||||
|
||||
export default [{
|
||||
input: 'index.js',
|
||||
output: {
|
||||
name: 'main',
|
||||
file: 'dist/index.js',
|
||||
format: 'umd',
|
||||
},
|
||||
plugins: pluginOptions,
|
||||
}];
|
@ -1,6 +1,5 @@
|
||||
var nock = require('nock');
|
||||
var chai = require('chai');
|
||||
var RetailcrmBotApiClient = require('../index');
|
||||
import chai from 'chai'
|
||||
import RetailcrmBotApiClient from '../dist/index'
|
||||
|
||||
describe('#Constructor', function () {
|
||||
it('Empty url', function () {
|
||||
|
@ -1,8 +1,8 @@
|
||||
var chai = require('chai');
|
||||
var request = require('../lib/request');
|
||||
import chai from 'chai'
|
||||
import Request from '../lib/request'
|
||||
|
||||
describe('#Request', function () {
|
||||
var req = new request.Request({
|
||||
let req = new Request({
|
||||
host: 'http://api.example.com',
|
||||
token: 'test_token'
|
||||
});
|
||||
|
@ -1,16 +1,16 @@
|
||||
var nock = require('nock');
|
||||
var chai = require('chai');
|
||||
var RetailcrmBotApiClient = require('../index');
|
||||
import nock from 'nock'
|
||||
import chai from 'chai'
|
||||
import RetailcrmBotApiClient from '../index'
|
||||
|
||||
describe('#API client v1', function() {
|
||||
beforeEach(function() {
|
||||
nock.cleanAll();
|
||||
});
|
||||
|
||||
var retailcrm = new RetailcrmBotApiClient({
|
||||
const retailcrm = new RetailcrmBotApiClient({
|
||||
host: 'https://api.example.com',
|
||||
token: 'test_token'
|
||||
}).getClient();
|
||||
}).client;
|
||||
|
||||
it('Get bots list', function() {
|
||||
nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, [{
|
||||
@ -153,7 +153,7 @@ describe('#API client v1', function() {
|
||||
});
|
||||
|
||||
it('Assign dialog incorrect', function () {
|
||||
chai.expect(retailcrm.assignDialog.bind(retailcrm)).to.throw('Body is not be empty');
|
||||
chai.expect(retailcrm.assignDialog.bind(retailcrm)).to.throw('Parameter `dialog_id` is required');
|
||||
});
|
||||
|
||||
it('Close dialog', function () {
|
||||
@ -165,7 +165,7 @@ describe('#API client v1', function() {
|
||||
});
|
||||
|
||||
it('Close dialog incorrect', function () {
|
||||
chai.expect(retailcrm.closeDialog.bind(retailcrm)).to.throw('dialog_id is required');
|
||||
chai.expect(retailcrm.closeDialog.bind(retailcrm)).to.throw('Parameter `dialog_id` is required');
|
||||
});
|
||||
|
||||
it('Send message', function () {
|
||||
@ -226,7 +226,7 @@ describe('#API client v1', function() {
|
||||
});
|
||||
|
||||
it('Delete message incorrect', function () {
|
||||
chai.expect(retailcrm.deleteMessage.bind(retailcrm)).to.throw('message_id is required');
|
||||
chai.expect(retailcrm.deleteMessage.bind(retailcrm)).to.throw('Parameter `message_id` is required');
|
||||
});
|
||||
|
||||
it('Edit message', function () {
|
||||
@ -242,7 +242,7 @@ describe('#API client v1', function() {
|
||||
});
|
||||
|
||||
it('Edit message incorrect', function () {
|
||||
chai.expect(retailcrm.editMessage.bind(retailcrm)).to.throw('Body is not be empty');
|
||||
chai.expect(retailcrm.editMessage.bind(retailcrm)).to.throw('Parameter `message_id` is required');
|
||||
});
|
||||
|
||||
it('Get commands', function () {
|
||||
@ -282,7 +282,7 @@ describe('#API client v1', function() {
|
||||
|
||||
it('Edit command incorrect', function () {
|
||||
chai.expect(retailcrm.editCommand.bind(retailcrm, 'command')).to.throw('Body is not be empty');
|
||||
chai.expect(retailcrm.editCommand.bind(retailcrm)).to.throw('Parameter command name is required');
|
||||
chai.expect(retailcrm.editCommand.bind(retailcrm)).to.throw('Parameter `command_name` is required');
|
||||
});
|
||||
|
||||
it('Delete command', function () {
|
||||
@ -294,7 +294,7 @@ describe('#API client v1', function() {
|
||||
});
|
||||
|
||||
it('Delete command incorrect', function () {
|
||||
chai.expect(retailcrm.deleteCommand.bind(retailcrm)).to.throw('command_name is required');
|
||||
chai.expect(retailcrm.deleteCommand.bind(retailcrm)).to.throw('Parameter `command_name` is required');
|
||||
});
|
||||
|
||||
it('Update bot info', function () {
|
||||
@ -336,14 +336,16 @@ describe('#API client v1', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('Get websocket url', function () {
|
||||
var url = retailcrm.getWebsocketUrl(['message_new', 'message_updated']);
|
||||
var expected = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated';
|
||||
it('Get websocket data', function () {
|
||||
const wsData = retailcrm.getWebsocketData(['message_new', 'message_updated']);
|
||||
const expectedUrl = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated';
|
||||
const expectedHeaders = {'X-Bot-Token': 'test_token'};
|
||||
|
||||
chai.assert.equal(url, expected);
|
||||
chai.assert.equal(wsData.get('url'), expectedUrl);
|
||||
chai.assert.equal(wsData.get('headers')["X-Bot-Token"], expectedHeaders["X-Bot-Token"]);
|
||||
});
|
||||
|
||||
it('Get websocket url incorrect', function () {
|
||||
chai.expect(retailcrm.getWebsocketUrl.bind(retailcrm)).to.throw('Events is required');
|
||||
chai.expect(retailcrm.getWebsocketData.bind(retailcrm)).to.throw('Events is required');
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user