Created
April 2, 2019 18:12
-
-
Save StevenACoffman/0ac5ecccfaf345de96ec94dcd2efc5dd to your computer and use it in GitHub Desktop.
axios logger (Interceptors)
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
// axios development only interceptors | |
// https://github.com/mzabriskie/axios#interceptors | |
/** | |
* Strip baseURL from URL | |
* | |
* @param config Object | |
* @returns String | |
*/ | |
function getUrl(config) { | |
if (config.baseURL) { | |
return config.url.replace(config.baseURL, ''); | |
} | |
return config.url; | |
} | |
// Intercept all requests | |
window.axios.interceptors.request.use( | |
(config) => { | |
console.log('%c ' + config.method.toUpperCase() + ' - ' + getUrl(config) + ':', 'color: #0086b3; font-weight: bold', config); | |
return config | |
}, | |
(error) => { | |
return Promise.reject(error); | |
} | |
); | |
// Intercept all responses | |
window.axios.interceptors.response.use( | |
(response) => { | |
console.log('%c ' + response.status + ' - ' + getUrl(response.config) + ':', 'color: #008000; font-weight: bold', response); | |
return response; | |
}, | |
(error) => { | |
if(error.response) { | |
console.log('%c ' + error.response.status + ' - ' + getUrl(error.response.config) + ':', 'color: #a71d5d; font-weight: bold', error.response); | |
} | |
return Promise.reject(error); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment