Skip to content

Instantly share code, notes, and snippets.

@nandomoreirame
Created January 23, 2020 21:12
Show Gist options
  • Save nandomoreirame/9cb6ab6f173632067b089e34a671b62d to your computer and use it in GitHub Desktop.
Save nandomoreirame/9cb6ab6f173632067b089e34a671b62d to your computer and use it in GitHub Desktop.
Nuxt.js plugin Axios
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