Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 1 | import Axios from 'axios'; |
Yoshie Muranaka | 816d947 | 2020-09-03 11:19:28 -0700 | [diff] [blame] | 2 | import router from '@/router'; |
Yoshie Muranaka | 8263d85 | 2020-10-16 07:58:06 -0700 | [diff] [blame^] | 3 | |
| 4 | //Do not change store import. |
| 5 | //Exact match alias set to support |
| 6 | //dotenv customizations. |
Yoshie Muranaka | 816d947 | 2020-09-03 11:19:28 -0700 | [diff] [blame] | 7 | import store from '../store'; |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 8 | |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 9 | const api = Axios.create({ |
| 10 | withCredentials: true |
| 11 | }); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 12 | |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 13 | api.interceptors.response.use(undefined, error => { |
| 14 | let response = error.response; |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 15 | |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 16 | // TODO: Provide user with a notification and way to keep system active |
| 17 | if (response.status == 401) { |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 18 | if (response.config.url != '/login') { |
| 19 | window.location = '/login'; |
Yoshie Muranaka | 68069e1 | 2020-05-15 08:06:46 -0700 | [diff] [blame] | 20 | // Commit logout to remove XSRF-TOKEN cookie |
| 21 | store.commit('authentication/logout'); |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 22 | } |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 23 | } |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 24 | |
| 25 | if (response.status == 403) { |
Yoshie Muranaka | c60d2e1 | 2020-06-01 09:44:23 -0700 | [diff] [blame] | 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 | } |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 33 | router.push({ name: 'unauthorized' }); |
| 34 | } |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 35 | |
| 36 | return Promise.reject(error); |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 37 | }); |
| 38 | |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 39 | export default { |
| 40 | get(path) { |
| 41 | return api.get(path); |
| 42 | }, |
| 43 | delete(path, payload) { |
| 44 | return api.delete(path, payload); |
| 45 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 46 | post(path, payload, config) { |
| 47 | return api.post(path, payload, config); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 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); |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 57 | }, |
| 58 | spread(callback) { |
| 59 | return Axios.spread(callback); |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 60 | } |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 61 | }; |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 62 | |
| 63 | export 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 | }; |