Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import COMMANDS from './commands';
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisFunctions, RedisModules, RedisExtensions, RedisScript, RedisScripts, RedisCommandSignature, ConvertArgumentType, RedisFunction, ExcludeMappedString, RedisCommands } from '../commands';
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisFunctions, RedisModules, RedisExtensions, RedisScript, RedisScripts, ConvertArgumentType, RedisFunction, ExcludeMappedString, RedisCommands, RedisCommandsSignatures } from '../commands';
import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
import RedisCommandsQueue, { QueueCommandOptions } from './commands-queue';
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
Expand All @@ -15,7 +15,6 @@ import { ClientClosedError, ClientOfflineError, DisconnectsClientError } from '.
import { URL } from 'url';
import { TcpSocketConnectOpts } from 'net';
import { PubSubType, PubSubListener, PubSubTypeListeners, ChannelListeners } from './pub-sub';
import { callbackify } from 'util';

export interface RedisClientOptions<
M extends RedisModules = RedisModules,
Expand Down Expand Up @@ -69,25 +68,17 @@ export interface RedisClientOptions<
pingInterval?: number;
}

type WithCommands = {
[P in keyof typeof COMMANDS]: RedisCommandSignature<(typeof COMMANDS)[P]>;
};
type WithCommands = RedisCommandsSignatures<typeof COMMANDS>;

export type WithModules<M extends RedisModules> = {
[P in keyof M as ExcludeMappedString<P>]: {
[C in keyof M[P] as ExcludeMappedString<C>]: RedisCommandSignature<M[P][C]>;
};
[P in keyof M as ExcludeMappedString<P>]: RedisCommandsSignatures<M[P]>;
};

export type WithFunctions<F extends RedisFunctions> = {
[P in keyof F as ExcludeMappedString<P>]: {
[FF in keyof F[P] as ExcludeMappedString<FF>]: RedisCommandSignature<F[P][FF]>;
};
[P in keyof F as ExcludeMappedString<P>]: RedisCommandsSignatures<F[P]>;
};

export type WithScripts<S extends RedisScripts> = {
[P in keyof S as ExcludeMappedString<P>]: RedisCommandSignature<S[P]>;
};
export type WithScripts<S extends RedisScripts> = RedisCommandsSignatures<S>;

export type RedisClientType<
M extends RedisModules = Record<string, never>,
Expand Down
6 changes: 2 additions & 4 deletions packages/client/lib/cluster/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import COMMANDS from './commands';
import { RedisCommand, RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisFunctions, RedisModules, RedisExtensions, RedisScript, RedisScripts, RedisCommandSignature, RedisFunction } from '../commands';
import { RedisCommand, RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisFunctions, RedisModules, RedisExtensions, RedisScript, RedisScripts, RedisFunction, RedisCommandsSignatures } from '../commands';
import { ClientCommandOptions, RedisClientOptions, RedisClientType, WithFunctions, WithModules, WithScripts } from '../client';
import RedisClusterSlots, { NodeAddressMap, ShardNode } from './cluster-slots';
import { attachExtensions, transformCommandReply, attachCommands, transformCommandArguments } from '../commander';
Expand Down Expand Up @@ -50,9 +50,7 @@ export interface RedisClusterOptions<
nodeAddressMap?: NodeAddressMap;
}

type WithCommands = {
[P in keyof typeof COMMANDS]: RedisCommandSignature<(typeof COMMANDS)[P]>;
};
type WithCommands = RedisCommandsSignatures<typeof COMMANDS>;

export type RedisClusterType<
M extends RedisModules = Record<string, never>,
Expand Down
32 changes: 21 additions & 11 deletions packages/client/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,27 @@ export type ConvertArgumentType<Type, ToType> =
)
);

export type RedisCommandSignature<
Command extends RedisCommand,
Params extends Array<unknown> = Parameters<Command['transformArguments']>
> = <Options extends CommandOptions<ClientCommandOptions>>(
...args: Params | [options: Options, ...rest: Params]
) => Promise<
ConvertArgumentType<
RedisCommandReply<Command>,
Options['returnBuffers'] extends true ? Buffer : string
>
>;
// using "Call Signatures" (https://www.typescriptlang.org/docs/handbook/2/functions.html#call-signatures) and not a union for better types in WebStorm
export type Command<
FN extends (...args: Array<any>) => any
> = {
(...args: Parameters<FN>): Promise<ConvertArgumentType<ReturnType<FN>, string>>;
<Options extends CommandOptions<ClientCommandOptions>>(
options: Options,
...rest: Parameters<FN>
): Promise<
ConvertArgumentType<
ReturnType<FN>,
Options['returnBuffers'] extends true ? Buffer : string
>
>;
};

export type RedisCommandsSignatures<T extends Record<string, RedisCommand>> = {
[P in keyof T as ExcludeMappedString<P>]: Command<
(...args: Parameters<T[P]['transformArguments']>) => RedisCommandReply<T[P]>
>;
};

export interface RedisCommands {
[command: string]: RedisCommand;
Expand Down