new changes
This commit is contained in:
parent
c70e3e2446
commit
6a653b55df
@ -1,26 +1,32 @@
|
|||||||
import { QueryClient, QueryFunction } from "@tanstack/react-query";
|
import { QueryClient, QueryFunction } from "@tanstack/react-query";
|
||||||
|
|
||||||
const API_BASE_URL =
|
const ENV_API_BASE_URL = ((import.meta as any).env?.VITE_API_BASE_URL as string | undefined)?.trim();
|
||||||
((import.meta as any).env?.VITE_API_BASE_URL as string | undefined) ||
|
const IS_LOCALHOST =
|
||||||
((typeof window !== "undefined" &&
|
typeof window !== "undefined" &&
|
||||||
(window.location.hostname === "localhost" ||
|
(window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1");
|
||||||
window.location.hostname === "127.0.0.1")) as any
|
const API_BASE_URL = (ENV_API_BASE_URL || (IS_LOCALHOST ? "http://localhost:8000" : "https://api.quantfortune.com"))
|
||||||
? "http://localhost:8000/api"
|
.replace(/\/+$/, "")
|
||||||
: undefined);
|
.replace(/\/api$/, "");
|
||||||
const NORMALIZED_API_BASE_URL = API_BASE_URL
|
|
||||||
? API_BASE_URL.replace(/\/+$/, "")
|
|
||||||
: "";
|
|
||||||
const REQUEST_TIMEOUT_MS = 12000;
|
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) {
|
export function resolveApiUrl(url: string) {
|
||||||
if (url.startsWith("http://") || url.startsWith("https://")) {
|
if (url.startsWith("http://") || url.startsWith("https://")) {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
if (!NORMALIZED_API_BASE_URL) {
|
const normalizedPath = normalizeApiPath(url);
|
||||||
return url;
|
if (!API_BASE_URL) {
|
||||||
|
return normalizedPath;
|
||||||
}
|
}
|
||||||
const normalizedPath = url.startsWith("/") ? url : `/${url}`;
|
return `${API_BASE_URL}${normalizedPath}`;
|
||||||
return `${NORMALIZED_API_BASE_URL}${normalizedPath}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function throwIfResNotOk(res: Response) {
|
async function throwIfResNotOk(res: Response) {
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { Route, Switch, useRoute } from "wouter";
|
|||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
import { apiRequest } from "@/lib/queryClient";
|
import { apiRequest, resolveApiUrl } from "@/lib/queryClient";
|
||||||
import AdminLayout from "./AdminLayout";
|
import AdminLayout from "./AdminLayout";
|
||||||
import AdminOverview from "./AdminOverview";
|
import AdminOverview from "./AdminOverview";
|
||||||
import AdminUsers from "./AdminUsers";
|
import AdminUsers from "./AdminUsers";
|
||||||
@ -31,7 +31,7 @@ export default function AdminPage() {
|
|||||||
|
|
||||||
const checkAccess = useCallback(async () => {
|
const checkAccess = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch("admin/overview", { credentials: "include" });
|
const res = await fetch(resolveApiUrl("admin/overview"), { credentials: "include" });
|
||||||
if (res.status === 401) {
|
if (res.status === 401) {
|
||||||
setAccessState("unauthenticated");
|
setAccessState("unauthenticated");
|
||||||
return;
|
return;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user