photoprism/frontend/src/common/api.js

46 lines
1.0 KiB
JavaScript
Raw Normal View History

import "@babel/polyfill/noConflict";
import Axios from "axios";
import Notify from "common/notify";
const Api = Axios.create({
baseURL: "/api/v1",
headers: {common: {
"X-Session-Token": window.localStorage.getItem("session_token"),
}},
});
Api.interceptors.request.use(function (config) {
// Do something before request is sent
Notify.ajaxStart();
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
Api.interceptors.response.use(function (response) {
Notify.ajaxEnd();
return response;
}, function (error) {
Notify.ajaxEnd();
if(console && console.log) {
console.log(error);
}
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;
}
Notify.error(errorMessage);
return Promise.reject(error);
});
export default Api;