Remove callApi, refactor API integrations, and adjust error handling
- Delete unused `callApi` utility and related imports across components. - Replace `callApi` with direct `fetch` usage in `validateCustomer` and `addCustomer`. - Update `customerRoutes` to include `/api` prefix for consistency. - Refactor `useErrorHandler` to ensure comprehensive state management during errors. - Improve `ErrorBoundary` component text for better clarity in fallback UI. - Align `CustomersPage` logic with `useCallback` for optimized dependency management.
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -7,7 +7,7 @@ export async function serverCall(
|
||||
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH",
|
||||
body?: unknown
|
||||
): Promise<Response> {
|
||||
const url = `${process.env.INTERNAL_BACKEND_URL ?? "http://localhost:8080"}/api${path}`;
|
||||
const url = `${process.env.INTERNAL_BACKEND_URL ?? "http://localhost:8080"}${path}`;
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
|
||||
Reference in New Issue
Block a user