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

Commit 02bcfe7

Browse files
authored
Merge pull request #62 from ozkanonur/devtest
Devtest
2 parents ec295bb + 498a829 commit 02bcfe7

File tree

7 files changed

+78
-92
lines changed

7 files changed

+78
-92
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ public async signUp() {
147147
```ts
148148
import { RateLimit } from 'nestjs-rate-limiter';
149149

150-
@RateLimit({ keyPrefix: () => programmaticFuncThatReturnsValue(), points: 1, duration: 60, errorMessage: 'You can only request 1 in a minute by giving access token' })
150+
@RateLimit({
151+
keyPrefix: () => programmaticFuncThatReturnsValue(),
152+
points: 1,
153+
duration: 60,
154+
customResponseSchema: () => { return { timestamp: '1611479696', message: 'Request has been blocked' }}
155+
})
151156
@Get('/example')
152157
public async example() {
153158
console.log('hello');
@@ -186,7 +191,8 @@ The usage of the limiter options is as in the code block below. For an explanati
186191
execEvenlyMinDelayMs: undefined,
187192
indexKeyPrefix: {},
188193
maxQueueSize: 100,
189-
errorMessage: 'Rate limit exceeded'
194+
errorMessage: 'Rate limit exceeded',
195+
customResponseSchema: undefined
190196
}),
191197
],
192198
providers: [
@@ -431,6 +437,13 @@ GraphQLModule.forRoot({
431437

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

440+
#### ● customResponseSchema
441+
<code> Default: undefined </code>
442+
<br>
443+
<code> Type: string</code>
444+
<br>
445+
446+
customResponseSchema option allows to provide customizable response schemas
434447
# Benchmarks
435448

436449
1000 concurrent clients with maximum 2000 requests per sec during 30 seconds.

lib/default-options.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ describe('defaultRateLimiterOptions', () => {
2929
expect(Object.keys(defaultRateLimiterOptions.indexKeyPrefix).length).toBe(0)
3030
expect(defaultRateLimiterOptions.maxQueueSize).toBe(100)
3131
expect(defaultRateLimiterOptions.errorMessage).toBe('Rate limit exceeded')
32+
expect(defaultRateLimiterOptions.customResponseSchema).toBeUndefined()
3233
})
3334
})

lib/default-options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ export const defaultRateLimiterOptions: RateLimiterOptions = {
2424
execEvenlyMinDelayMs: undefined,
2525
indexKeyPrefix: {},
2626
maxQueueSize: 100,
27-
errorMessage: 'Rate limit exceeded'
27+
errorMessage: 'Rate limit exceeded',
28+
customResponseSchema: undefined
2829
}

lib/rate-limiter.interceptor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ export class RateLimiterInterceptor implements NestInterceptor {
191191
}
192192
} catch (rateLimiterResponse) {
193193
response.header('Retry-After', Math.ceil(rateLimiterResponse.msBeforeNext / 1000))
194-
if (typeof this.options.createErrorBody === 'function') {
195-
throw new HttpException(this.options.createErrorBody(rateLimiterResponse), HttpStatus.TOO_MANY_REQUESTS)
194+
if (typeof this.spesificOptions?.customResponseSchema === 'function' || typeof this.options.customResponseSchema === 'function') {
195+
var errorBody = this.spesificOptions?.customResponseSchema || this.options.customResponseSchema;
196+
throw new HttpException(errorBody(rateLimiterResponse), HttpStatus.TOO_MANY_REQUESTS)
196197
} else {
197198
throw new HttpException(this.spesificOptions?.errorMessage || this.options.errorMessage, HttpStatus.TOO_MANY_REQUESTS)
198199
}

lib/rate-limiter.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface RateLimiterOptions {
2727
indexKeyPrefix?: {}
2828
maxQueueSize?: number
2929
errorMessage?: string
30-
createErrorBody?: (rateLimiterResponse: RateLimiterRes) => {}
30+
customResponseSchema?: (rateLimiterResponse: RateLimiterRes) => {}
3131
}
3232

3333
export interface RateLimiterOptionsFactory {

package-lock.json

Lines changed: 55 additions & 85 deletions
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.6.5",
3+
"version": "2.7.0",
44
"description": "Highly configurable rate limiter library",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)