Files
panel/includes/api.js
MCHost d38e2ad1f1 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
2025-06-16 12:30:18 -04:00

21 lines
627 B
JavaScript

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}` };
}
}