Files
wagfarm/wagfarm-ui/js/api.js
2025-04-17 17:00:15 +02:00

64 lines
1.5 KiB
JavaScript

// api.js
const API = {
baseUrl: 'https://your-api-url.com/api',
async get(endpoint) {
try {
const response = await fetch(`${this.baseUrl}/${endpoint}`);
if (!response.ok) throw new Error(`API Error: ${response.status}`);
return await response.json();
} catch (error) {
console.error('GET request failed:', error);
throw error;
}
},
async post(endpoint, data) {
try {
const response = await fetch(`${this.baseUrl}/${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (!response.ok) throw new Error(`API Error: ${response.status}`);
return await response.json();
} catch (error) {
console.error('POST request failed:', error);
throw error;
}
},
async put(endpoint, data) {
try {
const response = await fetch(`${this.baseUrl}/${endpoint}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (!response.ok) throw new Error(`API Error: ${response.status}`);
return await response.json();
} catch (error) {
console.error('PUT request failed:', error);
throw error;
}
},
async delete(endpoint) {
try {
const response = await fetch(`${this.baseUrl}/${endpoint}`, {
method: 'DELETE'
});
if (!response.ok) throw new Error(`API Error: ${response.status}`);
return await response.json();
} catch (error) {
console.error('DELETE request failed:', error);
throw error;
}
}
};
export default API;