Getting Started
Installation
// ScalaJs
libraryDependencies += "dev.cheleb" %%% "zio-tapir-laminar" % "0.8.3"
Sample
From a classical tapir endpoint definition:
import zio.*
import zio.json.*
import sttp.tapir.*
case class GetResponse(args: Map[String, String]) derives JsonCodec
trait BaseEndpoint {
val baseEndpoint: Endpoint[Unit, Unit, Throwable, Unit, Any] = endpoint
.errorOut(statusCode and plainBody[String])
.mapErrorOut[Throwable](HttpError.decode)(HttpError.encode)
val baseSecuredEndpoint: Endpoint[String, Unit, Throwable, Unit, Any] =
baseEndpoint
.securityIn(auth.bearer[String]())
}
In your Laminar app:
import dev.cheleb.ziotapir.laminar.* // (1)
val eventBus = new EventBus[GetResponse]() // (2)
val errorBus = new EventBus[Throwable]() // (3)
// ...
button(
"runJs",
onClick --> (_ => HttpBinEndpoints.get(()) // (4)
.runJs(eventBus, errorBus) // (5)
)
)
- Import the library, which provides extensions method on
Endpoint
instances (like in first approximationrunJs
). - Create an
EventBus
for the response type. - Create an
EventBus
for the error type. - Use the endpoint as a function from
Input => ZIO
. - Call the
runJs
method on the endpoint, passing theEventBus
instances.
In this article