Created
January 23, 2020 21:12
-
-
Save nandomoreirame/9cb6ab6f173632067b089e34a671b62d to your computer and use it in GitHub Desktop.
Nuxt.js plugin Axios
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
export default function ({ $axios }, { redirect }) { | |
$axios.defaults.headers['Content-Type'] = 'application/json'; | |
$axios.onRequest((config) => { | |
const apiToken = process.env.API_USER_TOKEN || ''; | |
config.headers.Authorization = apiToken ? `JWT ${apiToken}` : ''; | |
console.log(`${config.baseURL}${config.url}`, 'Request'); | |
return config; | |
}); | |
$axios.onResponse(({ data }) => { | |
console.log('Ok', 'Response'); | |
console.log(data, 'Data'); | |
return data; | |
}); | |
$axios.onError(({ response }) => { | |
console.error('Error response', response); | |
redirect(`/http-error/${response.status}`); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment