2021-08-18 00:01:11 +02:00
|
|
|
export default function ({ $axios, store }) {
|
|
|
|
$axios.onRequest(config => {
|
2021-10-18 01:05:43 +02:00
|
|
|
if (!config.url) {
|
|
|
|
console.error('Axios request invalid config', config)
|
|
|
|
return
|
|
|
|
}
|
2021-08-21 20:02:24 +02:00
|
|
|
if (config.url.startsWith('http:') || config.url.startsWith('https:')) {
|
|
|
|
return
|
|
|
|
}
|
2021-08-24 01:31:04 +02:00
|
|
|
var bearerToken = store.state.user.user ? store.state.user.user.token : null
|
2021-08-18 00:01:11 +02:00
|
|
|
if (bearerToken) {
|
|
|
|
config.headers.common['Authorization'] = `Bearer ${bearerToken}`
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
config.url = `/dev${config.url}`
|
2021-09-06 01:20:29 +02:00
|
|
|
console.log('Making request to ' + config.url)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
$axios.onError(error => {
|
|
|
|
const code = parseInt(error.response && error.response.status)
|
2021-09-29 17:16:38 +02:00
|
|
|
const message = error.response ? error.response.data || 'Unknown Error' : 'Unknown Error'
|
|
|
|
console.error('Axios error', code, message)
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
|
|
|
}
|