ZIO integration¶
The tapir-zio
module defines type aliases and extension methods which make it more ergonomic to work with
ZIO and tapir. Moreover, the tapir-zio-http4s-server
contains similar extensions useful when
exposing the endpoints using the http4s server.
You’ll need the following dependencies:
"com.softwaremill.sttp.tapir" %% "tapir-zio" % "0.16.9"
"com.softwaremill.sttp.tapir" %% "tapir-zio-http4s-server" % "0.16.9"
Next, instead of the usual import sttp.tapir._
, you should import:
import sttp.tapir.ztapir._
This brings into scope all of the basic input/output descriptions, which can be used to define an endpoint.
Additionally, it defines the ZEndpoint
type alias, which should be used instead of Endpoint
.
Note
You should have only one of these imports in your source file. Otherwise, you’ll get naming conflicts. The
import sttp.tapir.ztapir._
import is meant as a complete replacement of import sttp.tapir._
.
Server logic¶
When defining the business logic for an endpoint, the following methods are available, which replace the standard ones:
def zServerLogic(logic: I => ZIO[R, E, O]): ZServerEndpoint[R, I, E, O]
def zServerLogicPart(logicPart: T => ZIO[R, E, U])
def zServerLogicForCurrent(logicPart: I => ZIO[R, E, U])
The first defines complete server logic, while the second and third allow defining server logic in parts.
Exposing endpoints using the http4s server¶
To bring into scope the extension methods used to interpret a ZServerEndpoint
as a http4s server, add the following
import:
import sttp.tapir.server.http4s.ztapir._
This adds the following methods on ZEndpoint
:
def toRoutes(logic: I => ZIO[Any, E, O]): HttpRoutes[Task]
def toRoutesR(logic: I => ZIO[R, E, O]): URIO[R, HttpRoutes[Task]]
(when the logic requires an environment)
And the following methods on ZServerEndpoint
or List[ZServerEndpoint]
:
def toRoutes: HttpRoutes[Task]
def toRoutesR: URIO[R, HttpRoutes[Task]]
(when the logic requires an environment)
Example¶
Two examples of using the ZIO integration are available. The first showcases basic functionality, while the second shows how to use partial server logic methods: