Extract ui api requests to common functions

This commit is contained in:
2023-09-17 09:35:03 -04:00
parent 4a3225c3f9
commit 5443ed6d77
6 changed files with 43 additions and 21 deletions

View File

@@ -1,12 +1,12 @@
import axios from 'axios';
import { Bounds, GetAirportResponse, GetAirportsResponse } from './airport.types';
import { getRequest } from '.';
interface GetAirportProps {
icao: string;
}
export async function getAirport({ icao }: GetAirportProps): Promise<GetAirportResponse> {
const response = await axios.get(`http://localhost:5000/airports/${icao}`).catch((error) => console.error(error));
const response = await getRequest(`airports/${icao}`, {});
return response?.data || { data: undefined };
}
@@ -25,18 +25,14 @@ export async function getAirports({
limit = 10,
page = 1
}: GetAirportsProps): Promise<GetAirportsResponse> {
const response = await axios
.get(`http://localhost:5000/airports`, {
params: {
bounds: bounds
? `${bounds?.northEast.lat},${bounds?.northEast.lon},${bounds?.southWest.lat},${bounds?.southWest.lon}`
: undefined,
category: category ?? undefined,
filter: filter ?? undefined,
limit,
page
}
})
.catch((error) => console.error(error));
const response = await getRequest('airports', {
bounds: bounds
? `${bounds?.northEast.lat},${bounds?.northEast.lon},${bounds?.southWest.lat},${bounds?.southWest.lon}`
: undefined,
category: category ?? undefined,
filter: filter ?? undefined,
limit,
page
});
return response?.data || { data: [] };
}

View File

@@ -0,0 +1,18 @@
import axios, { AxiosResponse } from 'axios';
const serviceHost = process.env.SERVICE_HOST || 'http://localhost';
const servicePort = process.env.SERVICE_PORT || 5000;
export async function getRequest(endpoint: string, params: any): Promise<AxiosResponse<any, any> | undefined> {
const response = await axios
.get(`${serviceHost}:${servicePort}/${endpoint}`, { params })
.catch((error) => console.error(error));
return response || undefined;
}
export async function postRequest(endpoint: string, body: any): Promise<AxiosResponse<any, any> | undefined> {
const response = await axios
.post(`${serviceHost}:${servicePort}/${endpoint}`, { body })
.catch((error) => console.error(error));
return response || undefined;
}

View File

@@ -1,6 +1,6 @@
import axios from 'axios';
import { Airport } from './airport.types';
import { Metar } from './metar.types';
import { getRequest } from '.';
interface GetMetarsResponse {
data: Metar[];
@@ -11,7 +11,6 @@ export async function getMetars(airports: Airport[]): Promise<GetMetarsResponse>
return { data: [] };
}
const stationICAOs: string = airports.map((airport) => airport.icao).join(',');
const url = `http://localhost:5000/metars/${stationICAOs}`;
const response = await axios.get(url).catch((error) => console.error(error));
const response = await getRequest(`metars/${stationICAOs}`, {});
return response?.data || { data: [] };
}