|
1
|
/** |
|
2
|
* Global Configuration for JCRGuru |
|
3
|
*/ |
|
4
|
|
|
5
|
const STORAGE_KEY = "jcrguru_api_base_url"; |
|
6
|
|
|
7
|
// Default to relative /api if not specified, |
|
8
|
// but in hybrid mode we might need to point to a specific IP or domain. |
|
9
|
const DEFAULT_API_URL = window.location.origin.includes("androidplatform.net") |
|
10
|
? "https://jcr-guide-pro.replit.app" // Fallback to production if in hybrid local shell |
|
11
|
: ""; |
|
12
|
|
|
13
|
export const getApiBaseUrl = (): string => { |
|
14
|
const custom = localStorage.getItem(STORAGE_KEY); |
|
15
|
if (custom) return custom; |
|
16
|
if (typeof window !== "undefined" && window.location?.origin) { |
|
17
|
if (window.location.origin.includes("androidplatform.net")) { |
|
18
|
return "https://jcr-guide-pro.replit.app"; |
|
19
|
} |
|
20
|
return window.location.origin; |
|
21
|
} |
|
22
|
return DEFAULT_API_URL; |
|
23
|
}; |
|
24
|
|
|
25
|
export const getRawApiOverride = (): string => { |
|
26
|
return localStorage.getItem(STORAGE_KEY) || ""; |
|
27
|
}; |
|
28
|
|
|
29
|
export const setApiBaseUrl = (url: string) => { |
|
30
|
if (!url || url.trim() === "" || (typeof window !== "undefined" && url.trim() === window.location.origin)) { |
|
31
|
localStorage.removeItem(STORAGE_KEY); |
|
32
|
} else { |
|
33
|
// Ensure no trailing slash |
|
34
|
const formattedUrl = url.trim().replace(/\/+$/, ""); |
|
35
|
localStorage.setItem(STORAGE_KEY, formattedUrl); |
|
36
|
} |
|
37
|
}; |
|
38
|
|
|
39
|
/** |
|
40
|
* Helper to construct API URLs |
|
41
|
*/ |
|
42
|
export const apiurl = (path: string): string => { |
|
43
|
const base = getApiBaseUrl().replace(/\/+$/, ""); |
|
44
|
// Ensure path starts with single / |
|
45
|
const cleanPath = path.startsWith("/") ? path.replace(/^\/+/, "/") : `/${path}`; |
|
46
|
|
|
47
|
// If base is identical to current origin or empty, just return clean relative path |
|
48
|
if (typeof window !== "undefined" && (base === window.location.origin || !base)) { |
|
49
|
return cleanPath; |
|
50
|
} |
|
51
|
return `${base}${cleanPath}`; |
|
52
|
}; |