Add dashboard UI updates and settings modal
This commit is contained in:
89
pikit-web/assets/api.js
Normal file
89
pikit-web/assets/api.js
Normal file
@@ -0,0 +1,89 @@
|
||||
// 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 target = isMock
|
||||
? path.replace('/api/status', '/data/mock-status.json').replace('/api/updates/config', '/data/mock-updates.json')
|
||||
: 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),
|
||||
});
|
||||
|
||||
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 }),
|
||||
});
|
||||
Reference in New Issue
Block a user