#25 Moved sendLoginRequest and sendLogoutRequest to utils
This commit was merged in pull request #26.
This commit is contained in:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user