Files
sprint/packages/frontend/src/lib/server/timer/end.ts
2026-01-16 11:15:38 +00:00

31 lines
1.0 KiB
TypeScript

import type { TimerEndRequest, TimerState } from "@sprint/shared";
import { toast } from "sonner";
import { getCsrfToken, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
export async function end(request: TimerEndRequest & ServerQueryInput<TimerState>) {
const { onSuccess, onError, ...body } = request;
const csrfToken = getCsrfToken();
const res = await fetch(`${getServerURL()}/timer/end`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(csrfToken ? { "X-CSRF-Token": csrfToken } : {}),
},
body: JSON.stringify(body),
credentials: "include",
});
if (!res.ok) {
const error = await res.json().catch(() => res.text());
const message =
typeof error === "string" ? error : error.error || `failed to end timer (${res.status})`;
toast.error(message);
onError?.(error);
} else {
const data = await res.json();
onSuccess?.(data, res);
}
}