From 9d45bb5ceb5830971cf920cf4a310cde1d56ccb5 Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 12 Jun 2025 08:26:58 +0200 Subject: [PATCH] update example of typescript in README --- README.md | 114 +++++++++++++++++++++++++++--------------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index e903501..000d33e 100644 --- a/README.md +++ b/README.md @@ -237,74 +237,74 @@ To connect to the API from TypeScript (e.g. Vue application) it is possible to d * Generated by APIlite * https://gitea.tpsoft.org/TPsoft.org/APIlite * - * 2025-05-28 15:44:07 */ + * 2025-06-12 06:24:33 */ -export const backend = { - endpont: window.location.origin + "APIcalculator.php", +class APIcalculator { + endpont = "http://"; - /* ---------------------------------------------------- - * General API call - */ - call(method, data, callback) { - var xhttp = new XMLHttpRequest(); - xhttp.withCredentials = true; - xhttp.onreadystatechange = function() { - if (this.readyState == 4) { - if (this.status == 200) { - if (callback != null) callback(JSON.parse(this.responseText)); - } else { - if (callback != null) callback({'status': 'ERROR', 'message': 'HTTP STATUS ' + this.status}); - } - } - } - var form_data = new FormData(); - Object.keys(data).forEach(key => { - let val = data[key]; - if (typeof val == 'object') val = JSON.stringify(val); - form_data.append(key, val); - }); - xhttp.open('POST', this.endpont + '?action=' + method); - xhttp.send(form_data); - }, + /* ---------------------------------------------------- + * General API call + */ + call(method, data, callback) { + var xhttp = new XMLHttpRequest(); + xhttp.withCredentials = true; + xhttp.onreadystatechange = function() { + if (this.readyState == 4) { + if (this.status == 200) { + if (callback != null) callback(JSON.parse(this.responseText)); + } else { + if (callback != null) callback({'status': 'ERROR', 'message': 'HTTP STATUS ' + this.status}); + } + } + } + var form_data = new FormData(); + Object.keys(data).forEach(key => { + let val = data[key]; + if (typeof val == 'object') val = JSON.stringify(val); + form_data.append(key, val); + }); + xhttp.open('POST', this.endpont + '?action=' + method); + xhttp.send(form_data); + } - callPromise(method, data) { - return new Promise((resolve, reject) => { - this.call(method, data, function(response) { - if (response.status == 'OK') { - resolve(response.data); - } else { - reject(response.msg); - } - }); - }) - }, + callPromise(method, data) { + return new Promise((resolve, reject) => { + this.call(method, data, function(response) { + if (response.status == 'OK') { + resolve(response.data); + } else { + reject(response.msg); + } + }); + }) + } - /* ---------------------------------------------------- - * API actions - */ - help() { - return this.callPromise('__HELP__', {}); - }, + /* ---------------------------------------------------- + * API actions + */ + help() { + return this.callPromise('__HELP__', {}); + } - add(a, b) { - return this.callPromise('add', {a: a, b: b}); - }, + add(a, b) { + return this.callPromise('add', {a: a, b: b}); + } - subtract(a, b) { - return this.callPromise('subtract', {a: a, b: b}); - }, + subtract(a, b) { + return this.callPromise('subtract', {a: a, b: b}); + } - multiply(a, b) { - return this.callPromise('multiply', {a: a, b: b}); - }, - - divide(a, b) { - return this.callPromise('divide', {a: a, b: b}); - }, + multiply(a, b) { + return this.callPromise('multiply', {a: a, b: b}); + } + divide(a, b) { + return this.callPromise('divide', {a: a, b: b}); + } }; +export default new BackendAPI(); ``` These outputs can also be generated in the command line as follows