unjs / ofetch

😱 A better fetch API. Works on node, browser and workers.
MIT License
4.16k stars 128 forks source link

Failed to parse response with `json` on `ofetch.raw` #354

Closed becem-gharbi closed 5 months ago

becem-gharbi commented 9 months ago

Environment

Reproduction

https://stackblitz.com/edit/github-ojv4rk?file=app.vue

Describe the bug

When using ofetch.raw the raw response object cannot be parsed with json method (this works well with ofetch.native) . The failure happends both on client and server side thus I think it's not related to Node.

Additional context

I'm using @iconify/vue library. It provides setFetch method to use a custom fetch API. I have set fetch.raw in order benefit from the interceptors feature. The response on fetching icons is JSON thus it is parsed with json method.

Logs

### Node

TypeError: Body is unusable
    at specConsumeBody (https://rlmilwjxkgithub-cdbi.w-credentialless.staticblitz.com/blitz.6854296d.js:194:73015)
    at $.json (https://rlmilwjxkgithub-cdbi.w-credentialless.staticblitz.com/blitz.6854296d.js:194:74238)
    at eval (file:///home/projects/rlmilwjxk.github/app.vue:22:74)
    at Module.withAsyncContext (file:///home/projects/rlmilwjxk.github/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:3366:19)
    at setup (file:///home/projects/rlmilwjxk.github/app.vue:22:47)

### Browser

TypeError: Failed to execute 'json' on 'Response': body stream already read
Lpmvb commented 9 months ago

https://github.com/unjs/ofetch/blob/244fb6a720478a8e834f4e2715f8cef866802440/src/fetch.ts#L188

response._data is what you want.