import { checkOrigin } from "@/lib/origin-check"; import { checkRateLimit } from "@/lib/rate-limit"; type RateLimitOpts = { key: string; limit: number; windowMs: number; }; type GuardOpts = { origin?: boolean; rateLimit?: RateLimitOpts; }; type Handler = (request: Request, ctx: TCtx) => Promise; export function withGuards( opts: GuardOpts, handler: Handler ): Handler { return async (request, ctx) => { if (opts.origin !== false) { const blocked = checkOrigin(request); if (blocked) return blocked; } if (opts.rateLimit) { const blocked = checkRateLimit(request, opts.rateLimit); if (blocked) return blocked; } return handler(request, ctx); }; }