separated JavaScript and TypeScript export,
added AGENTS.md for AI bot
This commit is contained in:
98
README.md
98
README.md
@ -132,6 +132,7 @@ When you run this subfile through the webserver, you will see the JSON documenta
|
||||
{
|
||||
"name": "APIcalculator",
|
||||
"html_version": "http://localhost/APIlite/test/APIcalculator.php?format=html",
|
||||
"javascript_version": "http://localhost/APIlite/test/APIcalculator.php?format=javascript",
|
||||
"typescript_version": "http://localhost/APIlite/test/APIcalculator.php?format=typescript",
|
||||
"actions": [
|
||||
{
|
||||
@ -230,84 +231,33 @@ and there is also an HTML version available
|
||||
|
||||
<img src="test/Example.APIcalculator.png" />
|
||||
|
||||
To connect to the API from TypeScript (e.g. Vue application) it is possible to download the backend script
|
||||
You can also download generated frontend clients:
|
||||
|
||||
```ts
|
||||
/**
|
||||
* Generated by APIlite
|
||||
* https://gitea.tpsoft.org/TPsoft.org/APIlite
|
||||
*
|
||||
* 2025-06-12 06:24:33 */
|
||||
* JavaScript: `?format=javascript`
|
||||
* TypeScript (typed for Vue/TS projects): `?format=typescript`
|
||||
|
||||
class APIcalculator {
|
||||
endpont = "http://";
|
||||
JavaScript usage example:
|
||||
|
||||
/* ----------------------------------------------------
|
||||
* 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);
|
||||
}
|
||||
```js
|
||||
import backend from './backend.js';
|
||||
|
||||
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});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export default new BackendAPI();
|
||||
backend.add(1, 2).then((response) => {
|
||||
console.log(response.data);
|
||||
});
|
||||
```
|
||||
|
||||
These outputs can also be generated in the command line as follows
|
||||
TypeScript usage example (Vue + TS):
|
||||
|
||||
* for HTML`$> php APIcalculator.php --html`
|
||||
* for TypeScript`$> php APIcalculator.php --typescript`
|
||||
```ts
|
||||
import backend from './backend';
|
||||
|
||||
backend.add(1, 2).then((response) => {
|
||||
console.log(response.data); // typed value based on PHP return type
|
||||
});
|
||||
```
|
||||
|
||||
These outputs can also be generated in command line:
|
||||
|
||||
* HTML: `$> php APIcalculator.php --html`
|
||||
* JavaScript: `$> php APIcalculator.php --javascript > backend.js` (`--js` alias is available)
|
||||
* TypeScript: `$> php APIcalculator.php --typescript > backend.ts`
|
||||
|
||||
Reference in New Issue
Block a user