Skip to content

Commit

Permalink
feat: upgrade @nestjs/throttler to 4.0.0 along with required local ch…
Browse files Browse the repository at this point in the history
…anges for this
  • Loading branch information
kkoomen committed Jan 19, 2023
1 parent 243bce0 commit 3a1e31e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@nestjs/platform-ws": ">=7.0.0",
"@nestjs/schematics": ">=7.0.0",
"@nestjs/testing": ">=7.0.0",
"@nestjs/throttler": ">=2.0.0",
"@nestjs/throttler": "^4.0.0",
"@nestjs/websockets": ">=7.0.0",
"@types/express": "^4.17.12",
"@types/jest": "27.5.0",
Expand Down Expand Up @@ -83,7 +83,7 @@
"peerDependencies": {
"@nestjs/common": ">=7.0.0",
"@nestjs/core": ">=7.0.0",
"@nestjs/throttler": ">=2.0.0",
"@nestjs/throttler": ">=4.0.0",
"ioredis": "^5.0.4",
"reflect-metadata": "^0.1.13"
},
Expand Down
7 changes: 4 additions & 3 deletions src/throttler-storage-redis.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ThrottlerStorageRecord } from '@nestjs/throttler/dist/throttler-storage-record.interface';
import Redis, { Cluster } from 'ioredis';

export interface ThrottlerStorageRedis {
Expand All @@ -7,10 +8,10 @@ export interface ThrottlerStorageRedis {
redis: Redis | Cluster;

/**
* Add a record to the storage. The record will automatically be removed from
* the storage once its TTL has been reached.
* Increment the amount of requests for a given record. The record will
* automatically be removed from the storage once its TTL has been reached.
*/
addRecord(key: string, ttl: number): Promise<{ totalHits: number, timeToExpire: number }>;
increment(key: string, ttl: number): Promise<ThrottlerStorageRecord>;
}

export const ThrottlerStorageRedis = Symbol('ThrottlerStorageRedis');
3 changes: 2 additions & 1 deletion src/throttler-storage-redis.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import { ThrottlerStorageRecord } from '@nestjs/throttler/dist/throttler-storage-record.interface';
import Redis, { Cluster, RedisOptions } from 'ioredis';
import { ThrottlerStorageRedis } from './throttler-storage-redis.interface';

Expand Down Expand Up @@ -39,7 +40,7 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnMo
`.replace(/^\s+/gm, '').trim();
}

async addRecord(key: string, ttl: number): Promise<{ totalHits: number, timeToExpire: number }> {
async increment(key: string, ttl: number): Promise<ThrottlerStorageRecord> {
// Use EVAL instead of EVALSHA to support both redis instances and clusters.
const results: number[] = await this.redis.call(
'EVAL',
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1244,10 +1244,10 @@
dependencies:
tslib "2.4.1"

"@nestjs/throttler@>=2.0.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@nestjs/throttler/-/throttler-3.1.0.tgz#767b5fd8225be9e026576161d834b08bd7629e61"
integrity sha512-u9a5+rci6ybYtJ2is6gZWxE2dMZEpnK0qJ0C1OnchuNCvM21Bg6qym1TB6Uihhci+JfTv6E15WuASLXcIclsbA==
"@nestjs/throttler@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@nestjs/throttler/-/throttler-4.0.0.tgz#9f9c66df62da6ec1b1ad4305e6a3865676e3812f"
integrity sha512-2T2S/hFhxROa/PZRZhHWFkrukxg3T8Db32Y04m6U6j6N2XqFGSKXhjfIbORO8kk/S2jswa9oTX/K12E120tgaQ==
dependencies:
md5 "^2.2.1"

Expand Down

0 comments on commit 3a1e31e

Please sign in to comment.