question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

request intercepters order is reversed

See original GitHub issue
const axios = require('axios');

axios.interceptors.request.use(function(config) {
	console.log(1);
}, undefined);


axios.interceptors.request.use(function(config) {
	console.log(2);
}, undefined);

axios.get('http://www.baidu.com').then(function() {
	console.log('ok');
}).catch(function() {
	console.log('fail');
});

which prints:

node index.js
2
1
fail

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:10
  • Comments:25 (6 by maintainers)

github_iconTop GitHub Comments

11reactions
latelcommented, Sep 2, 2018

anyone here to see this problem?

6reactions
marceloloureirocommented, Dec 10, 2020

/* Edited: I had forgotten to pipe the handlers */

Well, I’ve used a different aproach. First off, I’ve defined an Object called InterceptionHandler. I also have defined a pipe function to chain the handlers

class InterceptionHandler {
  constructor({request, response}) {
    this.request = request;
    this.response = response;
  }
}

const pipe = (...handlers) => arg => handlers.reduce((output, actual) => actual ? actual(output) : output, arg);

So, the ideia is create multiple handlers, and use them in a single pair of interceptors.

const handler1 = new InterceptionHandler(
  {
    request: (config) => {
      console.log('request 1');
      return config;
    },

    response: (response) => {
      console.log('response 1')
      return response;
    }
  }
);

const handler2 = new InterceptionHandler(
  {
    request: (config) => {
      console.log('request 2');
      return config;
    },

    response: (response) => {
      console.log('response 2')
      return response;
    }
  }
);

After that we can line up the handlers in an appropriate execution order.


axios.interceptors.request.use(
  (config) => pipe(handler1.request, handler2.request)(config),
  (error) => Promise.reject(error)
);

axios.interceptors.response.use(
  (response) => pipe(handler1.response, handler2.response)(response),
  (error) => Promise.reject(error)
);

Which produces the output

request 1
request 2
response 1
response 2
Read more comments on GitHub >

github_iconTop Results From Across the Web

Order of applying request interceptors is reversed. #841 - GitHub
I've encountered a bug(or a feature) that my request interceptors were applied in reversed order. This is the culprit:
Read more >
What is the order of execution of $http interceptors in ...
It seems that the interceptors are executed: in registration order for requests; in reverse registration order for responses.
Read more >
Interceptors class - dio library - Dart API - Pub.dev
API docs for the Interceptors class from the dio library, ... reversed → Iterable<Interceptor>: An Iterable of the objects in this list in...
Read more >
OkHttp Logging and Order of Interceptors | by Herman Cheung
We may intercept the HTTP requests and responses, modify them. ... level interceptor, and the order to get the HTTP response is reversed....
Read more >
Introduction to CORBA Request-Level Interceptors
A request-level interceptor is a user-written CORBA object that provides a means to ... a response in the reverse order than that of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found