← back to Govarbitrage
src/app/api/import/extension/route.ts
36 lines
import { NextRequest, NextResponse } from "next/server";
import { ingest, type RawListing } from "@/importers/ingest";
import { requireWrite } from "@/lib/auth";
export const dynamic = "force-dynamic";
// Receives a captured auction page from the browser extension (content.js shape)
// and ingests it (create/update + research). CORS-open so the extension can POST.
const CORS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, x-import-token",
};
export function OPTIONS() {
return new NextResponse(null, { headers: CORS });
}
export async function POST(req: NextRequest) {
const denied = await requireWrite(req);
if (denied) return new NextResponse(JSON.stringify({ error: "Unauthorized" }), { status: 401, headers: CORS });
try {
const body = (await req.json()) as RawListing;
if (!body?.title || !body?.sourceAuctionId) {
return NextResponse.json(
{ error: "Missing title or sourceAuctionId" },
{ status: 400, headers: CORS },
);
}
const result = await ingest({ ...body, source: body.source || "EXTENSION" });
return NextResponse.json(result, { headers: CORS });
} catch (e) {
return NextResponse.json({ error: (e as Error).message }, { status: 500, headers: CORS });
}
}