5

I am currently using request to make calls to an external service provider for some of my REST API endpoints. I have a consumer key and secret that I can use to get a token which I then attach to every request.

Instead of chaining two operation (getting/refreshing) the token and then requesting (I'm using a promise-based version of request) (see code below). I would like to add a global interceptor that would intercept every outgoing request, fetch or refresh the token and then attach it to the request.

getToken().
   then(function (token) {
        return request({
              url: url,
              headers: { Authorization: `Bearer ${token}` }
        });
   })
   .then(function (data) { do things here})

any idea on how it can be achieved?

Thanks

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
islem baki
  • 51
  • 4
  • Possible duplicate of [How can I intercept all node request-promise http requests to modify the request options?](https://stackoverflow.com/questions/44004758/how-can-i-intercept-all-node-request-promise-http-requests-to-modify-the-request) – Sunil Garg Nov 28 '17 at 06:27

0 Answers0