Skip to content

Commit 94c1e24

Browse files
committedMar 14, 2022
feat: improve typings for transformers
1 parent 48ca227 commit 94c1e24

File tree

8 files changed

+10019
-1346
lines changed

8 files changed

+10019
-1346
lines changed
 

‎bin/generateRedisCommander/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const returnTypes = require("./returnTypes");
22
const argumentTypes = require("./argumentTypes");
33
const typeMaps = require("./typeMaps");
4+
const overrides = require("./overrides");
45
const { getCommanderInterface } = require("@ioredis/interface-generator");
56

67
const ignoredCommands = ["monitor", "multi"];
@@ -20,6 +21,7 @@ async function main() {
2021
redisOpts: {
2122
port: process.env.REDIS_PORT,
2223
},
24+
overrides,
2325
returnTypes,
2426
argumentTypes,
2527
typeMaps: typeMaps,
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const msetOverrides = {
2+
overwrite: false,
3+
defs: [
4+
"$1(object: Record<string, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
5+
"$1(map: Map<string | Buffer | number, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
6+
],
7+
};
8+
9+
module.exports = {
10+
hgetall: {
11+
overwrite: true,
12+
defs: [
13+
"$1(key: RedisKey, callback?: Callback<Record<string, string>>): Result<Record<string, string>, Context>",
14+
"$1Buffer(key: RedisKey, callback?: Callback<[field: Buffer, value: Buffer][]>): Result<[field: Buffer, value: Buffer][], Context>",
15+
],
16+
},
17+
mset: msetOverrides,
18+
msetnx: msetOverrides,
19+
hset: {
20+
overwrite: false,
21+
defs: [
22+
"$1(key: RedisKey, object: Record<string, string | Buffer | number>, callback?: Callback<number>): Result<number, Context>",
23+
"$1(key: RedisKey, map: Map<string | Buffer | number, string | Buffer | number>, callback?: Callback<number>): Result<number, Context>",
24+
],
25+
},
26+
hmset: {
27+
overwrite: false,
28+
defs: [
29+
"$1(key: RedisKey, object: Record<string, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
30+
"$1(key: RedisKey, map: Map<string | Buffer | number, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
31+
],
32+
},
33+
exec: {
34+
overwrite: true,
35+
defs: [
36+
"exec(callback?: Callback<[error: Error, result: unknown][] | null>): Promise<[error: Error, result: unknown][] | null>;",
37+
],
38+
},
39+
};

‎bin/generateRedisCommander/returnTypes.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module.exports = {
8181
hello: "unknown[]",
8282
hexists: "number",
8383
hget: "string | null",
84+
hgetall: "[field: string, value: string][]",
8485
hincrby: "number",
8586
hincrbyfloat: "string",
8687
hkeys: "string[]",
@@ -89,6 +90,28 @@ module.exports = {
8990
hmset: "'OK'",
9091
hset: "number",
9192
hsetnx: "number",
93+
acl: (types) => {
94+
if (matchSubcommand(types, "SAVE")) return '"OK"';
95+
if (matchSubcommand(types, "DELUSER")) return "number";
96+
if (matchSubcommand(types, "WHOAMI")) return "string";
97+
if (matchSubcommand(types, "DRYRUN")) return "string";
98+
if (matchSubcommand(types, "GENPASS")) return "string";
99+
if (matchSubcommand(types, "GETUSER")) return "string[] | null";
100+
if (matchSubcommand(types, "LIST")) return "string[]";
101+
if (matchSubcommand(types, "USERS")) return "string[]";
102+
if (matchSubcommand(types, "LOAD")) return '"OK"';
103+
if (matchSubcommand(types, "SETUSER")) return '"OK"';
104+
},
105+
client: (types) => {
106+
if (matchSubcommand(types, "CACHING")) return '"OK"';
107+
if (matchSubcommand(types, "PAUSE")) return '"OK"';
108+
if (matchSubcommand(types, "UNPAUSE")) return '"OK"';
109+
if (matchSubcommand(types, "SETNAME")) return '"OK"';
110+
if (matchSubcommand(types, "GETNAME")) return "string | null";
111+
if (matchSubcommand(types, "GETREDIR")) return "number";
112+
if (matchSubcommand(types, "INFO")) return "string";
113+
if (matchSubcommand(types, "ID")) return "number";
114+
},
92115
memory: (types) => {
93116
if (matchSubcommand(types, "MALLOC-STATS")) return "string";
94117
if (matchSubcommand(types, "PURGE")) return '"OK"';
@@ -227,8 +250,8 @@ module.exports = {
227250
zrevrangebyscore: "string[]",
228251
zrevrank: "number | null",
229252
zscore: "string",
230-
zunion: "unknown[]",
231-
zmscore: "unknown[] | null",
253+
zunion: "string[]",
254+
zmscore: "(string | null)[]",
232255
zunionstore: "number",
233256
scan: "[cursor: string, elements: string[]]",
234257
sscan: "[cursor: string, elements: string[]]",

‎bin/generateRedisCommander/typeMaps.js

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module.exports = {
1111
"threshold",
1212
"start",
1313
"end",
14+
"max",
15+
"min",
1416
].some((pattern) => name.toLowerCase().includes(pattern))
1517
? "string | Buffer | number"
1618
: "string | Buffer",

‎lib/Redis.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type RedisStatus =
4747

4848
class Redis extends Commander {
4949
static Cluster = Cluster;
50+
static Command = Command;
5051
/**
5152
* Default options
5253
*/
@@ -698,7 +699,7 @@ class Redis extends Commander {
698699

699700
/**
700701
* Flush offline queue and command queue with error.
701-
*
702+
*
702703
* @param error The error object to send to the commands
703704
* @param options options
704705
*/

‎lib/pipeline.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ Pipeline.prototype.multi = function () {
242242
return multi.apply(this, arguments);
243243
};
244244

245+
// @ts-expect-error
245246
const execBuffer = Pipeline.prototype.execBuffer;
246247
const exec = Pipeline.prototype.exec;
248+
// @ts-expect-error
247249
Pipeline.prototype.execBuffer = deprecate(function () {
248250
if (this._transactions > 0) {
249251
this._transactions -= 1;
@@ -257,7 +259,6 @@ Pipeline.prototype.execBuffer = deprecate(function () {
257259
//
258260
// If a different promise instance were returned, that promise would cause its own unhandled promise rejection
259261
// errors, even if that promise unconditionally resolved to **the resolved value of** this.promise.
260-
// @ts-expect-error
261262
Pipeline.prototype.exec = function (
262263
callback: CallbackFunction
263264
): Promise<Array<any>> {

‎lib/transaction.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import Pipeline from "./pipeline";
44
import { CallbackFunction } from "./types";
55
import { ChainableCommander } from "./utils/RedisCommander";
66

7+
interface MultiOptions {
8+
pipeline: boolean;
9+
}
10+
711
export interface Transaction {
812
pipeline(commands?: [name: string, ...args: unknown[]][]): ChainableCommander;
13+
multi(options?: MultiOptions): ChainableCommander;
914
multi(
1015
commands?: [name: string, ...args: unknown[]][],
11-
options?: { pipeline: boolean }
16+
options?: MultiOptions
1217
): ChainableCommander;
1318
}
1419

@@ -89,7 +94,9 @@ export function addTransactionSupport(redis) {
8994
);
9095
};
9196

97+
// @ts-expect-error
9298
const { execBuffer } = pipeline;
99+
// @ts-expect-error
93100
pipeline.execBuffer = function (callback: CallbackFunction) {
94101
if (this._transactions > 0) {
95102
execBuffer.call(pipeline);

‎lib/utils/RedisCommander.ts

+9,939-1,341
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.