2021-03-16 20:13:12 +01:00
|
|
|
import GlobalInfos from './GlobalInfos';
|
2021-05-08 15:19:13 +02:00
|
|
|
import {token} from './TokenHandler';
|
2021-03-16 20:13:12 +01:00
|
|
|
|
2021-04-02 17:04:15 +00:00
|
|
|
const APIPREFIX: string = '/api/';
|
2020-12-17 20:53:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* interface how an api request should look like
|
|
|
|
*/
|
|
|
|
interface ApiBaseRequest {
|
2021-03-14 14:51:53 +00:00
|
|
|
action: string | number;
|
2020-12-17 20:53:22 +00:00
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
[_: string]: string | number | boolean | object;
|
2020-12-17 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A backend api call
|
|
|
|
* @param apinode which api backend handler to call
|
|
|
|
* @param fd the object to send to backend
|
|
|
|
* @param callback the callback with json reply from backend
|
|
|
|
* @param errorcallback a optional callback if an error occured
|
|
|
|
*/
|
2021-03-14 14:51:53 +00:00
|
|
|
export function callAPI<T>(
|
|
|
|
apinode: APINode,
|
|
|
|
fd: ApiBaseRequest,
|
|
|
|
callback: (_: T) => void,
|
|
|
|
errorcallback: (_: string) => void = (_: string): void => {}
|
|
|
|
): void {
|
2021-05-07 17:31:35 +02:00
|
|
|
token.checkAPITokenValid((mytoken) => {
|
2021-04-02 17:04:15 +00:00
|
|
|
fetch(APIPREFIX + apinode, {
|
2021-03-14 14:51:53 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(fd),
|
|
|
|
headers: new Headers({
|
2021-03-09 12:56:53 +00:00
|
|
|
'Content-Type': 'application/json',
|
2021-05-07 17:31:35 +02:00
|
|
|
Authorization: 'Bearer ' + mytoken
|
2021-03-14 14:51:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.then((response) => {
|
2021-03-16 20:13:12 +01:00
|
|
|
if (response.status === 200) {
|
|
|
|
// success
|
2021-03-14 14:51:53 +00:00
|
|
|
response.json().then((result: T) => {
|
|
|
|
callback(result);
|
|
|
|
});
|
2021-03-16 20:13:12 +01:00
|
|
|
} 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);
|
2021-03-14 14:51:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((reason) => errorcallback(reason));
|
|
|
|
});
|
2020-12-17 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 12:49:24 +00:00
|
|
|
/**
|
|
|
|
* make a public unsafe api call (without token) -- use as rare as possible only for initialization (eg. check if pwd is neccessary)
|
|
|
|
* @param apinode
|
|
|
|
* @param fd
|
|
|
|
* @param callback
|
2021-03-14 14:51:53 +00:00
|
|
|
* @param errorcallback
|
2021-03-14 12:49:24 +00:00
|
|
|
*/
|
2021-03-14 14:51:53 +00:00
|
|
|
export function callApiUnsafe<T>(
|
|
|
|
apinode: APINode,
|
|
|
|
fd: ApiBaseRequest,
|
|
|
|
callback: (_: T) => void,
|
|
|
|
errorcallback?: (_: string) => void
|
|
|
|
): void {
|
2021-04-02 17:04:15 +00:00
|
|
|
fetch(APIPREFIX + apinode, {method: 'POST', body: JSON.stringify(fd)})
|
2021-03-14 14:51:53 +00:00
|
|
|
.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) : {}));
|
2021-03-14 12:49:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 20:53:22 +00:00
|
|
|
/**
|
|
|
|
* A backend api call
|
|
|
|
* @param apinode which api backend handler to call
|
|
|
|
* @param fd the object to send to backend
|
|
|
|
* @param callback the callback with PLAIN text reply from backend
|
|
|
|
*/
|
2021-01-29 22:15:17 +00:00
|
|
|
export function callAPIPlain(apinode: APINode, fd: ApiBaseRequest, callback: (_: string) => void): void {
|
2021-05-07 17:31:35 +02:00
|
|
|
token.checkAPITokenValid((mytoken) => {
|
2021-04-02 17:04:15 +00:00
|
|
|
fetch(APIPREFIX + apinode, {
|
2021-03-14 14:51:53 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(fd),
|
|
|
|
headers: new Headers({
|
2021-03-09 12:56:53 +00:00
|
|
|
'Content-Type': 'application/json',
|
2021-05-07 17:31:35 +02:00
|
|
|
Authorization: 'Bearer ' + mytoken
|
2021-03-09 12:56:53 +00:00
|
|
|
})
|
2021-03-14 14:51:53 +00:00
|
|
|
}).then((response) =>
|
|
|
|
response.text().then((result) => {
|
|
|
|
callback(result);
|
|
|
|
})
|
|
|
|
);
|
2021-03-09 12:56:53 +00:00
|
|
|
});
|
2020-12-17 20:53:22 +00:00
|
|
|
}
|
2021-01-29 22:15:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* API nodes definitions
|
|
|
|
*/
|
2021-03-16 20:13:12 +01:00
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
// eslint-disable-next-line no-shadow
|
2021-01-29 22:15:17 +00:00
|
|
|
export enum APINode {
|
2021-02-23 16:01:29 +00:00
|
|
|
Settings = 'settings',
|
|
|
|
Tags = 'tags',
|
|
|
|
Actor = 'actor',
|
2021-04-16 22:44:56 +02:00
|
|
|
Video = 'video',
|
|
|
|
TVShow = 'tvshow'
|
2021-01-29 22:15:17 +00:00
|
|
|
}
|