Refactor: Initial code split into includes directory for modularity
- Reorganized backend logic by moving API, authentication, Docker, status, and WebSocket handling into separate modules (api.js, auth.js, docker.js, status.js, websocket.js) within ./includes/ - Converted codebase to ES modules with import/export syntax for modern JavaScript - Updated index.js to serve as main entry point, importing from ./includes/ - Reduced code duplication and improved readability with modularized functions - Ensured full functionality preservation, including Docker stats and WebSocket communication - Updated README to reflect new folder structure and ES module setup
This commit is contained in:
21
includes/api.js
Normal file
21
includes/api.js
Normal file
@ -0,0 +1,21 @@
|
||||
import fetch from 'node-fetch';
|
||||
|
||||
export async function apiRequest(endpoint, apiKey, method = 'GET', body = null) {
|
||||
const headers = {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'x-my-mc-auth': apiKey
|
||||
};
|
||||
try {
|
||||
const response = await fetch(`${process.env.API_URL}${endpoint}`, {
|
||||
method,
|
||||
headers,
|
||||
body: body ? JSON.stringify(body) : null
|
||||
});
|
||||
const data = await response.json();
|
||||
if (!response.ok) return { error: data.message || `HTTP ${response.status}` };
|
||||
return data;
|
||||
} catch (error) {
|
||||
return { error: `Network error: ${error.message}` };
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user