mirror of
https://github.com/mudabbir-ahmad/MAD-Treasure-Hunt.git
synced 2026-10-07 19:00:20 +00:00
Add initial API interaction functions and database schema
This commit is contained in:
1 parent
533a7bd17e
commit
71a52da85b
6 files changed
+197
No files matched your search
@@ -0,0 +1,48 @@
|
||||
// This file contains functions to interact with the API or local database.
|
||||
|
||||
const API_BASE_URL = 'http://localhost:3000'; // Change this to the actual API endpoint when needed
|
||||
|
||||
// Generic fetch function
|
||||
const APIFetch = async (endpoint, options = {}) => {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/${endpoint}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`API error: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('Fetch error:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Example function to fetch a user by username
|
||||
const fetchUserByUsername = async (username) => {
|
||||
return await APIFetch(`users?username=${username}`);
|
||||
};
|
||||
|
||||
// Example function to fetch all groups
|
||||
const fetchGroups = async () => {
|
||||
return await APIFetch('groups');
|
||||
};
|
||||
|
||||
// Example function to fetch subgroups by group ID
|
||||
const fetchSubgroupsByGroupId = async (groupId) => {
|
||||
return await APIFetch(`groups/${groupId}/subgroups`);
|
||||
};
|
||||
|
||||
// RESTful API functions
|
||||
const fetchItems = async (endpoint) => await APIFetch(endpoint);
|
||||
const createItem = async (endpoint, data) => await APIFetch(endpoint, { method: 'POST', body: JSON.stringify(data) });
|
||||
const updateItem = async (endpoint, data) => await APIFetch(endpoint, { method: 'PUT', body: JSON.stringify(data) });
|
||||
const deleteItem = async (endpoint) => await APIFetch(endpoint, { method: 'DELETE' });
|
||||
|
||||
export { APIFetch, fetchUserByUsername, fetchGroups, fetchSubgroupsByGroupId, fetchItems, createItem, updateItem, deleteItem };
|
||||
Reference in new issue
Block a user