#25 Moved sendLoginRequest and sendLogoutRequest to utils

This commit was merged in pull request #26.
This commit is contained in:
2023-03-21 12:22:38 -04:00
parent b1f6d6b22c
commit 214b1c8657
4 changed files with 66 additions and 35 deletions

View File

@@ -1,4 +1,4 @@
const makeRequest = ({ url, method, body }): Promise<Response> => {
const makeRequest = ({ url, method, body = {} }): Promise<Response> => {
return fetch(url, {
method: method,
credentials: "include",
@@ -8,4 +8,41 @@ const makeRequest = ({ url, method, body }): Promise<Response> => {
});
};
export { makeRequest };
const sendLoginRequest = async (
username: string,
password: string
): Promise<object> => {
const p: Promise<object> = 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<object> => {
const p: Promise<object> = 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 };