fids/browser-domain/src/Api.ts

137 lines
4.1 KiB
TypeScript

import { Axios, AxiosError } from "axios";
import { Credentials, Token, User, Flight, FlightCreate, SubscriptionsCreate, FlightEdit } 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.request.use((request) => {
request.data = JSON.stringify(request.data);
return request;
});
instance.interceptors.response.use(
(response) => {
if (response.headers["x-count"]) {
let json: any = {}
json["flights"] = JSON.parse(response.data);
json["count"] = response.headers["x-count"]
return json
} else if (response.status === 204) {
return 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 createAirline = (
credentials: Credentials,
token: string
): Promise<{ id?: string; message: string }> => {
return instance.post("users/airline", credentials, {
headers: { Authorization: `Bearer ${token}` },
});
};
export const fetchUserById = (id: number, token: string): Promise<User & { message?: string }> => {
return instance.get("users/" + id, {
headers: { Authorization: `Bearer ${token}` },
});
};
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}` },
});
};
interface FlightData {
flights: Flight[]
count: number
}
export const fetchFlights = (page: number | null, search: string | null): Promise<FlightData> => {
return instance.get("flights" + (page ? "?page=" + page : "") + (search ? (page ? "&" : "?") + "search=" + search : ""))
};
export const createFlight = (
flight_data: FlightCreate,
token: string
): Promise<Flight> => {
return instance.post("flights", flight_data, {
headers: { Authorization: `Bearer ${token}` },
});
};
export const editFlight = (
flight_id: string,
fligth_data: FlightEdit,
token: string
): Promise<Flight> => {
return instance.patch("flights/" + flight_id, fligth_data, {
headers: { Authorization: `Bearer ${token}` },
});
};
export const fetchFlight = (
flight_id: string,
): Promise<Flight> => {
return instance.get("flights/" + flight_id);
};
export const subscribeToFlight = (subscription: SubscriptionsCreate, token: string): Promise<SubscriptionsCreate> => {
return instance.post("subscriptions", subscription, {
headers: { Authorization: `Bearer ${token}` },
});
};
export const getChatId = (user_id: number, token: string): Promise<Flight> => {
return instance.get("notifications?user_id=" + user_id, {
headers: { Authorization: `Bearer ${token}` },
});
};
export const getSubscription = (subscription: SubscriptionsCreate, token: string): Promise<SubscriptionsCreate> => {
return instance.get("subscriptions?user_id=" + subscription.user_id + "&flight_id=" + subscription.flight_id, {
headers: { Authorization: `Bearer ${token}` },
});
};
export const unsubscribeFromFlight = (subscription: SubscriptionsCreate, token: string): Promise<any> => {
return instance.delete("subscriptions", {
headers: { Authorization: `Bearer ${token}` },
data: subscription
});
};
export const fetchSubscriptions = (user_id: number, token: string): Promise<SubscriptionsCreate[]> => {
return instance.get("subscriptions?user_id=" + user_id, {
headers: { Authorization: `Bearer ${token}` },
});
};