Skip to content

Commit fe6f1cb

Browse files
authored
Add proxy methods for schema based headers and query params (#3372) (#3411)
1 parent 39bf034 commit fe6f1cb

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

zio-http/jvm/src/test/scala/zio/http/endpoint/RequestSpec.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ object RequestSpec extends ZIOHttpSpec {
4949
}
5050
},
5151
Endpoint(GET / "users" / int("userId") / "posts" / int("postId"))
52-
.header(HeaderCodec.headerAs[java.util.UUID]("X-Correlation-ID"))
52+
.header[java.util.UUID]("X-Correlation-ID")
5353
.out[String]
5454
.implementHandler {
5555
Handler.fromFunction { case (userId, postId, correlationId) =>

zio-http/shared/src/main/scala/zio/http/endpoint/Endpoint.scala

+53
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,23 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
225225
): Endpoint[PathInput, combiner.Out, Err, Output, Auth] =
226226
copy(input = self.input ++ codec)
227227

228+
/**
229+
* Returns a new endpoint that requires the specified headers to be present.
230+
*/
231+
def header[A](
232+
name: String,
233+
)(implicit schema: Schema[A], combiner: Combiner[Input, A]): Endpoint[PathInput, combiner.Out, Err, Output, Auth] =
234+
header(HeaderCodec.headerAs[A](name))
235+
236+
/**
237+
* Returns a new endpoint that requires the specified headers to be present.
238+
*/
239+
def header[A](implicit
240+
schema: Schema[A],
241+
combiner: Combiner[Input, A],
242+
): Endpoint[PathInput, combiner.Out, Err, Output, Auth] =
243+
header(HeaderCodec.headers[A])
244+
228245
def implement[Env](f: Input => ZIO[Env, Err, Output])(implicit
229246
trace: Trace,
230247
): Route[Env, Nothing] =
@@ -671,6 +688,25 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
671688
): Endpoint[PathInput, Input, Err, combiner.Out, Auth] =
672689
copy(output = self.output ++ codec)
673690

691+
/**
692+
* Returns a new endpoint that requires the specified headers to be part of
693+
* the response.
694+
*/
695+
def outHeader[A](
696+
name: String,
697+
)(implicit schema: Schema[A], combiner: Combiner[Output, A]): Endpoint[PathInput, Input, Err, combiner.Out, Auth] =
698+
outHeader(HeaderCodec.headerAs[A](name))
699+
700+
/**
701+
* Returns a new endpoint that requires the specified headers to be part of
702+
* the response.
703+
*/
704+
def outHeader[A](implicit
705+
schema: Schema[A],
706+
combiner: Combiner[Output, A],
707+
): Endpoint[PathInput, Input, Err, combiner.Out, Auth] =
708+
outHeader(HeaderCodec.headers[A])
709+
674710
/**
675711
* Returns a new endpoint derived from this one, whose output type is a stream
676712
* of the specified type for the ok status code.
@@ -805,6 +841,23 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
805841
): Endpoint[PathInput, combiner.Out, Err, Output, Auth] =
806842
copy(input = self.input ++ codec)
807843

844+
/**
845+
* Returns a new endpoint that requires the specified query.
846+
*/
847+
def query[A](
848+
name: String,
849+
)(implicit schema: Schema[A], combiner: Combiner[Input, A]): Endpoint[PathInput, combiner.Out, Err, Output, Auth] =
850+
copy(input = self.input ++ QueryCodec.query[A](name))
851+
852+
/**
853+
* Returns a new endpoint that requires the specified query.
854+
*/
855+
def query[A](implicit
856+
schema: Schema[A],
857+
combiner: Combiner[Input, A],
858+
): Endpoint[PathInput, combiner.Out, Err, Output, Auth] =
859+
copy(input = self.input ++ QueryCodec.query[A])
860+
808861
/**
809862
* Adds tags to the endpoint. They are used for documentation generation. For
810863
* example to group endpoints for OpenAPI.

0 commit comments

Comments
 (0)