UNPKG

@beignet/core

Version:

Core framework primitives for Beignet

66 lines 1.84 kB
/** * Input for a single rate-limit hit. */ export interface RateLimitHitOptions { /** * Unique key for this rate limit window. * * Examples: "global", "ip:203.0.113.10", "user:123". */ key: string; /** * Maximum allowed hits inside the window. */ limit: number; /** * Length of the window in seconds. */ windowSec: number; } /** * Result of recording a rate-limit hit. */ export interface RateLimitResult { /** * True when the hit is within the configured limit. */ allowed: boolean; /** * Remaining allowed hits in the window, if known. May be null if the * implementation does not track it. */ remaining: number | null; /** * Date when the window resets, if known. May be null. */ resetAt: Date | null; /** * Seconds until the caller should retry, if the hit was rejected and the * implementation can calculate it. */ retryAfterSeconds: number | null; } /** * App-facing rate limiting port. * * Implement this with an atomic shared store such as Redis for production. * Hook helpers call `hit(...)` to decide whether a request should continue. */ export interface RateLimitPort { /** * Record one hit for a rate-limit key and return the current decision. */ hit(options: RateLimitHitOptions): Promise<RateLimitResult>; } /** * Create an in-memory rate limiter for tests, examples, and single-process * development. * * This adapter is not durable or distributed. Production apps should use a * provider backed by a shared atomic store when multiple processes or regions * can serve requests. * * @returns A rate-limit port backed by a local `Map`. */ export declare function createMemoryRateLimiter(): RateLimitPort; //# sourceMappingURL=rate-limit.d.ts.map