Skip to content

Instantly share code, notes, and snippets.

@Celeo
Created August 1, 2022 22:47
Show Gist options
  • Save Celeo/8183c0e03d21837bf859762114cd0e29 to your computer and use it in GitHub Desktop.
Save Celeo/8183c0e03d21837bf859762114cd0e29 to your computer and use it in GitHub Desktop.
const STATUS_ENDPOINT = "https://status.vatsim.net/status.json";
export interface FlightPlan {
flight_rules: string;
aircraft: string;
aircraft_faa: string;
aircraft_short: string;
departure: string;
arrival: string;
alternate: string;
cruise_tas: string;
altitude: string;
deptime: string;
enroute_time: string;
fuel_time: string;
remarks: string;
route: string;
revision_id: number;
assigned_transponder: string;
}
export interface Pilot {
cid: number;
name: string;
callsign: string;
server: string;
pilot_rating: number;
latitude: number;
longitude: number;
altitude: number;
groundspeed: number;
transponder: string;
heading: number;
qnh_i_hg: number;
qnh_mb: number;
flight_plan: FlightPlan | null;
logon_time: string;
last_updated: string;
}
export interface Controller {
cid: number;
name: string;
callsign: string;
frequency: string;
facility: number;
rating: number;
server: string;
visual_range: number;
text_atis: Array<string> | null;
last_updated: string;
logon_time: string;
}
export interface V3ResponseData {
general: {
version: number;
reload: number;
update: string;
update_timestamp: string;
connected_clients: number;
unique_users: number;
};
pilots: Array<Pilot>;
controllers: Array<Controller>;
atis: Array<Record<string, unknown>>;
servers: Array<Record<string, unknown>>;
facilities: Array<{ id: number; short: string; long: string }>;
ratings: Array<{ id: number; short: string; long: string }>;
pilot_ratings: Array<{ id: number; short: string; long: string }>;
}
export async function getV3Endpoint(): Promise<string> {
const response = await fetch(STATUS_ENDPOINT);
if (response.status !== 200) {
throw new Error(`Got status ${response.status} from status endpoint`);
}
const data: { data: { v3: Array<string> } } = await response.json();
const endpoints = data.data.v3;
return endpoints[Math.floor(Math.random() * endpoints.length)];
}
export async function queryV3(endpoint: string): Promise<V3ResponseData> {
const response = await fetch(endpoint);
if (response.status !== 200) {
throw new Error(
`Got status ${response.status} from V3 endpoint on ${endpoint}`
);
}
return response.json();
}
if (import.meta.main) {
const v3Endpoint = await getV3Endpoint();
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment