liveperson-functions-cli
Version:
LivePerson Functions CLI
74 lines • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CsdsClient = void 0;
const requestGot = require("got");
class CsdsClient {
constructor(got = requestGot) {
this.got = got;
this.ttlInSeconds = 600;
this.lastCacheTimestamp = 0;
this.domains = [];
this.accountId = '';
}
/**
* Returns the desired service domain based on the provided accountId
* @param {string} accountId - accountId
* @param {string} service - Desired service
* @returns {Promise<string>} - csds domain
* @memberof CsdsClient
*/
async getUri(accountId, service) {
this.accountId = accountId;
const domains = await this.getCachedDomains();
const domain = domains.find(({ service: s }) => s === service);
/* istanbul ignore else */
if (domain) {
return domain.baseURI;
}
throw new Error(`Service "${service}" could not be found.`);
}
async getCachedDomains() {
if (!this.isCacheExpired()) {
return this.domains;
}
try {
const { baseURIs } = await this.got(this.getUrl(), {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'user-agent': 'faas-cli',
},
responseType: 'json',
resolveBodyOnly: true,
});
/* istanbul ignore else */
if (baseURIs && baseURIs.length !== 0) {
this.lastCacheTimestamp = Date.now();
this.domains = baseURIs;
return baseURIs;
}
return [];
}
catch (error) {
throw new Error(error.message);
}
}
isCacheExpired() {
return Date.now() > this.lastCacheTimestamp + this.ttlInSeconds * 1000;
}
getUrl() {
return `https://${this.getCsdsDomain()}/api/account/${this.accountId}/service/baseURI.json?version=1.0`;
}
getCsdsDomain() {
var _a, _b, _c;
if (((_a = this.accountId) === null || _a === void 0 ? void 0 : _a.startsWith('le')) || ((_b = this.accountId) === null || _b === void 0 ? void 0 : _b.startsWith('qa'))) {
return 'lp-csds-qa.dev.lprnd.net';
}
if ((_c = this.accountId) === null || _c === void 0 ? void 0 : _c.startsWith('fr')) {
return 'adminlogin-z0-intg.liveperson.net';
}
return 'api.liveperson.net';
}
}
exports.CsdsClient = CsdsClient;
//# sourceMappingURL=csds.service.js.map