blob: 0bd84e621a39fcb121b781419f227ec0d2967c5d [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import Axios from 'axios';
Ed Tanous01492c32024-04-20 16:34:54 -07002import { setupCache, buildWebStorage } from 'axios-cache-interceptor';
3
Yoshie Muranaka8263d852020-10-16 07:58:06 -07004//Do not change store import.
5//Exact match alias set to support
6//dotenv customizations.
Yoshie Muranaka816d9472020-09-03 11:19:28 -07007import store from '../store';
Yoshie Muranaka74c24f12019-12-03 10:45:46 -08008
Ed Tanous80d697d2023-03-27 13:19:31 -07009Axios.defaults.headers.common['Accept'] = 'application/json';
10Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
11
Ed Tanous01492c32024-04-20 16:34:54 -070012const axiosInstance = Axios.create({
Derick Montague602e98a2020-10-21 16:20:00 -050013 withCredentials: true,
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080014});
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080015
Ed Tanous01492c32024-04-20 16:34:54 -070016const 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 Montague602e98a2020-10-21 16:20:00 -050027api.interceptors.response.use(undefined, (error) => {
Derick Montague227c41a2019-12-20 17:08:59 -060028 let response = error.response;
Derick Montague126eaab2019-12-23 13:33:52 -060029
Derick Montague227c41a2019-12-20 17:08:59 -060030 // TODO: Provide user with a notification and way to keep system active
31 if (response.status == 401) {
Derick Montague676f2fc2019-12-23 20:53:49 -060032 if (response.config.url != '/login') {
33 window.location = '/login';
Yoshie Muranaka68069e12020-05-15 08:06:46 -070034 // Commit logout to remove XSRF-TOKEN cookie
35 store.commit('authentication/logout');
Derick Montague676f2fc2019-12-23 20:53:49 -060036 }
Derick Montague227c41a2019-12-20 17:08:59 -060037 }
Derick Montague126eaab2019-12-23 13:33:52 -060038
39 if (response.status == 403) {
Sukanya Pandeydd6aa0a2020-10-08 20:47:39 +053040 // 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 Montague126eaab2019-12-23 13:33:52 -060044 }
Derick Montague676f2fc2019-12-23 20:53:49 -060045
46 return Promise.reject(error);
Derick Montague227c41a2019-12-20 17:08:59 -060047});
48
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080049export default {
yubowei9822a87a2e2023-06-19 10:33:39 +080050 get(path, config) {
51 return api.get(path, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080052 },
yubowei9822a87a2e2023-06-19 10:33:39 +080053 delete(path, config) {
54 return api.delete(path, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080055 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070056 post(path, payload, config) {
57 return api.post(path, payload, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080058 },
yubowei9822a87a2e2023-06-19 10:33:39 +080059 patch(path, payload, config) {
60 return api.patch(path, payload, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080061 },
yubowei9822a87a2e2023-06-19 10:33:39 +080062 put(path, payload, config) {
63 return api.put(path, payload, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080064 },
65 all(promises) {
66 return Axios.all(promises);
Yoshie Muranaka183c2752020-02-12 11:30:49 -080067 },
68 spread(callback) {
69 return Axios.spread(callback);
Derick Montague602e98a2020-10-21 16:20:00 -050070 },
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080071};
Yoshie Muranakabe3af332020-05-11 08:23:04 -070072
Derick Montague602e98a2020-10-21 16:20:00 -050073export const getResponseCount = (responses) => {
Yoshie Muranakabe3af332020-05-11 08:23:04 -070074 let successCount = 0;
75 let errorCount = 0;
76
Derick Montague602e98a2020-10-21 16:20:00 -050077 responses.forEach((response) => {
Yoshie Muranakabe3af332020-05-11 08:23:04 -070078 if (response instanceof Error) errorCount++;
79 else successCount++;
80 });
81
82 return {
83 successCount,
Derick Montague602e98a2020-10-21 16:20:00 -050084 errorCount,
Yoshie Muranakabe3af332020-05-11 08:23:04 -070085 };
86};