96 lines
3.3 KiB
JavaScript
96 lines
3.3 KiB
JavaScript
import axios from "axios";
|
|
import analyticsMixIn from "@/mixins/analytics-mixin.js";
|
|
import store from "@/store";
|
|
import router from "@/router";
|
|
|
|
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,
|
|
isFormData = false,
|
|
additionalSuccessEventDataHandler,
|
|
}) {
|
|
return new Promise((resolve, reject) => {
|
|
const cfDistroUrl = applicationConfig.CONSUMER_CF_DISTRO;
|
|
const payloadAndAnalyticsData = Object.assign({}, payload, { AppName: "FixMyGlass" });
|
|
const headers = {
|
|
[headerKeys.EXPERIMENT]: JSON.stringify(store.getters.experimentSettings),
|
|
};
|
|
|
|
axios({
|
|
method: method,
|
|
url: cfDistroUrl + endpoint,
|
|
data: payloadAndAnalyticsData,
|
|
crossDomain: true,
|
|
responseType: {},
|
|
headers: headers,
|
|
}).then(
|
|
(response) => {
|
|
if (logApiCall) {
|
|
let additionalEventData = "";
|
|
if (additionalSuccessEventDataHandler) {
|
|
additionalEventData = "_" + additionalSuccessEventDataHandler(response);
|
|
}
|
|
const pageName = analyticsMixIn.methods.getPageName();
|
|
const nextPageName = router.lastNavigationPage || pageName;
|
|
analyticsMixIn.methods.pushEventToGA(
|
|
GaCategories.API_RESPONSE,
|
|
`${nextPageName}_${endpoint}`,
|
|
`${GaLabels.SUCCESS}${additionalEventData}`,
|
|
true
|
|
);
|
|
}
|
|
|
|
return resolve(response);
|
|
},
|
|
(error) => {
|
|
console.error(error);
|
|
|
|
if (logApiCall) {
|
|
analyticsMixIn.methods.pushEventToGA(
|
|
GaCategories.API_RESPONSE,
|
|
GaActions.RESULT,
|
|
`${GaLabels.ERROR}_${endpoint}`,
|
|
true
|
|
);
|
|
}
|
|
|
|
return reject(error.response);
|
|
}
|
|
);
|
|
});
|
|
},
|
|
|
|
/* istanbul ignore next */
|
|
callMockHttpClient({ method, endpoint, payload }) {
|
|
// For Mock use only!
|
|
return new Promise((resolve, reject) => {
|
|
axios({
|
|
method: method,
|
|
url: endpoint,
|
|
crossDomain: true,
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
responseType: {},
|
|
data: payload,
|
|
}).then(
|
|
(response) => {
|
|
// simulate a delayed response
|
|
setTimeout(() => {
|
|
resolve(response);
|
|
}, 2000);
|
|
},
|
|
(error) => {
|
|
return reject(error.response);
|
|
}
|
|
);
|
|
});
|
|
},
|
|
};
|