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) {
|
2019-09-05 07:48:27 +00:00
|
|
|
// 901, 902 are used to identify billing related issues
|
|
|
|
if ([901, 902].includes(error.response.status)) {
|
|
|
|
const name = Auth.isAdmin() ? 'billing' : 'billing_deactivated';
|
2019-08-14 09:48:44 +00:00
|
|
|
router.push({ name });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
};
|