From 907c1a4072f7d7bd8223e15532eb775c967848cd Mon Sep 17 00:00:00 2001 From: Jay McDoniel Date: Thu, 6 Jul 2023 10:47:34 -0700 Subject: [PATCH] feat: allowfor multiple throttler contexts This is a bit of something that I've wanted to do for a while and inspired by [this pr][pr]. With the new appraoch, we're now able to let users define scales at which they would like the throttling to work over, and let it work for any number of configuratins, from a single 10 requests in 5 seconds to scales of months, or milliseconds BREAKING CHANGES: It's worth noting there are quite a few breaking changes in this which will be reflected in the changelog as well, but better to have multiple mentions in my opinion * ttl is now in milliseconds, not seconds, but there are time helper exposed to ease the migration to that * the module options is now either an array or an object with a `throttlers` array property * `@Throttle()` now takes in an object instead of two parameters, to allow for setting multiple throttle contexts at once in a more readable manner * `@ThrottleSkip()` now takes in an object with string boolean to say which throttler should be skipped pr: https://github.com/nestjs/throttler/pull/1522 ref: #1369 ref: #1522 --- src/throttler-module-options.interface.ts | 7 +++++++ src/throttler.guard.ts | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/throttler-module-options.interface.ts b/src/throttler-module-options.interface.ts index ef5d1c3c..48ab5761 100644 --- a/src/throttler-module-options.interface.ts +++ b/src/throttler-module-options.interface.ts @@ -1,9 +1,12 @@ import { ExecutionContext, ModuleMetadata, Type } from '@nestjs/common/interfaces'; import { ThrottlerStorage } from './throttler-storage.interface'; +<<<<<<< HEAD export type Resolvable = | T | ((context: ExecutionContext) => T | Promise); +======= +>>>>>>> ccc52db (feat: allowfor multiple throttler contexts) /** * @publicApi @@ -56,7 +59,11 @@ export type ThrottlerModuleOptions = /** * The storage class to use where all the record will be stored in. */ +<<<<<<< HEAD storage?: ThrottlerStorage; +======= + storage?: Type; +>>>>>>> ccc52db (feat: allowfor multiple throttler contexts) /** * The named throttlers to use */ diff --git a/src/throttler.guard.ts b/src/throttler.guard.ts index 33f5983f..49da9110 100644 --- a/src/throttler.guard.ts +++ b/src/throttler.guard.ts @@ -1,11 +1,15 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import * as md5 from 'md5'; +<<<<<<< HEAD import { Resolvable, ThrottlerModuleOptions, ThrottlerOptions, } from './throttler-module-options.interface'; +======= +import { ThrottlerModuleOptions, ThrottlerOptions } from './throttler-module-options.interface'; +>>>>>>> ccc52db (feat: allowfor multiple throttler contexts) import { ThrottlerStorage } from './throttler-storage.interface'; import { THROTTLER_LIMIT, THROTTLER_SKIP, THROTTLER_TTL } from './throttler.constants'; import { InjectThrottlerOptions, InjectThrottlerStorage } from './throttler.decorator';