From 54991ad3d23444c2894b08481af75227a8eafc97 Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Fri, 4 Oct 2024 20:33:01 +0300 Subject: [PATCH] fix: update documentation and types for groupId (#384) Relates to https://github.com/fastify/fastify-rate-limit/pull/380. --- README.md | 1 + types/index.d.ts | 1 + types/index.test-d.ts | 1 + 3 files changed, 3 insertions(+) diff --git a/README.md b/README.md index a33e767..606a066 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ await fastify.register(import('@fastify/rate-limit'), { - `store`: a custom store to track requests and rates which allows you to use your own storage mechanism (using an RDBMS, MongoDB, etc.) as well as further customizing the logic used in calculating the rate limits. A simple example is provided below as well as a more detailed example using Knex.js can be found in the [`example/`](https://github.com/fastify/fastify-rate-limit/tree/master/example) folder - `skipOnError`: if `true` it will skip errors generated by the storage (e.g. redis not reachable). - `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}``})` - `addHeadersOnExceeding`: define which headers should be added in the response when the limit is not reached. Defaults all the headers will be shown - `addHeaders`: define which headers should be added in the response when the limit is reached. Defaults all the headers will be shown diff --git a/types/index.d.ts b/types/index.d.ts index b848454..81ce22b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -118,6 +118,7 @@ declare namespace fastifyRateLimit { ban?: number; onBanReach?: (req: FastifyRequest, key: string) => void; keyGenerator?: (req: FastifyRequest) => string | number | Promise; + groupId?: string; errorResponseBuilder?: ( req: FastifyRequest, context: errorResponseBuilderContext diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 4c14c0a..ac8c13b 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -41,6 +41,7 @@ const options1: RateLimitPluginOptions = { ban: 10, continueExceeding: false, keyGenerator: (req: FastifyRequest) => req.ip, + groupId: '42', errorResponseBuilder: ( req: FastifyRequest, context: errorResponseBuilderContext