@nestjs/throttler
Advanced tools
Comparing version 4.1.0 to 4.2.0
import { ExecutionContext, ModuleMetadata, Type } from '@nestjs/common/interfaces'; | ||
export type Resolvable<T extends number | string | boolean> = T | ((context: ExecutionContext) => T | Promise<T>); | ||
export interface ThrottlerModuleOptions { | ||
limit?: number; | ||
ttl?: number; | ||
limit?: Resolvable<number>; | ||
ttl?: Resolvable<number>; | ||
ignoreUserAgents?: RegExp[]; | ||
@@ -6,0 +7,0 @@ storage?: any; |
@@ -1,4 +0,5 @@ | ||
export declare const Throttle: (limit?: number, ttl?: number) => MethodDecorator & ClassDecorator; | ||
import { Resolvable } from './throttler-module-options.interface'; | ||
export declare const Throttle: (limit?: Resolvable<number>, ttl?: Resolvable<number>) => MethodDecorator & ClassDecorator; | ||
export declare const SkipThrottle: (skip?: boolean) => MethodDecorator & ClassDecorator; | ||
export declare const InjectThrottlerOptions: () => PropertyDecorator & ParameterDecorator; | ||
export declare const InjectThrottlerStorage: () => PropertyDecorator & ParameterDecorator; |
@@ -21,2 +21,3 @@ import { CanActivate, ExecutionContext } from '@nestjs/common'; | ||
protected throwThrottlingException(context: ExecutionContext): void; | ||
private resolveValue; | ||
} |
@@ -39,6 +39,3 @@ "use strict"; | ||
} | ||
const routeOrClassLimit = this.reflector.getAllAndOverride(throttler_constants_1.THROTTLER_LIMIT, [ | ||
handler, | ||
classRef, | ||
]); | ||
const routeOrClassLimit = this.reflector.getAllAndOverride(throttler_constants_1.THROTTLER_LIMIT, [handler, classRef]); | ||
const routeOrClassTtl = this.reflector.getAllAndOverride(throttler_constants_1.THROTTLER_TTL, [ | ||
@@ -48,4 +45,4 @@ handler, | ||
]); | ||
const limit = routeOrClassLimit || this.options.limit; | ||
const ttl = routeOrClassTtl || this.options.ttl; | ||
const limit = await this.resolveValue(context, routeOrClassLimit || this.options.limit); | ||
const ttl = await this.resolveValue(context, routeOrClassTtl || this.options.ttl); | ||
return this.handleRequest(context, limit, ttl); | ||
@@ -88,2 +85,5 @@ } | ||
} | ||
async resolveValue(context, resolvableValue) { | ||
return typeof resolvableValue === 'function' ? resolvableValue(context) : resolvableValue; | ||
} | ||
}; | ||
@@ -90,0 +90,0 @@ exports.ThrottlerGuard = ThrottlerGuard = __decorate([ |
{ | ||
"name": "@nestjs/throttler", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "A Rate-Limiting module for NestJS to work on Express, Fastify, Websockets, Socket.IO, and GraphQL, all rolled up into a simple package.", | ||
@@ -31,16 +31,16 @@ "author": "Jay McDoniel <me@jaymcdoniel.dev>", | ||
"devDependencies": { | ||
"@changesets/cli": "2.26.1", | ||
"@commitlint/cli": "17.6.5", | ||
"@commitlint/config-angular": "17.6.5", | ||
"@nestjs/cli": "10.0.1", | ||
"@nestjs/common": "10.0.0", | ||
"@nestjs/core": "10.0.0", | ||
"@nestjs/graphql": "11.0.6", | ||
"@nestjs/platform-express": "10.0.0", | ||
"@nestjs/platform-fastify": "10.0.0", | ||
"@nestjs/platform-socket.io": "10.0.0", | ||
"@nestjs/platform-ws": "10.0.0", | ||
"@changesets/cli": "2.26.2", | ||
"@commitlint/cli": "17.6.6", | ||
"@commitlint/config-angular": "17.6.6", | ||
"@nestjs/cli": "10.1.7", | ||
"@nestjs/common": "10.0.5", | ||
"@nestjs/core": "10.0.5", | ||
"@nestjs/graphql": "12.0.7", | ||
"@nestjs/platform-express": "10.0.5", | ||
"@nestjs/platform-fastify": "10.0.5", | ||
"@nestjs/platform-socket.io": "10.0.5", | ||
"@nestjs/platform-ws": "10.0.5", | ||
"@nestjs/schematics": "10.0.1", | ||
"@nestjs/testing": "10.0.0", | ||
"@nestjs/websockets": "10.0.0", | ||
"@nestjs/testing": "10.0.5", | ||
"@nestjs/websockets": "10.0.5", | ||
"@semantic-release/git": "10.0.1", | ||
@@ -51,31 +51,31 @@ "@types/express": "4.17.17", | ||
"@types/md5": "2.3.2", | ||
"@types/node": "18.16.18", | ||
"@types/node": "18.16.19", | ||
"@types/supertest": "2.0.12", | ||
"@typescript-eslint/eslint-plugin": "5.59.11", | ||
"@typescript-eslint/parser": "5.59.11", | ||
"@apollo/server": "4.7.3", | ||
"@typescript-eslint/eslint-plugin": "5.61.0", | ||
"@typescript-eslint/parser": "5.61.0", | ||
"@apollo/server": "4.7.5", | ||
"apollo-server-fastify": "3.12.0", | ||
"conventional-changelog-cli": "3.0.0", | ||
"cz-conventional-changelog": "3.3.0", | ||
"eslint": "8.42.0", | ||
"eslint": "8.44.0", | ||
"eslint-config-prettier": "8.8.0", | ||
"eslint-plugin-import": "2.27.5", | ||
"graphql": "16.6.0", | ||
"graphql-tools": "8.3.20", | ||
"graphql": "16.7.1", | ||
"graphql-tools": "9.0.0", | ||
"husky": "8.0.3", | ||
"jest": "29.5.0", | ||
"lint-staged": "13.2.2", | ||
"jest": "29.6.1", | ||
"lint-staged": "13.2.3", | ||
"nodemon": "2.0.22", | ||
"pinst": "3.0.0", | ||
"prettier": "2.8.8", | ||
"prettier": "3.0.0", | ||
"reflect-metadata": "0.1.13", | ||
"rimraf": "5.0.1", | ||
"rxjs": "7.8.1", | ||
"socket.io": "4.6.2", | ||
"socket.io": "4.7.1", | ||
"supertest": "6.3.3", | ||
"ts-jest": "29.1.0", | ||
"ts-loader": "9.4.3", | ||
"ts-jest": "29.1.1", | ||
"ts-loader": "9.4.4", | ||
"ts-node": "10.9.1", | ||
"tsconfig-paths": "4.2.0", | ||
"typescript": "5.1.3", | ||
"typescript": "5.1.6", | ||
"ws": "8.13.0" | ||
@@ -82,0 +82,0 @@ }, |
@@ -207,2 +207,21 @@ <p align="center"> | ||
### Setting contextual ttl or limit | ||
You can specify the `ttl` or `limit` based on request's information using a callback function instead of a value: | ||
```ts | ||
@Module({ | ||
imports: [ | ||
ThrottlerModule.forRoot({ | ||
ttl: 60, | ||
limit: (context) => context.swithToHttp().getRequest().ip === "some-ip" ? 1000 : 10, | ||
}) | ||
] | ||
}) | ||
``` | ||
The same is available for `ttl`, and on the `Throttle` decorator. | ||
The callback accepts a Promise if you need an asynchronous operation to retrieve the value. | ||
### ThrottlerStorage | ||
@@ -209,0 +228,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
179267
438
329