Skip to content

Commit 3444791

Browse files
authoredMar 30, 2022
fix: support TypeScript interface as parameters of hmset and mset (#1545)
Closes #1536
1 parent d62a808 commit 3444791

File tree

4 files changed

+93
-70
lines changed

4 files changed

+93
-70
lines changed
 

‎bin/overrides.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const msetOverrides = {
22
overwrite: false,
33
defs: [
4-
"$1(object: Record<string, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
4+
"$1(object: object, callback?: Callback<'OK'>): Result<'OK', Context>",
55
"$1(map: Map<string | Buffer | number, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
66
],
77
};
@@ -19,14 +19,14 @@ module.exports = {
1919
hset: {
2020
overwrite: false,
2121
defs: [
22-
"$1(key: RedisKey, object: Record<string, string | Buffer | number>, callback?: Callback<number>): Result<number, Context>",
22+
"$1(key: RedisKey, object: object, callback?: Callback<number>): Result<number, Context>",
2323
"$1(key: RedisKey, map: Map<string | Buffer | number, string | Buffer | number>, callback?: Callback<number>): Result<number, Context>",
2424
],
2525
},
2626
hmset: {
2727
overwrite: false,
2828
defs: [
29-
"$1(key: RedisKey, object: Record<string, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
29+
"$1(key: RedisKey, object: object, callback?: Callback<'OK'>): Result<'OK', Context>",
3030
"$1(key: RedisKey, map: Map<string | Buffer | number, string | Buffer | number>, callback?: Callback<'OK'>): Result<'OK', Context>",
3131
],
3232
},

‎lib/utils/RedisCommander.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -4280,7 +4280,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
42804280
*/
42814281
hmset(
42824282
key: RedisKey,
4283-
object: Record<string, string | Buffer | number>,
4283+
object: object,
42844284
callback?: Callback<"OK">
42854285
): Result<"OK", Context>;
42864286
hmset(
@@ -4407,7 +4407,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
44074407
*/
44084408
hset(
44094409
key: RedisKey,
4410-
object: Record<string, string | Buffer | number>,
4410+
object: object,
44114411
callback?: Callback<number>
44124412
): Result<number, Context>;
44134413
hset(
@@ -5513,10 +5513,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
55135513
* - _complexity_: O(N) where N is the number of keys to set.
55145514
* - _since_: 1.0.1
55155515
*/
5516-
mset(
5517-
object: Record<string, string | Buffer | number>,
5518-
callback?: Callback<"OK">
5519-
): Result<"OK", Context>;
5516+
mset(object: object, callback?: Callback<"OK">): Result<"OK", Context>;
55205517
mset(
55215518
map: Map<string | Buffer | number, string | Buffer | number>,
55225519
callback?: Callback<"OK">
@@ -5537,10 +5534,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
55375534
* - _complexity_: O(N) where N is the number of keys to set.
55385535
* - _since_: 1.0.1
55395536
*/
5540-
msetnx(
5541-
object: Record<string, string | Buffer | number>,
5542-
callback?: Callback<"OK">
5543-
): Result<"OK", Context>;
5537+
msetnx(object: object, callback?: Callback<"OK">): Result<"OK", Context>;
55445538
msetnx(
55455539
map: Map<string | Buffer | number, string | Buffer | number>,
55465540
callback?: Callback<"OK">

‎test/functional/transformer.ts

+35-57
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,63 @@ import { expect } from "chai";
55
describe("transformer", () => {
66
describe("default transformer", () => {
77
describe("hmset", () => {
8-
it("should support object", (done) => {
8+
it("should support object", async () => {
99
const redis = new Redis();
10-
redis.hmset("foo", { a: 1, b: "2" }, function (err, result) {
11-
expect(result).to.eql("OK");
12-
redis.hget("foo", "b", function (err, result) {
13-
expect(result).to.eql("2");
14-
done();
15-
});
16-
});
10+
expect(await redis.hmset("foo", { a: 1, b: "2" })).to.eql("OK");
11+
expect(await redis.hget("foo", "b")).to.eql("2");
1712
});
18-
it("should support Map", (done) => {
13+
14+
it("should support Map with string keys", async () => {
1915
const redis = new Redis();
2016
const map = new Map();
2117
map.set("a", 1);
2218
map.set("b", "2");
23-
redis.hmset("foo", map, function (err, result) {
24-
expect(result).to.eql("OK");
25-
redis.hget("foo", "b", function (err, result) {
26-
expect(result).to.eql("2");
27-
done();
28-
});
29-
});
19+
map.set(42, true);
20+
map.set(Buffer.from("buffer"), "utf8");
21+
map.set(Buffer.from([0xff]), "binary");
22+
expect(await redis.hmset("foo", map)).to.eql("OK");
23+
expect(await redis.hget("foo", "a")).to.eql("1");
24+
expect(await redis.hget("foo", "b")).to.eql("2");
25+
expect(await redis.hget("foo", "42")).to.eql("true");
26+
expect(await redis.hget("foo", "buffer")).to.eql("utf8");
27+
expect(await redis.hget("foo", Buffer.from([0xff]))).to.eql("binary");
3028
});
31-
it("should not affect the old way", (done) => {
29+
30+
it("should not affect the old way", async () => {
3231
const redis = new Redis();
33-
redis.hmset("foo", "a", 1, "b", "2", function (err, result) {
34-
expect(result).to.eql("OK");
35-
redis.hget("foo", "b", function (err, result) {
36-
expect(result).to.eql("2");
37-
done();
38-
});
39-
});
32+
expect(await redis.hmset("foo", "a", 1, "b", "2")).to.eql("OK");
33+
expect(await redis.hget("foo", "b")).to.eql("2");
4034
});
4135
});
4236

4337
describe("mset", () => {
44-
it("should support object", (done) => {
38+
it("should support object", async () => {
4539
const redis = new Redis();
46-
redis.mset({ a: 1, b: "2" }, function (err, result) {
47-
expect(result).to.eql("OK");
48-
redis.mget("a", "b", function (err, result) {
49-
expect(result).to.eql(["1", "2"]);
50-
done();
51-
});
52-
});
40+
expect(await redis.mset({ a: 1, b: "2" })).to.eql("OK");
41+
expect(await redis.mget("a", "b")).to.eql(["1", "2"]);
5342
});
54-
it("should support Map", (done) => {
43+
44+
it("should support Map", async () => {
5545
const redis = new Redis();
5646
const map = new Map();
5747
map.set("a", 1);
5848
map.set("b", "2");
59-
redis.mset(map, function (err, result) {
60-
expect(result).to.eql("OK");
61-
redis.mget("a", "b", function (err, result) {
62-
expect(result).to.eql(["1", "2"]);
63-
done();
64-
});
65-
});
49+
expect(await redis.mset(map)).to.eql("OK");
50+
expect(await redis.mget("a", "b")).to.eql(["1", "2"]);
6651
});
67-
it("should not affect the old way", (done) => {
52+
53+
it("should not affect the old way", async () => {
6854
const redis = new Redis();
69-
redis.mset("a", 1, "b", "2", function (err, result) {
70-
expect(result).to.eql("OK");
71-
redis.mget("a", "b", function (err, result) {
72-
expect(result).to.eql(["1", "2"]);
73-
done();
74-
});
75-
});
55+
expect(await redis.mset("a", 1, "b", "2")).to.eql("OK");
56+
expect(await redis.mget("a", "b")).to.eql(["1", "2"]);
7657
});
77-
it("should work with keyPrefix option", (done) => {
58+
59+
it("should work with keyPrefix option", async () => {
7860
const redis = new Redis({ keyPrefix: "foo:" });
79-
redis.mset({ a: 1, b: "2" }, function (err, result) {
80-
expect(result).to.eql("OK");
81-
const otherRedis = new Redis();
82-
otherRedis.mget("foo:a", "foo:b", function (err, result) {
83-
expect(result).to.eql(["1", "2"]);
84-
done();
85-
});
86-
});
61+
expect(await redis.mset({ a: 1, b: "2" })).to.eql("OK");
62+
63+
const otherRedis = new Redis();
64+
expect(await otherRedis.mget("foo:a", "foo:b")).to.eql(["1", "2"]);
8765
});
8866
});
8967

‎test/typing/transformers.test-d.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { expectType } from "tsd";
2+
import Redis from "../../built";
3+
4+
interface User {
5+
name: string;
6+
title: string;
7+
}
8+
9+
const user: User = { name: "Bob", title: "Engineer" };
10+
const stringMap = new Map([["key", "value"]]);
11+
const numberMap = new Map([[42, "value"]]);
12+
const bufferMap = new Map([[Buffer.from([0xff]), "value"]]);
13+
const mixedMap = new Map<string | Buffer | number, string>([
14+
[Buffer.from([0xff]), "value"],
15+
[42, "value"],
16+
["field", "value"],
17+
]);
18+
19+
const redis = new Redis();
20+
21+
// mset
22+
expectType<Promise<"OK">>(redis.mset("key1", "value1", "key2", "value2"));
23+
expectType<Promise<"OK">>(redis.mset(user));
24+
expectType<Promise<"OK">>(redis.mset(stringMap));
25+
expectType<Promise<"OK">>(redis.mset(numberMap));
26+
expectType<Promise<"OK">>(redis.mset(bufferMap));
27+
expectType<Promise<"OK">>(redis.mset(mixedMap));
28+
29+
// msetnx
30+
expectType<Promise<"OK">>(redis.msetnx(user));
31+
expectType<Promise<"OK">>(redis.msetnx(stringMap));
32+
expectType<Promise<"OK">>(redis.msetnx(numberMap));
33+
expectType<Promise<"OK">>(redis.msetnx(bufferMap));
34+
expectType<Promise<"OK">>(redis.msetnx(mixedMap));
35+
36+
// hmset
37+
expectType<Promise<"OK">>(redis.hmset("key", user));
38+
expectType<Promise<"OK">>(redis.hmset("key", stringMap));
39+
expectType<Promise<"OK">>(redis.hmset("key", numberMap));
40+
expectType<Promise<"OK">>(redis.hmset("key", bufferMap));
41+
expectType<Promise<"OK">>(redis.hmset("key", mixedMap));
42+
43+
// hset
44+
expectType<Promise<number>>(redis.hset("key", user));
45+
expectType<Promise<number>>(redis.hset("key", stringMap));
46+
expectType<Promise<number>>(redis.hset("key", numberMap));
47+
expectType<Promise<number>>(redis.hset("key", bufferMap));
48+
expectType<Promise<number>>(redis.hset("key", mixedMap));
49+
50+
// hgetall
51+
expectType<Promise<Record<string, string>>>(redis.hgetall("key"));

0 commit comments

Comments
 (0)
Please sign in to comment.