UNPKG

meses-messaging

Version:

Meses messaging SDK in JavaScript

83 lines (72 loc) 3.07 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _superagent = require('superagent'); var _superagent2 = _interopRequireDefault(_superagent); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var HttpClient = function () { function HttpClient() { _classCallCheck(this, HttpClient); } _createClass(HttpClient, null, [{ key: 'buildApiCallGetUrl', /** * Build url for GET method to be send over HTTP. */ value: function buildApiCallGetUrl(spec, url) { var specKeys = Object.keys(spec); if (specKeys == undefined || specKeys.length == 0) { return url; } else { var params = []; specKeys.forEach(function (specKey) { if (Array.isArray(spec[specKey])) { spec[specKey].forEach(function (arrVal) { var specValue = encodeURI(arrVal); params.push(specKey + '=' + specValue); }); } else if (spec[specKey] != undefined) { var specValue = encodeURI(spec[specKey]); params.push(specKey + '=' + specValue); } }); return url + '?' + params.join('&'); } } }, { key: 'executeGet', value: function executeGet(apiUrl, params, context, callback) { var apiGetUrl = this.buildApiCallGetUrl(params, apiUrl); _superagent2.default.get(apiGetUrl).end(function (err, response) { if (err) { callback(err, null); } else if (response.body.status != 'SUCCESS') { callback(response.body.message, null); } else { var data = response.body.data; callback(null, data); } }); } }, { key: 'executePost', value: function executePost(apiUrl, spec, context, callback) { var apiCallSpec = { data: spec, context: {} }; _superagent2.default.post(apiUrl).send(apiCallSpec).set('Content-Type', 'application/json').end(function (err, response) { if (err) { callback(err, null); } else if (response.body.status != 'SUCCESS') { callback(response.body.message, null); } else { var data = response.body.data; callback(null, data); } }); } }]); return HttpClient; }(); exports.default = HttpClient;