unjs / ofetch

๐Ÿ˜ฑ A better fetch API. Works on node, browser and workers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enable retry for customizable http methods

TimGuendel opened this issue ยท comments

Describe the feature

The docs state the following:

Default for retry is 1 retry, except for POST, PUT, PATCH and DELETE methods where ofetch does not retry.

Why does retry only work for GET? ๐Ÿค”

There are plenty of cases where a retry with the other methods would be useful.
I often use POST requests to send params in the body instead of path / query params, especially when there are a lot of them.

So far I am using the auto retry feature in order to resend the same request with an updated auth token in the header. But this is useless for me if I am limited to GET requests.

Maybe UseFetchOptions can be extended with an array of the methods to be retried?

retryStatusCodes: [GET, POST]

While I am at it, why not make the retry logic accessible to the user, like Axios? It was possible to resend the very same request inside of an interceptor:

const originalRequestConfig = error.config;
// [...] Update the headers
return $axios.request(originalRequestConfig); 

๐Ÿ‘‹

Additional information

  • Would you be willing to help implement this feature?

Because a retry for non-idempotent requests (e.g. those changing data, so anything except GET) could have unwanted consequences.

Think of an API endpoint sending a 500 after submitting your data to the database.
With retry, you'd accidentally spam the DB ๐Ÿ˜‹

Thanks for describing your issue @TimGuendel. As @manniL explained, the default behavior of retry is to support for non-idempotent requests that have no payload.

I think it is fair to ask supporting retry for other methods as long as once clearly knows it is fully safe to do so.

I have rechecked implementation and if you manually set retry option to a number like ofetch('/url', { method: 'POST', retry: 5 ]) ofetch still allows retrying would it solve your problem?

Adding a new option also works but the idea of unjs tools and ofetch is to be as minimal as possible. That's why adding this option is good idea if probably there is no alternative.

I should have the capability to retry any failed request, regardless of the method used. This was the sole reason for wanting compatibility with POST, PATCH, and DELETE.

A more effective approach would be to do it like axios by including the original request in the interceptor and providing a method to retry that specific request, irrespective of the method employed.

This very feature made us go back to using axios in a Nuxt 3 Project.

  1. a function only does one thing, retry is retry, why mix in other logic
  2. do not add any custom behavior, if you have to add, then you need to add the corresponding undo method

@TimGuendel It is as simple as setting retry: 3 to enable for all methods :)

@TimGuendel It is as simple as setting retry: 3 to enable for all methods :)

I did - but it is too late now ๐Ÿ˜ฌMaybe the docs should be updated to reflect this behavior? So far, it says:

Default for retry is 1 retry, except for POST, PUT, PATCH and DELETE methods where ofetch does not retry.

๐Ÿ‘‹

Looks like 3 is the lucky number.