Skip to content

Commit a46546b

Browse files
authored
fix: pass through end method (#202)
* fix: pass through end method * fix: avoid overly-strict Set.has type
1 parent e537c98 commit a46546b

File tree

7 files changed

+62
-7
lines changed

7 files changed

+62
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,5 @@ If you cloned without `--recursive` you'll need to run `git submodule update --i
101101
### Testing
102102

103103
If a snapshot test fails, it's possible it just needs to be updated. Make sure your git status is clean and run `npm test -- -u`.
104+
105+
Types are tested using [expect-type](https://npmjs.com/package/expect-type).

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"cross-spawn": "^7.0.1",
8282
"del-cli": "^3.0.0",
8383
"dotenv-extended": "^2.3.0",
84+
"expect-type": "^0.7.4",
8485
"jest": "^25.1.0",
8586
"lodash": "^4.17.14",
8687
"npm-run-all": "^4.1.5",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const createHandyClient: ICreateHandyClient = (...clientArgs: any[]) => {
2121

2222
Object.keys(nodeRedis.__proto__).forEach((key: keyof IHandyRedis) => {
2323
const func = nodeRedis[key];
24-
if (useUnderlyingImpl.has(key)) {
24+
if (useUnderlyingImpl.has(key as any)) {
2525
handyClient[key] = func.bind(nodeRedis);
2626
} else {
2727
const wrapped = (...args: any[]) => new Promise((resolve, reject) => {

src/overrides.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { IHandyRedis } from "./generated/interface";
2-
import { Multi } from "redis";
1+
import { Multi, RedisClient } from "redis";
32

4-
export const useUnderlyingImpl = new Set<keyof IHandyRedis>([
5-
"multi"
6-
]);
3+
export const useUnderlyingImpl = new Set(['multi', 'end'] as const)
4+
type UnderlyingImplMethods = typeof useUnderlyingImpl extends Set<infer X> ? X : never
75

86
export const additionalFunctions = {
97
/** promisified multi execution */
@@ -12,4 +10,6 @@ export const additionalFunctions = {
1210
),
1311
};
1412

15-
export type AdditionalFunctions = typeof additionalFunctions;
13+
export type AdditionalFunctions = typeof additionalFunctions & {
14+
[K in UnderlyingImplMethods]: RedisClient[K];
15+
};

test/basic-usage.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,9 @@ it("works with redis-mock", async () => {
8787

8888
expect(client.redis).toBe(mockClient);
8989
});
90+
91+
it('has quit and end methods', async () => {
92+
const client = createHandyClient();
93+
expect(typeof client.quit).toBe('function');
94+
expect(typeof client.end).toBe('function');
95+
})

test/types.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { expectTypeOf } from "expect-type";
2+
import { createHandyClient } from "../src";
3+
import { RedisClient } from "redis";
4+
5+
test("create client with existing client", () => {
6+
expectTypeOf(createHandyClient).toBeCallableWith({} as RedisClient)
7+
})
8+
9+
test("client has promisified redis methods", () => {
10+
expectTypeOf(createHandyClient)
11+
.returns.toHaveProperty("get")
12+
.returns.resolves.toEqualTypeOf<string | null>()
13+
14+
expectTypeOf(createHandyClient)
15+
.returns.toHaveProperty("set")
16+
.returns.resolves.toEqualTypeOf<string | null>()
17+
18+
expectTypeOf(createHandyClient)
19+
.returns.toHaveProperty("geohash")
20+
.parameters.toEqualTypeOf<[string, ...string[]]>()
21+
22+
expectTypeOf(createHandyClient)
23+
.returns.toHaveProperty("geohash")
24+
.returns.resolves.items.toBeString()
25+
26+
expectTypeOf(createHandyClient)
27+
.returns.toHaveProperty("zrevrange")
28+
.parameters.toEqualTypeOf<[string, number, number]>()
29+
30+
expectTypeOf(createHandyClient)
31+
.returns.toHaveProperty("zrevrange")
32+
.returns.resolves.items.toBeString()
33+
34+
expectTypeOf(createHandyClient)
35+
.returns.toHaveProperty("quit")
36+
.returns.resolves.toBeAny()
37+
38+
expectTypeOf(createHandyClient)
39+
.returns.toHaveProperty("end")
40+
.returns.toEqualTypeOf<void>()
41+
})

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,11 @@ expand-brackets@^2.1.4:
23522352
snapdragon "^0.8.1"
23532353
to-regex "^3.0.1"
23542354

2355+
expect-type@^0.7.4:
2356+
version "0.7.4"
2357+
resolved "https://registry.yarnpkg.com/expect-type/-/expect-type-0.7.4.tgz#588739f5e86e6713df49ae43812570f11225f9d7"
2358+
integrity sha512-/ykfDxhYq9vz/EgqH8YTeIehvb9YCP2W7qztpEqm84LaA23nOeGe6+H7huxxfncehig2bV0S0Gyhf+ncUwt2Mg==
2359+
23552360
expect@^25.4.0:
23562361
version "25.4.0"
23572362
resolved "https://registry.yarnpkg.com/expect/-/expect-25.4.0.tgz#0b16c17401906d1679d173e59f0d4580b22f8dc8"

0 commit comments

Comments
 (0)