UNPKG

@eagleeye-solutions/integration-events-common

Version:
75 lines 3.4 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, '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(); this.logger.info(`${requestParams.method} ${requestParams.url}: ${response.status} ${response.statusText} (${t1 - t0}ms)`); if (response.ok) { const contentType = response.headers.get('Content-Type'); if (contentType && contentType.includes('application/json')) { const responseJson = await response.json(); this.logger.debug(`Response Body: ${JSON.stringify(responseJson)}`); return responseJson; } else { return await response.text(); } } else { this.logger.error(`EE API returned error with status: ${response.status} and Unique Call ID: ${(0, utils_1.getEesCalledUniqueIdHeader)(response)}`, { url: requestParams.url, body: requestParams.body, data: await response.text(), headers: JSON.stringify(headers), }, EeAirClient.name); // 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 walletTransaction = await this.makeApiRequest(getWalletTransactionByIdRequest); return types_1.WalletTransactionEntityObjectValueSchema.parse(walletTransaction); } } exports.EeAirClient = EeAirClient; //# sourceMappingURL=ee-air-client.js.map