mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
const DEFAULT_REMOTE_HOSTS: Record<string, string> = {
|
|
tech: 'https://tech.worldmonitor.app',
|
|
full: 'https://worldmonitor.app',
|
|
world: 'https://worldmonitor.app',
|
|
};
|
|
|
|
const DEFAULT_LOCAL_API_BASE = 'http://127.0.0.1:46123';
|
|
|
|
function normalizeBaseUrl(baseUrl: string): string {
|
|
return baseUrl.replace(/\/$/, '');
|
|
}
|
|
|
|
export function isDesktopRuntime(): boolean {
|
|
if (typeof window === 'undefined') {
|
|
return false;
|
|
}
|
|
|
|
return '__TAURI_INTERNALS__' in window || '__TAURI__' in window;
|
|
}
|
|
|
|
export function getApiBaseUrl(): string {
|
|
if (!isDesktopRuntime()) {
|
|
return '';
|
|
}
|
|
|
|
const configuredBaseUrl = import.meta.env.VITE_TAURI_API_BASE_URL;
|
|
if (configuredBaseUrl) {
|
|
return normalizeBaseUrl(configuredBaseUrl);
|
|
}
|
|
|
|
return DEFAULT_LOCAL_API_BASE;
|
|
}
|
|
|
|
export function getRemoteApiBaseUrl(): string {
|
|
const configuredRemoteBase = import.meta.env.VITE_TAURI_REMOTE_API_BASE_URL;
|
|
if (configuredRemoteBase) {
|
|
return normalizeBaseUrl(configuredRemoteBase);
|
|
}
|
|
|
|
const variant = import.meta.env.VITE_VARIANT || 'world';
|
|
return DEFAULT_REMOTE_HOSTS[variant] ?? DEFAULT_REMOTE_HOSTS.world ?? 'https://worldmonitor.app';
|
|
}
|
|
|
|
export function toRuntimeUrl(path: string): string {
|
|
if (!path.startsWith('/')) {
|
|
return path;
|
|
}
|
|
|
|
const baseUrl = getApiBaseUrl();
|
|
if (!baseUrl) {
|
|
return path;
|
|
}
|
|
|
|
return `${baseUrl}${path}`;
|
|
}
|
|
|
|
export function installRuntimeFetchPatch(): void {
|
|
if (!isDesktopRuntime() || typeof window === 'undefined' || (window as unknown as Record<string, unknown>).__wmFetchPatched) {
|
|
return;
|
|
}
|
|
|
|
const nativeFetch = window.fetch.bind(window);
|
|
const localBase = getApiBaseUrl();
|
|
const remoteBase = getRemoteApiBaseUrl();
|
|
|
|
window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
|
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
|
|
if (!url.startsWith('/api/')) {
|
|
return nativeFetch(input, init);
|
|
}
|
|
|
|
try {
|
|
return await nativeFetch(`${localBase}${url}`, init);
|
|
} catch (error) {
|
|
console.warn(`[runtime] Local API fetch failed for ${url}, falling back to cloud`, error);
|
|
return nativeFetch(`${remoteBase}${url}`, init);
|
|
}
|
|
};
|
|
|
|
(window as unknown as Record<string, unknown>).__wmFetchPatched = true;
|
|
}
|