const makeRequest = ({ url, method, body = null }): Promise => { const req: RequestInit = { method: method, credentials: "include", headers: { "content-type": "application/json" }, mode: "cors", }; if (body) { req["body"] = JSON.stringify(body); } return fetch(url, req); }; const sendLoginRequest = async ( username: string, password: string ): Promise => { const p: Promise = new Promise(async (res) => { await makeRequest({ url: "http://localhost:5000/login", method: "POST", body: { username, password }, }) .then((resp) => resp.json()) .then((data) => { if (data.error) { res({ isError: true, ...data }); } res({ isError: false, ...data }); }); res({ isError: true }); }); return p; }; const sendLogoutRequest = async (): Promise => { const p: Promise = new Promise(async (res) => { await makeRequest({ url: "http://localhost:5000/logout", method: "POST", }) .then((resp) => resp.json()) .then((data) => { res({ isError: false, ...data }); }); res({ isError: true }); }); return p; }; export { makeRequest, sendLoginRequest, sendLogoutRequest };