1.started removal of CDNs.\n 2. Improved modular structure for all parts of project including database.

This commit is contained in:
2024-08-30 23:27:28 +01:00
parent 18977c8cac
commit f9cd9ec33a
2895 changed files with 490579 additions and 7561 deletions

69
static/js/api.js Normal file
View File

@@ -0,0 +1,69 @@
import DOM from './dom.js';
// Module for API calls
export default class API {
static getCsrfToken() {
// return document.querySelectorAll('meta[name=' + nameCSRFToken + ']').attr('content');
return document.querySelector(idCSRFToken).getAttribute('content');
}
static async request(hashEndpoint, method = 'GET', data = null) {
const url = mapHashToController(hashEndpoint);
const options = {
method,
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': API.getCsrfToken()
}
};
if (data) { //} && (method === 'POST' || method === 'PUT' || method === 'PATCH')) {
options.body = JSON.stringify(data);
}
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('API request failed:', error);
throw error;
}
}
// specific api calls
/* Example:
getUsers: () => request('/users'),
getUserById: (id) => request(`/users/${id}`),
createUser: (userData) => request('/users', 'POST', userData),
updateUser: (id, userData) => request(`/users/${id}`, 'PUT', userData),
deleteUser: (id) => request(`/users/${id}`, 'DELETE'),
*/
static async loginUser() {
let callback = {};
callback[keyCallback] = DOM.getHashPageCurrent();
return await API.request(hashPageUserLogin, 'POST', callback);
}
}
/*
const api = new API();
export default api;
Example of using the API
document.addEventListener('DOMContentLoaded', () => {
initializeApp();
setupEventListeners();
initializeComponents();
// Example of using the API
API.getData('/some-endpoint')
.then(data => console.log('Data received:', data))
.catch(error => console.error('Error:', error));
});
*/