From 767db4f62e34e8122010e6fc5d5319422b213d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 05:12:31 +0300 Subject: [PATCH 01/13] Add fast-ratelimit types. --- index.d.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000000..4dc1a9f358 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,58 @@ +// Type definitions for fast-ratelimit +// Project: https://github.com/valeriansaliou/node-fast-ratelimit +// Definitions by: Jørgen Vatle +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module 'fast-ratelimit' { + + /** + * 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) + } + + /** + * FastRateLimit + * @class + * @classdesc Instanciates a new rate-limiter + * @param {object} options + */ + export class FastRateLimit { + constructor(options: FastRateLimitOptions); + + /** + * FastRateLimit.prototype.consumeSync + * @public + * @param {string} namespace + * @return {boolean} Whether tokens remain in current timespan or not + */ + consumeSync(namespace: string): boolean; + + /** + * FastRateLimit.prototype.hasTokenSync + * @public + * @param {string} namespace + * @return {boolean} Whether tokens remain in current timespan or not + */ + hasTokenSync(namespace: string): boolean; + + /** + * FastRateLimit.prototype.consume + * @public + * @param {string} namespace + * @return {object} Promise object + */ + consume(namespace: string): Promise + + /** + * FastRateLimit.prototype.hasToken + * @public + * @param {string} namespace + * @return {object} Promise object + */ + hasToken(namespace: string): Promise + } +} \ No newline at end of file From d931f132d195981f0764744e0b129992e3481619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 05:13:52 +0300 Subject: [PATCH 02/13] Remove module declaration for fast-ratelimit. --- index.d.ts | 85 ++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/index.d.ts b/index.d.ts index 4dc1a9f358..476944a71d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,56 +3,53 @@ // Definitions by: Jørgen Vatle // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare module 'fast-ratelimit' { +/** + * 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) +} + +/** + * FastRateLimit + * @class + * @classdesc Instanciates a new rate-limiter + * @param {object} options + */ +export class FastRateLimit { + constructor(options: FastRateLimitOptions); /** - * Constructor options for fast-ratelimit. - * {@link https://github.com/valeriansaliou/node-fast-ratelimit#1-create-the-rate-limiter} + * FastRateLimit.prototype.consumeSync + * @public + * @param {string} namespace + * @return {boolean} Whether tokens remain in current timespan or not */ - export interface FastRateLimitOptions { - threshold: number; // available tokens over timespan - ttl: number; // time-to-live value of token bucket (in seconds) - } + consumeSync(namespace: string): boolean; /** - * FastRateLimit - * @class - * @classdesc Instanciates a new rate-limiter - * @param {object} options + * FastRateLimit.prototype.hasTokenSync + * @public + * @param {string} namespace + * @return {boolean} Whether tokens remain in current timespan or not */ - export class FastRateLimit { - constructor(options: FastRateLimitOptions); + hasTokenSync(namespace: string): boolean; - /** - * FastRateLimit.prototype.consumeSync - * @public - * @param {string} namespace - * @return {boolean} Whether tokens remain in current timespan or not - */ - consumeSync(namespace: string): boolean; + /** + * FastRateLimit.prototype.consume + * @public + * @param {string} namespace + * @return {object} Promise object + */ + consume(namespace: string): Promise - /** - * FastRateLimit.prototype.hasTokenSync - * @public - * @param {string} namespace - * @return {boolean} Whether tokens remain in current timespan or not - */ - hasTokenSync(namespace: string): boolean; - - /** - * FastRateLimit.prototype.consume - * @public - * @param {string} namespace - * @return {object} Promise object - */ - consume(namespace: string): Promise - - /** - * FastRateLimit.prototype.hasToken - * @public - * @param {string} namespace - * @return {object} Promise object - */ - hasToken(namespace: string): Promise - } + /** + * FastRateLimit.prototype.hasToken + * @public + * @param {string} namespace + * @return {object} Promise object + */ + hasToken(namespace: string): Promise } \ No newline at end of file From ee6ca7910bd8fef45816a8bda28dfa3afae5283a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 05:15:16 +0300 Subject: [PATCH 03/13] Add JSDoc to constructor for fast-ratelimit. --- index.d.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.d.ts b/index.d.ts index 476944a71d..16fae01ffc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,6 +19,10 @@ export interface FastRateLimitOptions { * @param {object} options */ export class FastRateLimit { + /** + * FastRateLimit constructor + * @param options + */ constructor(options: FastRateLimitOptions); /** From 09e75f138771d57a09e5c233b640092fc72284bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 05:16:33 +0300 Subject: [PATCH 04/13] Remove redundant JSDoc from class in fast-ratelimit. --- index.d.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 16fae01ffc..b6a586b7ea 100644 --- a/index.d.ts +++ b/index.d.ts @@ -12,12 +12,6 @@ export interface FastRateLimitOptions { ttl: number; // time-to-live value of token bucket (in seconds) } -/** - * FastRateLimit - * @class - * @classdesc Instanciates a new rate-limiter - * @param {object} options - */ export class FastRateLimit { /** * FastRateLimit constructor From 9e0eb0b225caf034c3a918b41c6e7581cce1b0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 18:39:45 +0300 Subject: [PATCH 05/13] Borrow tsconfig from `accept` https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/accept/tsconfig.json --- tsconfig.json | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tsconfig.json diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000000..cb8a2f5d3d --- /dev/null +++ b/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 From d32055c33ff8792f8a40e30087b3c6944bca7728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 19:05:18 +0300 Subject: [PATCH 06/13] Move types to fast-ratelimit 'types' directory. --- index.d.ts => types/fast-ratelimit/index.d.ts | 0 tsconfig.json => types/fast-ratelimit/tsconfig.json | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename index.d.ts => types/fast-ratelimit/index.d.ts (100%) rename tsconfig.json => types/fast-ratelimit/tsconfig.json (100%) diff --git a/index.d.ts b/types/fast-ratelimit/index.d.ts similarity index 100% rename from index.d.ts rename to types/fast-ratelimit/index.d.ts diff --git a/tsconfig.json b/types/fast-ratelimit/tsconfig.json similarity index 100% rename from tsconfig.json rename to types/fast-ratelimit/tsconfig.json From 74c46005e83c1ce823144856a9657dd14f78a799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 19:06:03 +0300 Subject: [PATCH 07/13] Add fast-ratelimit tests. --- types/fast-ratelimit/fast-ratelimit-tests.ts | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 types/fast-ratelimit/fast-ratelimit-tests.ts diff --git a/types/fast-ratelimit/fast-ratelimit-tests.ts b/types/fast-ratelimit/fast-ratelimit-tests.ts new file mode 100644 index 0000000000..c0e8793a2f --- /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 +consume.then(() => {}); // User has remaining token. +consume.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 \ No newline at end of file From 5741a4ff4ebf7c1a92f6374ba4334540beddba9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 19:07:04 +0300 Subject: [PATCH 08/13] Correct expected promise type in fast-ratelimit test comments. --- types/fast-ratelimit/fast-ratelimit-tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/fast-ratelimit/fast-ratelimit-tests.ts b/types/fast-ratelimit/fast-ratelimit-tests.ts index c0e8793a2f..d5c7467b11 100644 --- a/types/fast-ratelimit/fast-ratelimit-tests.ts +++ b/types/fast-ratelimit/fast-ratelimit-tests.ts @@ -7,11 +7,11 @@ const limit = new FastRateLimit({ // $type: FastRateLimit const someNamespace = 'some-namespace'; -const consume = limit.consume(someNamespace); // $type: Promise +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 +const hasToken = limit.hasToken(someNamespace); // $type: Promise consume.then(() => {}); // User has remaining token. consume.catch(() => {}); // User does not have remaining token. From e84bc517b05ab3b9f44e9761e1b973dc76c3c1de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 19:16:18 +0300 Subject: [PATCH 09/13] Add tslint.json to fast-ratelimit. --- types/fast-ratelimit/tslint.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 types/fast-ratelimit/tslint.json 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 From b752bb758868b5f82d74234befa82d6eb66a8bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 23:27:46 +0300 Subject: [PATCH 10/13] Correct linting issues with fast-ratelimit index.d.ts. --- types/fast-ratelimit/index.d.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/types/fast-ratelimit/index.d.ts b/types/fast-ratelimit/index.d.ts index b6a586b7ea..b9962d5b57 100644 --- a/types/fast-ratelimit/index.d.ts +++ b/types/fast-ratelimit/index.d.ts @@ -21,33 +21,21 @@ export class FastRateLimit { /** * FastRateLimit.prototype.consumeSync - * @public - * @param {string} namespace - * @return {boolean} Whether tokens remain in current timespan or not */ consumeSync(namespace: string): boolean; /** * FastRateLimit.prototype.hasTokenSync - * @public - * @param {string} namespace - * @return {boolean} Whether tokens remain in current timespan or not */ hasTokenSync(namespace: string): boolean; /** * FastRateLimit.prototype.consume - * @public - * @param {string} namespace - * @return {object} Promise object */ - consume(namespace: string): Promise + consume(namespace: string): Promise; /** * FastRateLimit.prototype.hasToken - * @public - * @param {string} namespace - * @return {object} Promise object */ - hasToken(namespace: string): Promise -} \ No newline at end of file + hasToken(namespace: string): Promise; +} From 541ca6975bea8a81472d9cf482a407435a1567ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 23:28:07 +0300 Subject: [PATCH 11/13] Add version to fast-ratelimit index.d.ts. --- types/fast-ratelimit/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/fast-ratelimit/index.d.ts b/types/fast-ratelimit/index.d.ts index b9962d5b57..5306b364b1 100644 --- a/types/fast-ratelimit/index.d.ts +++ b/types/fast-ratelimit/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for fast-ratelimit +// 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 From aebc9371057e65c60624d1e47ad24f45b3c2ef27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 23:28:48 +0300 Subject: [PATCH 12/13] Correct linting issues with fast-ratelimit-tests.ts. --- types/fast-ratelimit/fast-ratelimit-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/fast-ratelimit/fast-ratelimit-tests.ts b/types/fast-ratelimit/fast-ratelimit-tests.ts index d5c7467b11..2ff30cd999 100644 --- a/types/fast-ratelimit/fast-ratelimit-tests.ts +++ b/types/fast-ratelimit/fast-ratelimit-tests.ts @@ -19,4 +19,4 @@ consume.catch(() => {}); // User does not have remaining token. const consumeSync = limit.consumeSync(someNamespace); // $type: boolean // Synchronously check if user has remaining token. -const hasTokenSync = limit.hasTokenSync(someNamespace); // $type: boolean \ No newline at end of file +const hasTokenSync = limit.hasTokenSync(someNamespace); // $type: boolean From 225e921aec30352e80350051296ffee2bed1d906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 4 Apr 2019 23:31:15 +0300 Subject: [PATCH 13/13] Correct hasToken tests in fast-ratelimit-tests.ts. --- types/fast-ratelimit/fast-ratelimit-tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/fast-ratelimit/fast-ratelimit-tests.ts b/types/fast-ratelimit/fast-ratelimit-tests.ts index 2ff30cd999..2ca23b1fc9 100644 --- a/types/fast-ratelimit/fast-ratelimit-tests.ts +++ b/types/fast-ratelimit/fast-ratelimit-tests.ts @@ -12,8 +12,8 @@ consume.then(() => {}); // User can send message. consume.catch(() => {}); // Use cannot send message. const hasToken = limit.hasToken(someNamespace); // $type: Promise -consume.then(() => {}); // User has remaining token. -consume.catch(() => {}); // User does not have remaining token. +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