@0xbeejs/fetch
Provides an interface for fetching resources, base on Fetch API.
Fetures
- Supports Node.js and browsers
- Supports specifying a timeout.
- Supports specifying a proxy.
Installation
$ yarn add @0xbeejs/fetch
Usage
fetch
import fetch from "@0xbeejs/fetch";
function fetchWithTimeout() {
const timeout = 15000; // 15s
return fetch("http://www.google.com", {
// optional
proxyUrl: "http://127.0.0.1:7890",
}, timeout)
}
const rsp = await fetchWithTimeout()
console.log(await rsp.text())
faithfulRequest
Execute a specific asynchronous action until timeout, supporting automatic retries, specifying the number of retries, and retry conditions.
import fetch from "@0xbeejs/fetch";
function fetchWithTimeout() {
const timeout = 15000; // 15s
return fetch("http://www.google.com", {
// optional
proxyUrl: "http://127.0.0.1:7890",
}, timeout)
}
const resp = await faithfulRequest({
req: () => fetchWithTimeout(),
id: "get_google_homepage",
// Optional. The assertion is true, and it will continue retrying until the maximum number of attempts is reached.
maxRetries: -1, // -1: no limit,
// Optional
assert: (ex) => ex.name === "timeout" // 'true' will retry util reach the 'maxRetries'
// Optional, how long to wait before starting the next request
})