Libraries
    Preparing search index...
    • Creates a request batching middleware.

      Concurrent calls made through the same client are transparently coalesced into a single POST to the batch endpoint. Each individual call site receives its own typed response — the batching is completely invisible to application code.

      Placement: place batching() as the last middleware in the array (closest to the actual fetch). Wrapping it with retry() and timeout() lets those middlewares operate on each logical call's promise independently.

      Requests that are never batched:

      • The batch endpoint itself (prevents infinite recursion).
      • FormData or ReadableStream bodies (binary / streaming).
      • Any request matching the skip predicate.
      • Single-item flushes (sent directly, no batch overhead).

      Parameters

      Returns Middleware

      A Middleware that batches concurrent requests.

      import { retry } from '@cleverbrush/client/retry';
      import { timeout } from '@cleverbrush/client/timeout';
      import { batching } from '@cleverbrush/client/batching';

      const client = createClient(api, {
      middlewares: [
      retry(),
      timeout(),
      batching({ maxSize: 10, windowMs: 10 }),
      ],
      });