liveperson-functions-client
Version:
JavaScript client for LivePerson Functions.
91 lines • 3.72 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const isImplementedCache_1 = require("./../helper/isImplementedCache");
const guid_typescript_1 = require("guid-typescript");
const baseClient_1 = require("./baseClient");
const clientConfig_1 = require("./clientConfig");
const csdsClient_1 = require("../helper/csdsClient");
const networkErrors_1 = require("../helper/networkErrors");
const got_1 = __importDefault(require("got"));
const getTooling = (config, tooling) => {
const customTooling = tooling || {};
const defaultGetCsdsEntry = () => {
const csdsClient = new csdsClient_1.CsdsClient(config.csdsTtlSeconds);
return csdsClient.get.bind(csdsClient);
};
const getCsdsEntry = customTooling.getCsdsEntry || defaultGetCsdsEntry();
const metricCollector = customTooling.metricCollector;
const defaultFetch = async ({ url, body, headers, method }) => {
try {
const resp = await (0, got_1.default)(url, {
...(method === 'post' && { body: Buffer.from(JSON.stringify(body)) }),
method,
headers,
resolveBodyOnly: false,
responseType: 'json',
throwHttpErrors: true,
timeout: {
request: config.timeout,
},
retry: {
limit: 3,
methods: ['GET', 'POST'],
errorCodes: networkErrors_1.RETRIABLE_ERRORS,
calculateDelay: ({ computedValue, attemptCount }) => {
return computedValue ? attemptCount * 350 : 0;
},
},
});
return {
url,
headers: resp.headers,
body: resp.body,
ok: true,
status: resp.statusCode,
statusText: resp.statusMessage,
};
}
catch (error) {
const { response: resp, request: req } = error;
return {
url,
headers: resp === null || resp === void 0 ? void 0 : resp.headers,
body: resp === null || resp === void 0 ? void 0 : resp.body,
ok: false,
status: resp === null || resp === void 0 ? void 0 : resp.statusCode,
statusText: resp === null || resp === void 0 ? void 0 : resp.statusMessage,
retryCount: req === null || req === void 0 ? void 0 : req.retryCount,
};
}
};
const isImplementedCache = new isImplementedCache_1.IsImplementedCache(config.isImplementedCacheDurationInSeconds);
const defaultGenerateId = () => guid_typescript_1.Guid.create().toString();
return {
getCsdsEntry,
fetch: customTooling.fetch || defaultFetch,
generateId: customTooling.generateId || defaultGenerateId,
isImplementedCache,
metricCollector,
};
};
/**
* The FaaS Client.
*/
class Client extends baseClient_1.BaseClient {
/**
* Default constructor, creates a FaaS client.
*
* @param config The client configuration.
* @param tooling Optional tooling used internally in the client, usually only needed for debugging/testing purposes.
*/
constructor(config, tooling) {
const configuration = { ...clientConfig_1.defaultConfig, ...config };
super(configuration, getTooling(configuration, tooling));
}
}
exports.Client = Client;
//# sourceMappingURL=client.js.map