I’ve recently learned about type-safe routing in K...
# ktor
h
I’ve recently learned about type-safe routing in Ktor and was hoping I could leverage it in a client library. Our front-end application will be written in React, but having a Ktor client based library that we can import for accessing API routes, doing authentication, and exposing API types in typescript would be a real enabler for us. I’ve tried using
@JsExport
on a client class which wraps ktor client, but it seems the
ktor-client-core
library has a dependency on
ws
, which is a node dependency, so fails to resolve in the browser. This seems like an excellent use case for Ktor in multiplatform projects, so I assume I’m just not configuring something correctly. Should this work?
h
How exactly do you plan to use the Resources in Ktor with Typescript? You still need to pass the object to the client functions, like get. Not sure, if it works correctly from Typescript.
h
I was thinking I’d still hide the resources inside my ApiClient, which would wrap the Ktor HttpClient and Resources (like
Carts
below.
Copy code
@JsExport
class ApiClient(
) {
    private val httpClient =
        HttpClient(Js) {
            install(Resources)
            install(ContentNegotiation) {
                json()
            }
                ...
        }

    suspend fun listCarts(): List<Cart> = httpClient.get(Carts()).body()

    suspend fun getCart(id: CartId) = httpClient.get(Carts.Id(id)).body<Cart>()
}
Then my React components/hooks can use the @JsExport-ed API types (e.g.
Cart
above) to interact with my Kotlin wrapper cleanly
h
That should work
thank you color 1