blob: 51c5cdc4c816048a6e055c9046e3ea2965442a0a [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import Axios from 'axios';
Yoshie Muranaka8263d852020-10-16 07:58:06 -07002//Do not change store import.
3//Exact match alias set to support
4//dotenv customizations.
Yoshie Muranaka816d9472020-09-03 11:19:28 -07005import store from '../store';
Yoshie Muranaka74c24f12019-12-03 10:45:46 -08006
Ed Tanous80d697d2023-03-27 13:19:31 -07007Axios.defaults.headers.common['Accept'] = 'application/json';
8Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
9
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080010const api = Axios.create({
Derick Montague602e98a2020-10-21 16:20:00 -050011 withCredentials: true,
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080012});
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080013
Derick Montague602e98a2020-10-21 16:20:00 -050014api.interceptors.response.use(undefined, (error) => {
Derick Montague227c41a2019-12-20 17:08:59 -060015 let response = error.response;
Derick Montague126eaab2019-12-23 13:33:52 -060016
Derick Montague227c41a2019-12-20 17:08:59 -060017 // TODO: Provide user with a notification and way to keep system active
18 if (response.status == 401) {
Derick Montague676f2fc2019-12-23 20:53:49 -060019 if (response.config.url != '/login') {
20 window.location = '/login';
Yoshie Muranaka68069e12020-05-15 08:06:46 -070021 // Commit logout to remove XSRF-TOKEN cookie
22 store.commit('authentication/logout');
Derick Montague676f2fc2019-12-23 20:53:49 -060023 }
Derick Montague227c41a2019-12-20 17:08:59 -060024 }
Derick Montague126eaab2019-12-23 13:33:52 -060025
26 if (response.status == 403) {
Sukanya Pandeydd6aa0a2020-10-08 20:47:39 +053027 // Check if action is unauthorized.
28 // Toast error message will appear on screen
29 // when the action is unauthorized.
30 store.commit('global/setUnauthorized');
Derick Montague126eaab2019-12-23 13:33:52 -060031 }
Derick Montague676f2fc2019-12-23 20:53:49 -060032
33 return Promise.reject(error);
Derick Montague227c41a2019-12-20 17:08:59 -060034});
35
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080036export default {
37 get(path) {
38 return api.get(path);
39 },
40 delete(path, payload) {
41 return api.delete(path, payload);
42 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070043 post(path, payload, config) {
44 return api.post(path, payload, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080045 },
46 patch(path, payload) {
47 return api.patch(path, payload);
48 },
49 put(path, payload) {
50 return api.put(path, payload);
51 },
52 all(promises) {
53 return Axios.all(promises);
Yoshie Muranaka183c2752020-02-12 11:30:49 -080054 },
55 spread(callback) {
56 return Axios.spread(callback);
Derick Montague602e98a2020-10-21 16:20:00 -050057 },
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080058};
Yoshie Muranakabe3af332020-05-11 08:23:04 -070059
Derick Montague602e98a2020-10-21 16:20:00 -050060export const getResponseCount = (responses) => {
Yoshie Muranakabe3af332020-05-11 08:23:04 -070061 let successCount = 0;
62 let errorCount = 0;
63
Derick Montague602e98a2020-10-21 16:20:00 -050064 responses.forEach((response) => {
Yoshie Muranakabe3af332020-05-11 08:23:04 -070065 if (response instanceof Error) errorCount++;
66 else successCount++;
67 });
68
69 return {
70 successCount,
Derick Montague602e98a2020-10-21 16:20:00 -050071 errorCount,
Yoshie Muranakabe3af332020-05-11 08:23:04 -070072 };
73};