Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 1 | import Axios from 'axios'; |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 2 | import router from '../router'; |
Yoshie Muranaka | 68069e1 | 2020-05-15 08:06:46 -0700 | [diff] [blame] | 3 | import store from '@/store'; |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 4 | |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 5 | const api = Axios.create({ |
| 6 | withCredentials: true |
| 7 | }); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 8 | |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 9 | api.interceptors.response.use(undefined, error => { |
| 10 | let response = error.response; |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 11 | |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 12 | // TODO: Provide user with a notification and way to keep system active |
| 13 | if (response.status == 401) { |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 14 | if (response.config.url != '/login') { |
| 15 | window.location = '/login'; |
Yoshie Muranaka | 68069e1 | 2020-05-15 08:06:46 -0700 | [diff] [blame] | 16 | // Commit logout to remove XSRF-TOKEN cookie |
| 17 | store.commit('authentication/logout'); |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 18 | } |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 19 | } |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 20 | |
| 21 | if (response.status == 403) { |
Yoshie Muranaka | c60d2e1 | 2020-06-01 09:44:23 -0700 | [diff] [blame] | 22 | if (router.history.current.name === 'unauthorized') { |
| 23 | // Check if current router location is unauthorized |
| 24 | // to avoid NavigationDuplicated errors. |
| 25 | // The router throws an error if trying to push to the |
| 26 | // same/current router location. |
| 27 | return; |
| 28 | } |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 29 | router.push({ name: 'unauthorized' }); |
| 30 | } |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 31 | |
| 32 | return Promise.reject(error); |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 33 | }); |
| 34 | |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 35 | export default { |
| 36 | get(path) { |
| 37 | return api.get(path); |
| 38 | }, |
| 39 | delete(path, payload) { |
| 40 | return api.delete(path, payload); |
| 41 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 42 | post(path, payload, config) { |
| 43 | return api.post(path, payload, config); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 44 | }, |
| 45 | patch(path, payload) { |
| 46 | return api.patch(path, payload); |
| 47 | }, |
| 48 | put(path, payload) { |
| 49 | return api.put(path, payload); |
| 50 | }, |
| 51 | all(promises) { |
| 52 | return Axios.all(promises); |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 53 | }, |
| 54 | spread(callback) { |
| 55 | return Axios.spread(callback); |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 56 | } |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 57 | }; |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 58 | |
| 59 | export const getResponseCount = responses => { |
| 60 | let successCount = 0; |
| 61 | let errorCount = 0; |
| 62 | |
| 63 | responses.forEach(response => { |
| 64 | if (response instanceof Error) errorCount++; |
| 65 | else successCount++; |
| 66 | }); |
| 67 | |
| 68 | return { |
| 69 | successCount, |
| 70 | errorCount |
| 71 | }; |
| 72 | }; |