Files
sprint/packages/frontend/src/lib/server/user/update.ts
2026-01-13 15:33:55 +00:00

36 lines
1.2 KiB
TypeScript

import type { UserRecord, UserUpdateRequest } from "@issue/shared";
import { toast } from "sonner";
import { getCsrfToken, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
export async function update(request: UserUpdateRequest & ServerQueryInput<UserRecord>) {
const { onSuccess, onError, ...body } = request;
const csrfToken = getCsrfToken();
const res = await fetch(`${getServerURL()}/user/update`, {
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 update user (${res.status})`;
toast.error(message);
onError?.(error);
} else {
const data = await res.json();
if (!data.id) {
toast.error(`failed to update user (${res.status})`);
onError?.(`failed to update user (${res.status})`);
return;
}
onSuccess?.(data, res);
}
}