Repository Public
1// Vrai-Prix — méthode du coût : calcul d'une estimation (déterministe, sauvegardée avec son instantané de prix).2import { NextRequest, NextResponse } from "next/server";3import { runEstimate, sanitizeInput } from "@/lib/cost/estimate";4import { clientKey, rateLimit } from "@/lib/cost/cache";56export const runtime = "nodejs";7export const maxDuration = 30;89export async function POST(req: NextRequest) {10 if (!rateLimit(`estimate:${clientKey(req)}`, 60, 60_000)) return NextResponse.json({ error: "Trop de requêtes — réessayez dans une minute." }, { status: 429 });11 let body: unknown;12 try { body = await req.json(); } catch { return NextResponse.json({ error: "JSON invalide" }, { status: 400 }); }13 try {14 const input = sanitizeInput((body as { input?: unknown })?.input ?? body);15 const save = (body as { save?: boolean })?.save !== false;16 const est = runEstimate(input, { save, sessionId: req.headers.get("x-session-id") });17 return NextResponse.json(est);18 } catch (e) {19 return NextResponse.json({ error: (e as Error).message }, { status: 500 });20 }21}22