Declare a client
Declare a client as an Effect service with Fetch.Service.
Every call to one backend shares a few things: where the backend lives, the
headers each request needs, the shape of its errors and the plugins each
call goes through. A client holds them, so you declare them once and every
call picks them up. This page shows how to declare a client with
Fetch.Service, and how to use it anywhere in your app.
Declare the client
Fetch.Service declares a client as an Effect service (a value your
program asks for by its type, and that a layer provides when it runs). It
takes an id and a config, and gives you one class:
import * as from "effect/Effect";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
const = .({ : ., : . });
export class extends .<>()("Api", {
: "https://api.example.com",
}) {}
// Anywhere in your app
export const = .(function* () {
const = yield* ;
return yield* .("/todos/:id", { : { : 1 }, : });
});The config takes more than a baseUrl: default headers, error schemas and
plugins. Client configuration lists them all.
What the class gives you
The class gives you three things:
Apiis the service.yield* Apigives you the client, typed from the config.Api.layerbuilds the client. It needs anHttpClient, the Effect service that sends the requests, plus whatever thebaseUrlEffect and the plugins need. Provide the client shows how to give it those.Api.configis a frozen copy of the config you passed. You can read it when the module loads, before any layer is built, for example to reuse thedefaultErrorandstatusErrorsschemas. Itsheadersare an EffectHeadersvalue, with the names in lowercase.
Api.layer is a single value, and Effect shares a layer by value. Wherever
it appears in one layer graph, it's built once, and every layer that
depends on it gets the same client. Each Effect.provide builds it again,
though, so provide it once, where the program runs.
To check whether a value is a class made by Fetch.Service, use
Fetch.isService(value). It returns true for such a class, or one that
extends it, and false for anything else, including a key you wrote with
Context.Service.
Type the client
To type a client, for example as a function parameter, use
Effect.Success<typeof Api>. That's the type yield* Api gives you, with
the plugins, the defaultError and the statusErrors from your config:
import * as from "effect/Effect";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/plugins";
const = .({ : ., : . });
export class extends .<>()("Api", {
: "https://api.example.com",
: .({ : . }),
: [()],
}) {}
export const = (: .<typeof >) =>
.("/todos", { : .() });Fetch.Client is a shorter name for one case: a client with no plugins, no
defaultError and no statusErrors. The Api in the first example has
only a baseUrl, so its client is a Fetch.Client, and the samples in
these docs use declare const api: Fetch.Client for that reason. Adding any
of the three changes the client's type, because plugins add errors and
per-call options, defaultError types the error body, and statusErrors
add errors of your own. A client with any of them no longer fits
Fetch.Client, so use Effect.Success<typeof Api>.
Call from anywhere
A call doesn't need anything else from you to run. The client already holds
the HttpClient, the base URL and the services its plugins need, so you can
run a call from a service, a React Query function or a plain
Effect.runPromise.
The one exception is schemas. A schema can need a service of its own to decode or encode, and a call needs the services of every schema it uses:
- the call's own
input,output,errorandstatusErrors, or the endpoint's, itsqueryschema included; - the client's
defaultErrorandstatusErrors.
Most schemas need no services, so most calls need nothing at all.
Give each client its own id
The id you pass, "Api" in the first example, is the service's identity when the program
runs, as with Context.Service. Two classes with the same id are the same
service, and a layer built for one serves the other. So give each client
its own id.
What's next
- Client configuration: look up the options the config takes.
- Provide the client: give
Api.layertheHttpClientit needs, and run your program. - Make a call: call the API with the client's methods.
- Fetch.Service or Fetch.make: find out when to build a client without the class.