Skip to content

Axios Request ​

VTS provides a simple HTTP request module encapsulation based on Axios, offering unified interface invocation methods and response handling mechanisms.

Basic Configuration ​

The project creates an independent instance using axios, located in request.ts with the following basic configuration:

ts
const service: AxiosInstance = axios.create({
  baseURL: import.meta.env.VITE_BASE_API,
  timeout: 15000
})
  • baseURL: API base path obtained from environment variables
  • timeout: Request timeout set to 15 seconds

Request Interceptor ​

The request interceptor mainly handles the following:

  1. Token Processing:

    • Automatically adds Authorization token to requests (except login interface)
    • token is retrieved from Cookies
  2. Data Format Conversion:

    • Automatically converts request data from camelCase to snake_case
    • FormData type data remains unconverted
ts
service.interceptors.request.use(request => {
  const token = Cookie.get('token')
  
  // Convert naming convention (exclude FormData)
  if (!(request.data instanceof FormData)) {
    request.data = decamelizeKeys(request.data)
  }
  request.params = decamelizeKeys(request.params)

  // Add token (exclude login interface)
  if (request.url !== '/login') {
    request.headers.Authorization = token
  }

  return request
})

Response Interceptor ​

The response interceptor handles the following scenarios:

  1. Unified Response Data Structure:
ts
interface RespData<T> {
  success?: boolean
  errorCode?: number
  error?: number | string | null
  msg?: string
  data?: T
  [key: string]: any
}
  1. Special Response Handling:

    • Process binary data (Blob) responses
    • Handle file downloads
    • Automatically convert response data from snake_case to camelCase
  2. Error Handling:

    • HTTP status code error handling
    • Network request failure handling
    • Support for error redirection configuration

Error Codes ​

The system defines a unified error code message mapping:

Status CodeDescription
200Request successful
201Create or modify data successful
400Bad request
401Unauthorized (token/username/password error)
403Forbidden
404Resource not found
500Server error
502Gateway error
503Service unavailable
504Gateway timeout

Utility Functions ​

File Download ​

Provides file download-related utility functions:

ts
// Download file
export function downloadFile(boldData: any, filename = 'test-filename', type: string) {
  const blob = boldData instanceof Blob
    ? boldData
    : new Blob([boldData], { type })
  // ... download logic
}

// Process filename
function extractFileNameFromContentDispositionHeader(value: string) {
  // ... filename extraction logic
}

Usage Examples ​

ts
// Regular request
const response = await service.get('/api/users')

// File download request
const fileResponse = await service.get('/api/download', {
  responseType: 'blob'
})

// Request with redirection
const response = await service.get('/api/data', {
  redirect: 'error-page'
})

本站支持SSL安全访问Secure connection via SSL