fids/screen-domain/src/Api.ts

37 lines
1010 B
TypeScript

import { Axios, AxiosError } from "axios";
import { Credentials, User, Flight } from "./Types";
const instance = new Axios({
baseURL: process.env.REACT_APP_ENDPOINT ? process.env.REACT_APP_ENDPOINT : "http://127.0.0.1:5000/",
headers: {
accept: "application/json",
"Content-Type": "application/json",
},
validateStatus: (x) => { return !(x < 200 || x > 204) }
});
instance.interceptors.response.use(
(response) => {
return JSON.parse(response.data);
},
(error) => {
const err = error as AxiosError;
return Promise.reject(err);
}
);
instance.interceptors.request.use((request) => {
request.data = JSON.stringify(request.data);
return request;
});
export const ping = () => {
return instance.get("health");
};
export const fetchZones = (origin: string | undefined, lastUpdate: string | null): Promise<Flight[]> => {
return instance.get("flights" +
(origin ? "?origin=" + origin : "") +
(lastUpdate ? ( origin ? "&lastUpdated=" : "?lastUpdated=") + lastUpdate : ""))
};