Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 1 | import Axios from 'axios'; |
Yoshie Muranaka | 8263d85 | 2020-10-16 07:58:06 -0700 | [diff] [blame] | 2 | //Do not change store import. |
| 3 | //Exact match alias set to support |
| 4 | //dotenv customizations. |
Yoshie Muranaka | 816d947 | 2020-09-03 11:19:28 -0700 | [diff] [blame] | 5 | import store from '../store'; |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 6 | |
Ed Tanous | 80d697d | 2023-03-27 13:19:31 -0700 | [diff] [blame] | 7 | Axios.defaults.headers.common['Accept'] = 'application/json'; |
| 8 | Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; |
| 9 | |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 10 | const api = Axios.create({ |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 11 | withCredentials: true, |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 12 | }); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 13 | |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 14 | api.interceptors.response.use(undefined, (error) => { |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 15 | let response = error.response; |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 16 | |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 17 | // TODO: Provide user with a notification and way to keep system active |
| 18 | if (response.status == 401) { |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 19 | if (response.config.url != '/login') { |
| 20 | window.location = '/login'; |
Yoshie Muranaka | 68069e1 | 2020-05-15 08:06:46 -0700 | [diff] [blame] | 21 | // Commit logout to remove XSRF-TOKEN cookie |
| 22 | store.commit('authentication/logout'); |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 23 | } |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 24 | } |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 25 | |
| 26 | if (response.status == 403) { |
Sukanya Pandey | dd6aa0a | 2020-10-08 20:47:39 +0530 | [diff] [blame] | 27 | // Check if action is unauthorized. |
| 28 | // Toast error message will appear on screen |
| 29 | // when the action is unauthorized. |
| 30 | store.commit('global/setUnauthorized'); |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 31 | } |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 32 | |
| 33 | return Promise.reject(error); |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 34 | }); |
| 35 | |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 36 | export default { |
| 37 | get(path) { |
| 38 | return api.get(path); |
| 39 | }, |
| 40 | delete(path, payload) { |
| 41 | return api.delete(path, payload); |
| 42 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 43 | post(path, payload, config) { |
| 44 | return api.post(path, payload, config); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 45 | }, |
| 46 | patch(path, payload) { |
| 47 | return api.patch(path, payload); |
| 48 | }, |
| 49 | put(path, payload) { |
| 50 | return api.put(path, payload); |
| 51 | }, |
| 52 | all(promises) { |
| 53 | return Axios.all(promises); |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 54 | }, |
| 55 | spread(callback) { |
| 56 | return Axios.spread(callback); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 57 | }, |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 58 | }; |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 59 | |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 60 | export const getResponseCount = (responses) => { |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 61 | let successCount = 0; |
| 62 | let errorCount = 0; |
| 63 | |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 64 | responses.forEach((response) => { |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 65 | if (response instanceof Error) errorCount++; |
| 66 | else successCount++; |
| 67 | }); |
| 68 | |
| 69 | return { |
| 70 | successCount, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 71 | errorCount, |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 72 | }; |
| 73 | }; |