open-story-teller/story-player-raylib/classes/api-client.js
anthony@rabine.fr 432d72c80c
Some checks failed
build-story-editor / build_linux (push) Has been cancelled
build-story-editor / build_win32 (push) Has been cancelled
Deploy / deploy (push) Has been cancelled
First web server route, use of preact for raylib html version
2025-01-09 11:13:56 +01:00

55 lines
1.4 KiB
JavaScript

class ApiClient {
constructor(baseURL) {
this.baseURL = baseURL;
}
setBaseUrl(baseURL) {
this.baseURL = baseURL;
}
async request(endpoint, method = 'GET', data = null, headers = {}) {
const config = {
method,
headers: {
'Content-Type': 'application/json',
...headers
}
};
if (data) {
config.body = JSON.stringify(data);
}
try {
const response = await fetch(`${this.baseURL}${endpoint}`, config);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'Something went wrong');
}
return await response.json();
} catch (error) {
console.error('API request error:', error);
throw error;
}
}
get(endpoint, headers = {}) {
return this.request(endpoint, 'GET', null, headers);
}
post(endpoint, data, headers = {}) {
return this.request(endpoint, 'POST', data, headers);
}
put(endpoint, data, headers = {}) {
return this.request(endpoint, 'PUT', data, headers);
}
delete(endpoint, headers = {}) {
return this.request(endpoint, 'DELETE', null, headers);
}
}
// Export de l'instance ApiClient pour l'importer facilement
const apiClient = new ApiClient('127.0.0.1:8081');
export default apiClient;