Files
Project-MAD-crudler/src/components/API/API.js
T
2025-12-04 19:26:56 +00:00

32 lines
1.1 KiB
JavaScript

const API = {};
API.get = (endpoint) => callFetch(endpoint, 'GET');
API.post = (endpoint, data) => callFetch(endpoint, 'POST', data);
API.put = (endpoint, data) => callFetch(endpoint, 'PUT', data);
API.delete = (endpoint) => callFetch(endpoint, 'DELETE');
export default API;
const callFetch = async (endpoint, method, dataObj = null) => {
// Build request object
let requestObj = {method: method}; // GET, POST, PUT or DELETE
if (dataObj)
requestObj = {
...requestObj,
headers: {'Content-type': 'application/json'},
body: JSON.stringify(dataObj),
};
// Call the fetch and process the return
try {
let result = null;
const response = await fetch(endpoint, requestObj);
if (response.status !== 204) result = await response.json();
return response.status >= 200 && response.status < 300
? {isSuccess: true, result}
: {isSuccess: false, message: `${result.message}`};
} catch (error) {
return {isSuccess: false, message: error.message};
}
};