# @askrjs/fetch/middleware

> Published API exports for @askrjs/fetch/middleware.

Source: [https://askrjs.com/docs/reference/api/fetch/middleware](https://askrjs.com/docs/reference/api/fetch/middleware)

Status: stable. Packages: @askrjs/fetch/middleware.

**Published packages are authoritative.** Examples may lag behind a published contract. When guidance differs, verify the exports and TypeScript declarations in your installed package, then file an issue.

## Exports

This entrypoint publishes 7 exports from the declarations shipped by @askrjs/fetch.

### `apiKeyAuth`

```ts
apiKeyAuth: ({ key, value, in: location }: { key: string; value: string | (() => string | Promise<string>); in?: "header" | "query"; }) => Middleware
```

Middleware that attaches an API key to every request, either as a header or a query
parameter (`in`, default `"header"`). `value` may be a static string or a (possibly
async) function resolved on each request. The key is marked sensitive so the
{@link logging} middleware redacts it.

### `bearerAuth`

```ts
bearerAuth: ({ token }: { token: string | (() => string | Promise<string>); }) => Middleware
```

Middleware that attaches an `Authorization: Bearer <token>` header to every request.
`token` may be a static string or a (possibly async) function resolved on each request.

### `logging`

```ts
logging: (logger?: { log(event: Record<string, unknown>): void; }) => Middleware
```

Middleware that logs a `"request"` event before and a `"response"` event after each
request, via the given logger (defaults to `console`). Headers and query parameters
marked sensitive (e.g. by {@link apiKeyAuth}) or matching common sensitive-name
patterns (authorization, cookie, token, secret, password, api key) are redacted.

### `retry`

```ts
retry: (options?: RetryOptions) => Middleware
```

Middleware that retries failed requests. Retries eligible methods on network failures
or eligible response statuses, honoring a `Retry-After` header when present and
stopping early if the request's deadline would be exceeded. A non-streaming request body
is replayed only when it can be cloned before the first attempt; streaming, already-consumed,
or otherwise non-cloneable bodies are sent once without retrying.

### `RetryOptions`

```ts
RetryOptions: any
```

Options controlling the {@link retry} middleware's behavior.

- `attempts`: Maximum number of attempts, including the first. Defaults to 3.

- `methods`: HTTP methods eligible for retry. Defaults to idempotent-by-convention methods.

- `statuses`: Response statuses that trigger a retry. Defaults to common transient failure codes.

- `delay`: Computes the delay (ms) before the given retry attempt, if no `Retry-After` header is present.

- `maxRetryAfter`: Maximum delay (ms) accepted from `Retry-After`. Defaults to 60 seconds.

### `telemetry`

```ts
telemetry: (hooks: TelemetryHooks) => Middleware
```

Middleware that wraps each request with {@link TelemetryHooks}, calling `start` before
the request, `end` after it completes, and `error` (then rethrowing) if it throws.

### `TelemetryHooks`

```ts
TelemetryHooks: any
```

Hooks invoked by the {@link telemetry} middleware around each request.

- `start`: Called before the request proceeds; its return value (a "span") is passed to `end`/`error`.

- `end`: Called after the request completes successfully (from this middleware's perspective).

- `error`: Called if a downstream middleware or the transport throws.

## Documentation navigation

[Previous](https://askrjs.com/docs/reference/api/fetch/root/index.md) | [Next](https://askrjs.com/docs/reference/api/i18n/root/index.md)
