implement api fetch with async await and outsource general code into seperate function

This commit is contained in:
lukas 2021-06-24 17:58:42 +02:00
parent c93d02ca14
commit 64897d2abe

View File

@ -26,37 +26,7 @@ export function callAPI<T>(
errorcallback: (_: string) => void = (_: string): void => {} errorcallback: (_: string) => void = (_: string): void => {}
): void { ): void {
token.checkAPITokenValid((mytoken) => { token.checkAPITokenValid((mytoken) => {
fetch(APIPREFIX + apinode, { generalAPICall<T>(apinode, fd, callback, errorcallback, false, true, mytoken);
method: 'POST',
body: JSON.stringify(fd),
headers: new Headers({
'Content-Type': 'application/json',
Authorization: 'Bearer ' + mytoken
})
})
.then((response) => {
if (response.status === 200) {
// success
response
.json()
.then((result: T) => {
callback(result);
})
.catch((reason) => errorcallback(reason));
} else if (response.status === 400) {
// Bad Request --> invalid token
console.log('loading Password page.');
// load password page
if (GlobalInfos.loadPasswordPage) {
GlobalInfos.loadPasswordPage(() => {
callAPI(apinode, fd, callback, errorcallback);
});
}
} else {
console.log('Error: ' + response.statusText);
}
})
.catch((reason) => errorcallback(reason));
}); });
} }
@ -73,18 +43,7 @@ export function callApiUnsafe<T>(
callback: (_: T) => void, callback: (_: T) => void,
errorcallback?: (_: string) => void errorcallback?: (_: string) => void
): void { ): void {
fetch(APIPREFIX + apinode, {method: 'POST', body: JSON.stringify(fd)}) generalAPICall(apinode, fd, callback, errorcallback, true, true, '');
.then((response) => {
if (response.status !== 200) {
console.log('Error: ' + response.statusText);
// todo place error popup here
} else {
response.json().then((result: T) => {
callback(result);
});
}
})
.catch((reason) => (errorcallback ? errorcallback(reason) : {}));
} }
/** /**
@ -95,19 +54,54 @@ export function callApiUnsafe<T>(
*/ */
export function callAPIPlain(apinode: APINode, fd: ApiBaseRequest, callback: (_: string) => void): void { export function callAPIPlain(apinode: APINode, fd: ApiBaseRequest, callback: (_: string) => void): void {
token.checkAPITokenValid((mytoken) => { token.checkAPITokenValid((mytoken) => {
fetch(APIPREFIX + apinode, { generalAPICall(apinode, fd, callback, () => {}, false, false, mytoken);
});
}
function generalAPICall<T>(
apinode: APINode,
fd: ApiBaseRequest,
callback: (_: T) => void,
errorcallback: (_: string) => void = (_: string): void => {},
unsafe: boolean,
json: boolean,
mytoken: string
): void {
(async function (): Promise<void> {
const response = await fetch(APIPREFIX + apinode, {
method: 'POST', method: 'POST',
body: JSON.stringify(fd), body: JSON.stringify(fd),
headers: new Headers({ headers: new Headers({
'Content-Type': 'application/json', 'Content-Type': json ? 'application/json' : 'text/plain',
Authorization: 'Bearer ' + mytoken ...(!unsafe && {Authorization: 'Bearer ' + mytoken})
}) })
}).then((response) => });
response.text().then((result) => {
callback(result); if (response.status === 200) {
}) // success
); try {
}); // decode json or text
const data = json ? await response.json() : await response.text();
callback(data);
} catch (e) {
errorcallback(e);
}
} else if (response.status === 400) {
// Bad Request --> invalid token
console.log('loading Password page.');
// load password page
if (GlobalInfos.loadPasswordPage) {
GlobalInfos.loadPasswordPage(() => {
callAPI(apinode, fd, callback, errorcallback);
});
}
} else {
console.log('Error: ' + response.statusText);
if (errorcallback) {
errorcallback(response.statusText);
}
}
})();
} }
/** /**