Feathers hook for requests rate-limiting. Uses fast-ratelimit.
yarn add feathers-hooks-ratelimit
or
npm i feathers-hooks-ratelimit --save
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)
]
});
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.
See config.namespace
.
See fast-ratelimit docs.
See fast-ratelimit docs.
Used as message
for @feathersjs/errors TooManyRequests
error.
Used as data
for @feathersjs/errors TooManyRequests
error.