Fetch.Service or Fetch.make
Choose between declaring a client as a service and building one with Fetch.make.
Most apps declare each client with Fetch.Service, and never need anything
else. Underneath it is Fetch.make, a function that builds a client
directly. It's the better fit for a script, for a config that only an
Effect can produce, or for a client made while the program runs. This page
shows how the two relate, what you give up with Fetch.make, and when to
use each.
How they relate
Fetch.make takes the same config as Fetch.Service and returns an Effect
that builds the client. Fetch.Service calls Fetch.make in its layer, and
adds the pieces an app needs around it: a service key, a layer and the
config to read back. Both give you the same client, with the same calls and
the same types.
What you give up with Fetch.make
With Fetch.make, you write by hand what the class gives you:
- No service key. To share the client as a service, you declare one
with
Context.Service, as in Fetch.make with a config from an Effect. - No layer. You build one with
Layer.effect. - No
Api.config. There's no frozen config to spread into a derived client (see Derive a client). - No
Fetch.isService. It returnsfalsefor a key you wrote withContext.Service.
Which one to use
Use the class by default, and use Fetch.make when the class doesn't fit.
The following table matches common situations to each:
| Situation | Use |
|---|---|
| An app, with the client shared as a service | Fetch.Service |
A config known when the module loads (a baseUrl Effect such as Config is fine) | Fetch.Service |
Endpoints shared as a service with Fetch.Endpoints | Fetch.Service |
| A script, a one-off program, or a quick test | Fetch.make |
| A config that comes from an Effect, such as headers read from a service | Fetch.make |
| A client made inside your own service's layer, or one per tenant at runtime | Fetch.make |
Fetch.Endpoints takes the client's service key, so it works with any key,
including one you wrote by hand. Fetch.Service is the shorter way there,
since the class is already that key.
Fetch.make in a script
In a script, call Fetch.make inline and provide an HttpClient where you
run it:
import * as from "effect/Effect";
import * as from "effect/http/FetchHttpClient";
import { } from "@monarcode/effetch";
const = .(function* () {
const = yield* .({ : "https://api.example.com" });
return yield* .("/health");
});
await .(.(.(.)));Fetch.make with a config from an Effect
When the config itself comes from an Effect, such as a header read from
another service, call Fetch.make inside that Effect, then declare the
service and its layer by hand:
import * as from "effect/Context";
import * as from "effect/Effect";
import * as from "effect/Layer";
import { } from "@monarcode/effetch";
class extends .<, { readonly : string }>()(
"Tenant",
) {}
const = .(function* () {
const = yield* ;
return yield* .({
: "https://api.example.com",
: { "x-tenant": . },
});
});
export class extends .<, .<typeof >>()(
"Api",
) {}
export const = .()();ApiLive needs a Tenant and an HttpClient, and Api works as a client
key for Fetch.Endpoints.
What's next
- Declare a client: declare a client with
Fetch.Service. - Client configuration: look up the options both take.
- Share endpoints as a service: bind endpoints
to a client's service key with
Fetch.Endpoints.