DigitalConsumer.ISS/src/global-methods.js
2023-08-28 08:59:37 -04:00

71 lines
2.5 KiB
JavaScript

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: 'SelfService' };
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);
}
);
});
}
};