All files / lib axios.ts

42.3% Statements 11/26
43.75% Branches 7/16
50% Functions 3/6
45% Lines 9/20

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    37x 1x       37x 280x 280x   1x       2x       280x   203x                        
import axios from 'axios'
import { useAuthStore } from '@/features/auth/authStore'
 
const apiClient = axios.create({
  baseURL: '/api/v1',
  withCredentials: true,
  headers: {
    'Content-Type': 'application/json',
  },
})
 
apiClient.interceptors.request.use((config) => {
  const method = config.method?.toLowerCase()
  if (method && ['post', 'put', 'patch', 'delete'].includes(method)) {
    if (!config.headers['Idempotency-Key']) {
  I    config.headers.set('Idempotency-Key', crypto.randomUUID())
    }
  }
  return config
})
 
apiClient.interceptors.response.use(
  (response) => response,
  (error) => {
    if (error.response?.status === 401) {
      useAuthStore.getState().clearAuth()
      window.location.hash = '#/login?expired=true'
    }
    return Promise.reject(error)
  },
)
 
export { apiClient }