/* jshint esversion: 2024, module: true */ 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); } } export { ApiClient };