mirror of
https://github.com/fosrl/docs-v2.git
synced 2026-09-30 17:59:10 +02:00
22 lines
933 B
TypeScript
22 lines
933 B
TypeScript
import 'server-only';
|
|
import { clientKey, createRateLimiter } from '@/lib/ai/rate-limit';
|
|
|
|
const checkFeedbackLimit = createRateLimiter(Number(process.env.FEEDBACK_RATE_LIMIT_PER_MINUTE ?? 60));
|
|
|
|
export function json(status: number, body: object) {
|
|
return Response.json(body, { status });
|
|
}
|
|
|
|
/** shared guard for the feedback endpoints: rate limit + JSON body */
|
|
export async function readFeedback(req: Request): Promise<Record<string, unknown> | Response> {
|
|
if (!checkFeedbackLimit(clientKey(req)).ok) return json(429, { error: 'Too many requests.' });
|
|
const body = (await req.json().catch(() => null)) as unknown;
|
|
if (!body || typeof body !== 'object') return json(400, { error: 'Invalid body.' });
|
|
return body as Record<string, unknown>;
|
|
}
|
|
|
|
export function storageError(scope: string, error: unknown) {
|
|
console.error(`[analytics] ${scope}`, error);
|
|
return json(503, { error: 'Feedback storage is unavailable.' });
|
|
}
|