Skip to content

Commit

Permalink
Support root path. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-Ducret authored Oct 4, 2022
1 parent 73e3576 commit 0365c53
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
24 changes: 18 additions & 6 deletions core/js/src/main/scala/zio/app/FrontendUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import boopickle.Default._
import boopickle.{CompositePickler, UnpickleState}
import org.scalajs.dom.RequestMode
import sttp.client3._
import sttp.model.Uri
import zio._
import zio.stream._

Expand All @@ -16,16 +17,22 @@ object FrontendUtils {
private val sttpBackend =
FetchZioBackend(fetchOptions = FetchOptions(credentials = None, mode = Some(RequestMode.`same-origin`)))

def apiUri(config: ClientConfig): Uri =
Uri(org.scalajs.dom.document.location.hostname)
.scheme(org.scalajs.dom.document.location.protocol.replaceAll(":", ""))
.port(org.scalajs.dom.document.location.port.toIntOption)
.addPathSegments(config.root.add("api").segments)

def fetch[A: Pickler](service: String, method: String, config: ClientConfig): UIO[A] =
fetchRequest[A](bytesRequest.get(uri"/api/$service/$method"), config)
fetchRequest[A](bytesRequest.get(apiUri(config).addPath(service, method)), config)

def fetch[A: Pickler](
service: String,
method: String,
value: ByteBuffer,
config: ClientConfig,
): UIO[A] =
fetchRequest[A](bytesRequest.post(uri"/api/$service/$method").body(value), config)
fetchRequest[A](bytesRequest.post(apiUri(config).addPath(service, method)).body(value), config)

def fetchRequest[A: Pickler](request: Request[Array[Byte], Any], config: ClientConfig): UIO[A] =
sttpBackend
Expand All @@ -38,11 +45,16 @@ object FrontendUtils {
}
}

def fetchStream[A: Pickler](service: String, method: String): Stream[Nothing, A] =
fetchStreamRequest[A](basicRequest.get(uri"/api/$service/$method"))
def fetchStream[A: Pickler](service: String, method: String, config: ClientConfig): Stream[Nothing, A] =
fetchStreamRequest[A](basicRequest.get(apiUri(config).addPath(service, method)))

def fetchStream[A: Pickler](service: String, method: String, value: ByteBuffer): Stream[Nothing, A] =
fetchStreamRequest[A](basicRequest.post(uri"/api/$service/$method").body(value))
def fetchStream[A: Pickler](
service: String,
method: String,
value: ByteBuffer,
config: ClientConfig,
): Stream[Nothing, A] =
fetchStreamRequest[A](basicRequest.post(apiUri(config).addPath(service, method)).body(value))

def fetchStreamRequest[A: Pickler](request: Request[Either[String, String], Any]): Stream[Nothing, A] =
ZStream.unwrap {
Expand Down
9 changes: 7 additions & 2 deletions core/shared/src/main/scala/zio/app/ClientConfig.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package zio.app

final case class ClientConfig(authToken: Option[String])
import sttp.model.Uri

final case class ClientConfig(
authToken: Option[String],
root: Uri.AbsolutePath
)

object ClientConfig {
val empty: ClientConfig =
ClientConfig(None)
ClientConfig(None, Uri.AbsolutePath(Seq.empty))
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ private[app] class Macros(val c: blackbox.Context) {
val request =
if (isStream) {
if (params.isEmpty)
q"_root_.zio.app.FrontendUtils.fetchStream[$returnType](${serviceType.finalResultType.toString}, ${methodName.toString})"
q"_root_.zio.app.FrontendUtils.fetchStream[$returnType](${serviceType.finalResultType.toString}, ${methodName.toString}, $config)"
else
q"_root_.zio.app.FrontendUtils.fetchStream[$returnType](${serviceType.finalResultType.toString}, ${methodName.toString}, Pickle.intoBytes($pickleType))"
q"_root_.zio.app.FrontendUtils.fetchStream[$returnType](${serviceType.finalResultType.toString}, ${methodName.toString}, Pickle.intoBytes($pickleType), $config)"
} else {
if (params.isEmpty)
q"_root_.zio.app.FrontendUtils.fetch[$returnType](${serviceType.finalResultType.toString}, ${methodName.toString}, $config)"
Expand Down

0 comments on commit 0365c53

Please sign in to comment.