Provide the client
Give a program the HttpClient that the client's layer needs.
This page shows how to give Api.layer the HttpClient it needs, so you
can run a program that uses your client.
Provide an HttpClient where you run the program
Api.layer needs an HttpClient. Provide one where you run your program,
usually next to the other layers of your app:
import * as from "effect/Effect";
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 = ..(.(.));
export const = .(function* () {
const = yield* ;
return yield* .("/health");
});
export const = .(.());Keeping the HttpClient out of Api.layer is what lets tests swap in a
fake one.
Build the HttpClient into the layer
If you'd rather have Api.layer provide the HttpClient too, override the
layer in the class:
import * as from "effect/http/FetchHttpClient";
import * as from "effect/Layer";
import { } from "@monarcode/effetch";
export class extends .<>()("Api", {
: "https://api.example.com",
}) {
static override readonly = super..(
.(.),
);
}An override can need less than the original layer, but not more, and can't add an error.
The override builds the real HttpClient into Api.layer, so a test can no
longer swap in a fake one: FetchTest.layer provided under it is ignored, and
requests go to the network. If you test with Api.layer, keep it as it is and
export a separate ApiLive, as in the first example.
What's next
- Test with a fake backend: provide a fake
HttpClientinstead of the real one. - Derive a client: make a second client from the first one's config.
- Methods: make calls with the client.