Files
MikrotikManager/lib/backend-url.ts
T
Denozordec 883842636b
Docker images / prepare-release (push) Successful in 6s
Docker images / backend-image (push) Successful in 1m24s
Docker images / frontend-image (push) Successful in 2m6s
Docker images / notify-webhook (push) Skipped
Docker images / updater-image (push) Successful in 43s
Docker images / publish-release (push) Successful in 7s
refactor(api): streamline API requests with requestJson and requestBlob functions
Replaced direct fetch calls with requestJson and requestBlob utility functions across multiple components for improved consistency and error handling. This change enhances the maintainability of the codebase by centralizing API request logic and ensuring uniform handling of authentication and response parsing.
2026-09-05 01:42:44 +07:00

52 lines
1.8 KiB
TypeScript

export const LOCAL_DEFAULT_BACKEND_URL = "http://localhost:8000"
export type ConfiguredBackendUrl =
| { kind: "local" }
| { kind: "fixed"; url: string }
| { kind: "same-origin" }
export function configuredBackendUrl(): ConfiguredBackendUrl {
const raw = process.env.NEXT_PUBLIC_BACKEND_URL
if (raw === undefined || raw === "local") return { kind: "local" }
if (raw === "" || raw === "same-origin") return { kind: "same-origin" }
return { kind: "fixed", url: raw.replace(/\/$/, "") }
}
export function defaultDataSourceMode(): "mock" | "live" {
if (!isMockDataSourceAvailable()) return "live"
return process.env.NEXT_PUBLIC_DEFAULT_DATA_SOURCE === "live" ? "live" : "mock"
}
export function isMockDataSourceAvailable(): boolean {
return process.env.NEXT_PUBLIC_ALLOW_MOCK_DATA !== "false"
}
export function isBackendUrlLocked(): boolean {
const cfg = configuredBackendUrl()
return cfg.kind === "same-origin" || cfg.kind === "fixed"
}
function isLoopbackHost(hostname: string): boolean {
return hostname === "localhost" || hostname === "127.0.0.1"
}
/** Prefer same-origin when the UI is not on loopback — never point the browser at localhost. */
export function resolveStoredBackendUrl(stored: string | null): string {
const cfg = configuredBackendUrl()
if (cfg.kind === "fixed") return cfg.url
if (cfg.kind === "same-origin") {
if (typeof window !== "undefined") return window.location.origin
return ""
}
if (typeof window !== "undefined" && !isLoopbackHost(window.location.hostname)) {
return window.location.origin
}
const trimmed = stored?.trim().replace(/\/$/, "")
if (trimmed && /^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/i.test(trimmed)) {
if (typeof window !== "undefined" && !isLoopbackHost(window.location.hostname)) {
return window.location.origin
}
}
return trimmed || LOCAL_DEFAULT_BACKEND_URL
}