Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit 5306912

Browse files
committed
Upgrade package version to v2.5.6
1 parent 7db870e commit 5306912

File tree

3 files changed

+8
-23
lines changed

3 files changed

+8
-23
lines changed

lib/rate-limiter.interceptor.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Reflector } from '@nestjs/core'
2-
import { NestInterceptor, Injectable, ExecutionContext, CallHandler, Inject, HttpStatus, Logger } from '@nestjs/common'
2+
import { NestInterceptor, Injectable, ExecutionContext, CallHandler, Inject, HttpStatus, Logger, HttpException } from '@nestjs/common'
33
import {
44
RateLimiterMemory,
55
RateLimiterRes,
@@ -155,8 +155,8 @@ export class RateLimiterInterceptor implements NestInterceptor {
155155
const rateLimiter: RateLimiterAbstract = await this.getRateLimiter(reflectedOptions)
156156
const key = request.ip.replace(/^.*:/, '')
157157

158-
const operation = await this.responseHandler(response, key, rateLimiter, points, pointsConsumed, next)
159-
return operation
158+
await this.responseHandler(response, key, rateLimiter, points, pointsConsumed)
159+
return next.handle()
160160
}
161161

162162
private httpHandler(context: ExecutionContext) {
@@ -178,7 +178,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
178178
}
179179
}
180180

181-
private async responseHandler(response: any, key: any, rateLimiter: RateLimiterAbstract, points: number, pointsConsumed: number, next: CallHandler) {
181+
private async responseHandler(response: any, key: any, rateLimiter: RateLimiterAbstract, points: number, pointsConsumed: number) {
182182
if (this.options.for === 'Fastify' || this.options.for === 'FastifyGraphql') {
183183
try {
184184
if (this.spesificOptions?.queueEnabled || this.options.queueEnabled) await this.queueLimiter.removeTokens(1)
@@ -190,18 +190,9 @@ export class RateLimiterInterceptor implements NestInterceptor {
190190
response.header('X-Retry-Remaining', rateLimiterResponse.remainingPoints)
191191
response.header('X-Retry-Reset', new Date(Date.now() + rateLimiterResponse.msBeforeNext).toUTCString())
192192
}
193-
return next.handle()
194193
} catch (rateLimiterResponse) {
195194
response.header('Retry-After', Math.ceil(rateLimiterResponse.msBeforeNext / 1000))
196-
response
197-
.code(429)
198-
.header('Content-Type', 'application/json; charset=utf-8')
199-
.send({
200-
statusCode: HttpStatus.TOO_MANY_REQUESTS,
201-
error: 'Too Many Requests',
202-
message: this.spesificOptions?.errorMessage || this.options.errorMessage
203-
})
204-
return []
195+
throw new HttpException(this.spesificOptions?.errorMessage || this.options.errorMessage, HttpStatus.TOO_MANY_REQUESTS)
205196
}
206197
} else {
207198
try {
@@ -214,15 +205,9 @@ export class RateLimiterInterceptor implements NestInterceptor {
214205
response.set('X-Retry-Remaining', rateLimiterResponse.remainingPoints)
215206
response.set('X-Retry-Reset', new Date(Date.now() + rateLimiterResponse.msBeforeNext).toUTCString())
216207
}
217-
return next.handle()
218208
} catch (rateLimiterResponse) {
219209
response.set('Retry-After', Math.ceil(rateLimiterResponse.msBeforeNext / 1000))
220-
response.status(429).json({
221-
statusCode: HttpStatus.TOO_MANY_REQUESTS,
222-
error: 'Too Many Requests',
223-
message: this.spesificOptions?.errorMessage || this.options.errorMessage
224-
})
225-
return []
210+
throw new HttpException(this.spesificOptions?.errorMessage || this.options.errorMessage, HttpStatus.TOO_MANY_REQUESTS)
226211
}
227212
}
228213
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs-rate-limiter",
3-
"version": "2.5.5",
3+
"version": "2.5.6",
44
"description": "Highly configurable rate limiter library",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)