# Structure an app (https://effetch.monarcode.dev/docs/structuring-an-app)

Your first few calls can live anywhere. As the app grows, you have to
decide where the client goes, where the endpoints go, and how the rest of
the code reaches them. This page shows three layouts that work, from a
small app to one that talks to several backends, so you can pick the one
that fits.

## What every layout has in common

Whichever layout you pick, two things stay the same:

- **The client is declared once,** as a service, with its base URL, error
  body and plugins.
- **Endpoints are bound to it once,** in a layer, when the app starts. Code
  that makes calls gets the bound endpoints from a service, and never binds
  them itself.

The rest is up to you. These layouts are patterns, not rules, and Effetch
works the same way whichever you use.

## A small app

In 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:

```ts
import * as Effect from "effect/Effect";
import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";

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

const User = Schema.Struct({ id: Schema.Finite, name: Schema.String });
const NewUser = Schema.Struct({ name: Schema.String });
const Credentials = Schema.Struct({
  user: Schema.String,
  password: Schema.String,
});
const Session = Schema.Struct({ token: Schema.String });
const Todo = Schema.Struct({ id: Schema.Finite, title: Schema.String });

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

// users.ts
export class Users extends Fetch.Endpoints<Users>()("Users", Api, {
  get: Fetch.endpoint("GET /users/:id", { output: User }),
  create: Fetch.endpoint("POST /users", { input: NewUser, output: User }),
  login: [
    Fetch.endpoint("POST /auth/login", {
      input: Credentials,
      output: Session,
    }),
    { plugins: { bearer: false } },
  ],
}) {}

// todos.ts
export class Todos extends Fetch.Endpoints<Todos>()("Todos", Api, {
  list: Fetch.endpoint("GET /todos", { output: Schema.Array(Todo) }),
}) {}

// layer.ts
export const AppLayer = Layer.mergeAll(Users.layer, Todos.layer).pipe(
  Layer.provideMerge(Api.layer),
  Layer.provide(FetchHttpClient.layer),
);

// anywhere
export const program = Effect.gen(function* () {
  const users = yield* Users;
  const api = yield* Api; // for one-off calls
  const user = yield* users.get({ params: { id: 1 } });
  yield* api.post("/users/:id/ping", { params: { id: user.id } });
  return user;
});
```

`login` turns off the `bearer` plugin, so logging in sends no token. That
override is checked against `Api`'s plugins, and every endpoint's errors
are typed with `Api`'s `defaultError` and `statusErrors`.
`Layer.provideMerge` keeps `Api` itself in the layer, for the one-off calls
that aren't worth an endpoint.

### Endpoints made with api.endpoint

You may prefer to define each endpoint on the client with `api.endpoint`,
in one step. Then write each area as a function of the client, typed
`Effect.Success<typeof Api>`, so overrides and errors are still checked
against `Api`'s plugins and `defaultError`.

That function makes new endpoints each time it runs, so don't call it per
request. Run it once, in a layer, and share what it returns as a service:

```ts
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";

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

const User = Schema.Struct({ id: Schema.Finite, name: Schema.String });
const Credentials = Schema.Struct({
  user: Schema.String,
  password: Schema.String,
});
const Session = Schema.Struct({ token: Schema.String });

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

// The endpoints, made from the client
export const users = (api: Effect.Success<typeof Api>) => ({
  get: api.endpoint("GET /users/:id", { output: User }),
  login: api.endpoint("POST /auth/login", {
    input: Credentials,
    output: Session,
    plugins: { bearer: false },
  }),
});

// Made once, when the layer is built, and shared as a service
const makeUsers = Api.useSync(users);

export class Users extends Context.Service<
  Users,
  Effect.Success<typeof makeUsers>
>()("Users") {}

export const UsersLayer = Layer.effect(Users)(makeUsers);
```

`Api.useSync(users)` is an Effect that gets the client and calls `users`
with it. `Layer.effect` runs that Effect once, when the layer is built.
Code then gets the endpoints with `yield* Users`, as with a
`Fetch.Endpoints` class. `Fetch.Endpoints` writes these three steps for
you, so it's the shorter choice when your endpoints are defined with
`Fetch.endpoint`.

## A feature-sliced app

A larger app is often split into feature modules, each with its own
schemas. There, the client becomes shared infrastructure, and it imports
nothing from the modules. Each module defines its endpoints next to its
schemas, with no client, and binds them to the client in its data layer.

[`examples/app`](https://github.com/monarcode/effetch/tree/main/examples/app)
is a complete app in this layout, with two modules and two backends. Here
are its files:

```text
examples/app/
├── infrastructure/http/
│   ├── app-api-client.ts     the main backend's client
│   ├── wallet-api-client.ts  the wallet backend's client
│   └── plugins.ts            plugins both clients use
├── modules/
│   ├── todos/
│   │   ├── contract/todos.ts schemas, and endpoints from Fetch.endpoint
│   │   ├── data/todo.api.ts  the endpoints, bound to AppApiClient
│   │   ├── todo.layer.ts     the module's layers, merged
│   │   └── public.ts         what other code may import
│   └── wallet/
│       ├── application/      workflows that take more than one call
│       ├── contract/
│       ├── data/
│       ├── wallet.layer.ts
│       └── public.ts
├── runtime.ts                modules, over the clients, over HttpClient
└── main.ts                   the program: imports modules only through public.ts
```

> **This layout is a personal preference,** the one Effetch's author uses in
> their own apps, and Effetch doesn't depend on it. Rename, merge or split the
> folders to suit your project. What carries over is that modules import the
> client, never the other way around, and that endpoints are bound once, in a
> layer.

The following code puts the `todos` module's files in one block, each
under a comment with its path. It's shortened from the example, which has
more fields, endpoints and errors:

```ts
import * as Config from "effect/Config";
import * as Effect from "effect/Effect";
import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";

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

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

// modules/todos/contract/todos.ts: schemas and endpoints, no client
const Todo = Schema.Struct({ id: Schema.Finite, title: Schema.String });
const NewTodo = Schema.Struct({ title: Schema.String });

export const listTodos = Fetch.endpoint("GET /todos", {
  output: Schema.Array(Todo),
});
export const getTodo = Fetch.endpoint("GET /todos/:id", { output: Todo });
export const createTodo = Fetch.endpoint("POST /todos", {
  input: NewTodo,
  output: Todo,
});

// modules/todos/data/todo.api.ts: the endpoints, bound, as a service
export class TodoApi extends Fetch.Endpoints<TodoApi>()(
  "TodoApi",
  AppApiClient,
  {
    list: listTodos,
    get: getTodo,
    create: createTodo,
  },
) {}

// modules/todos/todo.layer.ts: every layer the module has
export const TodoModuleLive = Layer.mergeAll(TodoApi.layer);

// runtime.ts: the modules, on top of the client, on top of HttpClient
export const AppLayer = TodoModuleLive.pipe(
  Layer.provideMerge(AppApiClient.layer),
  Layer.provide(FetchHttpClient.layer),
);

// anywhere
export const program = Effect.gen(function* () {
  const todos = yield* TodoApi;
  const todo = yield* todos.create({ body: { title: "Write docs" } });
  return yield* todos.get({ params: { id: todo.id } });
});
```

### Why this layout scales

Each part of the app grows in its own place:

- **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.
  There's one line per operation and every type is 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, as `wallet` has.
- **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.

### api.endpoint in this layout

`api.endpoint` fits this layout too. Make the endpoints in `data/`, and
import only the schemas from `contract/`. Define them with `Fetch.endpoint`
instead when something has to read the routes and schemas without a
client, such as a test's fake backend.

The example app does both. `todos` defines its endpoints with
`Fetch.endpoint` in `contract/`, and binds them with `Fetch.Endpoints` in
`data/`. `wallet` makes its endpoints with its client's `endpoint` method
in `data/`, and shares them as a service the way
[Endpoints made with api.endpoint](https://effetch.monarcode.dev/docs/structuring-an-app#endpoints-made-with-apiendpoint) shows.

## Several backends

When the app talks to more than one backend, declare one client for 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:

```ts
import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";

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

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

// One plugin value, shared by both clients
const auth = bearer({ token: () => readToken() });

export class AppApiClient extends Fetch.Service<AppApiClient>()(
  "AppApiClient",
  {
    baseUrl: "https://api.example.com",
    defaultError: Schema.Struct({ code: Schema.String }),
    plugins: [auth],
  },
) {}

export class WalletApiClient extends Fetch.Service<WalletApiClient>()(
  "WalletApiClient",
  {
    baseUrl: "https://wallet.example.com",
    defaultError: Schema.Struct({ reason: Schema.String }),
    plugins: [auth],
  },
) {}

export const Clients = Layer.mergeAll(
  AppApiClient.layer,
  WalletApiClient.layer,
).pipe(Layer.provide(FetchHttpClient.layer));
```

Keep the following in mind:

- **Plugins are values,** so one `bearer` can serve several clients, as
  `auth` does here.
- **A route doesn't say which backend it's for.** Plugins get the call's
  route as `ctx.route` (see [Write a plugin](https://effetch.monarcode.dev/docs/plugins/writing)). It's
  the method and the path as written, with no base URL, so both backends
  can have a `GET /health`. Only a route written as a full URL keeps its
  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`](https://github.com/monarcode/effetch/blob/main/examples/backends.ts)
does this in one file, and
[`examples/app`](https://github.com/monarcode/effetch/tree/main/examples/app)
across modules.

## What's next

- [Share endpoints as a service](https://effetch.monarcode.dev/docs/endpoints/services): see everything
  a `Fetch.Endpoints` class gives you.
- [Declare a client](https://effetch.monarcode.dev/docs/clients): declare one client per backend, with
  its own base URL, errors and plugins.
- [Test with a fake backend](https://effetch.monarcode.dev/docs/testing): serve your endpoints from a
  fake backend in tests.