Merge pull request #34481 from JorgenVatle/master

[fast-ratelimit] Add type definitions for fast-ratelimit
This commit is contained in:
Daniel Rosenwasser
2019-04-04 19:15:13 -04:00
committed by GitHub
4 changed files with 87 additions and 0 deletions
@@ -0,0 +1,22 @@
import { FastRateLimit } from 'fast-ratelimit';
const limit = new FastRateLimit({ // $type: FastRateLimit
threshold: 20,
ttl: 60,
});
const someNamespace = 'some-namespace';
const consume = limit.consume(someNamespace); // $type: Promise<void>
consume.then(() => {}); // User can send message.
consume.catch(() => {}); // Use cannot send message.
const hasToken = limit.hasToken(someNamespace); // $type: Promise<void>
hasToken.then(() => {}); // User has remaining token.
hasToken.catch(() => {}); // User does not have remaining token.
// Synchronously check if user is allowed to send message.
const consumeSync = limit.consumeSync(someNamespace); // $type: boolean
// Synchronously check if user has remaining token.
const hasTokenSync = limit.hasTokenSync(someNamespace); // $type: boolean
+41
View File
@@ -0,0 +1,41 @@
// Type definitions for fast-ratelimit 2.2
// Project: https://github.com/valeriansaliou/node-fast-ratelimit
// Definitions by: Jørgen Vatle <https://github.com/JorgenVatle>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/**
* Constructor options for fast-ratelimit.
* {@link https://github.com/valeriansaliou/node-fast-ratelimit#1-create-the-rate-limiter}
*/
export interface FastRateLimitOptions {
threshold: number; // available tokens over timespan
ttl: number; // time-to-live value of token bucket (in seconds)
}
export class FastRateLimit {
/**
* FastRateLimit constructor
* @param options
*/
constructor(options: FastRateLimitOptions);
/**
* FastRateLimit.prototype.consumeSync
*/
consumeSync(namespace: string): boolean;
/**
* FastRateLimit.prototype.hasTokenSync
*/
hasTokenSync(namespace: string): boolean;
/**
* FastRateLimit.prototype.consume
*/
consume(namespace: string): Promise<void>;
/**
* FastRateLimit.prototype.hasToken
*/
hasToken(namespace: string): Promise<void>;
}
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"fast-ratelimit-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }