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