react-native-unit-components
Version:
Unit React Native components
54 lines (52 loc) • 1.71 kB
JavaScript
import axios from 'axios';
import { APIHeaders, UNIT_SECURE_URL } from '../common/UNNetworkConstants';
// 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.
export const getMobileWalletPayload = async (customerToken, cardId, env, signedNonce) => {
if (!signedNonce) return;
const headers = {
'Content-Type': APIHeaders.CONTENT_TYPE,
'Authorization': `Bearer ${customerToken}`
};
const data = {
data: {
attributes: {
signedNonce: signedNonce
}
}
};
const baseURL = `${UNIT_SECURE_URL[env]}/cards/${cardId}/mobile-wallet-payload`;
try {
const response = await axios.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 UNComponentsError
if (axios.isAxiosError(error) && error.response) {
const unComponentsErrors = error.response.data;
throw unComponentsErrors;
} else if (error instanceof Error) {
const unexpectedError = {
title: 'Unexpected error',
error: error
};
throw unexpectedError;
}
throw {
title: `Unexpected error: ${error}`
};
}
};
//# sourceMappingURL=UNWalletPayloadRequest.js.map