https://github.com/c4nzin/rate-limit-nestjs
A Nestjs library for limiting requests.
https://github.com/c4nzin/rate-limit-nestjs
Last synced: about 1 month ago
JSON representation
A Nestjs library for limiting requests.
- Host: GitHub
- URL: https://github.com/c4nzin/rate-limit-nestjs
- Owner: c4nzin
- Created: 2024-11-26T17:04:05.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-12-01T08:10:30.000Z (10 months ago)
- Last Synced: 2025-02-03T01:34:11.684Z (8 months ago)
- Language: TypeScript
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rate Limit Nestjs
```bash
npm i rate-limit-nestjs
```## Features and Decorators
| **Feature/Decorator** | **Description** | **Usage Example** |
| --------------------- | ------------------------------------------------------------------------------ | ----------------------------------------------------------- |
| `@RateLimit` | Apply a custom rate limit to a specific route or method. | `@RateLimit({ maxRequest: 20, ms: 30000 })` |
| `@SkipRateLimit` | Exclude a specific route or method from any rate limiting rules. | `@SkipRateLimit()` |
| `RateLimitGuard` | A guard to enable rate limiting globally or at the controller level. | `@UseGuards(RateLimitGuard)` |
| `RateLimiterModule` | A module to configure global rate limiting options for the entire application. | `RateLimiterModule.register({ maxRequest: 60, ms: 60000 })` |# Global Configuration
```typescript
import { Module } from "@nestjs/common";
import { RateLimiterModule } from "rate-limit-nestjs";@Module({
imports: [
RateLimiterModule.register({
maxRequest: 60, // Maximum 60 requests
ms: 60000, // Within 60 seconds (1 minute)
}),
],
})
export class ExampleModule {}
```# Controller Level Scope
```typescript
import { Controller, Get, UseGuards } from "@nestjs/common";
import { RateLimitGuard } from "rate-limit-nestjs";@Controller()
@UseGuards(RateLimitGuard) // Global scope for all methods in this controller
export class ExampleController {
@Get("/info")
public getInfo() {
return "This route is rate-limited by the global guard.";
}
}
```# Method-Specific Rate limit
```typescript
import { Controller, Get } from "@nestjs/common";
import { RateLimit } from "rate-limit-nestjs";@Controller()
export class ExampleController {
@Get("/create")
@RateLimit({
maxRequest: 20, // Maximum 20 requests
ms: 30000, // Within 30 seconds
}) // Specific to this route
public createUser() {
return "This route has a specific rate limit of 20 requests per 30 seconds.";
}@Get()
@SkipRateLimit() // You can optionally exclude specific routes from rate limiting.
getHello(): string {
return this.appService.getHello();
}
}
```