import axios from "axios"; import analyticsMixIn from "@/mixins/analytics-mixin.js"; import store from "@/store"; import { applicationConfig } from "@/constants/application-config.js"; import { GaCategories, GaActions, GaLabels } from "@/constants/analytics"; import { headerKeys } from "@/constants/header-keys"; // Add a response interceptor for global axios error handing. axios.interceptors.response.use( (response) => response, (error) => { let rejectionError = ""; if (typeof error.response === "undefined") { // The request was not made, could be a bad connection or a CORS error. rejectionError = { message: "A network error occurred. " + "This could be a CORS issue or a dropped internet connection. " + "It is impossible for us to know.", cause: error, }; } else if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx rejectionError = { message: "Status Code Error", cause: error.response, }; } else if (error.request) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js rejectionError = { message: "The request was made but no response was received", cause: error.request, }; } else { // Something happened in setting up the request that triggered an Error rejectionError = { message: "Error", cause: error.message, }; } return Promise.reject(rejectionError); } ); export default { callHttpClient({ method, endpoint, payload, logApiCall = false, pageNameToLog = null, 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 endpointWithoutParams = analyticsMixIn.methods.removeParamsFromEndpoint(endpoint); analyticsMixIn.methods.pushEventToGA( GaCategories.API_RESPONSE, `${pageNameToLog}_${endpointWithoutParams}`, `${GaLabels.SUCCESS}${additionalEventData}`, true ); } return resolve(response); }, (error) => { if (logApiCall) { analyticsMixIn.methods.pushEventToGA( GaCategories.API_RESPONSE, GaActions.RESULT, `${GaLabels.ERROR}_${endpoint}`, true ); } global.$logger.logError(`${method}: ${endpoint}: ${error.message}`, error); return reject(error); } ); }); }, /* 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); } ); }); }, };