Make RedisStore a class and expose RedisStoreOptions in rate-l… (#43113)

* 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.
This commit is contained in:
Austin Beer 2020-03-26 05:28:31 -04:00 committed by GitHub
parent f377d05079
commit f4073ff58a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 19 deletions

View File

@ -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;

View File

@ -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',
});