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

Commit

Permalink
Merge pull request #62 from ozkanonur/devtest
Browse files Browse the repository at this point in the history
Devtest
  • Loading branch information
onur-ozkan authored Jan 24, 2021
2 parents ec295bb + 498a829 commit 02bcfe7
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 92 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ public async signUp() {
```ts
import { RateLimit } from 'nestjs-rate-limiter';

@RateLimit({ keyPrefix: () => programmaticFuncThatReturnsValue(), points: 1, duration: 60, errorMessage: 'You can only request 1 in a minute by giving access token' })
@RateLimit({
keyPrefix: () => programmaticFuncThatReturnsValue(),
points: 1,
duration: 60,
customResponseSchema: () => { return { timestamp: '1611479696', message: 'Request has been blocked' }}
})
@Get('/example')
public async example() {
console.log('hello');
Expand Down Expand Up @@ -186,7 +191,8 @@ The usage of the limiter options is as in the code block below. For an explanati
execEvenlyMinDelayMs: undefined,
indexKeyPrefix: {},
maxQueueSize: 100,
errorMessage: 'Rate limit exceeded'
errorMessage: 'Rate limit exceeded',
customResponseSchema: undefined
}),
],
providers: [
Expand Down Expand Up @@ -431,6 +437,13 @@ GraphQLModule.forRoot({

errorMessage option can change the error message of rate limiter exception.

#### ● customResponseSchema
<code> Default: undefined </code>
<br>
<code> Type: string</code>
<br>

customResponseSchema option allows to provide customizable response schemas
# Benchmarks

1000 concurrent clients with maximum 2000 requests per sec during 30 seconds.
Expand Down
1 change: 1 addition & 0 deletions lib/default-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ describe('defaultRateLimiterOptions', () => {
expect(Object.keys(defaultRateLimiterOptions.indexKeyPrefix).length).toBe(0)
expect(defaultRateLimiterOptions.maxQueueSize).toBe(100)
expect(defaultRateLimiterOptions.errorMessage).toBe('Rate limit exceeded')
expect(defaultRateLimiterOptions.customResponseSchema).toBeUndefined()
})
})
3 changes: 2 additions & 1 deletion lib/default-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export const defaultRateLimiterOptions: RateLimiterOptions = {
execEvenlyMinDelayMs: undefined,
indexKeyPrefix: {},
maxQueueSize: 100,
errorMessage: 'Rate limit exceeded'
errorMessage: 'Rate limit exceeded',
customResponseSchema: undefined
}
5 changes: 3 additions & 2 deletions lib/rate-limiter.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ export class RateLimiterInterceptor implements NestInterceptor {
}
} catch (rateLimiterResponse) {
response.header('Retry-After', Math.ceil(rateLimiterResponse.msBeforeNext / 1000))
if (typeof this.options.createErrorBody === 'function') {
throw new HttpException(this.options.createErrorBody(rateLimiterResponse), HttpStatus.TOO_MANY_REQUESTS)
if (typeof this.spesificOptions?.customResponseSchema === 'function' || typeof this.options.customResponseSchema === 'function') {
var errorBody = this.spesificOptions?.customResponseSchema || this.options.customResponseSchema;
throw new HttpException(errorBody(rateLimiterResponse), HttpStatus.TOO_MANY_REQUESTS)
} else {
throw new HttpException(this.spesificOptions?.errorMessage || this.options.errorMessage, HttpStatus.TOO_MANY_REQUESTS)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rate-limiter.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface RateLimiterOptions {
indexKeyPrefix?: {}
maxQueueSize?: number
errorMessage?: string
createErrorBody?: (rateLimiterResponse: RateLimiterRes) => {}
customResponseSchema?: (rateLimiterResponse: RateLimiterRes) => {}
}

export interface RateLimiterOptionsFactory {
Expand Down
140 changes: 55 additions & 85 deletions 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.6.5",
"version": "2.7.0",
"description": "Highly configurable rate limiter library",
"repository": {
"type": "git",
Expand Down

0 comments on commit 02bcfe7

Please sign in to comment.