blob: 0f8c9484d5fccc3282ce7f34eb85414eec7455eb [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import Axios from 'axios';
Derick Montague126eaab2019-12-23 13:33:52 -06002import router from '../router';
Yoshie Muranaka74c24f12019-12-03 10:45:46 -08003
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -08004const api = Axios.create({
5 withCredentials: true
6});
Yoshie Muranaka74c24f12019-12-03 10:45:46 -08007
Derick Montague227c41a2019-12-20 17:08:59 -06008api.interceptors.response.use(undefined, error => {
9 let response = error.response;
Derick Montague126eaab2019-12-23 13:33:52 -060010
Derick Montague227c41a2019-12-20 17:08:59 -060011 // TODO: Provide user with a notification and way to keep system active
12 if (response.status == 401) {
Derick Montague676f2fc2019-12-23 20:53:49 -060013 if (response.config.url != '/login') {
14 window.location = '/login';
15 }
Derick Montague227c41a2019-12-20 17:08:59 -060016 }
Derick Montague126eaab2019-12-23 13:33:52 -060017
18 if (response.status == 403) {
19 router.push({ name: 'unauthorized' });
20 }
Derick Montague676f2fc2019-12-23 20:53:49 -060021
22 return Promise.reject(error);
Derick Montague227c41a2019-12-20 17:08:59 -060023});
24
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080025export default {
26 get(path) {
27 return api.get(path);
28 },
29 delete(path, payload) {
30 return api.delete(path, payload);
31 },
32 post(path, payload) {
33 return api.post(path, payload);
34 },
35 patch(path, payload) {
36 return api.patch(path, payload);
37 },
38 put(path, payload) {
39 return api.put(path, payload);
40 },
41 all(promises) {
42 return Axios.all(promises);
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080043 }
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080044};