import { NextResponse } from "next/server"; const IMGW_HYBRID_URL = "https://meteo.imgw.pl/api/v1/forecast/fcapi"; // This browser token is published by the official meteo.imgw.pl frontend. const IMGW_HYBRID_TOKEN = "p4DXKjsYadfBV21TYrDk"; function parseCoordinate(value: string | null, min: number, max: number) { if (!value?.trim()) return null; const coordinate = Number(value); return Number.isFinite(coordinate) && coordinate >= min && coordinate <= max ? coordinate : null; } export async function GET(request: Request) { const { searchParams } = new URL(request.url); const latitude = parseCoordinate(searchParams.get("latitude"), -90, 90); const longitude = parseCoordinate(searchParams.get("longitude"), -180, 180); if (latitude === null || longitude === null) { return NextResponse.json({ error: "Invalid coordinates." }, { status: 400 }); } const params = new URLSearchParams({ token: IMGW_HYBRID_TOKEN, lat: String(latitude), lon: String(longitude), m: "hybrid", }); try { const response = await fetch(`${IMGW_HYBRID_URL}?${params}`, { next: { revalidate: 120 } }); if (!response.ok) return NextResponse.json({ error: "IMGW Hybrid service is unavailable." }, { status: 502 }); return NextResponse.json(await response.json(), { headers: { "Cache-Control": "public, s-maxage=120, stale-while-revalidate=300" }, }); } catch { return NextResponse.json({ error: "IMGW Hybrid service is unavailable." }, { status: 502 }); } }