From 432ce23431a77d76c48ca9abe464efeb4f0885bb Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 18 May 2020 21:45:01 +0200 Subject: Support relative links for remote endpoints --- .../src/jsMain/kotlin/pl/treksoft/kvision/remote/CallAgent.kt | 6 +++--- .../jsMain/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft') diff --git a/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/CallAgent.kt b/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/CallAgent.kt index 6c1e629c..3d865f04 100644 --- a/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/CallAgent.kt +++ b/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/CallAgent.kt @@ -42,7 +42,7 @@ const val HTTP_UNAUTHORIZED = 401 open class CallAgent { private val kvUrlPrefix = window["kv_remote_url_prefix"] - private val urlPrefix: String = if (kvUrlPrefix != undefined) kvUrlPrefix else "" + private val urlPrefix: String = if (kvUrlPrefix != undefined) "$kvUrlPrefix/" else "" private var counter = 1 /** @@ -66,7 +66,7 @@ open class CallAgent { JSON.plain.stringify(jsonRpcRequest) } return Promise { resolve, reject -> - jQuery.ajax(urlPrefix + url, obj { + jQuery.ajax(urlPrefix + url.drop(1), obj { this.contentType = "application/json" this.data = jsonData this.method = method.name @@ -125,7 +125,7 @@ open class CallAgent { beforeSend: ((JQueryXHR, JQueryAjaxSettings) -> Boolean)? = null ): Promise { return Promise { resolve, reject -> - jQuery.ajax(urlPrefix + url, obj { + jQuery.ajax(urlPrefix + url.drop(1), obj { this.contentType = if (contentType != "multipart/form-data") contentType else false this.data = data this.method = method.name diff --git a/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt b/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt index 24cea176..8836c7f1 100644 --- a/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt +++ b/kvision-modules/kvision-common-remote/src/jsMain/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt @@ -33,6 +33,8 @@ import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.builtins.list import kotlinx.serialization.serializer import kotlinx.serialization.stringify +import org.w3c.dom.get +import kotlin.browser.window import kotlin.reflect.KClass /** @@ -456,6 +458,8 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : noinline function: suspend T.(ReceiveChannel, SendChannel) -> Unit, noinline handler: suspend (SendChannel, ReceiveChannel) -> Unit ) { + val kvUrlPrefix = window["kv_remote_url_prefix"] + val urlPrefix: String = if (kvUrlPrefix != undefined) "$kvUrlPrefix/" else "" val (url, _) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") @@ -464,7 +468,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val responseChannel = Channel() try { coroutineScope { - socket.connect(getWebSocketUrl(url)) + socket.connect(getWebSocketUrl(urlPrefix + url.drop(1))) lateinit var responseJob: Job lateinit var handlerJob: Job val requestJob = launch { @@ -532,6 +536,8 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : noinline function: suspend T.(ReceiveChannel, SendChannel>) -> Unit, noinline handler: suspend (SendChannel, ReceiveChannel>) -> Unit ) { + val kvUrlPrefix = window["kv_remote_url_prefix"] + val urlPrefix: String = if (kvUrlPrefix != undefined) "$kvUrlPrefix/" else "" val (url, _) = serviceManager.getCalls()[function.toString().replace("\\s".toRegex(), "")] ?: throw IllegalStateException("Function not specified!") @@ -540,7 +546,7 @@ open class KVRemoteAgent(val serviceManager: KVServiceMgr) : val responseChannel = Channel>() try { coroutineScope { - socket.connect(getWebSocketUrl(url)) + socket.connect(getWebSocketUrl(urlPrefix + url.drop(1))) lateinit var responseJob: Job lateinit var handlerJob: Job val requestJob = launch { -- cgit