Skip to content

Commit

Permalink
fix: Expose ChainableCommander and other types (#1560)
Browse files Browse the repository at this point in the history
  • Loading branch information
luin authored Apr 9, 2022
1 parent 4c433ec commit df04dd8
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 9 deletions.
4 changes: 2 additions & 2 deletions bin/template.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Callback } from "../types";

type RedisKey = string | Buffer;
type RedisValue = string | Buffer | number;
export type RedisKey = string | Buffer;
export type RedisValue = string | Buffer | number;

// Inspired by https://github.com/mmkal/handy-redis/blob/main/src/generated/interface.ts.
// Should be fixed with https://github.com/Microsoft/TypeScript/issues/1213
Expand Down
4 changes: 2 additions & 2 deletions lib/cluster/ClusterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { NodeRole } from "./util";
export type DNSResolveSrvFunction = (
hostname: string,
callback: (
err: NodeJS.ErrnoException | undefined,
err: NodeJS.ErrnoException | null | undefined,
records?: SrvRecord[]
) => void
) => void;

export type DNSLookupFunction = (
hostname: string,
callback: (
err: NodeJS.ErrnoException | undefined,
err: NodeJS.ErrnoException | null | undefined,
address: string,
family?: number
) => void
Expand Down
12 changes: 11 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,18 @@ export {
export { StandaloneConnectionOptions } from "./connectors/StandaloneConnector";
export { RedisOptions, CommonRedisOptions } from "./redis/RedisOptions";
export { ClusterNode } from "./cluster";
export { ClusterOptions } from "./cluster/ClusterOptions";
export {
ClusterOptions,
DNSLookupFunction,
DNSResolveSrvFunction,
NatMap,
} from "./cluster/ClusterOptions";
export { NodeRole } from "./cluster/util";
export type {
RedisKey,
RedisValue,
ChainableCommander,
} from "./utils/RedisCommander";

// No TS typings
export const ReplyError = require("redis-errors").ReplyError;
Expand Down
59 changes: 59 additions & 0 deletions lib/redis/RedisOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,58 @@ export type ReconnectOnError = (err: Error) => boolean | 1 | 2;
export interface CommonRedisOptions extends CommanderOptions {
Connector?: ConnectorConstructor;
retryStrategy?: (times: number) => number | void | null;

/**
* If a command does not return a reply within a set number of milliseconds,
* a "Command timed out" error will be thrown.
*/
commandTimeout?: number;
/**
* Enable/disable keep-alive functionality.
* @link https://nodejs.org/api/net.html#socketsetkeepaliveenable-initialdelay
* @default 0
*/
keepAlive?: number;

/**
* Enable/disable the use of Nagle's algorithm.
* @link https://nodejs.org/api/net.html#socketsetnodelaynodelay
* @default true
*/
noDelay?: boolean;

/**
* Set the name of the connection to make it easier to identity the connection
* in client list.
* @link https://redis.io/commands/client-setname
*/
connectionName?: string;

/**
* If set, client will send AUTH command with the value of this option as the first argument when connected.
* This is supported since Redis 6.
*/
username?: string;

/**
* If set, client will send AUTH command with the value of this option when connected.
*/
password?: string;

/**
* Database index to use.
*
* @default 0
*/
db?: number;

/**
* When the client reconnects, channels subscribed in the previous connection will be
* resubscribed automatically if `autoResubscribe` is `true`.
* @default true
*/
autoResubscribe?: boolean;

/**
* Whether or not to resend unfulfilled commands on reconnect.
* Unfulfilled commands are most likely to be blocking commands such as `brpop` or `blpop`.
Expand Down Expand Up @@ -65,6 +88,7 @@ export interface CommonRedisOptions extends CommanderOptions {
* @default null
*/
reconnectOnError?: ReconnectOnError | null;

/**
* @default false
*/
Expand All @@ -75,18 +99,33 @@ export interface CommonRedisOptions extends CommanderOptions {
* @default false
*/
stringNumbers?: boolean;

/**
* How long the client will wait before killing a socket due to inactivity during initial connection.
* @default 10000
*/
connectTimeout?: number;

/**
* This option is used internally when you call `redis.monitor()` to tell Redis
* to enter the monitor mode when the connection is established.
*
* @default false
*/
monitor?: boolean;

/**
* The commands that don't get a reply due to the connection to the server is lost are
* put into a queue and will be resent on reconnect (if allowed by the `retryStrategy` option).
* This option is used to configure how many reconnection attempts should be allowed before
* the queue is flushed with a `MaxRetriesPerRequestError` error.
* Set this options to `null` instead of a number to let commands wait forever
* until the connection is alive again.
*
* @default 20
*/
maxRetriesPerRequest?: number | null;

/**
* @default 10000
*/
Expand All @@ -101,18 +140,38 @@ export interface CommonRedisOptions extends CommanderOptions {
autoPipeliningIgnoredCommands?: string[];
offlineQueue?: boolean;
commandQueue?: boolean;

/**
*
* By default, if the connection to Redis server has not been established, commands are added to a queue
* and are executed once the connection is "ready" (when `enableReadyCheck` is true, "ready" means
* the Redis server has loaded the database from disk, otherwise means the connection to the Redis
* server has been established). If this option is false, when execute the command when the connection
* isn't ready, an error will be returned.
*
* @default true
*/
enableOfflineQueue?: boolean;

/**
* The client will sent an INFO command to check whether the server is still loading data from the disk (
* which happens when the server is just launched) when the connection is established, and only wait until
* the loading process is finished before emitting the `ready` event.
*
* @default true
*/
enableReadyCheck?: boolean;

/**
* When a Redis instance is initialized, a connection to the server is immediately established. Set this to
* true will delay the connection to the server until the first command is sent or `redis.connect()` is called
* explicitly.
*
* @default false
*/

lazyConnect?: boolean;

/**
* @default undefined
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/RedisCommander.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Callback } from "../types";

type RedisKey = string | Buffer;
type RedisValue = string | Buffer | number;
export type RedisKey = string | Buffer;
export type RedisValue = string | Buffer | number;

// Inspired by https://github.com/mmkal/handy-redis/blob/main/src/generated/interface.ts.
// Should be fixed with https://github.com/Microsoft/TypeScript/issues/1213
Expand Down
14 changes: 12 additions & 2 deletions test/typing/options.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectType } from "tsd";
import Redis, { Cluster } from "../../built";
import { expectAssignable, expectType } from "tsd";
import Redis, { Cluster, NatMap, DNSLookupFunction } from "../../built";

expectType<Redis>(new Redis());

Expand Down Expand Up @@ -39,3 +39,13 @@ expectType<Cluster>(
enableAutoPipelining: true,
})
);

expectAssignable<NatMap>({
"10.0.1.230:30001": { host: "203.0.113.73", port: 30001 },
"10.0.1.231:30001": { host: "203.0.113.73", port: 30002 },
"10.0.1.232:30001": { host: "203.0.113.73", port: 30003 },
});

expectAssignable<DNSLookupFunction>((address, callback) =>
callback(null, address)
);

0 comments on commit df04dd8

Please sign in to comment.