Getting Started
Installation
// ScalaJs
libraryDependencies += "dev.cheleb" %%% "zio-tapir-laminar" % "4.0.0+2-c793badb-SNAPSHOT"
Last version is `4.0.0+2-c793badb-SNAPSHOT it depends on sttp 4.x.
For sttp 3.x use version < 1.x
Sample
import zio.*
import zio.json.*
import sttp.tapir.*
import dev.cheleb.ziotapir.* // (1)
import dev.cheleb.ziotapir.laminar.* // (1)
// From a classical tapir:
//
// With a response type `GetResponse`
case class GetResponse(
args: Map[String, String],
headers: Map[String, String]
) derives JsonCodec
// Create an endpoint that handles a GET request
val get = endpoint.get // (2)
.in("get")
.out(jsonBody[GetResponse])
.errorOut(statusCode and plainBody[String])
.mapErrorOut[Throwable](HttpError.decode)(HttpError.encode)
// Create an event bus for the response type
val eventBus = EventBus[GetResponse]() // (3)
// Use the endpoint as as ZIO effect
get(()) // (4)
.emit(eventBus) // (5)
-
(1) Import the library, which provides extensions method on
Endpointinstances.- Import the necessary classes and implicits from the library.
- Impot the
HttpErrorclass for error handling.
-
(2) Create an
Endpointinstance using the Tapir DSL, defining the input, output, and error types.- Notice that the output is a JSON body of type
GetResponse, and the error output is a combination of a status code and a plain body. - Error muste be encoded and decoded using
HttpError.encodeandHttpError.decode.
- Notice that the output is a JSON body of type
-
(3) Create an
EventBusfor the responseGetResponse. -
(4) Use the endpoint as a function from
Input => ZIO[Backend, Throwable, GetResponse]. -
(5) Call the
emitmethod on the endpoint, passing theEventBusinstances.
In this article