From c00200d2ada70e2fbfd146df998f5aeef4972c81 Mon Sep 17 00:00:00 2001 From: onejsninja Date: Wed, 13 Mar 2019 22:48:01 +0100 Subject: [PATCH] Add types to allow string array argument for hmset (#33835) * add missing type * update test * add name to Definitions By * add missing types and update test * regularize formatting * remove string[] * remove duplicate typing * organized formatting * Merge branch 'master' of https://github.com/OpesanyaAdebayo/DefinitelyTyped * remove duplicate typing * add types and tests for typescript 3.0 upwards * resolve version conflicts * resolve version conflict * resolve formatting * remove new types from older version * remove header from submodule --- types/redis/index.d.ts | 1 + types/redis/package.json | 11 + types/redis/redis-tests.ts | 10 +- types/redis/ts3.1/index.d.ts | 1235 ++++++++++++++++++++++++++++++ types/redis/ts3.1/redis-tests.ts | 142 ++++ types/redis/ts3.1/tsconfig.json | 23 + types/redis/ts3.1/tslint.json | 1 + 7 files changed, 1419 insertions(+), 4 deletions(-) create mode 100644 types/redis/package.json create mode 100644 types/redis/ts3.1/index.d.ts create mode 100644 types/redis/ts3.1/redis-tests.ts create mode 100644 types/redis/ts3.1/tsconfig.json create mode 100644 types/redis/ts3.1/tslint.json diff --git a/types/redis/index.d.ts b/types/redis/index.d.ts index eae16b7ff1..3548be95b9 100644 --- a/types/redis/index.d.ts +++ b/types/redis/index.d.ts @@ -10,6 +10,7 @@ // Pirasis Leelatanon // Stanislav Dzhus // Jake Ferrante +// Adebayo Opesanya // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // Imported from: https://github.com/types/npm-redis diff --git a/types/redis/package.json b/types/redis/package.json new file mode 100644 index 0000000000..c1f0c68752 --- /dev/null +++ b/types/redis/package.json @@ -0,0 +1,11 @@ +{ + "private": true, + "types": "index", + "typesVersions": { + ">=3.1.0-0": { + "*": [ + "ts3.1/*" + ] + } + } +} \ No newline at end of file diff --git a/types/redis/redis-tests.ts b/types/redis/redis-tests.ts index fb5a97987d..16e690ca46 100644 --- a/types/redis/redis-tests.ts +++ b/types/redis/redis-tests.ts @@ -20,8 +20,8 @@ redis.print(err, value); // ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- const options: redis.ClientOpts = { - host: "localhost", - port: 6379, + host: 'localhost', + port: 6379, }; let client: redis.RedisClient = redis.createClient(num, str, options); @@ -90,6 +90,8 @@ client.incr(str, resCallback); client.hgetall(str, resCallback); client.hmset(str, value, okCallback); client.hmset(str, str, str, str, str, okCallback); +client.hmset(str, [str, str, str, str]); +client.hmset(str, [str, value, str, value], okCallback); // Publish / Subscribe client.publish(str, value); @@ -115,8 +117,8 @@ client.duplicate(); // Pipeline client.cork(); -client.set("abc", "fff", strCallback); -client.get("abc", resCallback); +client.set('abc', 'fff', strCallback); +client.get('abc', resCallback); client.uncork(); // Add command diff --git a/types/redis/ts3.1/index.d.ts b/types/redis/ts3.1/index.d.ts new file mode 100644 index 0000000000..d69332f3de --- /dev/null +++ b/types/redis/ts3.1/index.d.ts @@ -0,0 +1,1235 @@ +/// + +import { EventEmitter } from 'events'; +import { Duplex } from 'stream'; + +export interface RetryStrategyOptions { + error: NodeJS.ErrnoException; + total_retry_time: number; + times_connected: number; + attempt: number; +} + +export type RetryStrategy = (options: RetryStrategyOptions) => number | Error; + +export interface ClientOpts { + host?: string; + port?: number; + path?: string; + url?: string; + parser?: string; + string_numbers?: boolean; + return_buffers?: boolean; + detect_buffers?: boolean; + socket_keepalive?: boolean; + no_ready_check?: boolean; + enable_offline_queue?: boolean; + retry_max_delay?: number; + connect_timeout?: number; + max_attempts?: number; + retry_unfulfilled_commands?: boolean; + auth_pass?: string; + password?: string; + db?: string | number; + family?: string; + rename_commands?: { [command: string]: string } | null; + tls?: any; + prefix?: string; + retry_strategy?: RetryStrategy; +} + +export type Callback = (err: Error | null, reply: T) => void; + +export interface ServerInfo { + redis_version: string; + versions: number[]; +} + +export interface OverloadedCommand { + (arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, arg6: T, cb?: Callback): R; + (arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, cb?: Callback): R; + (arg1: T, arg2: T, arg3: T, arg4: T, cb?: Callback): R; + (arg1: T, arg2: T, arg3: T, cb?: Callback): R; + (arg1: T, arg2: T | T[], cb?: Callback): R; + (arg1: T | T[], cb?: Callback): R; + (...args: Array>): R; +} + +export interface OverloadedKeyCommand { + (key: string, arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, arg6: T, cb?: Callback): R; + (key: string, arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, cb?: Callback): R; + (key: string, arg1: T, arg2: T, arg3: T, arg4: T, cb?: Callback): R; + (key: string, arg1: T, arg2: T, arg3: T, cb?: Callback): R; + (key: string, arg1: T, arg2: T, cb?: Callback): R; + (key: string, arg1: T | T[], cb?: Callback): R; + (key: string, ...args: Array>): R; + (...args: Array>): R; +} + +export interface OverloadedListCommand { + (arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, arg6: T, cb?: Callback): R; + (arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, cb?: Callback): R; + (arg1: T, arg2: T, arg3: T, arg4: T, cb?: Callback): R; + (arg1: T, arg2: T, arg3: T, cb?: Callback): R; + (arg1: T, arg2: T, cb?: Callback): R; + (arg1: T | T[], cb?: Callback): R; + (...args: Array>): R; +} + +export interface OverloadedSetCommand { + (key: string, arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, arg6: T, cb?: Callback): R; + (key: string, arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, cb?: Callback): R; + (key: string, arg1: T, arg2: T, arg3: T, arg4: T, cb?: Callback): R; + (key: string, arg1: T, arg2: T, arg3: T, cb?: Callback): R; + (key: string, arg1: T, arg2: T, cb?: Callback): R; + (key: string, arg1: T | { [key: string]: T } | T[], cb?: Callback): R; + (key: string, ...args: Array>): R; + (args: [string, ...T[]], cb?: Callback): R; +} + +export interface OverloadedLastCommand { + (arg1: T1, arg2: T1, arg3: T1, arg4: T1, arg5: T1, arg6: T2, cb?: Callback): R; + (arg1: T1, arg2: T1, arg3: T1, arg4: T1, arg5: T2, cb?: Callback): R; + (arg1: T1, arg2: T1, arg3: T1, arg4: T2, cb?: Callback): R; + (arg1: T1, arg2: T1, arg3: T2, cb?: Callback): R; + (arg1: T1, arg2: T2 | Array, cb?: Callback): R; + (args: Array, cb?: Callback): R; + (...args: Array>): R; +} + +export interface Commands { + /** + * Listen for all requests received by the server in real time. + */ + monitor(cb?: Callback): R; + MONITOR(cb?: Callback): R; + + /** + * Get information and statistics about the server. + */ + info(cb?: Callback): R; + info(section?: string | string[], cb?: Callback): R; + INFO(cb?: Callback): R; + INFO(section?: string | string[], cb?: Callback): R; + + /** + * Ping the server. + */ + ping(callback?: Callback): R; + ping(message: string, callback?: Callback): R; + PING(callback?: Callback): R; + PING(message: string, callback?: Callback): R; + + /** + * Post a message to a channel. + */ + publish(channel: string, value: string, cb?: Callback): R; + PUBLISH(channel: string, value: string, cb?: Callback): R; + + /** + * Authenticate to the server. + */ + auth(password: string, callback?: Callback): R; + AUTH(password: string, callback?: Callback): R; + + /** + * KILL - Kill the connection of a client. + * LIST - Get the list of client connections. + * GETNAME - Get the current connection name. + * PAUSE - Stop processing commands from clients for some time. + * REPLY - Instruct the server whether to reply to commands. + * SETNAME - Set the current connection name. + */ + client: OverloadedCommand; + CLIENT: OverloadedCommand; + + /** + * Set multiple hash fields to multiple values. + */ + hmset: OverloadedSetCommand; + HMSET: OverloadedSetCommand; + + /** + * Listen for messages published to the given channels. + */ + subscribe: OverloadedListCommand; + SUBSCRIBE: OverloadedListCommand; + + /** + * Stop listening for messages posted to the given channels. + */ + unsubscribe: OverloadedListCommand; + UNSUBSCRIBE: OverloadedListCommand; + + /** + * Listen for messages published to channels matching the given patterns. + */ + psubscribe: OverloadedListCommand; + PSUBSCRIBE: OverloadedListCommand; + + /** + * Stop listening for messages posted to channels matching the given patterns. + */ + punsubscribe: OverloadedListCommand; + PUNSUBSCRIBE: OverloadedListCommand; + + /** + * Append a value to a key. + */ + append(key: string, value: string, cb?: Callback): R; + APPEND(key: string, value: string, cb?: Callback): R; + + /** + * Asynchronously rewrite the append-only file. + */ + bgrewriteaof(cb?: Callback<'OK'>): R; + BGREWRITEAOF(cb?: Callback<'OK'>): R; + + /** + * Asynchronously save the dataset to disk. + */ + bgsave(cb?: Callback): R; + BGSAVE(cb?: Callback): R; + + /** + * Count set bits in a string. + */ + bitcount(key: string, cb?: Callback): R; + bitcount(key: string, start: number, end: number, cb?: Callback): R; + BITCOUNT(key: string, cb?: Callback): R; + BITCOUNT(key: string, start: number, end: number, cb?: Callback): R; + + /** + * Perform arbitrary bitfield integer operations on strings. + */ + bitfield: OverloadedKeyCommand; + BITFIELD: OverloadedKeyCommand; + + /** + * Perform bitwise operations between strings. + */ + bitop(operation: string, destkey: string, key1: string, key2: string, key3: string, cb?: Callback): R; + bitop(operation: string, destkey: string, key1: string, key2: string, cb?: Callback): R; + bitop(operation: string, destkey: string, key: string, cb?: Callback): R; + bitop(operation: string, destkey: string, ...args: Array>): R; + BITOP(operation: string, destkey: string, key1: string, key2: string, key3: string, cb?: Callback): R; + BITOP(operation: string, destkey: string, key1: string, key2: string, cb?: Callback): R; + BITOP(operation: string, destkey: string, key: string, cb?: Callback): R; + BITOP(operation: string, destkey: string, ...args: Array>): R; + + /** + * Find first bit set or clear in a string. + */ + bitpos(key: string, bit: number, start: number, end: number, cb?: Callback): R; + bitpos(key: string, bit: number, start: number, cb?: Callback): R; + bitpos(key: string, bit: number, cb?: Callback): R; + BITPOS(key: string, bit: number, start: number, end: number, cb?: Callback): R; + BITPOS(key: string, bit: number, start: number, cb?: Callback): R; + BITPOS(key: string, bit: number, cb?: Callback): R; + + /** + * Remove and get the first element in a list, or block until one is available. + */ + blpop: OverloadedLastCommand; + BLPOP: OverloadedLastCommand; + + /** + * Remove and get the last element in a list, or block until one is available. + */ + brpop: OverloadedLastCommand; + BRPOP: OverloadedLastCommand; + + /** + * Pop a value from a list, push it to another list and return it; or block until one is available. + */ + brpoplpush(source: string, destination: string, timeout: number, cb?: Callback): R; + BRPOPLPUSH(source: string, destination: string, timeout: number, cb?: Callback): R; + + /** + * ADDSLOTS - Assign new hash slots to receiving node. + * COUNT-FAILURE-REPORTS - Return the number of failure reports active for a given node. + * COUNTKEYSINSLOT - Return the number of local keys in the specified hash slot. + * DELSLOTS - Set hash slots as unbound in receiving node. + * FAILOVER - Forces a slave to perform a manual failover of its master. + * FORGET - Remove a node from the nodes table. + * GETKEYSINSLOT - Return local key names in the specified hash slot. + * INFO - Provides info about Redis Cluster node state. + * KEYSLOT - Returns the hash slot of the specified key. + * MEET - Force a node cluster to handshake with another node. + * NODES - Get cluster config for the node. + * REPLICATE - Reconfigure a node as a slave of the specified master node. + * RESET - Reset a Redis Cluster node. + * SAVECONFIG - Forces the node to save cluster state on disk. + * SET-CONFIG-EPOCH - Set the configuration epoch in a new node. + * SETSLOT - Bind a hash slot to a specified node. + * SLAVES - List slave nodes of the specified master node. + * SLOTS - Get array of Cluster slot to node mappings. + */ + cluster: OverloadedCommand; + CLUSTER: OverloadedCommand; + + /** + * Get array of Redis command details. + * + * COUNT - Get total number of Redis commands. + * GETKEYS - Extract keys given a full Redis command. + * INFO - Get array of specific REdis command details. + */ + command(cb?: Callback>): R; + COMMAND(cb?: Callback>): R; + + /** + * Get array of Redis command details. + * + * COUNT - Get array of Redis command details. + * GETKEYS - Extract keys given a full Redis command. + * INFO - Get array of specific Redis command details. + * GET - Get the value of a configuration parameter. + * REWRITE - Rewrite the configuration file with the in memory configuration. + * SET - Set a configuration parameter to the given value. + * RESETSTAT - Reset the stats returned by INFO. + */ + config: OverloadedCommand; + CONFIG: OverloadedCommand; + + /** + * Return the number of keys in the selected database. + */ + dbsize(cb?: Callback): R; + DBSIZE(cb?: Callback): R; + + /** + * OBJECT - Get debugging information about a key. + * SEGFAULT - Make the server crash. + */ + debug: OverloadedCommand; + DEBUG: OverloadedCommand; + + /** + * Decrement the integer value of a key by one. + */ + decr(key: string, cb?: Callback): R; + DECR(key: string, cb?: Callback): R; + + /** + * Decrement the integer value of a key by the given number. + */ + decrby(key: string, decrement: number, cb?: Callback): R; + DECRBY(key: string, decrement: number, cb?: Callback): R; + + /** + * Delete a key. + */ + del: OverloadedCommand; + DEL: OverloadedCommand; + + /** + * Discard all commands issued after MULTI. + */ + discard(cb?: Callback<'OK'>): R; + DISCARD(cb?: Callback<'OK'>): R; + + /** + * Return a serialized version of the value stored at the specified key. + */ + dump(key: string, cb?: Callback): R; + DUMP(key: string, cb?: Callback): R; + + /** + * Echo the given string. + */ + echo(message: T, cb?: Callback): R; + ECHO(message: T, cb?: Callback): R; + + /** + * Execute a Lua script server side. + */ + eval: OverloadedCommand; + EVAL: OverloadedCommand; + + /** + * Execute a Lue script server side. + */ + evalsha: OverloadedCommand; + EVALSHA: OverloadedCommand; + + /** + * Determine if a key exists. + */ + exists: OverloadedCommand; + EXISTS: OverloadedCommand; + + /** + * Set a key's time to live in seconds. + */ + expire(key: string, seconds: number, cb?: Callback): R; + EXPIRE(key: string, seconds: number, cb?: Callback): R; + + /** + * Set the expiration for a key as a UNIX timestamp. + */ + expireat(key: string, timestamp: number, cb?: Callback): R; + EXPIREAT(key: string, timestamp: number, cb?: Callback): R; + + /** + * Remove all keys from all databases. + */ + flushall(cb?: Callback): R; + FLUSHALL(cb?: Callback): R; + + /** + * Remove all keys from the current database. + */ + flushdb(cb?: Callback<'OK'>): R; + FLUSHDB(cb?: Callback<'OK'>): R; + + /** + * Add one or more geospatial items in the geospatial index represented using a sorted set. + */ + geoadd: OverloadedKeyCommand; + GEOADD: OverloadedKeyCommand; + + /** + * Returns members of a geospatial index as standard geohash strings. + */ + geohash: OverloadedKeyCommand; + GEOHASH: OverloadedKeyCommand; + + /** + * Returns longitude and latitude of members of a geospatial index. + */ + geopos: OverloadedKeyCommand, R>; + GEOPOS: OverloadedKeyCommand, R>; + + /** + * Returns the distance between two members of a geospatial index. + */ + geodist: OverloadedKeyCommand; + GEODIST: OverloadedKeyCommand; + + /** + * Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point. + */ + georadius: OverloadedKeyCommand, R>; + GEORADIUS: OverloadedKeyCommand, R>; + + /** + * Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member. + */ + georadiusbymember: OverloadedKeyCommand, R>; + GEORADIUSBYMEMBER: OverloadedKeyCommand, R>; + + /** + * Get the value of a key. + */ + get(key: string, cb?: Callback): R; + GET(key: string, cb?: Callback): R; + + /** + * Returns the bit value at offset in the string value stored at key. + */ + getbit(key: string, offset: number, cb?: Callback): R; + GETBIT(key: string, offset: number, cb?: Callback): R; + + /** + * Get a substring of the string stored at a key. + */ + getrange(key: string, start: number, end: number, cb?: Callback): R; + GETRANGE(key: string, start: number, end: number, cb?: Callback): R; + + /** + * Set the string value of a key and return its old value. + */ + getset(key: string, value: string, cb?: Callback): R; + GETSET(key: string, value: string, cb?: Callback): R; + + /** + * Delete on or more hash fields. + */ + hdel: OverloadedKeyCommand; + HDEL: OverloadedKeyCommand; + + /** + * Determine if a hash field exists. + */ + hexists(key: string, field: string, cb?: Callback): R; + HEXISTS(key: string, field: string, cb?: Callback): R; + + /** + * Get the value of a hash field. + */ + hget(key: string, field: string, cb?: Callback): R; + HGET(key: string, field: string, cb?: Callback): R; + + /** + * Get all fields and values in a hash. + */ + hgetall(key: string, cb?: Callback<{ [key: string]: string }>): R; + HGETALL(key: string, cb?: Callback<{ [key: string]: string }>): R; + + /** + * Increment the integer value of a hash field by the given number. + */ + hincrby(key: string, field: string, increment: number, cb?: Callback): R; + HINCRBY(key: string, field: string, increment: number, cb?: Callback): R; + + /** + * Increment the float value of a hash field by the given amount. + */ + hincrbyfloat(key: string, field: string, increment: number, cb?: Callback): R; + HINCRBYFLOAT(key: string, field: string, increment: number, cb?: Callback): R; + + /** + * Get all the fields of a hash. + */ + hkeys(key: string, cb?: Callback): R; + HKEYS(key: string, cb?: Callback): R; + + /** + * Get the number of fields in a hash. + */ + hlen(key: string, cb?: Callback): R; + HLEN(key: string, cb?: Callback): R; + + /** + * Get the values of all the given hash fields. + */ + hmget: OverloadedKeyCommand; + HMGET: OverloadedKeyCommand; + + /** + * Set the string value of a hash field. + */ + hset(key: string, field: string, value: string, cb?: Callback): R; + HSET(key: string, field: string, value: string, cb?: Callback): R; + + /** + * Set the value of a hash field, only if the field does not exist. + */ + hsetnx(key: string, field: string, value: string, cb?: Callback): R; + HSETNX(key: string, field: string, value: string, cb?: Callback): R; + + /** + * Get the length of the value of a hash field. + */ + hstrlen(key: string, field: string, cb?: Callback): R; + HSTRLEN(key: string, field: string, cb?: Callback): R; + + /** + * Get all the values of a hash. + */ + hvals(key: string, cb?: Callback): R; + HVALS(key: string, cb?: Callback): R; + + /** + * Increment the integer value of a key by one. + */ + incr(key: string, cb?: Callback): R; + INCR(key: string, cb?: Callback): R; + + /** + * Increment the integer value of a key by the given amount. + */ + incrby(key: string, increment: number, cb?: Callback): R; + INCRBY(key: string, increment: number, cb?: Callback): R; + + /** + * Increment the float value of a key by the given amount. + */ + incrbyfloat(key: string, increment: number, cb?: Callback): R; + INCRBYFLOAT(key: string, increment: number, cb?: Callback): R; + + /** + * Find all keys matching the given pattern. + */ + keys(pattern: string, cb?: Callback): R; + KEYS(pattern: string, cb?: Callback): R; + + /** + * Get the UNIX time stamp of the last successful save to disk. + */ + lastsave(cb?: Callback): R; + LASTSAVE(cb?: Callback): R; + + /** + * Get an element from a list by its index. + */ + lindex(key: string, index: number, cb?: Callback): R; + LINDEX(key: string, index: number, cb?: Callback): R; + + /** + * Insert an element before or after another element in a list. + */ + linsert(key: string, dir: 'BEFORE' | 'AFTER', pivot: string, value: string, cb?: Callback): R; + LINSERT(key: string, dir: 'BEFORE' | 'AFTER', pivot: string, value: string, cb?: Callback): R; + + /** + * Get the length of a list. + */ + llen(key: string, cb?: Callback): R; + LLEN(key: string, cb?: Callback): R; + + /** + * Remove and get the first element in a list. + */ + lpop(key: string, cb?: Callback): R; + LPOP(key: string, cb?: Callback): R; + + /** + * Prepend one or multiple values to a list. + */ + lpush: OverloadedKeyCommand; + LPUSH: OverloadedKeyCommand; + + /** + * Prepend a value to a list, only if the list exists. + */ + lpushx(key: string, value: string, cb?: Callback): R; + LPUSHX(key: string, value: string, cb?: Callback): R; + + /** + * Get a range of elements from a list. + */ + lrange(key: string, start: number, stop: number, cb?: Callback): R; + LRANGE(key: string, start: number, stop: number, cb?: Callback): R; + + /** + * Remove elements from a list. + */ + lrem(key: string, count: number, value: string, cb?: Callback): R; + LREM(key: string, count: number, value: string, cb?: Callback): R; + + /** + * Set the value of an element in a list by its index. + */ + lset(key: string, index: number, value: string, cb?: Callback<'OK'>): R; + LSET(key: string, index: number, value: string, cb?: Callback<'OK'>): R; + + /** + * Trim a list to the specified range. + */ + ltrim(key: string, start: number, stop: number, cb?: Callback<'OK'>): R; + LTRIM(key: string, start: number, stop: number, cb?: Callback<'OK'>): R; + + /** + * Get the values of all given keys. + */ + mget: OverloadedCommand; + MGET: OverloadedCommand; + + /** + * Atomically tranfer a key from a Redis instance to another one. + */ + migrate: OverloadedCommand; + MIGRATE: OverloadedCommand; + + /** + * Move a key to another database. + */ + move(key: string, db: string | number): R; + MOVE(key: string, db: string | number): R; + + /** + * Set multiple keys to multiple values. + */ + mset: OverloadedCommand; + MSET: OverloadedCommand; + + /** + * Set multiple keys to multiple values, only if none of the keys exist. + */ + msetnx: OverloadedCommand; + MSETNX: OverloadedCommand; + + /** + * Inspect the internals of Redis objects. + */ + object: OverloadedCommand; + OBJECT: OverloadedCommand; + + /** + * Remove the expiration from a key. + */ + persist(key: string, cb?: Callback): R; + PERSIST(key: string, cb?: Callback): R; + + /** + * Remove a key's time to live in milliseconds. + */ + pexpire(key: string, milliseconds: number, cb?: Callback): R; + PEXPIRE(key: string, milliseconds: number, cb?: Callback): R; + + /** + * Set the expiration for a key as a UNIX timestamp specified in milliseconds. + */ + pexpireat(key: string, millisecondsTimestamp: number, cb?: Callback): R; + PEXPIREAT(key: string, millisecondsTimestamp: number, cb?: Callback): R; + + /** + * Adds the specified elements to the specified HyperLogLog. + */ + pfadd: OverloadedKeyCommand; + PFADD: OverloadedKeyCommand; + + /** + * Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s). + */ + pfcount: OverloadedCommand; + PFCOUNT: OverloadedCommand; + + /** + * Merge N different HyperLogLogs into a single one. + */ + pfmerge: OverloadedCommand; + PFMERGE: OverloadedCommand; + + /** + * Set the value and expiration in milliseconds of a key. + */ + psetex(key: string, milliseconds: number, value: string, cb?: Callback<'OK'>): R; + PSETEX(key: string, milliseconds: number, value: string, cb?: Callback<'OK'>): R; + + /** + * Inspect the state of the Pub/Sub subsytem. + */ + pubsub: OverloadedCommand; + PUBSUB: OverloadedCommand; + + /** + * Get the time to live for a key in milliseconds. + */ + pttl(key: string, cb?: Callback): R; + PTTL(key: string, cb?: Callback): R; + + /** + * Close the connection. + */ + quit(cb?: Callback<'OK'>): R; + QUIT(cb?: Callback<'OK'>): R; + + /** + * Return a random key from the keyspace. + */ + randomkey(cb?: Callback): R; + RANDOMKEY(cb?: Callback): R; + + /** + * Enables read queries for a connection to a cluster slave node. + */ + readonly(cb?: Callback): R; + READONLY(cb?: Callback): R; + + /** + * Disables read queries for a connection to cluster slave node. + */ + readwrite(cb?: Callback): R; + READWRITE(cb?: Callback): R; + + /** + * Rename a key. + */ + rename(key: string, newkey: string, cb?: Callback<'OK'>): R; + RENAME(key: string, newkey: string, cb?: Callback<'OK'>): R; + + /** + * Rename a key, only if the new key does not exist. + */ + renamenx(key: string, newkey: string, cb?: Callback): R; + RENAMENX(key: string, newkey: string, cb?: Callback): R; + + /** + * Create a key using the provided serialized value, previously obtained using DUMP. + */ + restore(key: string, ttl: number, serializedValue: string, cb?: Callback<'OK'>): R; + RESTORE(key: string, ttl: number, serializedValue: string, cb?: Callback<'OK'>): R; + + /** + * Return the role of the instance in the context of replication. + */ + role(cb?: Callback<[string, number, Array<[string, string, string]>]>): R; + ROLE(cb?: Callback<[string, number, Array<[string, string, string]>]>): R; + + /** + * Remove and get the last element in a list. + */ + rpop(key: string, cb?: Callback): R; + RPOP(key: string, cb?: Callback): R; + + /** + * Remove the last element in a list, prepend it to another list and return it. + */ + rpoplpush(source: string, destination: string, cb?: Callback): R; + RPOPLPUSH(source: string, destination: string, cb?: Callback): R; + + /** + * Append one or multiple values to a list. + */ + rpush: OverloadedKeyCommand; + RPUSH: OverloadedKeyCommand; + + /** + * Append a value to a list, only if the list exists. + */ + rpushx(key: string, value: string, cb?: Callback): R; + RPUSHX(key: string, value: string, cb?: Callback): R; + + /** + * Append one or multiple members to a set. + */ + sadd: OverloadedKeyCommand; + SADD: OverloadedKeyCommand; + + /** + * Synchronously save the dataset to disk. + */ + save(cb?: Callback): R; + SAVE(cb?: Callback): R; + + /** + * Get the number of members in a set. + */ + scard(key: string, cb?: Callback): R; + SCARD(key: string, cb?: Callback): R; + + /** + * DEBUG - Set the debug mode for executed scripts. + * EXISTS - Check existence of scripts in the script cache. + * FLUSH - Remove all scripts from the script cache. + * KILL - Kill the script currently in execution. + * LOAD - Load the specified Lua script into the script cache. + */ + script: OverloadedCommand; + SCRIPT: OverloadedCommand; + + /** + * Subtract multiple sets. + */ + sdiff: OverloadedCommand; + SDIFF: OverloadedCommand; + + /** + * Subtract multiple sets and store the resulting set in a key. + */ + sdiffstore: OverloadedKeyCommand; + SDIFFSTORE: OverloadedKeyCommand; + + /** + * Change the selected database for the current connection. + */ + select(index: number | string, cb?: Callback): R; + SELECT(index: number | string, cb?: Callback): R; + + /** + * Set the string value of a key. + */ + set(key: string, value: string, cb?: Callback<'OK'>): R; + set(key: string, value: string, flag: string, cb?: Callback<'OK'>): R; + set(key: string, value: string, mode: string, duration: number, cb?: Callback<'OK' | undefined>): R; + set(key: string, value: string, mode: string, duration: number, flag: string, cb?: Callback<'OK' | undefined>): R; + SET(key: string, value: string, cb?: Callback<'OK'>): R; + SET(key: string, value: string, flag: string, cb?: Callback<'OK'>): R; + SET(key: string, value: string, mode: string, duration: number, cb?: Callback<'OK' | undefined>): R; + SET(key: string, value: string, mode: string, duration: number, flag: string, cb?: Callback<'OK' | undefined>): R; + + /** + * Sets or clears the bit at offset in the string value stored at key. + */ + setbit(key: string, offset: number, value: string, cb?: Callback): R; + SETBIT(key: string, offset: number, value: string, cb?: Callback): R; + + /** + * Set the value and expiration of a key. + */ + setex(key: string, seconds: number, value: string, cb?: Callback): R; + SETEX(key: string, seconds: number, value: string, cb?: Callback): R; + + /** + * Set the value of a key, only if the key does not exist. + */ + setnx(key: string, value: string, cb?: Callback): R; + SETNX(key: string, value: string, cb?: Callback): R; + + /** + * Overwrite part of a string at key starting at the specified offset. + */ + setrange(key: string, offset: number, value: string, cb?: Callback): R; + SETRANGE(key: string, offset: number, value: string, cb?: Callback): R; + + /** + * Synchronously save the dataset to disk and then shut down the server. + */ + shutdown: OverloadedCommand; + SHUTDOWN: OverloadedCommand; + + /** + * Intersect multiple sets. + */ + sinter: OverloadedKeyCommand; + SINTER: OverloadedKeyCommand; + + /** + * Intersect multiple sets and store the resulting set in a key. + */ + sinterstore: OverloadedCommand; + SINTERSTORE: OverloadedCommand; + + /** + * Determine if a given value is a member of a set. + */ + sismember(key: string, member: string, cb?: Callback): R; + SISMEMBER(key: string, member: string, cb?: Callback): R; + + /** + * Make the server a slave of another instance, or promote it as master. + */ + slaveof(host: string, port: string | number, cb?: Callback): R; + SLAVEOF(host: string, port: string | number, cb?: Callback): R; + + /** + * Manages the Redis slow queries log. + */ + slowlog: OverloadedCommand, R>; + SLOWLOG: OverloadedCommand, R>; + + /** + * Get all the members in a set. + */ + smembers(key: string, cb?: Callback): R; + SMEMBERS(key: string, cb?: Callback): R; + + /** + * Move a member from one set to another. + */ + smove(source: string, destination: string, member: string, cb?: Callback): R; + SMOVE(source: string, destination: string, member: string, cb?: Callback): R; + + /** + * Sort the elements in a list, set or sorted set. + */ + sort: OverloadedCommand; + SORT: OverloadedCommand; + + /** + * Remove and return one or multiple random members from a set. + */ + spop(key: string, cb?: Callback): R; + spop(key: string, count: number, cb?: Callback): R; + SPOP(key: string, cb?: Callback): R; + SPOP(key: string, count: number, cb?: Callback): R; + + /** + * Get one or multiple random members from a set. + */ + srandmember(key: string, cb?: Callback): R; + srandmember(key: string, count: number, cb?: Callback): R; + SRANDMEMBER(key: string, cb?: Callback): R; + SRANDMEMBER(key: string, count: number, cb?: Callback): R; + + /** + * Remove one or more members from a set. + */ + srem: OverloadedKeyCommand; + SREM: OverloadedKeyCommand; + + /** + * Get the length of the value stored in a key. + */ + strlen(key: string, cb?: Callback): R; + STRLEN(key: string, cb?: Callback): R; + + /** + * Add multiple sets. + */ + sunion: OverloadedCommand; + SUNION: OverloadedCommand; + + /** + * Add multiple sets and store the resulting set in a key. + */ + sunionstore: OverloadedCommand; + SUNIONSTORE: OverloadedCommand; + + /** + * Internal command used for replication. + */ + sync(cb?: Callback): R; + SYNC(cb?: Callback): R; + + /** + * Return the current server time. + */ + time(cb?: Callback<[string, string]>): R; + TIME(cb?: Callback<[string, string]>): R; + + /** + * Get the time to live for a key. + */ + ttl(key: string, cb?: Callback): R; + TTL(key: string, cb?: Callback): R; + + /** + * Determine the type stored at key. + */ + type(key: string, cb?: Callback): R; + TYPE(key: string, cb?: Callback): R; + + /** + * Forget about all watched keys. + */ + unwatch(cb?: Callback<'OK'>): R; + UNWATCH(cb?: Callback<'OK'>): R; + + /** + * Wait for the synchronous replication of all the write commands sent in the context of the current connection. + */ + wait(numslaves: number, timeout: number, cb?: Callback): R; + WAIT(numslaves: number, timeout: number, cb?: Callback): R; + + /** + * Watch the given keys to determine execution of the MULTI/EXEC block. + */ + watch: OverloadedCommand; + WATCH: OverloadedCommand; + + /** + * Add one or more members to a sorted set, or update its score if it already exists. + */ + zadd: OverloadedKeyCommand; + ZADD: OverloadedKeyCommand; + + /** + * Get the number of members in a sorted set. + */ + zcard(key: string, cb?: Callback): R; + ZCARD(key: string, cb?: Callback): R; + + /** + * Count the members in a sorted set with scores between the given values. + */ + zcount(key: string, min: number | string, max: number | string, cb?: Callback): R; + ZCOUNT(key: string, min: number | string, max: number | string, cb?: Callback): R; + + /** + * Increment the score of a member in a sorted set. + */ + zincrby(key: string, increment: number, member: string, cb?: Callback): R; + ZINCRBY(key: string, increment: number, member: string, cb?: Callback): R; + + /** + * Intersect multiple sorted sets and store the resulting sorted set in a new key. + */ + zinterstore: OverloadedCommand; + ZINTERSTORE: OverloadedCommand; + + /** + * Count the number of members in a sorted set between a given lexicographic range. + */ + zlexcount(key: string, min: string, max: string, cb?: Callback): R; + ZLEXCOUNT(key: string, min: string, max: string, cb?: Callback): R; + + /** + * Return a range of members in a sorted set, by index. + */ + zrange(key: string, start: number, stop: number, cb?: Callback): R; + zrange(key: string, start: number, stop: number, withscores: string, cb?: Callback): R; + ZRANGE(key: string, start: number, stop: number, cb?: Callback): R; + ZRANGE(key: string, start: number, stop: number, withscores: string, cb?: Callback): R; + + /** + * Return a range of members in a sorted set, by lexicographical range. + */ + zrangebylex(key: string, min: string, max: string, cb?: Callback): R; + zrangebylex(key: string, min: string, max: string, limit: string, offset: number, count: number, cb?: Callback): R; + ZRANGEBYLEX(key: string, min: string, max: string, cb?: Callback): R; + ZRANGEBYLEX(key: string, min: string, max: string, limit: string, offset: number, count: number, cb?: Callback): R; + + /** + * Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings. + */ + zrevrangebylex(key: string, min: string, max: string, cb?: Callback): R; + zrevrangebylex(key: string, min: string, max: string, limit: string, offset: number, count: number, cb?: Callback): R; + ZREVRANGEBYLEX(key: string, min: string, max: string, cb?: Callback): R; + ZREVRANGEBYLEX(key: string, min: string, max: string, limit: string, offset: number, count: number, cb?: Callback): R; + + /** + * Return a range of members in a sorted set, by score. + */ + zrangebyscore(key: string, min: number | string, max: number | string, cb?: Callback): R; + zrangebyscore(key: string, min: number | string, max: number | string, withscores: string, cb?: Callback): R; + zrangebyscore(key: string, min: number | string, max: number | string, limit: string, offset: number, count: number, cb?: Callback): R; + zrangebyscore(key: string, min: number | string, max: number | string, withscores: string, limit: string, offset: number, count: number, cb?: Callback): R; + ZRANGEBYSCORE(key: string, min: number | string, max: number | string, cb?: Callback): R; + ZRANGEBYSCORE(key: string, min: number | string, max: number | string, withscores: string, cb?: Callback): R; + ZRANGEBYSCORE(key: string, min: number | string, max: number | string, limit: string, offset: number, count: number, cb?: Callback): R; + ZRANGEBYSCORE(key: string, min: number | string, max: number | string, withscores: string, limit: string, offset: number, count: number, cb?: Callback): R; + + /** + * Determine the index of a member in a sorted set. + */ + zrank(key: string, member: string, cb?: Callback): R; + ZRANK(key: string, member: string, cb?: Callback): R; + + /** + * Remove one or more members from a sorted set. + */ + zrem: OverloadedKeyCommand; + ZREM: OverloadedKeyCommand; + + /** + * Remove all members in a sorted set between the given lexicographical range. + */ + zremrangebylex(key: string, min: string, max: string, cb?: Callback): R; + ZREMRANGEBYLEX(key: string, min: string, max: string, cb?: Callback): R; + + /** + * Remove all members in a sorted set within the given indexes. + */ + zremrangebyrank(key: string, start: number, stop: number, cb?: Callback): R; + ZREMRANGEBYRANK(key: string, start: number, stop: number, cb?: Callback): R; + + /** + * Remove all members in a sorted set within the given indexes. + */ + zremrangebyscore(key: string, min: string | number, max: string | number, cb?: Callback): R; + ZREMRANGEBYSCORE(key: string, min: string | number, max: string | number, cb?: Callback): R; + + /** + * Return a range of members in a sorted set, by index, with scores ordered from high to low. + */ + zrevrange(key: string, start: number, stop: number, cb?: Callback): R; + zrevrange(key: string, start: number, stop: number, withscores: string, cb?: Callback): R; + ZREVRANGE(key: string, start: number, stop: number, cb?: Callback): R; + ZREVRANGE(key: string, start: number, stop: number, withscores: string, cb?: Callback): R; + + /** + * Return a range of members in a sorted set, by score, with scores ordered from high to low. + */ + zrevrangebyscore(key: string, min: number | string, max: number | string, cb?: Callback): R; + zrevrangebyscore(key: string, min: number | string, max: number | string, withscores: string, cb?: Callback): R; + zrevrangebyscore(key: string, min: number | string, max: number | string, limit: string, offset: number, count: number, cb?: Callback): R; + zrevrangebyscore(key: string, min: number | string, max: number | string, withscores: string, limit: string, offset: number, count: number, cb?: Callback): R; + ZREVRANGEBYSCORE(key: string, min: number | string, max: number | string, cb?: Callback): R; + ZREVRANGEBYSCORE(key: string, min: number | string, max: number | string, withscores: string, cb?: Callback): R; + ZREVRANGEBYSCORE(key: string, min: number | string, max: number | string, limit: string, offset: number, count: number, cb?: Callback): R; + ZREVRANGEBYSCORE(key: string, min: number | string, max: number | string, withscores: string, limit: string, offset: number, count: number, cb?: Callback): R; + + /** + * Determine the index of a member in a sorted set, with scores ordered from high to low. + */ + zrevrank(key: string, member: string, cb?: Callback): R; + ZREVRANK(key: string, member: string, cb?: Callback): R; + + /** + * Get the score associated with the given member in a sorted set. + */ + zscore(key: string, member: string, cb?: Callback): R; + ZSCORE(key: string, member: string, cb?: Callback): R; + + /** + * Add multiple sorted sets and store the resulting sorted set in a new key. + */ + zunionstore: OverloadedCommand; + ZUNIONSTORE: OverloadedCommand; + + /** + * Incrementally iterate the keys space. + */ + scan: OverloadedCommand; + SCAN: OverloadedCommand; + + /** + * Incrementally iterate Set elements. + */ + sscan: OverloadedKeyCommand; + SSCAN: OverloadedKeyCommand; + + /** + * Incrementally iterate hash fields and associated values. + */ + hscan: OverloadedKeyCommand; + HSCAN: OverloadedKeyCommand; + + /** + * Incrementally iterate sorted sets elements and associated scores. + */ + zscan: OverloadedKeyCommand; + ZSCAN: OverloadedKeyCommand; +} + +export const RedisClient: new (options: ClientOpts) => RedisClient; + +export interface RedisClient extends Commands, EventEmitter { + connected: boolean; + command_queue_length: number; + offline_queue_length: number; + retry_delay: number; + retry_backoff: number; + command_queue: any[]; + offline_queue: any[]; + connection_id: number; + server_info: ServerInfo; + stream: Duplex; + + on(event: 'message' | 'message_buffer', listener: (channel: string, message: string) => void): this; + on(event: 'pmessage' | 'pmessage_buffer', listener: (pattern: string, channel: string, message: string) => void): this; + on(event: 'subscribe' | 'unsubscribe', listener: (channel: string, count: number) => void): this; + on(event: 'psubscribe' | 'punsubscribe', listener: (pattern: string, count: number) => void): this; + on(event: string, listener: (...args: any[]) => void): this; + + /** + * Client methods. + */ + + end(flush?: boolean): void; + unref(): void; + + cork(): void; + uncork(): void; + + duplicate(options?: ClientOpts, cb?: Callback): RedisClient; + + sendCommand(command: string, cb?: Callback): boolean; + sendCommand(command: string, args?: any[], cb?: Callback): boolean; + send_command(command: string, cb?: Callback): boolean; + send_command(command: string, args?: any[], cb?: Callback): boolean; + + addCommand(command: string): void; + add_command(command: string): void; + + /** + * Mark the start of a transaction block. + */ + multi(args?: Array>>): Multi; + MULTI(args?: Array>>): Multi; + + batch(args?: Array>>): Multi; + BATCH(args?: Array>>): Multi; +} + +export const Multi: new () => Multi; + +export interface Multi extends Commands { + exec(cb?: Callback): boolean; + EXEC(cb?: Callback): boolean; + + exec_atomic(cb?: Callback): boolean; + EXEC_ATOMIC(cb?: Callback): boolean; +} + +export let debug_mode: boolean; + +export function createClient(port: number, host?: string, options?: ClientOpts): RedisClient; +export function createClient(unix_socket: string, options?: ClientOpts): RedisClient; +export function createClient(redis_url: string, options?: ClientOpts): RedisClient; +export function createClient(options?: ClientOpts): RedisClient; + +export function print(err: Error | null, reply: any): void; + +export class RedisError extends Error { } +export class ReplyError extends RedisError { } +export class AbortError extends RedisError { } +export class ParserError extends RedisError { + offset: number; + buffer: Buffer; +} +export class AggregateError extends AbortError { } diff --git a/types/redis/ts3.1/redis-tests.ts b/types/redis/ts3.1/redis-tests.ts new file mode 100644 index 0000000000..e2a0a37acf --- /dev/null +++ b/types/redis/ts3.1/redis-tests.ts @@ -0,0 +1,142 @@ +import redis = require('redis'); + +const value: any = 'any value'; +const commandArr: any[][] = []; +const num = 0; +const str = 'any string'; +const err: Error = new Error(); +const args: any[] = []; +const resCallback: (err: Error | null, res: any) => void = () => {}; +const numCallback: (err: Error | null, res: number) => void = () => {}; +const strCallback: (err: Error | null, res: string) => void = () => {}; +const okCallback: (err: Error | null, res: 'OK') => void = () => {}; +const messageHandler: (channel: string, message: any) => void = () => {}; + +// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- + +const debug_mode: boolean = redis.debug_mode; +redis.print(err, value); + +// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- + +const options: redis.ClientOpts = { + host: 'localhost', + port: 6379, +}; +let client: redis.RedisClient = redis.createClient(num, str, options); + +// Test the `retry_strategy` property +// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- +function retryStrategyNumber(options: redis.RetryStrategyOptions): number { + // Ensure that the properties of RetryStrategyOptions are resilient to breaking change. + // If the properties of the interface changes, the variables below will also need to be adapted. + const error: Error = options.error; + const total_retry_time: number = options.total_retry_time; + const times_connected: number = options.times_connected; + const attempt: number = options.attempt; + return 5000; +} +function retryStrategyError(options: redis.RetryStrategyOptions): Error { + return new Error('Foo'); +} +client = redis.createClient({ + retry_strategy: retryStrategyNumber +}); +client = redis.createClient({ + retry_strategy: retryStrategyError +}); +// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- + +const connected: boolean = client.connected; +const retry_delay: number = client.retry_delay; +const retry_backoff: number = client.retry_backoff; +const command_queue: any[] = client.command_queue; +const offline_queue: any[] = client.offline_queue; +const info: redis.ServerInfo = client.server_info; + +// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- + +client.end(true); + +// Connection (http://redis.io/commands#connection) +client.auth(str, resCallback); +client.ping(strCallback); +client.unref(); + +// Strings (http://redis.io/commands#strings) +client.append(str, str, numCallback); +client.bitcount(str, numCallback); +client.bitcount(str, num, num, numCallback); +client.set(str, str, okCallback); +client.get(str, strCallback); +client.exists(str, numCallback); + +// Event handlers +client.on(str, messageHandler); +client.once(str, messageHandler); + +// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- + +// some of the bulk methods +client.get('test'); +client.get('test', resCallback); +client.set('test', 'test'); +client.set('test', 'test', okCallback); +client.mset(args, resCallback); + +client.incr(str, resCallback); + +// Friendlier hash commands +client.hgetall(str, resCallback); +client.hmset(str, value, okCallback); +client.hmset(str, str, str, str, str, okCallback); +client.hmset([str, str, str, str, str], okCallback); +client.hmset([str, str, str, str, str]); +client.hmset(str, [str, str, str, str]); +client.hmset(str, [str, value, str, value], okCallback); + +// Publish / Subscribe +client.publish(str, value); +client.subscribe(str); + +// Multi +client.multi() + .scard(str) + .smembers(str) + .keys('*', resCallback) + .dbsize() + .exec(resCallback); + +client.multi([['get', 'test']]).exec(); + +// Monitor mode +client.monitor(resCallback); + +// Send command +client.send_command(str, args, resCallback); +// Duplicate +client.duplicate(); + +// Pipeline +client.cork(); +client.set('abc', 'fff', strCallback); +client.get('abc', resCallback); +client.uncork(); + +// Add command +client.add_command('my command'); +client.addCommand('my other command'); + +// redis.print as callback +client.set(str, str, redis.print); +client.get(str, redis.print); + +// increase-by-float reply a string +client.incrbyfloat('a', 1.5, (error, value) => value.startsWith('1')); +client.INCRBYFLOAT('a', 1.5, (error, value) => value.startsWith('1')); +client.hincrbyfloat('a', 'b', 1.5, (error, value) => value.startsWith('1')); +client.HINCRBYFLOAT('a', 'b', 1.5, (error, value) => value.startsWith('1')); +client.zincrby('a', 1, 'b', strCallback); +client.ZINCRBY('a', 1, 'b', strCallback); + +client.flushdb(okCallback); diff --git a/types/redis/ts3.1/tsconfig.json b/types/redis/ts3.1/tsconfig.json new file mode 100644 index 0000000000..eb55ed1d6f --- /dev/null +++ b/types/redis/ts3.1/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "redis-tests.ts" + ] +} \ No newline at end of file diff --git a/types/redis/ts3.1/tslint.json b/types/redis/ts3.1/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/redis/ts3.1/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }