Skip to content

Commit

Permalink
fix: support TypeScript interface as parameters of hmset and mset
Browse files Browse the repository at this point in the history
Closes #1536
  • Loading branch information
luin committed Mar 27, 2022
1 parent d57960a commit 7d97b3c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 91 deletions.
9 changes: 3 additions & 6 deletions bin/overrides.js
@@ -1,8 +1,7 @@
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>",
"$1(object: object, callback?: Callback<'OK'>): Result<'OK', Context>",
],
};

Expand All @@ -19,15 +18,13 @@ module.exports = {
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>",
"$1(key: RedisKey, object: object, 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>",
"$1(key: RedisKey, object: object, callback?: Callback<'OK'>): Result<'OK', Context>",
],
},
exec: {
Expand Down
32 changes: 4 additions & 28 deletions lib/utils/RedisCommander.ts
Expand Up @@ -4280,12 +4280,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
*/
hmset(
key: RedisKey,
object: Record<string, string | Buffer | number>,
callback?: Callback<"OK">
): Result<"OK", Context>;
hmset(
key: RedisKey,
map: Map<string | Buffer | number, string | Buffer | number>,
object: object,
callback?: Callback<"OK">
): Result<"OK", Context>;
hmset(
Expand Down Expand Up @@ -4407,12 +4402,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
*/
hset(
key: RedisKey,
object: Record<string, string | Buffer | number>,
callback?: Callback<number>
): Result<number, Context>;
hset(
key: RedisKey,
map: Map<string | Buffer | number, string | Buffer | number>,
object: object,
callback?: Callback<number>
): Result<number, Context>;
hset(
Expand Down Expand Up @@ -5513,14 +5503,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
* - _complexity_: O(N) where N is the number of keys to set.
* - _since_: 1.0.1
*/
mset(
object: Record<string, string | Buffer | number>,
callback?: Callback<"OK">
): Result<"OK", Context>;
mset(
map: Map<string | Buffer | number, string | Buffer | number>,
callback?: Callback<"OK">
): Result<"OK", Context>;
mset(object: object, callback?: Callback<"OK">): Result<"OK", Context>;
mset(
...args: [
...keyValues: (RedisKey | string | Buffer | number)[],
Expand All @@ -5537,14 +5520,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
* - _complexity_: O(N) where N is the number of keys to set.
* - _since_: 1.0.1
*/
msetnx(
object: Record<string, string | Buffer | number>,
callback?: Callback<"OK">
): Result<"OK", Context>;
msetnx(
map: Map<string | Buffer | number, string | Buffer | number>,
callback?: Callback<"OK">
): Result<"OK", Context>;
msetnx(object: object, callback?: Callback<"OK">): Result<"OK", Context>;
msetnx(
...args: [
...keyValues: (RedisKey | string | Buffer | number)[],
Expand Down
85 changes: 28 additions & 57 deletions test/functional/transformer.ts
Expand Up @@ -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"]);
});
});

Expand Down
31 changes: 31 additions & 0 deletions 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<Promise<"OK">>(redis.mset(user));
expectType<Promise<"OK">>(redis.mset(map));

// msetnx
expectType<Promise<"OK">>(redis.msetnx(user));
expectType<Promise<"OK">>(redis.msetnx(map));

// hmset
expectType<Promise<"OK">>(redis.hmset("key", user));
expectType<Promise<"OK">>(redis.hmset("key", map));

// hset
expectType<Promise<number>>(redis.hset("key", user));
expectType<Promise<number>>(redis.hset("key", map));

// hgetall
expectType<Promise<Record<string, string>>>(redis.hgetall("key"));

0 comments on commit 7d97b3c

Please sign in to comment.