Initial MUH app implementation
This commit is contained in:
58
frontend/src/lib/api.ts
Normal file
58
frontend/src/lib/api.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
const API_ROOT = import.meta.env.VITE_API_URL ?? "http://localhost:8090/api";
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(text || "Unbekannter API-Fehler");
|
||||
}
|
||||
if (response.status === 204) {
|
||||
return undefined as T;
|
||||
}
|
||||
return (await response.json()) as T;
|
||||
}
|
||||
|
||||
export async function apiGet<T>(path: string): Promise<T> {
|
||||
return handleResponse<T>(await fetch(`${API_ROOT}${path}`));
|
||||
}
|
||||
|
||||
export async function apiPost<T>(path: string, body: unknown): Promise<T> {
|
||||
return handleResponse<T>(
|
||||
await fetch(`${API_ROOT}${path}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function apiPut<T>(path: string, body: unknown): Promise<T> {
|
||||
return handleResponse<T>(
|
||||
await fetch(`${API_ROOT}${path}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function apiPatch<T>(path: string, body: unknown): Promise<T> {
|
||||
return handleResponse<T>(
|
||||
await fetch(`${API_ROOT}${path}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function apiDelete(path: string): Promise<void> {
|
||||
await handleResponse<void>(
|
||||
await fetch(`${API_ROOT}${path}`, {
|
||||
method: "DELETE",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function pdfUrl(sampleId: string): string {
|
||||
return `${API_ROOT}/portal/reports/${sampleId}/pdf`;
|
||||
}
|
||||
Reference in New Issue
Block a user