How to create a child/proxy class of the client in TypeScript? #2841
Answered
by
leibale
Frederick888
asked this question in
Q&A
|
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. |
Answered by
leibale
Sep 20, 2024
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;
}; |
1 reply
Answer selected by
Frederick888
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)