Skip to content

Commit

Permalink
feat: expose official declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Mar 14, 2022
1 parent a29d9c4 commit 7a436b1
Show file tree
Hide file tree
Showing 35 changed files with 2,775 additions and 11,155 deletions.
29 changes: 29 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/adjacent-overload-signatures": 0,
"@typescript-eslint/ban-types": 0,
"@typescript-eslint/member-ordering": [
1,
{
"default": {
"memberTypes": [
"public-static-field",
"protected-static-field",
"private-static-field",

"public-static-method",
"protected-static-method",
"private-static-method",

"public-instance-field",
"protected-instance-field",
"private-instance-field",

"public-constructor",
"private-constructor",
"protected-constructor",

"public-instance-method",
"protected-instance-method",
"private-instance-method"
]
}
}
],
"@typescript-eslint/explicit-member-accessibility": [1, { "accessibility": "no-public" }],
"@typescript-eslint/no-empty-interface": 0,
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/no-unused-vars": [
Expand Down
2 changes: 1 addition & 1 deletion bin/generateRedisCommander/argumentTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
[{ name: "subcommand", type: "string" }],
[
{ name: "subcommand", type: "string" },
{ name: "args", type: typeMaps.string, multiple: true },
{ name: "args", type: typeMaps.string("args"), multiple: true },
],
],
};
10 changes: 9 additions & 1 deletion bin/generateRedisCommander/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const returnTypes = require("./returnTypes");
const argumentTypes = require("./argumentTypes");
const typeMaps = require("./typeMaps");
const { getCommanderInterface } = require("ioredis-interface-generator");
const { getCommanderInterface } = require("@ioredis/interface-generator");

const ignoredCommands = ["monitor", "multi"];
const commands = require("redis-commands").list.filter(
Expand All @@ -23,6 +23,14 @@ async function main() {
returnTypes,
argumentTypes,
typeMaps: typeMaps,
ignoredBufferVariant: [
"incrbyfloat",
"type",
"info",
"latency",
"lolwut",
"memory",
],
});

fs.writeFileSync(
Expand Down
52 changes: 42 additions & 10 deletions bin/generateRedisCommander/returnTypes.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
const hasToken = (types, token) => {
if (Array.isArray(token)) return token.some((t) => hasToken(types, t));
return types.find((type) => type.includes(token));
};

const matchSubcommand = (types, subcommand) => {
if (Array.isArray(subcommand))
return subcommand.some((s) => matchSubcommand(types, s));
return types[0].includes(subcommand);
};

module.exports = {
multi: "ChainableCommander",
get: "string | null",
set: (types) => {
if (types.find((type) => type.includes("GET"))) {
return "string | null";
}
if (types.find((type) => type.includes("NX") || type.includes("XX"))) {
return "'OK' | null";
}
if (hasToken(types, "GET")) return "string | null";
if (hasToken(types, ["NX", "XX"])) return "'OK' | null";
return "'OK'";
},
ping: (types) => {
return types.length ? "string" : "'PONG'";
},
latency: (types) => {
if (matchSubcommand(types, ["HELP", "LATEST", "HISTORY"])) {
return "unknown[]";
}
if (matchSubcommand(types, "RESET")) {
return "number";
}
if (matchSubcommand(types, ["DOCTOR", "GRAPH"])) {
return "string";
}
},
append: "number",
asking: "'OK'",
auth: "'OK'",
Expand Down Expand Up @@ -71,6 +89,14 @@ module.exports = {
hmset: "'OK'",
hset: "number",
hsetnx: "number",
memory: (types) => {
if (matchSubcommand(types, "MALLOC-STATS")) return "string";
if (matchSubcommand(types, "PURGE")) return '"OK"';
if (matchSubcommand(types, "HELP")) return "unknown[]";
if (matchSubcommand(types, "STATS")) return "unknown[]";
if (matchSubcommand(types, "USAGE")) return "number | null";
if (matchSubcommand(types, "DOCTOR")) return "string";
},
hrandfield: "string | unknown[] | null",
hstrlen: "number",
hvals: "string[]",
Expand All @@ -84,8 +110,12 @@ module.exports = {
lindex: "string | null",
linsert: "number",
llen: "number",
lpop: "string" | "unknown[] | null",
lpos: null,
lpop: (types) => {
return types.includes("number") ? "string[] | null" : "string | null";
},
lpos: (types) => {
return hasToken(types, "COUNT") ? "number[]" : "number | null";
},
lpush: "number",
lpushx: "number",
lrange: "string[]",
Expand Down Expand Up @@ -116,7 +146,9 @@ module.exports = {
reset: "'OK'",
restore: "'OK'",
role: "unknown[]",
rpop: "string" | "unknown[] | null",
rpop: (types) => {
return types.includes("number") ? "string[] | null" : "string | null";
},
rpoplpush: "string",
lmove: "string",
rpush: "number",
Expand Down Expand Up @@ -144,7 +176,7 @@ module.exports = {
sort: "number" | "unknown[]",
sortRo: "unknown[]",
spop: (types) => (types.length > 1 ? "string[]" : "string | null"),
srandmember: "string" | "unknown[] | null",
srandmember: "string | unknown[] | null",
srem: "number",
strlen: "number",
sunion: "string[]",
Expand Down
3 changes: 1 addition & 2 deletions bin/generateRedisCommander/template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type RedisKey = string | Buffer;
type RedisValue = string | Buffer | number;
type Callback<T> = (err: Error | null, res: T) => void;
type Callback<T> = (err: Error | null | undefined, res?: T) => void;

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

interface RedisCommander<Context extends ClientContext = { type: "default" }> {
multi(): ChainableCommander;
////
}

Expand Down
22 changes: 20 additions & 2 deletions bin/generateRedisCommander/typeMaps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
module.exports = {
key: "RedisKey",
string: "string | Buffer | number",
string: (name) =>
[
"value",
"member",
"element",
"arg",
"id",
"pivot",
"threshold",
"start",
"end",
].some((pattern) => name.toLowerCase().includes(pattern))
? "string | Buffer | number"
: "string | Buffer",
pattern: "string",
number: "number | string",
number: (name) =>
["seconds", "count", "start", "stop", "index"].some((pattern) =>
name.toLowerCase().includes(pattern)
)
? "number"
: "number | string",
};
Loading

0 comments on commit 7a436b1

Please sign in to comment.