2024-11-17 16:26:52 -05:00

47 lines
1.4 KiB
TypeScript

import { NextResponse } from 'next/server';
interface VatsimFlight {
callsign: string;
departure: string;
arrival: string;
route: string;
planned_deptime: string;
planned_arrtime: string;
flightplan: object;
}
export async function GET(
request: Request,
{ params }: { params: { icao: string } }
) {
try {
const response = await fetch('https://data.vatsim.net/v3/vatsim-data.json', {
next: { revalidate: 60 } // Cache for 1 minute
});
if (!response.ok) {
throw new Error('Failed to fetch VATSIM data');
}
const data = await response.json();
const icao = params.icao.toUpperCase();
// Filter out pilots without a valid flight plan
const flights = data.pilots
.filter((pilot) =>
pilot.flight_plan && (pilot.flight_plan.departure === icao || pilot.flight_plan.arrival === icao)
)
.map((pilot) => ({
callsign: pilot.callsign,
type: pilot.flight_plan.departure === icao ? 'Departure' : 'Arrival',
time: pilot.flight_plan.departure === icao ? pilot.flight_plan.deptime : pilot.flight_plan.enroute_time,
route: pilot.flight_plan.route,
origin: pilot.flight_plan.departure,
destination: pilot.flight_plan.arrival
}));
return NextResponse.json(flights);
} catch (error) {
console.log(error);
return NextResponse.json({ error: 'Failed to fetch VATSIM data' }, { status: 500 });
}
}