add javascript client demo
This commit is contained in:
parent
7750a72454
commit
dcf70ae89c
|
@ -43,6 +43,10 @@ In the .env file this is the only thing you can set
|
|||
PORT=8080
|
||||
TOKEN=bearer-token-here # The program will automatically make a new token if not found so do not bother putting your own one
|
||||
```
|
||||
# Client demostrations
|
||||
|
||||
[JavaScript](https://git.fluffy.pw/matu6968/webdesk-app-market-server/src/branch/main/client-demo/demo.js)
|
||||
|
||||
## Autostart with systemd or OpenRC
|
||||
|
||||
You can autostart the web server using systemd or OpenRC (init scripts are in the init-scripts folder)
|
||||
|
|
105
client-demo/demo.js
Normal file
105
client-demo/demo.js
Normal file
|
@ -0,0 +1,105 @@
|
|||
// WebDesk App Market server demo
|
||||
|
||||
const apiUrl = "http://localhost:8080";
|
||||
const bearerToken = "your-secret-bearer-token";
|
||||
|
||||
// Utility function for making authorized requests
|
||||
async function apiRequest(endpoint, method = "GET", data = null, isFileUpload = false) {
|
||||
const headers = { "Authorization": `Bearer ${bearerToken}` };
|
||||
|
||||
let body;
|
||||
if (data) {
|
||||
if (isFileUpload) {
|
||||
body = new FormData();
|
||||
for (const key in data) {
|
||||
body.append(key, data[key]);
|
||||
}
|
||||
} else {
|
||||
headers["Content-Type"] = "application/json";
|
||||
body = JSON.stringify(data);
|
||||
}
|
||||
}
|
||||
|
||||
const response = await fetch(`${apiUrl}${endpoint}`, {
|
||||
method,
|
||||
headers,
|
||||
body
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Error: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// List all apps
|
||||
async function listApps() {
|
||||
try {
|
||||
const apps = await apiRequest("/apps", "GET");
|
||||
console.log("List of Apps:", apps);
|
||||
} catch (error) {
|
||||
console.error("Failed to list apps:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Upload a new app
|
||||
async function uploadApp(appData, file, customPath = "") {
|
||||
try {
|
||||
const data = { metadata: JSON.stringify(appData) };
|
||||
data.file = file;
|
||||
if (customPath) data.customPath = customPath;
|
||||
|
||||
const newApp = await apiRequest("/apps", "POST", data, true);
|
||||
console.log("Uploaded App:", newApp);
|
||||
} catch (error) {
|
||||
console.error("Failed to upload app:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Edit an existing app
|
||||
async function editApp(appID, updates, newFile = null, customPath = "") {
|
||||
try {
|
||||
const data = { id: appID, ...updates };
|
||||
if (newFile) data.file = newFile;
|
||||
if (customPath) data.customPath = customPath;
|
||||
|
||||
const updatedApp = await apiRequest("/editapp", "POST", data, true);
|
||||
console.log("Updated App:", updatedApp);
|
||||
} catch (error) {
|
||||
console.error("Failed to edit app:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete an app by ID
|
||||
async function deleteApp(appID) {
|
||||
try {
|
||||
const params = new URLSearchParams({ id: appID });
|
||||
const result = await apiRequest(`/delete?${params}`, "POST");
|
||||
console.log("App deleted:", result);
|
||||
} catch (error) {
|
||||
console.error("Failed to delete app:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Example Usage
|
||||
// List all apps
|
||||
listApps();
|
||||
|
||||
// Upload a new app
|
||||
const newAppData = {
|
||||
developer: "John Doe",
|
||||
name: "Camera App",
|
||||
version: "1.0",
|
||||
info: "A great camera app",
|
||||
id: "1234"
|
||||
};
|
||||
const appFile = new File(["dummy content"], "camera_app.zip", { type: "application/zip" });
|
||||
uploadApp(newAppData, appFile, "custom_camera_path");
|
||||
|
||||
// Edit an existing app
|
||||
const appUpdates = { name: "Updated Camera App", info: "An updated camera app description" };
|
||||
const newAppFile = new File(["new content"], "updated_camera_app.zip", { type: "application/zip" });
|
||||
editApp("1234", appUpdates, newAppFile, "updated_camera_path");
|
||||
|
||||
// Delete an app by ID
|
||||
deleteApp("1234");
|
Loading…
Reference in New Issue
Block a user