liveperson-functions-client
Version:
JavaScript client for LivePerson Functions.
84 lines • 2.85 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CsdsClient = void 0;
const got_1 = __importDefault(require("got"));
const VError = require("verror");
class CsdsClient {
/**G
* @param ttlInSeconds TTL of the domains cache in seconds
*/
constructor(ttlInSeconds = 600) {
this.ttlInSeconds = ttlInSeconds;
this.domainCache = new Map();
}
async get(accountId, service) {
const domains = await this.getCachedDomains(accountId);
const domain = domains.find(({ service: s }) => s === service);
if (domain) {
return domain.baseURI;
}
throw new VError({
name: 'CSDSDomainNotFound',
}, `Service "${service}" could not be found`);
}
async getCachedDomains(accountId) {
if (!this.isCacheExpired(accountId)) {
const cache = this.domainCache.get(accountId);
return cache.domains;
}
try {
const url = this.getUrl(accountId);
const { baseURIs } = await (0, got_1.default)(url, {
responseType: 'json',
throwHttpErrors: true,
resolveBodyOnly: true,
});
if (baseURIs && baseURIs.length !== 0) {
this.domainCache.set(accountId, {
lastCacheTimestamp: Date.now(),
domains: baseURIs,
});
return baseURIs;
}
return [];
}
catch (error) {
throw new VError({
cause: error,
name: 'CSDSFailure',
}, 'Error while fetching CSDS entries');
}
}
isCacheExpired(accountId) {
const cache = this.domainCache.get(accountId);
if (cache) {
return Date.now() > cache.lastCacheTimestamp + this.ttlInSeconds * 1000;
}
return true;
}
getUrl(accountId) {
return `http://${this.getCsdsDomain(accountId)}/api/account/${accountId}/service/baseURI.json?version=1.0`;
}
getCsdsDomain(accountId) {
if (accountId.startsWith('le') ||
accountId.startsWith('qa') ||
accountId.startsWith('c') // new QA accounts
) {
return 'csds-app.qa.int.gw.lpcloud.io';
}
if (accountId.startsWith('fr')) {
return 'adminlogin-z0-intg.liveperson.net';
}
// new alpha
if (accountId.startsWith('a')) {
return 'adminlogin-a.liveperson.net';
}
// alpha/production
return 'adminlogin.liveperson.net';
}
}
exports.CsdsClient = CsdsClient;
//# sourceMappingURL=csdsClient.js.map