Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example snippet code to post the content of a form to a rest api which then returns some json response using kotlin compiled (=transpiled) to javascript and the fetch api
// This is an example snippet code to post the content of a form to a rest api which
// then returns some json response using kotlin compiled (=transpiled) to javascript
//
// <form method="POST" action="<url to rest api which returns json>">
// ...fields...
// <input type="button" id="my-submit-button" value="Click Me!">
// </form>
import org.w3c.dom.HTMLInputElement
import org.w3c.xhr.FormData
import org.w3c.fetch.RequestInit
import kotlin.browser.*
fun submitForm(submitElt: HTMLInputElement, handler: (dynamic) -> Unit) {
submitElt.form?.let { form ->
window.fetch(form.action,
RequestInit(method = form.method,
body = FormData(form)))
.then { response ->
if (response.status == 200.toShort()) {
response.json()
} else
println("there was an error...${response.status}")
}
.then { json: dynamic ->
handler(json)
}
}
}
fun main(args: Array<String>) {
val elt = document.getElementById("my-submit-button") as HTMLInputElement
submitForm(elt) { json -> // here json is "dynamic" => access like in plain js
println("success from request => ${json.uri}")
}
}
/*
This is the transpiled result...
kotlin.kotlin.io.output.flush();
if (typeof kotlin === 'undefined') {
throw new Error("Error loading module 'moduleId'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'moduleId'.");
}
var moduleId = function (_, Kotlin) {
'use strict';
var toShort = Kotlin.toShort;
var println = Kotlin.kotlin.io.println_s8jyv4$;
var Unit = Kotlin.kotlin.Unit;
var throwCCE = Kotlin.throwCCE;
function submitForm$lambda$lambda(response) {
if (response.status === toShort(200)) {
return response.json();
}
else
return println('there was an error...' + response.status), Unit;
}
function submitForm$lambda$lambda_0(closure$handler) {
return function (json) {
closure$handler(json);
return Unit;
};
}
function submitForm(submitElt, handler) {
var tmp$;
if ((tmp$ = submitElt.form) != null) {
var tmp$_0 = window;
var tmp$_1 = tmp$.action;
var method = tmp$.method;
var body = new FormData(tmp$);
var headers;
var referrer;
var referrerPolicy;
var mode;
var credentials;
var cache;
var redirect;
var integrity;
var keepalive;
var window_0;
headers = undefined;
referrer = undefined;
referrerPolicy = undefined;
mode = undefined;
credentials = undefined;
cache = undefined;
redirect = undefined;
integrity = undefined;
keepalive = undefined;
window_0 = undefined;
var o = {};
o['method'] = method;
o['headers'] = headers;
o['body'] = body;
o['referrer'] = referrer;
o['referrerPolicy'] = referrerPolicy;
o['mode'] = mode;
o['credentials'] = credentials;
o['cache'] = cache;
o['redirect'] = redirect;
o['integrity'] = integrity;
o['keepalive'] = keepalive;
o['window'] = window_0;
tmp$_0.fetch(tmp$_1, o).then(submitForm$lambda$lambda).then(submitForm$lambda$lambda_0(handler));
}
}
function main$lambda(json) {
println('success from request => ' + json.uri.toString());
return Unit;
}
function main(args) {
var tmp$;
var elt = Kotlin.isType(tmp$ = document.getElementById('my-submit-button'), HTMLInputElement) ? tmp$ : throwCCE();
submitForm(elt, main$lambda);
}
_.submitForm_9lm2sa$ = submitForm;
_.main_kand9s$ = main;
main([]);
Kotlin.defineModule('moduleId', _);
return _;
}(typeof moduleId === 'undefined' ? {} : moduleId, kotlin);
kotlin.kotlin.io.output.buffer;
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment