feat: introduce API rate limiting for API routes and cap package query limits.

This commit is contained in:
Yusuf İpek
2025-11-20 15:55:43 +03:00
parent 48e2d2f05d
commit af1ae5f1f8
7 changed files with 130 additions and 17 deletions
+36
View File
@@ -0,0 +1,36 @@
import { LRUCache } from 'lru-cache'
type Options = {
uniqueTokenPerInterval?: number
interval?: number
}
export default function rateLimit(options?: Options) {
const tokenCache = new LRUCache({
max: options?.uniqueTokenPerInterval || 500,
ttl: options?.interval || 60000,
})
return {
check: (res: Response | null, limit: number, token: string) =>
new Promise<void>((resolve, reject) => {
const tokenCount = (tokenCache.get(token) as number[]) || [0]
if (tokenCount[0] === 0) {
tokenCache.set(token, tokenCount)
}
tokenCount[0] += 1
const currentUsage = tokenCount[0]
const isRateLimited = currentUsage >= limit
// If we had a response object we could set headers, but for Next.js middleware
// we just want to know if we should block.
if (isRateLimited) {
reject()
} else {
resolve()
}
}),
}
}