SPB Git
747 B · 9 lines typescript
1import { rateLimit } from "@/lib/auth/ratelimit";2export function ip(req: Request) { return (req.headers.get("x-forwarded-for")?.split(",")[0] ?? req.headers.get("x-real-ip") ?? "0.0.0.0").trim(); }3export function limited(req: Request, bucket: string, max = 120, windowMs = 60_000) { return !rateLimit(`${bucket}:${ip(req)}`, max, windowMs); }4export function json(data: unknown, init: ResponseInit & { maxAge?: number } = {}) {5  const { maxAge = 60, ...rest } = init;6  return Response.json(data, { ...rest, headers: { "Cache-Control": `public, max-age=${maxAge}, s-maxage=${maxAge}`, ...(rest.headers ?? {}) } });7}8export const tooMany = () => Response.json({ error: "Too many requests" }, { status: 429, headers: { "Retry-After": "60" } });9
Files · ba542514eac709a3e76ac3b3f88404c61356d594
Menu

Theme