diff --git a/README.md b/README.md index 61f9c6b..127b33a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,272 @@ # APIlite -A set of tools to simplify the work of creating backend APIs for your frontend projects. Includes tutorials, patterns and practical examples for creating projects based on REST APIs. \ No newline at end of file +A set of tools to simplify the work of creating backend APIs for your frontend projects. Includes tutorials, patterns and practical examples for creating projects based on REST APIs. + +## Basic usage + +For example, we create an API for calculator. So we create class `APIcalculator` and store in file `test/APIcalculator.php` where we defined each actions for API as public method. + +``` + + +To connect to the API from TypeScript (e.g. Vue application) it is possible to download the backend script + +``` +/** + * Generated by APIlite + * https://gitea.tpsoft.org/TPsoft.org/APIlite + * + * 2025-05-28 15:44:07 */ + +export const backend = { + endpont: window.location.origin + "APIcalculator.php", + + /* ---------------------------------------------------- + * 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); + } + }); + }) + }, + + /* ---------------------------------------------------- + * API actions + */ + help() { + return this.callPromise('__HELP__', {}); + }, + + add(a, b) { + return this.callPromise('add', {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}); + }, + + +}; + +``` + +These outputs can also be generated in the command line as follows + +* for HTML`$> php APIcalculator.php --html` +* for TypeScript`$> php APIcalculator.php --typescript` diff --git a/src/APIlite.php b/src/APIlite.php index ebb087b..92ef7fe 100644 --- a/src/APIlite.php +++ b/src/APIlite.php @@ -28,7 +28,7 @@ class APIlite $this->doAction($_REQUEST['action']); } else { global $argv; - $switches = array_map('strtolower', $argv); + $switches = array_map('strtolower', is_array($argv) ? $argv : []); if ((isset($_REQUEST['format']) && $_REQUEST['format'] == 'html') || in_array('--html', $switches)) { $this->printHelpHTML(); } elseif ((isset($_REQUEST['format']) && $_REQUEST['format'] == 'typescript') || in_array('--typescript', $switches)) { diff --git a/test/Example.APIcalculator.png b/test/Example.APIcalculator.png new file mode 100644 index 0000000..217cb5a Binary files /dev/null and b/test/Example.APIcalculator.png differ