1import Axios from 'axios'; 2//Do not change store import. 3//Exact match alias set to support 4//dotenv customizations. 5import store from '../store'; 6 7Axios.defaults.headers.common['Accept'] = 'application/json'; 8Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; 9 10const api = Axios.create({ 11 withCredentials: true, 12}); 13 14api.interceptors.response.use(undefined, (error) => { 15 let response = error.response; 16 17 // TODO: Provide user with a notification and way to keep system active 18 if (response.status == 401) { 19 if (response.config.url != '/login') { 20 window.location = '/login'; 21 // Commit logout to remove XSRF-TOKEN cookie 22 store.commit('authentication/logout'); 23 } 24 } 25 26 if (response.status == 403) { 27 // Check if action is unauthorized. 28 // Toast error message will appear on screen 29 // when the action is unauthorized. 30 store.commit('global/setUnauthorized'); 31 } 32 33 return Promise.reject(error); 34}); 35 36export default { 37 get(path, config) { 38 return api.get(path, config); 39 }, 40 delete(path, config) { 41 return api.delete(path, config); 42 }, 43 post(path, payload, config) { 44 return api.post(path, payload, config); 45 }, 46 patch(path, payload, config) { 47 return api.patch(path, payload, config); 48 }, 49 put(path, payload, config) { 50 return api.put(path, payload, config); 51 }, 52 all(promises) { 53 return Axios.all(promises); 54 }, 55 spread(callback) { 56 return Axios.spread(callback); 57 }, 58}; 59 60export const getResponseCount = (responses) => { 61 let successCount = 0; 62 let errorCount = 0; 63 64 responses.forEach((response) => { 65 if (response instanceof Error) errorCount++; 66 else successCount++; 67 }); 68 69 return { 70 successCount, 71 errorCount, 72 }; 73}; 74