blob: ac1b2e36f9437ddb353e24cb29917ba8211c0b4e [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import Axios from 'axios';
Yoshie Muranaka816d9472020-09-03 11:19:28 -07002import router from '@/router';
Yoshie Muranaka8263d852020-10-16 07:58:06 -07003
4//Do not change store import.
5//Exact match alias set to support
6//dotenv customizations.
Yoshie Muranaka816d9472020-09-03 11:19:28 -07007import store from '../store';
Yoshie Muranaka74c24f12019-12-03 10:45:46 -08008
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -08009const api = Axios.create({
10 withCredentials: true
11});
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080012
Derick Montague227c41a2019-12-20 17:08:59 -060013api.interceptors.response.use(undefined, error => {
14 let response = error.response;
Derick Montague126eaab2019-12-23 13:33:52 -060015
Derick Montague227c41a2019-12-20 17:08:59 -060016 // TODO: Provide user with a notification and way to keep system active
17 if (response.status == 401) {
Derick Montague676f2fc2019-12-23 20:53:49 -060018 if (response.config.url != '/login') {
19 window.location = '/login';
Yoshie Muranaka68069e12020-05-15 08:06:46 -070020 // Commit logout to remove XSRF-TOKEN cookie
21 store.commit('authentication/logout');
Derick Montague676f2fc2019-12-23 20:53:49 -060022 }
Derick Montague227c41a2019-12-20 17:08:59 -060023 }
Derick Montague126eaab2019-12-23 13:33:52 -060024
25 if (response.status == 403) {
Yoshie Muranakac60d2e12020-06-01 09:44:23 -070026 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 }
Derick Montague126eaab2019-12-23 13:33:52 -060033 router.push({ name: 'unauthorized' });
34 }
Derick Montague676f2fc2019-12-23 20:53:49 -060035
36 return Promise.reject(error);
Derick Montague227c41a2019-12-20 17:08:59 -060037});
38
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080039export default {
40 get(path) {
41 return api.get(path);
42 },
43 delete(path, payload) {
44 return api.delete(path, payload);
45 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070046 post(path, payload, config) {
47 return api.post(path, payload, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080048 },
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);
Yoshie Muranaka183c2752020-02-12 11:30:49 -080057 },
58 spread(callback) {
59 return Axios.spread(callback);
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080060 }
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080061};
Yoshie Muranakabe3af332020-05-11 08:23:04 -070062
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};