From 82f94556e5301d6ce3ef8006fbf6892dc1141bd1 Mon Sep 17 00:00:00 2001 From: Amit Saini Date: Thu, 31 Oct 2024 19:31:37 +0530 Subject: [PATCH] updated interceptor use type definition to remove interceptor options parameter from use method for response interceptors (#5237) * updated interceptor use type definition to remove interceptor options parameter from use method for response interceptors * incorporated type changes to index.d.cts --------- Co-authored-by: Jay --- README.md | 4 +++- index.d.cts | 6 +++++- index.d.ts | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 626a695..70e1a2e 100644 --- a/README.md +++ b/README.md @@ -764,7 +764,7 @@ axios.interceptors.request.use(function (config) { ``` If you want to execute a particular interceptor based on a runtime check, -you can add a `runWhen` function to the options object. The interceptor will not be executed **if and only if** the return +you can add a `runWhen` function to the options object. The request interceptor will not be executed **if and only if** the return of `runWhen` is `false`. The function will be called with the config object (don't forget that you can bind your own arguments to it as well.) This can be handy when you have an asynchronous request interceptor that only needs to run at certain times. @@ -779,6 +779,8 @@ axios.interceptors.request.use(function (config) { }, null, { runWhen: onGetCall }); ``` +> **Note:** options parameter(having `synchronous` and `runWhen` properties) is only supported for request interceptors at the moment. + ### Multiple Interceptors Given you add multiple response interceptors diff --git a/index.d.cts b/index.d.cts index 7d12dd3..d513617 100644 --- a/index.d.cts +++ b/index.d.cts @@ -493,8 +493,12 @@ declare namespace axios { runWhen?: (config: InternalAxiosRequestConfig) => boolean; } + type AxiosRequestInterceptorUse = (onFulfilled?: ((value: T) => T | Promise) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions) => number; + + type AxiosResponseInterceptorUse = (onFulfilled?: ((value: T) => T | Promise) | null, onRejected?: ((error: any) => any) | null) => number; + interface AxiosInterceptorManager { - use(onFulfilled?: (value: V) => V | Promise, onRejected?: (error: any) => any, options?: AxiosInterceptorOptions): number; + use: V extends AxiosResponse ? AxiosResponseInterceptorUse : AxiosRequestInterceptorUse; eject(id: number): void; clear(): void; } diff --git a/index.d.ts b/index.d.ts index 9ecf467..21bb581 100644 --- a/index.d.ts +++ b/index.d.ts @@ -476,8 +476,12 @@ export interface AxiosInterceptorOptions { runWhen?: (config: InternalAxiosRequestConfig) => boolean; } +type AxiosRequestInterceptorUse = (onFulfilled?: ((value: T) => T | Promise) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions) => number; + +type AxiosResponseInterceptorUse = (onFulfilled?: ((value: T) => T | Promise) | null, onRejected?: ((error: any) => any) | null) => number; + export interface AxiosInterceptorManager { - use(onFulfilled?: ((value: V) => V | Promise) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions): number; + use: V extends AxiosResponse ? AxiosResponseInterceptorUse : AxiosRequestInterceptorUse; eject(id: number): void; clear(): void; }