From dcf70ae89c9bae0ad8e0263ca205ea168cfa7733 Mon Sep 17 00:00:00 2001 From: matu6968 Date: Mon, 11 Nov 2024 00:08:24 +0100 Subject: [PATCH] add javascript client demo --- README.md | 4 ++ client-demo/demo.js | 105 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 client-demo/demo.js diff --git a/README.md b/README.md index 430343c..a880a50 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/client-demo/demo.js b/client-demo/demo.js new file mode 100644 index 0000000..cc17028 --- /dev/null +++ b/client-demo/demo.js @@ -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");