-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetFlight.ts
More file actions
29 lines (23 loc) · 1.02 KB
/
getFlight.ts
File metadata and controls
29 lines (23 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import onAirRequest, { FlightApiResponse } from './onAirRequest';
import { Flight, GetFlight, } from '../types';
import { isValidGuid } from '../utils';
const endPoint = 'flights/';
export const getFlight:GetFlight = async (flightId: string, apiKey: string):Promise<Flight> => {
if (!flightId) throw new Error('No Flight Id provided');
if (!apiKey) throw new Error('No Api Key provided');
if (!isValidGuid(flightId)) throw new Error('Invalid Flight Id provided');
if (!isValidGuid(apiKey)) throw new Error('Invalid Api Key provided');
try {
const response = await onAirRequest<FlightApiResponse>(
`https://server1.onair.company/api/v1/${endPoint}${flightId}`,
apiKey,
);
if (typeof response.data.Content !== 'undefined') {
return response.data.Content as Flight;
} else {
throw new Error(response.data.Error ? response.data.Error : `Flight ID ${flightId} not found`);
}
} catch (e) {
throw new Error(e.message);
}
};