Skip to content

Commit

Permalink
feat: add SetReply (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored Oct 27, 2024
1 parent 651282c commit 176db18
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 58 deletions.
34 changes: 10 additions & 24 deletions command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ export interface SetOpts {
*/
get?: boolean;
}
/** Return type for {@linkcode RedisCommands.set} */
export type SetReply<T extends SetOpts | SetWithModeOpts> = T extends
{ get: true } ? SimpleString | BulkNil
: T extends { nx: true } ? SimpleString | BulkNil
: T extends { xx: true } ? SimpleString | BulkNil
: T extends SetWithModeOpts ? SimpleString | BulkNil
: SimpleString;

/**
* @deprecated Use {@linkcode SetOpts.nx}/{@linkcode SetOpts.xx} instead. This type will be removed in the future.
Expand Down Expand Up @@ -366,32 +373,11 @@ export interface RedisCommands {
milliseconds: number,
value: RedisValue,
): Promise<SimpleString>;
set(
set<TSetOpts extends SetOpts | SetWithModeOpts = SetOpts>(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "get" | "nx" | "xx"> & {
get?: false | null;
nx?: false | null;
xx?: false | null;
},
): Promise<SimpleString>;
set(
key: string,
value: RedisValue,
opts?: (Omit<SetOpts, "get"> & { get: true }) | SetWithModeOpts,
): Promise<SimpleString | BulkNil>;
/** Executes `SET` command with `NX` option enabled. */
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "nx"> & { nx: true },
): Promise<SimpleString | BulkNil>;
/** Executes `SET` command with `XX` option enabled. */
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "xx"> & { xx: true },
): Promise<SimpleString | BulkNil>;
opts?: TSetOpts,
): Promise<SetReply<TSetOpts>>;
setbit(key: string, offset: number, value: RedisValue): Promise<Integer>;
setex(key: string, seconds: number, value: RedisValue): Promise<SimpleString>;
setnx(key: string, value: RedisValue): Promise<Integer>;
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type {
ScanOpts,
ScriptDebugMode,
SetOpts,
SetReply,
SetWithModeOpts,
ShutdownMode,
SortOpts,
Expand Down
40 changes: 6 additions & 34 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
ScanOpts,
ScriptDebugMode,
SetOpts,
SetReply,
SetWithModeOpts,
ShutdownMode,
SortOpts,
Expand Down Expand Up @@ -1345,41 +1346,11 @@ class RedisImpl implements Redis {
return this.execStatusReply("SELECT", index);
}

set(
set<TSetOpts extends SetOpts | SetWithModeOpts = SetOpts>(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "get" | "nx" | "xx"> & {
get?: false | null;
nx?: false | null;
xx?: false | null;
},
): Promise<SimpleString>;
set(
key: string,
value: RedisValue,
opts?: (Omit<SetOpts, "get"> & { get: true }) | SetWithModeOpts,
): Promise<SimpleString | BulkNil>;
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "nx"> & { nx: true },
): Promise<SimpleString | BulkNil>;
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "xx"> & { xx: true },
): Promise<SimpleString | BulkNil>;
set(
key: string,
value: RedisValue,
opts?:
| (Omit<SetOpts, "get" | "nx" | "xx"> & {
get?: boolean | null;
nx?: boolean | null;
xx?: boolean | null;
})
| SetWithModeOpts,
) {
opts?: TSetOpts,
): Promise<SetReply<TSetOpts>> {
const args: RedisValue[] = [key, value];
if (opts?.ex != null) {
args.push("EX", opts.ex);
Expand Down Expand Up @@ -1409,9 +1380,10 @@ class RedisImpl implements Redis {
if (opts?.get) {
args.push("GET");
}
return isAbleToReturnNil
const promise = isAbleToReturnNil
? this.execStatusOrNilReply("SET", ...args)
: this.execStatusReply("SET", ...args);
return promise as Promise<SetReply<TSetOpts>>;
}

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

0 comments on commit 176db18

Please sign in to comment.