11"use client"
22
3- import axios , { AxiosError } from 'axios'
3+ import axios , { AxiosError , InternalAxiosRequestConfig } from 'axios'
44import { DefaultService as AnclaxService } from '@/api-anclax'
55import toast from 'react-hot-toast'
66import { authActions , getAuthState } from '@/atoms/auth'
77import { setStoredAuthUser } from '@/lib/storage'
88
9+ interface RetryableRequestConfig extends InternalAxiosRequestConfig {
10+ _retry ?: boolean
11+ }
12+
913let initialized = false
1014let interceptorId : number | null = null
1115let refreshPromise : Promise < string > | null = null
@@ -57,7 +61,7 @@ export function initAxiosAuthInterceptors() {
5761 interceptorId = axios . interceptors . response . use (
5862 ( response ) => response ,
5963 async ( error : AxiosError ) => {
60- const status = error . response ?. status ?? ( error as any ) . status
64+ const status = error . response ?. status ?? error . status
6165 const requestUrl =
6266 error . config ?. url || error . request ?. responseURL || ''
6367
@@ -71,15 +75,19 @@ export function initAxiosAuthInterceptors() {
7175
7276 // Handle 401 with single-flight refresh & replay
7377 if ( status === 401 ) {
74- const originalRequest : any = error . config || { }
78+ if ( ! error . config ) {
79+ return Promise . reject ( error )
80+ }
81+
82+ const originalRequest = error . config as RetryableRequestConfig
7583 if ( originalRequest . _retry ) {
7684 return Promise . reject ( error )
7785 }
7886 originalRequest . _retry = true
7987
8088 try {
8189 const token = await refreshAccessToken ( )
82- if ( originalRequest && token ) {
90+ if ( token ) {
8391 const headers = originalRequest . headers || { }
8492 if ( typeof headers . set === 'function' ) {
8593 headers . set ( 'Authorization' , `Bearer ${ token } ` )
0 commit comments