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

Pass complete request to catchers, for easier request replay #15

Closed
c-eliasson opened this issue Jan 5, 2018 · 3 comments
Closed

Pass complete request to catchers, for easier request replay #15

c-eliasson opened this issue Jan 5, 2018 · 3 comments

Comments

@c-eliasson
Copy link

Just started using wretch and I really like the API. Currently building a large app, with a micro service architecture, and really appreciate how you can reuse a wretch object for each micro service component, with very small variations for each endpoint.

One thing that I'm missing, that would be convenient, is to have the complete wretch object for a given request, passed as an argument to the callback of any catcher.

The scenario I'm trying to address is primarily HTTP 401 responses, where my access token have expired. In that case, I would like to have a callback that can refresh the token, and then take the failed request and replay it (but with a new access token).

Pseudo-code example:

wretch("...")
  .get()
  .unauthorized((err, req) => {
    const token = getNewToken();
    // Replay the failed request, with the new token
    const response = req.auth(`Bearer ${token}`).json()
  })

Not the most useful example, but I believe it shows what I'm after.

What do you think about that?

@elbywan
Copy link
Owner

elbywan commented Jan 5, 2018

@c-eliasson

Just started using wretch and I really like the API.

Thanks a bunch 👍 !

What do you think about that?

Actually I have the exact same use case in the project I'm currently working on !

Just made a commit on the dev branch which should alleviate the issue by passing the original request along for catcher/resolver callbacks.

Here's an example for early catchers :

const reAuthOn401 = wretch()
  .catcher(401, async (error, originalRequest) => {
    // Renew credentials
    const token = await renewToken()
    // Replay the original request with new credentials
    return originalRequest
      .auth(`Bearer ${ token }`)
      .get()
      // Redefine authorized to prevent infinite loops in case or multiple 401 errors
      .unauthorized(err => { throw err })
      .json()
  })

reAuthOn401.url("/resource")
  .get()
  .json() // <- Will only be called for the original promise
  .then(callback) // <- Will be called for the original OR the replayed promise result

And for regular error catchers :

wretch("/resource")
  .get()
  .unauthorized(async (error, req) => {
    const token = await renewToken()
    return req
      .auth(`Bearer ${ token }`)
      .get()
      .unauthorized(err => { throw err })
      .json()
  })
  .json()
  .then(callback)

Could you check this out and tell me what you think about the syntax and if it solves your issue before I publish a new version ?

Thanks !

@c-eliasson
Copy link
Author

Wow, quick response indeed! This looks exactly like what I'm after, very nice! 👍

@elbywan
Copy link
Owner

elbywan commented Jan 8, 2018

@c-eliasson Released with version 1.1.1 📦

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

No branches or pull requests

2 participants