Skip to Content
Plugins

Built-in plugins

Add a bearer token, retries, a timeout and logging to every call a client makes.

Most APIs need the same things on every call: a token, retries when the network drops, a time limit, a log line. Plugins add that behavior to every call a client makes. You list them in the client's config, and any call can turn one off or adjust it. This page shows the plugins that ship with Effetch and what each one does.

Add plugins to a client

The built-in plugins come from @monarcode/effetch/plugins. List them in the client's plugins option:

import * as  from "effect/Effect";

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

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

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

export const  = ({ : .("token") });

The order in this example is the one to use. To learn why, see Plugin order.

What each plugin does

The following table lists each built-in plugin, what it does, and what a call can pass for it in its plugins option:

PluginWhat it doesPer call
bearer({ token, scheme?, refresh? })Sends Authorization: Bearer <token>. The token is a string, a function of the request (sync or async), or an Effect, read for every request. null, undefined and "" send no header, and an existing Authorization header is kept. With refresh, a 401 gets a new token and the request is sent again, once. See Refresh a token.false
logger({ level? })Logs one entry per call at Debug (or Info): GET <url> 200 (35ms), or failed with the cause, with the call's route as an annotation. Never logs headers or query strings.false
consoleLogger(options?)Prints each call to the console while you develop: a line with the route, status, duration and outcome, opening a group with the full URL, headers, bodies and error. Sensitive headers, query parameters and body keys are redacted. See Log to the console.false, its settings
retry({ times?, schedule?, methods?, statuses?, retryAfter?, budget? })Retries network errors and 408, 429, 500, 502, 503 and 504, 2 times by default, with jittered exponential backoff from 200ms. Only idempotent methods (GET, HEAD, OPTIONS, PUT, DELETE) by default, so a POST is never sent twice. When retries run out, the last response is returned. retryAfter waits as Retry-After asks, and budget limits the whole call. See Retry-After and a time limit.false, { times }, and { budget } with a budget
timeout({ duration })Fails with TimeoutError and aborts the request when the response headers don't arrive in time. Downloading the body isn't covered.false, { duration }

Fail with a typed error from a token

A bearer token function that throws, or whose promise rejects, makes the call die. To fail with a typed error instead, pass an Effect: its error and services become the client's, and the client's layer (or Fetch.make) asks for the services.

Choose a time limit

Time limits apply per attempt or to the whole call:

  • Per attempt: timeout, placed after retry in the plugin list, as in the example on this page. Each attempt gets the full duration, and a timed-out attempt isn't retried.
  • For the whole call: retry({ budget }), every attempt and wait included. For one call, or on a client without retry, wrap the call in Effect.timeout.

To use both together, see Retry-After and a time limit.

What's next

On this page