import { Axios, AxiosError } from "axios"; import { Credentials, Token, User, Flight, FlightCreate } from "./Types"; const instance = new Axios({ baseURL: "http://127.0.0.1:5002/", headers: { accept: "application/json", "Content-Type": "application/json", }, validateStatus: (x) => { return !(x < 200 || x > 204) } }); instance.interceptors.request.use((request) => { request.data = JSON.stringify(request.data); return request; }); instance.interceptors.response.use( (response) => { return JSON.parse(response.data); }, (error) => { const err = error as AxiosError; return Promise.reject(err); } ); export const createUser = ( credentials: Credentials ): Promise<{ id?: string; message: string }> => { return instance.post("users", credentials); }; export const fetchUsers = (): Promise => { return instance.get("users"); }; export const fetchUserById = (id: number): Promise => { return instance.get("users/" + id); }; export const logIn = ( credentials: Credentials ): Promise> => { return instance.post("auth/login", credentials); }; export const tokenStatus = ( token: string ): Promise => { return instance.get("auth/status", { headers: { Authorization: `Bearer ${token}` }, }); }; export const fetchZones = (origin: string | null): Promise => { return instance.get("flights" + (origin ? "?origin=" + origin : "")) }; export const createFlight = ( flight_data: FlightCreate, token: string ): Promise => { return instance.post("flights", flight_data, { headers: { Authorization: `Bearer ${token}` }, }); };