All files / utils request.ts

100% Statements 26/26
87.5% Branches 14/16
100% Functions 4/4
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 50 51 52 53 54 55 56 57 58      3x               3x   6x 6x 1x   6x     1x       3x   1x     6x 4x 1x 1x 1x 1x 1x   3x     1x 1x   2x 2x 2x   2x 1x   1x   6x          
import axios from 'axios'
import { message } from 'ant-design-vue'
 
const request = axios.create({
  // Use relative path for Vercel deployment compatibility
  // In dev (Vite), /api/v1 is proxied to localhost:8000/api/v1
  // In prod (Vercel), /api/v1 is routed to /api/index.py via vercel.json rewrites
  baseURL: '/api/v1',
  timeout: 10000 // Increased timeout for serverless cold starts
})
 
request.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
 
request.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    if (error.response) {
      if (error.response.status === 401) {
        localStorage.removeItem('token')
        localStorage.removeItem('user')
        Eif (!window.location.pathname.includes('/auth/login')) {
             message.error('会话已过期,请重新登录')
             window.location.href = '/auth/login'
        }
      } else if (error.response.status >= 500) {
        // Log detailed error for debugging
        // Use JSON.stringify to safely print object content
        console.error('Server Error:', JSON.stringify(error.response.data, null, 2))
        message.error('服务器内部错误,请稍后重试')
      } else {
        const detail = error.response.data?.detail
        const msg = typeof detail === 'string' ? detail : (detail?.message || '请求失败')
        message.error(msg)
      }
    } else if (error.request) {
        message.error('网络连接失败,请检查网络设置')
    } else {
        message.error('请求配置错误')
    }
    return Promise.reject(error)
  }
)
 
export default request