Files
rheinsw-mono-repo/internal_frontend/lib/api/serverCall.ts
Thatsaphorn Atchariyaphap e42b352216 Refactor navigation structure and API routes
- Centralize user menu, sidebar items, and breadcrumb logic.
- Map consistent API endpoints in `customerRoutes`.
- Replace inline route definitions with reusable constants.
- Refactor auth configuration file location.
- Improve `<Link>` usage to replace static `<a>` elements.
- Adjust sidebar and dropdown components to use dynamic navigation configurations.
2025-07-07 19:49:58 +02:00

28 lines
817 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"}/api${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,
});
}