Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: decorators #284

Open
dmurvihill opened this issue Oct 27, 2024 · 4 comments
Open

Feature Request: decorators #284

dmurvihill opened this issue Oct 27, 2024 · 4 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@dmurvihill
Copy link

dmurvihill commented Oct 27, 2024

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?

@animir
Copy link
Owner

animir commented Oct 28, 2024

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

@animir animir added enhancement New feature or request question Further information is requested labels Oct 28, 2024
@dmurvihill
Copy link
Author

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?)

@animir
Copy link
Owner

animir commented Nov 3, 2024

@dmurvihill Could you write a usage example? Probably, for RateLimiterRedis as it is the most popular limiter. I'll add it to the docs.

@dmurvihill
Copy link
Author

See the original comment for suggested usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants