From 74cdccf5cfd0f91373289ab95b909866994ddc92 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Fri, 1 Mar 2019 13:46:30 +0300 Subject: [PATCH 1/4] es6 syntax --- .babelrc | 3 + .gitignore | 5 +- README.md | 45 +++- index.js | 81 ++++---- lib/request.js | 301 ++++++++++++++------------- lib/v1/client.js | 443 +++++++++++++++++++++------------------- package.json | 23 ++- rollup.config.js | 19 ++ tests/test_index.js | 5 +- tests/test_request.js | 6 +- tests/test_v1_client.js | 34 +-- 11 files changed, 547 insertions(+), 418 deletions(-) create mode 100644 .babelrc create mode 100644 rollup.config.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..1320b9a --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["@babel/preset-env"] +} diff --git a/.gitignore b/.gitignore index cf9a6f3..31fdc6d 100644 --- a/.gitignore +++ b/.gitignore @@ -60,4 +60,7 @@ typings/ # next.js build output .next .idea -package-lock.json \ No newline at end of file + +package-lock.json +out/ +dist/ \ No newline at end of file diff --git a/README.md b/README.md index ab68a76..ee712a1 100644 --- a/README.md +++ b/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); + }) + } +}); +``` diff --git a/index.js b/index.js index a80ccf2..1f10f6e 100644 --- a/index.js +++ b/index.js @@ -1,48 +1,49 @@ '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'; -/** - * @param {Object} options - * @throws {Error} - * @constructor - */ -function RetailcrmBotApiClient(options) { - if (!options.host) { - throw new Error('Url is required'); +/** Class init bot api client */ +export default class RetailcrmBotApiClient { + /** + * @param {Object} options + * @throws {Error} + */ + constructor(options) { + if (!options.host) { + throw new Error('Url is required'); + } + + if (options.host.indexOf('https') !== 0) { + throw new Error('HTTPS required'); + } + + if (!(options.token)) { + throw new Error('Token is required'); + } + + let currentVersion; + + const clients = { + 'v1': v1 + }; + + if (options.apiVersion) { + currentVersion = options.apiVersion; + } else { + currentVersion = lastApiVersion; + } + + this._client = new clients[currentVersion](new Request(options)); } - if (options.host.indexOf('https') !== 0) { - throw new Error('HTTPS required'); - } - - if (!(options.token)) { - throw new Error('Token is required'); - } - - var currentVersion; - var lastApiVersion = 'v1'; - - var clients = { - 'v1': v1.Client + /** + * Get API client + * @returns {Client} + */ + get client() { + return this._client; }; - - if (options.apiVersion) { - currentVersion = options.apiVersion; - } else { - currentVersion = lastApiVersion; - } - - this._client = new clients[currentVersion](new request.Request(options)); } - -/** - * Get API client - * @returns {Client} - */ -RetailcrmBotApiClient.prototype.getClient = function () { - return this._client; -}; diff --git a/lib/request.js b/lib/request.js index 7266179..c37691d 100644 --- a/lib/request.js +++ b/lib/request.js @@ -1,161 +1,180 @@ '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 + */ + constructor(options) { + /** + * @prop System host + * @type {string} + * @private + */ + this._host = url.parse(options.host).host; -/** - * @param {Object} options - * @constructor - */ -function Request(options) { - this._host = url.parse(options.host).host; - this._token = options.token; -} - -/** - * Get request path - * @param {string} endpoint - * @returns {string} - * @private - */ -Request.prototype._getPath = function (endpoint) { - return '/api/bot/' + endpoint; -}; - -/** - * Make request - * @param {string} endpoint - * @param {string} method - * @param {Object} data - * @returns {Promise} - * @throws {Error} - * @private - */ -Request.prototype._request = function (endpoint, method, data) { - var path = this._getPath(endpoint); - var response = ''; - - if (method === 'GET' && data.length > 0) { - path += '?' + querystring.stringify(data); + /** + * @prop Bot token + * @type {*|string|string} + * @private + */ + this._token = options.token; } - var options = { - host: this._host, - method: method, - path: path, - headers: { - 'x-bot-token': this._token + /** + * Get request path + * @param {string} endpoint + * @returns {string} + * @private + */ + _getPath(endpoint) { + return '/api/bot/' + endpoint; + } + + /** + * Make request + * @param {string} endpoint + * @param {string} method + * @param {Object} data + * @returns {Promise} + * @throws {Error} + * @private + */ + _request(endpoint, method, data = {}) { + let path = this._getPath(endpoint); + let response = ''; + + if (method === 'GET' && data.length > 0) { + path += '?' + querystring.stringify(data); } + + const options = { + host: this._host, + method: method, + path: path, + headers: { + 'X-Bot-Token': this._token + } + }; + + return new Promise(function(resolve, reject) { + const request = https.request(options, function (res) { + res.on('data', function (chunk) { + response += chunk; + }); + + res.on('end', function () { + try { + const result = JSON.parse(response); + + if (res.statusCode < 400) { + resolve(result); + } else { + reject(new Error(result.errors.join(','))); + } + } catch (e) { + reject(e); + } + }); + + res.on('error', function (e) { + reject(e); + }) + }); + + if (['POST', 'PUT', 'PATCH'].includes(method)) { + request.write(JSON.stringify(data)); + } + + request.end(); + + request.on('error', function(e) { + reject(e); + }); + }); + } + + /** + * Method GET + * @param {string} endpoint + * @param {Object} params + * @returns {Promise} + */ + get(endpoint, params = {}) { + return this._request(endpoint, 'GET', params); + } + + /** + * Method POST + * @param {string} endpoint + * @param {Object} data + * @returns {Promise} + * @throws {Error} + */ + post(endpoint, data) { + if (!data) { + throw new Error('Body is not be empty'); + } + + return this._request(endpoint, 'POST', data); }; - return new Promise(function(resolve, reject) { - var request = https.request(options, function (res) { - res.on('data', function (chunk) { - response += chunk; - }); - - res.on('end', function () { - try { - var result = JSON.parse(response); - - if (res.statusCode < 400) { - resolve(result); - } else { - reject(new Error(result.errors.join(','))); - } - } catch (e) { - reject(e); - } - }); - - res.on('error', function (e) { - reject(e); - }) - }); - - if (['POST', 'PUT', 'PATCH'].includes(method)) { - request.write(JSON.stringify(data)); + /** + * Method PATCH + * @param {string} endpoint + * @param {Object} data + * @returns {Promise} + * @throws {Error} + */ + patch(endpoint, data) { + if (!data) { + throw new Error('Body is not be empty'); } - request.end(); - - request.on('error', function(e) { - reject(e); - }); - }); -}; - -/** - * Method GET - * @param {string} endpoint - * @param {Object} params - * @returns {Promise} - */ -Request.prototype.get = function (endpoint, params) { - if (params === undefined) { - params = {}; + return this._request(endpoint, 'PATCH', data); } - return this._request(endpoint, 'GET', params); -}; + /** + * Method PUT + * @param {string} endpoint + * @param {Object} data + * @returns {Promise} + * @throws {Error} + */ + put(endpoint, data) { + if (!data) { + throw new Error('Body is not be empty'); + } -/** - * Method POST - * @param {string} endpoint - * @param {Object} data - * @returns {Promise} - * @throws {Error} - */ -Request.prototype.post = function (endpoint, data) { - if (!data) { - throw new Error('Body is not be empty'); + return this._request(endpoint, 'PUT', data); } - return this._request(endpoint, 'POST', data); -}; - -/** - * Method PATCH - * @param {string} endpoint - * @param {Object} data - * @returns {Promise} - * @throws {Error} - */ -Request.prototype.patch = function (endpoint, data) { - if (!data) { - throw new Error('Body is not be empty'); + /** + * Method DELETE + * @param {string} endpoint + * @returns {Promise} + */ + delete(endpoint) { + return this._request(endpoint, 'DELETE'); } - return this._request(endpoint, 'PATCH', data); -}; - -/** - * Method PUT - * @param {string} endpoint - * @param {Object} data - * @returns {Promise} - * @throws {Error} - */ -Request.prototype.put = function (endpoint, data) { - if (!data) { - throw new Error('Body is not be empty'); + /** + * Get api host + * @returns {string | *} + */ + get host() { + return this._host; } - return this._request(endpoint, 'PUT', data); -}; - -/** - * Method DELETE - * @param {string} endpoint - * @returns {Promise} - */ -Request.prototype.delete = function (endpoint) { - return this._request(endpoint, 'DELETE', {}); -}; - -Request.prototype.getHost = function () { - return this._host; -}; + /** + * Get bot token + * @returns {*|string} + */ + get token() { + return this._token; + } +} diff --git a/lib/v1/client.js b/lib/v1/client.js index c7dadbb..013bb29 100644 --- a/lib/v1/client.js +++ b/lib/v1/client.js @@ -1,210 +1,243 @@ 'use strict'; -exports.Client = Client; - /** - * @param {Request} request - * @constructor + * @classdesc Bot API v1 methods + * @namespace ClientV1 + * @readonly */ -function Client(request) { - this._version = 'v1'; - this._request = request; +export default class Client { + /** + * @param {Request} request + * @constructor + */ + constructor(request) { + /** + * @prop API version + * @type {string} + * @private + */ + this._version = 'v1'; + + /** + * @prop Request object + * @type {Request} + * @private + */ + this._request = request; + } + + /** + * Get bots + * @param {Object} params + * @since 1.0.0 + * @returns {Promise} + */ + getBots(params = {}) { + return this._request.get(this._version + '/bots', params); + }; + + /** + * Get channels + * @param {Object} params + * @returns {Promise} + */ + getChannels(params = {}) { + return this._request.get(this._version + '/channels', params); + }; + + /** + * Get chats + * @param {Object} params + * @returns {Promise} + */ + getChats(params = {}) { + return this._request.get(this._version + '/chats', params); + }; + + /** + * Get customers + * @param {Object} params + * @returns {Promise} + */ + getCustomers(params = {}) { + return this._request.get(this._version + '/customers', params); + }; + + /** + * Get dialogs + * @param {Object} params + * @returns {Promise} + */ + getDialogs(params = {}) { + return this._request.get(this._version + '/dialogs', params); + }; + + /** + * Get members + * @param {Object} params + * @returns {Promise} + */ + getMembers(params = {}) { + return this._request.get(this._version + '/members', params); + }; + + /** + * Assign dialog + * @param {Number} dialog_id - Dialog identificator + * @param {Object} dialog - Dialog object + * @returns {Promise} + * @throws {Error} + */ + 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} + */ + closeDialog(dialog_id) { + if (!dialog_id) { + 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} + */ + sendMessage(data) { + return this._request.post(this._version + '/messages', data); + }; + + /** + * Get messages + * @param {Object} params + * @returns {Promise} + */ + getMessages(params = {}) { + return this._request.get(this._version + '/messages', params); + }; + + /** + * Delete message + * @param {Number} message_id + * @returns {Promise} + * @throws {Error} + */ + deleteMessage(message_id) { + if (!message_id) { + 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} + */ + 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} + */ + 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} + */ + editCommand(command_name, data) { + if (!command_name) { + 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} + */ + deleteCommand(command_name) { + if (!command_name) { + 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} + */ + info(data) { + return this._request.patch(this._version + '/my/info', data); + }; + + /** + * Get users + * @param {Object} params + * @returns {Promise} + */ + getUsers(params = {}) { + return this._request.get(this._version + '/users', params); + }; + + /** + * Get websocket url + * @param {array} events + * @returns {Map} + * @throws {Error} + */ + getWebsocketData(events) { + if (!events) { + throw new Error('Events is required'); + } + + let map = new Map(); + let url = 'wss://' + this._request.host + '/api/bot/' + this._version + '/ws?events='; + + events.forEach(function (event) { + url += event + ','; + }); + + url = url.slice(0, -1); + + map.set('url', url); + map.set('headers', { + 'X-Bot-Token': this._request.token + }); + + return map; + }; } - -/** - * Get bots - * @param {string} params - * @returns {Promise} - */ -Client.prototype.getBots = function (params) { - return this._request.get(this._version + '/bots', params); -}; - -/** - * Get channels - * @param {string} params - * @returns {Promise} - */ -Client.prototype.getChannels = function (params) { - return this._request.get(this._version + '/channels', params); -}; - -/** - * Get chats - * @param {string} params - * @returns {Promise} - */ -Client.prototype.getChats = function (params) { - return this._request.get(this._version + '/chats', params); -}; - -/** - * Get customers - * @param {string} params - * @returns {Promise} - */ -Client.prototype.getCustomers = function (params) { - return this._request.get(this._version + '/customers', params); -}; - -/** - * Get dialogs - * @param {string} params - * @returns {Promise} - */ -Client.prototype.getDialogs = function (params) { - return this._request.get(this._version + '/dialogs', params); -}; - -/** - * Get members - * @param {string} params - * @returns {Promise} - */ -Client.prototype.getMembers = function (params) { - return this._request.get(this._version + '/members', params); -}; - -/** - * Assign dialog - * @param {Number} dialog_id - * @param {Object} dialog - * @returns {Promise} - */ -Client.prototype.assignDialog = function (dialog_id, dialog) { - 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) { - if (!dialog_id) { - throw new Error('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) { - return this._request.post(this._version + '/messages', data); -}; - -/** - * Get messages - * @param {Object} params - * @returns {Promise} - */ -Client.prototype.getMessages = function (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) { - if (!message_id) { - throw new Error('message_id is required'); - } - - return this._request.delete(this._version + '/messages/' + message_id); -}; - -/** - * Edit message - * @param {Number} message_id - * @param {Object} message - * @returns {Promise} - */ -Client.prototype.editMessage = function (message_id, message) { - return this._request.patch(this._version + '/messages/' + message_id, message); -}; - -/** - * Get bot commands - * @param {Object} params - * @returns {Promise} - */ -Client.prototype.getCommands = function (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) { - if (!command_name) { - 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) { - if (!command_name) { - throw new Error('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) { - return this._request.patch(this._version + '/my/info', data); -}; - -/** - * Get users - * @param {Object} params - * @returns {Promise} - */ -Client.prototype.getUsers = function (params) { - return this._request.get(this._version + '/users', params); -}; - -/** - * Get websocket url - * @param {array} events - * @returns {string} - * @throws {Error} - */ -Client.prototype.getWebsocketUrl = function (events) { - if (!events) { - throw new Error('Events is required'); - } - - var url = 'wss://' + this._request.getHost() + '/api/bot/' + this._version + '/ws?events='; - - events.forEach(function (event) { - url += event + ','; - }); - - url = url.slice(0, -1); - - return url; -}; diff --git a/package.json b/package.json index 5f1ff20..d29fb88 100644 --- a/package.json +++ b/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" diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..9dee187 --- /dev/null +++ b/rollup.config.js @@ -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, +}]; \ No newline at end of file diff --git a/tests/test_index.js b/tests/test_index.js index a58a973..83f0bc1 100644 --- a/tests/test_index.js +++ b/tests/test_index.js @@ -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 () { diff --git a/tests/test_request.js b/tests/test_request.js index f88ca69..17f5b1d 100644 --- a/tests/test_request.js +++ b/tests/test_request.js @@ -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' }); diff --git a/tests/test_v1_client.js b/tests/test_v1_client.js index 49d5f83..577646e 100644 --- a/tests/test_v1_client.js +++ b/tests/test_v1_client.js @@ -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'); }); }); From e71a9ab7e30e3e128db4dd9fcbb95de85ed31364 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Wed, 6 Mar 2019 14:03:41 +0300 Subject: [PATCH 2/4] JSDoc --- .travis.yml | 15 ++- README.md | 9 +- index.js | 12 ++- lib/consts.js | 212 ++++++++++++++++++++++++++++++++++++++++ lib/v1/client.js | 88 ++++++++--------- package.json | 3 +- rollup.config.js | 2 - tests/test_index.js | 8 +- tests/test_v1_client.js | 78 +++++++-------- 9 files changed, 332 insertions(+), 95 deletions(-) create mode 100644 lib/consts.js diff --git a/.travis.yml b/.travis.yml index 74ba571..67e5d4a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,19 @@ language: node_js + node_js: - "11" - "10" - "8" -script: npm run test \ No newline at end of file + +script: npm run test + +after_success: npm run doc + +deploy: + provider: pages + skip-cleanup: true + github-token: $GITHUB_TOKEN + keep-history: true + local-dir: out + on: + branch: master \ No newline at end of file diff --git a/README.md b/README.md index ee712a1..5555edf 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,16 @@ This is js retailCRM bot API client. npm install --save mg-api-client ``` In your file + +###### CommonJS ``` var RetailcrmBotApiClient = require('mg-api-client'); ``` +###### es6 +``` +import RetailcrmBotApiClient from 'mg-api-client'; +``` + # Usage #### Get users ```javascript @@ -66,7 +73,7 @@ const api = new RetailcrmBotApiClient({ apiVersion: 'v1' // optional }).client; -const wsData = api.getWebsocketData(['message_new']); +const wsData = api.getWebsocketData([RetailcrmBotApiClient.types().wsMessageNew]); const ws = new WebSocket(wsData.get('url'), { headers: wsData.get('headers') }); diff --git a/index.js b/index.js index 1f10f6e..2858d02 100644 --- a/index.js +++ b/index.js @@ -2,11 +2,11 @@ import v1 from './lib/v1/client' import Request from './lib/request' +import * as consts from './lib/consts' const lastApiVersion = 'v1'; -/** Class init bot api client */ -export default class RetailcrmBotApiClient { +export default class MgBotApiClient { /** * @param {Object} options * @throws {Error} @@ -46,4 +46,12 @@ export default class RetailcrmBotApiClient { get client() { return this._client; }; + + /** + * Get types + * @returns {{msgTypeOrder?: string, wsUserJoinedChat?: string, msgTypeImage?: string, wsDialogAssign?: string, msgTypeText?: string, messageScopePublic?: string, wsMessageDeleted?: string, msgTypeCommand?: string, msgTypeFile?: string, msgTypeSystem?: string, wsBotUpdated?: string, msgTypeProduct?: string, wsDialogClosed?: string, wsMessageNew?: string, wsMessageUpdated?: string, wsSettingsUpdated?: string, wsUserUpdated?: string, wsCustomerUpdated?: string, wsChatCreated?: string, wsUserLeftChat?: string, wsChannelUpdated?: string, wsDialogOpened?: string, messageScopePrivate?: string, wsUserOnlineUpdated?: string, wsChatUnreadUpdated?: string, wsChatUpdated?: string}} + */ + static types() { + return consts; + } } diff --git a/lib/consts.js b/lib/consts.js new file mode 100644 index 0000000..928ac15 --- /dev/null +++ b/lib/consts.js @@ -0,0 +1,212 @@ +/** + * New message websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsMessageNew + */ +export const wsMessageNew = 'message_new'; + +/** + * Updating message websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsMessageUpdated + */ +export const wsMessageUpdated = 'message_updated'; + +/** + * Websocket event delete message + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsMessageDeleted + */ +export const wsMessageDeleted = 'message_deleted'; + +/** + * Dialogue opening websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsDialogOpened + * + */ +export const wsDialogOpened = 'dialog_opened'; + +/** + * Dialogue closing websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsDialogClosed + */ +export const wsDialogClosed = 'dialog_closed'; + +/** + * Dialogue appointment websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsDialogAssign + */ +export const wsDialogAssign = 'dialog_assign'; + +/** + * Chat creating websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsChatCreated + */ +export const wsChatCreated = 'chat_created'; + +/** + * Chat updating websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsChatUpdated + */ +export const wsChatUpdated = 'chat_updated'; + +/** + * Unread chat updating websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsChatUnreadUpdated + */ +export const wsChatUnreadUpdated = 'chat_unread_updated'; + +/** + * User online status updating websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsUserOnlineUpdated + */ +export const wsUserOnlineUpdated = 'user_online_updated'; + +/** + * User joined chat websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsUserJoinedChat + */ +export const wsUserJoinedChat = 'user_joined_chat'; + +/** + * User left chat websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsUserLeftChat + * + */ +export const wsUserLeftChat = 'user_left_chat'; + +/** + * User updating websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsUserUpdated + */ +export const wsUserUpdated = 'user_updated'; + +/** + * Customer updating websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsCustomerUpdated + */ +export const wsCustomerUpdated = 'customer_updated'; + +/** + * Bot updating websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsBotUpdated + * + */ +export const wsBotUpdated = 'bot_updated'; + +/** + * Channel updating websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsChannelUpdated + */ +export const wsChannelUpdated = 'channel_updated'; + +/** + * Settings updating websocket event + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().wsSettingsUpdated + */ +export const wsSettingsUpdated = 'settings_updated'; + + +/** + * Public message scope + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().messageScopePublic + */ +export const messageScopePublic = 'public'; + +/** + * Public message scope + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().messageScopePrivate + */ +export const messageScopePrivate = 'private'; + + +/** + * Text message type + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().msgTypeText + */ +export const msgTypeText = 'text'; + +/** + * System message type + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().msgTypeSystem + */ +export const msgTypeSystem = 'system'; + +/** + * Command message type + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().msgTypeCommand + */ +export const msgTypeCommand = 'command'; + +/** + * Order message type + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().msgTypeOrder + */ +export const msgTypeOrder = 'order'; + +/** + * Product message type + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().msgTypeProduct + */ +export const msgTypeProduct = 'product'; + +/** + * File message type + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().msgTypeFile + */ +export const msgTypeFile = 'file'; + +/** + * Image message type + * @constant + * @type {string} + * @example RetailcrmBotApiClient.types().msgTypeImage + */ +export const msgTypeImage = 'image'; diff --git a/lib/v1/client.js b/lib/v1/client.js index 013bb29..275a04b 100644 --- a/lib/v1/client.js +++ b/lib/v1/client.js @@ -1,36 +1,17 @@ 'use strict'; -/** - * @classdesc Bot API v1 methods - * @namespace ClientV1 - * @readonly - */ +/** @class Client */ export default class Client { - /** - * @param {Request} request - * @constructor - */ constructor(request) { - /** - * @prop API version - * @type {string} - * @private - */ this._version = 'v1'; - - /** - * @prop Request object - * @type {Request} - * @private - */ this._request = request; } /** * Get bots - * @param {Object} params - * @since 1.0.0 + * @param {Object} params - Filter's object for bots * @returns {Promise} + * @memberOf Client */ getBots(params = {}) { return this._request.get(this._version + '/bots', params); @@ -38,8 +19,9 @@ export default class Client { /** * Get channels - * @param {Object} params + * @param {Object} params - Filter's object for channels * @returns {Promise} + * @memberOf Client */ getChannels(params = {}) { return this._request.get(this._version + '/channels', params); @@ -47,8 +29,9 @@ export default class Client { /** * Get chats - * @param {Object} params + * @param {Object} params - Filter's object for chats * @returns {Promise} + * @memberOf Client */ getChats(params = {}) { return this._request.get(this._version + '/chats', params); @@ -56,8 +39,9 @@ export default class Client { /** * Get customers - * @param {Object} params + * @param {Object} params - Filter's object for customers * @returns {Promise} + * @memberOf Client */ getCustomers(params = {}) { return this._request.get(this._version + '/customers', params); @@ -65,8 +49,9 @@ export default class Client { /** * Get dialogs - * @param {Object} params + * @param {Object} params - Filter's object for dialogs * @returns {Promise} + * @memberOf Client */ getDialogs(params = {}) { return this._request.get(this._version + '/dialogs', params); @@ -74,8 +59,9 @@ export default class Client { /** * Get members - * @param {Object} params + * @param {Object} params - Filter's object for members * @returns {Promise} + * @memberOf Client */ getMembers(params = {}) { return this._request.get(this._version + '/members', params); @@ -83,10 +69,11 @@ export default class Client { /** * Assign dialog - * @param {Number} dialog_id - Dialog identificator + * @param {Number} dialog_id - Dialog id * @param {Object} dialog - Dialog object * @returns {Promise} * @throws {Error} + * @memberOf Client */ assignDialog(dialog_id, dialog) { if (!dialog_id) { @@ -98,9 +85,10 @@ export default class Client { /** * Close dialog - * @param {Number} dialog_id + * @param {Number} dialog_id - Dialog id * @returns {Promise} * @throws {Error} + * @memberOf Client */ closeDialog(dialog_id) { if (!dialog_id) { @@ -112,17 +100,19 @@ export default class Client { /** * Send message - * @param {Object} data + * @param {Object} message - Message object * @returns {Promise} + * @memberOf Client */ - sendMessage(data) { - return this._request.post(this._version + '/messages', data); + sendMessage(message) { + return this._request.post(this._version + '/messages', message); }; /** * Get messages - * @param {Object} params + * @param {Object} params - Filter's object for messages * @returns {Promise} + * @memberOf Client */ getMessages(params = {}) { return this._request.get(this._version + '/messages', params); @@ -130,9 +120,10 @@ export default class Client { /** * Delete message - * @param {Number} message_id + * @param {Number} message_id - Message id * @returns {Promise} * @throws {Error} + * @memberOf Client */ deleteMessage(message_id) { if (!message_id) { @@ -144,10 +135,11 @@ export default class Client { /** * Edit message - * @param {Number} message_id - * @param {Object} message + * @param {Number} message_id - Message id + * @param {Object} message - Message object * @returns {Promise} * @throws {Error} + * @memberOf Client */ editMessage(message_id, message) { if (!message_id) { @@ -159,8 +151,9 @@ export default class Client { /** * Get bot commands - * @param {Object} params + * @param {Object} params - Filter's object for commands * @returns {Promise} + * @memberOf Client */ getCommands(params = {}) { return this._request.get(this._version + '/my/commands', params); @@ -168,24 +161,26 @@ export default class Client { /** * Edit bot command - * @param {string} command_name - * @param {Object} data + * @param {string} command_name - Command name + * @param {Object} command - Command object * @returns {Promise} * @throws {Error} + * @memberOf Client */ - editCommand(command_name, data) { + editCommand(command_name, command) { if (!command_name) { throw new Error('Parameter `command_name` is required'); } - return this._request.put(this._version + '/my/commands/' + command_name, data); + return this._request.put(this._version + '/my/commands/' + command_name, command); }; /** * Delete bot command - * @param {string} command_name + * @param {string} command_name - Command name * @returns {Promise} * @throws {Error} + * @memberOf Client */ deleteCommand(command_name) { if (!command_name) { @@ -197,8 +192,9 @@ export default class Client { /** * Bot information update - * @param {Object} data + * @param {Object} data - Bot data * @returns {Promise} + * @memberOf Client */ info(data) { return this._request.patch(this._version + '/my/info', data); @@ -206,8 +202,9 @@ export default class Client { /** * Get users - * @param {Object} params + * @param {Object} params - Filter's object for users * @returns {Promise} + * @memberOf Client */ getUsers(params = {}) { return this._request.get(this._version + '/users', params); @@ -215,9 +212,10 @@ export default class Client { /** * Get websocket url - * @param {array} events + * @param {array} events - Array of strings with websocket events * @returns {Map} * @throws {Error} + * @memberOf Client */ getWebsocketData(events) { if (!events) { diff --git a/package.json b/package.json index d29fb88..13f28f5 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,9 @@ ], "version": "1.0.1", "scripts": { + "doc": "jsdoc lib/v1/client.js lib/types.js -R README.md", "build": "./node_modules/.bin/rollup -c", - "test": "./node_modules/.bin/_mocha --compilers js:@babel/register ./tests/." + "test": "./node_modules/.bin/_mocha --require @babel/register ./tests/." }, "author": "retailCRM", "license": "MIT", diff --git a/rollup.config.js b/rollup.config.js index 9dee187..3e54ecf 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,8 +1,6 @@ import babel from 'rollup-plugin-babel'; -// import builtins from 'rollup-plugin-node-builtins'; let pluginOptions = [ - // builtins(), babel({ exclude: 'node_modules/**', }), diff --git a/tests/test_index.js b/tests/test_index.js index 83f0bc1..4d1a3a8 100644 --- a/tests/test_index.js +++ b/tests/test_index.js @@ -1,16 +1,16 @@ import chai from 'chai' -import RetailcrmBotApiClient from '../dist/index' +import MgBotApiClient from '../index' describe('#Constructor', function () { it('Empty url', function () { chai.expect(function() { - new RetailcrmBotApiClient({token: 'test_token'}); + new MgBotApiClient({token: 'test_token'}); }).to.throw('Url is required'); }); it('Incorrect url', function () { chai.expect(function() { - new RetailcrmBotApiClient({ + new MgBotApiClient({ host: 'http://api.example.com', token: 'test_token' }); @@ -19,7 +19,7 @@ describe('#Constructor', function () { it('Empty token', function () { chai.expect(function() { - new RetailcrmBotApiClient({host: 'https://api.example.com'}); + new MgBotApiClient({host: 'https://api.example.com'}); }).to.throw('Token is required'); }); }); diff --git a/tests/test_v1_client.js b/tests/test_v1_client.js index 577646e..0b2c396 100644 --- a/tests/test_v1_client.js +++ b/tests/test_v1_client.js @@ -1,13 +1,13 @@ import nock from 'nock' import chai from 'chai' -import RetailcrmBotApiClient from '../index' +import MgBotApiClient from '../index' describe('#API client v1', function() { beforeEach(function() { nock.cleanAll(); }); - const retailcrm = new RetailcrmBotApiClient({ + const api = new MgBotApiClient({ host: 'https://api.example.com', token: 'test_token' }).client; @@ -18,7 +18,7 @@ describe('#API client v1', function() { isActive: true }]); - retailcrm.getBots().then(function (value) { + api.getBots().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.not.empty; }); @@ -27,7 +27,7 @@ describe('#API client v1', function() { it('Get empty bots list', function () { nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, []); - retailcrm.getBots().then(function (value) { + api.getBots().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.empty; }); @@ -38,7 +38,7 @@ describe('#API client v1', function() { id: 1 }]); - retailcrm.getChannels().then(function (value) { + api.getChannels().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.not.empty; }); @@ -47,7 +47,7 @@ describe('#API client v1', function() { it('Get empty channels list', function () { nock('https://api.example.com/api/bot/v1').get('/channels').reply(200, []); - retailcrm.getChannels().then(function (value) { + api.getChannels().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.empty; }); @@ -59,7 +59,7 @@ describe('#API client v1', function() { id: 1 }]); - retailcrm.getChats().then(function (value) { + api.getChats().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.not.empty; }); @@ -68,7 +68,7 @@ describe('#API client v1', function() { it('Get empty chats list', function () { nock('https://api.example.com/api/bot/v1').get('/chats').reply(200, []); - retailcrm.getChats().then(function (value) { + api.getChats().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.empty; }); @@ -81,7 +81,7 @@ describe('#API client v1', function() { id: 1 }]); - retailcrm.getCustomers().then(function (value) { + api.getCustomers().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.not.empty; }); @@ -90,7 +90,7 @@ describe('#API client v1', function() { it('Get empty customers list', function () { nock('https://api.example.com/api/bot/v1').get('/customers').reply(200, []); - retailcrm.getCustomers().then(function (value) { + api.getCustomers().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.empty; }); @@ -102,7 +102,7 @@ describe('#API client v1', function() { id: 1 }]); - retailcrm.getDialogs().then(function (value) { + api.getDialogs().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.not.empty; }); @@ -111,7 +111,7 @@ describe('#API client v1', function() { it('Get empty dialogs list', function () { nock('https://api.example.com/api/bot/v1').get('/dialogs').reply(200, []); - retailcrm.getDialogs().then(function (value) { + api.getDialogs().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.empty; }); @@ -122,7 +122,7 @@ describe('#API client v1', function() { id: 1 }]); - retailcrm.getMembers().then(function (value) { + api.getMembers().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.not.empty; }); @@ -131,7 +131,7 @@ describe('#API client v1', function() { it('Get empty members list', function () { nock('https://api.example.com/api/bot/v1').get('/members').reply(200, []); - retailcrm.getMembers().then(function (value) { + api.getMembers().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.empty; }); @@ -145,7 +145,7 @@ describe('#API client v1', function() { } }); - retailcrm.assignDialog(1, { + api.assignDialog(1, { manager_id: 1 }).then(function (value) { chai.expect(value).to.be.an('object'); @@ -153,19 +153,19 @@ describe('#API client v1', function() { }); it('Assign dialog incorrect', function () { - chai.expect(retailcrm.assignDialog.bind(retailcrm)).to.throw('Parameter `dialog_id` is required'); + chai.expect(api.assignDialog.bind(api)).to.throw('Parameter `dialog_id` is required'); }); it('Close dialog', function () { nock('https://api.example.com/api/bot/v1').delete('/dialogs/1/close').reply(200, {}); - retailcrm.closeDialog(1).then(function (value) { + api.closeDialog(1).then(function (value) { chai.expect(value).to.be.empty; }); }); it('Close dialog incorrect', function () { - chai.expect(retailcrm.closeDialog.bind(retailcrm)).to.throw('Parameter `dialog_id` is required'); + chai.expect(api.closeDialog.bind(api)).to.throw('Parameter `dialog_id` is required'); }); it('Send message', function () { @@ -178,7 +178,7 @@ describe('#API client v1', function() { message_id: 1 }); - retailcrm.sendMessage({ + api.sendMessage({ chat_id: 1, scope: 'public', type: 'text', @@ -190,7 +190,7 @@ describe('#API client v1', function() { }); it('Send message incorrect', function () { - chai.expect(retailcrm.sendMessage.bind(retailcrm)).to.throw('Body is not be empty'); + chai.expect(api.sendMessage.bind(api)).to.throw('Body is not be empty'); }); it('Get messages', function() { @@ -202,7 +202,7 @@ describe('#API client v1', function() { } }]); - retailcrm.getMessages().then(function (value) { + api.getMessages().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.not.empty; }); @@ -211,7 +211,7 @@ describe('#API client v1', function() { it('Get empty messages', function () { nock('https://api.example.com/api/bot/v1').get('/messages').reply(200, []); - retailcrm.getMessages().then(function (value) { + api.getMessages().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.empty; }); @@ -220,13 +220,13 @@ describe('#API client v1', function() { it('Delete message', function () { nock('https://api.example.com/api/bot/v1').delete('/messages/1').reply(200, {}); - retailcrm.deleteMessage(1).then(function (value) { + api.deleteMessage(1).then(function (value) { chai.expect(value).to.be.empty; }); }); it('Delete message incorrect', function () { - chai.expect(retailcrm.deleteMessage.bind(retailcrm)).to.throw('Parameter `message_id` is required'); + chai.expect(api.deleteMessage.bind(api)).to.throw('Parameter `message_id` is required'); }); it('Edit message', function () { @@ -234,7 +234,7 @@ describe('#API client v1', function() { content: 'tests message' }).reply(200, {}); - retailcrm.editMessage(1, { + api.editMessage(1, { content: 'tests message' }).then(function (value) { chai.expect(value).to.be.empty; @@ -242,7 +242,7 @@ describe('#API client v1', function() { }); it('Edit message incorrect', function () { - chai.expect(retailcrm.editMessage.bind(retailcrm)).to.throw('Parameter `message_id` is required'); + chai.expect(api.editMessage.bind(api)).to.throw('Parameter `message_id` is required'); }); it('Get commands', function () { @@ -251,7 +251,7 @@ describe('#API client v1', function() { name: 'Command name' }]); - retailcrm.getCommands().then(function (value) { + api.getCommands().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.not.empty; }); @@ -260,7 +260,7 @@ describe('#API client v1', function() { it('Get empty commands', function () { nock('https://api.example.com/api/bot/v1').get('/my/commands').reply(200, []); - retailcrm.getCommands().then(function (value) { + api.getCommands().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.empty; }); @@ -272,7 +272,7 @@ describe('#API client v1', function() { name: 'name' }).reply(200, {}); - retailcrm.editCommand('command', { + api.editCommand('command', { description: 'Desc', name: 'name' }).then(function (value) { @@ -281,20 +281,20 @@ 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(api.editCommand.bind(api, 'command')).to.throw('Body is not be empty'); + chai.expect(api.editCommand.bind(api)).to.throw('Parameter `command_name` is required'); }); it('Delete command', function () { nock('https://api.example.com/api/bot/v1').delete('/my/commands/command').reply(200, {}); - retailcrm.deleteCommand('command').then(function (value) { + api.deleteCommand('command').then(function (value) { chai.expect(value).to.be.empty; }); }); it('Delete command incorrect', function () { - chai.expect(retailcrm.deleteCommand.bind(retailcrm)).to.throw('Parameter `command_name` is required'); + chai.expect(api.deleteCommand.bind(api)).to.throw('Parameter `command_name` is required'); }); it('Update bot info', function () { @@ -303,7 +303,7 @@ describe('#API client v1', function() { name: 'Bot' }).reply(200, {}); - retailcrm.info({ + api.info({ avatar_url: 'http://tests.ru/avatar.png', name: 'Bot' }).then(function (value) { @@ -312,7 +312,7 @@ describe('#API client v1', function() { }); it('Update bot info incorrect', function () { - chai.expect(retailcrm.info.bind(retailcrm)).to.throw('Body is not be empty'); + chai.expect(api.info.bind(api)).to.throw('Body is not be empty'); }); it('Get users', function () { @@ -321,7 +321,7 @@ describe('#API client v1', function() { name: 'Username' }]); - retailcrm.getUsers().then(function (value) { + api.getUsers().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.not.empty; }); @@ -330,14 +330,14 @@ describe('#API client v1', function() { it('Get empty users', function () { nock('https://api.example.com/api/bot/v1').get('/users').reply(200, []); - retailcrm.getUsers().then(function (value) { + api.getUsers().then(function (value) { chai.expect(value).to.be.an('array'); chai.expect(value).to.be.empty; }); }); it('Get websocket data', function () { - const wsData = retailcrm.getWebsocketData(['message_new', 'message_updated']); + const wsData = api.getWebsocketData([MgBotApiClient.types().wsMessageNew, MgBotApiClient.types().wsMessageUpdated]); const expectedUrl = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated'; const expectedHeaders = {'X-Bot-Token': 'test_token'}; @@ -346,6 +346,6 @@ describe('#API client v1', function() { }); it('Get websocket url incorrect', function () { - chai.expect(retailcrm.getWebsocketData.bind(retailcrm)).to.throw('Events is required'); + chai.expect(api.getWebsocketData.bind(api)).to.throw('Events is required'); }); }); From d0b31da80d7b4a2b81ee984f8c8990cdb096d226 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Wed, 6 Mar 2019 14:11:28 +0300 Subject: [PATCH 3/4] Fix package.json --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 13f28f5..2143ff7 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ ], "version": "1.0.1", "scripts": { - "doc": "jsdoc lib/v1/client.js lib/types.js -R README.md", + "doc": "./node_modules/.bin/jsdoc lib/v1/client.js lib/consts.js -R README.md", "build": "./node_modules/.bin/rollup -c", "test": "./node_modules/.bin/_mocha --require @babel/register ./tests/." }, @@ -27,6 +27,7 @@ "@babel/register": "^7.0.0", "babelrc-rollup": "^3.0.0", "chai": "^4.2.0", + "jsdoc": "^3.5.5", "mocha": "^5.2.0", "nock": "^10.0.6", "rollup": "^1.2.2", From 6edec6141c6f07ca536e028cafac16d70763afa1 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Wed, 6 Mar 2019 16:38:47 +0300 Subject: [PATCH 4/4] update class name --- README.md | 12 ++++++------ lib/consts.js | 52 +++++++++++++++++++++++++-------------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 5555edf..5d50505 100644 --- a/README.md +++ b/README.md @@ -15,17 +15,17 @@ In your file ###### CommonJS ``` -var RetailcrmBotApiClient = require('mg-api-client'); +var MgBotApiClient = require('mg-api-client'); ``` ###### es6 ``` -import RetailcrmBotApiClient from 'mg-api-client'; +import MgBotApiClient from 'mg-api-client'; ``` # Usage #### Get users ```javascript -const api = new RetailcrmBotApiClient({ +const api = new MgBotApiClient({ host: 'https://api.example.com', token: 'your bot token', apiVersion: 'v1' // optional @@ -42,7 +42,7 @@ api.getUsers() #### Send message ```javascript -const api = new RetailcrmBotApiClient({ +const api = new MgBotApiClient({ host: 'https://api.example.com', token: 'your bot token', apiVersion: 'v1' // optional @@ -67,13 +67,13 @@ api.sendMessage(message) ```javascript const WebSocket = require('ws'); -const api = new RetailcrmBotApiClient({ +const api = new MgBotApiClient({ host: 'https://api.example.com', token: 'your bot token', apiVersion: 'v1' // optional }).client; -const wsData = api.getWebsocketData([RetailcrmBotApiClient.types().wsMessageNew]); +const wsData = api.getWebsocketData([MgBotApiClient.types().wsMessageNew]); const ws = new WebSocket(wsData.get('url'), { headers: wsData.get('headers') }); diff --git a/lib/consts.js b/lib/consts.js index 928ac15..d600510 100644 --- a/lib/consts.js +++ b/lib/consts.js @@ -2,7 +2,7 @@ * New message websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsMessageNew + * @example MgBotApiClient.types().wsMessageNew */ export const wsMessageNew = 'message_new'; @@ -10,7 +10,7 @@ export const wsMessageNew = 'message_new'; * Updating message websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsMessageUpdated + * @example MgBotApiClient.types().wsMessageUpdated */ export const wsMessageUpdated = 'message_updated'; @@ -18,7 +18,7 @@ export const wsMessageUpdated = 'message_updated'; * Websocket event delete message * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsMessageDeleted + * @example MgBotApiClient.types().wsMessageDeleted */ export const wsMessageDeleted = 'message_deleted'; @@ -26,7 +26,7 @@ export const wsMessageDeleted = 'message_deleted'; * Dialogue opening websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsDialogOpened + * @example MgBotApiClient.types().wsDialogOpened * */ export const wsDialogOpened = 'dialog_opened'; @@ -35,7 +35,7 @@ export const wsDialogOpened = 'dialog_opened'; * Dialogue closing websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsDialogClosed + * @example MgBotApiClient.types().wsDialogClosed */ export const wsDialogClosed = 'dialog_closed'; @@ -43,7 +43,7 @@ export const wsDialogClosed = 'dialog_closed'; * Dialogue appointment websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsDialogAssign + * @example MgBotApiClient.types().wsDialogAssign */ export const wsDialogAssign = 'dialog_assign'; @@ -51,7 +51,7 @@ export const wsDialogAssign = 'dialog_assign'; * Chat creating websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsChatCreated + * @example MgBotApiClient.types().wsChatCreated */ export const wsChatCreated = 'chat_created'; @@ -59,7 +59,7 @@ export const wsChatCreated = 'chat_created'; * Chat updating websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsChatUpdated + * @example MgBotApiClient.types().wsChatUpdated */ export const wsChatUpdated = 'chat_updated'; @@ -67,7 +67,7 @@ export const wsChatUpdated = 'chat_updated'; * Unread chat updating websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsChatUnreadUpdated + * @example MgBotApiClient.types().wsChatUnreadUpdated */ export const wsChatUnreadUpdated = 'chat_unread_updated'; @@ -75,7 +75,7 @@ export const wsChatUnreadUpdated = 'chat_unread_updated'; * User online status updating websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsUserOnlineUpdated + * @example MgBotApiClient.types().wsUserOnlineUpdated */ export const wsUserOnlineUpdated = 'user_online_updated'; @@ -83,7 +83,7 @@ export const wsUserOnlineUpdated = 'user_online_updated'; * User joined chat websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsUserJoinedChat + * @example MgBotApiClient.types().wsUserJoinedChat */ export const wsUserJoinedChat = 'user_joined_chat'; @@ -91,7 +91,7 @@ export const wsUserJoinedChat = 'user_joined_chat'; * User left chat websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsUserLeftChat + * @example MgBotApiClient.types().wsUserLeftChat * */ export const wsUserLeftChat = 'user_left_chat'; @@ -100,7 +100,7 @@ export const wsUserLeftChat = 'user_left_chat'; * User updating websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsUserUpdated + * @example MgBotApiClient.types().wsUserUpdated */ export const wsUserUpdated = 'user_updated'; @@ -108,7 +108,7 @@ export const wsUserUpdated = 'user_updated'; * Customer updating websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsCustomerUpdated + * @example MgBotApiClient.types().wsCustomerUpdated */ export const wsCustomerUpdated = 'customer_updated'; @@ -116,7 +116,7 @@ export const wsCustomerUpdated = 'customer_updated'; * Bot updating websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsBotUpdated + * @example MgBotApiClient.types().wsBotUpdated * */ export const wsBotUpdated = 'bot_updated'; @@ -125,7 +125,7 @@ export const wsBotUpdated = 'bot_updated'; * Channel updating websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsChannelUpdated + * @example MgBotApiClient.types().wsChannelUpdated */ export const wsChannelUpdated = 'channel_updated'; @@ -133,7 +133,7 @@ export const wsChannelUpdated = 'channel_updated'; * Settings updating websocket event * @constant * @type {string} - * @example RetailcrmBotApiClient.types().wsSettingsUpdated + * @example MgBotApiClient.types().wsSettingsUpdated */ export const wsSettingsUpdated = 'settings_updated'; @@ -142,7 +142,7 @@ export const wsSettingsUpdated = 'settings_updated'; * Public message scope * @constant * @type {string} - * @example RetailcrmBotApiClient.types().messageScopePublic + * @example MgBotApiClient.types().messageScopePublic */ export const messageScopePublic = 'public'; @@ -150,7 +150,7 @@ export const messageScopePublic = 'public'; * Public message scope * @constant * @type {string} - * @example RetailcrmBotApiClient.types().messageScopePrivate + * @example MgBotApiClient.types().messageScopePrivate */ export const messageScopePrivate = 'private'; @@ -159,7 +159,7 @@ export const messageScopePrivate = 'private'; * Text message type * @constant * @type {string} - * @example RetailcrmBotApiClient.types().msgTypeText + * @example MgBotApiClient.types().msgTypeText */ export const msgTypeText = 'text'; @@ -167,7 +167,7 @@ export const msgTypeText = 'text'; * System message type * @constant * @type {string} - * @example RetailcrmBotApiClient.types().msgTypeSystem + * @example MgBotApiClient.types().msgTypeSystem */ export const msgTypeSystem = 'system'; @@ -175,7 +175,7 @@ export const msgTypeSystem = 'system'; * Command message type * @constant * @type {string} - * @example RetailcrmBotApiClient.types().msgTypeCommand + * @example MgBotApiClient.types().msgTypeCommand */ export const msgTypeCommand = 'command'; @@ -183,7 +183,7 @@ export const msgTypeCommand = 'command'; * Order message type * @constant * @type {string} - * @example RetailcrmBotApiClient.types().msgTypeOrder + * @example MgBotApiClient.types().msgTypeOrder */ export const msgTypeOrder = 'order'; @@ -191,7 +191,7 @@ export const msgTypeOrder = 'order'; * Product message type * @constant * @type {string} - * @example RetailcrmBotApiClient.types().msgTypeProduct + * @example MgBotApiClient.types().msgTypeProduct */ export const msgTypeProduct = 'product'; @@ -199,7 +199,7 @@ export const msgTypeProduct = 'product'; * File message type * @constant * @type {string} - * @example RetailcrmBotApiClient.types().msgTypeFile + * @example MgBotApiClient.types().msgTypeFile */ export const msgTypeFile = 'file'; @@ -207,6 +207,6 @@ export const msgTypeFile = 'file'; * Image message type * @constant * @type {string} - * @example RetailcrmBotApiClient.types().msgTypeImage + * @example MgBotApiClient.types().msgTypeImage */ export const msgTypeImage = 'image';