feat: intercept 403 HTML responses and show gate page
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import axios, { AxiosHeaders } from 'axios'
|
||||
import { apiBaseURL } from '@/shared/config'
|
||||
|
||||
// Глобальный application/json ломает FormData: axios сериализует его в JSON. Тип для JSON задаёт transformRequest.
|
||||
export const apiClient = axios.create({
|
||||
baseURL: apiBaseURL,
|
||||
})
|
||||
@@ -13,7 +12,6 @@ apiClient.interceptors.request.use((config) => {
|
||||
if (token) {
|
||||
config.headers.set('Authorization', `Bearer ${token}`)
|
||||
}
|
||||
// FormData: нельзя задавать Content-Type вручную (нужен boundary). Иначе сервер не видит файлы → { urls: [] }.
|
||||
if (config.data instanceof FormData) {
|
||||
config.headers.delete('Content-Type')
|
||||
config.headers.delete('content-type')
|
||||
@@ -23,3 +21,13 @@ apiClient.interceptors.request.use((config) => {
|
||||
return config
|
||||
}
|
||||
})
|
||||
|
||||
apiClient.interceptors.response.use(undefined, (error) => {
|
||||
const ct = error.response?.headers?.['content-type'] ?? ''
|
||||
if (error.response?.status === 403 && ct.includes('text/html')) {
|
||||
document.open()
|
||||
document.write(error.response.data)
|
||||
document.close()
|
||||
}
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user