2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-console: 0 */
|
|
|
|
import constants from '../constants';
|
|
|
|
import Auth from '../api/auth';
|
|
|
|
import router from '../routes';
|
|
|
|
|
2019-08-21 07:29:56 +00:00
|
|
|
const parseErrorCode = error => {
|
2019-08-14 09:48:44 +00:00
|
|
|
if (error.response) {
|
|
|
|
if (error.response.status === 401) {
|
|
|
|
// If auth failed
|
|
|
|
} else if (error.response.status === 500) {
|
|
|
|
// If server failed
|
|
|
|
} else if (error.response.status === 422) {
|
|
|
|
// If request params are errored
|
|
|
|
} else if (error.response.status === 901 || error.response.status === 902) {
|
|
|
|
let name = 'billing_deactivated';
|
|
|
|
if (Auth.isAdmin()) {
|
|
|
|
name = 'billing';
|
|
|
|
}
|
|
|
|
// If Trial ended
|
|
|
|
router.push({ name });
|
|
|
|
} else {
|
|
|
|
// Anything else
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
|
|
|
}
|
|
|
|
// Do something with request error
|
|
|
|
return Promise.reject(error);
|
|
|
|
};
|
|
|
|
|
2019-08-21 07:29:56 +00:00
|
|
|
export default axios => {
|
2019-08-14 09:48:44 +00:00
|
|
|
const wootApi = axios.create();
|
|
|
|
wootApi.defaults.baseURL = constants.apiURL;
|
|
|
|
// Add Auth Headers to requests if logged in
|
|
|
|
if (Auth.isLoggedIn()) {
|
|
|
|
Object.assign(wootApi.defaults.headers.common, Auth.getAuthData());
|
|
|
|
}
|
|
|
|
// Response parsing interceptor
|
2019-08-21 07:29:56 +00:00
|
|
|
wootApi.interceptors.response.use(
|
|
|
|
response => response,
|
|
|
|
error => parseErrorCode(error)
|
|
|
|
);
|
2019-08-14 09:48:44 +00:00
|
|
|
return wootApi;
|
|
|
|
};
|