رفع باگ: تصحیح مسیردهی ارتباط کلاینت با سرویس و حل خطای دسترسی شبکه به همراه افزودن آیکون سامانه

This commit is contained in:
mjalmoudy94
2026-10-13 08:05:58 +00:00
parent 0b72c4ec6d
commit 5a64ebf560
8 changed files with 55 additions and 7 deletions
+26 -3
View File
@@ -8,8 +8,30 @@
import type { ApiErrorBody } from "@agy/contracts";
export const API_BASE_URL =
process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:8000";
/**
* Resolves the API base URL.
* In a browser:
* - If NEXT_PUBLIC_API_URL is configured and not pointing to localhost on a remote host, use it.
* - Otherwise default to relative path "" so requests go directly to the current origin (e.g. /api/*)
* behind reverse proxies (Caddy), preventing ERR_CONNECTION_REFUSED on clients.
* In SSR / Node / tests:
* - Fall back to NEXT_PUBLIC_API_URL or "http://localhost:8000".
*/
export function getApiBaseUrl(): string {
const configured = process.env.NEXT_PUBLIC_API_URL?.trim();
if (typeof window !== "undefined") {
const isLocalhost =
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1";
if (!configured || (!isLocalhost && configured.includes("localhost"))) {
return "";
}
return configured;
}
return configured || "http://localhost:8000";
}
export const API_BASE_URL = getApiBaseUrl();
export class ApiError extends Error {
readonly status: number;
@@ -73,9 +95,10 @@ export async function apiRequest<T>(path: string, options: RequestOptions = {}):
const authToken = token ?? getStoredToken();
if (authToken) headers.Authorization = `Bearer ${authToken}`;
const baseUrl = getApiBaseUrl();
let response: Response;
try {
response = await fetch(`${API_BASE_URL}${path}`, {
response = await fetch(`${baseUrl}${path}`, {
method,
headers,
body: body === undefined ? undefined : JSON.stringify(body),