# Plugin recipes (https://effetch.monarcode.dev/docs/plugins/recipes)

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](https://effetch.monarcode.dev/docs/plugins/token-refresh).

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

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

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

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

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

export const auth = Fetch.plugin({
  name: "auth",
  onRequest: (request) =>
    Effect.gen(function* () {
      const session = yield* Session;
      const token = yield* session.accessToken;
      return token === undefined
        ? request
        : HttpClientRequest.bearerToken(request, token);
    }),
  onResponse: (response) =>
    Effect.gen(function* () {
      const sentToken = Headers.has(response.request.headers, "authorization");
      if (response.status !== 401 || !sentToken) {
        return response;
      }
      const session = yield* Session;
      yield* session.signOut;
      return yield* new SessionExpired();
    }),
});

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

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:

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

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

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

export const deadline = Fetch.plugin({
  name: "deadline",
  defaults: { millis: 5000 },
  wrap: (client, options) =>
    HttpClient.transform(client, (response, request) =>
      Effect.gen(function* () {
        const { millis } = yield* options;
        return yield* Effect.timeoutOrElse(response, {
          duration: millis,
          orElse: () => Effect.fail(new TooSlow({ url: request.url })),
        });
      }),
    ),
});
```

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

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

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

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

const deadline = Fetch.plugin({
  name: "deadline",
  defaults: { millis: 5000 },
  wrap: (client, options) =>
    HttpClient.transform(client, (response, request) =>
      Effect.gen(function* () {
        const { millis } = yield* options;
        return yield* Effect.timeoutOrElse(response, {
          duration: millis,
          orElse: () => Effect.fail(new TooSlow({ url: request.url })),
        });
      }),
    ),
});

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

export const report = Effect.gen(function* () {
  const api = yield* Api;
  return yield* api.get("/reports/export", {
    plugins: { deadline: { millis: 30_000 } },
  });
}).pipe(Effect.catchTag("TooSlow", () => Effect.succeed(undefined)));
```

## What's next

- [Write a plugin](https://effetch.monarcode.dev/docs/plugins/writing): learn the hooks these recipes use.
- [Refresh a token](https://effetch.monarcode.dev/docs/plugins/token-refresh): renew the token on a `401`
  in place of signing out.
- [Catch errors](https://effetch.monarcode.dev/docs/errors/catching): handle `SessionExpired` and
  `TooSlow` where you make the call.