Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runWhen option for response interceptors #4792

Open
aleksander237 opened this issue Jun 17, 2022 · 3 comments
Open

runWhen option for response interceptors #4792

aleksander237 opened this issue Jun 17, 2022 · 3 comments

Comments

@aleksander237
Copy link

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,

@shepard-liu
Copy link

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.

@twbagustin
Copy link

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

@agroves333
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants