function mesh() {
return typeof window !== "undefined" ? window.MobuxMesh : undefined;
}
export async function apiGet(path) {
const m = mesh();
if (m) return m.apiFetchJSON(path);
const res = await fetch(path, { headers: { Accept: "application/json" } });
if (!res.ok) throw new Error(`GET ${path} -> ${res.status}`);
return res.json();
}
export async function apiPutJSON(path, body) {
const m = mesh();
if (m) {
return m.apiFetch(path, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
}
return fetch(path, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
}
export async function apiPost(path, body) {
const m = mesh();
const opts = body
? {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
}
: { method: "POST" };
if (m) return m.apiFetch(path, opts);
return fetch(path, opts);
}
export async function apiSend(path, opts = {}) {
const m = mesh();
const merged = {
...opts,
headers: { "Content-Type": "application/json", ...(opts.headers || {}) },
};
if (m) return m.apiFetchJSON(path, merged);
const res = await fetch(path, merged);
if (!res.ok)
throw new Error(`${opts.method || "GET"} ${path} -> ${res.status}`);
const text = await res.text();
return text ? JSON.parse(text) : null;
}
export async function localGet(path) {
const res = await fetch(path, {
headers: { Accept: "application/json" },
credentials: "same-origin",
});
if (!res.ok) throw new Error(`GET ${path} -> ${res.status}`);
return res.json();
}
export async function localFetch(path, opts = {}) {
return fetch(path, { credentials: "same-origin", ...opts });
}