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