75869364b0
Frontend rewrite (frontend/, replacing backend/static/): - Vite + TypeScript + React 19, React Router 7, zustand, TanStack Query 5 - dev: vite proxy /api -> :9000; prod: npm run build -> dist/, served by the backend with SPA fallback - pages: Control (gun bays w/ scope+readiness-gated start/stop, relays), History (who/via/gun/when/duration/kWh audit), Cards (pending review, enrolled, rejected), Users (permission checkbox matrix) - self-contained: fonts bundled, no CDN; light/dark; reduced-motion safe Backend: - learn mode removed: unknown cards are always refused and queued in pending_cards; admin approves (name + owner + gun restrictions in one step) or rejects (denied_cards, silent, un-rejectable); pending count rides /api/status as the notification signal - sessions record the auth method (card tap vs in-app button) for audit - GET /api/permissions: grantable-permission catalog driving the UI matrix - SPA static serving with path-traversal guard; 503 hint when not built Smoke test: 54 checks incl. approve/reject round-trips over a fake charge point and method audit assertions.
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
export class ApiError extends Error {
|
|
status: number;
|
|
constructor(status: number, detail: string) {
|
|
super(detail);
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
|
const res = await fetch(`/api${path}`, {
|
|
headers: init?.body ? { "Content-Type": "application/json" } : undefined,
|
|
...init,
|
|
});
|
|
if (!res.ok) {
|
|
let detail = res.statusText;
|
|
try {
|
|
detail = (await res.json()).detail ?? detail;
|
|
} catch {
|
|
/* non-JSON error body */
|
|
}
|
|
throw new ApiError(res.status, detail);
|
|
}
|
|
return res.json() as Promise<T>;
|
|
}
|
|
|
|
export const api = {
|
|
get: <T>(path: string) => request<T>(path),
|
|
post: <T>(path: string, body?: unknown) =>
|
|
request<T>(path, { method: "POST", body: body ? JSON.stringify(body) : undefined }),
|
|
put: <T>(path: string, body: unknown) =>
|
|
request<T>(path, { method: "PUT", body: JSON.stringify(body) }),
|
|
delete: <T>(path: string) => request<T>(path, { method: "DELETE" }),
|
|
};
|
|
|
|
/** Mirrors backend/users.py `allowed()` — fnmatch-lite scope check. */
|
|
export function scopeAllowed(scopes: string[], needed: string): boolean {
|
|
return scopes.some((grant) => {
|
|
if (grant === "*" || grant === "admin") return true;
|
|
const re = new RegExp(
|
|
"^" +
|
|
grant
|
|
.split("*")
|
|
.map((p) => p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
|
|
.join(".*") +
|
|
"$",
|
|
);
|
|
return re.test(needed);
|
|
});
|
|
}
|
|
|
|
export const isAdmin = (scopes: string[]) =>
|
|
scopes.some((s) => s === "admin" || s === "*");
|