From f4073ff58a7b8972310b6197ca9d427065eeaa9f Mon Sep 17 00:00:00 2001 From: Austin Beer Date: Thu, 26 Mar 2020 05:28:31 -0400 Subject: [PATCH] =?UTF-8?q?Make=20RedisStore=20a=20class=20and=20expose=20?= =?UTF-8?q?RedisStoreOptions=20in=20rate-l=E2=80=A6=20(#43113)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Expose RedisStoreOptions in rate-limit-redis * Also refactor to expose RedisStore as a class instead of a variable * Rename RedisStore.RedisStoreOptions to RedisStore.Options so it is less redundant and is consistent with RateLimit.Options from express-rate-limit. --- types/rate-limit-redis/index.d.ts | 29 ++++++++++++------- .../rate-limit-redis-tests.ts | 17 +++++------ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/types/rate-limit-redis/index.d.ts b/types/rate-limit-redis/index.d.ts index 4b27c7edff..255fc582ad 100644 --- a/types/rate-limit-redis/index.d.ts +++ b/types/rate-limit-redis/index.d.ts @@ -8,18 +8,27 @@ import { RedisClient } from 'redis'; import IORedis = require('ioredis'); -import { Store } from 'express-rate-limit'; +import { Store, StoreIncrementCallback } from 'express-rate-limit'; -interface RedisStoreOptions { - expiry?: number; - prefix?: string; - resetExpiryOnChange?: boolean; - client?: RedisClient | IORedis.Redis; - redisURL?: string; +declare namespace RedisStore { + interface Options { + expiry?: number; + prefix?: string; + resetExpiryOnChange?: boolean; + client?: RedisClient | IORedis.Redis; + redisURL?: string; + } } -declare var RedisStore: { - new (options?: RedisStoreOptions): Store; -}; +declare class RedisStore implements Store { + constructor(options?: RedisStore.Options); + incr(key: string, cb: StoreIncrementCallback): void; + decrement(key: string): void; + resetKey(key: string): void; + // rate-limit-redis 1.7.0 doesn't actually implement resetAll() and + // express-rate-limit 5.1.1 doesn't actually call it, but it's required by + // the Store interface so it's included here. + resetAll(): void; +} export = RedisStore; diff --git a/types/rate-limit-redis/rate-limit-redis-tests.ts b/types/rate-limit-redis/rate-limit-redis-tests.ts index 1aa8778c08..7069f73249 100644 --- a/types/rate-limit-redis/rate-limit-redis-tests.ts +++ b/types/rate-limit-redis/rate-limit-redis-tests.ts @@ -1,39 +1,38 @@ import { RedisClient } from 'redis'; import IORedis = require('ioredis'); import RedisStore from 'rate-limit-redis'; -import { Store } from 'express-rate-limit'; -let store: Store; +let store: RedisStore; -// $ExpectType Store +// $ExpectType RedisStore store = new RedisStore(); -// $ExpectType Store +// $ExpectType RedisStore store = new RedisStore({ expiry: 1000, }); -// $ExpectType Store +// $ExpectType RedisStore store = new RedisStore({ prefix: 'types', }); -// $ExpectType Store +// $ExpectType RedisStore store = new RedisStore({ resetExpiryOnChange: false, }); -// $ExpectType Store +// $ExpectType RedisStore store = new RedisStore({ client: new RedisClient({}), }); -// $ExpectType Store +// $ExpectType RedisStore store = new RedisStore({ client: new IORedis({}), }); -// $ExpectType Store +// $ExpectType RedisStore store = new RedisStore({ redisURL: 'redis://localhost:6379', });