fetchbee
Version:
A lightweight JS library to simplify all kinds of API calls.
32 lines (26 loc) • 765 B
JavaScript
async function deleteFetch(url, options = {}) {
if (!url || typeof url !== "string") {
console.warn("deleteFetch: URL is missing or invalid.");
return null;
}
const { query = {}, headers = {}, ...rest } = options;
const queryString = new URLSearchParams(query).toString();
const fullUrl = queryString ? `${url}?${queryString}` : url;
try {
const response = await fetch(fullUrl, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
...headers,
},
...rest,
});
if (!response.ok) {
throw new Error(`DELETE Error: ${response.status}`);
}
return await response.json();
} catch (err) {
console.error("deleteFetch failed:", err.message);
return null;
}
}