express-rate-limit 6.4.0 → 6.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/changelog.md CHANGED
@@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to
7
7
  [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8
8
 
9
+ ## [6.5.0](https://github.com/nfriedly/express-rate-limit/releases/tag/v6.5.0)
10
+
11
+ ## Changed
12
+
13
+ - The message option can now be a (sync/asynx) function that returns a value
14
+ (#311)
15
+ - Updated all dependencies
16
+
9
17
  ## [6.4.0](https://github.com/nfriedly/express-rate-limit/releases/tag/v6.3.0)
10
18
 
11
19
  ### Added
package/dist/index.cjs CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -118,8 +119,12 @@ var parseOptions = (passedOptions) => {
118
119
  }
119
120
  return request.ip;
120
121
  },
121
- handler(_request, response, _next, _optionsUsed) {
122
- response.status(config.statusCode).send(config.message);
122
+ async handler(request, response, _next, _optionsUsed) {
123
+ response.status(config.statusCode);
124
+ const message = typeof config.message === "function" ? await config.message(request, response) : config.message;
125
+ if (!response.writableEnded) {
126
+ response.send(message != null ? message : "Too many requests, please try again later.");
127
+ }
123
128
  },
124
129
  onLimitReached(_request, _response, _optionsUsed) {
125
130
  },
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Generated by dts-bundle-generator v6.8.0
1
+ // Generated by dts-bundle-generator v6.12.0
2
2
 
3
3
  import { NextFunction, Request, RequestHandler, Response } from 'express';
4
4
 
@@ -152,7 +152,7 @@ export interface Options {
152
152
  *
153
153
  * Defaults to `'Too many requests, please try again later.'`
154
154
  */
155
- readonly message: any;
155
+ readonly message: any | ValueDeterminingMiddleware<any>;
156
156
  /**
157
157
  * The HTTP status code to send back when a client is rate limited.
158
158
  *
@@ -276,7 +276,7 @@ export interface RateLimitInfo {
276
276
  *
277
277
  * @public
278
278
  */
279
- export declare const rateLimit: (passedOptions?: Partial<Options> | undefined) => RateLimitRequestHandler;
279
+ export declare const rateLimit: (passedOptions?: Partial<Options>) => RateLimitRequestHandler;
280
280
  /**
281
281
  * A `Store` that stores the hit count for each client in memory.
282
282
  *
package/dist/index.mjs CHANGED
@@ -91,8 +91,12 @@ var parseOptions = (passedOptions) => {
91
91
  }
92
92
  return request.ip;
93
93
  },
94
- handler(_request, response, _next, _optionsUsed) {
95
- response.status(config.statusCode).send(config.message);
94
+ async handler(request, response, _next, _optionsUsed) {
95
+ response.status(config.statusCode);
96
+ const message = typeof config.message === "function" ? await config.message(request, response) : config.message;
97
+ if (!response.writableEnded) {
98
+ response.send(message != null ? message : "Too many requests, please try again later.");
99
+ }
96
100
  },
97
101
  onLimitReached(_request, _response, _optionsUsed) {
98
102
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-rate-limit",
3
- "version": "6.4.0",
3
+ "version": "6.5.1",
4
4
  "description": "Basic IP rate-limiting middleware for Express. Use to limit repeated requests to public APIs and/or endpoints such as password reset.",
5
5
  "author": {
6
6
  "name": "Nathan Friedly",
@@ -70,25 +70,25 @@
70
70
  "express": "^4 || ^5"
71
71
  },
72
72
  "devDependencies": {
73
- "@jest/globals": "27.4.6",
73
+ "@jest/globals": "28.1.3",
74
74
  "@types/express": "4.17.13",
75
- "@types/jest": "27.4.1",
76
- "@types/node": "16.11.27",
75
+ "@types/jest": "28.1.6",
76
+ "@types/node": "18.0.6",
77
77
  "@types/supertest": "2.0.12",
78
78
  "cross-env": "7.0.3",
79
79
  "del-cli": "4.0.1",
80
- "dts-bundle-generator": "6.8.0",
81
- "esbuild": "0.14.38",
82
- "express": "4.17.3",
83
- "husky": "7.0.4",
84
- "jest": "27.5.1",
85
- "lint-staged": "12.4.0",
80
+ "dts-bundle-generator": "6.12.0",
81
+ "esbuild": "0.14.49",
82
+ "express": "4.18.1",
83
+ "husky": "8.0.1",
84
+ "jest": "28.1.3",
85
+ "lint-staged": "13.0.3",
86
86
  "npm-run-all": "4.1.5",
87
- "supertest": "6.2.2",
88
- "ts-jest": "27.1.4",
89
- "ts-node": "10.7.0",
90
- "typescript": "4.6.3",
91
- "xo": "0.48.0"
87
+ "supertest": "6.2.4",
88
+ "ts-jest": "28.0.7",
89
+ "ts-node": "10.9.1",
90
+ "typescript": "4.7.4",
91
+ "xo": "0.49.0"
92
92
  },
93
93
  "xo": {
94
94
  "prettier": true,
package/readme.md CHANGED
@@ -239,11 +239,30 @@ const limiter = rateLimit({
239
239
  The response body to send back when a client is rate limited.
240
240
 
241
241
  May be a `string`, JSON object, or any other value that Express's
242
- [response.send](https://expressjs.com/en/4x/api.html#response.send) method
243
- supports.
242
+ [`response.send`](https://expressjs.com/en/4x/api.html#res.send) method
243
+ supports. It can also be a (sync/async) function that accepts the Express
244
+ request and response objects and then returns a `string`, JSON object or any
245
+ other value the Express `response.send` function accepts.
244
246
 
245
247
  Defaults to `'Too many requests, please try again later.'`
246
248
 
249
+ An example of using a function:
250
+
251
+ ```ts
252
+ const isPremium = async (user) => {
253
+ // ...
254
+ }
255
+
256
+ const limiter = rateLimit({
257
+ // ...
258
+ message: async (request, response) => {
259
+ if (await isPremium(request.user))
260
+ return 'You can only make 10 requests every hour.'
261
+ else return 'You can only make 5 requests every hour.'
262
+ },
263
+ })
264
+ ```
265
+
247
266
  ### `statusCode`
248
267
 
249
268
  > `number`
@@ -348,7 +367,8 @@ const limiter = rateLimit({
348
367
  Express request handler that sends back a response when a client is
349
368
  rate-limited.
350
369
 
351
- By default, sends back the `statusCode` and `message` set via the options:
370
+ By default, sends back the `statusCode` and `message` set via the `options`,
371
+ similar to this:
352
372
 
353
373
  ```ts
354
374
  const limiter = rateLimit({