Skip to content

Instantly share code, notes, and snippets.

@vasylnahuliak
Last active July 14, 2019 08:43
Show Gist options
  • Save vasylnahuliak/3ccd2b846dce73bb9a60772f66b6b5b0 to your computer and use it in GitHub Desktop.
Save vasylnahuliak/3ccd2b846dce73bb9a60772f66b6b5b0 to your computer and use it in GitHub Desktop.
import axios from 'axios';
const api = axios.create({
baseURL: process.env.API_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
});
api.interceptors.request.use(
(config) => {
config.headers.Authorization = localStorage.getItem('token');
console.log('request: ', config);
return config;
},
error => Promise.reject(error),
);
api.interceptors.response.use(
(response) => {
console.log('response: ', response);
return response;
},
(error) => {
if (!error.response) {
console.log('Network Error!');
}
if (error && error.response && error.response.status === 401) {
console.log('Unauthorized');
}
if (error && error.response && error.response.status >= 500) {
alert(`Server Error. ${error.message}`);
}
return Promise.reject(error);
},
);
export default api;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment