File size: 1,462 Bytes
f6c1709 | 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 38 39 40 41 42 43 44 45 46 47 48 49 | import axios, { type AxiosInstance, type AxiosError } from 'axios'
import { useAuthStore } from '@/stores/auth'
import router from '@/router'
export const apiClient: AxiosInstance = axios.create({
baseURL: import.meta.env.VITE_API_URL || '',
timeout: 30000,
withCredentials: true,
})
apiClient.interceptors.request.use(
(config) => config,
(error) => Promise.reject(error)
)
let isRedirecting = false
apiClient.interceptors.response.use(
(response) => response.data,
async (error: AxiosError) => {
const rawHeaders = (error.config?.headers || {}) as Record<string, unknown>
const skipAuthRedirectHeader = rawHeaders['X-Skip-Auth-Redirect'] ?? rawHeaders['x-skip-auth-redirect']
const skipAuthRedirect = String(skipAuthRedirectHeader ?? '') === '1'
if (error.response?.status === 401 && !isRedirecting && !skipAuthRedirect) {
isRedirecting = true
const authStore = useAuthStore()
authStore.isLoggedIn = false
await router.push('/login')
isRedirecting = false
}
const errorMessage = error.response?.data
? (error.response.data as any).detail || (error.response.data as any).message
: error.message
const wrapped = new Error(errorMessage || '请求失败') as Error & {
status?: number
data?: unknown
}
wrapped.status = error.response?.status
wrapped.data = error.response?.data
return Promise.reject(wrapped)
}
)
export default apiClient
|