Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Required if collection hasn't been set.
* **expireTimeMs**: integer -- time period, in milliseconds, after which record will be reset (deleted).
Defaults to `60 * 1000`. Notice that current implementation uses on mongodb ttl indexes - background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task. See [mongodb ttl indexes doc](https://docs.mongodb.com/v3.6/core/index-ttl/#timing-of-the-delete-operation) for more information.

Note: unless express-tate-limit's headers are disabled, `windowMs` on express-tate-limit's options should be set to the same value as `expireTimeMs` on rate-limit-mongo's options in order for the `Retry-After` header to be correct.
Note: unless express-rate-limit's headers are disabled, `windowMs` on express-rate-limit's options should be set to the same value as `expireTimeMs` on rate-limit-mongo's options in order for the `Retry-After` header to be correct.

* **resetExpireDateOnChange**: boolean -- indicates whether expireDate should be reset when changed or not.
Defaults to `false`.
Expand All @@ -84,7 +84,7 @@ required by [express-rate-limit](https://github.com/nfriedly/express-rate-limit)
In addition following methods provided:

* `getClient(callback)` - if `collection` was not passed to the constructor then
that method will pass (as second argument) initiated instace of
that method will pass (as second argument) initiated instance of
[MongoClient](http://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html)
to the `callback`, otherwise `null` will be passed. Thus this method provides
control over connection initiated by the library to the end user. This method
Expand Down
118 changes: 118 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { MongoClient, MongoClientOptions, Collection } from 'mongodb';

declare module 'rate-limit-mongo' {

export default class MongoStore {

constructor(options: Partial<MongoStoreOptions>);

/**
* Increments the value in the underlying store for the given key.
* @param key The key to use as the unique identifier passed down from RateLimit.
* @param cb he callback issued when the underlying store is finished.
*
* The callback should be called with three values:
* - `error` (usually null)
* - `hitCount` for this IP
* - `resetTime` - JS Date object (optional, but necessary for X-RateLimit-Reset header)
*/
incr(key: string, cb: (error: any, hitCount: number, resetTime: Date) => void): void;

/**
* Decrements the value in the underlying store for the given key. Used only when skipFailedRequests is true
* @param key The key to use as the unique identifier passed down from RateLimit.
*/
decrement(key: string): void;

/**
* Resets a value with the given key.
* @param key The key to reset.
*/
resetKey(key: string): void;

/**
* Resets every value
*/
resetAll(): void;

/**
* if `collection` was not passed to the constructor then that method will pass (as second argument) initiated instance of
* [MongoClient](http://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html)
* to the `callback`, otherwise `null` will be passed.
* Thus this method provides control over connection initiated by the library to the end user.
* This method is promisified (when `util.promisify` is presented (node.js >= 8)).
* @param callback
*/
getClient(callback: (collection: null, client: MongoClient | null) => any): void;

}

export interface MongoStoreOptions {

/**
* uri for connecting to mongodb, `mongodb://127.0.0.1:27017/test_db` for example. Required if collection hasn't been set.
*/
uri: string;

/**
* name of collection for storing records. Defaults to `expressRateRecords`
*/
collectionName: string;

/**
* username for authentication in mongodb
*/
user: string;

/**
* password for authentication in mongodb
*/
password: string;

/**
* db name against which authenticate use. If not set db name from uri will be taken.
*/
authSource: string;

/**
* mongodb collection instance. Required if uri hasn't been set.
*/
collection: Collection;

/**
* mongodb connection options. Allows to pass additional connection options to mongodb.
* The default connection options are `useUnifiedTopology: true`, `useNewUrlParser: true`.
*/
connectionOptions: MongoClientOptions;

/**
* time period, in milliseconds, after which record will be reset (deleted).
* Defaults to `60 * 1000`.
* Notice that current implementation uses on mongodb ttl indexes - background task that removes expired documents runs every 60 seconds.
* As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task.
* See [mongodb ttl indexes doc](https://docs.mongodb.com/v3.6/core/index-ttl/#timing-of-the-delete-operation) for more information.
*/
expireTimeMs: number;

/**
* indicates whether expireDate should be reset when changed or not.
* Defaults to `false`.
*/
resetExpireDateOnChange: boolean;

/**
* function that will be called if error happened during incr, decrement or resetKey methods. Defaults to `_.noop`.
*/
errorHandler: (err: any) => any;

/**
* defines whether create ttl index (on `expirationDate` field with `expireAfterSeconds: 0`) on collection or not.
* Could be useful in situations when you don't want to create index from the app e.g.
* due to restricted db permissions (see [#15](https://github.com/2do2go/rate-limit-mongo/issues/15) for details).
* Defaults to `true`.
*/
createTtlIndex: boolean;

}

}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": "2do2go team <[email protected]>",
"license": "MIT",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"repository": {
"type": "git",
"url": "[email protected]:2do2go/rate-limit-mongo.git"
Expand Down