Last active
July 14, 2019 08:43
-
-
Save vasylnahuliak/3ccd2b846dce73bb9a60772f66b6b5b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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