@borrowdev/limiter
v2.1.0
Published
Self-hostable rate limiting API for serverless environments
Maintainers
Readme
Borrow Limiter
Features
- Self-hostable - Easily deploy on your own infrastructure
- Minimal dependencies - Lightweight and secure
- Fully typed - Complete TypeScript + JSDoc support
- Simple API - Intuitive interface for defining rate limits
- Serverless-first - Integration with modern cloud environments
Installation
# npm
npm install @borrowdev/limiter
# pnpm
pnpm add @borrowdev/limiter
# yarn
yarn add @borrowdev/limiter
# bun
bun add @borrowdev/limiterAuthentication
Usage
Let's use the fixed window algorithm to rate limit our login endpoint to 10 requests per minute.
import { limiter } from "@borrowdev/limiter";
const { success, timeLeft } = await limiter(
{
key: "my-limiter-id",
userId: "current-user-id",
},
{
limiters: [
{
maxRequests: 10,
interval: "minute",
type: "fixed",
},
],
},
);
if (!success) {
return {
message:
"Rate limit exceeded." +
(timeLeft !== null ? ` You can try again in ${timeLeft} seconds.` : ""),
};
}