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-10-10 03:41:18 +00:00
|
|
|
const errorStatus = error.response ? error.response.status : undefined;
|
|
|
|
// 901, 902 are used to identify billing related issues
|
|
|
|
if ([901, 902].includes(errorStatus)) {
|
|
|
|
const name = Auth.isAdmin() ? 'billing' : 'billing_deactivated';
|
|
|
|
router.push({ name });
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
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()) {
|
2019-11-04 07:24:41 +00:00
|
|
|
const {
|
|
|
|
'access-token': accessToken,
|
|
|
|
'token-type': tokenType,
|
|
|
|
client,
|
|
|
|
expiry,
|
|
|
|
uid,
|
|
|
|
} = Auth.getAuthData();
|
|
|
|
Object.assign(wootApi.defaults.headers.common, {
|
|
|
|
'access-token': accessToken,
|
|
|
|
'token-type': tokenType,
|
|
|
|
client,
|
|
|
|
expiry,
|
|
|
|
uid,
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
};
|