2019-08-14 09:48:44 +00:00
|
|
|
import Auth from '../api/auth';
|
|
|
|
|
2021-05-28 13:51:16 +00:00
|
|
|
const parseErrorCode = error => Promise.reject(error);
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-08-21 07:29:56 +00:00
|
|
|
export default axios => {
|
2021-05-28 13:51:16 +00:00
|
|
|
const { apiHost = '' } = window.chatwootConfig || {};
|
|
|
|
const wootApi = axios.create({ baseURL: `${apiHost}/` });
|
2019-08-14 09:48:44 +00:00
|
|
|
// Add Auth Headers to requests if logged in
|
2022-04-14 15:24:26 +00:00
|
|
|
if (Auth.hasAuthCookie()) {
|
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;
|
|
|
|
};
|