How to create a child/proxy class of the client in TypeScript? #2841
-
We have a few frequently used methods from the Redis client that we'd like to collection some metrics on, so I'm looking into creating a child/proxy class of the Redis client like export class RedisClientWithMetrics extends RedisClientType {
override async get(key: RedisCommandArgument): Promise<string | null> {
const res = await super.get(key)
sendMetrics({ key, hit: !!res })
return res
}
}
const client = new RedisClientWithMetrics({/* options */})
client.get('foo') // has metrics
client.del('foo') // calls the original del() directly (Pardon my rough pseudo code, quite new to the TypeScript world 🤦) But now since Can someone please shed some light on me? Maybe I had been looking into the wrong directions and it's actually quite simple? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
As long as you are not changing the function signature you can just override the function without worrying about the types (as they can stay as is) const originalGet = client.get.bind(client);
client.get = async key => {
const result = await originalGet(key);
// ...
return result;
}; |
Beta Was this translation helpful? Give feedback.
As long as you are not changing the function signature you can just override the function without worrying about the types (as they can stay as is)