Skip to Content

Structure an app

Lay out clients and endpoints in a small app, a feature-sliced app and an app with several backends.

This page shows where clients and endpoints can live as your app grows, from a single client to several backends.

A client is declared once, and endpoints are bound to it once, in a layer. Where each piece lives depends on the size of your app. These are patterns, not rules: Effetch works the same way whichever you pick.

A small app

Keep the client in one file, and each area's endpoints in its own file, as a Fetch.Endpoints class. One layer puts them together:

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";

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

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

// users.ts
export class  extends .<>()("Users", , {
  : .("GET /users/:id", { :  }),
  : .("POST /users", { : , :  }),
  : [
    .("POST /auth/login", { :  }),
    { : { : false } },
  ],
}) {}

// todos.ts
export class  extends .<>()("Todos", , {
  : .("GET /todos", { : .() }),
}) {}

// layer.ts
export const  = .(., .).(
  .(.),
  .(.),
);

// anywhere
export const  = .(function* () {
  const  = yield* ;
  const  = yield* ; // for one-off calls
  const  = yield* .({ : { : 1 } });
  yield* .("/users/:id/ping", { : { : . } });
  return ;
});

The plugin override on login is checked against Api's plugins, and every endpoint's errors are typed with Api's defaultError and statusErrors.

Endpoints made with client.endpoint

To make endpoints with client.endpoint instead, write each area as a function of the client, typed Effect.Success<typeof Api>, so overrides and errors are still checked against your plugins and defaultError:

import * as  from "effect/Effect";
import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";
import {  } from "@monarcode/effetch/plugins";

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

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

export const  = (: .<typeof >) => ({
  : .("GET /users/:id", { :  }),
  : .("POST /auth/login", {
    : ,
    : { : false },
  }),
});

A feature-sliced app

When your app is split into feature modules, each with its own schemas, the client becomes shared infrastructure that knows nothing about the modules. Each module defines its endpoints next to its schemas, with no client, and binds them to the client in its data layer:

src/
├── infrastructure/http/
│   └── app-api-client.ts  the client: base URL, error body, plugins
├── modules/
│   ├── todos/
│   │   ├── contract/      schemas, and endpoints defined with Fetch.endpoint
│   │   ├── data/          the endpoints, bound to AppApiClient
│   │   ├── application/   workflows that take more than one call
│   │   └── public.ts
│   └── wallet/
└── runtime.ts             modules, on top of clients, on top of HttpClient

The following code shows each file of the layout, with the todos module:

import * as  from "effect/Config";
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";

// infrastructure/http/app-api-client.ts: how to reach the backend, nothing else
export class  extends .<>()(
  "AppApiClient",
  { : .("APP_API_URL") },
) {}

// modules/todos/contract/todos.ts: schemas and endpoints, no client
const  = .({ : ., : . });
const  = .({ : . });

export const  = .("GET /todos", {
  : .(),
});
export const  = .("GET /todos/:id", { :  });
export const  = .("POST /todos", {
  : ,
  : ,
});

// modules/todos/data/todo.api.ts: the endpoints, bound, as the module's service
export class  extends .<>()(
  "TodoApi",
  ,
  {
    : ,
    : ,
    : ,
  },
) {}

// runtime.ts
export const  = ..(
  .(.),
  .(.),
);

// anywhere
export const  = .(function* () {
  const  = yield* ;
  const  = yield* .({ : { : "Write docs" } });
  return yield* .({ : { : . } });
});

Why this layout scales

This scales well as the app grows:

  • The client file stays the same size, however many endpoints the app has, and it never imports a module: dependencies point from the modules to the infrastructure.
  • A module with 20 endpoints has 20 definitions in its own contract/ folder, beside their schemas, and 20 one-line entries in its Fetch.Endpoints class in data/. When they serve separate concerns, split them into several services, each in its own file.
  • Every module's data service is its endpoints, as TodoApi is here: one line per operation, and every type inferred, so every module's data/ folder looks the same.
  • An operation that isn't one HTTP call (it combines calls, reshapes a response or adds a domain error) is a workflow over those endpoints, in the module's application/ folder.
  • Declare a service's interface by hand only when you need another implementation, such as an offline one or a fake for UI work, and implement it with the endpoints.

client.endpoint in this layout

client.endpoint fits this layout too: make the endpoints in data/, and import only the schemas from contract/. Define them with Fetch.endpoint when something should read the routes and schemas without a client. examples/app is a complete app in this layout, with two modules and two backends: todos declares its definitions with Fetch.Endpoints, and wallet uses client.endpoint.

Several backends

Declare one client per backend, each with its own id, baseUrl, defaultError and plugins. Each module uses the client of the backend it talks to, and one HttpClient layer serves them all.

Keep the following in mind:

  • Plugins are values: one bearer can serve several clients.
  • ctx.route has no host. A plugin that groups by route and is shared by several clients should take the backend's name, so each client gets its own instance.
  • Catch a backend's HttpError next to its calls, where body has that backend's error shape.

examples/backends.ts does this in one file, and examples/app across modules.

What's next

On this page