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,7 +1,3 @@
|
||||
"use server";
|
||||
|
||||
import {callApi} from "@/lib/api/callApi";
|
||||
import {customerRoutes} from "@/app/api/customers/customerRoutes";
|
||||
import {Customer} from "@/services/customers/entities/customer";
|
||||
|
||||
export async function validateCustomer(input: {
|
||||
@@ -15,5 +11,27 @@ export async function validateCustomer(input: {
|
||||
return [];
|
||||
}
|
||||
|
||||
return await callApi<Customer[]>(customerRoutes.validate, "POST", input);
|
||||
}
|
||||
const res = await fetch('/api/customers/validate', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
|
||||
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 /api/customers/validate] Response:`, res.status, rawBody);
|
||||
|
||||
if (!res.ok) {
|
||||
const errorMessage = isJson
|
||||
? (rawBody?.message ?? rawBody?.errors?.join(", ")) ?? "Unbekannter Fehler"
|
||||
: String(rawBody);
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
return rawBody as Customer[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user