Getting started
Declare a client, make a typed call and run it, from an empty file to a working program.
In this tutorial, you write a complete program that fetches a todo from an API, typed and checked by a schema. Along the way, you meet the four ideas the rest of Effetch builds on.
Before you begin
Install Effetch and Effect, and check that your project meets the requirements. For the commands, see Installation.
Write your first client
The following program fetches a todo from JSONPlaceholder, a free fake API. It's complete, so you can run it as it is:
import * as from "effect/Effect";
import * as from "effect/http/FetchHttpClient";
import * as from "effect/Layer";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
// 1. What a todo looks like
const = .({
: .,
: .,
: .,
});
// 2. The client, declared once for the whole app
class extends .<>()("Api", {
: "https://jsonplaceholder.typicode.com",
}) {}
// 3. A call
const = .(function* () {
const = yield* ;
const = yield* .("/todos/:id", {
: { : 1 },
: ,
});
.(.);
});
// 4. Run it, with the client and the HttpClient that sends the requests
const = ..(.(.));
await .(.(.()));What each part does
The numbered comments in the program mark its four parts:
Tododescribes the response. Effetch decodes the body with it, sotodois typed, and checked when it arrives. If the API sends something else, the call fails with aDecodeErrorinstead of handing you bad data.Apiis your client.Fetch.Servicedeclares it as an Effect service, with everything its calls have in common: here the base URL, later the headers, error schemas and plugins.api.getmakes the call.yield* Apigives you the client, and the:idin the path is filled in fromparams.Api.layerbuilds the client. It needs anHttpClientto send the requests.FetchHttpClient.layeruses the globalfetch. Node's works too, and in tests you'd use a fake one (see Test with a fake backend).
How the pieces fit
Everything in Effetch builds on four ideas, from the bottom up:
- An
HttpClientsends requests. It's Effect's, and you choose it when you run your program: the globalfetch, Node's, or a fake in tests. - A client is one backend: its base URL, default headers, error schemas
and plugins. You declare it once, with
Fetch.Service. - A call is one request: a method such as
api.get, or an endpoint you saved. It returns an Effect, typed by your schemas, that fails with typed errors. - Plugins wrap every call a client makes, for auth, retries, timeouts and logging.
The sidebar follows the same order: clients, calls and errors, then plugins, including your own, and endpoints. After those come streams, pagination, how to structure an app and testing.
The examples in these docs
To stay short, many examples start from declare const api: Fetch.Client: a
client without plugins, like the one yield* Api gives you when Api has
none. In your app, get the client with yield* Api. To type a client in a
function signature, use Effect.Success<typeof Api>, which includes your
plugins and error schemas.
Some examples also bind an endpoint inline, with api.bind, to stay short.
In your app, bind endpoints once, with Fetch.Endpoints (see
Share endpoints as a service).
Every TypeScript example in these docs is compiled with the package, so they all match the API.
What's next
- Declare a client: see what the client class gives you and how to type it.
- Client configuration: add headers, error schemas and plugins to your client.
- Methods: make calls with each HTTP method.
- Call errors: see what can go wrong in a call and how to catch it.