Skip to content

docs: clarify ofetch.raw response body access - #614

Open
vittorioexp wants to merge 1 commit into
unjs:mainfrom
vittorioexp:docs/raw-response-body
Open

docs: clarify ofetch.raw response body access#614
vittorioexp wants to merge 1 commit into
unjs:mainfrom
vittorioexp:docs/raw-response-body

Conversation

@vittorioexp

@vittorioexp vittorioexp commented Jul 22, 2026

Copy link
Copy Markdown

Summary

Closes #101.

Documents that ofetch.raw parses the response body into response._data, so native Response body methods are unavailable after parsing.

Test plan

  • Verified behavior in src/fetch.ts and FetchResponse._data typing
  • README-only change

Summary by CodeRabbit

  • Documentation
    • Clarified how to use ofetch.raw to access response headers and status information.
    • Documented parsed response data availability and body stream limitations.
    • Added a blob response example.
    • Clarified how to access parsed error bodies in onResponseError hooks.

Document that ofetch.raw parses the body into response._data and that native Response body methods will fail after parsing. Closes unjs#101.
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The README expands guidance for ofetch.raw, documenting parsed response data, consumed body streams, blob responses, and parsed error bodies in onResponseError.

Changes

Raw response documentation

Layer / File(s) Summary
Document ofetch.raw body handling
README.md
Explains native Response access, parsed content in response._data, body reader limitations, responseType: "blob", and error data access through error.data.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Suggested reviewers: maxtaran2010

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately describes the README change about clarifying ofetch.raw response body access.
Linked Issues check ✅ Passed The docs now explain that $fetch.raw/ofetch.raw parses the body into _data and native body readers fail after consumption.
Out of Scope Changes check ✅ Passed The changes are limited to README documentation and stay aligned with the stated body-consumption clarification.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a36d4788-4926-4fb1-b669-35d0fb773e58

📥 Commits

Reviewing files that changed from the base of the PR and between 1dbc37f and f49c044.

📒 Files selected for processing (1)
  • README.md

Comment thread README.md
Comment on lines +287 to +293
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 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 . || true

Repository: 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' || true

Repository: 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.md

Repository: 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.md

Repository: 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.

Comment thread README.md
// await response.blob() // throws: body already consumed
```

In `onResponseError` hooks, use `error.data` to access the parsed error response body.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 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.md

Repository: 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Body has already been consumed. with $fetch.raw

1 participant