Chatwoot/app/javascript/dashboard/helper/APIHelper.js

33 lines
805 B
JavaScript
Raw Normal View History

/* eslint no-console: 0 */
import Auth from '../api/auth';
const parseErrorCode = error => Promise.reject(error);
export default axios => {
const { apiHost = '' } = window.chatwootConfig || {};
const wootApi = axios.create({ baseURL: `${apiHost}/` });
// 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,
});
}
// Response parsing interceptor
wootApi.interceptors.response.use(
response => response,
error => parseErrorCode(error)
);
return wootApi;
};