import axios from 'axios'; import analyticsMixIn from '@/mixins/analytics-mixin.js'; import { useMainStore } from '@/store'; import applicationConfig from '@/constants/application-config.js'; import { GaCategories, GaActions, GaLabels } from '@/constants/analytics'; import headerKeys from '@/constants/header-keys'; export default { callHttpClient({ method, endpoint, payload, logApiCall = true }) { return new Promise((resolve, reject) => { const store = useMainStore(); const cfDistroUrl = applicationConfig.CONSUMER_CF_DISTRO; const payloadAndAnalyticsData = { ...payload, AppName: 'ISS' }; const headers = { [headerKeys.EXPERIMENT]: JSON.stringify(store.experimentSettings) }; axios({ method, url: cfDistroUrl + endpoint, data: payloadAndAnalyticsData, crossDomain: true, responseType: 'json', headers }) .then( (response) => { if (logApiCall) { analyticsMixIn.methods.pushEventToGA( GaCategories.API_RESPONSE, GaActions.RESULT, `${GaLabels.SUCCESS}_${endpoint}`, true ); } return resolve(response); }, (error) => { window.console.error(error); // implement if analytics service is down if (endpoint.includes('analytics')) { return resolve({ data: '' }); } return reject(error.response); } ); }); }, // used for mocked services async mockCallHttpClient(method, endpoint) { return new Promise((resolve, reject) => { axios({ method, url: endpoint, crossDomain: true, responseType: {} }) .then( (response) => resolve(response), (error) => { window.console.error(error); return reject(error.response); } ); }); } };