67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
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<User[]> => {
|
|
return instance.get("users");
|
|
};
|
|
|
|
export const fetchUserById = (id: number): Promise<User> => {
|
|
return instance.get("users/" + id);
|
|
};
|
|
|
|
export const logIn = (
|
|
credentials: Credentials
|
|
): Promise<Token & Partial<{ message: string; user_id: number }>> => {
|
|
return instance.post("auth/login", credentials);
|
|
};
|
|
|
|
export const tokenStatus = (
|
|
token: string
|
|
): Promise<User & { message?: string }> => {
|
|
return instance.get("auth/status", {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
};
|
|
|
|
export const fetchZones = (origin: string | null): Promise<Flight[]> => {
|
|
return instance.get("flights" + (origin ? "?origin=" + origin : ""))
|
|
};
|
|
|
|
export const createFlight = (
|
|
flight_data: FlightCreate,
|
|
token: string
|
|
): Promise<Flight> => {
|
|
return instance.post("flights", flight_data, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
}; |