import { PizzaOrder } from "./types"; import { getBaseUrl, getToken } from "./Utils"; async function request( url: string, config: RequestInit = {} ): Promise { config.headers = config?.headers ? new Headers(config.headers) : new Headers(); config.headers.set("Authorization", `Bearer ${getToken()}`); return fetch(getBaseUrl() + url, config).then(response => { if (!response.ok) { throw new Error(response.statusText); } return response.json() as TResponse; }); } const api = { get: (url: string) => request(url), post: (url: string, body: TBody) => request(url, { method: 'POST', body, headers: { 'Content-Type': 'application/json' } }), } export const getQrUrl = (login: string) => { return `${getBaseUrl()}/api/qr?login=${login}`; } export const getData = async () => { return await api.get('/api/data'); } export const getFood = async () => { return await api.get('/api/food'); } export const getPizzy = async () => { return await api.get('/api/pizza'); } export const createPizzaDay = async () => { return await api.post('/api/createPizzaDay', undefined); } export const deletePizzaDay = async () => { return await api.post('/api/deletePizzaDay', undefined); } export const lockPizzaDay = async () => { return await api.post('/api/lockPizzaDay', undefined); } export const unlockPizzaDay = async () => { return await api.post('/api/unlockPizzaDay', undefined); } export const finishOrder = async () => { return await api.post('/api/finishOrder', undefined); } export const finishDelivery = async (bankAccount?: string, bankAccountHolder?: string) => { return await api.post('/api/finishDelivery', JSON.stringify({ bankAccount, bankAccountHolder })); } export const addChoice = async (locationIndex: number, foodIndex?: number) => { return await api.post('/api/addChoice', JSON.stringify({ locationIndex, foodIndex })); } export const removeChoices = async (locationIndex: number) => { return await api.post('/api/removeChoices', JSON.stringify({ locationIndex })); } export const removeChoice = async (locationIndex: number, foodIndex: number) => { return await api.post('/api/removeChoice', JSON.stringify({ locationIndex, foodIndex })); } export const addPizza = async (pizzaIndex: number, pizzaSizeIndex: number) => { return await api.post('/api/addPizza', JSON.stringify({ pizzaIndex, pizzaSizeIndex })); } export const removePizza = async (pizzaOrder: PizzaOrder) => { return await api.post('/api/removePizza', JSON.stringify({ pizzaOrder })); } export const updateNote = async (note?: string) => { return await api.post('/api/updateNote', JSON.stringify({ note })); } export const login = async (login: string) => { return await api.post('/api/login', JSON.stringify({ login })); }