Giter Club home page Giter Club logo

Comments (8)

rjaros avatar rjaros commented on August 12, 2024 1

pl.treksoft.kvision.utils.obj is a helper function to create a dynamic JS objects. obj { q = "kvision" } is an equivalent of js("{\"q\": \"kvision\"}") but looks a lot better :)

from kvision.

rjaros avatar rjaros commented on August 12, 2024

You can use kotlinx.serialization library (already a dependency of KVision) for (de)serialization and pl.treksoft.kvision.remote.CallAgent class to make calls to your backend. A working example:

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.asDeferred
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.list
import pl.treksoft.kvision.hmr.ApplicationBase
import pl.treksoft.kvision.remote.CallAgent
import pl.treksoft.kvision.utils.obj

@Serializable
data class Repository(val id: Int, val full_name: String?, val description: String?, val fork: Boolean)

object App : ApplicationBase {
    val callAgent = CallAgent()

    override fun start(state: Map<String, Any>) {
        GlobalScope.launch {
            val result: dynamic = callAgent.remoteCall("https://api.github.com/search/repositories", obj { q = "kvision" }).asDeferred().await()
            val items: List<Repository> = Json.nonstrict.parse(Repository.serializer().list, JSON.stringify(result.items))
            println(items)
        }
    }
}

The remoteCall method returns kotlin.js.Promise object, so you can use it without coroutines if you want. I will think about adding some helper methods to CallAgent class to simplify above code and I'll put similar example in the docs.

from kvision.

checketts avatar checketts commented on August 12, 2024

Thanks for the great example! Would DynamicObjectParser be useful or essentially equivalent for the parsing?

https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/runtime_usage.md#dynamic-object-parser-js-only

from kvision.

rjaros avatar rjaros commented on August 12, 2024

You are right! This is simpler and probably more efficient:

val items: List<Repository> = DynamicObjectParser().parse(result.items, Repository.serializer().list)

from kvision.

rjaros avatar rjaros commented on August 12, 2024

From KVision 0.0.25, it will be enough to write the whole call as:

val items: List<Repository> = callAgent.remoteCall("https://api.github.com/search/repositories", obj { q = "kvision" }, Repository.serializer().list) {
    it.items
}.asDeferred().await()

from kvision.

checketts avatar checketts commented on August 12, 2024

In your 0.0.25 example, what is obj { q = "kvision" }, ?

from kvision.

rjaros avatar rjaros commented on August 12, 2024

Fully type-safe calls with 0.0.25 will be possible as well:

@Serializable
data class Query(val q: String?)

@Serializable
data class SearchResult(val total_count: Int, val incomplete_results: Boolean)

GlobalScope.launch {
    val searchResult: SearchResult = callAgent.call<SearchResult, Query>("https://api.github.com/search/repositories", Query("kvision")).asDeferred().await()
    println(searchResult)
}

from kvision.

rjaros avatar rjaros commented on August 12, 2024

Version 0.0.25 is released. The KVision guide is updated as well.

from kvision.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.