Skip to Content
Endpoints

Share endpoints as a service

Bind a record of endpoint definitions to a client once, and share them as an Effect service.

Your definitions can't make calls until they're bound to a client, and you want that done once, not in every function that calls them. Fetch.Endpoints binds a record of definitions when its layer is built, and shares the result as an Effect service, so any code in your app can yield* it. This page shows how to declare one, and what the class gives you.

Declare the service

Fetch.Endpoints does for endpoints what Fetch.Service does for the client. It takes the service's id, the client's service and the record of definitions:

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";
import {  } from "@monarcode/effetch/plugins";

declare const : () => <string | null>;

const  = .({ : ., : . });
const  = .({ : . });
const  = .({
  : .,
  : .,
});
const  = .({ : . });

const  = .("GET /users/:id", { :  });
const  = .("GET /users", {
  : .({ : .(.) }),
  : .(),
});
const  = .("POST /users", {
  : ,
  : ,
});
const  = .("POST /auth/login", {
  : ,
  : ,
});

export class  extends .<>()("Api", {
  : "https://api.example.com",
  : [({ : () => () })],
}) {}

export class  extends .<>()("UserApi", , {
  : ,
  : ,
  : ,
  // A [definition, options] pair: logging in sends no token.
  : [, { : { : false } }],
}) {}

export const  = .(function* () {
  const  = yield* ;
  const  = yield* .({ : { : "Ada" } });
  const  = yield* .({ : { : 2 } });
  const  = yield* .({ : { : . } });
  return { ,  };
});

export const  = ..(
  .(.),
  .(.),
);

Most entries are a definition. An entry can also be a [definition, options] pair, with the options api.bind takes: headers, and plugins overrides, which are checked against the client's plugins. Here, login turns off the bearer plugin, so logging in sends no token (see Built-in plugins).

Layer.provideMerge keeps Api in the layer's output, so code can still yield* Api for one-off calls.

What the class gives you

Like Fetch.Service, the class gives you three things:

  • UserApi is the service. yield* UserApi gives the endpoints, each typed with the client's plugins, defaultError and statusErrors.
  • UserApi.layer binds them to the client once, when it's built. It needs the client's service, here Api, and never fails. The client can also be a service you wrote by hand.
  • UserApi.definitions is the record you passed, frozen. You can read it when the module loads, without building anything.

Two things to keep in mind when you declare one:

  • Give each class its own id. The id, "UserApi" here, is the service's identity at run time, as with any Effect service. Two classes with the same id are the same service.
  • Each entry is checked when the class is declared. An entry that isn't a definition or a [definition, options] pair throws then, so the mistake shows up when the module is imported.

How bound endpoints get the client's defaults

Each endpoint is bound to the client, so it picks up what the client holds. A definition without an error schema decodes error bodies with the client's defaultError, and the client's statusErrors are tried after the endpoint's. Headers can come from the definition and from the pair, and the pair's win. The rules for headers, plugin overrides and schemas are in How defaults combine.

What's next

On this page