UNPKG

@remote.it/core

Version:

Core remote.it JavasScript/TypeScript library

75 lines 2.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var debug_1 = __importDefault(require("debug")); var axios_1 = __importDefault(require("axios")); var Logger_1 = require("./Logger"); var d = debug_1.default('remoteit:API'); var API_URL = 'https://api01.remot3.it/apv/v27'; var API_KEY = 'remote.it.developertoolsHW9iHnd'; var API = /** @class */ (function () { function API() { } // constructor(args: { token?: string }) { // this.token = args.token // } API.post = function (url, data) { return this.request('post', url, data); }; API.get = function (url) { return this.request('get', url); }; API.request = function (method, url, data) { // if (!this.token) throw new Error('No authentication token provided!') var _this = this; var headers = { apikey: API_KEY, 'Content-Type': 'application/json' }; if (this.token) headers.token = this.token; var request = { method: method, url: API_URL + url, data: data, headers: headers, }; return axios_1.default .request(request) .then(function (d) { return _this.processData(d); }, function (e) { return _this.processError(e); }) .catch(function (error) { var data = { message: error.message, method: request.method, url: request.url, data: request.data, headers: request.headers, }; if (error.response) { data.body = error.response.config.data; data.response = error.response.data; } d('request error:', data); Logger_1.Logger.error('request error', data); throw error; }); }; API.processData = function (resp) { if (resp === void 0) { resp = {}; } if (resp.data && resp.data.status && resp.data.reason && resp.data.status === 'false') { throw new Error(resp.data.reason); } return resp.data; }; API.processError = function (error) { if (error.response && error.response.data && error.response.data.reason) { error.message = error.response.data.reason; } throw error; }; return API; }()); exports.API = API; //# sourceMappingURL=API.js.map