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

Original request method #35

Closed
jsardev opened this issue Sep 12, 2018 · 11 comments
Closed

Original request method #35

jsardev opened this issue Sep 12, 2018 · 11 comments

Comments

@jsardev
Copy link

jsardev commented Sep 12, 2018

So I'd like to create an app-wide wretch instance which will retry requests on unauthorized statuses. It would be pretty easy with wretch, 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?

@elbywan
Copy link
Owner

elbywan commented Sep 12, 2018

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!

@jsardev
Copy link
Author

jsardev commented Sep 12, 2018

@elbywan Ehm... You've posted a link to my issue 😄

@elbywan
Copy link
Owner

elbywan commented Sep 12, 2018

@sarneeh Oops sorry my bad 😄!

Here is the correct link.

@jsardev
Copy link
Author

jsardev commented Sep 12, 2018

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()

@elbywan
Copy link
Owner

elbywan commented Sep 12, 2018

I've seen this particular issue and the documentation and they still don't answer my question.

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 _options field.

In the meantime if is perfectly safe to use the syntax above.

@jsardev
Copy link
Author

jsardev commented Sep 12, 2018

@elbywan It would be great if the originalRequest could have some additional API for this kind of situation. This would make this library even more awesome! 😄 Thank you very much for the example. I'd love to help to implement this but unfortunately, I'm completely out of time for about a month right now 😢 But if you don't have it either you can leave me this issue for later resolution 😄

@elbywan
Copy link
Owner

elbywan commented Sep 12, 2018

I'd love to help to implement this but unfortunately, I'm completely out of time for about a month right now 😢 But if you don't have it either you can leave me this issue for later resolution 😄

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 😉.

@MarlBurroW
Copy link

MarlBurroW commented Feb 15, 2019

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.

@jsardev
Copy link
Author

jsardev commented Feb 15, 2019

@MarlBurroW Unfortunately I'm still completely out of time, but I guess PRs are still welcome 😄

elbywan added a commit that referenced this issue Feb 23, 2019
@elbywan
Copy link
Owner

elbywan commented Feb 23, 2019

@sarneeh @MarlBurroW Sorry for the delay, I was quite busy also 😉 .

I just added a new .replay() method that should do the trick. (released in 1.5.0)

@elbywan elbywan closed this as completed Feb 23, 2019
@jsardev
Copy link
Author

jsardev commented Feb 23, 2019

@elbywan Awesome, thank you! 😄

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

3 participants