refactored frontend api helpers to promise interface

This commit is contained in:
Oliver Bryan
2026-01-20 16:59:32 +00:00
parent 11bf3e68f8
commit 45343571f5
31 changed files with 253 additions and 553 deletions

View File

@@ -1,10 +1,8 @@
import type { OrganisationRecord, OrgCreateRequest } from "@sprint/shared";
import { toast } from "sonner";
import { getCsrfToken, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
import { getErrorMessage } from "..";
export async function create(request: OrgCreateRequest & ServerQueryInput<OrganisationRecord>) {
const { onSuccess, onError, ...body } = request;
export async function create(request: OrgCreateRequest): Promise<OrganisationRecord> {
const csrfToken = getCsrfToken();
const res = await fetch(`${getServerURL()}/organisation/create`, {
@@ -13,25 +11,18 @@ export async function create(request: OrgCreateRequest & ServerQueryInput<Organi
"Content-Type": "application/json",
...(csrfToken ? { "X-CSRF-Token": csrfToken } : {}),
},
body: JSON.stringify(body),
body: JSON.stringify(request),
credentials: "include",
});
if (!res.ok) {
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);
const message = await getErrorMessage(res, `failed to create organisation (${res.status})`);
throw new Error(message);
}
const data = (await res.json()) as OrganisationRecord;
if (!data.id) {
throw new Error(`failed to create organisation (${res.status})`);
}
return data;
}