@nestjs/throttler
Advanced tools
Comparing version 4.2.0 to 4.2.1
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?: Resolvable<number>; | ||
ttl?: Resolvable<number>; | ||
limit?: number; | ||
ttl?: number; | ||
ignoreUserAgents?: RegExp[]; | ||
@@ -7,0 +6,0 @@ storage?: any; |
@@ -1,5 +0,4 @@ | ||
import { Resolvable } from './throttler-module-options.interface'; | ||
export declare const Throttle: (limit?: Resolvable<number>, ttl?: Resolvable<number>) => MethodDecorator & ClassDecorator; | ||
export declare const Throttle: (limit?: number, ttl?: number) => MethodDecorator & ClassDecorator; | ||
export declare const SkipThrottle: (skip?: boolean) => MethodDecorator & ClassDecorator; | ||
export declare const InjectThrottlerOptions: () => PropertyDecorator & ParameterDecorator; | ||
export declare const InjectThrottlerStorage: () => PropertyDecorator & ParameterDecorator; |
@@ -21,3 +21,2 @@ import { CanActivate, ExecutionContext } from '@nestjs/common'; | ||
protected throwThrottlingException(context: ExecutionContext): void; | ||
private resolveValue; | ||
} |
@@ -39,3 +39,6 @@ "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, [ | ||
@@ -45,4 +48,4 @@ handler, | ||
]); | ||
const limit = await this.resolveValue(context, routeOrClassLimit || this.options.limit); | ||
const ttl = await this.resolveValue(context, routeOrClassTtl || this.options.ttl); | ||
const limit = routeOrClassLimit || this.options.limit; | ||
const ttl = routeOrClassTtl || this.options.ttl; | ||
return this.handleRequest(context, limit, ttl); | ||
@@ -85,5 +88,2 @@ } | ||
} | ||
async resolveValue(context, resolvableValue) { | ||
return typeof resolvableValue === 'function' ? resolvableValue(context) : resolvableValue; | ||
} | ||
}; | ||
@@ -90,0 +90,0 @@ exports.ThrottlerGuard = ThrottlerGuard = __decorate([ |
{ | ||
"name": "@nestjs/throttler", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"description": "A Rate-Limiting module for NestJS to work on Express, Fastify, Websockets, Socket.IO, and GraphQL, all rolled up into a simple package.", | ||
@@ -5,0 +5,0 @@ "author": "Jay McDoniel <me@jaymcdoniel.dev>", |
@@ -207,21 +207,2 @@ <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 | ||
@@ -324,2 +305,3 @@ | ||
- [Redis](https://github.com/kkoomen/nestjs-throttler-storage-redis) | ||
- [Mongo](https://www.npmjs.com/package/nestjs-throttler-storage-mongo) | ||
@@ -326,0 +308,0 @@ Feel free to submit a PR with your custom storage provider being added to this list. |
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
178189
435
311