106 lines
3.0 KiB
JavaScript
106 lines
3.0 KiB
JavaScript
|
// 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");
|