Skip to content

Commit 176db18

Browse files
authored
feat: add SetReply (#459)
1 parent 651282c commit 176db18

File tree

3 files changed

+17
-58
lines changed

3 files changed

+17
-58
lines changed

command.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ export interface SetOpts {
141141
*/
142142
get?: boolean;
143143
}
144+
/** Return type for {@linkcode RedisCommands.set} */
145+
export type SetReply<T extends SetOpts | SetWithModeOpts> = T extends
146+
{ get: true } ? SimpleString | BulkNil
147+
: T extends { nx: true } ? SimpleString | BulkNil
148+
: T extends { xx: true } ? SimpleString | BulkNil
149+
: T extends SetWithModeOpts ? SimpleString | BulkNil
150+
: SimpleString;
144151

145152
/**
146153
* @deprecated Use {@linkcode SetOpts.nx}/{@linkcode SetOpts.xx} instead. This type will be removed in the future.
@@ -366,32 +373,11 @@ export interface RedisCommands {
366373
milliseconds: number,
367374
value: RedisValue,
368375
): Promise<SimpleString>;
369-
set(
376+
set<TSetOpts extends SetOpts | SetWithModeOpts = SetOpts>(
370377
key: string,
371378
value: RedisValue,
372-
opts?: Omit<SetOpts, "get" | "nx" | "xx"> & {
373-
get?: false | null;
374-
nx?: false | null;
375-
xx?: false | null;
376-
},
377-
): Promise<SimpleString>;
378-
set(
379-
key: string,
380-
value: RedisValue,
381-
opts?: (Omit<SetOpts, "get"> & { get: true }) | SetWithModeOpts,
382-
): Promise<SimpleString | BulkNil>;
383-
/** Executes `SET` command with `NX` option enabled. */
384-
set(
385-
key: string,
386-
value: RedisValue,
387-
opts?: Omit<SetOpts, "nx"> & { nx: true },
388-
): Promise<SimpleString | BulkNil>;
389-
/** Executes `SET` command with `XX` option enabled. */
390-
set(
391-
key: string,
392-
value: RedisValue,
393-
opts?: Omit<SetOpts, "xx"> & { xx: true },
394-
): Promise<SimpleString | BulkNil>;
379+
opts?: TSetOpts,
380+
): Promise<SetReply<TSetOpts>>;
395381
setbit(key: string, offset: number, value: RedisValue): Promise<Integer>;
396382
setex(key: string, seconds: number, value: RedisValue): Promise<SimpleString>;
397383
setnx(key: string, value: RedisValue): Promise<Integer>;

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type {
3737
ScanOpts,
3838
ScriptDebugMode,
3939
SetOpts,
40+
SetReply,
4041
SetWithModeOpts,
4142
ShutdownMode,
4243
SortOpts,

redis.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
ScanOpts,
2525
ScriptDebugMode,
2626
SetOpts,
27+
SetReply,
2728
SetWithModeOpts,
2829
ShutdownMode,
2930
SortOpts,
@@ -1345,41 +1346,11 @@ class RedisImpl implements Redis {
13451346
return this.execStatusReply("SELECT", index);
13461347
}
13471348

1348-
set(
1349+
set<TSetOpts extends SetOpts | SetWithModeOpts = SetOpts>(
13491350
key: string,
13501351
value: RedisValue,
1351-
opts?: Omit<SetOpts, "get" | "nx" | "xx"> & {
1352-
get?: false | null;
1353-
nx?: false | null;
1354-
xx?: false | null;
1355-
},
1356-
): Promise<SimpleString>;
1357-
set(
1358-
key: string,
1359-
value: RedisValue,
1360-
opts?: (Omit<SetOpts, "get"> & { get: true }) | SetWithModeOpts,
1361-
): Promise<SimpleString | BulkNil>;
1362-
set(
1363-
key: string,
1364-
value: RedisValue,
1365-
opts?: Omit<SetOpts, "nx"> & { nx: true },
1366-
): Promise<SimpleString | BulkNil>;
1367-
set(
1368-
key: string,
1369-
value: RedisValue,
1370-
opts?: Omit<SetOpts, "xx"> & { xx: true },
1371-
): Promise<SimpleString | BulkNil>;
1372-
set(
1373-
key: string,
1374-
value: RedisValue,
1375-
opts?:
1376-
| (Omit<SetOpts, "get" | "nx" | "xx"> & {
1377-
get?: boolean | null;
1378-
nx?: boolean | null;
1379-
xx?: boolean | null;
1380-
})
1381-
| SetWithModeOpts,
1382-
) {
1352+
opts?: TSetOpts,
1353+
): Promise<SetReply<TSetOpts>> {
13831354
const args: RedisValue[] = [key, value];
13841355
if (opts?.ex != null) {
13851356
args.push("EX", opts.ex);
@@ -1409,9 +1380,10 @@ class RedisImpl implements Redis {
14091380
if (opts?.get) {
14101381
args.push("GET");
14111382
}
1412-
return isAbleToReturnNil
1383+
const promise = isAbleToReturnNil
14131384
? this.execStatusOrNilReply("SET", ...args)
14141385
: this.execStatusReply("SET", ...args);
1386+
return promise as Promise<SetReply<TSetOpts>>;
14151387
}
14161388

14171389
setbit(key: string, offset: number, value: RedisValue) {

0 commit comments

Comments
 (0)