summaryrefslogtreecommitdiffstats
path: root/static/api-client.js
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--static/api-client.js280
1 files changed, 141 insertions, 139 deletions
diff --git a/static/api-client.js b/static/api-client.js
index 7a6ffbd..9582dc0 100644
--- a/static/api-client.js
+++ b/static/api-client.js
@@ -1,150 +1,152 @@
/* jshint esversion: 2024, module: true */
-import { ApiEndpoints } from './enums.js';
+import { ApiEndpoints } from "./enums.js";
/**
* Static API Client for network operations
* @class ApiClient
*/
class ApiClient {
- /**
- * API utility function
- * @static
- * @param {string} path - API endpoint
- * @param {Object} [options] - Fetch options
- * @returns {Promise<Response>}
- */
- static async request(path, options = {}) {
- const response = await fetch(path, options);
-
- if (!response.ok) {
- const text = await response.text();
- throw new Error(`${response.status} ${text}`);
- }
-
- return response;
- }
-
- /**
- * GET request returning JSON
- * @static
- * @param {string} path - API endpoint
- * @returns {Promise<Object>}
- */
- static async get(path) {
- const response = await ApiClient.request(path);
- return response.json();
- }
-
- /**
- * GET request returning text
- * @static
- * @param {string} path - API endpoint
- * @returns {Promise<string>}
- */
- static async getText(path) {
- const response = await ApiClient.request(path);
- return response.text();
- }
-
- /**
- * POST request with JSON body
- * @static
- * @param {string} path - API endpoint
- * @param {Object} [data] - Request body
- * @returns {Promise<Object>}
- */
- static async post(path, data = null) {
- const options = {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- };
-
- if (data) {
- options.body = JSON.stringify(data);
- }
-
- const response = await ApiClient.request(path, options);
- return response.json();
- }
-
- /**
- * Get network status
- * @static
- * @returns {Promise<Object>}
- */
- static async getNetworkStatus() {
- return ApiClient.get(ApiEndpoints.STATUS);
- }
-
- /**
- * Get configuration files list
- * @static
- * @returns {Promise<Object>}
- */
- static async getConfigFiles() {
- return ApiClient.get(ApiEndpoints.CONFIGS);
- }
-
- /**
- * Get configuration file content
- * @static
- * @param {string} name - File name
- * @returns {Promise<string>}
- */
- static async getConfigFile(name) {
- return ApiClient.getText(`${ApiEndpoints.CONFIG}/${encodeURIComponent(name)}`);
- }
-
- /**
- * Validate configuration
- * @static
- * @param {string} name - File name
- * @param {string} content - File content
- * @returns {Promise<Object>}
- */
- static async validateConfig(name, content) {
- return ApiClient.post(ApiEndpoints.VALIDATE, { name, content });
- }
-
- /**
- * Save configuration file
- * @static
- * @param {string} name - File name
- * @param {string} content - File content
- * @param {boolean} restart - Restart service
- * @returns {Promise<Object>}
- */
- static async saveConfig(name, content, restart) {
- return ApiClient.post(ApiEndpoints.SAVE, { name, content, restart });
- }
-
- /**
- * Get system logs
- * @static
- * @returns {Promise<string>}
- */
- static async getSystemLogs() {
- return ApiClient.getText(ApiEndpoints.LOGS);
- }
-
- /**
- * Restart networkd service
- * @static
- * @returns {Promise<Object>}
- */
- static async restartNetworkd() {
- return ApiClient.post(ApiEndpoints.RELOAD);
- }
-
- /**
- * Reboot device
- * @static
- * @returns {Promise<Object>}
- */
- static async rebootDevice() {
- return ApiClient.post(ApiEndpoints.REBOOT);
- }
+ /**
+ * API utility function
+ * @static
+ * @param {string} path - API endpoint
+ * @param {Object} [options] - Fetch options
+ * @returns {Promise<Response>}
+ */
+ static async request(path, options = {}) {
+ const response = await fetch(path, options);
+
+ if (!response.ok) {
+ const text = await response.text();
+ throw new Error(`${response.status} ${text}`);
+ }
+
+ return response;
+ }
+
+ /**
+ * GET request returning JSON
+ * @static
+ * @param {string} path - API endpoint
+ * @returns {Promise<Object>}
+ */
+ static async get(path) {
+ const response = await ApiClient.request(path);
+ return response.json();
+ }
+
+ /**
+ * GET request returning text
+ * @static
+ * @param {string} path - API endpoint
+ * @returns {Promise<string>}
+ */
+ static async getText(path) {
+ const response = await ApiClient.request(path);
+ return response.text();
+ }
+
+ /**
+ * POST request with JSON body
+ * @static
+ * @param {string} path - API endpoint
+ * @param {Object} [data] - Request body
+ * @returns {Promise<Object>}
+ */
+ static async post(path, data = null) {
+ const options = {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ };
+
+ if (data) {
+ options.body = JSON.stringify(data);
+ }
+
+ const response = await ApiClient.request(path, options);
+ return response.json();
+ }
+
+ /**
+ * Get network status
+ * @static
+ * @returns {Promise<Object>}
+ */
+ static async getNetworkStatus() {
+ return ApiClient.get(ApiEndpoints.STATUS);
+ }
+
+ /**
+ * Get configuration files list
+ * @static
+ * @returns {Promise<Object>}
+ */
+ static async getConfigFiles() {
+ return ApiClient.get(ApiEndpoints.CONFIGS);
+ }
+
+ /**
+ * Get configuration file content
+ * @static
+ * @param {string} name - File name
+ * @returns {Promise<string>}
+ */
+ static async getConfigFile(name) {
+ return ApiClient.getText(
+ `${ApiEndpoints.CONFIG}/${encodeURIComponent(name)}`,
+ );
+ }
+
+ /**
+ * Validate configuration
+ * @static
+ * @param {string} name - File name
+ * @param {string} content - File content
+ * @returns {Promise<Object>}
+ */
+ static async validateConfig(name, content) {
+ return ApiClient.post(ApiEndpoints.VALIDATE, { name, content });
+ }
+
+ /**
+ * Save configuration file
+ * @static
+ * @param {string} name - File name
+ * @param {string} content - File content
+ * @param {boolean} restart - Restart service
+ * @returns {Promise<Object>}
+ */
+ static async saveConfig(name, content, restart) {
+ return ApiClient.post(ApiEndpoints.SAVE, { name, content, restart });
+ }
+
+ /**
+ * Get system logs
+ * @static
+ * @returns {Promise<string>}
+ */
+ static async getSystemLogs() {
+ return ApiClient.getText(ApiEndpoints.LOGS);
+ }
+
+ /**
+ * Restart networkd service
+ * @static
+ * @returns {Promise<Object>}
+ */
+ static async restartNetworkd() {
+ return ApiClient.post(ApiEndpoints.RELOAD);
+ }
+
+ /**
+ * Reboot device
+ * @static
+ * @returns {Promise<Object>}
+ */
+ static async rebootDevice() {
+ return ApiClient.post(ApiEndpoints.REBOOT);
+ }
}
export { ApiClient };