Skip to content

Commit

Permalink
updated interceptor use type definition to remove interceptor options…
Browse files Browse the repository at this point in the history
… parameter from use method for response interceptors (axios#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 <[email protected]>
  • Loading branch information
amitsainii and jasonsaayman authored Oct 31, 2024
1 parent dbb56aa commit 82f9455
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,12 @@ declare namespace axios {
runWhen?: (config: InternalAxiosRequestConfig) => boolean;
}

type AxiosRequestInterceptorUse<T> = (onFulfilled?: ((value: T) => T | Promise<T>) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions) => number;

type AxiosResponseInterceptorUse<T> = (onFulfilled?: ((value: T) => T | Promise<T>) | null, onRejected?: ((error: any) => any) | null) => number;

interface AxiosInterceptorManager<V> {
use(onFulfilled?: (value: V) => V | Promise<V>, onRejected?: (error: any) => any, options?: AxiosInterceptorOptions): number;
use: V extends AxiosResponse ? AxiosResponseInterceptorUse<V> : AxiosRequestInterceptorUse<V>;
eject(id: number): void;
clear(): void;
}
Expand Down
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,12 @@ export interface AxiosInterceptorOptions {
runWhen?: (config: InternalAxiosRequestConfig) => boolean;
}

type AxiosRequestInterceptorUse<T> = (onFulfilled?: ((value: T) => T | Promise<T>) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions) => number;

type AxiosResponseInterceptorUse<T> = (onFulfilled?: ((value: T) => T | Promise<T>) | null, onRejected?: ((error: any) => any) | null) => number;

export interface AxiosInterceptorManager<V> {
use(onFulfilled?: ((value: V) => V | Promise<V>) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions): number;
use: V extends AxiosResponse ? AxiosResponseInterceptorUse<V> : AxiosRequestInterceptorUse<V>;
eject(id: number): void;
clear(): void;
}
Expand Down

0 comments on commit 82f9455

Please sign in to comment.