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