Impossible to provide a `RequestInit` with fetch
In my sample application with Kotlin/Wasm 2.1.21 , window.fetch(url) works as expected, but window.fetch(url, RequestInit(method = "POST")) throws the following error:
sample-wasm-js.uninstantiated.mjs:150 Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Failed to read the 'cache' property from 'RequestInit': The provided value 'null' is not a valid enum value of type RequestCache.
at org.w3c.dom.fetch_$external_fun (sample-wasm-js.uninstantiated.mjs:150:82)
at 04d61904f8aaba159022.wasm:0x35d09
at 04d61904f8aaba159022.wasm:0x34d5c
at HTMLFormElement.<anonymous> (sample-wasm-js.uninstantiated.mjs:142:127)
If I change to window.fetch(url, RequestInit(method = "POST", cache = RequestCache.Companion.DEFAULT)), I get:
sample-wasm-js.uninstantiated.mjs:150 Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Failed to read the 'credentials' property from 'RequestInit': The provided value 'null' is not a valid enum value of type RequestCredentials.
at org.w3c.dom.fetch_$external_fun (sample-wasm-js.uninstantiated.mjs:150:82)
at ba229722ec51e8dd8a68.wasm:0x35d74
at ba229722ec51e8dd8a68.wasm:0x34d9d
at HTMLFormElement.<anonymous> (sample-wasm-js.uninstantiated.mjs:142:127)
org.w3c.dom.fetch_$external_fun @ sample-wasm-js.uninstantiated.mjs:150
$func1542 @ ba229722ec51e8dd8a68.wasm:0x35d74
$__callFunction___Js_->Unit_ @ ba229722ec51e8dd8a68.wasm:0x34d9d
(anonymous) @ sample-wasm-js.uninstantiated.mjs:142
If I change to window.fetch(url, RequestInit(method = "POST", cache = RequestInit(method = "POST", cache = RequestCache.Companion.DEFAULT, credentials = RequestCredentials.Companion.OMIT)), I get:
sample-wasm-js.uninstantiated.mjs:150 Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Failed to read the 'headers' property from 'RequestInit': The provided value is not of type '(record<ByteString, ByteString> or sequence<sequence<ByteString>>)'.
at org.w3c.dom.fetch_$external_fun (sample-wasm-js.uninstantiated.mjs:150:82)
at 99192d8e1d750b4a96d2.wasm:0x35de7
at 99192d8e1d750b4a96d2.wasm:0x34dfc
at HTMLFormElement.<anonymous> (sample-wasm-js.uninstantiated.mjs:142:127)
org.w3c.dom.fetch_$external_fun @ sample-wasm-js.uninstantiated.mjs:150
$func1544 @ 99192d8e1d750b4a96d2.wasm:0x35de7
$__callFunction___Js_->Unit_ @ 99192d8e1d750b4a96d2.wasm:0x34dfc
(anonymous) @ sample-wasm-js.uninstantiated.mjs:142
Please allows to specify RequestInit with just some properties defined.
Hi! Thanks for the report!
Looks like a bug in RequestInit implementation, as workaround you can define own version, something like:
public fun RequestInit(
method: String? = null,
headers: JsAny? /* Headers|JsArray<JsArray<JsString>>|OpenEndedDictionary<JsString> */ = null,
body: JsAny? /* Blob|BufferSource|FormData|URLSearchParams|String */ = null,
referrer: String? = null,
referrerPolicy: JsAny? = null,
mode: RequestMode? = null,
credentials: RequestCredentials? = null,
cache: RequestCache? = null,
redirect: RequestRedirect? = null,
integrity: String? = null,
keepalive: Boolean? = null,
window: JsAny? = null
): RequestInit {
js("""
const obj = {};
if (method) obj.method = method;
if (headers) obj.headers = headers;
if (body) obj.body = body;
if (referrer) obj.referrer = referrer;
if (referrerPolicy) obj.referrerPolicy = referrerPolicy;
if (mode) obj.mode = mode;
if (credentials) obj.credentials = credentials;
if (cache) obj.cache = cache;
if (redirect) obj.redirect = redirect;
if (integrity) obj.integrity = integrity;
if (keepalive) obj.keepalive = keepalive;
if (window) obj.window = window;
return obj;
""")
}