Issue:
I encountered a problem while attempting to resend my original request using superagent. Here is some pseudo code that I came up with:
function retryRequest({
params
}) {
return superagent.post(url)
.set("Authorization", `Bearer ${process.env.initialToken}`)
.send({data})
.then(res => {
if (res.unauthorized) {
// My goal is to make an API call, obtain a new token, and then retry the postRequest() with the updated bearer
}
})
.catch(err => {
throw err
});
}
Inquiries:
While researching how others handle this situation, I observed that some developers tackle it in the catch block, while others address it within the response. Additionally, some incorporate the .on
plugin to validate.
Upon examining the superagent documentation, I am contemplating whether I should experiment with a custom retry()
function. Is there a possibility of utilizing
request.auth('my_token', { type: 'bearer' })
in a certain manner? Although seemingly straightforward, deciphering their documentation for such scenarios can be challenging. I would appreciate hearing your insights on this matter!