24 lines
1.1 KiB
TypeScript
24 lines
1.1 KiB
TypeScript
import { normalizeWarning } from "@/lib/weather-utils";
|
|
import { isImgwNoProductsResponse, readImgwResponseBody } from "@/lib/imgw-empty-response";
|
|
import type { RawWarning, WeatherWarning } from "@/types/imgw";
|
|
|
|
const IMGW_WARNINGS_METEO_URL = "https://danepubliczne.imgw.pl/api/data/warningsmeteo";
|
|
const USER_AGENT = "wtr./1.0 (weather notification worker)";
|
|
|
|
export async function fetchMeteoWarnings(signal?: AbortSignal): Promise<WeatherWarning[]> {
|
|
const response = await fetch(IMGW_WARNINGS_METEO_URL, {
|
|
headers: { accept: "application/json", "user-agent": USER_AGENT },
|
|
next: { revalidate: 300 },
|
|
signal,
|
|
});
|
|
if (!response.ok) {
|
|
const details = await readImgwResponseBody(response);
|
|
if (response.status === 404 && isImgwNoProductsResponse(details.json)) return [];
|
|
throw new Error(
|
|
`Unable to load IMGW meteorological warnings: ${response.status}${details.text ? ` ${details.text.slice(0, 240)}` : ""}`,
|
|
);
|
|
}
|
|
const rows = (await response.json()) as RawWarning[];
|
|
return Array.isArray(rows) ? rows.map((warning, index) => normalizeWarning(warning, "meteo", index)) : [];
|
|
}
|