Custom hook option
leomp12 opened this issue ยท 3 comments
leomp12 commented
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the feature has not already been requested
๐ Feature Proposal
Add an option to use custom hook:
{
hook: 'preHandler', // default 'onRequest'
}
Motivation
I have (and I think other people might have too) the exact opposite case mentioned here #83 ๐ฅฒ
I mean, sometimes the need is to limit authenticated requests, to count requests per tenant/authentication, rate limiting by IP (soft limits) is already done in the proxy (Imo it's also a good practice, right ?), so the keyGenerator
for rate limit plugin must be called after other hooks that handle authentication.
Example
import type { RateLimitPluginOptions } from 'fastify-rate-limit';
const rateLimitOptions: RateLimitPluginOptions = {
hook: 'preHandler',
max: 30,
timeWindow: '1 minute',
allowList: ['_'],
keyGenerator(request) {
if (request.isPermissionGranted) {
return request.authenticationId;
}
return '_';
},
};
export default rateLimitOptions;
leomp12 commented
And I can give try if you want me to
mcollina commented
Would you like to send a Pull Request to address this issue? Remember to add unit tests.