ptokens-providers
Version:
repo holding the code related to network providers
109 lines (103 loc) • 5.28 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/runtime/helpers/toConsumableArray'), require('@babel/runtime/helpers/asyncToGenerator'), require('@babel/runtime/helpers/classCallCheck'), require('@babel/runtime/helpers/createClass'), require('@babel/runtime/regenerator'), require('axios')) :
typeof define === 'function' && define.amd ? define(['exports', '@babel/runtime/helpers/toConsumableArray', '@babel/runtime/helpers/asyncToGenerator', '@babel/runtime/helpers/classCallCheck', '@babel/runtime/helpers/createClass', '@babel/runtime/regenerator', 'axios'], factory) :
(global = global || self, factory(global.Providers = {}, global._toConsumableArray, global._asyncToGenerator, global._classCallCheck, global._createClass, global._regeneratorRuntime, global.axios));
}(this, (function (exports, _toConsumableArray, _asyncToGenerator, _classCallCheck, _createClass, _regeneratorRuntime, axios) { 'use strict';
_toConsumableArray = _toConsumableArray && Object.prototype.hasOwnProperty.call(_toConsumableArray, 'default') ? _toConsumableArray['default'] : _toConsumableArray;
_asyncToGenerator = _asyncToGenerator && Object.prototype.hasOwnProperty.call(_asyncToGenerator, 'default') ? _asyncToGenerator['default'] : _asyncToGenerator;
_classCallCheck = _classCallCheck && Object.prototype.hasOwnProperty.call(_classCallCheck, 'default') ? _classCallCheck['default'] : _classCallCheck;
_createClass = _createClass && Object.prototype.hasOwnProperty.call(_createClass, 'default') ? _createClass['default'] : _createClass;
_regeneratorRuntime = _regeneratorRuntime && Object.prototype.hasOwnProperty.call(_regeneratorRuntime, 'default') ? _regeneratorRuntime['default'] : _regeneratorRuntime;
axios = axios && Object.prototype.hasOwnProperty.call(axios, 'default') ? axios['default'] : axios;
var DEFAULT_TIMEOUT = 10000;
var DEFAULT_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Origin, Content-Type',
'Content-Type': 'application/json'
};
var HttpProvider = function () {
function HttpProvider(_endpoint, _headers) {
_classCallCheck(this, HttpProvider);
this.headers = _headers || DEFAULT_HEADERS;
this.endpoint = _endpoint || null;
this.api = _endpoint ? axios.create({
baseURL: _endpoint,
headers: _headers || DEFAULT_HEADERS
}) : null;
}
_createClass(HttpProvider, [{
key: "call",
value: function () {
var _call = _asyncToGenerator( _regeneratorRuntime.mark(function _callee(_callType, _apiPath) {
var _params,
_timeout,
_this$api,
CancelToken,
options,
details,
_yield$this$api$_call,
data,
_args = arguments;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_params = _args.length > 2 && _args[2] !== undefined ? _args[2] : [];
_timeout = _args.length > 3 && _args[3] !== undefined ? _args[3] : DEFAULT_TIMEOUT;
_context.prev = 2;
CancelToken = axios.CancelToken;
options = {
cancelToken: _timeout ? new CancelToken(function (_cancel) {
return setTimeout(function () {
return _cancel("timeout of ".concat(_timeout, "ms exceeded"));
}, _timeout);
}) : undefined
};
details = [_apiPath].concat(_toConsumableArray(_callType === 'GET' ? [options] : [_params, options]));
_context.next = 8;
return (_this$api = this.api)[_callType.toLowerCase()].apply(_this$api, _toConsumableArray(details));
case 8:
_yield$this$api$_call = _context.sent;
data = _yield$this$api$_call.data;
return _context.abrupt("return", data);
case 13:
_context.prev = 13;
_context.t0 = _context["catch"](2);
throw new Error(_context.t0.message);
case 16:
case "end":
return _context.stop();
}
}
}, _callee, this, [[2, 13]]);
}));
function call(_x, _x2) {
return _call.apply(this, arguments);
}
return call;
}()
}, {
key: "setEndpoint",
value: function setEndpoint(_endpoint) {
this.endpoint = _endpoint;
this.api = axios.create({
baseURL: _endpoint,
headers: this.headers
});
}
}, {
key: "setHeaders",
value: function setHeaders(_headers) {
this.headers = _headers;
this.api = axios.create({
baseURL: this.endpoint,
headers: _headers
});
}
}]);
return HttpProvider;
}();
exports.HttpProvider = HttpProvider;
Object.defineProperty(exports, '__esModule', { value: true });
})));