1.0.0 • Published 2 years ago

@risy/nestjs-rate-limiter v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Nestjs Rate Limiter

Nestjs Rate Limiter is a module for Nestjs that provides a rate limiter for Express, Fastify, Microservice, ExpressGraphql, and FastifyGraphql.

Installation

$ npm install --save @risy/nestjs-rate-limiter

Basic Usage

app.module.ts

import { RateLimiterModule } from '@risy/nestjs-rate-limiter';

@Module({ imports: RateLimiterModule.forRoot({ framework: 'Express', keyPrefix: 'global', points: 10, // 10 requests duration: 60, // per 1 minute by IP errorMessage: 'Too many requests, please try again later.', logger: true, redis: { host: 'localhost', port: 6379, } }) , })

export class AppModule {}

## Use For All Routes
> app.module.ts
```typescript
import { RateLimiterModule } from '@risy/nestjs-rate-limiter';

@Module({
    imports: [
        RateLimiterModule.forRoot({
            ...
        })
    ],
    providers: [
        {
            provide: APP_GUARD,
            useClass: RateLimiterGuard
        }
    ]
})

Use Guard

app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { UseGuards } from '@nestjs/common/decorator';
import { RateLimiterGuard } from '@risy/nestjs-rate-limiter';

@Controller()
export class AppController {
  @UseGuards(RateLimiterGuard)
  @Get()
  getHello() {
    return 'Hello World!';
  }
}

Use Decorator For Specific Routes

app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { RateLimiter, RateLimiterGuard } from '@risy/nestjs-rate-limiter';

@Controller() @RateLimiter({ keyPrefix: 'my-controller', points: 5, duration: 60, errorMessage: 'Too many requests, please try again later.' }) export class AppController { @UseGuards(RateLimiterGuard) @Get() getHello() { return 'Hello World!'; } }

## Options
```typescript
export interface RateLimiterOptions {
    framework: 'Express' | 'Fastify' | 'Microservice' | 'ExpressGraphql' | 'FastifyGraphql'
    redis?: {
        host: string
        port: number
    }
    keyPrefix?: string
    points: number
    duration: number
    errorMessage?: string
    logger?: boolean
}
1.0.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago