UNPKG

@elastic.io/component-commons-library

Version:
88 lines (87 loc) 4.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FacelessRestClient = void 0; const http_1 = __importDefault(require("http")); const https_1 = __importDefault(require("https")); const axios_1 = __importDefault(require("axios")); const remove_trailing_slash_1 = __importDefault(require("remove-trailing-slash")); const remove_leading_slash_1 = __importDefault(require("remove-leading-slash")); const externalApi_1 = require("../externalApi"); const PlatformApiLogicClient_1 = require("../platformApi/PlatformApiLogicClient"); class FacelessRestClient { constructor(emitter, cfg, userAgent, msgId) { this.emitter = emitter; this.cfg = cfg; this.axiosInst = axios_1.default.create({ httpAgent: new http_1.default.Agent({ keepAlive: true }), httpsAgent: new https_1.default.Agent({ keepAlive: true }), }); this.cfg.resourceServerUrl = cfg.resourceServerUrl; this.accessToken = null; this.logger = emitter.logger; this.platformClient = new PlatformApiLogicClient_1.PlatformApiLogicClient(emitter, cfg, userAgent, msgId); } async addAuthenticationToRequestOptions(requestOptions) { if (!this.accessToken) { const { secretId } = this.cfg; if (secretId) { this.logger.debug('Fetching credentials by secretId'); const secret = await this.platformClient.fetchSecretById({ secretId }); this.accessToken = secret.attributes.credentials.access_token; } else { this.logger.debug('Fetching credentials from this.cfg'); this.accessToken = this.cfg.oauth.access_token; } } // eslint-disable-next-line no-param-reassign requestOptions.headers.Authorization = `Bearer ${this.accessToken}`; } // options expects the following sub-variables: // url: Url to call // method: HTTP verb to use // data: Body of the request, if applicable. Defaults to undefined. // headers: Any HTTP headers to add to the request. Defaults to {} // urlIsSegment: Whether to append to the base server url or // if the provided URL is an absolute path. Defaults to true async makeRequest(options) { var _a, _b; const { url, method, data, headers = {}, urlIsSegment = true, } = options; const urlToCall = urlIsSegment ? `${(0, remove_trailing_slash_1.default)(this.cfg.resourceServerUrl.trim())}/${(0, remove_leading_slash_1.default)(url.trim())}` // Trim trailing or leading '/' : url.trim(); this.logger.debug(`Making ${method} request...`); const requestOptions = { method, url: urlToCall, data, headers, }; let error; let currentRetry = 0; const facelessRetriesCount = (0, externalApi_1.getFacelessRetriesCount)(); while (currentRetry < facelessRetriesCount) { await this.addAuthenticationToRequestOptions(requestOptions); try { const response = await externalApi_1.axiosReqWithRetryOnServerError.call(this, requestOptions, this.axiosInst); return response; } catch (err) { this.logger.error((0, externalApi_1.getErrMsg)(err.response)); error = err; if (((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) < 500 && ((_b = err.response) === null || _b === void 0 ? void 0 : _b.status) !== 401) { throw err; } this.logger.info(`Request failed, faceless retrying(${1 + currentRetry})`); this.accessToken = null; currentRetry++; } } this.logger.error('The number of attempts to receive the proper token has been exhausted.'); throw error; } } exports.FacelessRestClient = FacelessRestClient;