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