refactor(network): simplify PlatformConfig logic for URL resolution

- Streamlined `resolveApiBaseUrl` and `resolveKeycloakUrl` by introducing `globalScope` as a reusable property.
- Improved readability and maintainability by reducing redundant code and enhancing structure.
- Removed outdated comments and polished behavior for fallback mechanisms.

Signed-off-by: Stefan Mogeritsch <stefan.mo.co@gmail.com>
This commit is contained in:
Stefan Mogeritsch 2026-03-16 10:20:28 +01:00
parent b6fda98c89
commit d1fce33716
2 changed files with 8 additions and 14 deletions

View File

@ -25,7 +25,6 @@ class DeepLinkHandler(
return processDeepLink(parsed)
}
// TODO: Implement deep link processing logic
private fun processDeepLink(deepLink: DeepLink): Boolean {
val route = cleanRoute(deepLink.route)

View File

@ -4,12 +4,14 @@ import kotlinx.browser.window
@Suppress("UnsafeCastFromDynamic", "EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
actual object PlatformConfig {
private val globalScope: dynamic
get() = js("typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : (typeof self !== 'undefined' ? self : {}))")
actual fun resolveApiBaseUrl(): String {
// 1) Prefer a global JS variable (can be injected by index.html or nginx)
val global =
js("typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : (typeof self !== 'undefined' ? self : {}))")
// 1) Prefer a global JS variable (injected by main.kt via AppConfig)
val fromGlobal = try {
(global.API_BASE_URL as? String)?.trim().orEmpty()
(globalScope.API_BASE_URL as? String)?.trim().orEmpty()
} catch (_: dynamic) {
""
}
@ -17,20 +19,17 @@ actual object PlatformConfig {
console.log("[PlatformConfig] Resolved API_BASE_URL from global: $fromGlobal")
return fromGlobal.removeSuffix("/")
}
// 2) Try window location origin (same origin gateway/proxy setup)
val origin = try {
window.location.origin
} catch (_: dynamic) {
null
}
if (!origin.isNullOrBlank()) {
val resolvedUrl = origin.removeSuffix("/") + "/api"
console.log("[PlatformConfig] Resolved API_BASE_URL from window.location.origin: $resolvedUrl")
return resolvedUrl
}
// 3) Fallback to the local gateway directly (e.g., for tests without a window)
val fallbackUrl = "http://localhost:8081/api"
console.log("[PlatformConfig] Fallback API_BASE_URL: $fallbackUrl")
@ -38,11 +37,9 @@ actual object PlatformConfig {
}
actual fun resolveKeycloakUrl(): String {
// 1) Prefer a global JS variable injected by index.html at runtime
val global =
js("typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : (typeof self !== 'undefined' ? self : {}))")
// 1) Prefer a global JS variable (injected by main.kt via AppConfig)
val fromGlobal = try {
(global.KEYCLOAK_URL as? String)?.trim().orEmpty()
(globalScope.KEYCLOAK_URL as? String)?.trim().orEmpty()
} catch (_: dynamic) {
""
}
@ -50,7 +47,6 @@ actual object PlatformConfig {
console.log("[PlatformConfig] Resolved KEYCLOAK_URL from global: $fromGlobal")
return fromGlobal.removeSuffix("/")
}
// 2) Derive from window.location.hostname with default Keycloak port
val hostname = try {
window.location.hostname
@ -62,7 +58,6 @@ actual object PlatformConfig {
console.log("[PlatformConfig] Resolved KEYCLOAK_URL from window.location.hostname: $resolvedUrl")
return resolvedUrl
}
// 3) Fallback for local development
val fallbackUrl = "http://localhost:8180"
console.log("[PlatformConfig] Fallback KEYCLOAK_URL: $fallbackUrl")