diff --git a/client/README.md b/client/README.md index 2e26451..458449b 100644 --- a/client/README.md +++ b/client/README.md @@ -6,4 +6,30 @@ Call a Ktor API described with type-safety in common code. -To declare a Ktor API in common code, see the `api` module. +Spine allows us to [declare Ktor endpoints in code shared between the client and server](https://spine.opensavvy.dev/setup.html). + +## Calling a fullstack endpoint + +First, create a module that will contain the endpoint definition (see the `api` module), with the following API: + +```kotlin +object Api : RootResource("v1") { + object Users : StaticResource("users", Api) { + val create by post() + .request() + .response() + } +} +``` + +Create a new module, with a dependency on `dev.opensavvy.spine:client`. Instantiate a Ktor client by following [our tutorial](https://spine.opensavvy.dev/setup.html#client-side-implementation). You can now call the endpoint like this: + +```kotlin +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.