UNPKG

electerm-sync

Version:
100 lines (99 loc) 2.91 kB
// based on tyler's work: https://github.com/tylerlong/ringcentral-js-concise import axios from 'axios'; import { sign } from 'jsonwebtoken'; const version = process.env.version || ''; export class HTTPError extends Error { constructor(status, statusText, data, config) { super(`status: ${status} statusText: ${statusText} data: ${JSON.stringify(data, null, 2)} config: ${JSON.stringify(config, null, 2)}`); this.status = status; this.statusText = statusText; this.data = data; this.config = config; } } class ElectermSync { constructor(str, userAgentHeader = `electerm-sync/v${version}`) { const [token, server, userId = '', algorithm = 'HS256'] = str.split('####'); this.token = token; this.server = server; this.userId = userId; this.algorithm = algorithm; this.userAgentHeader = userAgentHeader; this._axios = axios.create(); const request = this._axios.request.bind(this._axios); this._axios.request = async (config) => { try { return await request(config); } catch (e) { if (e.response) { throw new HTTPError(e.response.status, e.response.statusText, e.response.data, e.response.config); } else { throw e; } } }; } async request(config) { return await this._axios.request({ ...config, headers: this._patchHeaders(config.headers) }); } async create(data, conf) { return await this.request({ url: this.server, method: 'PUT', data, ...conf }); } async update(userId, data, conf) { this.userId = userId; return await this.request({ url: this.server, method: 'PUT', data, ...conf }); } async getOne(userId, conf) { this.userId = userId; return await this.request({ url: this.server, method: 'GET', ...conf }); } async test(conf) { return await this.request({ url: this.server, method: 'POST', ...conf }); } _patchHeaders(headers = {}) { return { 'Content-Type': 'application/json', ...this._authHeader(), 'X-User-Agent': this.userAgentHeader, ...headers }; } _authHeader() { const tk = this.userId !== '' ? sign({ id: this.userId, exp: Date.now() + 1000000 }, this.token, { algorithm: this.algorithm }) : this.token; return { Authorization: `Bearer ${tk}` }; } } export default ElectermSync;