diff --git a/src/lib/queryClient.ts b/src/lib/queryClient.ts index 34a742a4..25c0b99e 100644 --- a/src/lib/queryClient.ts +++ b/src/lib/queryClient.ts @@ -1,26 +1,32 @@ import { QueryClient, QueryFunction } from "@tanstack/react-query"; -const API_BASE_URL = - ((import.meta as any).env?.VITE_API_BASE_URL as string | undefined) || - ((typeof window !== "undefined" && - (window.location.hostname === "localhost" || - window.location.hostname === "127.0.0.1")) as any - ? "http://localhost:8000/api" - : undefined); -const NORMALIZED_API_BASE_URL = API_BASE_URL - ? API_BASE_URL.replace(/\/+$/, "") - : ""; +const ENV_API_BASE_URL = ((import.meta as any).env?.VITE_API_BASE_URL as string | undefined)?.trim(); +const IS_LOCALHOST = + typeof window !== "undefined" && + (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1"); +const API_BASE_URL = (ENV_API_BASE_URL || (IS_LOCALHOST ? "http://localhost:8000" : "https://api.quantfortune.com")) + .replace(/\/+$/, "") + .replace(/\/api$/, ""); const REQUEST_TIMEOUT_MS = 12000; +function normalizeApiPath(url: string) { + const parsed = new URL(url, "https://frontend.local"); + const pathname = parsed.pathname.startsWith("/api") + ? parsed.pathname + : `/api${parsed.pathname.startsWith("/") ? parsed.pathname : `/${parsed.pathname}`}`; + + return `${pathname}${parsed.search}${parsed.hash}`; +} + export function resolveApiUrl(url: string) { if (url.startsWith("http://") || url.startsWith("https://")) { return url; } - if (!NORMALIZED_API_BASE_URL) { - return url; + const normalizedPath = normalizeApiPath(url); + if (!API_BASE_URL) { + return normalizedPath; } - const normalizedPath = url.startsWith("/") ? url : `/${url}`; - return `${NORMALIZED_API_BASE_URL}${normalizedPath}`; + return `${API_BASE_URL}${normalizedPath}`; } async function throwIfResNotOk(res: Response) { diff --git a/src/pages/admin/AdminPage.tsx b/src/pages/admin/AdminPage.tsx index 8d8159be..f560d63f 100644 --- a/src/pages/admin/AdminPage.tsx +++ b/src/pages/admin/AdminPage.tsx @@ -3,7 +3,7 @@ import { Route, Switch, useRoute } from "wouter"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; -import { apiRequest } from "@/lib/queryClient"; +import { apiRequest, resolveApiUrl } from "@/lib/queryClient"; import AdminLayout from "./AdminLayout"; import AdminOverview from "./AdminOverview"; import AdminUsers from "./AdminUsers"; @@ -31,7 +31,7 @@ export default function AdminPage() { const checkAccess = useCallback(async () => { try { - const res = await fetch("admin/overview", { credentials: "include" }); + const res = await fetch(resolveApiUrl("admin/overview"), { credentials: "include" }); if (res.status === 401) { setAccessState("unauthenticated"); return;