Skip to content

Commit

Permalink
fix(axios): duplicated register interceptors when hot updating
Browse files Browse the repository at this point in the history
  • Loading branch information
akinoccc committed Nov 23, 2024
1 parent 8dba55d commit e44bf3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-yaks-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@uozi-admin/request": patch
---

fix(axios): duplicated register interceptor when hot updating
12 changes: 12 additions & 0 deletions packages/request/src/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export function createRequestInstance(service: AxiosInstance) {

export const service = createService()

// 记录已注册的拦截器
const registeredRequestInterceptors = new Set<string>()
const registeredResponseInterceptors = new Set<string>()

export const useAxios = () => {
return {
service,
Expand All @@ -35,14 +39,22 @@ export const useAxios = () => {
onRejected?: (error: any) => any | null,
options?: AxiosInterceptorOptions,
) {
const id = onFulfilled.toString() + onRejected?.toString() + JSON.stringify(options)
if (registeredRequestInterceptors.has(id)) return

service.interceptors.request.use(onFulfilled, onRejected, options)
registeredRequestInterceptors.add(id)
},
setResponseInterceptor(
onFulfilled: (value: any) => any | Promise<any>,
onRejected?: (error: any) => any | null,
options?: AxiosInterceptorOptions,
) {
const id = onFulfilled.toString() + onRejected?.toString() + JSON.stringify(options)
if (registeredResponseInterceptors.has(id)) return

service.interceptors.response.use(onFulfilled, onRejected, options)
registeredResponseInterceptors.add(id)
},
}
}

0 comments on commit e44bf3f

Please sign in to comment.