Skip to content

Client options ​

The examples in these docs use a client like this one:

js
import { WasmClient } from 'rust-fetch';

const client = new WasmClient({ baseUrl: 'https://api.example.com/v1' });
ts
const client = new WasmClient({
  baseUrl: 'https://api.example.com/v1', // relative request URLs are appended to this
  headers: { Authorization: 'Bearer …' }, // sent with every request
  timeoutMs: 10_000,                      // per attempt; 0 disables. Default: 30000
  retries: 3,                             // or a RetryOptions object, see Retries
  redirect: 'follow',                     // or 'manual' / 'error', see Redirects. Default: 'follow'
  maxRedirects: 20,                       // Node.js; browsers apply their own limit. Default: 20
  proxy: 'env',                           // Node.js: HTTPS_PROXY / HTTP_PROXY / NO_PROXY, or a proxy URL; see Proxies
  allowAbsoluteUrls: false,               // reject absolute URLs instead of letting them replace baseUrl; see Security. Default: true
});

Every client option:

OptionDefaultDescription
baseUrlnoneRelative request URLs resolve under it; see Making requests
headersnoneSent with every request; a request header with the same name wins
timeoutMs30000Per attempt, until the last byte of the body; 0 disables it. See Timeouts
retriesno retriesA count or RetryOptions; see Retries
retryBudgetno budgettrue or { ratio, minPerSecond, windowMs }: limits retries across the client; see Retries
redirect, maxRedirects'follow', 20See Redirects
sensitiveHeadersnoneYour own credential headers (e.g. X-Api-Key), dropped on cross-origin redirects like Authorization (Node.js); see Security
allowInsecureRedirectstruefalse rejects a redirect from https: to http: (Node.js); see Security
allowAbsoluteUrlstruefalse rejects absolute URLs; see Absolute URLs
allowPrivateNetworktruefalse blocks loopback, private, link-local and metadata addresses (Node.js); see SSRF
maxResponseBytesno limitLargest response body, after decompression; see Size limits
maxPendingRequestsno limitRefuse requests beyond this many unfinished ones; see Load shedding
maxLineBytes, maxEventBytesno limitLongest NDJSON or SSE line, and largest SSE event; see Limiting line length
proxynoneNode.js; see Proxies
credentials, mode, cache, keepalive, priority, referrer, referrerPolicy, integrityas in fetch()Browsers; see Browser request options

Most of them can also be set per request; see Request options.

Proxies ​

In Node.js, requests can go through an HTTP or HTTPS proxy: HTTPS requests through a CONNECT tunnel, so the proxy never sees their contents, and plain HTTP requests as absolute-URL requests.

js
new WasmClient({ proxy: 'env' });                              // HTTPS_PROXY, HTTP_PROXY and NO_PROXY (or lower case)
new WasmClient({ proxy: 'http://user:pass@proxy.internal:3128' }); // every request; credentials as Proxy-Authorization
  • 'env' reads the variables on each request: HTTPS_PROXY for https: URLs, HTTP_PROXY for http: ones, and skips hosts in NO_PROXY. A NO_PROXY entry is *, a host name (which also covers its subdomains; a leading . or *. is optional) or an IP address (matched exactly), each with an optional :port.
  • NODE_USE_ENV_PROXY=1, which Node.js uses for its own fetch(), turns on 'env' for clients without a proxy option.
  • Proxied requests use the node:http transport, with a pool of connections per proxy. This includes streamed responses ('stream', 'ndjson') and events().
  • Browsers use the system's proxy settings and ignore this option.

Invalid options throw an InvalidInputError right away, from the constructor.

Released under the MIT or Apache-2.0 license.