audiobookshelf/client/plugins/axios.js

26 lines
843 B
JavaScript
Raw Normal View History

export default function ({ $axios, store, $config }) {
2021-08-18 00:01:11 +02:00
$axios.onRequest(config => {
if (!config.url) {
console.error('Axios request invalid config', config)
return
}
if (config.url.startsWith('http:') || config.url.startsWith('https:')) {
return
}
2023-05-28 17:47:28 +02:00
const bearerToken = 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}`
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)
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
})
}