From 38ac4db03560200df2517ed7e06e555828ce0a15 Mon Sep 17 00:00:00 2001 From: Petri Hienonen Date: Sun, 28 Sep 2025 16:25:48 +0300 Subject: Update --- static/api-client.js | 280 ++++++++++++++++++++++++++------------------------- 1 file changed, 141 insertions(+), 139 deletions(-) (limited to 'static/api-client.js') 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} - */ - 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} - */ - 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} - */ - 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} - */ - 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} - */ - static async getNetworkStatus() { - return ApiClient.get(ApiEndpoints.STATUS); - } - - /** - * Get configuration files list - * @static - * @returns {Promise} - */ - static async getConfigFiles() { - return ApiClient.get(ApiEndpoints.CONFIGS); - } - - /** - * Get configuration file content - * @static - * @param {string} name - File name - * @returns {Promise} - */ - 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} - */ - 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} - */ - static async saveConfig(name, content, restart) { - return ApiClient.post(ApiEndpoints.SAVE, { name, content, restart }); - } - - /** - * Get system logs - * @static - * @returns {Promise} - */ - static async getSystemLogs() { - return ApiClient.getText(ApiEndpoints.LOGS); - } - - /** - * Restart networkd service - * @static - * @returns {Promise} - */ - static async restartNetworkd() { - return ApiClient.post(ApiEndpoints.RELOAD); - } - - /** - * Reboot device - * @static - * @returns {Promise} - */ - static async rebootDevice() { - return ApiClient.post(ApiEndpoints.REBOOT); - } + /** + * API utility function + * @static + * @param {string} path - API endpoint + * @param {Object} [options] - Fetch options + * @returns {Promise} + */ + 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} + */ + 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} + */ + 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} + */ + 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} + */ + static async getNetworkStatus() { + return ApiClient.get(ApiEndpoints.STATUS); + } + + /** + * Get configuration files list + * @static + * @returns {Promise} + */ + static async getConfigFiles() { + return ApiClient.get(ApiEndpoints.CONFIGS); + } + + /** + * Get configuration file content + * @static + * @param {string} name - File name + * @returns {Promise} + */ + 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} + */ + 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} + */ + static async saveConfig(name, content, restart) { + return ApiClient.post(ApiEndpoints.SAVE, { name, content, restart }); + } + + /** + * Get system logs + * @static + * @returns {Promise} + */ + static async getSystemLogs() { + return ApiClient.getText(ApiEndpoints.LOGS); + } + + /** + * Restart networkd service + * @static + * @returns {Promise} + */ + static async restartNetworkd() { + return ApiClient.post(ApiEndpoints.RELOAD); + } + + /** + * Reboot device + * @static + * @returns {Promise} + */ + static async rebootDevice() { + return ApiClient.post(ApiEndpoints.REBOOT); + } } export { ApiClient }; -- cgit v1.2.3-70-g09d2