Gathering detailed insights and metrics for @zedquads/nestjs-bottleneck
Gathering detailed insights and metrics for @zedquads/nestjs-bottleneck
Gathering detailed insights and metrics for @zedquads/nestjs-bottleneck
Gathering detailed insights and metrics for @zedquads/nestjs-bottleneck
npm install @zedquads/nestjs-bottleneck
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
8
This package provides a NestJS module and decorators to easily integrate the bottleneck
rate-limiting library with your NestJS applications, supporting both REST and GraphQL.
First, install the package along with its peer dependencies:
1npm install @zedquads/nestjs-bottleneck bottleneck
optional:
1npm install ioredis redis
Module Setup Register the BottleneckModule in your NestJS application.
Static Configuration:
1import { Module } from '@nestjs/common';
2import { BottleneckModule } from '@zedquads/nestjs-bottleneck';
3
4@Module({
5 imports: [
6 BottleneckModule.forRoot({
7 maxConcurrent: 5,
8 minTime: 100,
9 datastore: 'ioredis',
10 connection: { host: 'localhost', port: 6379 },
11 isGlobal: true, // Set to 'false' to make the module local
12 }),
13 ],
14})
15export class AppModule {}
Async Configuration:
1import { Module } from '@nestjs/common'; 2import { ConfigModule, ConfigService } from '@nestjs/config'; 3import { BottleneckModule } from '@zedquads/nestjs-bottleneck'; 4 5@Module({ 6 imports: [ 7 ConfigModule.forRoot(), 8 BottleneckModule.forRootAsync({ 9 imports: [ConfigModule], 10 useFactory: async (configService: ConfigService) => ({ 11 maxConcurrent: configService.get<number>('BOTTLENECK_MAX_CONCURRENT'), 12 minTime: configService.get<number>('BOTTLENECK_MIN_TIME'), 13 datastore: 'ioredis', 14 connection: { 15 host: configService.get<string>('REDIS_HOST'), 16 port: configService.get<number>('REDIS_PORT'), 17 }, 18 }), 19 inject: [ConfigService], 20 isGlobal: true, // Set to 'false' to make the module local 21 }), 22 ], 23}) 24export class AppModule {}
Use the @Bottleneck decorator to apply rate limiting to your methods. It can be used in both REST controllers and GraphQL resolvers.
REST Example:
1import { Controller, Get, Query } from '@nestjs/common'; 2import { Bottleneck } from '@zedquads/nestjs-bottleneck'; 3 4@Controller('example') 5export class ExampleController { 6 @Get() 7 @Bottleneck() // Default key: ExampleController:exampleMethod 8 async exampleMethod() { 9 return 'This method is rate limited by Bottleneck.'; 10 } 11 12 @Get('custom') 13 @Bottleneck('custom-key') // Custom static key 14 async customKeyMethod() { 15 return 'This method is rate limited by Bottleneck with a custom key.'; 16 } 17 18 @Get('mapped') 19 @Bottleneck((param) => `mapped-key:${param}`, { 20 maxConcurrent: 3, 21 minTime: 200, 22 }) // Dynamic key and options 23 async mappedKeyMethod(@Query('param') param: string) { 24 return `This method is rate limited by Bottleneck with a dynamic key: ${param}.`; 25 } 26}
GraphQL Example:
1import { Resolver, Query, Args } from '@nestjs/graphql'; 2import { Bottleneck } from '@zedquads/nestjs-bottleneck'; 3 4@Resolver() 5export class ExampleResolver { 6 @Query(() => String) 7 @Bottleneck() // Default key: ExampleResolver:exampleMethod 8 async exampleMethod() { 9 return 'This method is rate limited by Bottleneck.'; 10 } 11 12 @Query(() => String) 13 @Bottleneck('custom-key') // Custom static key 14 async customKeyMethod() { 15 return 'This method is rate limited by Bottleneck with a custom key.'; 16 } 17 18 @Query(() => String) 19 @Bottleneck((param) => `mapped-key:${param}`, { 20 maxConcurrent: 3, 21 minTime: 200, 22 }) // Dynamic key and options 23 async mappedKeyMethod(@Args('param') param: string) { 24 return `This method is rate limited by Bottleneck with a dynamic key: ${param}.`; 25 } 26}
Wrapping Functions You can also use the Bottleneck service to wrap functions:
1import { Injectable } from '@nestjs/common'; 2import { BottleneckService } from '@zedquads/nestjs-bottleneck'; 3 4@Injectable() 5export class ExampleService { 6 constructor(private readonly bottleneckService: BottleneckService) {} 7 8 async wrapFunctionExample() { 9 const wrappedFunction = this.bottleneckService 10 .getGroupLimiter( 11 'example-group', 12 'example-key', 13 { maxConcurrent: 3, minTime: 200 }, // Optional custom options 14 ) 15 .wrap(async (param: string) => { 16 // Function logic here 17 return `Processed: ${param}`; 18 }); 19 20 return wrappedFunction('exampleParam'); 21 } 22}
Custom Decorator Create custom decorators to use Bottleneck options:
1import { Inject } from '@nestjs/common'; 2import { BOTTLENECK_OPTIONS } from '@zedquads/nestjs-bottleneck'; 3 4export const InjectBottleneckOptions = () => Inject(BOTTLENECK_OPTIONS);
Refer to the official Bottleneck documentation for more detailed configurations and options: Bottleneck on npm.
No vulnerabilities found.
No security vulnerabilities found.