express-rate-limit 6.7.0 → 6.8.0

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.
@@ -0,0 +1,363 @@
1
+ // Generated by dts-bundle-generator v8.0.1
2
+
3
+ import { NextFunction, Request, RequestHandler, Response } from 'express';
4
+
5
+ /**
6
+ * Callback that fires when a client's hit counter is incremented.
7
+ *
8
+ * @param error {Error | undefined} - The error that occurred, if any.
9
+ * @param totalHits {number} - The number of hits for that client so far.
10
+ * @param resetTime {Date | undefined} - The time when the counter resets.
11
+ */
12
+ export type IncrementCallback = (error: Error | undefined, totalHits: number, resetTime: Date | undefined) => void;
13
+ /**
14
+ * Method (in the form of middleware) to generate/retrieve a value based on the
15
+ * incoming request.
16
+ *
17
+ * @param request {Request} - The Express request object.
18
+ * @param response {Response} - The Express response object.
19
+ *
20
+ * @returns {T} - The value needed.
21
+ */
22
+ export type ValueDeterminingMiddleware<T> = (request: Request, response: Response) => T | Promise<T>;
23
+ /**
24
+ * Express request handler that sends back a response when a client is
25
+ * rate-limited.
26
+ *
27
+ * @param request {Request} - The Express request object.
28
+ * @param response {Response} - The Express response object.
29
+ * @param next {NextFunction} - The Express `next` function, can be called to skip responding.
30
+ * @param optionsUsed {Options} - The options used to set up the middleware.
31
+ */
32
+ export type RateLimitExceededEventHandler = (request: Request, response: Response, next: NextFunction, optionsUsed: Options) => void;
33
+ /**
34
+ * Event callback that is triggered on a client's first request that exceeds the limit
35
+ * but not for subsequent requests. May be used for logging, etc. Should *not*
36
+ * send a response.
37
+ *
38
+ * @param request {Request} - The Express request object.
39
+ * @param response {Response} - The Express response object.
40
+ * @param optionsUsed {Options} - The options used to set up the middleware.
41
+ */
42
+ export type RateLimitReachedEventHandler = (request: Request, response: Response, optionsUsed: Options) => void;
43
+ /**
44
+ * Data returned from the `Store` when a client's hit counter is incremented.
45
+ *
46
+ * @property totalHits {number} - The number of hits for that client so far.
47
+ * @property resetTime {Date | undefined} - The time when the counter resets.
48
+ */
49
+ export type IncrementResponse = {
50
+ totalHits: number;
51
+ resetTime: Date | undefined;
52
+ };
53
+ /**
54
+ * A modified Express request handler with the rate limit functions.
55
+ */
56
+ export type RateLimitRequestHandler = RequestHandler & {
57
+ /**
58
+ * Method to reset a client's hit counter.
59
+ *
60
+ * @param key {string} - The identifier for a client.
61
+ */
62
+ resetKey: (key: string) => void;
63
+ };
64
+ /**
65
+ * An interface that all hit counter stores must implement.
66
+ *
67
+ * @deprecated 6.x - Implement the `Store` interface instead.
68
+ */
69
+ export type LegacyStore = {
70
+ /**
71
+ * Method to increment a client's hit counter.
72
+ *
73
+ * @param key {string} - The identifier for a client.
74
+ * @param callback {IncrementCallback} - The callback to call once the counter is incremented.
75
+ */
76
+ incr: (key: string, callback: IncrementCallback) => void;
77
+ /**
78
+ * Method to decrement a client's hit counter.
79
+ *
80
+ * @param key {string} - The identifier for a client.
81
+ */
82
+ decrement: (key: string) => void;
83
+ /**
84
+ * Method to reset a client's hit counter.
85
+ *
86
+ * @param key {string} - The identifier for a client.
87
+ */
88
+ resetKey: (key: string) => void;
89
+ /**
90
+ * Method to reset everyone's hit counter.
91
+ */
92
+ resetAll?: () => void;
93
+ };
94
+ /**
95
+ * An interface that all hit counter stores must implement.
96
+ */
97
+ export type Store = {
98
+ /**
99
+ * Method that initializes the store, and has access to the options passed to
100
+ * the middleware too.
101
+ *
102
+ * @param options {Options} - The options used to setup the middleware.
103
+ */
104
+ init?: (options: Options) => void;
105
+ /**
106
+ * Method to increment a client's hit counter.
107
+ *
108
+ * @param key {string} - The identifier for a client.
109
+ *
110
+ * @returns {IncrementResponse} - The number of hits and reset time for that client.
111
+ */
112
+ increment: (key: string) => Promise<IncrementResponse> | IncrementResponse;
113
+ /**
114
+ * Method to decrement a client's hit counter.
115
+ *
116
+ * @param key {string} - The identifier for a client.
117
+ */
118
+ decrement: (key: string) => Promise<void> | void;
119
+ /**
120
+ * Method to reset a client's hit counter.
121
+ *
122
+ * @param key {string} - The identifier for a client.
123
+ */
124
+ resetKey: (key: string) => Promise<void> | void;
125
+ /**
126
+ * Method to reset everyone's hit counter.
127
+ */
128
+ resetAll?: () => Promise<void> | void;
129
+ /**
130
+ * Method to shutdown the store, stop timers, and release all resources.
131
+ */
132
+ shutdown?: () => Promise<void> | void;
133
+ };
134
+ /**
135
+ * The configuration options for the rate limiter.
136
+ */
137
+ export type Options = {
138
+ /**
139
+ * How long we should remember the requests.
140
+ *
141
+ * Defaults to `60000` ms (= 1 minute).
142
+ */
143
+ windowMs: number;
144
+ /**
145
+ * The maximum number of connections to allow during the `window` before
146
+ * rate limiting the client.
147
+ *
148
+ * Can be the limit itself as a number or express middleware that parses
149
+ * the request and then figures out the limit.
150
+ *
151
+ * Defaults to `5`.
152
+ */
153
+ max: number | ValueDeterminingMiddleware<number>;
154
+ /**
155
+ * The response body to send back when a client is rate limited.
156
+ *
157
+ * Defaults to `'Too many requests, please try again later.'`
158
+ */
159
+ message: any | ValueDeterminingMiddleware<any>;
160
+ /**
161
+ * The HTTP status code to send back when a client is rate limited.
162
+ *
163
+ * Defaults to `HTTP 429 Too Many Requests` (RFC 6585).
164
+ */
165
+ statusCode: number;
166
+ /**
167
+ * Whether to send `X-RateLimit-*` headers with the rate limit and the number
168
+ * of requests.
169
+ *
170
+ * Defaults to `true` (for backward compatibility).
171
+ */
172
+ legacyHeaders: boolean;
173
+ /**
174
+ * Whether to enable support for the standardized rate limit headers (`RateLimit-*`).
175
+ *
176
+ * Defaults to `false` (for backward compatibility, but its use is recommended).
177
+ */
178
+ standardHeaders: boolean;
179
+ /**
180
+ * The name of the property on the request object to store the rate limit info.
181
+ *
182
+ * Defaults to `rateLimit`.
183
+ */
184
+ requestPropertyName: string;
185
+ /**
186
+ * If `true`, the library will (by default) skip all requests that have a 4XX
187
+ * or 5XX status.
188
+ *
189
+ * Defaults to `false`.
190
+ */
191
+ skipFailedRequests: boolean;
192
+ /**
193
+ * If `true`, the library will (by default) skip all requests that have a
194
+ * status code less than 400.
195
+ *
196
+ * Defaults to `false`.
197
+ */
198
+ skipSuccessfulRequests: boolean;
199
+ /**
200
+ * Method to generate custom identifiers for clients.
201
+ *
202
+ * By default, the client's IP address is used.
203
+ */
204
+ keyGenerator: ValueDeterminingMiddleware<string>;
205
+ /**
206
+ * Express request handler that sends back a response when a client is
207
+ * rate-limited.
208
+ *
209
+ * By default, sends back the `statusCode` and `message` set via the options.
210
+ */
211
+ handler: RateLimitExceededEventHandler;
212
+ /**
213
+ * Express request handler that sends back a response when a client has
214
+ * reached their rate limit, and will be rate limited on their next request.
215
+ *
216
+ * @deprecated 6.x - Please use a custom `handler` that checks the number of
217
+ * hits instead.
218
+ */
219
+ onLimitReached: RateLimitReachedEventHandler;
220
+ /**
221
+ * Method (in the form of middleware) to determine whether or not this request
222
+ * counts towards a client's quota.
223
+ *
224
+ * By default, skips no requests.
225
+ */
226
+ skip: ValueDeterminingMiddleware<boolean>;
227
+ /**
228
+ * Method to determine whether or not the request counts as 'succesful'. Used
229
+ * when either `skipSuccessfulRequests` or `skipFailedRequests` is set to true.
230
+ *
231
+ * By default, requests with a response status code less than 400 are considered
232
+ * successful.
233
+ */
234
+ requestWasSuccessful: ValueDeterminingMiddleware<boolean>;
235
+ /**
236
+ * The `Store` to use to store the hit count for each client.
237
+ *
238
+ * By default, the built-in `MemoryStore` will be used.
239
+ */
240
+ store: Store | LegacyStore;
241
+ /**
242
+ * Whether or not the validation checks should run.
243
+ */
244
+ validate: boolean;
245
+ /**
246
+ * Whether to send `X-RateLimit-*` headers with the rate limit and the number
247
+ * of requests.
248
+ *
249
+ * @deprecated 6.x - This option was renamed to `legacyHeaders`.
250
+ */
251
+ headers?: boolean;
252
+ /**
253
+ * Whether to send `RateLimit-*` headers with the rate limit and the number
254
+ * of requests.
255
+ *
256
+ * @deprecated 6.x - This option was renamed to `standardHeaders`.
257
+ */
258
+ draft_polli_ratelimit_headers?: boolean;
259
+ };
260
+ /**
261
+ * The extended request object that includes information about the client's
262
+ * rate limit.
263
+ */
264
+ export type AugmentedRequest = Request & {
265
+ [key: string]: RateLimitInfo;
266
+ };
267
+ /**
268
+ * The rate limit related information for each client included in the
269
+ * Express request object.
270
+ */
271
+ export type RateLimitInfo = {
272
+ limit: number;
273
+ current: number;
274
+ remaining: number;
275
+ resetTime: Date | undefined;
276
+ };
277
+ /**
278
+ *
279
+ * Create an instance of IP rate-limiting middleware for Express.
280
+ *
281
+ * @param passedOptions {Options} - Options to configure the rate limiter.
282
+ *
283
+ * @returns {RateLimitRequestHandler} - The middleware that rate-limits clients based on your configuration.
284
+ *
285
+ * @public
286
+ */
287
+ export declare const rateLimit: (passedOptions?: Partial<Options>) => RateLimitRequestHandler;
288
+ /**
289
+ * A `Store` that stores the hit count for each client in memory.
290
+ *
291
+ * @public
292
+ */
293
+ export declare class MemoryStore implements Store {
294
+ /**
295
+ * The duration of time before which all hit counts are reset (in milliseconds).
296
+ */
297
+ windowMs: number;
298
+ /**
299
+ * The map that stores the number of hits for each client in memory.
300
+ */
301
+ hits: {
302
+ [key: string]: number | undefined;
303
+ };
304
+ /**
305
+ * The time at which all hit counts will be reset.
306
+ */
307
+ resetTime: Date;
308
+ /**
309
+ * Reference to the active timer.
310
+ */
311
+ interval?: NodeJS.Timer;
312
+ /**
313
+ * Method that initializes the store.
314
+ *
315
+ * @param options {Options} - The options used to setup the middleware.
316
+ */
317
+ init(options: Options): void;
318
+ /**
319
+ * Method to increment a client's hit counter.
320
+ *
321
+ * @param key {string} - The identifier for a client.
322
+ *
323
+ * @returns {IncrementResponse} - The number of hits and reset time for that client.
324
+ *
325
+ * @public
326
+ */
327
+ increment(key: string): Promise<IncrementResponse>;
328
+ /**
329
+ * Method to decrement a client's hit counter.
330
+ *
331
+ * @param key {string} - The identifier for a client.
332
+ *
333
+ * @public
334
+ */
335
+ decrement(key: string): Promise<void>;
336
+ /**
337
+ * Method to reset a client's hit counter.
338
+ *
339
+ * @param key {string} - The identifier for a client.
340
+ *
341
+ * @public
342
+ */
343
+ resetKey(key: string): Promise<void>;
344
+ /**
345
+ * Method to reset everyone's hit counter.
346
+ *
347
+ * @public
348
+ */
349
+ resetAll(): Promise<void>;
350
+ /**
351
+ * Method to stop the timer (if currently running) and prevent any memory
352
+ * leaks.
353
+ *
354
+ * @public
355
+ */
356
+ shutdown(): void;
357
+ }
358
+
359
+ export {
360
+ rateLimit as default,
361
+ };
362
+
363
+ export {};