Skip to content

Commit

Permalink
Docs appendable to Endpoint, no name req. for out methods in Endpoint (
Browse files Browse the repository at this point in the history
…#2203)

Docs appendable to`Endpoint`, no name req. for out methods in `Endpoint`

Also add method to append doc to `EndpointMiddleware`

fixes #2195
fixes #2193
  • Loading branch information
987Nabil authored May 29, 2023
1 parent 4640310 commit 1e1be69
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 24 deletions.
187 changes: 164 additions & 23 deletions zio-http/src/main/scala/zio/http/endpoint/Endpoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,34 @@ final case class Endpoint[Input, Err, Output, Middleware <: EndpointMiddleware](

/**
* Returns a new endpoint derived from this one, whose request content must
* satisfy the specified schema.
* satisfy the specified schema and is documented.
*/
def in[Input2](doc: Doc)(implicit
schema: Schema[Input2],
combiner: Combiner[Input, Input2],
): Endpoint[combiner.Out, Err, Output, Middleware] =
copy(input = input ++ HttpCodec.content(schema) ?? doc)

/**
* Returns a new endpoint derived from this one, whose request content must
* satisfy the specified schema and is documented.
*/
def in[Input2](name: String)(implicit
schema: Schema[Input2],
combiner: Combiner[Input, Input2],
): Endpoint[combiner.Out, Err, Output, Middleware] =
copy(input = input ++ HttpCodec.content(name)(schema))

/**
* Returns a new endpoint derived from this one, whose request content must
* satisfy the specified schema and is documented.
*/
def in[Input2](name: String, doc: Doc)(implicit
schema: Schema[Input2],
combiner: Combiner[Input, Input2],
): Endpoint[combiner.Out, Err, Output, Middleware] =
copy(input = input ++ (HttpCodec.content(name)(schema) ?? doc))

/**
* Returns a new endpoint derived from this one, whose request must satisfy
* the specified codec.
Expand All @@ -202,7 +222,7 @@ final case class Endpoint[Input, Err, Output, Middleware <: EndpointMiddleware](

/**
* Returns a new endpoint derived from this one, whose input type is a stream
* of the specified typ
* of the specified typ.
*/
def inStream[Input2: Schema](implicit
combiner: Combiner[Input, ZStream[Any, Nothing, Input2]],
Expand All @@ -217,7 +237,22 @@ final case class Endpoint[Input, Err, Output, Middleware <: EndpointMiddleware](

/**
* Returns a new endpoint derived from this one, whose input type is a stream
* of the specified type
* of the specified type and is documented.
*/
def inStream[Input2: Schema](doc: Doc)(implicit
combiner: Combiner[Input, ZStream[Any, Nothing, Input2]],
): Endpoint[combiner.Out, Err, Output, Middleware] =
Endpoint(
input = self.input ++ (ContentCodec.contentStream[Input2] ?? doc),
output,
error,
doc,
mw,
)

/**
* Returns a new endpoint derived from this one, whose input type is a stream
* of the specified type.
*/
def inStream[Input2: Schema](name: String)(implicit
combiner: Combiner[Input, ZStream[Any, Nothing, Input2]],
Expand All @@ -230,6 +265,21 @@ final case class Endpoint[Input, Err, Output, Middleware <: EndpointMiddleware](
mw,
)

/**
* Returns a new endpoint derived from this one, whose input type is a stream
* of the specified type and is documented.
*/
def inStream[Input2: Schema](name: String, doc: Doc)(implicit
combiner: Combiner[Input, ZStream[Any, Nothing, Input2]],
): Endpoint[combiner.Out, Err, Output, Middleware] =
Endpoint(
input = self.input ++ (ContentCodec.contentStream[Input2](name) ?? doc),
output,
error,
doc,
mw,
)

/**
* Returns a new endpoint derived from this one whose middleware is composed
* from the existing middleware of this endpoint, and the specified
Expand Down Expand Up @@ -259,24 +309,82 @@ final case class Endpoint[Input, Err, Output, Middleware <: EndpointMiddleware](

/**
* Returns a new endpoint derived from this one, whose output type is the
* specified type for the ok status code.
* specified type for the ok status code and is documented.
*/
def out[Output2: Schema](name: String)(implicit
def out[Output2: Schema](doc: Doc)(implicit
alt: Alternator[Output, Output2],
): Endpoint[Input, Err, alt.Out, Middleware] =
out[Output2](name, Status.Ok)
out[Output2](Status.Ok, doc)

/**
* Returns a new endpoint derived from this one, whose output type is the
* specified type for the ok status code.
*/
def out[Output2: Schema](
mediaType: MediaType,
)(implicit alt: Alternator[Output, Output2]): Endpoint[Input, Err, alt.Out, Middleware] =
out[Output2](Status.Ok, mediaType)

/**
* Returns a new endpoint derived from this one, whose output type is the
* specified type for the specified status code.
*/
def out[Output2: Schema](
name: String,
status: Status,
)(implicit alt: Alternator[Output, Output2]): Endpoint[Input, Err, alt.Out, Middleware] =
Endpoint(
input,
output = (self.output | HttpCodec.content(name)(implicitly[Schema[Output2]])) ++ StatusCodec.status(status),
output = self.output | (HttpCodec.content(implicitly[Schema[Output2]]) ++ StatusCodec.status(status)),
error,
doc,
mw,
)

/**
* Returns a new endpoint derived from this one, whose output type is the
* specified type for the specified status code and is documented.
*/
def out[Output2: Schema](
status: Status,
doc: Doc,
)(implicit alt: Alternator[Output, Output2]): Endpoint[Input, Err, alt.Out, Middleware] =
Endpoint(
input,
output = self.output | ((HttpCodec.content(implicitly[Schema[Output2]]) ++ StatusCodec.status(status)) ?? doc),
error,
doc,
mw,
)

/**
* Returns a new endpoint derived from this one, whose output type is the
* specified type for the ok status code and is documented.
*/
def out[Output2: Schema](
mediaType: MediaType,
doc: Doc,
)(implicit alt: Alternator[Output, Output2]): Endpoint[Input, Err, alt.Out, Middleware] =
Endpoint(
input,
output = self.output | (HttpCodec.content(mediaType)(implicitly[Schema[Output2]]) ?? doc),
error,
doc,
mw,
)

/**
* Returns a new endpoint derived from this one, whose output type is the
* specified type for the specified status code and is documented.
*/
def out[Output2: Schema](
status: Status,
mediaType: MediaType,
doc: Doc,
)(implicit alt: Alternator[Output, Output2]): Endpoint[Input, Err, alt.Out, Middleware] =
Endpoint(
input,
output = self.output |
((HttpCodec.content(mediaType)(implicitly[Schema[Output2]]) ++ StatusCodec.status(status)) ?? doc),
error,
doc,
mw,
Expand All @@ -287,14 +395,12 @@ final case class Endpoint[Input, Err, Output, Middleware <: EndpointMiddleware](
* specified type for the specified status code.
*/
def out[Output2: Schema](
name: String,
status: Status,
mediaType: MediaType,
)(implicit alt: Alternator[Output, Output2]): Endpoint[Input, Err, alt.Out, Middleware] =
Endpoint(
input,
output =
(self.output | HttpCodec.content(name, mediaType)(implicitly[Schema[Output2]])) ++ StatusCodec.status(status),
output = self.output | (HttpCodec.content(mediaType)(implicitly[Schema[Output2]]) ++ StatusCodec.status(status)),
error,
doc,
mw,
Expand All @@ -308,8 +414,20 @@ final case class Endpoint[Input, Err, Output, Middleware <: EndpointMiddleware](
schema: Schema[Err2],
alt: Alternator[Err, Err2],
): Endpoint[Input, alt.Out, Output, Middleware] =
copy[Input, alt.Out, Output, Middleware](error =
self.error | (ContentCodec.content[Err2]("error-response") ++ StatusCodec.status(status)),
copy[Input, alt.Out, Output, Middleware](
error = self.error | (ContentCodec.content[Err2]("error-response") ++ StatusCodec.status(status)),
)

/**
* Returns a new endpoint that can fail with the specified error type for the
* specified status code and is documented.
*/
def outError[Err2](status: Status, doc: Doc)(implicit
schema: Schema[Err2],
alt: Alternator[Err, Err2],
): Endpoint[Input, alt.Out, Output, Middleware] =
copy[Input, alt.Out, Output, Middleware](
error = self.error | ((ContentCodec.content[Err2]("error-response") ++ StatusCodec.status(status)) ?? doc),
)

def outErrors[Err2]: OutErrors[Input, Err, Output, Middleware, Err2] = OutErrors(self)
Expand All @@ -332,7 +450,7 @@ final case class Endpoint[Input, Err, Output, Middleware <: EndpointMiddleware](
): Endpoint[Input, Err, alt.Out, Middleware] =
Endpoint(
input,
output = (self.output | ContentCodec.contentStream[Output2]) ++ StatusCodec.status(Status.Ok),
output = self.output | (ContentCodec.contentStream[Output2] ++ StatusCodec.status(Status.Ok)),
error,
doc,
mw,
Expand All @@ -342,41 +460,64 @@ final case class Endpoint[Input, Err, Output, Middleware <: EndpointMiddleware](
* Returns a new endpoint derived from this one, whose output type is a stream
* of the specified type for the ok status code.
*/
def outStream[Output2: Schema](name: String)(implicit
def outStream[Output2: Schema](doc: Doc)(implicit
alt: Alternator[Output, ZStream[Any, Nothing, Output2]],
): Endpoint[Input, Err, alt.Out, Middleware] =
outStream[Output2](name, Status.Ok)
Endpoint(
input,
output = self.output | (ContentCodec.contentStream[Output2] ++ StatusCodec.status(Status.Ok) ?? doc),
error,
doc,
mw,
)

/**
* Returns a new endpoint derived from this one, whose output type is a stream
* of the specified type for the specified status code.
* of the specified type for the specified status code and is documented.
*/
def outStream[Output2: Schema](
name: String,
status: Status,
doc: Doc,
)(implicit alt: Alternator[Output, ZStream[Any, Nothing, Output2]]): Endpoint[Input, Err, alt.Out, Middleware] =
Endpoint(
input,
output = (self.output | ContentCodec.contentStream[Output2](name)) ++ StatusCodec.status(status),
output = self.output | (ContentCodec.contentStream[Output2] ++ StatusCodec.status(status) ?? doc),
error,
doc,
mw,
)

def outStream[Output2: Schema](
name: String,
mediaType: MediaType,
)(implicit alt: Alternator[Output, ZStream[Any, Nothing, Output2]]): Endpoint[Input, Err, alt.Out, Middleware] =
outStream(name, Status.Ok, mediaType)
outStream(Status.Ok, mediaType)

def outStream[Output2: Schema](
mediaType: MediaType,
doc: Doc,
)(implicit alt: Alternator[Output, ZStream[Any, Nothing, Output2]]): Endpoint[Input, Err, alt.Out, Middleware] =
outStream(Status.Ok, mediaType, doc)

def outStream[Output2: Schema](
status: Status,
mediaType: MediaType,
)(implicit alt: Alternator[Output, ZStream[Any, Nothing, Output2]]): Endpoint[Input, Err, alt.Out, Middleware] =
Endpoint(
input,
output = self.output | (ContentCodec.contentStream[Output2](mediaType) ++ StatusCodec.status(status)),
error,
doc,
mw,
)

def outStream[Output2: Schema](
name: String,
status: Status,
mediaType: MediaType,
doc: Doc,
)(implicit alt: Alternator[Output, ZStream[Any, Nothing, Output2]]): Endpoint[Input, Err, alt.Out, Middleware] =
Endpoint(
input,
output = (self.output | ContentCodec.contentStream[Output2](name, mediaType)) ++ StatusCodec.status(status),
output = self.output | ((ContentCodec.contentStream[Output2](mediaType) ++ StatusCodec.status(status)) ?? doc),
error,
doc,
mw,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ sealed trait EndpointMiddleware { self =>
self.doc + that.doc,
)

def ??(doc: Doc): EndpointMiddleware.Typed[In, Err, Out] =
EndpointMiddleware.Spec(input, output, error, doc)

def implement[R, S](incoming: In => ZIO[R, Err, S])(
outgoing: S => ZIO[R, Err, Out],
): RoutesMiddleware[R, S, this.type] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ object EndpointSpec extends ZIOSpecDefault {
route =
Endpoint
.get(literal("test-byte-stream"))
.outStream[Byte]("response", Status.Ok, MediaType.image.png)
.outStream[Byte](Status.Ok, MediaType.image.png)
.implement { _ =>
ZIO.succeed(ZStream.fromChunk(bytes).rechunk(16))
}
Expand Down

0 comments on commit 1e1be69

Please sign in to comment.