import { NextRequest } from 'next/server'; import { resolveLocateRequest, getLocateRequest } from '@/lib/db'; import { corsJson, corsPreflight } from '../../cors'; /** The extension reports back where the element landed, or why it couldn't. */ export async function POST(req: NextRequest) { try { const body = await req.json() as { id?: unknown; result?: unknown; error?: unknown }; const id = Number(body.id); if (!Number.isInteger(id) || id <= 0) { return corsJson({ error: '`id` is required' }, { status: 400 }); } if (!getLocateRequest(id)) { return corsJson({ error: 'No such request' }, { status: 404 }); } const error = typeof body.error === 'string' && body.error ? body.error : null; resolveLocateRequest(id, error ? null : (body.result ?? null), error); return corsJson({ ok: true }); } catch (err: any) { return corsJson({ error: err?.message ?? 'Failed to record result' }, { status: 500 }); } } export async function OPTIONS() { return corsPreflight(); }