Part of the alepha package. Import from alepha/server/rate-limit.
npm install alepha
Provides rate limiting capabilities for server routes and actions with configurable limits and windows.
The server-rate-limit module enables per-route and per-action rate limiting using either:
$rateLimit primitive with paths option for path-based rate limitingrateLimit option in action primitives for action-specific limitingIt offers sliding window rate limiting, custom key generation, and seamless integration with server routes.
1import { $rateLimit, AlephaServerRateLimit } from "alepha/server/rate-limit"; 2 3class ApiService { 4 // Path-specific rate limiting 5 apiRateLimit = $rateLimit({ 6 paths: ["/api/*"], 7 max: 100, 8 windowMs: 15 * 60 * 1000, // 15 minutes 9 });10}
Primitives are functions that define and configure various aspects of your application. They follow the convention of starting with $ and return configured primitive instances.
For more details, see the Primitives documentation.
Declares rate limiting for server routes or custom usage. This primitive provides methods to check rate limits and configure behavior within the server request/response cycle.
1class ApiService { 2 // Apply rate limiting to specific paths 3 apiRateLimit = $rateLimit({ 4 paths: ["/api/*"], 5 max: 100, 6 windowMs: 15 * 60 * 1000, // 15 minutes 7 }); 8 9 // Or use check() method for manual rate limiting10 customAction = $action({11 handler: async (req) => {12 const result = await this.apiRateLimit.check(req);13 if (!result.allowed) throw new Error("Rate limited");14 return "ok";15 },16 });17}