-
Notifications
You must be signed in to change notification settings - Fork 188
docs: clarify ofetch.raw response body access #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -272,16 +272,29 @@ await ofetch("/movies", { | |
|
|
||
| ## 🍣 Access to Raw Response | ||
|
|
||
| If you need to access raw response (for headers, etc), you can use `ofetch.raw`: | ||
| If you need to access response headers or status while still using `ofetch` parsing, use `ofetch.raw`: | ||
|
|
||
| ```js | ||
| const response = await ofetch.raw("/sushi"); | ||
|
|
||
| // response._data | ||
| // response.status | ||
| // response.headers | ||
| // ... | ||
| // response._data | ||
| ``` | ||
|
|
||
| `ofetch.raw` returns the native `Response` object, but `ofetch` still reads and parses the body for you. The parsed result is available on `response._data` (the same value returned by `ofetch()`). | ||
|
|
||
| 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 | ||
| ``` | ||
|
|
||
| In `onResponseError` hooks, use `error.data` to access the parsed error response body. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.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 🤖 Prompt for AI Agents |
||
|
|
||
| ## 🌿 Using Native Fetch | ||
|
|
||
| As a shortcut, you can use `ofetch.native` that provides native `fetch` API | ||
|
|
||
There was a problem hiding this comment.
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:
Repository: unjs/ofetch
Length of output: 8563
🏁 Script executed:
Repository: unjs/ofetch
Length of output: 1867
🏁 Script executed:
Repository: unjs/ofetch
Length of output: 2843
🏁 Script executed:
Repository: unjs/ofetch
Length of output: 2843
Mention the
streamexception.responseType: "stream"leaves the body available onresponse._datawithout 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