Platform notes
Set up Effetch for browsers and React Native.
Effetch runs the same code on every platform, with no platform-specific
imports or builds. Requests go through Effect's HttpClient, so what a
platform needs mostly comes from there. Two platforms need some setup:
browsers, for calls to other origins, and React Native. This page covers
both. For the full list of runtimes, and which of them CI tests, see
Compatibility.
Browsers
In a browser, a relative path in a client without a baseUrl is resolved
against the page's URL. Calls to another origin need the setup in this
section.
Turn off tracing headers
Effect's HttpClient adds traceparent and b3 tracing headers to every
request. A cross-origin call to an API that doesn't allow those headers then
fails the browser's CORS preflight. To stop sending them, turn propagation
off in the layer that builds the client:
import * as from "effect/http/FetchHttpClient";
import * as from "effect/http/HttpClient";
import * as from "effect/Layer";
import { } from "@monarcode/effetch";
export class extends .<>()("Api", {
: "https://api.example.com",
}) {}
export const = ..(
.(.(.)(false)),
.(.),
);The client keeps what its layer was given, so the setting applies to all of its calls.
Send cookies
By default, fetch sends cookies only to the page's own origin. To send
them to an API on another origin, set credentials: "include" in
FetchHttpClient.RequestInit, the default options for every fetch the
client makes:
import * as from "effect/http/FetchHttpClient";
import * as from "effect/Layer";
import { } from "@monarcode/effetch";
export class extends .<>()("Api", {
: "https://api.example.com",
}) {}
export const = ..(
.(
.(.)({ : "include" }),
),
.(.),
);The API has to allow it too: its CORS response needs
Access-Control-Allow-Credentials: true, and an
Access-Control-Allow-Origin that names your page's origin rather than *.
RequestInit can set other fetch options the same way, such as cache
or redirect. The call still sets the method, the body and the abort
signal, and its headers win over any in RequestInit. A RequestInit
provided around one call replaces the client's for that call.
React Native
React Native differs from the other platforms in the following ways:
URL. Effetch joins URLs as strings and never callsnew URL()itself. Effect'sHttpClientdoes useURL,searchParamsincluded, when it sends each request. An endpoint'surlhelper uses it too. Expo provides a standardURL. In a React Native app without Expo, install a polyfill such asreact-native-url-polyfill.- Package
exports. Effetch and Effect use packageexports, as ineffect/http/HttpClientand@monarcode/effetch/plugins. Metro resolves them by default from React Native 0.79 (Expo SDK 53). On older versions, setresolver.unstable_enablePackageExports = trueinmetro.config.js. - Streams.
FetchHttpClient.layersends requests with the globalfetch, and React Native'sfetchdoesn't stream response bodies. Soapi.sse,api.ndjsonand stream endpoints fail with aDecodeErrorthere. A stream also needsTextDecoder, and fails with aDecodeErroron a runtime without it. In Expo, use thefetchfromexpo/fetch, which streams (see the sample after this list). - File uploads. To upload a file a picker gave you, send its
{ uri, name, type }in aFetch.UploadFilefield (see Upload files). React Native'sFormDatareads the file from disk by itsuri, and can't send aBlob. - Console logging. Metro's terminal drops
console.groupCollapsedand everything inside it, soconsoleLoggerdoesn't collapse its groups in React Native (see Log to the console).
To stream in Expo, provide the fetch from expo/fetch as
FetchHttpClient.Fetch, in the layer that builds the client:
import * as from "effect/http/FetchHttpClient";
import * as from "effect/Layer";
import { } from "@monarcode/effetch";
// In your app: import { fetch as expoFetch } from "expo/fetch";
declare const : unknown;
export class extends .<>()("Api", {
: "https://api.example.com",
}) {}
export const = ..(
.(
.(.)( as typeof .),
),
.(.),
);The cast gives expo/fetch's function the type of the global fetch,
which FetchHttpClient.Fetch expects.
What's next
- Compatibility: check the Effect, runtime and TypeScript versions Effetch supports.
- Upload files: send a file from a picker in a typed multipart form.
- Streaming responses: read Server-Sent Events and
newline-delimited JSON as a
Stream.