Skip to content

Commit

Permalink
Add transform methods on Endpoint, for input, output and errors (#2551
Browse files Browse the repository at this point in the history
)
  • Loading branch information
987Nabil authored Dec 18, 2023
1 parent f42676e commit f8f5b3c
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion zio-http/src/main/scala/zio/http/endpoint/Endpoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import zio.schema._

import zio.http.Header.Accept.MediaTypeWithQFactor
import zio.http._
import zio.http.codec.{HttpCodec, _}
import zio.http.codec._
import zio.http.endpoint.Endpoint.{OutErrors, defaultMediaTypes}

/**
Expand Down Expand Up @@ -583,6 +583,41 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
combiner: Combiner[Input, A],
): Endpoint[PathInput, combiner.Out, Err, Output, Middleware] =
copy(input = self.input ++ codec)

/**
* Transforms the input of this endpoint using the specified functions. This
* is useful to build from different http inputs a domain specific input.
*
* For example
* {{{
* case class ChangeUserName(userId: UUID, name: String)
* val endpoint =
* Endpoint(Method.POST / "user" / uuid("userId") / "changeName").in[String]
* .transformIn { case (userId, name) => ChangeUserName(userId, name) } {
* case ChangeUserName(userId, name) => (userId, name)
* }
* }}}
*/
def transformIn[Input1](f: Input => Input1)(
g: Input1 => Input,
): Endpoint[PathInput, Input1, Err, Output, Middleware] =
copy(input = self.input.transform(f)(g))

/**
* Transforms the output of this endpoint using the specified functions.
*/
def transformOut[Output1](f: Output => Output1)(
g: Output1 => Output,
): Endpoint[PathInput, Input, Err, Output1, Middleware] =
copy(output = self.output.transform(f)(g))

/**
* Transforms the error of this endpoint using the specified functions.
*/
def transformError[Err1](f: Err => Err1)(
g: Err1 => Err,
): Endpoint[PathInput, Input, Err1, Output, Middleware] =
copy(error = self.error.transform(f)(g))
}

object Endpoint {
Expand Down

0 comments on commit f8f5b3c

Please sign in to comment.