Skip to content

BrowserHttpClient

Browser implementations of the Effect HttpClient.

This module exposes HTTP client layers for code that runs in a browser. It re-exports the fetch-based Fetch, RequestInit, and layerFetch APIs for the common case where requests use the platform fetch implementation. It also provides an XMLHttpRequest-backed client, including controls for the XHR response type, an overridable XMLHttpRequest constructor service, and the layerXMLHttpRequest layer.

8 exports Added in v4.0.0 Source

Layers

layerFetch

Added in v4.0.0

Layer that provides an HttpClient implementation backed by the configured Fetch function.

When to use

Use when an Effect program should execute HttpClient requests through the platform fetch implementation, especially in browser, edge, or Node.js runtimes with globalThis.fetch.

Details

The layer uses the current Fetch reference and optional RequestInit service for each request. Request-specific method, headers, body, and abort signal are supplied by the client and override matching RequestInit fields.

Gotchas

Fetch behavior comes from the runtime's implementation, so CORS, cookies, redirects, abort handling, and streaming support can vary by platform. Stream request bodies are sent as Web streams with duplex: "half", and any content-length header is removed before calling fetch.

See

  • Fetch for supplying the fetch implementation used by this layer
  • RequestInit for default RequestInit options applied before request-specific fields

Signature

declare const layerFetch: Layer.Layer<HttpClient.HttpClient>;

Layer that provides an HttpClient implementation backed by the browser XMLHttpRequest API.

Signature

declare const layerXMLHttpRequest: Layer.Layer<HttpClient.HttpClient>;

Models

XHRResponseType type

Added in v4.0.0 Source

Allowed response body modes for the browser XHR HTTP client.

Signature

type XHRResponseType = "arraybuffer" | "text";

Providing Services

Runs an effect with CurrentXHRResponseType set to "arraybuffer" so the XHR HTTP client receives response bodies as ArrayBuffer values.

Signature

declare function withXHRArrayBuffer<A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>;

Services

Context reference for the XMLHttpRequest.responseType used by the browser XHR HTTP client, defaulting to "text".

When to use

Use when you need XHR-backed HTTP requests to receive response bodies as text or raw ArrayBuffer values.

See

Signature

declare const CurrentXHRResponseType: Context.Reference<XHRResponseType>;

Fetch

Added in v4.0.0

Context reference for the fetch implementation used by the fetch-based HTTP client.

Details

Defaults to globalThis.fetch.

Signature

declare const Fetch: Context.Reference<typeof globalThis.fetch>;

RequestInit

Added in v4.0.0

Service that contains default fetch options for the fetch-based HTTP client.

When to use

Use to provide default credentials, cache, redirect, integrity, or other fetch options for outgoing HTTP requests.

Details

Request-specific method, headers, body, and abort signal are supplied by the client when a request is executed.

Signature

declare class RequestInit extends RequestInit_base {
  constructor(_: never);
}

Service tag for the XMLHttpRequest constructor used by the browser XHR HTTP client.

Signature

declare class XMLHttpRequest extends Shape<
  "@effect/platform-browser/BrowserHttpClient/XMLHttpRequest",
  LazyArg<XMLHttpRequest>,
  this
> {
  constructor(_: never);
}