open-story-teller/story-player-web/classes/api-client.js
anthony@rabine.fr 0ad614699c
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
dedicated directory for story web version
2025-01-10 22:04:39 +01:00

59 lines
1.5 KiB
JavaScript

class ApiClient {
constructor(baseURL) {
this.baseURL = baseURL;
}
setBaseUrl(baseURL) {
this.baseURL = baseURL;
}
async request(endpoint, method = 'GET', data = null, headers = {}) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 5000);
const config = {
method,
headers: {
'Content-Type': 'application/json',
...headers
}
};
if (data) {
config.body = JSON.stringify(data);
}
const promise = fetch(`${this.baseURL}${endpoint}`, config).then(async (response) => {
const data = await response.json();
if (response.ok) {
return data;
} else {
return Promise.reject(data);
}
});
return promise.finally(() => clearTimeout(timeout));
}
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;