1.0.5 • Published 3 months ago
@specter-labs/fastify-rate-limit v1.0.5
Fastify Rate Limit
A simple HTTP rate limiting plugin for Fastify.
Installation
npm install @specter-labs/fastify-rate-limit
Usage
import fastify from 'fastify'
import fastifyRateLimit from '@specter-labs/fastify-rate-limit'
// Implement your own storage adapter
const redisStorageAdapter = {
async push({ path, data, expiresIn }: { path: string, data: unknown, expiresIn?: number })
async remove({ pattern }) { /* implementation */ }
}
const app = fastify()
// Register the plugin
app.register(fastifyRateLimit, {
storageAdapter: redisStorageAdapter,
ttl: 300, // 5 minutes default TTL
prefix: 'my-app:rate-limit' // custom prefix
})
Storage
In memory
This plugin provides a in-memory storage adapter instance that can be used out-of-the-box.
import fastify from 'fastify'
import fastifyRateLimitm { defaultStorageAdapter } from '@specter-labs/fastify-rate-limit'
const app = fastify();
app.register(fastifyRateLimit, {
storageAdapter: defaultStorageAdapter,
exception: Error
})
API
Plugin Options
storageAdapter
: Required. An object implementing the IStorageAdapter interfaceexception
: Required. A custom exception error class to throw when the rate limit is exceeded.keyGenerator
: Optional. Function to generate cache keys from requestsprefix
: Optional. Prefix for cache keysmaxRequests
: Optional. Maximum number of requests allowed per minute.maxBurst
: Optional. Maximum burst of requests allowed per second.ignorePaths
: Optional. List of paths to ignore for rate limiting.
License
ISC