All files / lib axios.ts

68.18% Statements 15/22
25% Branches 2/8
66.66% Functions 4/6
72.22% Lines 13/18

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    55x 2x     55x 263x 263x 263x 2x 263x 3x 224x   3x     2x 3x                    
import axios from 'axios'
import { useAuthStore } from '@/features/auth/authStore'
 
const apiClient = axios.create({
  baseURL: '/api/v1',
  headers: {
    'Content-Type': 'application/json',
  },
}E)
 
apiClient.interceptors.request.use((config) => {
  const token = useAuthStore.getState().accessToken
  Iif (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

apiClient.interceptors.response.use(
  (response) => response,
  (error) => {
    if (error.response?.status === 401) {
      useAuthStore.getState().clearAuth()
    }
    return Promise.reject(error)
  },
)
 
export { apiClient }