Comparing version 1.1.1 to 1.2.0
@@ -1,3 +0,3 @@ | ||
import { $ as $Fetch } from './error-d5becc6e.js'; | ||
export { C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './error-d5becc6e.js'; | ||
import { $ as $Fetch } from './shared/ofetch.ffd77bf3.js'; | ||
export { C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './shared/ofetch.ffd77bf3.js'; | ||
@@ -9,5 +9,9 @@ declare const fetch: typeof globalThis.fetch; | ||
}; | ||
declare const AbortController: { | ||
new (): AbortController; | ||
prototype: AbortController; | ||
}; | ||
declare const ofetch: $Fetch; | ||
declare const $fetch: $Fetch; | ||
export { $Fetch, $fetch, Headers, fetch, ofetch }; | ||
export { $Fetch, $fetch, AbortController, Headers, fetch, ofetch }; |
@@ -1,3 +0,3 @@ | ||
import { $ as $Fetch } from './error-d5becc6e.js'; | ||
export { C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './error-d5becc6e.js'; | ||
import { $ as $Fetch } from './shared/ofetch.ffd77bf3.js'; | ||
export { C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './shared/ofetch.ffd77bf3.js'; | ||
@@ -10,5 +10,9 @@ declare function createNodeFetch(): (input: RequestInfo, init?: RequestInit) => any; | ||
}; | ||
declare const AbortController: { | ||
new (): AbortController; | ||
prototype: AbortController; | ||
}; | ||
declare const ofetch: $Fetch; | ||
declare const $fetch: $Fetch; | ||
export { $Fetch, $fetch, Headers, createNodeFetch, fetch, ofetch }; | ||
export { $Fetch, $fetch, AbortController, Headers, createNodeFetch, fetch, ofetch }; |
{ | ||
"name": "ofetch", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A better fetch API. Works on node, browser and workers.", | ||
@@ -23,17 +23,20 @@ "repository": "unjs/ofetch", | ||
"workerd": "./dist/index.mjs", | ||
"node": { | ||
"types": "./dist/node.d.ts", | ||
"import": "./dist/node.mjs", | ||
"require": "./dist/node.cjs" | ||
"import": { | ||
"types": "./dist/index.d.mts", | ||
"default": "./dist/index.mjs" | ||
}, | ||
"default": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.cjs" | ||
"require": { | ||
"types": "./dist/node.d.cts", | ||
"default": "./dist/node.cjs" | ||
} | ||
}, | ||
"./node": { | ||
"types": "./dist/node.d.ts", | ||
"import": "./dist/node.mjs", | ||
"require": "./dist/node.cjs" | ||
"import": { | ||
"types": "./dist/node.d.mts", | ||
"default": "./dist/node.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/node.d.cts", | ||
"default": "./dist/node.cjs" | ||
} | ||
} | ||
@@ -59,23 +62,23 @@ }, | ||
"dependencies": { | ||
"destr": "^2.0.0", | ||
"node-fetch-native": "^1.2.0", | ||
"ufo": "^1.1.2" | ||
"destr": "^2.0.1", | ||
"node-fetch-native": "^1.3.0", | ||
"ufo": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.3.1", | ||
"@vitest/coverage-v8": "^0.32.2", | ||
"changelogen": "^0.5.3", | ||
"eslint": "^8.43.0", | ||
"@types/node": "^20.5.2", | ||
"@vitest/coverage-v8": "^0.34.2", | ||
"changelogen": "^0.5.5", | ||
"eslint": "^8.47.0", | ||
"eslint-config-unjs": "^0.2.1", | ||
"fetch-blob": "^4.0.0", | ||
"formdata-polyfill": "^4.0.10", | ||
"h3": "^1.6.6", | ||
"jiti": "^1.18.2", | ||
"listhen": "^1.0.4", | ||
"prettier": "^2.8.8", | ||
"typescript": "^5.1.3", | ||
"unbuild": "^1.2.1", | ||
"vitest": "^0.32.2" | ||
"h3": "^1.8.0", | ||
"jiti": "^1.19.3", | ||
"listhen": "^1.3.0", | ||
"prettier": "^3.0.2", | ||
"typescript": "^5.1.6", | ||
"unbuild": "2.0.0-rc.0", | ||
"vitest": "^0.34.2" | ||
}, | ||
"packageManager": "pnpm@8.6.2" | ||
"packageManager": "pnpm@8.6.12" | ||
} |
134
README.md
@@ -28,6 +28,6 @@ # ofetch | ||
// ESM / Typescript | ||
import { ofetch } from 'ofetch' | ||
import { ofetch } from "ofetch"; | ||
// CommonJS | ||
const { ofetch } = require('ofetch') | ||
const { ofetch } = require("ofetch"); | ||
``` | ||
@@ -38,3 +38,3 @@ | ||
We use [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) to detect Node.js | ||
and automatically use [unjs/node-fetch-native](https://github.com/unjs/node-fetch-native). If `globalThis.fetch` is available, will be used instead. To leverage Node.js 17.5.0 experimental native fetch API use [`--experimental-fetch` flag](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch). | ||
and automatically use [unjs/node-fetch-native](https://github.com/unjs/node-fetch-native). If `globalThis.fetch` is available, will be used instead. To leverage Node.js 17.5.0 experimental native fetch API use [`--experimental-fetch` flag](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch). | ||
@@ -52,3 +52,3 @@ ### `keepAlive` support | ||
```js | ||
const { users } = await ofetch('/api/users') | ||
const { users } = await ofetch("/api/users"); | ||
``` | ||
@@ -62,9 +62,9 @@ | ||
// Use JSON.parse | ||
await ofetch('/movie?lang=en', { parseResponse: JSON.parse }) | ||
await ofetch("/movie?lang=en", { parseResponse: JSON.parse }); | ||
// Return text as is | ||
await ofetch('/movie?lang=en', { parseResponse: txt => txt }) | ||
await ofetch("/movie?lang=en", { parseResponse: (txt) => txt }); | ||
// Get the blob version of the response | ||
await ofetch('/api/generate-image', { responseType: 'blob' }) | ||
await ofetch("/api/generate-image", { responseType: "blob" }); | ||
``` | ||
@@ -77,3 +77,6 @@ | ||
```js | ||
const { users } = await ofetch('/api/users', { method: 'POST', body: { some: 'json' } }) | ||
const { users } = await ofetch("/api/users", { | ||
method: "POST", | ||
body: { some: "json" }, | ||
}); | ||
``` | ||
@@ -88,3 +91,3 @@ | ||
```ts | ||
await ofetch('http://google.com/404') | ||
await ofetch("http://google.com/404"); | ||
// FetchError: 404 Not Found (http://google.com/404) | ||
@@ -97,3 +100,3 @@ // at async main (/project/playground.ts:4:3) | ||
```ts | ||
await ofetch('/url').catch(err => err.data) | ||
await ofetch("/url").catch((err) => err.data); | ||
``` | ||
@@ -104,3 +107,3 @@ | ||
```ts | ||
await ofetch('/url', { ignoreResponseError: true }) | ||
await ofetch("/url", { ignoreResponseError: true }); | ||
``` | ||
@@ -110,10 +113,38 @@ | ||
`ofetch` Automatically retries the request if an error happens. Default is `1` (except for `POST`, `PUT`, `PATCH` and `DELETE` methods that is `0`) | ||
`ofetch` Automatically retries the request if an error happens and if response status code is included in `retryStatusCodes` list: | ||
**Retry status codes:** | ||
- `408` - Request Timeout | ||
- `409` - Conflict | ||
- `425` - Too Early | ||
- `429` - Too Many Requests | ||
- `500` - Internal Server Error | ||
- `502` - Bad Gateway | ||
- `503` - Service Unavailable | ||
- `504` - Gateway Timeout | ||
You can specifcy amount of retry and delay between them using `retry` and `retryDelay` options. | ||
Default for `retry` is `1` retry, except for `POST`, `PUT`, `PATCH` and `DELETE` methods where `ofetch` does not retry. | ||
Default for `retryDelay` is `0` ms. | ||
```ts | ||
await ofetch('http://google.com/404', { | ||
retry: 3 | ||
}) | ||
await ofetch("http://google.com/404", { | ||
retry: 3, | ||
retryDelay: 500, // ms | ||
}); | ||
``` | ||
## ✔️ Timeout | ||
You can specify `timeout` in milliseconds to automatically abort request after a timeout (default is disabled). | ||
```ts | ||
await ofetch("http://google.com/404", { | ||
timeout: 3000, // Timeout after 3 seconds | ||
}); | ||
``` | ||
## ✔️ Type Friendly | ||
@@ -124,3 +155,3 @@ | ||
```ts | ||
const article = await ofetch<Article>(`/api/article/${id}`) | ||
const article = await ofetch<Article>(`/api/article/${id}`); | ||
// Auto complete working with article.id | ||
@@ -134,3 +165,3 @@ ``` | ||
```js | ||
await ofetch('/config', { baseURL }) | ||
await ofetch("/config", { baseURL }); | ||
``` | ||
@@ -143,3 +174,3 @@ | ||
```js | ||
await ofetch('/movie?lang=en', { query: { id: 123 } }) | ||
await ofetch("/movie?lang=en", { query: { id: 123 } }); | ||
``` | ||
@@ -158,12 +189,12 @@ | ||
```js | ||
await ofetch('/api', { | ||
await ofetch("/api", { | ||
async onRequest({ request, options }) { | ||
// Log request | ||
console.log('[fetch request]', request, options) | ||
console.log("[fetch request]", request, options); | ||
// Add `?t=1640125211170` to query search params | ||
options.query = options.query || {} | ||
options.query.t = new Date() | ||
} | ||
}) | ||
options.query = options.query || {}; | ||
options.query.t = new Date(); | ||
}, | ||
}); | ||
``` | ||
@@ -176,11 +207,10 @@ | ||
```js | ||
await ofetch('/api', { | ||
await ofetch("/api", { | ||
async onRequestError({ request, options, error }) { | ||
// Log error | ||
console.log('[fetch request error]', request, error) | ||
} | ||
}) | ||
console.log("[fetch request error]", request, error); | ||
}, | ||
}); | ||
``` | ||
### `onResponse({ request, options, response })` | ||
@@ -191,8 +221,8 @@ | ||
```js | ||
await ofetch('/api', { | ||
await ofetch("/api", { | ||
async onResponse({ request, response, options }) { | ||
// Log response | ||
console.log('[fetch response]', request, response.status, response.body) | ||
} | ||
}) | ||
console.log("[fetch response]", request, response.status, response.body); | ||
}, | ||
}); | ||
``` | ||
@@ -205,8 +235,13 @@ | ||
```js | ||
await ofetch('/api', { | ||
await ofetch("/api", { | ||
async onResponseError({ request, response, options }) { | ||
// Log error | ||
console.log('[fetch response error]', request, response.status, response.body) | ||
} | ||
}) | ||
console.log( | ||
"[fetch response error]", | ||
request, | ||
response.status, | ||
response.body | ||
); | ||
}, | ||
}); | ||
``` | ||
@@ -221,5 +256,5 @@ | ||
```js | ||
const apiFetch = ofetch.create({ baseURL: '/api' }) | ||
const apiFetch = ofetch.create({ baseURL: "/api" }); | ||
apiFetch('/test') // Same as ofetch('/test', { baseURL: '/api' }) | ||
apiFetch("/test"); // Same as ofetch('/test', { baseURL: '/api' }) | ||
``` | ||
@@ -232,8 +267,8 @@ | ||
```js | ||
await ofetch('/movies', { | ||
await ofetch("/movies", { | ||
headers: { | ||
Accept: 'application/json', | ||
'Cache-Control': 'no-cache' | ||
} | ||
}) | ||
Accept: "application/json", | ||
"Cache-Control": "no-cache", | ||
}, | ||
}); | ||
``` | ||
@@ -248,5 +283,5 @@ | ||
await ofetch('/api', { | ||
agent: new HttpsProxyAgent('http://example.com') | ||
}) | ||
await ofetch("/api", { | ||
agent: new HttpsProxyAgent("http://example.com"), | ||
}); | ||
``` | ||
@@ -259,3 +294,3 @@ | ||
```js | ||
const response = await ofetch.raw('/sushi') | ||
const response = await ofetch.raw("/sushi"); | ||
@@ -272,3 +307,3 @@ // response._data | ||
```js | ||
const json = await ofetch.native('/sushi').then(r => r.json()) | ||
const json = await ofetch.native("/sushi").then((r) => r.json()); | ||
``` | ||
@@ -306,2 +341,3 @@ | ||
<!-- Badges --> | ||
[npm-version-src]: https://img.shields.io/npm/v/ofetch?style=flat&colorA=18181B&colorB=F0DB4F | ||
@@ -308,0 +344,0 @@ [npm-version-href]: https://npmjs.com/package/ofetch |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48542
19
795
333
11
Updateddestr@^2.0.1
Updatednode-fetch-native@^1.3.0
Updatedufo@^1.2.0