1
0
mirror of synced 2024-11-22 04:26:02 +03:00
mg-bot-api-client-js/lib/request.js
2020-08-24 11:17:55 +03:00

190 lines
4.2 KiB
JavaScript

'use strict';
import url from 'url'
import https from 'https'
import querystring from 'querystring'
export default class Request {
/**
* @param {Object} options
* @constructor
*/
constructor(options) {
/**
* @prop System host
* @type {string}
* @private
*/
this._host = url.parse(options.host).host;
/**
* @prop Bot token
* @type {*|string|string}
* @private
*/
this._token = options.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
* @param {boolean} serializable
* @returns {Promise}
* @throws {Error}
* @private
*/
_request(endpoint, method, data = {}, serializable = true) {
let path = this._getPath(endpoint);
let response = '';
if (method === 'GET' && Object.keys(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)) {
let sendData;
if (serializable) {
sendData = JSON.stringify(data);
} else {
sendData = data;
}
request.write(sendData);
}
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
* @param {boolean} serializable
* @returns {Promise}
* @throws {Error}
*/
post(endpoint, data, serializable = true) {
if (!data) {
throw new Error('Body is not be empty');
}
return this._request(endpoint, 'POST', data, serializable);
};
/**
* 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');
}
return this._request(endpoint, 'PATCH', data);
}
/**
* 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');
}
return this._request(endpoint, 'PUT', data);
}
/**
* Method DELETE
* @param {string} endpoint
* @returns {Promise}
*/
delete(endpoint) {
return this._request(endpoint, 'DELETE');
}
/**
* Get api host
* @returns {string | *}
*/
get host() {
return this._host;
}
/**
* Get bot token
* @returns {*|string}
*/
get token() {
return this._token;
}
}