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-01-22 21:05:21 +00:00
|
|
|
[_: string]: string | number | boolean
|
2020-12-17 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* helper function to build a formdata for requesting post data correctly
|
|
|
|
* @param args api request object
|
|
|
|
*/
|
|
|
|
function buildFormData(args: ApiBaseRequest): FormData {
|
|
|
|
const req = new FormData();
|
|
|
|
|
|
|
|
for (const i in args) {
|
2020-12-29 19:39:30 +00:00
|
|
|
req.append(i, (args[i].toString()));
|
2020-12-17 20:53:22 +00:00
|
|
|
}
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-12-29 19:39:30 +00:00
|
|
|
export function callAPI<T>(apinode: string, fd: ApiBaseRequest, callback: (_: T) => void, errorcallback: (_: string) => void = (_: string): void => {}): void {
|
2020-12-17 20:53:22 +00:00
|
|
|
fetch(getAPIDomain() + apinode, {method: 'POST', body: buildFormData(fd)})
|
|
|
|
.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
|
|
|
|
*/
|
2020-12-29 19:39:30 +00:00
|
|
|
export function callAPIPlain(apinode: string, fd: ApiBaseRequest, callback: (_: string) => void): void {
|
2020-12-17 20:53:22 +00:00
|
|
|
fetch(getAPIDomain() + apinode, {method: 'POST', body: buildFormData(fd)})
|
|
|
|
.then((response) => response.text()
|
|
|
|
.then((result) => {
|
|
|
|
callback(result);
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|