Feature Request: decorators
I'd like to wrap my functions like this:
let limiter: RateLimiterAbstract;
//...
@limiter.enforce
// or for more tokens: @limiter.enforce(2)
function limitedFunction() {
// implementation
}
And the same on RateLimiterQueue.
Any interest in this feature?
@dmurvihill Hey, interesting approach. It could be created as a new package based on rate-limiter-flexible. After that we could add it to docs.
This is the implementation:
import { RateLimiterQueue } from "rate-limiter-flexible";
export function limitWith<Args extends unknown[], Return>(
queue: RateLimiterQueue,
cost: number = 1,
) {
return (f: (...args: Args) => Return) => {
return async (...args: Args) => {
await queue.removeTokens(cost);
return f(...args);
};
};
}
I don't really want to publish an entire NPM package just for that, or have it as a separate dependency for that matter. (anyone remember left-pad?)
@dmurvihill Could you write a usage example? Probably, for RateLimiterRedis as it is the most popular limiter. I'll add it to the docs.
See the original comment for suggested usage.