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

Commit

Permalink
Upgrade package version to v2.5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
onur-ozkan committed Oct 10, 2020
1 parent 7db870e commit 5306912
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
27 changes: 6 additions & 21 deletions lib/rate-limiter.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Reflector } from '@nestjs/core'
import { NestInterceptor, Injectable, ExecutionContext, CallHandler, Inject, HttpStatus, Logger } from '@nestjs/common'
import { NestInterceptor, Injectable, ExecutionContext, CallHandler, Inject, HttpStatus, Logger, HttpException } from '@nestjs/common'
import {
RateLimiterMemory,
RateLimiterRes,
Expand Down Expand Up @@ -155,8 +155,8 @@ export class RateLimiterInterceptor implements NestInterceptor {
const rateLimiter: RateLimiterAbstract = await this.getRateLimiter(reflectedOptions)
const key = request.ip.replace(/^.*:/, '')

const operation = await this.responseHandler(response, key, rateLimiter, points, pointsConsumed, next)
return operation
await this.responseHandler(response, key, rateLimiter, points, pointsConsumed)
return next.handle()
}

private httpHandler(context: ExecutionContext) {
Expand All @@ -178,7 +178,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
}
}

private async responseHandler(response: any, key: any, rateLimiter: RateLimiterAbstract, points: number, pointsConsumed: number, next: CallHandler) {
private async responseHandler(response: any, key: any, rateLimiter: RateLimiterAbstract, points: number, pointsConsumed: number) {
if (this.options.for === 'Fastify' || this.options.for === 'FastifyGraphql') {
try {
if (this.spesificOptions?.queueEnabled || this.options.queueEnabled) await this.queueLimiter.removeTokens(1)
Expand All @@ -190,18 +190,9 @@ export class RateLimiterInterceptor implements NestInterceptor {
response.header('X-Retry-Remaining', rateLimiterResponse.remainingPoints)
response.header('X-Retry-Reset', new Date(Date.now() + rateLimiterResponse.msBeforeNext).toUTCString())
}
return next.handle()
} catch (rateLimiterResponse) {
response.header('Retry-After', Math.ceil(rateLimiterResponse.msBeforeNext / 1000))
response
.code(429)
.header('Content-Type', 'application/json; charset=utf-8')
.send({
statusCode: HttpStatus.TOO_MANY_REQUESTS,
error: 'Too Many Requests',
message: this.spesificOptions?.errorMessage || this.options.errorMessage
})
return []
throw new HttpException(this.spesificOptions?.errorMessage || this.options.errorMessage, HttpStatus.TOO_MANY_REQUESTS)
}
} else {
try {
Expand All @@ -214,15 +205,9 @@ export class RateLimiterInterceptor implements NestInterceptor {
response.set('X-Retry-Remaining', rateLimiterResponse.remainingPoints)
response.set('X-Retry-Reset', new Date(Date.now() + rateLimiterResponse.msBeforeNext).toUTCString())
}
return next.handle()
} catch (rateLimiterResponse) {
response.set('Retry-After', Math.ceil(rateLimiterResponse.msBeforeNext / 1000))
response.status(429).json({
statusCode: HttpStatus.TOO_MANY_REQUESTS,
error: 'Too Many Requests',
message: this.spesificOptions?.errorMessage || this.options.errorMessage
})
return []
throw new HttpException(this.spesificOptions?.errorMessage || this.options.errorMessage, HttpStatus.TOO_MANY_REQUESTS)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-rate-limiter",
"version": "2.5.5",
"version": "2.5.6",
"description": "Highly configurable rate limiter library",
"repository": {
"type": "git",
Expand Down

0 comments on commit 5306912

Please sign in to comment.