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