From 7d97b3c508b4a9bcd1e72b0ea27fe9f24f0282b1 Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Sun, 27 Mar 2022 11:06:19 +0800 Subject: [PATCH] fix: support TypeScript interface as parameters of hmset and mset Closes #1536 --- bin/overrides.js | 9 ++-- lib/utils/RedisCommander.ts | 32 ++--------- test/functional/transformer.ts | 85 ++++++++++-------------------- test/typing/transformers.test-d.ts | 31 +++++++++++ 4 files changed, 66 insertions(+), 91 deletions(-) create mode 100644 test/typing/transformers.test-d.ts diff --git a/bin/overrides.js b/bin/overrides.js index 42ca1bdf..96a8606f 100644 --- a/bin/overrides.js +++ b/bin/overrides.js @@ -1,8 +1,7 @@ const msetOverrides = { overwrite: false, defs: [ - "$1(object: Record, callback?: Callback<'OK'>): Result<'OK', Context>", - "$1(map: Map, callback?: Callback<'OK'>): Result<'OK', Context>", + "$1(object: object, callback?: Callback<'OK'>): Result<'OK', Context>", ], }; @@ -19,15 +18,13 @@ module.exports = { hset: { overwrite: false, defs: [ - "$1(key: RedisKey, object: Record, callback?: Callback): Result", - "$1(key: RedisKey, map: Map, callback?: Callback): Result", + "$1(key: RedisKey, object: object, callback?: Callback): Result", ], }, hmset: { overwrite: false, defs: [ - "$1(key: RedisKey, object: Record, callback?: Callback<'OK'>): Result<'OK', Context>", - "$1(key: RedisKey, map: Map, callback?: Callback<'OK'>): Result<'OK', Context>", + "$1(key: RedisKey, object: object, callback?: Callback<'OK'>): Result<'OK', Context>", ], }, exec: { diff --git a/lib/utils/RedisCommander.ts b/lib/utils/RedisCommander.ts index 5aabbecc..8d26741c 100644 --- a/lib/utils/RedisCommander.ts +++ b/lib/utils/RedisCommander.ts @@ -4280,12 +4280,7 @@ interface RedisCommander { */ hmset( key: RedisKey, - object: Record, - callback?: Callback<"OK"> - ): Result<"OK", Context>; - hmset( - key: RedisKey, - map: Map, + object: object, callback?: Callback<"OK"> ): Result<"OK", Context>; hmset( @@ -4407,12 +4402,7 @@ interface RedisCommander { */ hset( key: RedisKey, - object: Record, - callback?: Callback - ): Result; - hset( - key: RedisKey, - map: Map, + object: object, callback?: Callback ): Result; hset( @@ -5513,14 +5503,7 @@ interface RedisCommander { * - _complexity_: O(N) where N is the number of keys to set. * - _since_: 1.0.1 */ - mset( - object: Record, - callback?: Callback<"OK"> - ): Result<"OK", Context>; - mset( - map: Map, - callback?: Callback<"OK"> - ): Result<"OK", Context>; + mset(object: object, callback?: Callback<"OK">): Result<"OK", Context>; mset( ...args: [ ...keyValues: (RedisKey | string | Buffer | number)[], @@ -5537,14 +5520,7 @@ interface RedisCommander { * - _complexity_: O(N) where N is the number of keys to set. * - _since_: 1.0.1 */ - msetnx( - object: Record, - callback?: Callback<"OK"> - ): Result<"OK", Context>; - msetnx( - map: Map, - callback?: Callback<"OK"> - ): Result<"OK", Context>; + msetnx(object: object, callback?: Callback<"OK">): Result<"OK", Context>; msetnx( ...args: [ ...keyValues: (RedisKey | string | Buffer | number)[], diff --git a/test/functional/transformer.ts b/test/functional/transformer.ts index 50cb2d36..d54b8da5 100644 --- a/test/functional/transformer.ts +++ b/test/functional/transformer.ts @@ -5,85 +5,56 @@ import { expect } from "chai"; describe("transformer", () => { describe("default transformer", () => { describe("hmset", () => { - it("should support object", (done) => { + it("should support object", async () => { const redis = new Redis(); - redis.hmset("foo", { a: 1, b: "2" }, function (err, result) { - expect(result).to.eql("OK"); - redis.hget("foo", "b", function (err, result) { - expect(result).to.eql("2"); - done(); - }); - }); + expect(await redis.hmset("foo", { a: 1, b: "2" })).to.eql("OK"); + expect(await redis.hget("foo", "b")).to.eql("2"); }); - it("should support Map", (done) => { + + it("should support Map", async () => { const redis = new Redis(); const map = new Map(); map.set("a", 1); map.set("b", "2"); - redis.hmset("foo", map, function (err, result) { - expect(result).to.eql("OK"); - redis.hget("foo", "b", function (err, result) { - expect(result).to.eql("2"); - done(); - }); - }); + expect(await redis.hmset("foo", map)).to.eql("OK"); + expect(await redis.hget("foo", "b")).to.eql("2"); }); - it("should not affect the old way", (done) => { + + it("should not affect the old way", async () => { const redis = new Redis(); - redis.hmset("foo", "a", 1, "b", "2", function (err, result) { - expect(result).to.eql("OK"); - redis.hget("foo", "b", function (err, result) { - expect(result).to.eql("2"); - done(); - }); - }); + expect(await redis.hmset("foo", "a", 1, "b", "2")).to.eql("OK"); + expect(await redis.hget("foo", "b")).to.eql("2"); }); }); describe("mset", () => { - it("should support object", (done) => { + it("should support object", async () => { const redis = new Redis(); - redis.mset({ a: 1, b: "2" }, function (err, result) { - expect(result).to.eql("OK"); - redis.mget("a", "b", function (err, result) { - expect(result).to.eql(["1", "2"]); - done(); - }); - }); + expect(await redis.mset({ a: 1, b: "2" })).to.eql("OK"); + expect(await redis.mget("a", "b")).to.eql(["1", "2"]); }); - it("should support Map", (done) => { + + it("should support Map", async () => { const redis = new Redis(); const map = new Map(); map.set("a", 1); map.set("b", "2"); - redis.mset(map, function (err, result) { - expect(result).to.eql("OK"); - redis.mget("a", "b", function (err, result) { - expect(result).to.eql(["1", "2"]); - done(); - }); - }); + expect(await redis.mset(map)).to.eql("OK"); + expect(await redis.mget("a", "b")).to.eql(["1", "2"]); }); - it("should not affect the old way", (done) => { + + it("should not affect the old way", async () => { const redis = new Redis(); - redis.mset("a", 1, "b", "2", function (err, result) { - expect(result).to.eql("OK"); - redis.mget("a", "b", function (err, result) { - expect(result).to.eql(["1", "2"]); - done(); - }); - }); + expect(await redis.mset("a", 1, "b", "2")).to.eql("OK"); + expect(await redis.mget("a", "b")).to.eql(["1", "2"]); }); - it("should work with keyPrefix option", (done) => { + + it("should work with keyPrefix option", async () => { const redis = new Redis({ keyPrefix: "foo:" }); - redis.mset({ a: 1, b: "2" }, function (err, result) { - expect(result).to.eql("OK"); - const otherRedis = new Redis(); - otherRedis.mget("foo:a", "foo:b", function (err, result) { - expect(result).to.eql(["1", "2"]); - done(); - }); - }); + expect(await redis.mset({ a: 1, b: "2" })).to.eql("OK"); + + const otherRedis = new Redis(); + expect(await otherRedis.mget("foo:a", "foo:b")).to.eql(["1", "2"]); }); }); diff --git a/test/typing/transformers.test-d.ts b/test/typing/transformers.test-d.ts new file mode 100644 index 00000000..ab80580a --- /dev/null +++ b/test/typing/transformers.test-d.ts @@ -0,0 +1,31 @@ +import { expectType } from "tsd"; +import Redis from "../../built"; + +interface User { + name: string; + title: string; +} + +const user: User = { name: "Bob", title: "Engineer" }; +const map = new Map([["key", "value"]]); + +const redis = new Redis(); + +// mset +expectType>(redis.mset(user)); +expectType>(redis.mset(map)); + +// msetnx +expectType>(redis.msetnx(user)); +expectType>(redis.msetnx(map)); + +// hmset +expectType>(redis.hmset("key", user)); +expectType>(redis.hmset("key", map)); + +// hset +expectType>(redis.hset("key", user)); +expectType>(redis.hset("key", map)); + +// hgetall +expectType>>(redis.hgetall("key"));