nestjs-rate-limiter
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,1 +0,2 @@ | ||
export declare const RateLimit: any; | ||
import { RateLimiterModuleOptions } from './rate-limiter.interface'; | ||
export declare const RateLimit: (options: RateLimiterModuleOptions) => MethodDecorator; |
@@ -65,4 +65,5 @@ "use strict"; | ||
exports: [rate_limiter_constants_1.RATE_LIMITER_OPTIONS], | ||
providers: [{ provide: rate_limiter_constants_1.RATE_LIMITER_OPTIONS, useValue: default_options_1.defaultRateLimiterOptions }], | ||
}) | ||
], RateLimiterModule); | ||
exports.RateLimiterModule = RateLimiterModule; |
{ | ||
"name": "nestjs-rate-limiter", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A configurable rate limiter for NestJS", | ||
@@ -21,13 +21,14 @@ "repository": { | ||
"homepage": "https://github.com/RyanTheAllmighty/nestjs-rate-limiter#readme", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"directories": { | ||
"lib": "lib" | ||
}, | ||
"main": "dist", | ||
"scripts": { | ||
"prebuild": "rimraf dist/", | ||
"build": "tsc -p tsconfig.json", | ||
"test": "jest --passWithNoTests", | ||
"prepublish:next": "npm run build", | ||
"publish:next": "npm publish --access public --tag next", | ||
"prepublish:npm": "npm run build", | ||
"publish:npm": "npm publish --access public" | ||
"prepublishOnly": "npm run build", | ||
"release": "np" | ||
}, | ||
@@ -59,4 +60,6 @@ "husky": { | ||
"lint-staged": "^8.2.1", | ||
"np": "^5.0.3", | ||
"prettier": "^1.18.2", | ||
"reflect-metadata": "^0.1.13", | ||
"rimraf": "^2.6.3", | ||
"rxjs": "^6.5.2", | ||
@@ -63,0 +66,0 @@ "ts-jest": "^24.0.2", |
@@ -15,7 +15,6 @@ <p align="center"> | ||
`nestjs-rate-limiter` is a module which adds in configurable rate limiting for | ||
[NestJS](https://github.com/nestjs/nest) applications. | ||
`nestjs-rate-limiter` is a module which adds in configurable rate limiting for [NestJS](https://github.com/nestjs/nest) | ||
applications. | ||
Under the hood it uses | ||
[rate-limiter-flexible](https://github.com/animir/node-rate-limiter-flexible). | ||
Under the hood it uses [rate-limiter-flexible](https://github.com/animir/node-rate-limiter-flexible). | ||
@@ -64,3 +63,3 @@ ## Installation | ||
@UseInterceptors(new RateLimiterInterceptor()) | ||
@UseInterceptors(RateLimiterInterceptor) | ||
@Get('/login') | ||
@@ -85,3 +84,3 @@ public async login() { | ||
provide: APP_INTERCEPTOR, | ||
useValue: new RateLimiterInterceptor(), | ||
useClass: RateLimiterInterceptor, | ||
}, | ||
@@ -95,4 +94,4 @@ ], | ||
You can use the `@RateLimit` decorator to specify the points and duration for rate limiting on a per | ||
controller or per route basis: | ||
You can use the `@RateLimit` decorator to specify the points and duration for rate limiting on a per controller or per | ||
route basis: | ||
@@ -113,21 +112,18 @@ > app.controller.ts | ||
Note that when passing in options via the decorator, it will combine the options for the module | ||
(defined via `RateLimiterModule.register` or the default ones) along with the decorator options. | ||
While this should be fine for most use cases, if you have defined a global interceptor with a | ||
`pointsConsumed` option, that will also apply to all decorated requests. So if you need to have | ||
a different `pointsConsumed` for decorated requests than what you have defined globally, you must | ||
pass it in when writing your decorator. | ||
Note that when passing in options via the decorator, it will combine the options for the module (defined via | ||
`RateLimiterModule.register` or the default ones) along with the decorator options. While this should be fine for most | ||
use cases, if you have defined a global interceptor with a `pointsConsumed` option, that will also apply to all | ||
decorated requests. So if you need to have a different `pointsConsumed` for decorated requests than what you have | ||
defined globally, you must pass it in when writing your decorator. | ||
Also note that if the `keyPrefix` is already in use, it will not update any options, only reuse the | ||
existing rate limiter object when it was last instantiated. This should be fine with the decorators, | ||
unless you manually specify a duplicate `keyPrefix` or reuse the same class and method names with | ||
the decorator. | ||
Also note that if the `keyPrefix` is already in use, it will not update any options, only reuse the existing rate | ||
limiter object when it was last instantiated. This should be fine with the decorators, unless you manually specify a | ||
duplicate `keyPrefix` or reuse the same class and method names with the decorator. | ||
## Configuration | ||
By default, the rate limiter will limit requests to 4 requests per 1 second window, using an | ||
in memory cache. | ||
By default, the rate limiter will limit requests to 4 requests per 1 second window, using an in memory cache. | ||
To change the settings for `nestjs-rate-limiter`, you can define a `RateLimiterModuleOptions` object | ||
when registering the module: | ||
To change the settings for `nestjs-rate-limiter`, you can define a `RateLimiterModuleOptions` object when registering | ||
the module: | ||
@@ -155,4 +151,3 @@ > app.module.ts | ||
The above example would rate limit the `/login` route to 1 request every 1 second using an im memory | ||
cache. | ||
The above example would rate limit the `/login` route to 1 request every 1 second using an im memory cache. | ||
@@ -166,4 +161,4 @@ When defining your options, you can pass through any options supported by `rate-limiter-flexible` in order to setup any | ||
This is the type of rate limiter that the underlying `rate-limiter-flexible` library will use to | ||
keep track of the requests made by users. | ||
This is the type of rate limiter that the underlying `rate-limiter-flexible` library will use to keep track of the | ||
requests made by users. | ||
@@ -180,15 +175,15 @@ Valid values for this library are: | ||
There are other options that the `rate-limiter-flexible` library supports, but aren't implemented | ||
within this library yet. Feel free to submit a PR adding support for those. | ||
There are other options that the `rate-limiter-flexible` library supports, but aren't implemented within this library | ||
yet. Feel free to submit a PR adding support for those. | ||
### points: number | ||
This is the number of 'points' the user will be given per period. You can think of points as simply | ||
the number of requests that a user can make in a set period. | ||
This is the number of 'points' the user will be given per period. You can think of points as simply the number of | ||
requests that a user can make in a set period. | ||
The underlying library allows consuming a set amount of points per action, for instance maybe some | ||
actions a user can take, might be more resource intensive, and therefor take up more 'points'. | ||
The underlying library allows consuming a set amount of points per action, for instance maybe some actions a user can | ||
take, might be more resource intensive, and therefor take up more 'points'. | ||
By default we assume all requests consume 1 point. But this can be set using the `pointsConsumed` | ||
configuration option or via the `@RateLimit` decorator. | ||
By default we assume all requests consume 1 point. But this can be set using the `pointsConsumed` configuration option | ||
or via the `@RateLimit` decorator. | ||
@@ -201,4 +196,4 @@ ### pointsConsumed: number | ||
For instance if you have a limit of 100 points per 60 seconds, and `pointsConsumed` is set to 10, | ||
the user will effectively be able to make 10 requests per 60 seconds. | ||
For instance if you have a limit of 100 points per 60 seconds, and `pointsConsumed` is set to 10, the user will | ||
effectively be able to make 10 requests per 60 seconds. | ||
@@ -217,11 +212,10 @@ ### duration: number | ||
When setting up `nestjs-rate-limiter`, you should make sure that any `keyPrefix` values are unique. | ||
If they are not unique, then they will share the same rate limit. | ||
When setting up `nestjs-rate-limiter`, you should make sure that any `keyPrefix` values are unique. If they are not | ||
unique, then they will share the same rate limit. | ||
By default, if you don't set this up, the underlying library will use a `keyPrefix` of `rlflx`. | ||
When using the `@RateLimit` decorator, the controller name and route name will be used. | ||
By default, if you don't set this up, the underlying library will use a `keyPrefix` of `rlflx`. When using the | ||
`@RateLimit` decorator, the controller name and route name will be used. | ||
For instance if you have the decorator on a controller, the `keyPrefix` will be the controllers | ||
name. If used on a route, it will be a combination of the controllers name and the route functions | ||
name. | ||
For instance if you have the decorator on a controller, the `keyPrefix` will be the controllers name. If used on a | ||
route, it will be a combination of the controllers name and the route functions name. | ||
@@ -242,4 +236,4 @@ ## Examples | ||
Then you must create a client (offline queue must be turned off) and pass it via | ||
the `storeClient` config option to `RateLimiterModule.register`: | ||
Then you must create a client (offline queue must be turned off) and pass it via the `storeClient` config option to | ||
`RateLimiterModule.register`: | ||
@@ -246,0 +240,0 @@ > app.module.ts |
29471
19
303
16
382