Skip to Content
Clients

Client configuration

Look up an option a client accepts, its default, and what it changes for each call.

A client's config holds what every call to one backend has in common. You pass it to Fetch.Service, or to Fetch.make, which takes the same options (see Fetch.Service or Fetch.make). This page lists each option with its default, then the rules that come with each one.

Example

Here's a client that uses every option:

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

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

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

class  extends .<>()(
  "SessionExpired",
  { : . },
) {}

const : { ?: string } = {};

export class  extends .<>()("Api", {
  : .("API_URL"),
  : { "x-client": "web" },
  : ,
  : { 401:  },
  : [
    (),
    (),
    ({ : "10 seconds" }),
    ({ : () => . }),
  ],
}) {}

Options

Every option is optional. The following table lists what each one is for, and what a client without it does:

OptionWhat it's forDefault
baseUrlWhere the API lives. A string, or an Effect such as a Config. See The base URL.None: each path is sent as you wrote it.
headersHeaders sent with every request. See Headers.None.
defaultErrorA schema for the body of the API's error responses, decoded onto HttpError.body.Schema.Unknown: HttpError.body is the parsed JSON.
statusErrorsYour own errors for error responses, by status, for every call.None: an error status fails with HttpError.
pluginsBehavior every call goes through, such as auth, retries and logging.[]: no plugins.

The base URL

Effetch puts the baseUrl in front of every path that isn't a full http:// or https:// URL. It joins the two with exactly one /, so a base path is kept: with https://api.example.com/v1, both "/todos" and "todos" go to https://api.example.com/v1/todos.

A baseUrl can be a string, or an Effect that's run one time, when the client is built. A Config such as Config.String("API_URL") works, and its failure is the error of Api.layer.

The baseUrl can't contain a query string or a fragment. Pass query parameters with each call's query option instead (see Build a query string).

Without a baseUrl, a browser resolves a relative path against the page's URL. Other runtimes have no page to resolve it against, so there a call with a relative path dies.

Headers

The client's headers are sent with every request, and a header with the same name on a call, or on an endpoint, wins over the client's.

You can't set content-type or content-length here, and Effetch drops them if you do. Each body sets its own, and a client-wide value would be wrong for most of them (see Send a body).

An accept header here replaces the one Effetch picks for a call's output, such as application/json (see Read the response). Set one only when every call expects the same type.

Error schemas

defaultError decodes the body of every error response onto HttpError.body. A call's or an endpoint's error option replaces it for that call (see Catch errors).

statusErrors apply to every call. An endpoint and a call can have their own as well. For a given status, the call's map, or on an endpoint call the endpoint's, is tried before the client's (see Errors by status).

Plugins

Plugins run outermost first: plugins[0] wraps all the others (see How plugins nest). Two rules keep the list working:

  • Each plugin's name must be unique. Calls turn a plugin off or change its options by its name, so two plugins with one name make building the client die.
  • Pass the list inline, in the config. Annotating it as Plugin.Any[] erases the plugins' types, and with them the errors and per-call options they add.

When a config is wrong

Some mistakes in a config are bugs in your code, not failures to handle at runtime, so they're defects: the Effect dies, and you don't catch it in normal code (see Logging and defects).

Building the client dies when Api.layer is built or Fetch.make runs, not when the class is declared, if:

  • the baseUrl contains a ? or a #;
  • two plugins have the same name.

A call dies when it runs if its path is relative, the client has no baseUrl, and the program isn't running in a browser.

What's next

On this page