UNPKG

@eagleeye-solutions/integration-events-common

Version:
87 lines 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EeAirClient = void 0; const types_1 = require("../../types"); const exceptions_1 = require("../../exceptions"); const utils_1 = require("./utils"); class EeAirClient { clientId; clientSecret; domains; logger; constructor(clientId, clientSecret, domains, logger) { this.clientId = clientId; this.clientSecret = clientSecret; this.domains = domains; this.logger = logger; } async makeApiRequest(requestParams) { const headers = { 'Content-Type': 'application/json', 'X-EES-AUTH-CLIENT-ID': this.clientId, 'caller-unique-id': requestParams.headers['called-unique-id'], 'X-EES-AUTH-HASH': (0, utils_1.generateAuthenticationHash)(this.clientSecret, requestParams.url, requestParams.body ?? ''), ...requestParams.headers, }; const t0 = performance.now(); const response = await fetch(requestParams.url, { method: requestParams.method, body: requestParams.body, headers, }); const t1 = performance.now(); const calledUniqueId = (0, utils_1.getEesCalledUniqueIdHeader)(response); this.logger.info(`${requestParams.method} ${requestParams.url}: ${response.status} ${response.statusText} (${t1 - t0}ms)`); if (response.ok) { const contentType = response.headers.get('Content-Type'); let data; if (contentType && contentType.includes('application/json')) { data = await response.json(); this.logger.debug(`Response Body: ${JSON.stringify(data)}`); } else { data = await response.text(); } return { data, calledUniqueId }; } else { const errorContext = { url: requestParams.url, body: requestParams.body, data: await response.text(), headers: JSON.stringify(headers), }; // In pino v9 the logger's methods accept (obj?, msg?, ...args). Avoid // passing multiple non-object params; include details in the object. this.logger.error({ context: errorContext, status: response.status, statusText: response.statusText, uniqueCallId: (0, utils_1.getEesCalledUniqueIdHeader)(response), scope: EeAirClient.name, }, 'EE API returned error'); // May need tweaking based on endpoints handled and their expected errors. switch (response.status) { case 404: throw new exceptions_1.PermanentDeliveryFailure("The customer identity doesn't exist in EE AIR Platform."); case 401: throw new exceptions_1.PermanentDeliveryFailure('401 Unauthorized'); case 400: throw new exceptions_1.PermanentDeliveryFailure('The request could not be processed by the EE AIR Platform.'); default: throw new exceptions_1.TemporaryDeliveryFailure('The request failed to be processed by the EE AIR Platform due to an unexpected error.'); } } } async getWalletTransactionById(walletId, transactionId) { const getWalletTransactionByIdRequest = { method: 'GET', url: new URL(`/wallet/${walletId}/transaction/${transactionId}`, this.domains['wallet']).href, headers: {}, }; const { data: walletTransaction } = await this.makeApiRequest(getWalletTransactionByIdRequest); return types_1.WalletTransactionEntityObjectValueSchema.parse(walletTransaction); } } exports.EeAirClient = EeAirClient; //# sourceMappingURL=ee-air-client.js.map