2019-11-16 16:06:34 +01:00
|
|
|
import "@babel/polyfill/noConflict";
|
2019-11-16 23:55:56 +01:00
|
|
|
import Axios from "axios";
|
|
|
|
import Notify from "common/notify";
|
2018-07-27 17:31:39 +02:00
|
|
|
|
2019-11-16 23:55:56 +01:00
|
|
|
const Api = Axios.create({
|
2019-05-09 06:48:10 +02:00
|
|
|
baseURL: "/api/v1",
|
2018-07-27 17:31:39 +02:00
|
|
|
headers: {common: {
|
2019-05-09 06:48:10 +02:00
|
|
|
"X-Session-Token": window.localStorage.getItem("session_token"),
|
2018-07-27 17:31:39 +02:00
|
|
|
}},
|
|
|
|
});
|
|
|
|
|
|
|
|
Api.interceptors.request.use(function (config) {
|
|
|
|
// Do something before request is sent
|
2019-11-16 23:55:56 +01:00
|
|
|
Notify.ajaxStart();
|
2018-07-27 17:31:39 +02:00
|
|
|
return config;
|
|
|
|
}, function (error) {
|
|
|
|
// Do something with request error
|
|
|
|
return Promise.reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
Api.interceptors.response.use(function (response) {
|
2019-11-16 23:55:56 +01:00
|
|
|
Notify.ajaxEnd();
|
2018-07-27 17:31:39 +02:00
|
|
|
return response;
|
|
|
|
}, function (error) {
|
2019-11-16 23:55:56 +01:00
|
|
|
Notify.ajaxEnd();
|
|
|
|
|
2018-07-27 17:31:39 +02:00
|
|
|
if(console && console.log) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
|
2019-05-09 06:48:10 +02:00
|
|
|
let errorMessage = "An error occurred - are you offline?";
|
2018-09-25 08:16:52 +02:00
|
|
|
let code = error.code;
|
|
|
|
|
|
|
|
if(error.response && error.response.data) {
|
|
|
|
let data = error.response.data;
|
|
|
|
code = data.code;
|
|
|
|
errorMessage = data.message ? data.message : data.error;
|
|
|
|
}
|
2018-07-27 17:31:39 +02:00
|
|
|
|
2019-11-16 23:55:56 +01:00
|
|
|
Notify.error(errorMessage);
|
2018-07-27 17:31:39 +02:00
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Api;
|