mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 02:33:01 +00:00
frontend server utility improvement
This commit is contained in:
39
packages/frontend/src/lib/server/organisation/create.ts
Normal file
39
packages/frontend/src/lib/server/organisation/create.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { getAuthHeaders, 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());
|
||||
|
||||
const res = await fetch(url.toString(), {
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const error = await res.text();
|
||||
onError?.(error || `failed to create organisation (${res.status})`);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
if (!data.id) {
|
||||
onError?.(`failed to create organisation (${res.status})`);
|
||||
return;
|
||||
}
|
||||
|
||||
onSuccess?.(data, res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user