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.
Layers
layerFetch
Signature
declare const layerFetch: Layer.Layer<HttpClient.HttpClient>;layerXMLHttpRequest
Layer that provides an HttpClient implementation backed by the browser XMLHttpRequest API.
Signature
declare const layerXMLHttpRequest: Layer.Layer<HttpClient.HttpClient>;Models
XHRResponseType type
Allowed response body modes for the browser XHR HTTP client.
Signature
type XHRResponseType = "arraybuffer" | "text";Providing Services
withXHRArrayBuffer
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
CurrentXHRResponseType
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
XHRResponseTypefor the allowed response body modeswithXHRArrayBufferfor scoping XHR response handling toArrayBuffer
Signature
declare const CurrentXHRResponseType: Context.Reference<XHRResponseType>;Fetch
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
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);
}XMLHttpRequest
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);
}
Layer that provides an
HttpClientimplementation backed by the configuredFetchfunction.When to use
Use when an Effect program should execute
HttpClientrequests through the platformfetchimplementation, especially in browser, edge, or Node.js runtimes withglobalThis.fetch.Details
The layer uses the current
Fetchreference and optionalRequestInitservice for each request. Request-specific method, headers, body, and abort signal are supplied by the client and override matchingRequestInitfields.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 anycontent-lengthheader is removed before callingfetch.See
Fetchfor supplying the fetch implementation used by this layerRequestInitfor defaultRequestInitoptions applied before request-specific fields