Files
pi-kit/pikit-web/assets/api.js

132 lines
3.2 KiB
JavaScript

// Lightweight fetch wrapper for the Pi-Kit API endpoints exposed by the mock server
// and on-device Python API. All helpers below return parsed JSON or throw the
// JSON error body when the response is not 2xx.
const headers = { "Content-Type": "application/json" };
export async function api(path, opts = {}) {
// When running `npm run dev` without the backend, allow mock JSON from /data/
const isMock = import.meta?.env?.MODE === 'development' && path.startsWith('/api');
const mockMap = {
'/api/status': '/data/mock-status.json',
'/api/updates/config': '/data/mock-updates.json',
'/api/update/status': '/data/mock-update-status.json',
};
const target = isMock && mockMap[path] ? mockMap[path] : path;
const res = await fetch(target, { headers, ...opts });
// If mock files are missing, surface a clear error instead of JSON parse of HTML
const text = await res.text();
let data;
try {
data = JSON.parse(text);
} catch (e) {
throw new Error(`Expected JSON from ${target}, got: ${text.slice(0, 120)}...`);
}
if (!res.ok) throw data;
return data;
}
export const getStatus = () => api("/api/status");
export const toggleUpdates = (enable) =>
api("/api/updates/auto", {
method: "POST",
body: JSON.stringify({ enable }),
});
export const getUpdateConfig = () => api("/api/updates/config");
export const saveUpdateConfig = (config) =>
api("/api/updates/config", {
method: "POST",
body: JSON.stringify(config),
});
// Pi-Kit release updater endpoints
export const getReleaseStatus = () => api("/api/update/status");
export const checkRelease = () =>
api("/api/update/check", {
method: "POST",
});
export const applyRelease = () =>
api("/api/update/apply", {
method: "POST",
});
export const rollbackRelease = () =>
api("/api/update/rollback", {
method: "POST",
});
export const setReleaseAutoCheck = (enable) =>
api("/api/update/auto", {
method: "POST",
body: JSON.stringify({ enable }),
});
export const setReleaseChannel = (channel) =>
api("/api/update/channel", {
method: "POST",
body: JSON.stringify({ channel }),
});
export const triggerReset = (confirm) =>
api("/api/reset", {
method: "POST",
body: JSON.stringify({ confirm }),
});
export const addService = ({
name,
port,
scheme,
path,
notice,
notice_link,
self_signed,
}) =>
api("/api/services/add", {
method: "POST",
body: JSON.stringify({ name, port, scheme, path, notice, notice_link, self_signed }),
});
export const updateService = ({
port,
name,
new_port,
scheme,
path,
notice,
notice_link,
self_signed,
}) =>
api("/api/services/update", {
method: "POST",
body: JSON.stringify({
port,
name,
new_port,
scheme,
path,
notice,
notice_link,
self_signed,
}),
});
export const removeService = ({ port }) =>
api("/api/services/remove", {
method: "POST",
body: JSON.stringify({ port }),
});
// Diagnostics
export const getDiagLog = () => api("/api/diag/log");
export const setDiagLevel = ({ enabled, level }) =>
api("/api/diag/log/level", {
method: "POST",
body: JSON.stringify({ enabled, level }),
});
export const clearDiagLog = () =>
api("/api/diag/log/clear", {
method: "POST",
});