Does anyone here use http4k with KMP app with Ktor...
# http4k
r
Does anyone here use http4k with KMP app with Ktor client and share Kotlin code directly (via a shared module) as opposed to using OpenAPI to generate client types? Are there any guides or examples on best practices for this? More details inside:
The main thing I was wondering was keeping client and server routes in sync. My initial idea was to just define the routes in a shared module. But I imagine that would mean losing lenses since we need to support non-JVM targets. But furthermore, even if we did, I feel like we would be complicate extracting parameters. Like in the example below:
Copy code
val routes = listOf(
    "/merchants" / merchantIdLens / "items" bindContract Method.GET to
            { id, _ ->
                { _: Request ->
                    Response(OK)
                }
            }
)
Say that we instead defined the routes in the shared module instead of in the server's. I admit I haven't tested it, but even if it did work, it would still complicate the developer experience since we'd have to go to another file to check what the route we're extracting is.
Different question, but I imagine it might be semi-related the solution. Are there any 'best practices' way to avoid strings when declaring routes (such as "/merchants")? In my latest projects, I decided to rename routes twice — and relying on regex/manual renaming is less safe than using the IDE's built in renaming for variables. I did consider just making them variables anyways, such as
val merchant = "/merchants"
. However, that makes telling what is a static or dynamic segment difficult. To make it somewhat differentiable, I did consider just wrapping the variable names with backticks, such as `val `merchants``. However, the Intellj does not seem to like this workaround. It warns that the backticks are redundant and are simply ignored by the compiler. And I'm rather iffy of a trick that causes IDE warnings. Furthermore, it means losing the IDE color coding where static segments (strings) and dynamic segments (variables) are colored differently.
The reason why I am so desperate to share code directly as opposed to using OpenAPI code gen is twofold: 1. I like using sealed interfaces to model results. There is no official OpenAPI-support for this. There are third-party implementations for this, but that brings me to my next point. 2. I use a custom DTO tool that I built. Relying on OpenAPI codegen would mean losing a lot of the ergonomics features my library sought to provide. I imagine OpenAPI might still come useful for documentation, or if I ever have a non-Kotlin client (say React), but for now everything would be seamless if I could just use shared code.
Currently I am just manually duplicating the API routes on the client-side. But that means two sources of truth that must be kept in sync. But also that I don't have much prior experience with building a full-stack project, which is why I was wondering if there's any best practices guide.
s
http4k doesn’t support multi platform because it relies on JVM-specific things that aren’t available across platforms (input stream, datetime, url encoder etc)