I'd like to write Kotlin code server-side, with AP...
# announcements
c
I'd like to write Kotlin code server-side, with APIs that implement an interface, and auto-generate JS (or TS, even better) clients for my interfaces. The idea is, if I needed data from the backend, I'd add a method to the interface, regenerate the clients, and now it's available for use. I'm not 100% sure how to go about this though. One option I thought would be to use OpenAPI and maybe get Gradle to autogenerate clients. Is there a better way to do this?
h
Don't know whats better, but i would try to use kotlinpoet and gradle to generate that into a common source set. And than compile kotlin to jvm and js :)
Because it is a rather simple approach
p
gRPC
and
GraphQL
both provide mechanisms for specifying both your contract (the interface) and for generating the client code.
c
oh wow. kotlinpoet is cool, this would do it too
I've only ever used REST APIs so would prefer to stick with them but I have heard a lot about graphql. Will read up on it
t
I hacked together a simple "Endpoint" class that defines the path/params/body/etc
then just have JS/JVM API clients that consume that definition
and use the definition to also define the endpoints in Spring from the class (using functional router), so endpoints are defined in one spot and fully type-safe server -> backend without code generation, and kotlinx.serialization doing the encording
obviously this requires using KotlinJS, so it'd probably be too heavy unless you were using KotlinJS anyway
m
OpenAPI is a great tool, but in this case you’ll have to write the templates for your client code, since its generators don’t support Kotlin frameworks last time I checked. You could use Ktor for both JS/JVM.
c
Ktor seems nice but I would still have to manually write my client right?
m
Yes sort of, but you could create a template for OA that generates the Ktor client code needed, so you would achieve what you wanted. Alternatively you may want to try something similar to Feign, which is a Java lib that dynamically generates rest clients from an interface. I don’t know if there exists something kotlin-specific, but that would save you time avoiding manual coding. Last but not least you could try generating JS/TS clients directly from your OA specification: • https://openapi-generator.tech/docs/generatorshttps://openapi.tools/#sdk
👍 1