Skip to content

Commit 7a436b1

Browse files
committedMar 14, 2022
feat: expose official declarations
1 parent a29d9c4 commit 7a436b1

35 files changed

+2775
-11155
lines changed
 

‎.eslintrc.json

+29
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@
2020
"@typescript-eslint/ban-ts-comment": 0,
2121
"@typescript-eslint/adjacent-overload-signatures": 0,
2222
"@typescript-eslint/ban-types": 0,
23+
"@typescript-eslint/member-ordering": [
24+
1,
25+
{
26+
"default": {
27+
"memberTypes": [
28+
"public-static-field",
29+
"protected-static-field",
30+
"private-static-field",
31+
32+
"public-static-method",
33+
"protected-static-method",
34+
"private-static-method",
35+
36+
"public-instance-field",
37+
"protected-instance-field",
38+
"private-instance-field",
39+
40+
"public-constructor",
41+
"private-constructor",
42+
"protected-constructor",
43+
44+
"public-instance-method",
45+
"protected-instance-method",
46+
"private-instance-method"
47+
]
48+
}
49+
}
50+
],
51+
"@typescript-eslint/explicit-member-accessibility": [1, { "accessibility": "no-public" }],
2352
"@typescript-eslint/no-empty-interface": 0,
2453
"@typescript-eslint/no-empty-function": 0,
2554
"@typescript-eslint/no-unused-vars": [

‎bin/generateRedisCommander/argumentTypes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
[{ name: "subcommand", type: "string" }],
66
[
77
{ name: "subcommand", type: "string" },
8-
{ name: "args", type: typeMaps.string, multiple: true },
8+
{ name: "args", type: typeMaps.string("args"), multiple: true },
99
],
1010
],
1111
};

‎bin/generateRedisCommander/index.js

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

66
const ignoredCommands = ["monitor", "multi"];
77
const commands = require("redis-commands").list.filter(
@@ -23,6 +23,14 @@ async function main() {
2323
returnTypes,
2424
argumentTypes,
2525
typeMaps: typeMaps,
26+
ignoredBufferVariant: [
27+
"incrbyfloat",
28+
"type",
29+
"info",
30+
"latency",
31+
"lolwut",
32+
"memory",
33+
],
2634
});
2735

2836
fs.writeFileSync(

‎bin/generateRedisCommander/returnTypes.js

+42-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1+
const hasToken = (types, token) => {
2+
if (Array.isArray(token)) return token.some((t) => hasToken(types, t));
3+
return types.find((type) => type.includes(token));
4+
};
5+
6+
const matchSubcommand = (types, subcommand) => {
7+
if (Array.isArray(subcommand))
8+
return subcommand.some((s) => matchSubcommand(types, s));
9+
return types[0].includes(subcommand);
10+
};
11+
112
module.exports = {
213
multi: "ChainableCommander",
314
get: "string | null",
415
set: (types) => {
5-
if (types.find((type) => type.includes("GET"))) {
6-
return "string | null";
7-
}
8-
if (types.find((type) => type.includes("NX") || type.includes("XX"))) {
9-
return "'OK' | null";
10-
}
16+
if (hasToken(types, "GET")) return "string | null";
17+
if (hasToken(types, ["NX", "XX"])) return "'OK' | null";
1118
return "'OK'";
1219
},
1320
ping: (types) => {
1421
return types.length ? "string" : "'PONG'";
1522
},
23+
latency: (types) => {
24+
if (matchSubcommand(types, ["HELP", "LATEST", "HISTORY"])) {
25+
return "unknown[]";
26+
}
27+
if (matchSubcommand(types, "RESET")) {
28+
return "number";
29+
}
30+
if (matchSubcommand(types, ["DOCTOR", "GRAPH"])) {
31+
return "string";
32+
}
33+
},
1634
append: "number",
1735
asking: "'OK'",
1836
auth: "'OK'",
@@ -71,6 +89,14 @@ module.exports = {
7189
hmset: "'OK'",
7290
hset: "number",
7391
hsetnx: "number",
92+
memory: (types) => {
93+
if (matchSubcommand(types, "MALLOC-STATS")) return "string";
94+
if (matchSubcommand(types, "PURGE")) return '"OK"';
95+
if (matchSubcommand(types, "HELP")) return "unknown[]";
96+
if (matchSubcommand(types, "STATS")) return "unknown[]";
97+
if (matchSubcommand(types, "USAGE")) return "number | null";
98+
if (matchSubcommand(types, "DOCTOR")) return "string";
99+
},
74100
hrandfield: "string | unknown[] | null",
75101
hstrlen: "number",
76102
hvals: "string[]",
@@ -84,8 +110,12 @@ module.exports = {
84110
lindex: "string | null",
85111
linsert: "number",
86112
llen: "number",
87-
lpop: "string" | "unknown[] | null",
88-
lpos: null,
113+
lpop: (types) => {
114+
return types.includes("number") ? "string[] | null" : "string | null";
115+
},
116+
lpos: (types) => {
117+
return hasToken(types, "COUNT") ? "number[]" : "number | null";
118+
},
89119
lpush: "number",
90120
lpushx: "number",
91121
lrange: "string[]",
@@ -116,7 +146,9 @@ module.exports = {
116146
reset: "'OK'",
117147
restore: "'OK'",
118148
role: "unknown[]",
119-
rpop: "string" | "unknown[] | null",
149+
rpop: (types) => {
150+
return types.includes("number") ? "string[] | null" : "string | null";
151+
},
120152
rpoplpush: "string",
121153
lmove: "string",
122154
rpush: "number",
@@ -144,7 +176,7 @@ module.exports = {
144176
sort: "number" | "unknown[]",
145177
sortRo: "unknown[]",
146178
spop: (types) => (types.length > 1 ? "string[]" : "string | null"),
147-
srandmember: "string" | "unknown[] | null",
179+
srandmember: "string | unknown[] | null",
148180
srem: "number",
149181
strlen: "number",
150182
sunion: "string[]",

‎bin/generateRedisCommander/template.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type RedisKey = string | Buffer;
22
type RedisValue = string | Buffer | number;
3-
type Callback<T> = (err: Error | null, res: T) => void;
3+
type Callback<T> = (err: Error | null | undefined, res?: T) => void;
44

55
// Inspired by https://github.com/mmkal/handy-redis/blob/main/src/generated/interface.ts.
66
// Should be fixed with https://github.com/Microsoft/TypeScript/issues/1213
@@ -19,7 +19,6 @@ export type Result<T, Context extends ClientContext> =
1919
ResultTypes<T, Context>[Context["type"]];
2020

2121
interface RedisCommander<Context extends ClientContext = { type: "default" }> {
22-
multi(): ChainableCommander;
2322
////
2423
}
2524

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
module.exports = {
22
key: "RedisKey",
3-
string: "string | Buffer | number",
3+
string: (name) =>
4+
[
5+
"value",
6+
"member",
7+
"element",
8+
"arg",
9+
"id",
10+
"pivot",
11+
"threshold",
12+
"start",
13+
"end",
14+
].some((pattern) => name.toLowerCase().includes(pattern))
15+
? "string | Buffer | number"
16+
: "string | Buffer",
417
pattern: "string",
5-
number: "number | string",
18+
number: (name) =>
19+
["seconds", "count", "start", "stop", "index"].some((pattern) =>
20+
name.toLowerCase().includes(pattern)
21+
)
22+
? "number"
23+
: "number | string",
624
};

0 commit comments

Comments
 (0)
Please sign in to comment.