Skip to content

Commit

Permalink
feat: allowfor multiple throttler contexts
Browse files Browse the repository at this point in the history
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: #1522

ref: #1369
ref: #1522
  • Loading branch information
jmcdo29 committed Sep 4, 2023
1 parent 19ccc45 commit 907c1a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/throttler-module-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ExecutionContext, ModuleMetadata, Type } from '@nestjs/common/interfaces';
import { ThrottlerStorage } from './throttler-storage.interface';
<<<<<<< HEAD

export type Resolvable<T extends number | string | boolean> =
| T
| ((context: ExecutionContext) => T | Promise<T>);
=======
>>>>>>> ccc52db (feat: allowfor multiple throttler contexts)

/**
* @publicApi
Expand Down Expand Up @@ -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<ThrottlerStorage>;
>>>>>>> ccc52db (feat: allowfor multiple throttler contexts)
/**
* The named throttlers to use
*/
Expand Down
4 changes: 4 additions & 0 deletions src/throttler.guard.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit 907c1a4

Please sign in to comment.