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:
8
packages/frontend/src/lib/server/index.ts
Normal file
8
packages/frontend/src/lib/server/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export * as issue from "./issue";
|
||||
export * as organisation from "./organisation";
|
||||
export * as project from "./project";
|
||||
|
||||
export type ServerQueryInput = {
|
||||
onSuccess?: (data: any, res: Response) => void;
|
||||
onError?: (error: any) => void;
|
||||
};
|
||||
26
packages/frontend/src/lib/server/issue/byProject.ts
Normal file
26
packages/frontend/src/lib/server/issue/byProject.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { getAuthHeaders, getServerURL } from "@/lib/utils";
|
||||
import type { ServerQueryInput } from "..";
|
||||
|
||||
export async function byProject({
|
||||
projectId,
|
||||
onSuccess,
|
||||
onError,
|
||||
}: {
|
||||
projectId: number;
|
||||
} & ServerQueryInput) {
|
||||
const url = new URL(`${getServerURL()}/issues/by-project`);
|
||||
url.searchParams.set("projectId", `${projectId}`);
|
||||
|
||||
const res = await fetch(url.toString(), {
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const error = await res.text();
|
||||
onError?.(error || `failed to get issues by project (${res.status})`);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
|
||||
onSuccess?.(data, res);
|
||||
}
|
||||
}
|
||||
36
packages/frontend/src/lib/server/issue/create.ts
Normal file
36
packages/frontend/src/lib/server/issue/create.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { getAuthHeaders, getServerURL } from "@/lib/utils";
|
||||
import type { ServerQueryInput } from "..";
|
||||
|
||||
export async function create({
|
||||
projectId,
|
||||
title,
|
||||
description,
|
||||
onSuccess,
|
||||
onError,
|
||||
}: {
|
||||
projectId: number;
|
||||
title: string;
|
||||
description: string;
|
||||
} & ServerQueryInput) {
|
||||
const url = new URL(`${getServerURL()}/issue/create`);
|
||||
url.searchParams.set("projectId", `${projectId}`);
|
||||
url.searchParams.set("title", title.trim());
|
||||
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 issue (${res.status})`);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
if (!data.id) {
|
||||
onError?.(`failed to create issue (${res.status})`);
|
||||
return;
|
||||
}
|
||||
|
||||
onSuccess?.(data, res);
|
||||
}
|
||||
}
|
||||
2
packages/frontend/src/lib/server/issue/index.ts
Normal file
2
packages/frontend/src/lib/server/issue/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { byProject } from "./byProject";
|
||||
export { create } from "./create";
|
||||
30
packages/frontend/src/lib/server/organisation/byUser.ts
Normal file
30
packages/frontend/src/lib/server/organisation/byUser.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { getAuthHeaders, getServerURL } from "@/lib/utils";
|
||||
import type { ServerQueryInput } from "..";
|
||||
|
||||
export async function byUser({
|
||||
userId,
|
||||
onSuccess,
|
||||
onError,
|
||||
}: {
|
||||
userId: number;
|
||||
} & ServerQueryInput) {
|
||||
const url = new URL(`${getServerURL()}/organisations/by-user`);
|
||||
url.searchParams.set("userId", `${userId}`);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
2
packages/frontend/src/lib/server/organisation/index.ts
Normal file
2
packages/frontend/src/lib/server/organisation/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { byUser } from "./byUser";
|
||||
export { create } from "./create";
|
||||
26
packages/frontend/src/lib/server/project/byOrganisation.ts
Normal file
26
packages/frontend/src/lib/server/project/byOrganisation.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { getAuthHeaders, getServerURL } from "@/lib/utils";
|
||||
import type { ServerQueryInput } from "..";
|
||||
|
||||
export async function byOrganisation({
|
||||
organisationId,
|
||||
onSuccess,
|
||||
onError,
|
||||
}: {
|
||||
organisationId: number;
|
||||
} & ServerQueryInput) {
|
||||
const url = new URL(`${getServerURL()}/projects/by-organisation`);
|
||||
url.searchParams.set("organisationId", `${organisationId}`);
|
||||
|
||||
const res = await fetch(url.toString(), {
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const error = await res.text();
|
||||
onError?.(error || `failed to get projects by organisation (${res.status})`);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
|
||||
onSuccess?.(data, res);
|
||||
}
|
||||
}
|
||||
39
packages/frontend/src/lib/server/project/create.ts
Normal file
39
packages/frontend/src/lib/server/project/create.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { getAuthHeaders, getServerURL } from "@/lib/utils";
|
||||
import type { ServerQueryInput } from "..";
|
||||
|
||||
export async function create({
|
||||
key,
|
||||
name,
|
||||
creatorId,
|
||||
organisationId,
|
||||
onSuccess,
|
||||
onError,
|
||||
}: {
|
||||
key: string;
|
||||
name: string;
|
||||
creatorId: number;
|
||||
organisationId: number;
|
||||
} & ServerQueryInput) {
|
||||
const url = new URL(`${getServerURL()}/project/create`);
|
||||
url.searchParams.set("key", key.trim());
|
||||
url.searchParams.set("name", name.trim());
|
||||
url.searchParams.set("creatorId", `${creatorId}`);
|
||||
url.searchParams.set("organisationId", `${organisationId}`);
|
||||
|
||||
const res = await fetch(url.toString(), {
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const error = await res.text();
|
||||
onError?.(error || `failed to create project (${res.status})`);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
if (!data.id) {
|
||||
onError?.(`failed to create project (${res.status})`);
|
||||
return;
|
||||
}
|
||||
|
||||
onSuccess?.(data, res);
|
||||
}
|
||||
}
|
||||
2
packages/frontend/src/lib/server/project/index.ts
Normal file
2
packages/frontend/src/lib/server/project/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { byOrganisation } from "./byOrganisation";
|
||||
export { create } from "./create";
|
||||
Reference in New Issue
Block a user