docs: clarify ofetch.raw response body access - #614
Conversation
Document that ofetch.raw parses the body into response._data and that native Response body methods will fail after parsing. Closes unjs#101.
📝 WalkthroughWalkthroughThe README expands guidance for ChangesRaw response documentation
Estimated code review effort: 1 (Trivial) | ~2 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@README.md`:
- Around line 287-293: Update the README warning to exclude responseType:
"stream": clarify that body-consuming response types such as json, blob, text,
and arrayBuffer consume the stream and require reading response._data, while
stream leaves the body available without consuming it.
- Line 296: Update the README onResponseError hook example to destructure
response and access its parsed body through response._data instead of
error.data. Preserve error.data usage in the separate caught FetchError example.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
| Because the body stream is consumed during parsing, calling `response.json()`, `response.blob()`, `response.text()`, or similar methods on the returned response will throw a "body stream already read" error. Read the parsed body from `response._data` instead: | ||
|
|
||
| ```js | ||
| const response = await ofetch.raw("/favicon.png", { responseType: "blob" }); | ||
|
|
||
| const blob = response._data; // Blob | ||
| // await response.blob() // throws: body already consumed |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Locate the relevant README section and implementation around response handling.
rg -n 'responseType|_data|body already read|stream' README.md src test . -g '!node_modules' -g '!dist' -g '!build' || true
# Show the README excerpt around the cited lines.
sed -n '270,310p' README.md
# Find the core implementation files that assign _data / handle stream responses.
fd -a 'ofetch' .
fd -a 'fetch' src . || trueRepository: unjs/ofetch
Length of output: 8563
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Narrow the implementation search to likely source files if present.
rg -n '_data|responseType|stream|body consumed|body already read' . \
-g '!node_modules' -g '!dist' -g '!build' -g '!coverage' \
-g '!*.md' || trueRepository: unjs/ofetch
Length of output: 1867
🏁 Script executed:
#!/bin/bash
set -euo pipefail
sed -n '200,240p' src/fetch.ts
printf '\n---\n'
sed -n '280,320p' README.mdRepository: unjs/ofetch
Length of output: 2843
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Inspect the responseType handling and any README mention of stream behavior.
sed -n '200,240p' src/fetch.ts
printf '\n---\n'
sed -n '280,320p' README.mdRepository: unjs/ofetch
Length of output: 2843
Mention the stream exception. responseType: "stream" leaves the body available on response._data without consuming it, so this warning should apply only to response types that actually read the body (json, blob, text, arrayBuffer, etc.).
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@README.md` around lines 287 - 293, Update the README warning to exclude
responseType: "stream": clarify that body-consuming response types such as json,
blob, text, and arrayBuffer consume the stream and require reading
response._data, while stream leaves the body available without consuming it.
| // await response.blob() // throws: body already consumed | ||
| ``` | ||
|
|
||
| In `onResponseError` hooks, use `error.data` to access the parsed error response body. |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- README excerpt around line 296 ---'
sed -n '280,310p' README.md
printf '\n%s\n' '--- search for onResponseError / FetchError.data / response._data ---'
rg -n "onResponseError|FetchError\.data|response\._data|_data" README.md . --glob '!**/node_modules/**' --glob '!**/dist/**' --glob '!**/build/**'Repository: unjs/ofetch
Length of output: 5291
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- src/fetch.ts relevant section ---'
sed -n '200,270p' src/fetch.ts
printf '\n%s\n' '--- src/error.ts relevant section ---'
sed -n '1,140p' src/error.ts
printf '\n%s\n' '--- README onResponseError section ---'
sed -n '205,225p' README.mdRepository: unjs/ofetch
Length of output: 4852
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo '--- src/fetch.ts onResponseError and parsing order ---'
nl -ba src/fetch.ts | sed -n '210,265p'
echo
echo '--- src/error.ts data aliasing ---'
nl -ba src/error.ts | sed -n '1,120p'
echo
echo '--- src/types.ts onResponseError type ---'
nl -ba src/types.ts | sed -n '100,150p'Repository: unjs/ofetch
Length of output: 244
Use response._data in onResponseError
onResponseError receives { request, response, options }, so error.data is out of scope here. Update the example to ({ response }) => response._data; keep error.data for the caught FetchError.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@README.md` at line 296, Update the README onResponseError hook example to
destructure response and access its parsed body through response._data instead
of error.data. Preserve error.data usage in the separate caught FetchError
example.
Summary
Closes #101.
Documents that
ofetch.rawparses the response body intoresponse._data, so nativeResponsebody methods are unavailable after parsing.Test plan
src/fetch.tsandFetchResponse._datatypingSummary by CodeRabbit
ofetch.rawto access response headers and status information.onResponseErrorhooks.