Skip to content

Commit df04dd8

Browse files
authoredApr 9, 2022
fix: Expose ChainableCommander and other types (#1560)
1 parent 4c433ec commit df04dd8

File tree

6 files changed

+88
-9
lines changed

6 files changed

+88
-9
lines changed
 

‎bin/template.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Callback } from "../types";
22

3-
type RedisKey = string | Buffer;
4-
type RedisValue = string | Buffer | number;
3+
export type RedisKey = string | Buffer;
4+
export type RedisValue = string | Buffer | number;
55

66
// Inspired by https://github.com/mmkal/handy-redis/blob/main/src/generated/interface.ts.
77
// Should be fixed with https://github.com/Microsoft/TypeScript/issues/1213

‎lib/cluster/ClusterOptions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import { NodeRole } from "./util";
66
export type DNSResolveSrvFunction = (
77
hostname: string,
88
callback: (
9-
err: NodeJS.ErrnoException | undefined,
9+
err: NodeJS.ErrnoException | null | undefined,
1010
records?: SrvRecord[]
1111
) => void
1212
) => void;
1313

1414
export type DNSLookupFunction = (
1515
hostname: string,
1616
callback: (
17-
err: NodeJS.ErrnoException | undefined,
17+
err: NodeJS.ErrnoException | null | undefined,
1818
address: string,
1919
family?: number
2020
) => void

‎lib/index.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,18 @@ export {
5454
export { StandaloneConnectionOptions } from "./connectors/StandaloneConnector";
5555
export { RedisOptions, CommonRedisOptions } from "./redis/RedisOptions";
5656
export { ClusterNode } from "./cluster";
57-
export { ClusterOptions } from "./cluster/ClusterOptions";
57+
export {
58+
ClusterOptions,
59+
DNSLookupFunction,
60+
DNSResolveSrvFunction,
61+
NatMap,
62+
} from "./cluster/ClusterOptions";
5863
export { NodeRole } from "./cluster/util";
64+
export type {
65+
RedisKey,
66+
RedisValue,
67+
ChainableCommander,
68+
} from "./utils/RedisCommander";
5969

6070
// No TS typings
6171
export const ReplyError = require("redis-errors").ReplyError;

‎lib/redis/RedisOptions.ts

+59
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,58 @@ export type ReconnectOnError = (err: Error) => boolean | 1 | 2;
88
export interface CommonRedisOptions extends CommanderOptions {
99
Connector?: ConnectorConstructor;
1010
retryStrategy?: (times: number) => number | void | null;
11+
12+
/**
13+
* If a command does not return a reply within a set number of milliseconds,
14+
* a "Command timed out" error will be thrown.
15+
*/
1116
commandTimeout?: number;
1217
/**
1318
* Enable/disable keep-alive functionality.
1419
* @link https://nodejs.org/api/net.html#socketsetkeepaliveenable-initialdelay
1520
* @default 0
1621
*/
1722
keepAlive?: number;
23+
1824
/**
1925
* Enable/disable the use of Nagle's algorithm.
2026
* @link https://nodejs.org/api/net.html#socketsetnodelaynodelay
2127
* @default true
2228
*/
2329
noDelay?: boolean;
30+
2431
/**
2532
* Set the name of the connection to make it easier to identity the connection
2633
* in client list.
2734
* @link https://redis.io/commands/client-setname
2835
*/
2936
connectionName?: string;
37+
38+
/**
39+
* If set, client will send AUTH command with the value of this option as the first argument when connected.
40+
* This is supported since Redis 6.
41+
*/
3042
username?: string;
43+
44+
/**
45+
* If set, client will send AUTH command with the value of this option when connected.
46+
*/
3147
password?: string;
48+
3249
/**
50+
* Database index to use.
51+
*
3352
* @default 0
3453
*/
3554
db?: number;
55+
3656
/**
57+
* When the client reconnects, channels subscribed in the previous connection will be
58+
* resubscribed automatically if `autoResubscribe` is `true`.
3759
* @default true
3860
*/
3961
autoResubscribe?: boolean;
62+
4063
/**
4164
* Whether or not to resend unfulfilled commands on reconnect.
4265
* Unfulfilled commands are most likely to be blocking commands such as `brpop` or `blpop`.
@@ -65,6 +88,7 @@ export interface CommonRedisOptions extends CommanderOptions {
6588
* @default null
6689
*/
6790
reconnectOnError?: ReconnectOnError | null;
91+
6892
/**
6993
* @default false
7094
*/
@@ -75,18 +99,33 @@ export interface CommonRedisOptions extends CommanderOptions {
7599
* @default false
76100
*/
77101
stringNumbers?: boolean;
102+
78103
/**
104+
* How long the client will wait before killing a socket due to inactivity during initial connection.
79105
* @default 10000
80106
*/
81107
connectTimeout?: number;
108+
82109
/**
110+
* This option is used internally when you call `redis.monitor()` to tell Redis
111+
* to enter the monitor mode when the connection is established.
112+
*
83113
* @default false
84114
*/
85115
monitor?: boolean;
116+
86117
/**
118+
* The commands that don't get a reply due to the connection to the server is lost are
119+
* put into a queue and will be resent on reconnect (if allowed by the `retryStrategy` option).
120+
* This option is used to configure how many reconnection attempts should be allowed before
121+
* the queue is flushed with a `MaxRetriesPerRequestError` error.
122+
* Set this options to `null` instead of a number to let commands wait forever
123+
* until the connection is alive again.
124+
*
87125
* @default 20
88126
*/
89127
maxRetriesPerRequest?: number | null;
128+
90129
/**
91130
* @default 10000
92131
*/
@@ -101,18 +140,38 @@ export interface CommonRedisOptions extends CommanderOptions {
101140
autoPipeliningIgnoredCommands?: string[];
102141
offlineQueue?: boolean;
103142
commandQueue?: boolean;
143+
104144
/**
145+
*
146+
* By default, if the connection to Redis server has not been established, commands are added to a queue
147+
* and are executed once the connection is "ready" (when `enableReadyCheck` is true, "ready" means
148+
* the Redis server has loaded the database from disk, otherwise means the connection to the Redis
149+
* server has been established). If this option is false, when execute the command when the connection
150+
* isn't ready, an error will be returned.
151+
*
105152
* @default true
106153
*/
107154
enableOfflineQueue?: boolean;
155+
108156
/**
157+
* The client will sent an INFO command to check whether the server is still loading data from the disk (
158+
* which happens when the server is just launched) when the connection is established, and only wait until
159+
* the loading process is finished before emitting the `ready` event.
160+
*
109161
* @default true
110162
*/
111163
enableReadyCheck?: boolean;
164+
112165
/**
166+
* When a Redis instance is initialized, a connection to the server is immediately established. Set this to
167+
* true will delay the connection to the server until the first command is sent or `redis.connect()` is called
168+
* explicitly.
169+
*
113170
* @default false
114171
*/
172+
115173
lazyConnect?: boolean;
174+
116175
/**
117176
* @default undefined
118177
*/

‎lib/utils/RedisCommander.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Callback } from "../types";
22

3-
type RedisKey = string | Buffer;
4-
type RedisValue = string | Buffer | number;
3+
export type RedisKey = string | Buffer;
4+
export type RedisValue = string | Buffer | number;
55

66
// Inspired by https://github.com/mmkal/handy-redis/blob/main/src/generated/interface.ts.
77
// Should be fixed with https://github.com/Microsoft/TypeScript/issues/1213

‎test/typing/options.test-d.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { expectType } from "tsd";
2-
import Redis, { Cluster } from "../../built";
1+
import { expectAssignable, expectType } from "tsd";
2+
import Redis, { Cluster, NatMap, DNSLookupFunction } from "../../built";
33

44
expectType<Redis>(new Redis());
55

@@ -39,3 +39,13 @@ expectType<Cluster>(
3939
enableAutoPipelining: true,
4040
})
4141
);
42+
43+
expectAssignable<NatMap>({
44+
"10.0.1.230:30001": { host: "203.0.113.73", port: 30001 },
45+
"10.0.1.231:30001": { host: "203.0.113.73", port: 30002 },
46+
"10.0.1.232:30001": { host: "203.0.113.73", port: 30003 },
47+
});
48+
49+
expectAssignable<DNSLookupFunction>((address, callback) =>
50+
callback(null, address)
51+
);

0 commit comments

Comments
 (0)
Please sign in to comment.