mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-10-10 03:30:59 +00:00
Replace the old React Router SSR setup with a static SPA build served by fabro-server, move setup and GitHub auth handling into Rust, and update the default local web URL and stale Arc-era references to match the Fabro name.
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
export interface ApiOptions {
|
|
init?: RequestInit;
|
|
request?: Request;
|
|
}
|
|
|
|
export async function apiFetch(path: string, options?: ApiOptions): Promise<Response> {
|
|
const { init } = options ?? {};
|
|
const response = await fetch(`/api/v1${path}`, {
|
|
...init,
|
|
credentials: "include",
|
|
headers: init?.headers,
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
window.location.href = "/auth/login";
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
export async function apiJson<T>(path: string, options?: ApiOptions): Promise<T> {
|
|
const response = await apiFetch(path, options);
|
|
if (!response.ok) {
|
|
throw new Response(null, { status: response.status, statusText: response.statusText });
|
|
}
|
|
return response.json() as Promise<T>;
|
|
}
|
|
|
|
export async function getSetupStatus(): Promise<{ configured: boolean }> {
|
|
const response = await fetch("/api/v1/setup/status", { credentials: "include" });
|
|
if (!response.ok) {
|
|
throw new Response(null, { status: response.status, statusText: response.statusText });
|
|
}
|
|
return response.json();
|
|
}
|
|
|
|
export async function getAuthMe(): Promise<{
|
|
user: {
|
|
login: string;
|
|
name: string;
|
|
email: string;
|
|
avatarUrl: string;
|
|
userUrl: string;
|
|
};
|
|
provider: string;
|
|
demoMode: boolean;
|
|
features: { session_sandboxes: boolean; retros: boolean };
|
|
}> {
|
|
const response = await fetch("/api/v1/auth/me", { credentials: "include" });
|
|
if (response.status === 401) {
|
|
throw new Response(null, { status: 401, statusText: "Unauthorized" });
|
|
}
|
|
if (!response.ok) {
|
|
throw new Response(null, { status: response.status, statusText: response.statusText });
|
|
}
|
|
return response.json();
|
|
}
|