Skip to content

Commit

Permalink
fix: pass through end method (#202)
Browse files Browse the repository at this point in the history
* fix: pass through end method

* fix: avoid overly-strict Set.has type
  • Loading branch information
mmkal committed May 31, 2020
1 parent e537c98 commit a46546b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -101,3 +101,5 @@ If you cloned without `--recursive` you'll need to run `git submodule update --i
### Testing

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`.

Types are tested using [expect-type](https://npmjs.com/package/expect-type).
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -81,6 +81,7 @@
"cross-spawn": "^7.0.1",
"del-cli": "^3.0.0",
"dotenv-extended": "^2.3.0",
"expect-type": "^0.7.4",
"jest": "^25.1.0",
"lodash": "^4.17.14",
"npm-run-all": "^4.1.5",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -21,7 +21,7 @@ export const createHandyClient: ICreateHandyClient = (...clientArgs: any[]) => {

Object.keys(nodeRedis.__proto__).forEach((key: keyof IHandyRedis) => {
const func = nodeRedis[key];
if (useUnderlyingImpl.has(key)) {
if (useUnderlyingImpl.has(key as any)) {
handyClient[key] = func.bind(nodeRedis);
} else {
const wrapped = (...args: any[]) => new Promise((resolve, reject) => {
Expand Down
12 changes: 6 additions & 6 deletions src/overrides.ts
@@ -1,9 +1,7 @@
import { IHandyRedis } from "./generated/interface";
import { Multi } from "redis";
import { Multi, RedisClient } from "redis";

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

export const additionalFunctions = {
/** promisified multi execution */
Expand All @@ -12,4 +10,6 @@ export const additionalFunctions = {
),
};

export type AdditionalFunctions = typeof additionalFunctions;
export type AdditionalFunctions = typeof additionalFunctions & {
[K in UnderlyingImplMethods]: RedisClient[K];
};
6 changes: 6 additions & 0 deletions test/basic-usage.test.ts
Expand Up @@ -87,3 +87,9 @@ it("works with redis-mock", async () => {

expect(client.redis).toBe(mockClient);
});

it('has quit and end methods', async () => {
const client = createHandyClient();
expect(typeof client.quit).toBe('function');
expect(typeof client.end).toBe('function');
})
41 changes: 41 additions & 0 deletions test/types.test.ts
@@ -0,0 +1,41 @@
import { expectTypeOf } from "expect-type";
import { createHandyClient } from "../src";
import { RedisClient } from "redis";

test("create client with existing client", () => {
expectTypeOf(createHandyClient).toBeCallableWith({} as RedisClient)
})

test("client has promisified redis methods", () => {
expectTypeOf(createHandyClient)
.returns.toHaveProperty("get")
.returns.resolves.toEqualTypeOf<string | null>()

expectTypeOf(createHandyClient)
.returns.toHaveProperty("set")
.returns.resolves.toEqualTypeOf<string | null>()

expectTypeOf(createHandyClient)
.returns.toHaveProperty("geohash")
.parameters.toEqualTypeOf<[string, ...string[]]>()

expectTypeOf(createHandyClient)
.returns.toHaveProperty("geohash")
.returns.resolves.items.toBeString()

expectTypeOf(createHandyClient)
.returns.toHaveProperty("zrevrange")
.parameters.toEqualTypeOf<[string, number, number]>()

expectTypeOf(createHandyClient)
.returns.toHaveProperty("zrevrange")
.returns.resolves.items.toBeString()

expectTypeOf(createHandyClient)
.returns.toHaveProperty("quit")
.returns.resolves.toBeAny()

expectTypeOf(createHandyClient)
.returns.toHaveProperty("end")
.returns.toEqualTypeOf<void>()
})
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -2352,6 +2352,11 @@ expand-brackets@^2.1.4:
snapdragon "^0.8.1"
to-regex "^3.0.1"

expect-type@^0.7.4:
version "0.7.4"
resolved "https://registry.yarnpkg.com/expect-type/-/expect-type-0.7.4.tgz#588739f5e86e6713df49ae43812570f11225f9d7"
integrity sha512-/ykfDxhYq9vz/EgqH8YTeIehvb9YCP2W7qztpEqm84LaA23nOeGe6+H7huxxfncehig2bV0S0Gyhf+ncUwt2Mg==

expect@^25.4.0:
version "25.4.0"
resolved "https://registry.yarnpkg.com/expect/-/expect-25.4.0.tgz#0b16c17401906d1679d173e59f0d4580b22f8dc8"
Expand Down

0 comments on commit a46546b

Please sign in to comment.