Need private packages and team management tools?Check out npm Teams »

feathers-hooks-ratelimit

1.0.0 • Public • Published

feathers-hooks-ratelimit

Feathers hook for requests rate-limiting. Uses fast-ratelimit.

Install

yarn add feathers-hooks-ratelimit

or

npm i feathers-hooks-ratelimit --save

Usage

const rateLimit = require('feathers-hooks-ratelimit');
 
const config = {
  threshold: 20,           // available tokens over timespan
  ttl: 60                  // time-to-live value of token bucket (in seconds)
  userIdKey: 'user_id',    // used for request namespacing
  errorMessage: 'Too many requests. Please, try later.',
};
 
// And then just use it in your service's hooks.
myService.before({
  all: [
      rateLimit(config)
  ]
});

Options

config.namespace

fast-ratelimit uses namespaces to distinguish requests from different consumers. So you need to specify this value.

Or you can specify config.userIdKey if you want to use unique user id as a namespace.

config.userIdKey

See config.namespace.

config.threshold

See fast-ratelimit docs.

config.ttl

See fast-ratelimit docs.

config.errorMessage (optional)

Used as message for @feathersjs/errors TooManyRequests error.

config.errorData (optional)

Used as data for @feathersjs/errors TooManyRequests error.

Install

npm i feathers-hooks-ratelimit

DownloadsWeekly Downloads

3

Version

1.0.0

License

MIT

Last publish

Collaborators

  • avatar