237 lines
6.3 KiB
JavaScript
237 lines
6.3 KiB
JavaScript
/**
|
|
* Generated by APIlite
|
|
* https://gitea.tpsoft.org/TPsoft.org/APIlite
|
|
*
|
|
* 2026-08-22 20:42:16 */
|
|
|
|
class backend {
|
|
endpoint = import.meta.env.VITE_BACKENDAPI_URL;
|
|
bearerStorageKey = 'apilite_bearer_token';
|
|
responseHandlers = [];
|
|
errorHandlers = [];
|
|
|
|
normalizeBearerToken(token) {
|
|
if (typeof token !== 'string') return null;
|
|
token = token.trim();
|
|
return token === '' ? null : token;
|
|
}
|
|
|
|
getStorage() {
|
|
try {
|
|
if (typeof window !== 'undefined' && window.localStorage) return window.localStorage;
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
getBearerToken() {
|
|
var storage = this.getStorage();
|
|
if (storage == null) return null;
|
|
return this.normalizeBearerToken(storage.getItem(this.bearerStorageKey));
|
|
}
|
|
|
|
getRequestHeaders(headers = {}) {
|
|
var requestHeaders = Object.assign({}, headers);
|
|
if (typeof requestHeaders.Authorization === 'undefined') {
|
|
var token = this.getBearerToken();
|
|
if (token != null) requestHeaders.Authorization = 'Bearer ' + token;
|
|
}
|
|
return requestHeaders;
|
|
}
|
|
|
|
applyHeaders(xhttp, headers) {
|
|
Object.keys(headers).forEach(key => {
|
|
var value = headers[key];
|
|
if (typeof value === 'undefined' || value === null) return;
|
|
xhttp.setRequestHeader(key, value);
|
|
});
|
|
}
|
|
|
|
bearerSet(token) {
|
|
var storage = this.getStorage();
|
|
if (storage == null) return;
|
|
token = this.normalizeBearerToken(token);
|
|
if (token == null) {
|
|
storage.removeItem(this.bearerStorageKey);
|
|
return;
|
|
}
|
|
storage.setItem(this.bearerStorageKey, token);
|
|
}
|
|
|
|
addResponseHandler(handler) {
|
|
this.responseHandlers.push(handler);
|
|
return () => {
|
|
this.responseHandlers = this.responseHandlers.filter(registeredHandler => registeredHandler !== handler);
|
|
};
|
|
}
|
|
|
|
addErrorHandler(handler) {
|
|
this.errorHandlers.push(handler);
|
|
return () => {
|
|
this.errorHandlers = this.errorHandlers.filter(registeredHandler => registeredHandler !== handler);
|
|
};
|
|
}
|
|
|
|
emitResponseHandlers(response, context) {
|
|
this.responseHandlers.forEach(handler => {
|
|
try {
|
|
handler(response, context);
|
|
} catch (error) {
|
|
// Response handlers must not change the API request result.
|
|
}
|
|
});
|
|
}
|
|
|
|
emitErrorHandlers(response, context) {
|
|
this.errorHandlers.forEach(handler => {
|
|
try {
|
|
handler(response, context);
|
|
} catch (error) {
|
|
// Error handlers must not change the API request result.
|
|
}
|
|
});
|
|
}
|
|
|
|
emitHandlers(response, context) {
|
|
this.emitResponseHandlers(response, context);
|
|
if (response.status === 'ERROR') this.emitErrorHandlers(response, context);
|
|
}
|
|
|
|
/* ----------------------------------------------------
|
|
* General API call
|
|
*/
|
|
call(method, data, callback) {
|
|
var xhttp = new XMLHttpRequest();
|
|
var headers = this.getRequestHeaders();
|
|
var api = this;
|
|
xhttp.withCredentials = true;
|
|
xhttp.onreadystatechange = function() {
|
|
if (this.readyState === 4) {
|
|
var response;
|
|
if (this.status === 200) {
|
|
response = JSON.parse(this.responseText);
|
|
} else {
|
|
response = {'status': 'ERROR', 'msg': 'HTTP STATUS ' + this.status};
|
|
}
|
|
var context = {method: method, data: data, httpStatus: this.status};
|
|
api.emitHandlers(response, context);
|
|
if (callback != null) callback(response);
|
|
}
|
|
}
|
|
var form_data = new FormData();
|
|
Object.keys(data).forEach(key => {
|
|
let val = data[key];
|
|
if (typeof val === 'undefined') return;
|
|
if (typeof val == 'object') val = JSON.stringify(val);
|
|
form_data.append(key, val);
|
|
});
|
|
xhttp.open('POST', this.endpoint + '?action=' + method);
|
|
this.applyHeaders(xhttp, headers);
|
|
xhttp.send(form_data);
|
|
}
|
|
|
|
callPromise(method, data) {
|
|
return new Promise((resolve, reject) => {
|
|
this.call(method, data, function(response) {
|
|
if (method === '__HELP__') {
|
|
resolve(response);
|
|
return;
|
|
}
|
|
if (response.status === 'OK') {
|
|
resolve(response.data);
|
|
} else {
|
|
reject(response.msg);
|
|
}
|
|
});
|
|
})
|
|
}
|
|
|
|
/* ----------------------------------------------------
|
|
* API actions
|
|
*/
|
|
help() {
|
|
return this.callPromise('__HELP__', {});
|
|
}
|
|
|
|
add(title, description, status, project_id, priority) {
|
|
return this.callPromise('add', {title: title, description: description, status: status, project_id: project_id, priority: priority});
|
|
}
|
|
|
|
update(report_id, report_data) {
|
|
return this.callPromise('update', {report_id: report_id, report_data: report_data});
|
|
}
|
|
|
|
delete(report_id) {
|
|
return this.callPromise('delete', {report_id: report_id});
|
|
}
|
|
|
|
get(report_id) {
|
|
return this.callPromise('get', {report_id: report_id});
|
|
}
|
|
|
|
getAll(status, page, project_id) {
|
|
return this.callPromise('getAll', {status: status, page: page, project_id: project_id});
|
|
}
|
|
|
|
getAllGrouped(status, page, project_id) {
|
|
return this.callPromise('getAllGrouped', {status: status, page: page, project_id: project_id});
|
|
}
|
|
|
|
getArchived(page, project_id) {
|
|
return this.callPromise('getArchived', {page: page, project_id: project_id});
|
|
}
|
|
|
|
updateOrdNum(ordnums) {
|
|
return this.callPromise('updateOrdNum', {ordnums: ordnums});
|
|
}
|
|
|
|
updateStatus(report_id, status) {
|
|
return this.callPromise('updateStatus', {report_id: report_id, status: status});
|
|
}
|
|
|
|
projectGetAll(include_archived) {
|
|
return this.callPromise('projectGetAll', {include_archived: include_archived});
|
|
}
|
|
|
|
projectGet(project_id) {
|
|
return this.callPromise('projectGet', {project_id: project_id});
|
|
}
|
|
|
|
projectAdd(name, code, color) {
|
|
return this.callPromise('projectAdd', {name: name, code: code, color: color});
|
|
}
|
|
|
|
projectUpdate(project_id, name, color) {
|
|
return this.callPromise('projectUpdate', {project_id: project_id, name: name, color: color});
|
|
}
|
|
|
|
projectArchive(project_id, archived) {
|
|
return this.callPromise('projectArchive', {project_id: project_id, archived: archived});
|
|
}
|
|
|
|
attachmentAdd(report_id, attachment_type, attachment_content) {
|
|
return this.callPromise('attachmentAdd', {report_id: report_id, attachment_type: attachment_type, attachment_content: attachment_content});
|
|
}
|
|
|
|
attachmentUpdate(attachment_id, attachment_content) {
|
|
return this.callPromise('attachmentUpdate', {attachment_id: attachment_id, attachment_content: attachment_content});
|
|
}
|
|
|
|
attachmentGetAll(report_id) {
|
|
return this.callPromise('attachmentGetAll', {report_id: report_id});
|
|
}
|
|
|
|
attachmentDelete(attachment_id) {
|
|
return this.callPromise('attachmentDelete', {attachment_id: attachment_id});
|
|
}
|
|
|
|
attachmentDownload(filename) {
|
|
return this.callPromise('attachmentDownload', {filename: filename});
|
|
}
|
|
|
|
|
|
};
|
|
|
|
export default new backend();
|