Skip to content

Commit

Permalink
feat: improve typings for transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Mar 14, 2022
1 parent 48ca227 commit 94c1e24
Show file tree
Hide file tree
Showing 8 changed files with 10,019 additions and 1,346 deletions.
2 changes: 2 additions & 0 deletions bin/generateRedisCommander/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const returnTypes = require("./returnTypes");
const argumentTypes = require("./argumentTypes");
const typeMaps = require("./typeMaps");
const overrides = require("./overrides");
const { getCommanderInterface } = require("@ioredis/interface-generator");

const ignoredCommands = ["monitor", "multi"];
Expand All @@ -20,6 +21,7 @@ async function main() {
redisOpts: {
port: process.env.REDIS_PORT,
},
overrides,
returnTypes,
argumentTypes,
typeMaps: typeMaps,
Expand Down
39 changes: 39 additions & 0 deletions bin/generateRedisCommander/overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const msetOverrides = {
overwrite: false,
defs: [
"$1(object: Record<string, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
"$1(map: Map<string | Buffer | number, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
],
};

module.exports = {
hgetall: {
overwrite: true,
defs: [
"$1(key: RedisKey, callback?: Callback<Record<string, string>>): Result<Record<string, string>, Context>",
"$1Buffer(key: RedisKey, callback?: Callback<[field: Buffer, value: Buffer][]>): Result<[field: Buffer, value: Buffer][], Context>",
],
},
mset: msetOverrides,
msetnx: msetOverrides,
hset: {
overwrite: false,
defs: [
"$1(key: RedisKey, object: Record<string, string | Buffer | number>, callback?: Callback<number>): Result<number, Context>",
"$1(key: RedisKey, map: Map<string | Buffer | number, string | Buffer | number>, callback?: Callback<number>): Result<number, Context>",
],
},
hmset: {
overwrite: false,
defs: [
"$1(key: RedisKey, object: Record<string, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
"$1(key: RedisKey, map: Map<string | Buffer | number, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
],
},
exec: {
overwrite: true,
defs: [
"exec(callback?: Callback<[error: Error, result: unknown][] | null>): Promise<[error: Error, result: unknown][] | null>;",
],
},
};
27 changes: 25 additions & 2 deletions bin/generateRedisCommander/returnTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ module.exports = {
hello: "unknown[]",
hexists: "number",
hget: "string | null",
hgetall: "[field: string, value: string][]",
hincrby: "number",
hincrbyfloat: "string",
hkeys: "string[]",
Expand All @@ -89,6 +90,28 @@ module.exports = {
hmset: "'OK'",
hset: "number",
hsetnx: "number",
acl: (types) => {
if (matchSubcommand(types, "SAVE")) return '"OK"';
if (matchSubcommand(types, "DELUSER")) return "number";
if (matchSubcommand(types, "WHOAMI")) return "string";
if (matchSubcommand(types, "DRYRUN")) return "string";
if (matchSubcommand(types, "GENPASS")) return "string";
if (matchSubcommand(types, "GETUSER")) return "string[] | null";
if (matchSubcommand(types, "LIST")) return "string[]";
if (matchSubcommand(types, "USERS")) return "string[]";
if (matchSubcommand(types, "LOAD")) return '"OK"';
if (matchSubcommand(types, "SETUSER")) return '"OK"';
},
client: (types) => {
if (matchSubcommand(types, "CACHING")) return '"OK"';
if (matchSubcommand(types, "PAUSE")) return '"OK"';
if (matchSubcommand(types, "UNPAUSE")) return '"OK"';
if (matchSubcommand(types, "SETNAME")) return '"OK"';
if (matchSubcommand(types, "GETNAME")) return "string | null";
if (matchSubcommand(types, "GETREDIR")) return "number";
if (matchSubcommand(types, "INFO")) return "string";
if (matchSubcommand(types, "ID")) return "number";
},
memory: (types) => {
if (matchSubcommand(types, "MALLOC-STATS")) return "string";
if (matchSubcommand(types, "PURGE")) return '"OK"';
Expand Down Expand Up @@ -227,8 +250,8 @@ module.exports = {
zrevrangebyscore: "string[]",
zrevrank: "number | null",
zscore: "string",
zunion: "unknown[]",
zmscore: "unknown[] | null",
zunion: "string[]",
zmscore: "(string | null)[]",
zunionstore: "number",
scan: "[cursor: string, elements: string[]]",
sscan: "[cursor: string, elements: string[]]",
Expand Down
2 changes: 2 additions & 0 deletions bin/generateRedisCommander/typeMaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = {
"threshold",
"start",
"end",
"max",
"min",
].some((pattern) => name.toLowerCase().includes(pattern))
? "string | Buffer | number"
: "string | Buffer",
Expand Down
3 changes: 2 additions & 1 deletion lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type RedisStatus =

class Redis extends Commander {
static Cluster = Cluster;
static Command = Command;
/**
* Default options
*/
Expand Down Expand Up @@ -698,7 +699,7 @@ class Redis extends Commander {

/**
* Flush offline queue and command queue with error.
*
*
* @param error The error object to send to the commands
* @param options options
*/
Expand Down
3 changes: 2 additions & 1 deletion lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,10 @@ Pipeline.prototype.multi = function () {
return multi.apply(this, arguments);
};

// @ts-expect-error
const execBuffer = Pipeline.prototype.execBuffer;
const exec = Pipeline.prototype.exec;
// @ts-expect-error
Pipeline.prototype.execBuffer = deprecate(function () {
if (this._transactions > 0) {
this._transactions -= 1;
Expand All @@ -257,7 +259,6 @@ Pipeline.prototype.execBuffer = deprecate(function () {
//
// If a different promise instance were returned, that promise would cause its own unhandled promise rejection
// errors, even if that promise unconditionally resolved to **the resolved value of** this.promise.
// @ts-expect-error
Pipeline.prototype.exec = function (
callback: CallbackFunction
): Promise<Array<any>> {
Expand Down
9 changes: 8 additions & 1 deletion lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import Pipeline from "./pipeline";
import { CallbackFunction } from "./types";
import { ChainableCommander } from "./utils/RedisCommander";

interface MultiOptions {
pipeline: boolean;
}

export interface Transaction {
pipeline(commands?: [name: string, ...args: unknown[]][]): ChainableCommander;
multi(options?: MultiOptions): ChainableCommander;
multi(
commands?: [name: string, ...args: unknown[]][],
options?: { pipeline: boolean }
options?: MultiOptions
): ChainableCommander;
}

Expand Down Expand Up @@ -89,7 +94,9 @@ export function addTransactionSupport(redis) {
);
};

// @ts-expect-error
const { execBuffer } = pipeline;
// @ts-expect-error
pipeline.execBuffer = function (callback: CallbackFunction) {
if (this._transactions > 0) {
execBuffer.call(pipeline);
Expand Down
Loading

0 comments on commit 94c1e24

Please # to comment.