File size: 2,769 Bytes
bc18056
2e50ccd
 
 
 
 
 
 
 
 
 
 
 
bc18056
 
 
 
2e50ccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6f7eeb
 
 
 
 
 
 
 
 
 
 
 
 
2e50ccd
 
 
 
 
 
 
 
 
 
 
02df639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import type { AutocompleteResult, BookingRequest, BookingResponse, CalendarResponse, CheapFlightsResponse, ConfigResponse, CurrencyRatesResponse, GeolocationResponse, PopularDestinationsResponse, SearchRequest, SearchResponse } from './types';

const BASE_URL = '/api';

async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> {
  const res = await fetch(url, init);
  if (!res.ok) {
    const text = await res.text();
    throw new Error(`API error ${res.status}: ${text}`);
  }
  return res.json();
}

export async function getConfig(): Promise<ConfigResponse> {
  return fetchJson<ConfigResponse>(`${BASE_URL}/config`);
}

export async function searchAirports(query: string): Promise<AutocompleteResult[]> {
  if (!query || query.length < 1) return [];
  return fetchJson<AutocompleteResult[]>(
    `${BASE_URL}/airports/autocomplete?q=${encodeURIComponent(query)}`
  );
}

export async function searchFlights(req: SearchRequest): Promise<SearchResponse> {
  return fetchJson<SearchResponse>(`${BASE_URL}/search`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(req),
  });
}

export async function bookFlight(req: BookingRequest): Promise<BookingResponse> {
  const res = await fetch(`${BASE_URL}/booking`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(req),
  });
  if (!res.ok) {
    const data = await res.json().catch(() => null);
    throw new Error(data?.detail || `Booking failed (${res.status})`);
  }
  return res.json();
}

export async function getCalendar(
  origin: string,
  destination: string,
  year: number,
  month: number,
  cabinClass: string = 'economy'
): Promise<CalendarResponse> {
  return fetchJson<CalendarResponse>(
    `${BASE_URL}/calendar?origin=${origin}&destination=${destination}&year=${year}&month=${month}&cabin_class=${cabinClass}`
  );
}

export async function getGeolocation(): Promise<GeolocationResponse> {
  return fetchJson<GeolocationResponse>(`${BASE_URL}/geolocation`);
}

export async function getCurrencyRates(): Promise<CurrencyRatesResponse> {
  return fetchJson<CurrencyRatesResponse>(`${BASE_URL}/currency/rates`);
}

export async function getPopularDestinations(origin: string, limit: number = 8): Promise<PopularDestinationsResponse> {
  return fetchJson<PopularDestinationsResponse>(
    `${BASE_URL}/destinations/popular?origin=${encodeURIComponent(origin)}&limit=${limit}`
  );
}

export async function getCheapFlights(origin: string, category: string = 'popular', limit: number = 8): Promise<CheapFlightsResponse> {
  return fetchJson<CheapFlightsResponse>(
    `${BASE_URL}/destinations/cheap-flights?origin=${encodeURIComponent(origin)}&category=${category}&limit=${limit}`
  );
}