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 REVERSE_GEOCODING_URL = "https://nominatim.openstreetmap.org/reverse";
|
||||
const USER_AGENT = "wtr./1.0 (https://git.zvcloud.net/zv/wtr)";
|
||||
@@ -13,6 +14,8 @@ interface RawReverseLocation {
|
||||
municipality?: string;
|
||||
county?: string;
|
||||
state?: string;
|
||||
country?: string;
|
||||
country_code?: string;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -54,10 +57,17 @@ export async function GET(request: Request) {
|
||||
?? data.name
|
||||
?? data.display_name?.split(",")[0]?.trim();
|
||||
if (!name) return NextResponse.json({ error: "Place name is unavailable." }, { status: 404 });
|
||||
const countryCode = data.address?.country_code?.toUpperCase() ?? null;
|
||||
return NextResponse.json({
|
||||
name,
|
||||
province: data.address?.state ?? null,
|
||||
district: data.address?.county ?? null,
|
||||
country: data.address?.country ?? null,
|
||||
countryCode,
|
||||
admin1: data.address?.state ?? null,
|
||||
admin2: data.address?.county ?? null,
|
||||
timezone: null,
|
||||
region: getWeatherRegionForCountryCode(countryCode),
|
||||
}, {
|
||||
headers: { "Cache-Control": "public, s-maxage=86400, stale-while-revalidate=604800" },
|
||||
});
|
||||
|
||||
@@ -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