Something went wrong. Try again.
Share your Ktor endpoints between client and server spine.opensavvy.dev
rest http ktor kotlin arrow
Something went wrong. Try again.
spine client
1 folder · 2 files
README.md
Module Client-side typesafe Spine schema usage #
Call a Ktor API described with type-safety in common code.
Spine allows us to declare Ktor endpoints in code shared between the client and server.
Calling a fullstack endpoint #
First, create a module that will contain the endpoint definition (see the api module), with the following API:
object Api : RootResource("v1") {
object Users : StaticResource<Api>("users", Api) {
val create by post()
.request<CreateUserRequest>()
.response<User>()
}
}
Create a new module, with a dependency on dev.opensavvy.spine:client. Instantiate a Ktor client by following our tutorial. You can now call the endpoint like this:
val user = client.request(Api / Users / Users.create, CreateUserRequest(name = "John"))
.bodyOrThrow()
Learn more #
- [
request][opensavvy.spine.client.request] calls an endpoint declared in shared code. - [
SpineResponse][opensavvy.spine.client.SpineResponse] encapsulates multiple strategies for facing failures.