Skip to Content
Clients

Fetch.Service or Fetch.make

Choose between declaring a client as a service and building one with Fetch.make.

This page explains how Fetch.Service and Fetch.make relate, and helps you choose between them.

How they relate

Fetch.make is the function underneath Fetch.Service: it takes the same config and returns an Effect that builds the client. Both give you the same client, with the same calls and the same types.

Which one to use

Use the class by default, and reach for Fetch.make when the class doesn't fit:

SituationUse
An app, with the client shared as a serviceFetch.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.EndpointsFetch.Service
A script, a one-off program, or a quick testFetch.make
A config that comes from an Effect, such as headers read from a serviceFetch.make
A client made inside your own service's layer, or one per tenant at runtimeFetch.make

Fetch.make in a script

In a script, use Fetch.make inline:

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, call Fetch.make inside that Effect and declare the service 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  = .()();

On this page