2020-05-16 08:52:26 +00:00
|
|
|
import axios from 'axios';
|
2020-06-20 14:55:33 +00:00
|
|
|
import userService from './userService';
|
2020-05-20 05:19:14 +00:00
|
|
|
|
|
|
|
export const apiBase = '/api/v1';
|
2020-05-16 08:52:26 +00:00
|
|
|
|
|
|
|
export const api = axios.create({
|
|
|
|
baseURL: apiBase,
|
|
|
|
timeout: 1000,
|
|
|
|
withCredentials: true,
|
2020-05-18 18:49:30 +00:00
|
|
|
});
|
2020-05-16 08:52:26 +00:00
|
|
|
|
2020-05-18 18:49:30 +00:00
|
|
|
api.interceptors.request.use(
|
|
|
|
(config) => {
|
|
|
|
const custconfig = config;
|
|
|
|
// Merge custconfig dicts
|
2020-06-20 14:55:33 +00:00
|
|
|
custconfig.headers = { ...config.headers, ...userService.getAuthHeader() };
|
2020-05-18 18:49:30 +00:00
|
|
|
// Do something before request is sent
|
|
|
|
// console.log(custconfig)
|
|
|
|
return custconfig;
|
|
|
|
},
|
|
|
|
(error) => Promise.reject(error),
|
2020-05-16 08:52:26 +00:00
|
|
|
);
|
|
|
|
|
2020-05-18 18:49:30 +00:00
|
|
|
api.interceptors.response.use(
|
|
|
|
(response) => response,
|
|
|
|
(err) => {
|
|
|
|
// console.log(err.response.status);
|
|
|
|
console.log(err);
|
|
|
|
if (err.response && err.response.status === 401) {
|
|
|
|
console.log('Dispatching refresh_token...');
|
2020-06-20 14:55:33 +00:00
|
|
|
userService.refreshToken();
|
2020-05-18 18:49:30 +00:00
|
|
|
// maybe redirect to /login if needed !
|
2020-06-23 17:57:15 +00:00
|
|
|
} else if (err.response && err.response.status === 500) {
|
2020-06-22 17:39:44 +00:00
|
|
|
console.log('Bot seems to be offline...');
|
|
|
|
}
|
2020-05-18 18:49:30 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
// // return Promise.reject(err);
|
|
|
|
},
|
|
|
|
);
|
2020-05-20 05:19:14 +00:00
|
|
|
|
2020-06-22 18:18:45 +00:00
|
|
|
export function setBaseUrl(baseURL: string) {
|
2020-05-20 05:19:14 +00:00
|
|
|
if (baseURL === null) {
|
|
|
|
// Reset to "local" baseurl
|
|
|
|
api.defaults.baseURL = apiBase;
|
|
|
|
} else if (!baseURL.endsWith(apiBase)) {
|
|
|
|
api.defaults.baseURL = `${baseURL}${apiBase}`;
|
|
|
|
} else {
|
|
|
|
api.defaults.baseURL = `${baseURL}`;
|
|
|
|
}
|
|
|
|
// Do some more testing here ?
|
|
|
|
}
|
2020-06-22 18:18:45 +00:00
|
|
|
|
|
|
|
setBaseUrl(userService.getAPIUrl());
|