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,14 +1,8 @@
import type { UserRecord } from "@sprint/shared";
import { getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
import { getErrorMessage } from "..";
export async function byUsername({
username,
onSuccess,
onError,
}: {
username: string;
} & ServerQueryInput<UserRecord>) {
export async function byUsername(username: string): Promise<UserRecord> {
const url = new URL(`${getServerURL()}/user/by-username`);
url.searchParams.set("username", username);
@@ -17,12 +11,9 @@ export async function byUsername({
});
if (!res.ok) {
const error = await res.json().catch(() => res.text());
const message =
typeof error === "string" ? error : error.error || `failed to get user (${res.status})`;
onError?.(message);
} else {
const data = (await res.json()) as UserRecord;
onSuccess?.(data, res);
const message = await getErrorMessage(res, `failed to get user (${res.status})`);
throw new Error(message);
}
return res.json();
}