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