UNPKG

react-native-unit-components

Version:

Unit React Native components

62 lines (59 loc) 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getMobileWalletPayload = void 0; var _axios = _interopRequireDefault(require("axios")); var _UNNetworkConstants = require("../common/UNNetworkConstants"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // eslint-disable-next-line @typescript-eslint/no-explicit-any const isUNMobileWalletPayload = object => { return 'data' in object && 'attributes' in object.data && 'payload' in object.data.attributes; }; // TODO: use apiClient class for handling requests. const getMobileWalletPayload = async (customerToken, cardId, env, signedNonce) => { if (!signedNonce) return; const headers = { 'Content-Type': _UNNetworkConstants.APIHeaders.CONTENT_TYPE, 'Authorization': `Bearer ${customerToken}` }; const data = { data: { attributes: { signedNonce: signedNonce } } }; const baseURL = `${_UNNetworkConstants.UNIT_SECURE_URL[env]}/cards/${cardId}/mobile-wallet-payload`; try { const response = await _axios.default.post(baseURL, data, { headers: headers }); if (!isUNMobileWalletPayload(response.data)) { const error = { title: `Couldn't get mobile wallet payload. Status:${response.status} Response data:${response.data}` }; throw error; } else { const encryptedPayload = response.data.data.attributes.payload; return encryptedPayload; } } catch (error) { // Converts the error into a UNError if (_axios.default.isAxiosError(error) && error.response) { const unErrors = error.response.data; throw unErrors; } else if (error instanceof Error) { const unexpectedError = { title: 'Unexpected error', error: error }; throw unexpectedError; } throw { title: `Unexpected error: ${error}` }; } }; exports.getMobileWalletPayload = getMobileWalletPayload; //# sourceMappingURL=UNWalletPayloadRequest.js.map