52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
const makeRequest = ({ url, method, body = null }): Promise<Response> => {
|
|
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<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 };
|