Vampire
08/14/2020, 10:53 PM@actions/http-client
and want to use HttpClient().post(...
.
The ...d.ts
has
export interface IHeaders {
[key: string]: any;
}
[...]
post(requestUrl: string, data: string, additionalHeaders?: ifm.IHeaders): Promise<ifm.IHttpClientResponse>;
Dukat made from that
external interface IHeaders {
@nativeGetter
operator fun get(key: String): Any?
@nativeSetter
operator fun set(key: String, value: Any)
}
[...]
open fun post(requestUrl: String, data: String, additionalHeaders: IHeaders = definedExternally): Promise<IHttpClientResponse>
My first approach was basically naively following the IDE autocompletion which of course does not work (no additional headers set):
val response = HttpClient().post(
requestUrl = "<http://localhost:9999/api/GetFiles>",
data = "type=ProductId&url=$productId",
additionalHeaders = object : IHeaders {
override fun get(key: String): Any? = when (key) {
"Content-Type" -> "application/x-www-form-urlencoded"
else -> null
}
override fun set(key: String, value: Any) = error("headers are read-only")
}
).await()
My next try was to do what my guts suggested and made the compiler happy:
val response = HttpClient().post(
requestUrl = "<http://localhost:9999/api/GetFiles>",
data = "type=ProductId&url=$productId",
additionalHeaders = mapOf("Content-Type" to "application/x-www-form-urlencoded") as IHeaders
).await()
IJ complains about "Unchecked cast to external interface: Map<String, String> to IHeader" and it also does not work, but sends the additional headers
_keys_up5z3z$_0: null
_values_6nw1f1$_0: null
_keys_qe2m0n$_0: null
_values_kxdlqh$_0: null
internalmap_uxhen5$_0: [object Object]
equality_vgh6cm$_0: [object Object]
_entries_7ih87x$_0: null
araqnid
08/14/2020, 11:26 PMjs {
this["Content-Type"] = "text/plain"
}.unsafeCast<IHeaders>()
This is basically just using js("{}")
to source a raw empty object and dynamic
to assign it arbitrary properties: https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-extensions/src/main/kotlin/kotlinext/js/Helpers.ktaraqnid
08/14/2020, 11:27 PMVampire
08/14/2020, 11:29 PMval response = HttpClient().post(
requestUrl = "<http://localhost:9999/api/GetFiles>",
data = "type=ProductId&url=$productId",
additionalHeaders = js("{ 'Content-Type': 'application/x-www-form-urlencoded' }") as IHeaders
).await()
But that didn't work either, because the compiler removed the single quotes around Content-Type
and then of course JS complains about the dash:
But that actually sounds like a Kotlin compiler bugaraqnid
08/14/2020, 11:30 PMjsObject<IHeaders> { this["Content-Type"] = "text/plain" }
I think it’s defining get/set property (from the Kotlin point of view) to just be passed through to the underlying JSVampire
08/14/2020, 11:31 PMaraqnid
08/14/2020, 11:33 PMfun Map<String,String>.toHeaders(): IHeaders
would make senseVampire
08/14/2020, 11:37 PMkotlin-extensions
, only pre-releases 😞Vampire
08/15/2020, 12:08 AMVampire
08/15/2020, 12:08 AMfun Map<String, Any>.asIHeaders() = jsObject<IHeaders> {
for ((name, value) in this@asIHeaders) {
this[name] = value
}
}
Vampire
08/15/2020, 12:08 AMval response = HttpClient().post(
requestUrl = "<http://localhost:9999/api/GetFiles>",
data = "type=ProductId&url=$productId",
additionalHeaders = mapOf("Content-Type" to "application/x-www-form-urlencoded").asIHeaders()
).await()
Vampire
08/15/2020, 12:10 AMGunslingor
08/22/2020, 7:31 PMpluginsOpts = jsObject<dynamic> {
this["grapesjs-lory-slider"] = jsObject<dynamic> {
sliderBlock = jsObject<dynamic> {
category = "Extra"
}
}
}