frontend helper functions for timer

This commit is contained in:
Oliver Bryan
2026-01-09 22:25:03 +00:00
parent d6604d2843
commit d64470da06
6 changed files with 130 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
import { getCsrfToken, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
export async function list({
limit,
offset,
onSuccess,
onError,
}: {
limit?: number;
offset?: number;
} & ServerQueryInput) {
const url = new URL(`${getServerURL()}/timers`);
if (limit != null) url.searchParams.set("limit", `${limit}`);
if (offset != null) url.searchParams.set("offset", `${offset}`);
const csrfToken = getCsrfToken();
const headers: HeadersInit = {};
if (csrfToken) headers["X-CSRF-Token"] = csrfToken;
const res = await fetch(url.toString(), {
headers,
credentials: "include",
});
if (!res.ok) {
const error = await res.text();
onError?.(error || `failed to get timers (${res.status})`);
} else {
const data = await res.json();
onSuccess?.(data, res);
}
}