2020-12-17 20:53:22 +00:00
|
|
|
let customBackendURL: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the domain of the api backend
|
|
|
|
* @return string domain of backend http://x.x.x.x/bla
|
|
|
|
*/
|
|
|
|
export function getBackendDomain(): string {
|
|
|
|
let userAgent = navigator.userAgent.toLowerCase();
|
|
|
|
if (userAgent.indexOf(' electron/') > -1) {
|
|
|
|
// Electron-specific code - force a custom backendurl
|
|
|
|
return (customBackendURL);
|
|
|
|
} else {
|
|
|
|
// use custom only if defined
|
|
|
|
if (customBackendURL) {
|
|
|
|
return (customBackendURL);
|
|
|
|
} else {
|
|
|
|
return (window.location.origin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set a custom backend domain
|
|
|
|
* @param domain a url in format [http://x.x.x.x/somanode]
|
|
|
|
*/
|
2020-12-29 19:39:30 +00:00
|
|
|
export function setCustomBackendDomain(domain: string): void {
|
2020-12-17 20:53:22 +00:00
|
|
|
customBackendURL = domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* a helper function to get the api path
|
|
|
|
*/
|
|
|
|
function getAPIDomain(): string {
|
|
|
|
return getBackendDomain() + '/api/';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* interface how an api request should look like
|
|
|
|
*/
|
|
|
|
interface ApiBaseRequest {
|
2020-12-29 19:39:30 +00:00
|
|
|
action: string | number,
|
2020-12-17 20:53:22 +00:00
|
|
|
|
2021-02-23 16:01:29 +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-01-29 22:15:17 +00:00
|
|
|
export function callAPI<T>(apinode: APINode, fd: ApiBaseRequest, callback: (_: T) => void, errorcallback: (_: string) => void = (_: string): void => {}): void {
|
2021-02-23 16:01:29 +00:00
|
|
|
fetch(getAPIDomain() + apinode, {method: 'POST', body: JSON.stringify(fd)})
|
2020-12-17 20:53:22 +00:00
|
|
|
.then((response) => response.json()
|
|
|
|
.then((result) => {
|
|
|
|
callback(result);
|
|
|
|
})).catch(reason => errorcallback(reason));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-02-23 16:01:29 +00:00
|
|
|
fetch(getAPIDomain() + apinode, {method: 'POST', body: JSON.stringify(fd)})
|
2020-12-17 20:53:22 +00:00
|
|
|
.then((response) => response.text()
|
|
|
|
.then((result) => {
|
|
|
|
callback(result);
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
2021-01-29 22:15:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* API nodes definitions
|
|
|
|
*/
|
|
|
|
export enum APINode {
|
2021-02-23 16:01:29 +00:00
|
|
|
Settings = 'settings',
|
|
|
|
Tags = 'tags',
|
|
|
|
Actor = 'actor',
|
|
|
|
Video = 'video'
|
2021-01-29 22:15:17 +00:00
|
|
|
}
|