blob: 0e119c2871a6f15ef242cd7f20773fadce0762cf [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import Axios from 'axios';
Paul Fertser6de03412024-07-05 10:46:38 +00002import router from '../router';
Ed Tanous01492c32024-04-20 16:34:54 -07003import { setupCache, buildWebStorage } from 'axios-cache-interceptor';
4
Yoshie Muranaka8263d852020-10-16 07:58:06 -07005//Do not change store import.
6//Exact match alias set to support
7//dotenv customizations.
Ed Tanous7d6b44c2024-03-23 14:56:34 -07008import store from '.';
Yoshie Muranaka74c24f12019-12-03 10:45:46 -08009
Ed Tanous80d697d2023-03-27 13:19:31 -070010Axios.defaults.headers.common['Accept'] = 'application/json';
11Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
12
Ed Tanous01492c32024-04-20 16:34:54 -070013const axiosInstance = Axios.create({
Derick Montague602e98a2020-10-21 16:20:00 -050014 withCredentials: true,
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080015});
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080016
Ed Tanous01492c32024-04-20 16:34:54 -070017const 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 Montague602e98a2020-10-21 16:20:00 -050028api.interceptors.response.use(undefined, (error) => {
Derick Montague227c41a2019-12-20 17:08:59 -060029 let response = error.response;
Derick Montague126eaab2019-12-23 13:33:52 -060030
Derick Montague227c41a2019-12-20 17:08:59 -060031 // TODO: Provide user with a notification and way to keep system active
32 if (response.status == 401) {
Derick Montague676f2fc2019-12-23 20:53:49 -060033 if (response.config.url != '/login') {
34 window.location = '/login';
Yoshie Muranaka68069e12020-05-15 08:06:46 -070035 // Commit logout to remove XSRF-TOKEN cookie
36 store.commit('authentication/logout');
Derick Montague676f2fc2019-12-23 20:53:49 -060037 }
Derick Montague227c41a2019-12-20 17:08:59 -060038 }
Derick Montague126eaab2019-12-23 13:33:52 -060039
Paul Fertser6de03412024-07-05 10:46:38 +000040 // Check if action is unauthorized.
Derick Montague126eaab2019-12-23 13:33:52 -060041 if (response.status == 403) {
Paul Fertserce7db822024-07-05 11:04:04 +000042 if (isPasswordExpired(response.data)) {
Paul Fertser6de03412024-07-05 10:46:38 +000043 router.push('/change-password');
44 } else {
45 // Toast error message will appear on screen.
46 store.commit('global/setUnauthorized');
47 }
Derick Montague126eaab2019-12-23 13:33:52 -060048 }
Derick Montague676f2fc2019-12-23 20:53:49 -060049
50 return Promise.reject(error);
Derick Montague227c41a2019-12-20 17:08:59 -060051});
52
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080053export default {
yubowei9822a87a2e2023-06-19 10:33:39 +080054 get(path, config) {
55 return api.get(path, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080056 },
yubowei9822a87a2e2023-06-19 10:33:39 +080057 delete(path, config) {
58 return api.delete(path, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080059 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070060 post(path, payload, config) {
61 return api.post(path, payload, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080062 },
yubowei9822a87a2e2023-06-19 10:33:39 +080063 patch(path, payload, config) {
64 return api.patch(path, payload, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080065 },
yubowei9822a87a2e2023-06-19 10:33:39 +080066 put(path, payload, config) {
67 return api.put(path, payload, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080068 },
69 all(promises) {
70 return Axios.all(promises);
Yoshie Muranaka183c2752020-02-12 11:30:49 -080071 },
72 spread(callback) {
73 return Axios.spread(callback);
Derick Montague602e98a2020-10-21 16:20:00 -050074 },
Paul Fertser09a3b9e2024-07-03 14:11:03 +000075 set_auth_token(token) {
76 axiosInstance.defaults.headers.common['X-Auth-Token'] = token;
77 },
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080078};
Yoshie Muranakabe3af332020-05-11 08:23:04 -070079
Derick Montague602e98a2020-10-21 16:20:00 -050080export const getResponseCount = (responses) => {
Yoshie Muranakabe3af332020-05-11 08:23:04 -070081 let successCount = 0;
82 let errorCount = 0;
83
Derick Montague602e98a2020-10-21 16:20:00 -050084 responses.forEach((response) => {
Yoshie Muranakabe3af332020-05-11 08:23:04 -070085 if (response instanceof Error) errorCount++;
86 else successCount++;
87 });
88
89 return {
90 successCount,
Derick Montague602e98a2020-10-21 16:20:00 -050091 errorCount,
Yoshie Muranakabe3af332020-05-11 08:23:04 -070092 };
93};
Paul Fertser6de03412024-07-05 10:46:38 +000094
Paul Fertserce7db822024-07-05 11:04:04 +000095export const isPasswordExpired = (data) => {
96 let extInfoMsgs = data?.['@Message.ExtendedInfo'];
Paul Fertser6de03412024-07-05 10:46:38 +000097 return (
98 extInfoMsgs &&
99 extInfoMsgs.find(
100 (i) => i.MessageId.split('.')[4] === 'PasswordChangeRequired',
101 )
102 );
103};