Skip to content

runWhen option for response interceptors #4792

Closed

Description

@aleksander237

Is your feature request related to a problem? Please describe.

It is impossible to define runWhen function for response interceprtors.

Describe the solution you'd like

I would love to have the possibility of defining the runWhen option for response interceptors

Describe alternatives you've considered

http.interceptors.response.use((response) => {
    const message = response.data?.message;
    if(message && response.config.notify) {
        Notify.create({
            position: 'top',
            color: 'positive',
            message: message,
            icon: 'fal fa-info-circle'
        });
    }
    return Promise.resolve(response);
},(error) => {
    if(error.config.notify) {
        let message = '';
        if (error.response && error.response.data.message) {
            message = error.response.data.message;
        } else if (error.request) {
            message = 'No response';
        } else {
            // Something happened in setting up the request that triggered an Error
            message = `Unknown error: ${error.message}`;
        }
        Notify.create({
            position: 'top',
            color: 'negative',
            message: message,
            icon: 'fal fa-exclamation-circle'
        });
    }
    return Promise.reject(error);
});

Additional context

it is useful sometimes,

Activity

  1. shepard-liu commented on Jul 22, 2022

    @shepard-liu

    I ran into that problem while configuring the response interceptors too.

    The runWhen function you parsed into the options is simply not invoked when axios adds the response interceptors to the chain. See the following snippet from the source code(lib/core/Axios.js line 79 ~ 94).

    const requestInterceptorChain = [];
    let synchronousRequestInterceptors = true;
    this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
      if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
        return;
      }
    
      synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
    
      requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
    });
    
    const responseInterceptorChain = [];
    this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
      responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
    });

    I have no idea why it was designed so but you can actually set the runWhen function as an option without getting an error with TypeScript. The request interceptors and the response interceptors share a common InterceptorManager class, and they are typed consistently.

    btw, the request interceptors are invoked in the reversed order, notice that unshift is used. This is not mentioned in the docs, but I found it in the interceptors test at test/specs/interceptors.spec.js.

    /* request interceptors have a reversed execution order */

    There are several issues and pull requests related to this feature. It's still not documented or changed, together with the runWhen problem.

    Maybe you can workaround it by modifying the source by now. Here's a forked version I did, hope it can be helpful. npm - axios-no-reverse. Note that I also changed the execution order of request interceptors.

  2. twbagustin commented on Jan 19, 2023

    @twbagustin

    Ping in 2023 for understanding the rationale here of runWhen on requests only but not on responses, besides the docs needs updating yup, found this closed issue #4228 which should be reopened, latest axios here v1.2.3 seems to lack support still

  3. agroves333 commented on Aug 25, 2023

    @agroves333

    +1

  4. jasonsaayman commented on Jul 30, 2025

    @jasonsaayman
    Member

    Thanks for the issue, we are adding this as a potential feature to a mega-feature issue with many feature requests please see the motivation for doing this:

    #6983

    And the mega-feature issue here:

    #6984

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions