frontend structiure generated
This commit is contained in:
64
wagfarm-ui/js/api.js
Normal file
64
wagfarm-ui/js/api.js
Normal file
@@ -0,0 +1,64 @@
|
||||
// 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;
|
||||
Reference in New Issue
Block a user