Add customer management
This commit is contained in:
28
internal_frontend/lib/api/callApi.ts
Normal file
28
internal_frontend/lib/api/callApi.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
// lib/api/callApi.ts
|
||||
import {serverCall} from "@/lib/api/serverCall";
|
||||
|
||||
export async function callApi<TResponse, TRequest = unknown>(
|
||||
path: string,
|
||||
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH",
|
||||
body?: TRequest
|
||||
): Promise<TResponse> {
|
||||
const res = await serverCall(path, method, body);
|
||||
|
||||
const contentType = res.headers.get("content-type") ?? "";
|
||||
const isJson = contentType.includes("application/json");
|
||||
|
||||
const rawBody = isJson ? await res.json() : await res.text();
|
||||
|
||||
console.log(`[api ${path}] Response:`, res.status, rawBody);
|
||||
|
||||
if (!res.ok) {
|
||||
const errorMessage = isJson
|
||||
? (rawBody?.message ?? rawBody?.errors?.join(", ")) ?? "Unbekannter Fehler"
|
||||
: String(rawBody);
|
||||
|
||||
console.error(`[api ${path}] Error:`, errorMessage);
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
return rawBody as TResponse;
|
||||
}
|
||||
Reference in New Issue
Block a user