diff --git a/types/fast-ratelimit/fast-ratelimit-tests.ts b/types/fast-ratelimit/fast-ratelimit-tests.ts new file mode 100644 index 0000000000..2ca23b1fc9 --- /dev/null +++ b/types/fast-ratelimit/fast-ratelimit-tests.ts @@ -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 +consume.then(() => {}); // User can send message. +consume.catch(() => {}); // Use cannot send message. + +const hasToken = limit.hasToken(someNamespace); // $type: Promise +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 diff --git a/types/fast-ratelimit/index.d.ts b/types/fast-ratelimit/index.d.ts new file mode 100644 index 0000000000..5306b364b1 --- /dev/null +++ b/types/fast-ratelimit/index.d.ts @@ -0,0 +1,41 @@ +// Type definitions for fast-ratelimit 2.2 +// Project: https://github.com/valeriansaliou/node-fast-ratelimit +// Definitions by: Jørgen Vatle +// 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; + + /** + * FastRateLimit.prototype.hasToken + */ + hasToken(namespace: string): Promise; +} diff --git a/types/fast-ratelimit/tsconfig.json b/types/fast-ratelimit/tsconfig.json new file mode 100644 index 0000000000..cb8a2f5d3d --- /dev/null +++ b/types/fast-ratelimit/tsconfig.json @@ -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" + ] +} \ No newline at end of file diff --git a/types/fast-ratelimit/tslint.json b/types/fast-ratelimit/tslint.json new file mode 100644 index 0000000000..2750cc0197 --- /dev/null +++ b/types/fast-ratelimit/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } \ No newline at end of file