Skip to content

Commit

Permalink
feat: add declarations for methods
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Feb 20, 2022
1 parent ec96ba9 commit 99d9371
Show file tree
Hide file tree
Showing 15 changed files with 12,661 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"plugin:@typescript-eslint/eslint-recommended",
"prettier"
],
"ignorePatterns": ["bin/generateRedisCommander/template.ts"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"env": { "node": true },
Expand All @@ -18,6 +19,7 @@
"@typescript-eslint/no-this-alias": 0,
"@typescript-eslint/ban-ts-ignore": 0,
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/adjacent-overload-signatures": 0,
"@typescript-eslint/ban-types": 0,
"@typescript-eslint/no-empty-interface": 0,
"@typescript-eslint/no-empty-function": 0,
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node_modules
*.cpuprofile
/test.js
/test.*
/.idea
built

Expand Down
11 changes: 11 additions & 0 deletions bin/generateRedisCommander/argumentTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const typeMaps = require("./typeMaps");

module.exports = {
debug: [
[{ name: "subcommand", type: "string" }],
[
{ name: "subcommand", type: "string" },
{ name: "args", type: typeMaps.string, multiple: true },
],
],
};
36 changes: 36 additions & 0 deletions bin/generateRedisCommander/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const returnTypes = require("./returnTypes");
const argumentTypes = require("./argumentTypes");
const typeMaps = require("./typeMaps");
const { getCommanderInterface } = require("ioredis-interface-generator");

const ignoredCommands = ["monitor", "multi"];
const commands = require("redis-commands").list.filter(
(name) => !ignoredCommands.includes(name)
);

const fs = require("fs");
const path = require("path");

const template = fs.readFileSync(path.join(__dirname, "/template.ts"), "utf8");

async function main() {
const interface = await getCommanderInterface({
commands,
complexityLimit: 50,
redisOpts: {
port: process.env.REDIS_PORT,
},
returnTypes,
argumentTypes,
typeMaps: typeMaps,
});

fs.writeFileSync(
path.join(__dirname, "..", "..", "lib/utils/RedisCommander.ts"),
template.replace("////", () => interface)
);
}

main()
.catch(console.error)
.then(() => process.exit(0));
222 changes: 222 additions & 0 deletions bin/generateRedisCommander/returnTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
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";
}
return "'OK'";
},
ping: (types) => {
return types.length ? "string" : "'PONG'";
},
append: "number",
asking: "'OK'",
auth: "'OK'",
bgrewriteaof: "string",
bgsave: "'OK'",
bitcount: "number",
bitfield_ro: "unknown[]",
bitop: "number",
bitpos: "number",
blpop: "[string, string] | null",
brpop: "[string, string] | null",
brpoplpush: "string | null",
blmove: "string | null",
lmpop: "unknown[] | null",
blmpop: "unknown[] | null",
bzpopmin: "unknown[] | null",
bzpopmax: "unknown[] | null",
command: "unknown[]",
copy: "number",
dbsize: "number",
decr: "number",
decrby: "number",
del: "number",
discard: "'OK'",
dump: "string",
echo: "string",
exec: "[error: Error, result: unknown][] | null",
exists: "number",
expire: "number",
expireat: "number",
expiretime: "number",
failover: "'OK'",
flushall: "'OK'",
flushdb: "'OK'",
geoadd: "number",
geohash: "string[]",
geopos: "unknown[] | null",
geodist: "string | null",
georadius: "unknown[]",
geosearch: "unknown[]",
geosearchstore: "number",
getbit: "number",
getdel: "string | null",
getex: "string | null",
getrange: "string",
getset: "string | null",
hdel: "number",
hello: "unknown[]",
hexists: "number",
hget: "string | null",
hincrby: "number",
hincrbyfloat: "string",
hkeys: "string[]",
hlen: "number",
hmget: "(string | null)[]",
hmset: "'OK'",
hset: "number",
hsetnx: "number",
hrandfield: "string | unknown[] | null",
hstrlen: "number",
hvals: "string[]",
incr: "number",
incrby: "number",
incrbyfloat: "string",
info: "string",
lolwut: "string",
keys: "string[]",
lastsave: "number",
lindex: "string | null",
linsert: "number",
llen: "number",
lpop: "string" | "unknown[] | null",
lpos: null,
lpush: "number",
lpushx: "number",
lrange: "string[]",
lrem: "number",
lset: "'OK'",
ltrim: "'OK'",
mget: "(string | null)[]",
migrate: "'OK'",
move: "number",
mset: "'OK'",
msetnx: "number",
persist: "number",
pexpire: "number",
pexpireat: "number",
pexpiretime: "number",
pfadd: "number",
pfcount: "number",
pfmerge: "'OK'",
pubsub: "unknown[]",
pttl: "number",
publish: "number",
quit: "'OK'",
randomkey: "string | null",
readonly: "'OK'",
readwrite: "'OK'",
rename: "'OK'",
renamenx: "number",
reset: "'OK'",
restore: "'OK'",
role: "unknown[]",
rpop: "string" | "unknown[] | null",
rpoplpush: "string",
lmove: "string",
rpush: "number",
rpushx: "number",
sadd: "number",
save: "'OK'",
scard: "number",
sdiff: "string[]",
sdiffstore: "number",
select: "'OK'",
setbit: "number",
setex: "'OK'",
setnx: "number",
setrange: "number",
shutdown: "'OK'",
sinter: "string[]",
sintercard: "number",
sinterstore: "number",
sismember: "number",
smismember: "unknown[]",
slaveof: "'OK'",
replicaof: "'OK'",
smembers: "string[]",
smove: "number",
sort: "number" | "unknown[]",
sortRo: "unknown[]",
spop: (types) => (types.length > 1 ? "string[]" : "string | null"),
srandmember: "string" | "unknown[] | null",
srem: "number",
strlen: "number",
sunion: "string[]",
sunionstore: "number",
swapdb: "'OK'",
time: "number[]",
touch: "number",
ttl: "number",
type: "string",
unlink: "number",
unwatch: "'OK'",
wait: "number",
watch: "'OK'",
zadd: (types) => {
if (types.find((type) => type.includes("INCR"))) {
if (types.find((type) => type.includes("XX") || type.includes("NX"))) {
return "string | null";
}
return "string";
}
return "number";
},
zcard: "number",
zcount: "number",
zdiff: "string[]",
zdiffstore: "number",
zincrby: "string",
zinter: "string[]",
zintercard: "number",
zinterstore: "number",
zlexcount: "number",
zpopmax: "string[]",
zpopmin: "string[]",
zrandmember: (types) => {
return types.includes("number") ? "string | null" : "string[]";
},
zrangestore: "number",
zrange: "string[]",
zrangebylex: "string[]",
zrevrangebylex: "string[]",
zrangebyscore: "string[]",
zrank: "number | null",
zrem: "number",
zremrangebylex: "number",
zremrangebyrank: "number",
zremrangebyscore: "number",
zrevrange: "string[]",
zrevrangebyscore: "string[]",
zrevrank: "number | null",
zscore: "string",
zunion: "unknown[]",
zmscore: "unknown[] | null",
zunionstore: "number",
scan: "[cursor: string, elements: string[]]",
sscan: "[cursor: string, elements: string[]]",
hscan: "[cursor: string, elements: string[]]",
zscan: "[cursor: string, elements: string[]]",
xadd: "string | null",
xtrim: "number",
xdel: "number",
xrange: "[id: string, fields: string[]][]",
xrevrange: "[id: string, fields: string[]][]",
xlen: "number",
xread: (types) => {
if (types.find((type) => type.includes("BLOCK"))) {
return "unknown[] | null";
}
return "unknown[]";
},
xreadgroup: "unknown[]",
xack: "number",
xclaim: "unknown[]",
xautoclaim: "unknown[]",
xpending: "unknown[]",
};
26 changes: 26 additions & 0 deletions bin/generateRedisCommander/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type RedisKey = string | Buffer;
type RedisValue = string | Buffer | number;
type Callback<T> = (err: Error | null, 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
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface ResultTypes<Result, Context> {
default: Promise<Result>;
pipeline: ChainableCommander;
}

export interface ChainableCommander
extends RedisCommander<{ type: "pipeline" }> {}

export type ClientContext = { type: keyof ResultTypes<unknown, unknown> };
export type Result<T, Context extends ClientContext> =
// prettier-break
ResultTypes<T, Context>[Context["type"]];

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

export default RedisCommander;
6 changes: 6 additions & 0 deletions bin/generateRedisCommander/typeMaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
key: "RedisKey",
string: "string | Buffer | number",
pattern: "string",
number: "number | string",
};
9 changes: 4 additions & 5 deletions lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { EventEmitter } from "events";
import * as commands from "redis-commands";
import asCallback from "standard-as-callback";
import { AbstractConnector, Command, ScanStream, SentinelConnector } from ".";
import Commander from "./utils/Commander";
import { StandaloneConnector } from "./connectors";
import * as eventHandler from "./redis/event_handler";
import {
DEFAULT_REDIS_OPTIONS,
ReconnectOnError,
RedisOptions,
} from "./redis/RedisOptions";
import { addTransactionSupport } from "./transaction";
import { addTransactionSupport, Transaction } from "./transaction";
import { CallbackFunction, ICommandItem, NetStream } from "./types";
import {
CONNECTION_CLOSED_ERROR_MSG,
Expand All @@ -20,6 +19,7 @@ import {
resolveTLSProfile,
} from "./utils";
import applyMixin from "./utils/applyMixin";
import Commander from "./utils/Commander";
import { defaults, noop } from "./utils/lodash";
import Deque = require("denque");
const debug = Debug("redis");
Expand Down Expand Up @@ -409,7 +409,6 @@ class Redis extends Commander {
this.condition.select !== item.select &&
item.command.name !== "select"
) {
// @ts-expect-error
this.select(item.select);
}
// TODO
Expand Down Expand Up @@ -460,8 +459,7 @@ class Redis extends Commander {
*/
private _readyCheck(callback: CallbackFunction) {
const _this = this;
// @ts-expect-error
this.info(function (err: Error | null, res: string) {
this.info(function (err, res) {
if (err) {
return callback(err);
}
Expand Down Expand Up @@ -782,5 +780,6 @@ applyMixin(Redis, EventEmitter);
});

addTransactionSupport(Redis.prototype);
interface Redis extends Transaction {}

export default Redis;
4 changes: 2 additions & 2 deletions lib/autoPipelining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function shouldUseAutoPipelining(
*/
export function getFirstValueInFlattenedArray(
args: ArgumentType[]
): string | undefined {
): string | Buffer | number | null | undefined {
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (typeof arg === "string") {
Expand All @@ -107,7 +107,7 @@ export function getFirstValueInFlattenedArray(
}
const flattened = flatten([arg]);
if (flattened.length > 0) {
return String(flattened[0]);
return flattened[0];
}
}
return undefined;
Expand Down
Loading

0 comments on commit 99d9371

Please sign in to comment.