import { NextResponse } from "next/server"; import { corsHeaders, forbidden, isAllowedOrigin } from "@/lib/api-cors"; import { getGameProfile } from "@/lib/game-profile"; export async function OPTIONS(request: Request) { if (!isAllowedOrigin(request)) { return forbidden(); } return new NextResponse(null, { status: 204, headers: { ...corsHeaders(), "Access-Control-Allow-Methods": "GET, OPTIONS", "Access-Control-Max-Age": "86400", }, }); } export async function GET(request: Request, context: { params: Promise<{ id: string }> }) { if (!isAllowedOrigin(request)) { return forbidden(); } const { id } = await context.params; if (!/^\d+$/.test(id)) { return NextResponse.json({ error: "Invalid application id" }, { status: 400 }); } const { searchParams } = new URL(request.url); const country = /^[A-Z]{2}$/.test(searchParams.get("country") ?? "") ? (searchParams.get("country") as string) : "TR"; const lang = ["tr", "en", "de"].includes(searchParams.get("lang") ?? "") ? (searchParams.get("lang") as string) : "en"; const profile = await getGameProfile(id, country, lang); if (!profile) { return NextResponse.json({ error: "Game not found" }, { status: 404 }); } return NextResponse.json(profile, { headers: { "Cache-Control": "public, s-maxage=3600, stale-while-revalidate=86400", ...corsHeaders(), }, }); }