Skip to content

Commit 5f17585

Browse files
authored
refactor!: use native JSON.parse (#520)
1 parent 6a879d4 commit 5f17585

File tree

5 files changed

+9
-20
lines changed

5 files changed

+9
-20
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ import { ofetch } from "ofetch";
3333

3434
## ✔️ Parsing Response
3535

36-
`ofetch` will smartly parse JSON and native values using [destr](https://github.com/unjs/destr), falling back to the text if it fails to parse.
36+
`ofetch` smartly parse JSON responses.
3737

3838
```js
3939
const { users } = await ofetch("/api/users");
4040
```
4141

4242
For binary content types, `ofetch` will instead return a `Blob` object.
4343

44-
You can optionally provide a different parser than `destr`, or specify `blob`, `arrayBuffer`, `text` or `stream` to force parsing the body with the respective `FetchResponse` method.
44+
You can optionally provide a different parser than `JSON.parse`, or specify `blob`, `arrayBuffer`, `text` or `stream` to force parsing the body with the respective `FetchResponse` method.
4545

4646
```js
4747
// Use JSON.parse
@@ -398,9 +398,9 @@ myFetch("/foo", { requiresAuth: true });
398398

399399
## 📦 Bundler Notes
400400

401-
- All targets are exported with Module and CommonJS format and named exports
401+
- All targets are exported with ESM format and named exports
402402
- No export is transpiled for the sake of modern syntax
403-
- You probably need to transpile `ofetch`, `destr`, and `ufo` packages with Babel for ES5 support
403+
- You probably need to transpile `ofetch` for ES5 support
404404
- You need to polyfill `fetch` global for supporting legacy browsers like using [unfetch](https://github.com/developit/unfetch)
405405

406406
## ❓ FAQ

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
"release": "pnpm test && pnpm build && changelogen --release --prerelease --publish --publishTag alpha --push",
2424
"test": "pnpm lint && vitest run --coverage"
2525
},
26-
"dependencies": {
27-
"destr": "^2.0.5",
28-
"ufo": "^1.6.1"
29-
},
3026
"devDependencies": {
3127
"@types/node": "^24.9.1",
3228
"@vitest/coverage-v8": "^4.0.4",

pnpm-lock.yaml

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fetch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Readable } from "node:stream";
2-
import destr from "destr";
32
import { withBase, withQuery } from "./utils.url.ts";
43
import { createFetchError } from "./error.ts";
54
import {
@@ -213,12 +212,13 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
213212
: context.options.responseType) ||
214213
detectResponseType(context.response.headers.get("content-type") || "");
215214

216-
// We override the `.json()` method to parse the body more securely with `destr`
217215
switch (responseType) {
218216
case "json": {
219217
const data = await context.response.text();
220-
const parseFunction = context.options.parseResponse || destr;
221-
context.response._data = parseFunction(data);
218+
if (data) {
219+
const parseFunction = context.options.parseResponse || JSON.parse;
220+
context.response._data = parseFunction(data);
221+
}
222222
break;
223223
}
224224
case "stream": {

test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ describe("ofetch", () => {
252252

253253
it("204 no content", async () => {
254254
const res = await $fetch(getURL("204"));
255-
expect(res).toBe("");
255+
expect(res).toBe(undefined);
256256
});
257257

258258
it("HEAD no content", async () => {

0 commit comments

Comments
 (0)