feat: add global weather support
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getWeatherRegionForCountryCode } from "@/types/weather-region";
|
||||
|
||||
const GEOCODING_URL = "https://geocoding-api.open-meteo.com/v1/search";
|
||||
|
||||
@@ -7,8 +8,11 @@ interface RawLocation {
|
||||
name?: string;
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
country?: string;
|
||||
country_code?: string;
|
||||
admin1?: string;
|
||||
admin2?: string;
|
||||
timezone?: string;
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
@@ -19,10 +23,9 @@ export async function GET(request: Request) {
|
||||
|
||||
const params = new URLSearchParams({
|
||||
name: query,
|
||||
count: "8",
|
||||
count: "10",
|
||||
language,
|
||||
format: "json",
|
||||
countryCode: "PL",
|
||||
});
|
||||
try {
|
||||
const response = await fetch(`${GEOCODING_URL}?${params}`, { next: { revalidate: 86400 } });
|
||||
@@ -30,6 +33,7 @@ export async function GET(request: Request) {
|
||||
const data = await response.json() as { results?: RawLocation[] };
|
||||
const results = (data.results ?? []).flatMap((location) => {
|
||||
if (!location.id || !location.name || !Number.isFinite(location.latitude) || !Number.isFinite(location.longitude)) return [];
|
||||
const countryCode = location.country_code?.toUpperCase() ?? null;
|
||||
return [{
|
||||
id: location.id,
|
||||
name: location.name,
|
||||
@@ -37,6 +41,12 @@ export async function GET(request: Request) {
|
||||
longitude: location.longitude as number,
|
||||
province: location.admin1 ?? null,
|
||||
district: location.admin2 ?? null,
|
||||
country: location.country ?? null,
|
||||
countryCode,
|
||||
admin1: location.admin1 ?? null,
|
||||
admin2: location.admin2 ?? null,
|
||||
timezone: location.timezone ?? null,
|
||||
region: getWeatherRegionForCountryCode(countryCode),
|
||||
}];
|
||||
});
|
||||
return NextResponse.json(results, { headers: { "Cache-Control": "public, s-maxage=86400, stale-while-revalidate=604800" } });
|
||||
|
||||
Reference in New Issue
Block a user