Skip to Content
Plugins

Plugin recipes

Copy a plugin that signs out on a 401, or one that sets a deadline with its own error.

Some plugins come up in almost every app, and you can copy these two into yours. Each one also shows a different part of the plugin API: hooks that need a service and add an error, and a wrap with an option that calls can override.

Sign out on a 401

When the API answers 401 to a request that carried the session's token, the session is over. This plugin sends the session's token, and on such a 401, signs out and fails with a typed SessionExpired. To renew the token instead, see Refresh a token.

The plugin reads the token from a Session service in onRequest, and signs out in onResponse:

import * as  from "effect/Context";
import * as  from "effect/Data";
import * as  from "effect/Effect";
import * as  from "effect/http/Headers";
import * as  from "effect/http/HttpClientRequest";

import {  } from "@monarcode/effetch";

export class  extends .("SessionExpired")<{}> {}

export class  extends .<
  ,
  {
    readonly : .<string | undefined>;
    readonly : .<void>;
  }
>()("Session") {}

export const  = .({
  : "auth",
  : () =>
    .(function* () {
      const  = yield* ;
      const  = yield* .;
      return  === 
        ? 
        : .(, );
    }),
  : () =>
    .(function* () {
      const  = .(.., "authorization");
      if (. !== 401 || !) {
        return ;
      }
      const  = yield* ;
      yield* .;
      return yield* new ();
    }),
});

// Api.layer needs Session, and calls can fail with SessionExpired too.
export class  extends .<>()("Api", {
  : "https://api.example.com",
  : [],
}) {}

The plugin signs out only when the refused request carried a token, as bearer checks before it refreshes. A request sent without one, such as a login with the wrong password while signed out, gets its 401 back as usual. To keep the plugin off a call entirely, pass plugins: { auth: false }.

Set a deadline with its own error

timeout and retry's budget both fail with TimeoutError. When one kind of slow call should fail with an error of its own, with its own fields, so you can catch it apart from every other timeout, write a wrap that adds that error:

import * as  from "effect/Data";
import * as  from "effect/Effect";
import * as  from "effect/http/HttpClient";

import {  } from "@monarcode/effetch";

export class  extends .("TooSlow")<{
  readonly : string;
}> {}

export const  = .({
  : "deadline",
  : { : 5000 },
  : (, ) =>
    .(, (, ) =>
      .(function* () {
        const {  } = yield* ;
        return yield* .(, {
          : ,
          : () => .(new ({ : . })),
        });
      }),
    ),
});

Like timeout, the deadline covers only the plugins after it in the list and the network, up to the response headers. Reading the body isn't covered. Put it first to cover every retry, or after retry to limit each attempt.

A call can change the deadline, because the plugin has defaults:

import * as  from "effect/Data";
import * as  from "effect/Effect";
import * as  from "effect/http/HttpClient";

import {  } from "@monarcode/effetch";

class  extends .("TooSlow")<{ readonly : string }> {}

const  = .({
  : "deadline",
  : { : 5000 },
  : (, ) =>
    .(, (, ) =>
      .(function* () {
        const {  } = yield* ;
        return yield* .(, {
          : ,
          : () => .(new ({ : . })),
        });
      }),
    ),
});

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

export const  = .(function* () {
  const  = yield* ;
  return yield* .("/reports/export", {
    : { : { : 30_000 } },
  });
}).(.("TooSlow", () => .()));

What's next

On this page