alepha@docs:~/docs/packages/alepha/server$
cat rate-limit.md
1 min read
Last commit:

#Alepha - Server Rate Limit

#Installation

Part of the alepha package. Import from alepha/server/rate-limit.

npm install alepha

#Overview

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:

  • The $rateLimit primitive with paths option for path-based rate limiting
  • The rateLimit option in action primitives for action-specific limiting

It offers sliding window rate limiting, custom key generation, and seamless integration with server routes.

ts
 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}

#API Reference

#Primitives

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.

#$rateLimit()

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.

ts
 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}
On This Page
No headings found...
ready
mainTypeScript
UTF-8packages_alepha_server_rate_limit.md