xref: /openbmc/webui-vue/src/store/api.js (revision 7385e139)
1import Axios from 'axios';
2//Do not change store import.
3//Exact match alias set to support
4//dotenv customizations.
5import store from '../store';
6
7const api = Axios.create({
8  withCredentials: true,
9});
10
11api.interceptors.response.use(undefined, (error) => {
12  let response = error.response;
13
14  // TODO: Provide user with a notification and way to keep system active
15  if (response.status == 401) {
16    if (response.config.url != '/login') {
17      window.location = '/login';
18      // Commit logout to remove XSRF-TOKEN cookie
19      store.commit('authentication/logout');
20    }
21  }
22
23  if (response.status == 403) {
24    // Check if action is unauthorized.
25    // Toast error message will appear on screen
26    // when the action is unauthorized.
27    store.commit('global/setUnauthorized');
28  }
29
30  return Promise.reject(error);
31});
32
33export default {
34  get(path) {
35    return api.get(path);
36  },
37  delete(path, payload) {
38    return api.delete(path, payload);
39  },
40  post(path, payload, config) {
41    return api.post(path, payload, config);
42  },
43  patch(path, payload) {
44    return api.patch(path, payload);
45  },
46  put(path, payload) {
47    return api.put(path, payload);
48  },
49  all(promises) {
50    return Axios.all(promises);
51  },
52  spread(callback) {
53    return Axios.spread(callback);
54  },
55};
56
57export const getResponseCount = (responses) => {
58  let successCount = 0;
59  let errorCount = 0;
60
61  responses.forEach((response) => {
62    if (response instanceof Error) errorCount++;
63    else successCount++;
64  });
65
66  return {
67    successCount,
68    errorCount,
69  };
70};
71