ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

Inconsistent headers object returned after a retried request

Open cmgdragon opened this issue 1 year ago • 0 comments

Environment

ofetch 1.3.4 node 20.12.2

Reproduction

https://stackblitz.com/edit/vitejs-vite-53ww8b?file=main.js

Open the devtools console and click the POST REQUEST button to see the issue

You can see how the headers are being turned into a Headers object no matter the original input, making the headers not accesible anymore when the request loops back

Describe the bug

coming from https://github.com/nuxt/nuxt/issues/27042

$fetch is transforming the headers object into a Headers class if there's a body and the method is other than GET; so if headers are being added in the onRequest interceptor using normal object property accessors instead of the Headers constructor (as shown in the useFetch documentation at the moment I'm writing this issue), when the request is retried and loops back, these are being added as extra properties of the Headers object and ignored later

The expected behavior would be that the headers object was consistent across retried requests, and even across all interceptors

I think the headers object should either be normalized or at least be clear that the Headers constructor is required for modifying the request inside interceptors

Additional context

I came across this issue when I tried to refresh a JWT using the retry option along with the onResponseError interceptor: https://github.com/nuxt/nuxt/discussions/26991

I also tried to fix it in this commit, normalizing the headers before the retry and after the onRequest: https://github.com/cmgdragon/ofetch/commit/8954d8c2f828004c6af703579eab422ce7404bdf, though this solution isn't taking into consideration any other interceptor but onRequest for modifying the headers

Logs

No response

cmgdragon avatar May 07 '24 22:05 cmgdragon