Files
rheinsw-mono-repo/internal_frontend/lib/api/serverCall.ts
Thatsaphorn Atchariyaphap 0724f3b1e7 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.
2025-07-11 20:21:45 +02:00

28 lines
813 B
TypeScript

// lib/callBackendApi.ts
import {getServerSession} from "next-auth";
import {authOptions} from "@/lib/api/auth/authOptions";
export async function serverCall(
path: string,
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH",
body?: unknown
): Promise<Response> {
const url = `${process.env.INTERNAL_BACKEND_URL ?? "http://localhost:8080"}${path}`;
const session = await getServerSession(authOptions);
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (session != null) {
headers["Authorization"] = `Bearer ${session.accessToken}`;
}
console.log("[api] Calling backend API: ", method, url, body);
return fetch(url, {
method,
headers,
body: body ? JSON.stringify(body) : undefined,
});
}