-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathclient.js
More file actions
37 lines (32 loc) · 1.36 KB
/
client.js
File metadata and controls
37 lines (32 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import axios from 'axios'
import Alerts from '../services/alerts.js'
const client = axios.create({
headers: {
'Content-Type': 'application/json'
},
timeout: 30000
})
// Common error handler
client.interceptors.response.use(function (response) {
return response
}, async function (error) {
if (/^http/.test(error.config.url)) {
// This request is to an external URL. Allow this error to pass back to the caller
return Promise.reject(error)
}
// Dynamic import breaks the circular dep: client.js → account-auth.js → api/* → client.js
const { useAccountAuthStore } = await import('../stores/account-auth.js')
const { useUxLoadingStore } = await import('../stores/ux-loading.js')
if (error.code === 'ERR_NETWORK') {
// Backend failed to respond
useUxLoadingStore().setOffline(true)
} else if (error.response && error.response.status === 401 && !useUxLoadingStore().appLoader && !useAccountAuthStore().loginInflight) {
// 401 when !pending && !loginInflight means the session has expired
useAccountAuthStore().logout()
} else if (error.response && error.response.status === 500) {
// show toast notification
Alerts.emit(error.response.data.error + ': ' + error.response.data.message, 'warning', 7500)
}
return Promise.reject(error)
})
export default client