RateShield is a simple, extensible, and framework-friendly rate limiting library for NestJS applications.
It allows you to protect sensitive actions (such as login, password reset, user updates, etc.) using decorators, without coupling rate limit logic to controllers or services.
- Built specifically for NestJS
- Decorator-based API
- Dynamic configuration via
forRoot - Action-based rate limiting (not route-based)
- In-memory storage by default
- Easy to extend (Redis, database, user-based, etc.)
- Clean and minimal public API
npm install @yonki/rateshieldImport RateShieldModule in your root module:
import { Module } from '@nestjs/common';
import { RateShieldModule } from '@yonki/rateshield';
@Module({
imports: [
RateShieldModule.forRoot({
// Set the storage type (currently only 'memory' is supported, Redis will be suported soon).
storage: 'memory',
rules: {
LOGIN: {
maxRequests: 5,
windowMs: 60,
blockDurationMs: 300
},
EDIT_USER: {
maxRequests: 10,
windowMs: 60,
blockDurationMs: 60
}
}
})
]
})
export class AppModule {}Notes:
- Storage: set
storageto the storage provider you want (for now use'memory'). - Fields:
maxRequests— máximo de requisições por janela;windowMs— janela em segundos;blockDurationMs— duração do bloqueio em segundos após estourar o limite.
Protect any route using the @RateLimit() decorator.
import { Controller, Post } from '@nestjs/common';
import { RateLimit } from '@yonki/rateshield';
@Controller('auth')
export class AuthController {
@Post('login')
@RateLimit('LOGIN')
login() {
return 'ok';
}
}import { Put } from '@nestjs/common';
import { RateLimit } from '@yonki/rateshield';
@Put(':id')
@RateLimit('EDIT_USER')
updateUser() {
return 'user updated';
}Each key (LOGIN, EDIT_USER, etc.) maps to a rule defined in forRoot.
When the rate limit is exceeded, the request is blocked with the following response:
{
"statusCode": 429,
"message": "Too many requests, try again later"
}- Rate limits are defined by action, not by route
- Controllers remain clean and focused on business logic
- Configuration is centralized in one place
- The library is easy to extend without breaking the public API
- Redis storage adapter
- Rate limiting by userId (JWT-based)
- Global default rate limit rule
- Role-based limits
- Cluster-safe support
Contributions are welcome.
If you want to add new storage providers, improve performance, or suggest new features, feel free to open an issue or pull request.
MIT