UNPKG

vcloud-client

Version:

Library which consumes directly VCloud API

56 lines (49 loc) 1.62 kB
const https = require('https'); const axios = require('axios'); const TimeCalculation = require('../../model/time-calculation'); const serviceConfiguration = (service, url, token, refreshToken = "") => { return { url: url, token: token, refreshToken: refreshToken, expires: configExpirationToken(token), checkInternalData: function() { if (!this.url || !this.token) { throw new Error(`No se puede establecer la conexión con ${service} url: ${this.url}, token: ${this.token}`); } }, init: function() { if (this.petitionsCounter === 0) { this.timeElapsed = new TimeCalculation(); } }, petitionsCounter: 0, time: undefined, resetPetitions: function() { this.petitionsCounter = 0; this.timeElapsed = new TimeCalculation(); }, timeElapsed: undefined } } const configExpirationToken = (token) => { try{ const parsed = parseJwt(token); return parsed?.exp ? new Date((parsed.exp * 1000) - (600 * 1000)) : undefined; } catch(e) { console.log('Error configuring exp date'); } return undefined; } const parseJwt = (token) => { return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()); } const instance = () => { https.globalAgent.options.rejectUnauthorized = false; return axios.create({ httpsAgent: new https.Agent({ rejectUnauthorized: false }) }); } module.exports = {instance, serviceConfiguration};