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.3
Browse files Browse the repository at this point in the history
  • Loading branch information
onur-ozkan committed Oct 4, 2020
1 parent 16df519 commit 87e4d5b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
27 changes: 15 additions & 12 deletions lib/rate-limiter.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class RateLimiterInterceptor implements NestInterceptor {
this.spesificOptions = null
this.spesificOptions = options


const limiterOptions: RateLimiterOptions = {
...this.options,
...options
Expand All @@ -39,7 +38,8 @@ export class RateLimiterInterceptor implements NestInterceptor {

let rateLimiter: RateLimiterAbstract = this.rateLimiters.get(libraryArguments.keyPrefix)

if (libraryArguments.execEvenlyMinDelayMs === undefined) libraryArguments.execEvenlyMinDelayMs = (this.options.duration * 1000) / this.options.points
if (libraryArguments.execEvenlyMinDelayMs === undefined)
libraryArguments.execEvenlyMinDelayMs = (this.options.duration * 1000) / this.options.points

if (!rateLimiter) {
switch (this.spesificOptions?.type || this.options.type) {
Expand All @@ -56,7 +56,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
Logger.log(`Rate Limiter started with ${limiterOptions.keyPrefix} key prefix`, 'RateLimiterMemcache')
break
case 'Postgres':
if (libraryArguments.storeType === undefined) libraryArguments.storeType = this.options.storeClient.constructor.name
if (libraryArguments.storeType === undefined) libraryArguments.storeType = this.options.storeClient.constructor.name

libraryArguments.tableName = this.spesificOptions?.tableName || this.options.tableName
if (libraryArguments.tableName === undefined) {
Expand All @@ -78,7 +78,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
Logger.log(`Rate Limiter started with ${limiterOptions.keyPrefix} key prefix`, 'RateLimiterPostgres')
break
case 'MySQL':
if (libraryArguments.storeType === undefined) libraryArguments.storeType = this.options.storeClient.constructor.name
if (libraryArguments.storeType === undefined) libraryArguments.storeType = this.options.storeClient.constructor.name

libraryArguments.tableName = this.spesificOptions?.tableName || this.options.tableName
if (libraryArguments.tableName === undefined) {
Expand All @@ -100,7 +100,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
Logger.log(`Rate Limiter started with ${limiterOptions.keyPrefix} key prefix`, 'RateLimiterMySQL')
break
case 'Mongo':
if (libraryArguments.storeType === undefined) libraryArguments.storeType = this.options.storeClient.constructor.name
if (libraryArguments.storeType === undefined) libraryArguments.storeType = this.options.storeClient.constructor.name

libraryArguments.tableName = this.spesificOptions?.tableName || this.options.tableName
if (libraryArguments.tableName === undefined) {
Expand Down Expand Up @@ -190,15 +190,18 @@ 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
})
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 next.handle()
} else {
try {
if (this.spesificOptions?.queueEnabled || this.options.queueEnabled) await this.queueLimiter.removeTokens(1)
Expand All @@ -210,7 +213,6 @@ 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({
Expand All @@ -219,6 +221,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
message: this.spesificOptions?.errorMessage || this.options.errorMessage
})
}
return next.handle()
}
}
}
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.2",
"version": "2.5.3",
"description": "Highly configurable rate limiter library",
"repository": {
"type": "git",
Expand Down

0 comments on commit 87e4d5b

Please sign in to comment.