1.3.7 • Published 1 year ago

rate-limit-nestjs v1.3.7

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Rate Limit Nestjs

npm i rate-limit-nestjs

Features and Decorators

Feature/DecoratorDescriptionUsage Example
@RateLimitApply a custom rate limit to a specific route or method.@RateLimit({ maxRequest: 20, ms: 30000 })
@SkipRateLimitExclude a specific route or method from any rate limiting rules.@SkipRateLimit()
RateLimitGuardA guard to enable rate limiting globally or at the controller level.@UseGuards(RateLimitGuard)
RateLimiterModuleA module to configure global rate limiting options for the entire application.RateLimiterModule.register({ maxRequest: 60, ms: 60000 })

Global Configuration

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

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

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();
  }
}
1.3.7

1 year ago

1.3.6

1 year ago

1.3.5

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.2

1 year ago

1.2.0

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago