@fastify/rate-limit
Advanced tools
Comparing version 10.1.0 to 10.1.1
13
index.js
@@ -141,3 +141,3 @@ 'use strict' | ||
fastify.addHook('onRoute', (routeOptions) => { | ||
if (routeOptions.config?.rateLimit !== undefined) { | ||
if (routeOptions.config?.rateLimit != null) { | ||
if (typeof routeOptions.config.rateLimit === 'object') { | ||
@@ -148,9 +148,8 @@ const newPluginComponent = Object.create(pluginComponent) | ||
if (routeOptions?.config?.rateLimit?.groupId) { | ||
const groupId = routeOptions.config.rateLimit.groupId | ||
if (typeof groupId === 'string') { | ||
addRouteRateHook(pluginComponent, globalParams, routeOptions) | ||
} else { | ||
if (routeOptions.config.rateLimit.groupId) { | ||
if (typeof routeOptions.config.rateLimit.groupId !== 'string') { | ||
throw new Error('groupId must be a string') | ||
} | ||
addRouteRateHook(pluginComponent, globalParams, routeOptions) | ||
} else { | ||
@@ -224,3 +223,3 @@ addRouteRateHook(newPluginComponent, mergedRateLimitParams, routeOptions) | ||
let key = await params.keyGenerator(req) | ||
const groupId = req?.routeOptions?.config?.rateLimit?.groupId | ||
const groupId = req.routeOptions.config?.rateLimit?.groupId | ||
@@ -227,0 +226,0 @@ if (groupId) { |
{ | ||
"name": "@fastify/rate-limit", | ||
"version": "10.1.0", | ||
"version": "10.1.1", | ||
"description": "A low overhead rate limiter for your routes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -155,2 +155,3 @@ # @fastify/rate-limit | ||
- `keyGenerator`: a sync or async function to generate a unique identifier for each incoming request. Defaults to `(request) => request.ip`, the IP is resolved by fastify using `request.connection.remoteAddress` or `request.headers['x-forwarded-for']` if [trustProxy](https://fastify.dev/docs/latest/Reference/Server/#trustproxy) option is enabled. Use it if you want to override this behavior | ||
- `groupId`: a string to group multiple routes together introducing separate per-group rate limit. This will be added on top of the result of `keyGenerator`. | ||
- `errorResponseBuilder`: a function to generate a custom response object. Defaults to `(request, context) => ({statusCode: 429, error: 'Too Many Requests', message: ``Rate limit exceeded, retry in ${context.after}``})` | ||
@@ -157,0 +158,0 @@ - `addHeadersOnExceeding`: define which headers should be added in the response when the limit is not reached. Defaults all the headers will be shown |
@@ -121,2 +121,3 @@ /// <reference types='node' /> | ||
keyGenerator?: (req: FastifyRequest) => string | number | Promise<string | number>; | ||
groupId?: string; | ||
errorResponseBuilder?: ( | ||
@@ -123,0 +124,0 @@ req: FastifyRequest, |
@@ -44,2 +44,3 @@ import fastify, { | ||
keyGenerator: (req: FastifyRequest<RequestGenericInterface>) => req.ip, | ||
groupId: '42', | ||
errorResponseBuilder: ( | ||
@@ -46,0 +47,0 @@ req: FastifyRequest<RequestGenericInterface>, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
182203
475