@lucid-softworks/rate-limit
v0.1.0
Published
A typed fixed-window rate limiter for sync and async functions.
Maintainers
Readme
@lucid-softworks/rate-limit
Wrap a synchronous or asynchronous function so only a fixed number of calls start during each time window. Calls beyond the limit wait in FIFO order.
import { rateLimit } from "@lucid-softworks/rate-limit";
const fetchUser = rateLimit(
async (id: string) =>
fetch(`/api/users/${id}`).then((response) => response.json()),
{ limit: 5, intervalMs: 1_000 },
);
const user = await fetchUser("42");pending reports how many calls are queued. clear(reason?) rejects queued
calls without affecting work that has already started. Without a custom reason,
queued calls reject with RateLimitClearedError.
limit must be a positive integer and intervalMs must be a finite,
non-negative number. An interval of zero disables waiting while preserving the
same promise-based interface.
