Skip to content

Commit

Permalink
Merge pull request #39 from cheleb/38-provide-base-controller
Browse files Browse the repository at this point in the history
38 provide base controller
  • Loading branch information
cheleb authored Nov 23, 2024
2 parents de8c96a + c4b21c7 commit f6e557c
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 44 deletions.
19 changes: 12 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ val dev = sys.env.get("DEV").getOrElse("demo")
val scala33 = "3.5.2"

val Versions = new {
val chimney = "1.5.0"
val laminar = "17.1.0"
val tapir = "1.11.8"
val sttp = "3.9.8"
val tapir = "1.11.9"
val sttp = "3.10.1"
val sttpModel = "1.7.11"
val zio = "2.1.11"
val zio = "2.1.13"
val zioJson = "0.7.3"
}

Expand Down Expand Up @@ -67,7 +68,7 @@ lazy val root = project

lazy val server = project
.in(file("modules/server"))
.settings(name := "zio-jwt-server")
.settings(name := "zio-tapir-server")
.settings(
libraryDependencies ++= Seq(
"com.softwaremill.sttp.tapir" %% "tapir-zio" % Versions.tapir
Expand All @@ -87,18 +88,22 @@ lazy val shared = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("modules/shared"))
.settings(
name := "zio-jwt"
name := "zio-tapir-shared"
)
.settings(
libraryDependencies += "com.softwaremill.sttp.model" %%% "core" % Versions.sttpModel
libraryDependencies ++= Seq(
// "com.softwaremill.sttp.model" %%% "core" % Versions.sttpModel,
"io.scalaland" %% "chimney" % Versions.chimney,
"dev.zio" %%% "zio" % Versions.zio
)
)

lazy val sharedJvm = shared.jvm
lazy val sharedJs = shared.js

lazy val core = scalajsProject("core", false)
.settings(
name := "zio-laminar-tapir"
name := "zio-tapir-laminar"
)
.dependsOn(sharedJs)
.settings(scalacOptions ++= usedScalacOptions)
Expand Down
4 changes: 2 additions & 2 deletions docs/_docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: Getting Started

```sbt
// ScalaJs
libraryDependencies += "dev.cheleb" %%% "zio-laminar-tapir" % "{{ projectVersion}}"
libraryDependencies += "dev.cheleb" %%% "zio-tapir-laminar" % "{{ projectVersion}}"
```


Expand Down Expand Up @@ -40,7 +40,7 @@ In your Laminar app:

```scala sc:nocompile

import dev.cheleb.ziolaminartapir.* // (1)
import dev.cheleb.ziotapir.laminar.* // (1)

val eventBus = new EventBus[GetResponse]() // (2)
val errorBus = new EventBus[Throwable]() // (3)
Expand Down
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/demo/DemoApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package demo
import org.scalajs.dom
import com.raquo.laminar.api.L.*

import dev.cheleb.ziolaminartapir.*
import dev.cheleb.ziotapir.laminar.*
import sttp.model.Uri

val httpbin = Uri.unsafeParse("https://httpbin.org")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cheleb.ziolaminartapir
package dev.cheleb.ziotapir

import dev.cheleb.ziojwt.WithToken

Expand All @@ -18,7 +18,7 @@ import sttp.model.Uri
* @param interpreter
* @param config
*/
private[ziolaminartapir] abstract class BackendClient(
private[ziotapir] abstract class BackendClient(
backend: SttpBackend[Task, ZioStreamsWithWebSockets],
interpreter: SttpClientInterpreter
) {
Expand All @@ -32,7 +32,7 @@ private[ziolaminartapir] abstract class BackendClient(
* @param endpoint
* @return
*/
private[ziolaminartapir] def endpointRequest[I, E, O](
private[ziotapir] def endpointRequest[I, E, O](
baseUri: Uri,
endpoint: Endpoint[Unit, I, E, O, Any]
): I => Request[Either[E, O], Any] =
Expand All @@ -47,7 +47,7 @@ private[ziolaminartapir] abstract class BackendClient(
* @param endpoint
* @return
*/
private[ziolaminartapir] def securedEndpointRequest[SI, I, E, O](
private[ziotapir] def securedEndpointRequest[SI, I, E, O](
baseUri: Uri,
endpoint: Endpoint[SI, I, E, O, Any]
): SI => I => Request[Either[E, O], Any] =
Expand All @@ -57,7 +57,7 @@ private[ziolaminartapir] abstract class BackendClient(
)

/** Get the token from the session, or fail with an exception. */
private[ziolaminartapir] def tokenOfFail[UserToken <: WithToken](
private[ziotapir] def tokenOfFail[UserToken <: WithToken](
issuer: Uri
)(using
session: Session[UserToken]
Expand All @@ -69,7 +69,7 @@ private[ziolaminartapir] abstract class BackendClient(

} yield withToken.token

private[ziolaminartapir] def endpointRequestZIO[I, E <: Throwable, O](
private[ziotapir] def endpointRequestZIO[I, E <: Throwable, O](
baseUri: Uri,
endpoint: Endpoint[Unit, I, E, O, Any]
)(
Expand All @@ -80,7 +80,7 @@ private[ziolaminartapir] abstract class BackendClient(
.map(_.body)
.absolve

private[ziolaminartapir] def securedEndpointRequestZIO[
private[ziotapir] def securedEndpointRequestZIO[
UserToken <: WithToken,
I,
E <: Throwable,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cheleb.ziolaminartapir
package dev.cheleb.ziotapir

import sttp.model.Uri

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cheleb.ziolaminartapir
package dev.cheleb.ziotapir

import izumi.reflect.Tag

Expand Down Expand Up @@ -27,7 +27,7 @@ trait DifferentOriginBackendClient {
* @param payload
* @return
*/
private[ziolaminartapir] def endpointRequestZIO[I, E <: Throwable, O](
private[ziotapir] def endpointRequestZIO[I, E <: Throwable, O](
baseUri: Uri,
endpoint: Endpoint[Unit, I, E, O, Any]
)(
Expand All @@ -50,7 +50,7 @@ trait DifferentOriginBackendClient {
* the session with the token
* @return
*/
private[ziolaminartapir] def securedEndpointRequestZIO[
private[ziotapir] def securedEndpointRequestZIO[
UserToken <: WithToken,
I,
E <: Throwable,
Expand Down Expand Up @@ -89,7 +89,7 @@ object DifferentOriginBackendClientLive {

/** The layer to create the client.
*/
private[ziolaminartapir] def configuredLayer
private[ziotapir] def configuredLayer
: ULayer[DifferentOriginBackendClient] = {
val backend: SttpBackend[Task, ZioStreamsWithWebSockets] = FetchZioBackend()
val interpreter = SttpClientInterpreter()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cheleb.ziolaminartapir
package dev.cheleb.ziotapir

import dev.cheleb.ziojwt.WithToken

Expand Down Expand Up @@ -32,7 +32,7 @@ trait SameOriginBackendClient {
* @param payload
* @return
*/
private[ziolaminartapir] def endpointRequestZIO[I, E <: Throwable, O](
private[ziotapir] def endpointRequestZIO[I, E <: Throwable, O](
endpoint: Endpoint[Unit, I, E, O, Any]
)(
payload: I
Expand All @@ -49,7 +49,7 @@ trait SameOriginBackendClient {
* @param payload
* @return
*/
private[ziolaminartapir] def securedEndpointRequestZIO[
private[ziotapir] def securedEndpointRequestZIO[
UserToken <: WithToken,
I,
E <: Throwable,
Expand Down Expand Up @@ -80,7 +80,7 @@ private class SameOriginBackendClientLive(
* @param payload
* @return
*/
private[ziolaminartapir] def endpointRequestZIO[I, E <: Throwable, O](
private[ziotapir] def endpointRequestZIO[I, E <: Throwable, O](
endpoint: Endpoint[Unit, I, E, O, Any]
)(
payload: I
Expand All @@ -92,7 +92,7 @@ private class SameOriginBackendClientLive(
* @param payload
* @return
*/
private[ziolaminartapir] def securedEndpointRequestZIO[
private[ziotapir] def securedEndpointRequestZIO[
UserToken <: WithToken,
I,
E <: Throwable,
Expand Down Expand Up @@ -138,7 +138,7 @@ object SameOriginBackendClientLive {

/** The layer that can be used to create
*/
private[ziolaminartapir] def configuredLayer
private[ziotapir] def configuredLayer
: ZLayer[Any, Nothing, SameOriginBackendClientLive] = {
val backend: SttpBackend[Task, ZioStreamsWithWebSockets] = FetchZioBackend()
val interpreter = SttpClientInterpreter()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cheleb.ziolaminartapir
package dev.cheleb.ziotapir

import scala.scalajs.js.Date

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cheleb.ziolaminartapir
package dev.cheleb.ziotapir

import zio.json.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cheleb.ziolaminartapir
package dev.cheleb.ziotapir.laminar

import zio.*

Expand All @@ -7,6 +7,7 @@ import scala.annotation.targetName
import com.raquo.laminar.api.L.*
import sttp.tapir.Endpoint
import dev.cheleb.ziojwt.WithToken
import dev.cheleb.ziotapir.*
import sttp.model.Uri
import sttp.capabilities.zio.ZioStreams
import sttp.capabilities.WebSockets
Expand All @@ -20,17 +21,6 @@ import sttp.capabilities.WebSockets
* Also, there are two kinds of ZIO, and associated 2 extensions:
* - SameOriginBackendClient: for requests to the same origin
* - DifferentOriginBackendClient: for requests to a different origin
*/

type ZioStreamsWithWebSockets = ZioStreams & WebSockets

/** Typed exception for restricted endpoints.
* @param message
*/
case class RestrictedEndpointException(message: String)
extends RuntimeException(message)

/** ZIO JS extension methods.
*
* This object contains:
* - convenience methods for calling endpoints.
Expand All @@ -40,6 +30,7 @@ case class RestrictedEndpointException(message: String)

/** Extension to ZIO[SameOriginBackendClient, E, A] that allows us to run in JS.
*/

extension [E <: Throwable, A](zio: ZIO[SameOriginBackendClient, E, A])

/** Run the underlying request to the default backend.
Expand Down
12 changes: 12 additions & 0 deletions modules/core/src/main/scala/dev/cheleb/ziotapir/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.cheleb.ziotapir

import sttp.capabilities.zio.ZioStreams
import sttp.capabilities.WebSockets

type ZioStreamsWithWebSockets = ZioStreams & WebSockets

/** Typed exception for restricted endpoints.
* @param message
*/
case class RestrictedEndpointException(message: String)
extends RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.cheleb.ziotapir

import sttp.tapir.server.ServerEndpoint
import zio.Task

trait BaseController {

val routes: List[ServerEndpoint[Any, Task]]

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cheleb.ziojwt
package dev.cheleb.ziotapir

import zio.*

Expand All @@ -13,7 +13,7 @@ import sttp.tapir.ztapir.*
*/
trait SecuredBaseController[SI, Principal](
principalExtractor: SI => Task[Principal]
):
) extends BaseController:
/** Enriches an endpoint with security logic
*/
extension [I, O, R](endpoint: Endpoint[SI, I, Throwable, O, R])
Expand Down
19 changes: 19 additions & 0 deletions modules/shared/src/main/scala/dev/cheleb/ziochimney/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.cheleb.ziochimney

import zio.Task
import io.scalaland.chimney.dsl.*
import io.scalaland.chimney.Transformer

/** Extension methods for `Task` that allow to transform the result of the task
* using Chimney.
*/
extension [A](task: Task[A])
def mapInto[B](using Transformer[A, B]): Task[B] =
task.map(_.into[B].transform)

/** Extension methods for `Task[Option[A]]` that allow to transform the result
* of the task using Chimney.
*/
extension [A](task: Task[Option[A]])
def mapOption[B](using Transformer[A, B]): Task[Option[B]] =
task.map(_.map(_.into[B].transform))

0 comments on commit f6e557c

Please sign in to comment.