express-rate-limit
Advanced tools
Comparing version 0.0.0-typescript-beta-6 to 0.0.0-typescript-beta-7
import express = require("express"); | ||
declare namespace RateLimit { | ||
type IncrCallback = (error: any, hit: number, resetTime: Date) => void; | ||
type IncrCallback = (error: any, hit: number, resetTime: Date | undefined) => void; | ||
interface Store { | ||
@@ -11,29 +11,31 @@ incr(key: string, cb: IncrCallback): void; | ||
interface Options { | ||
windowMs: number; | ||
max: number | ((req: express.Request, res: express.Response) => number); | ||
message: any; | ||
statusCode: number; | ||
headers: boolean; | ||
skipFailedRequests: boolean; | ||
skipSuccessfulRequests: boolean; | ||
keyGenerator: (req: express.Request, res: express.Response) => string; | ||
skip: (req: express.Request, res: express.Response) => boolean; | ||
handler: express.RequestHandler; | ||
onLimitReached: (req: express.Request, res: express.Response, optionsUsed: Options) => void; | ||
store: Store; | ||
readonly windowMs: number; | ||
readonly max: number | ((req: express.Request, res: express.Response) => number); | ||
readonly message: any; | ||
readonly statusCode: number; | ||
readonly headers: boolean; | ||
readonly skipFailedRequests: boolean; | ||
readonly skipSuccessfulRequests: boolean; | ||
readonly keyGenerator: (req: express.Request, res: express.Response) => string; | ||
readonly skip: (req: express.Request, res: express.Response) => boolean; | ||
readonly handler: express.RequestHandler; | ||
readonly onLimitReached: (req: express.Request, res: express.Response, optionsUsed: Options) => void; | ||
readonly store: Store; | ||
} | ||
interface AugmentedRequest extends express.Request { | ||
rateLimit: { | ||
limit: number; | ||
current: number; | ||
remaining: number; | ||
resetTime?: Date; | ||
}; | ||
rateLimit: RateLimitInfo; | ||
} | ||
interface RateLimitInfo { | ||
readonly limit: number; | ||
readonly current: number; | ||
readonly remaining: number; | ||
readonly resetTime: Date | undefined; | ||
} | ||
} | ||
declare type RateLimit = express.RequestHandler & { | ||
resetKey: RateLimit.Store["resetKey"]; | ||
resetIp: RateLimit.Store["resetKey"]; | ||
readonly resetKey: RateLimit.Store["resetKey"]; | ||
readonly resetIp: RateLimit.Store["resetKey"]; | ||
readonly getRateLimit: (req: express.Request) => RateLimit.RateLimitInfo | undefined; | ||
}; | ||
declare function RateLimit(incomingOptions: Partial<RateLimit.Options>): RateLimit; | ||
export = RateLimit; |
@@ -113,5 +113,17 @@ "use strict"; | ||
rateLimit.resetIp = rateLimit.resetKey; | ||
rateLimit.getRateLimit = function getRateLimit(req) { | ||
return isAugmentedRequest(req) ? req.rateLimit : undefined; | ||
}; | ||
return rateLimit; | ||
} | ||
function isAugmentedRequest(input) { | ||
var asAny = input; | ||
return ("rateLimit" in asAny && | ||
typeof asAny.rateLimit.limit === "number" && | ||
typeof asAny.rateLimit.current === "number" && | ||
typeof asAny.rateLimit.remaining === "number" && | ||
(asAny.rateLimit.resetTime === undefined || | ||
asAny.rateLimit.resetTime instanceof Date)); | ||
} | ||
module.exports = RateLimit; | ||
//# sourceMappingURL=express-rate-limit.js.map |
{ | ||
"name": "express-rate-limit", | ||
"version": "0.0.0-typescript-beta-6", | ||
"version": "0.0.0-typescript-beta-7", | ||
"description": "Basic IP rate-limiting middleware for Express. Use to limit repeated requests to public APIs and/or endpoints such as password reset.", | ||
@@ -40,2 +40,3 @@ "homepage": "https://github.com/nfriedly/express-rate-limit", | ||
"@types/node": "^12.0.12", | ||
"@types/supertest": "^2.0.8", | ||
"@typescript-eslint/parser": "^1.11.0", | ||
@@ -57,3 +58,3 @@ "eslint": "^6.0.1", | ||
"pretest": "npm run build", | ||
"test": "eslint . && mocha --require ts-node/register test/*-test.*", | ||
"test": "eslint . && TS_NODE_PROJECT=test/tsconfig.json mocha --require ts-node/register test/*-test.*", | ||
"precommit": "pretty-quick --staged", | ||
@@ -60,0 +61,0 @@ "prepublish": "npm run build" |
@@ -102,2 +102,12 @@ # Express Rate Limit | ||
### Typescript | ||
Import the module like this: | ||
```ts | ||
import RateLimit = require("express-rate-limit"); | ||
``` | ||
Notice the mixture of `import` and `require`. This is typescript's way of importing modules that use the older commonjs default export style. | ||
## Request API | ||
@@ -107,2 +117,4 @@ | ||
There is also a helper function `getRateLimit` on the default export, that safely returns the current rateLimit property from a request if it is availble. This is meant for typescript users mostly, since the express request type is not able to tell whether the rate limit is actually available. | ||
## Configuration options | ||
@@ -109,0 +121,0 @@ |
Sorry, the diff of this file is not supported yet
35599
220
309
16