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