Files
wehub-resource-sync e4dcfc49aa
Tests / Lint and Format (push) Waiting to run
Tests / Web Node Tests (push) Waiting to run
Tests / Import Check (Python 3.11) (push) Waiting to run
Tests / Import Check (Python 3.12) (push) Waiting to run
Tests / Import Check (Python 3.13) (push) Waiting to run
Tests / Import Check (Python 3.14) (push) Waiting to run
Tests / Python Tests (Python 3.11) (push) Blocked by required conditions
Tests / Python Tests (Python 3.12) (push) Blocked by required conditions
Tests / Python Tests (Python 3.13) (push) Blocked by required conditions
Tests / Python Tests (Python 3.14) (push) Blocked by required conditions
Tests / Test Summary (push) Blocked by required conditions
chore: import upstream snapshot with attribution
2026-07-13 13:00:43 +08:00

49 lines
1.5 KiB
TypeScript

import { apiFetch, apiUrl } from "@/lib/api";
import type { GrantPayload, MultiUserResources } from "./types";
async function readError(res: Response, fallback: string): Promise<string> {
try {
const data = await res.json();
return String(data?.detail || fallback);
} catch {
return fallback;
}
}
export async function fetchAdminResources(): Promise<MultiUserResources> {
const res = await apiFetch(apiUrl("/api/v1/multi-user/admin/resources"));
if (!res.ok)
throw new Error(
await readError(res, "Failed to load assignable resources"),
);
return (await res.json()) as MultiUserResources;
}
export async function fetchUserGrant(userId: string): Promise<GrantPayload> {
const res = await apiFetch(
apiUrl(`/api/v1/multi-user/users/${encodeURIComponent(userId)}/grants`),
);
if (!res.ok)
throw new Error(await readError(res, "Failed to load user grant"));
const data = await res.json();
return data.grant as GrantPayload;
}
export async function saveUserGrant(
userId: string,
grant: GrantPayload,
): Promise<GrantPayload> {
const res = await apiFetch(
apiUrl(`/api/v1/multi-user/users/${encodeURIComponent(userId)}/grants`),
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ grant }),
},
);
if (!res.ok)
throw new Error(await readError(res, "Failed to save user grant"));
const data = await res.json();
return data.grant as GrantPayload;
}