Skip to content

Commit

Permalink
Remove route builder (#3115)
Browse files Browse the repository at this point in the history
* Remove route builder

* Env access in handle error (#2870)
  • Loading branch information
987Nabil authored Sep 9, 2024
1 parent 8731a44 commit b900d12
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 193 deletions.
8 changes: 4 additions & 4 deletions docs/reference/aop/handler_aspect.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ object UserRepository {

```scala mdoc:silent
Routes(
Method.GET / "user" / int("userId") -> sessionMiddleware -> handler {
(userId: Int, session: Session, request: Request) =>
UserRepository.getUser(session.organizationId, userId)
Method.GET / "user" / int("userId") -> handler {
(userId: Int, request: Request) =>
withContext((session: Session) => UserRepository.getUser(session.organizationId, userId))
}
)
) @@ sessionMiddleware
```

The `HandlerAspect` companion object provides a number of helpful constructors for these middlewares.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ object AuthSpec extends ZIOHttpSpec with TestExtensions {
test("Extract username via context with Routes") {
val app = {
Routes(
Method.GET / "context" -> basicAuthContextM ->
handler { (c: AuthContext, _: Request) => Response.text(c.value) },
Method.GET / "context" ->
handler { (_: Request) => withContext((c: AuthContext) => Response.text(c.value)) } @@ basicAuthContextM,
)
}
assertZIO(
Expand Down
294 changes: 123 additions & 171 deletions zio-http/shared/src/main/scala/zio/http/Route.scala

Large diffs are not rendered by default.

16 changes: 4 additions & 12 deletions zio-http/shared/src/main/scala/zio/http/RoutePattern.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import zio.http.codec._
* `RoutePattern.GET`.
*/
final case class RoutePattern[A](method: Method, pathCodec: PathCodec[A]) { self =>
type Params = A

/**
* Attaches documentation to the route pattern, which may be used when
Expand All @@ -61,7 +62,7 @@ final case class RoutePattern[A](method: Method, pathCodec: PathCodec[A]) { self
zippable: RequestHandlerInput[A, I],
trace: zio.Trace,
): Route[Env, Err] =
Route.route(Route.Builder(self, HandlerAspect.identity))(handler)(zippable.zippable, trace)
Route.route(self)(handler)(zippable.zippable, trace)

/**
* Creates a route from this pattern and the specified handler, which ignores
Expand All @@ -72,16 +73,7 @@ final case class RoutePattern[A](method: Method, pathCodec: PathCodec[A]) { self
def ->[Env, Err](handler: Handler[Env, Response, Request, Response])(implicit
trace: zio.Trace,
): Route[Env, Err] =
Route.handled(self)(handler)

/**
* Combines this route pattern with the specified middleware, which can be
* used to build a route by providing a handler.
*/
def ->[Env, Context](middleware: HandlerAspect[Env, Context])(implicit
zippable: Zippable[A, Context],
): Route.Builder[Env, zippable.Out] =
Route.Builder(self, middleware)(zippable)
Route.handledIgnoreParams(self)(handler)

def alternatives: List[RoutePattern[A]] = pathCodec.alternatives.map(RoutePattern(method, _))

Expand Down Expand Up @@ -147,7 +139,7 @@ final case class RoutePattern[A](method: Method, pathCodec: PathCodec[A]) { self
def unapply(tuple: (Method, Path)): Option[A] =
decode(tuple._1, tuple._2).toOption
}
object RoutePattern {
object RoutePattern {
import PathCodec.SegmentSubtree

val CONNECT: RoutePattern[Unit] = fromMethod(Method.CONNECT)
Expand Down
4 changes: 3 additions & 1 deletion zio-http/shared/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
def handleError(f: Err => Response)(implicit trace: Trace): Routes[Env, Nothing] =
new Routes(routes.map(_.handleError(f)))

def handleErrorZIO(f: Err => ZIO[Any, Nothing, Response])(implicit trace: Trace): Routes[Env, Nothing] =
def handleErrorZIO[Env1 <: Env](f: Err => ZIO[Env1, Nothing, Response])(implicit
trace: Trace,
): Routes[Env1, Nothing] =
new Routes(routes.map(_.handleErrorZIO(f)))

/**
Expand Down
10 changes: 8 additions & 2 deletions zio-http/shared/src/main/scala/zio/http/codec/QueryCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

package zio.http.codec
import scala.annotation.tailrec

import zio.stacktracer.TracingImplicits.disableAutoTrace

import zio.schema.Schema
Expand Down Expand Up @@ -118,10 +120,14 @@ private[codec] trait QueryCodecs {
)
}

private def supportedElementSchema(elementSchema: Schema[Any]) =
elementSchema.isInstanceOf[Schema.Primitive[_]] ||
@tailrec
private def supportedElementSchema(elementSchema: Schema[Any]): Boolean = elementSchema match {
case Schema.Lazy(schema0) => supportedElementSchema(schema0())
case _ =>
elementSchema.isInstanceOf[Schema.Primitive[_]] ||
elementSchema.isInstanceOf[Schema.Enum[_]] && elementSchema.annotations.exists(_.isInstanceOf[simpleEnum]) ||
elementSchema.isInstanceOf[Schema.Record[_]] && elementSchema.asInstanceOf[Schema.Record[_]].fields.size == 1
}

def queryAll[A](implicit schema: Schema[A]): QueryCodec[A] =
schema match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
}
}

Route.handled(self.route)(handler)
Route.handledIgnoreParams(self.route)(handler)
}

/**
Expand Down

0 comments on commit b900d12

Please sign in to comment.