Skip to content

Commit

Permalink
add the "key" in the "onExceeding" and "onBanReach" (#258)
Browse files Browse the repository at this point in the history
* Update index.d.ts

* Update index.js

* Update types.test.ts

* Update README.md

* test unit
  • Loading branch information
SergioGlorias authored Aug 30, 2022
1 parent 6f54dbf commit 011bbb2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Custom `onExceeding` example usage:
```js
await fastify.register(import('@fastify/rate-limit'), {
/* */
onExceeding: function (req) {
onExceeding: function (req, key) {
console.log('callback on exceeding ... executed before response to client')
}
})
Expand All @@ -268,7 +268,7 @@ Custom `onExceeded` example usage:
```js
await fastify.register(import('@fastify/rate-limit'), {
/* */
onExceeded: function (req) {
onExceeded: function (req, key) {
console.log('callback on exceeded ... executed before response to client')
}
})
Expand Down Expand Up @@ -348,10 +348,10 @@ fastify.get('/public/sub-rated-1', {
rateLimit: {
timeWindow: '1 minute',
allowList: ['127.0.0.1'],
onExceeding: function (request) {
onExceeding: function (request, key) {
console.log('callback on exceededing ... executed before response to client')
},
onExceeded: function (request) {
onExceeded: function (request, key) {
console.log('callback on exceeded ... to black ip in security group for example, request is give as argument')
}
}
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export interface RateLimitOptions {
context: errorResponseBuilderContext
) => object;
enableDraftSpec?: boolean;
onExceeding?: (req: FastifyRequest) => void;
onExceeded?: (req: FastifyRequest) => void;
onExceeding?: (req: FastifyRequest, key: string) => void;
onExceeded?: (req: FastifyRequest, key: string) => void;
}

export interface RateLimitPluginOptions extends RateLimitOptions {
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ function rateLimitRequestHandler (params, pluginComponent) {
if (params.addHeadersOnExceeding[params.labels.rateReset]) { res.header(params.labels.rateReset, timeLeft) }

if (typeof params.onExceeding === 'function') {
params.onExceeding(req)
params.onExceeding(req, key)
}
return
}
if (typeof params.onExceeded === 'function') {
params.onExceeded(req)
params.onExceeded(req, key)
}

if (params.addHeaders[params.labels.rateLimit]) { res.header(params.labels.rateLimit, maximum) }
Expand Down
8 changes: 4 additions & 4 deletions test/global-rate-limit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ test('With onExceeding option', async t => {
await fastify.register(rateLimit, {
max: 2,
timeWindow: '2s',
onExceeding: function (req) {
t.pass('onExceeding called')
onExceeding: function (req, key) {
if (req && key) t.pass('onExceeding called')
}
})

Expand All @@ -271,8 +271,8 @@ test('With onExceeded option', async t => {
await fastify.register(rateLimit, {
max: 2,
timeWindow: '2s',
onExceeded: function (req) {
t.pass('onExceeded called')
onExceeded: function (req, key) {
if (req && key) t.pass('onExceeded called')
}
})

Expand Down
4 changes: 2 additions & 2 deletions test/types/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ const options1: RateLimitPluginOptions = {
'x-ratelimit-reset': false,
'retry-after': false
},
onExceeding: (req: FastifyRequest<RequestGenericInterface>) => ({}),
onExceeded: (req: FastifyRequest<RequestGenericInterface>) => ({}),
onExceeding: (req: FastifyRequest<RequestGenericInterface>, key: string) => ({}),
onExceeded: (req: FastifyRequest<RequestGenericInterface>, key: string) => ({}),
onBanReach: (req: FastifyRequest<RequestGenericInterface>, key: string) => ({})
}
const options2: RateLimitPluginOptions = {
Expand Down

0 comments on commit 011bbb2

Please sign in to comment.