use ServerQueryInput properly

This commit is contained in:
Oliver Bryan
2026-01-13 15:33:55 +00:00
parent 4e93eb5878
commit ca371b1751
21 changed files with 284 additions and 329 deletions

View File

@@ -1,44 +1,37 @@
import type { OrganisationRecord, OrgCreateRequest } from "@issue/shared";
import { toast } from "sonner";
import { getCsrfToken, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
export async function create({
name,
slug,
userId,
description,
onSuccess,
onError,
}: {
name: string;
slug: string;
userId: number;
description: string;
} & ServerQueryInput) {
const url = new URL(`${getServerURL()}/organisation/create`);
url.searchParams.set("name", name.trim());
url.searchParams.set("slug", slug.trim());
url.searchParams.set("userId", `${userId}`);
if (description.trim() !== "") url.searchParams.set("description", description.trim());
export async function create(request: OrgCreateRequest & ServerQueryInput<OrganisationRecord>) {
const { onSuccess, onError, ...body } = request;
const csrfToken = getCsrfToken();
const headers: HeadersInit = {};
if (csrfToken) headers["X-CSRF-Token"] = csrfToken;
const res = await fetch(url.toString(), {
headers,
const res = await fetch(`${getServerURL()}/organisation/create`, {
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.text();
onError?.(error || `failed to create organisation (${res.status})`);
const error = await res.json().catch(() => res.text());
const message =
typeof error === "string"
? error
: error.error || `failed to create organisation (${res.status})`;
toast.error(message);
onError?.(error);
} else {
const data = await res.json();
if (!data.id) {
toast.error(`failed to create organisation (${res.status})`);
onError?.(`failed to create organisation (${res.status})`);
return;
}
onSuccess?.(data, res);
}
}