feat: redesign dashboard around place search
This commit is contained in:
46
app/api/locations/search/route.ts
Normal file
46
app/api/locations/search/route.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
const GEOCODING_URL = "https://geocoding-api.open-meteo.com/v1/search";
|
||||
|
||||
interface RawLocation {
|
||||
id?: number;
|
||||
name?: string;
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
admin1?: string;
|
||||
admin2?: string;
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const query = searchParams.get("query")?.trim() ?? "";
|
||||
const language = searchParams.get("language") === "en" ? "en" : "pl";
|
||||
if (query.length < 2 || query.length > 80) return NextResponse.json([]);
|
||||
|
||||
const params = new URLSearchParams({
|
||||
name: query,
|
||||
count: "8",
|
||||
language,
|
||||
format: "json",
|
||||
countryCode: "PL",
|
||||
});
|
||||
try {
|
||||
const response = await fetch(`${GEOCODING_URL}?${params}`, { next: { revalidate: 86400 } });
|
||||
if (!response.ok) return NextResponse.json({ error: "Location search is unavailable." }, { status: 502 });
|
||||
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 [];
|
||||
return [{
|
||||
id: location.id,
|
||||
name: location.name,
|
||||
latitude: location.latitude as number,
|
||||
longitude: location.longitude as number,
|
||||
province: location.admin1 ?? null,
|
||||
district: location.admin2 ?? null,
|
||||
}];
|
||||
});
|
||||
return NextResponse.json(results, { headers: { "Cache-Control": "public, s-maxage=86400, stale-while-revalidate=604800" } });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Location search is unavailable." }, { status: 502 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user