-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Comments
Thanks a bunch 👍 !
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 ! |
Wow, quick response indeed! This looks exactly like what I'm after, very nice! 👍 |
@c-eliasson Released with version 1.1.1 📦 |
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:
Not the most useful example, but I believe it shows what I'm after.
What do you think about that?
The text was updated successfully, but these errors were encountered: