File size: 669 Bytes
d585643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
export function getApiUrl(path: string): string {
  const envUrl = process.env.NEXT_PUBLIC_API_URL || "";
  const cleanBase = envUrl.trim().replace(/\/+$/, "");
  const cleanPath = path.startsWith("/") ? path : `/${path}`;

  if (cleanBase) {
    // Direct cross-origin request to backend (bypasses Next.js proxy body mangling)
    const apiPrefix = cleanPath.startsWith("/api") ? cleanPath : `/api${cleanPath}`;
    return `${cleanBase}${apiPrefix}`;
  }

  // Fallback to relative path using Next.js rewrite
  const backendPrefix = cleanPath.startsWith("/backend-api")
    ? cleanPath
    : `/backend-api${cleanPath.replace(/^\/api/, "")}`;
  return backendPrefix;
}