Skip to main content

@rtorcato/api-rate-limit v2.0.0

Interfaces

RateLimiterOptions

Defined in: index.ts:1

Properties

requests

requests: number

Defined in: index.ts:3

Max requests allowed per key within the window.

windowMs

windowMs: number

Defined in: index.ts:5

Sliding window size in milliseconds.

store

store: RateLimitStore

Defined in: index.ts:13

Where hit counts live. Required — there is no default, because a silent in-memory default is wrong across multiple instances (each process holds its own counts, so the real limit becomes N× the configured value). Use memoryStore() for single-process/dev/tests, or redisStore() from @rtorcato/api-rate-limit-redis for limits shared across instances.


RateLimitResult

Defined in: index.ts:16

Properties

allowed

allowed: boolean

Defined in: index.ts:17

remaining

remaining: number

Defined in: index.ts:19

Requests remaining in the current window (0 when blocked).


RateLimitStore

Defined in: index.ts:22

Properties

hit

hit: (key, opts) => Promise<RateLimitResult>

Defined in: index.ts:30

Record a hit for key and report whether it is within the limit.

The window/limit are passed per call (not bound at construction) so one store instance — e.g. a shared Redis connection — can back many limiters with different windows.

Parameters
key

string

opts
windowMs

number

limit

number

Returns

Promise<RateLimitResult>

reset

reset: () => Promise<void>

Defined in: index.ts:32

Drop all tracked keys (handy for tests and manual resets).

Returns

Promise<void>


RateLimiter

Defined in: index.ts:35

Properties

check

check: (key) => Promise<RateLimitResult>

Defined in: index.ts:37

Record a hit for key and report whether it is within the limit.

Parameters
key

string

Returns

Promise<RateLimitResult>

reset

reset: () => Promise<void>

Defined in: index.ts:39

Drop all tracked keys (handy for tests and manual resets).

Returns

Promise<void>

Functions

memoryStore()

memoryStore(): RateLimitStore

Defined in: index.ts:52

In-memory sliding-window (log) store. Single-process only — counts live in this process's Map, so behind N replicas the effective limit is N× the configured value. Good for local dev, tests, and single-instance apps; reach for redisStore() when limits must hold across instances.

Returns

RateLimitStore


createRateLimiter()

createRateLimiter(__namedParameters): RateLimiter

Defined in: index.ts:102

Create a sliding-window rate limiter over a pluggable store.

Framework-agnostic — await check(key) from any handler and react to the result. Wrap it for Hono/Express in a dedicated adapter package.

const limiter = createRateLimiter({ requests: 100, windowMs: 60_000, store: memoryStore() })
const { allowed, remaining } = await limiter.check(ip)

Parameters

__namedParameters

RateLimiterOptions

Returns

RateLimiter