45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
export const API_BASE = import.meta.env.VITE_API_BASE || "/api";
|
|
|
|
async function request(path, { method = "GET", body, headers } = {}) {
|
|
const response = await fetch(`${API_BASE}${path}`, {
|
|
method,
|
|
headers: { "content-type": "application/json", ...headers },
|
|
body: body === undefined ? undefined : JSON.stringify(body),
|
|
});
|
|
|
|
if (response.status === 204) return null;
|
|
|
|
const text = await response.text();
|
|
let data = null;
|
|
try {
|
|
data = text ? JSON.parse(text) : null;
|
|
} catch {
|
|
data = text;
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const error = new Error(data?.message || `Request failed (${response.status}).`);
|
|
error.status = response.status;
|
|
error.data = data;
|
|
throw error;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
export const api = {
|
|
get: (path) => request(path),
|
|
post: (path, body) => request(path, { method: "POST", body }),
|
|
patch: (path, body) => request(path, { method: "PATCH", body }),
|
|
del: (path) => request(path, { method: "DELETE" }),
|
|
};
|
|
|
|
export const sampleJson = JSON.stringify(
|
|
[
|
|
{ domain: "gitpipeline.metatronhost.com", ports: [7777], is_backup: false },
|
|
{ domain: "rayaari-email.metatronhost.com", ports: [13000], is_backup: false },
|
|
],
|
|
null,
|
|
2,
|
|
);
|