mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 18:33:01 +00:00
refactored frontend api helpers to promise interface
This commit is contained in:
@@ -1,19 +1,16 @@
|
||||
import type { OrganisationResponse } from "@sprint/shared";
|
||||
import { getServerURL } from "@/lib/utils";
|
||||
import type { ServerQueryInput } from "..";
|
||||
import { getErrorMessage } from "..";
|
||||
|
||||
export async function byUser({ onSuccess, onError }: ServerQueryInput<OrganisationResponse[]>) {
|
||||
export async function byUser(): Promise<OrganisationResponse[]> {
|
||||
const res = await fetch(`${getServerURL()}/organisations/by-user`, {
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const error = await res.json().catch(() => res.text());
|
||||
const message =
|
||||
typeof error === "string" ? error : error.error || `failed to get organisations (${res.status})`;
|
||||
onError?.(message);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
onSuccess?.(data, res);
|
||||
const message = await getErrorMessage(res, `failed to get organisations (${res.status})`);
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
return res.json();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user