diff --git a/server-arrow/README.md b/server-arrow/README.md index 5a49139..035a5aa 100644 --- a/server-arrow/README.md +++ b/server-arrow/README.md @@ -5,3 +5,5 @@ Implement a Ktor API described with type-safety in common code, using Arrow type + +This module introduces the [`routeWithRaise`][opensavvy.spine.server.arrow.routeWithRaise] helper to declare a Ktor endpoint handler which has the possibility to raise typed errors. diff --git a/server-arrow/src/commonMain/kotlin/ServerRaise.kt b/server-arrow/src/commonMain/kotlin/ServerRaise.kt index a9ea105..8d53d18 100644 --- a/server-arrow/src/commonMain/kotlin/ServerRaise.kt +++ b/server-arrow/src/commonMain/kotlin/ServerRaise.kt @@ -14,6 +14,29 @@ import opensavvy.spine.server.fail import opensavvy.spine.server.route import kotlin.jvm.JvmName +/** + * Declares a Ktor handler matching a Spine [endpoint][opensavvy.spine.api.AnyEndpoint]. + * + * The handler has the ability to `Raise`, including using any method from the Arrow DSL. + * + * ### Example + * + * ```kotlin + * routing { + * route(Api.Users.logIn) { + * ensure(body.password.size > 12) { PasswordTooShort } + * + * val (user, token) = withError({ IncorrectCredentials }) { + * call.response.cookies.append("token", token) + * } + * + * respond(user) + * } + * } + * ``` + * + * For the full list of available methods, see [TypedResponseScope]. + */ @KtorDsl @JvmName("routeOrRaise1") inline fun >, reified Params : Parameters> Route.routeWithRaise( @@ -28,6 +51,29 @@ inline fun 12) { PasswordTooShort } + * + * val (user, token) = withError({ IncorrectCredentials }) { + * call.response.cookies.append("token", token) + * } + * + * respond(user) + * } + * } + * ``` + * + * For the full list of available methods, see [TypedResponseScope]. + */ @KtorDsl @JvmName("routeOrRaise2") inline fun >, ByCode>, reified Params : Parameters> Route.routeWithRaise( @@ -47,6 +93,29 @@ inline fun 12) { PasswordTooShort } + * + * val (user, token) = withError({ IncorrectCredentials }) { + * call.response.cookies.append("token", token) + * } + * + * respond(user) + * } + * } + * ``` + * + * For the full list of available methods, see [TypedResponseScope]. + */ @KtorDsl @JvmName("routeOrRaise3") inline fun >, ByCode>, ByCode>, reified Params : Parameters> Route.routeWithRaise( @@ -71,6 +140,29 @@ inline fun 12) { PasswordTooShort } + * + * val (user, token) = withError({ IncorrectCredentials }) { + * call.response.cookies.append("token", token) + * } + * + * respond(user) + * } + * } + * ``` + * + * For the full list of available methods, see [TypedResponseScope]. + */ @KtorDsl @JvmName("routeOrRaise4") inline fun >, ByCode>, ByCode>, ByCode>, reified Params : Parameters> Route.routeWithRaise( @@ -100,6 +192,29 @@ inline fun 12) { PasswordTooShort } + * + * val (user, token) = withError({ IncorrectCredentials }) { + * call.response.cookies.append("token", token) + * } + * + * respond(user) + * } + * } + * ``` + * + * For the full list of available methods, see [TypedResponseScope]. + */ @KtorDsl @JvmName("routeOrRaise5") inline fun >, ByCode>, ByCode>, ByCode>, ByCode>, reified Params : Parameters> Route.routeWithRaise( @@ -134,6 +249,29 @@ inline fun 12) { PasswordTooShort } + * + * val (user, token) = withError({ IncorrectCredentials }) { + * call.response.cookies.append("token", token) + * } + * + * respond(user) + * } + * } + * ``` + * + * For the full list of available methods, see [TypedResponseScope]. + */ @KtorDsl @JvmName("routeOrRaise6") inline fun >, ByCode>, ByCode>, ByCode>, ByCode>, ByCode>, reified Params : Parameters> Route.routeWithRaise(