From e44bf3fd6c755de005b7b5d114a3a541e5c6fb5a Mon Sep 17 00:00:00 2001 From: Akino Date: Sat, 23 Nov 2024 11:09:54 +0800 Subject: [PATCH] fix(axios): duplicated register interceptors when hot updating --- .changeset/tough-yaks-sparkle.md | 5 +++++ packages/request/src/axios.ts | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 .changeset/tough-yaks-sparkle.md diff --git a/.changeset/tough-yaks-sparkle.md b/.changeset/tough-yaks-sparkle.md new file mode 100644 index 0000000..769a42f --- /dev/null +++ b/.changeset/tough-yaks-sparkle.md @@ -0,0 +1,5 @@ +--- +"@uozi-admin/request": patch +--- + +fix(axios): duplicated register interceptor when hot updating diff --git a/packages/request/src/axios.ts b/packages/request/src/axios.ts index 629fe6a..24f953a 100644 --- a/packages/request/src/axios.ts +++ b/packages/request/src/axios.ts @@ -27,6 +27,10 @@ export function createRequestInstance(service: AxiosInstance) { export const service = createService() +// 记录已注册的拦截器 +const registeredRequestInterceptors = new Set() +const registeredResponseInterceptors = new Set() + export const useAxios = () => { return { service, @@ -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, 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) }, } }