51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
// type Pizza = {
|
|
// name: string;
|
|
// // TODO ingredience
|
|
// sizes: [
|
|
// size: number,
|
|
// price: number,
|
|
// ];
|
|
// }
|
|
|
|
import { getBaseUrl } from "./Utils";
|
|
|
|
async function request<TResponse>(
|
|
url: string,
|
|
config: RequestInit = {}
|
|
): Promise<TResponse> {
|
|
return fetch(getBaseUrl() + url, config).then(response => {
|
|
if (!response.ok) {
|
|
throw new Error(response.statusText);
|
|
}
|
|
return response.json() as TResponse;
|
|
});
|
|
}
|
|
|
|
const api = {
|
|
get: <TResponse>(url: string) => request<TResponse>(url),
|
|
post: <TBody extends BodyInit, TResponse>(url: string, body: TBody) => request<TResponse>(url, { method: 'POST', body, headers: { 'Content-Type': 'application/json' } }),
|
|
}
|
|
|
|
export const getData = async () => {
|
|
return await api.get<any>('/api/data');
|
|
}
|
|
|
|
export const getFood = async () => {
|
|
return await api.get<any>('/api/food');
|
|
}
|
|
|
|
export const getPizzy = async () => {
|
|
return await api.get<any>('/api/pizza');
|
|
}
|
|
|
|
export const createPizzaDay = async () => {
|
|
return await api.post<any, any>('/api/createPizzaDay', {});
|
|
}
|
|
|
|
export const deletePizzaDay = async () => {
|
|
return await api.post<any, any>('/api/deletePizzaDay', {});
|
|
}
|
|
|
|
export const updateChoice = async (name: string, choice: number | null) => {
|
|
return await api.post<any, any>('/api/updateChoice', JSON.stringify({ name, choice }));
|
|
} |