Client

Interceptors

Run async hooks before every req and withProgress call.

An interceptor is an async function that runs before a request is issued. Register one with use():

import type { Interceptor } from "accorudo/client";

const logger: Interceptor = ({ method, path }) => {
  console.log(`${method.toUpperCase()} ${path}`);
};

mainApi.use(logger);

use() returns the client, so calls can be chained:

mainApi.use(logger).use(anotherInterceptor);

InterceptorContext

Every interceptor receives the same context, whether the call came from req or withProgress:

type InterceptorContext = {
  method: string;   // e.g. "get"
  path: string;     // resolved, e.g. "/widget/abc-123"
  endpoint: string; // the route template as declared, e.g. "/widget/:id"
  config: unknown;  // the raw config object passed to req/withProgress
};

An interceptor observes the call — it cannot rewrite config or add headers. Use endpoint to match against a specific route regardless of path params, or path when you need the resolved URL.

Execution order

  • Interceptors run in registration order, and each is awaited before the next runs.
  • All of them finish before the underlying fetch/XMLHttpRequest call is made.
  • An interceptor can delay the request by awaiting inside itself (this is how rate limiting works).
  • An interceptor can block the request entirely by throwing — the error propagates out of req/withProgress and the request never fires:
const blockDrafts: Interceptor = ({ path }) => {
  if (path.startsWith("/draft/")) {
    throw new Error(`Refusing to call draft endpoint: ${path}`);
  }
};

Built-in interceptors

  • Rate limiting — global and per-route request throttling via createRateLimiterInterceptor.
Copyright © 2026