Skip to content

Commit 06239ed

Browse files
authored
fix(web): remove unsafe any types and improve error handling (#70)
Signed-off-by: wibus-wee <62133302+wibus-wee@users.noreply.github.com>
1 parent f59ccb1 commit 06239ed

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

web/lib/axios-interceptors.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"use client"
22

3-
import axios, { AxiosError } from 'axios'
3+
import axios, { AxiosError, InternalAxiosRequestConfig } from 'axios'
44
import { DefaultService as AnclaxService } from '@/api-anclax'
55
import toast from 'react-hot-toast'
66
import { authActions, getAuthState } from '@/atoms/auth'
77
import { setStoredAuthUser } from '@/lib/storage'
88

9+
interface RetryableRequestConfig extends InternalAxiosRequestConfig {
10+
_retry?: boolean
11+
}
12+
913
let initialized = false
1014
let interceptorId: number | null = null
1115
let 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}`)

web/lib/network-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const DEFAULT_INTERNAL_BASE = 'http://localhost:30080/api/v1'
88
const DEFAULT_EXTERNAL_BASE = 'http://localhost:8020/api/v1'
99

1010

11-
const resolveAccessToken = async (_options?: any): Promise<string> => {
11+
const resolveAccessToken = async (): Promise<string> => {
1212
const current = getAuthState()
1313
return current?.user?.accessToken ?? ''
1414
}
@@ -18,7 +18,7 @@ let lastBase = ''
1818

1919
export const resolveBaseUrl = () => {
2020
if (typeof window === 'undefined') return DEFAULT_INTERNAL_BASE
21-
return (window as any).APP_ENDPOINT || DEFAULT_EXTERNAL_BASE
21+
return window.APP_ENDPOINT || DEFAULT_EXTERNAL_BASE
2222
}
2323

2424
export function initNetworkConfig() {

web/lib/storage.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Credentials } from '@/api-anclax'
21
import type { AuthUser } from '@/atoms/auth'
32

43
const isBrowser = typeof window !== 'undefined'

0 commit comments

Comments
 (0)