-
-
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
Original request method #35
Comments
Hi @sarneeh, could you check this issue? I think that it might contain a detailed answer 😉. Also here is the link to the documentation section that covers how to replay a request with a simple example. Hope this will help! |
@elbywan Ehm... You've posted a link to my issue 😄 |
@sarneeh Oops sorry my bad 😄! Here is the correct link. |
I've seen this particular issue and the documentation and they still don't answer my question. Let me show you an example based on your post: const reAuthOn401 = wretch()
.catcher(401, async (error, originalRequest) => {
const token = await renewToken()
return originalRequest
.auth(`Bearer ${ token }`)
.get() // This won't work as my originalRequest was a .post()
.unauthorized(err => { throw err })
.json()
})
reAuthOn401.url("/resource")
.post({})
.json() |
I'm sorry I understand the issue now. There is indeed a way to achieve this, event if it may (rightfully) seem a bit hacky but that should still work. // Same example
const reAuthOn401 = wretch()
.catcher(401, async (error, originalRequest) => {
const token = await renewToken()
// Reuse the same http method
// If you use OPTIONS verb, then map it to 'opts', otherwise all good
const method = originalRequest._options.method.toLowerCase()
return originalRequest
.auth(`Bearer ${ token }`)
[method]() // <- here
.unauthorized(err => { throw err })
.json()
})
reAuthOn401.url("/resource")
.post({})
.json() I really need to think about this. Maybe I'll expose this variable in a less hacky way, or at least document the In the meantime if is perfectly safe to use the syntax above. |
@elbywan It would be great if the |
Sure, thanks! I'm also a bit busy these days but I think I can free some time before a month to work on this. If so I'll update this issue 😉. |
Any news about this issue ? I'm exactly in the same case (global api instance with a global 401 catcher for multiple methods), this is a very common use case and didn't find an elegant way to replay the original request (with the same method). I think something like this could be nice: // Same example
const reAuthOn401 = wretch()
.catcher(401, async (error, originalRequest) => {
const token = await renewToken()
return originalRequest.auth(`Bearer ${ token }`).play() // Replay the request with original method
.unauthorized(err => { throw err })
// then go ... (see below)
})
reAuthOn401.url("/resource")
.post({})
// ... Here !
.json() But I do not know if the Fetch or Promise API allows to do this. |
@MarlBurroW Unfortunately I'm still completely out of time, but I guess PRs are still welcome 😄 |
@sarneeh @MarlBurroW Sorry for the delay, I was quite busy also 😉 . I just added a new |
@elbywan Awesome, thank you! 😄 |
So I'd like to create an app-wide
wretch
instance which will retry requests onunauthorized
statuses. It would be pretty easy withwretch
, but unfortunately, I don't really know how do I replay the same specific request.Because actually sending the request requires a call to
.get()
,.post()
or whatever method it was, I can't replay the same request having a global instance.Am I missing something? Is there a way to send the request without calling the
.[httpMethod]()
function?The text was updated successfully, but these errors were encountered: