fetchbee
Version:
A lightweight JS library to simplify all kinds of API calls.
57 lines (43 loc) • 1.11 kB
JavaScript
async function getCombined(baseUrl, config = {}) {
if (!baseUrl || typeof baseUrl !== "string") {
console.warn("getCombined: baseUrl is missing or invalid.");
return null;
}
const {
id,
search,
page,
size,
from,
to,
headers = {},
...rest
} = config || {};
const url = id ? `${baseUrl}/${id}` : baseUrl;
const query = {};
if (search != null && search !== "") query.search = search;
if (page != null) query.page = page;
if (size != null) query.size = size;
if (from) query.from = from;
if (to) query.to = to;
return getFetch(url, {
...rest,
query,
headers,
});
}
// ------------------- Minimal:
// getCombined("http://localhost:1201/api/accounts");
// ------------------- With search and pagination:
// getCombined("http://localhost:1201/api/accounts", {
// search: "john",
// page: 1,
// size: 10,
// });
// ------------------ With ID and headers:
// getCombined("http://localhost:1201/api/accounts", {
// id: 42,
// headers: {
// Authorization: `Bearer ${sessionStorage.getItem("authToken")}`,
// }
// });