Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 1 | import Axios from 'axios'; |
Paul Fertser | 6de0341 | 2024-07-05 10:46:38 +0000 | [diff] [blame] | 2 | import router from '../router'; |
Ed Tanous | 01492c3 | 2024-04-20 16:34:54 -0700 | [diff] [blame] | 3 | import { setupCache, buildWebStorage } from 'axios-cache-interceptor'; |
| 4 | |
Yoshie Muranaka | 8263d85 | 2020-10-16 07:58:06 -0700 | [diff] [blame] | 5 | //Do not change store import. |
| 6 | //Exact match alias set to support |
| 7 | //dotenv customizations. |
Ed Tanous | 7d6b44c | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 8 | import store from '.'; |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 9 | |
Ed Tanous | 80d697d | 2023-03-27 13:19:31 -0700 | [diff] [blame] | 10 | Axios.defaults.headers.common['Accept'] = 'application/json'; |
| 11 | Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; |
| 12 | |
Ed Tanous | 01492c3 | 2024-04-20 16:34:54 -0700 | [diff] [blame] | 13 | const axiosInstance = Axios.create({ |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 14 | withCredentials: true, |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 15 | }); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 16 | |
Ed Tanous | 01492c3 | 2024-04-20 16:34:54 -0700 | [diff] [blame] | 17 | const api = setupCache(axiosInstance, { |
| 18 | debug: console.log, |
| 19 | methods: ['get'], |
| 20 | interpretHeader: false, |
| 21 | etag: true, |
| 22 | modifiedSince: false, |
| 23 | staleIfError: false, |
| 24 | ttl: 0, |
| 25 | storage: buildWebStorage(localStorage, 'webui-vue-cache:'), |
| 26 | }); |
| 27 | |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 28 | api.interceptors.response.use(undefined, (error) => { |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 29 | let response = error.response; |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 30 | |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 31 | // TODO: Provide user with a notification and way to keep system active |
| 32 | if (response.status == 401) { |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 33 | if (response.config.url != '/login') { |
| 34 | window.location = '/login'; |
Yoshie Muranaka | 68069e1 | 2020-05-15 08:06:46 -0700 | [diff] [blame] | 35 | // Commit logout to remove XSRF-TOKEN cookie |
| 36 | store.commit('authentication/logout'); |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 37 | } |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 38 | } |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 39 | |
Paul Fertser | 6de0341 | 2024-07-05 10:46:38 +0000 | [diff] [blame] | 40 | // Check if action is unauthorized. |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 41 | if (response.status == 403) { |
Paul Fertser | ce7db82 | 2024-07-05 11:04:04 +0000 | [diff] [blame^] | 42 | if (isPasswordExpired(response.data)) { |
Paul Fertser | 6de0341 | 2024-07-05 10:46:38 +0000 | [diff] [blame] | 43 | router.push('/change-password'); |
| 44 | } else { |
| 45 | // Toast error message will appear on screen. |
| 46 | store.commit('global/setUnauthorized'); |
| 47 | } |
Derick Montague | 126eaab | 2019-12-23 13:33:52 -0600 | [diff] [blame] | 48 | } |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 49 | |
| 50 | return Promise.reject(error); |
Derick Montague | 227c41a | 2019-12-20 17:08:59 -0600 | [diff] [blame] | 51 | }); |
| 52 | |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 53 | export default { |
yubowei982 | 2a87a2e | 2023-06-19 10:33:39 +0800 | [diff] [blame] | 54 | get(path, config) { |
| 55 | return api.get(path, config); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 56 | }, |
yubowei982 | 2a87a2e | 2023-06-19 10:33:39 +0800 | [diff] [blame] | 57 | delete(path, config) { |
| 58 | return api.delete(path, config); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 59 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 60 | post(path, payload, config) { |
| 61 | return api.post(path, payload, config); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 62 | }, |
yubowei982 | 2a87a2e | 2023-06-19 10:33:39 +0800 | [diff] [blame] | 63 | patch(path, payload, config) { |
| 64 | return api.patch(path, payload, config); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 65 | }, |
yubowei982 | 2a87a2e | 2023-06-19 10:33:39 +0800 | [diff] [blame] | 66 | put(path, payload, config) { |
| 67 | return api.put(path, payload, config); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 68 | }, |
| 69 | all(promises) { |
| 70 | return Axios.all(promises); |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 71 | }, |
| 72 | spread(callback) { |
| 73 | return Axios.spread(callback); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 74 | }, |
Paul Fertser | 09a3b9e | 2024-07-03 14:11:03 +0000 | [diff] [blame] | 75 | set_auth_token(token) { |
| 76 | axiosInstance.defaults.headers.common['X-Auth-Token'] = token; |
| 77 | }, |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 78 | }; |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 79 | |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 80 | export const getResponseCount = (responses) => { |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 81 | let successCount = 0; |
| 82 | let errorCount = 0; |
| 83 | |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 84 | responses.forEach((response) => { |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 85 | if (response instanceof Error) errorCount++; |
| 86 | else successCount++; |
| 87 | }); |
| 88 | |
| 89 | return { |
| 90 | successCount, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 91 | errorCount, |
Yoshie Muranaka | be3af33 | 2020-05-11 08:23:04 -0700 | [diff] [blame] | 92 | }; |
| 93 | }; |
Paul Fertser | 6de0341 | 2024-07-05 10:46:38 +0000 | [diff] [blame] | 94 | |
Paul Fertser | ce7db82 | 2024-07-05 11:04:04 +0000 | [diff] [blame^] | 95 | export const isPasswordExpired = (data) => { |
| 96 | let extInfoMsgs = data?.['@Message.ExtendedInfo']; |
Paul Fertser | 6de0341 | 2024-07-05 10:46:38 +0000 | [diff] [blame] | 97 | return ( |
| 98 | extInfoMsgs && |
| 99 | extInfoMsgs.find( |
| 100 | (i) => i.MessageId.split('.')[4] === 'PasswordChangeRequired', |
| 101 | ) |
| 102 | ); |
| 103 | }; |