Skip to content

Commit

Permalink
add lightweight fetch-based http request-transport for the browser (#244
Browse files Browse the repository at this point in the history
)

* add lightweight fetch-based http request-transport for the browser

* readme
  • Loading branch information
cornerman authored Jun 20, 2024
1 parent c5971f1 commit 92de6be
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,25 @@ val api: MyApi[fs2.Stream[IO, *]] = client.wire[MyApi[fs2.Stream[IO, *]]]

```

### fetch (browser)

This is useful when running in the browser, because it will have a smaller bundle-size then using the http4s client.

Use with:
```
libraryDependencies += "com.github.cornerman" %%% "sloth-jsdom-client" % "0.7.3"
```

In the client:
```scala
import sloth.Client
import sloth.ext.jsdom.client.HttpRpcTransport

// for usual rpc
val client = Client[String, IO](HttpRpcTransport[IO])
val api: MyApi[IO] = client.wire[MyApi[IO]]
```

## Experimental: Checksum for Apis

Currently scala-2 only.
Expand Down
13 changes: 13 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ lazy val sloth = crossProject(JSPlatform, JVMPlatform)
Nil
).jsSettings(jsSettings)

lazy val jsdomClient = project
.enablePlugins(ScalaJSPlugin)
.dependsOn(sloth.js)
.settings(commonSettings)
.settings(jsSettings)
.settings(
name := "sloth-jsdom-client",
libraryDependencies ++=
Deps.scalaJsDom.value ::
Deps.catsEffect.value ::
Nil
)

lazy val http4sClient = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.dependsOn(sloth)
Expand Down
27 changes: 27 additions & 0 deletions jsdomClient/src/main/scala/HttpRpcTransport.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package sloth.ext.jsdom.client

import cats.effect.Async
import cats.implicits._
import sloth.{Request, RequestTransport}
import org.scalajs.dom

import scala.scalajs.js
import scala.scalajs.js.JSConverters._

case class HttpRequestConfig(
baseUri: String = "/",
headers: Map[String, String] = Map.empty,
)

object HttpRpcTransport {
def apply[F[_]: Async]: RequestTransport[String, F] = apply(HttpRequestConfig().pure[F])

def apply[F[_]: Async](config: F[HttpRequestConfig]): RequestTransport[String, F] = new RequestTransport[String, F] {
override def apply(request: Request[String]): F[String] = for {
config <- config
url = s"${config.baseUri}${request.path.mkString("/")}"
requestArgs = new dom.RequestInit { headers = config.headers.toJSDictionary; method = dom.HttpMethod.POST; body = request.payload }
result <- Async[F].fromThenable(Async[F].delay[js.Thenable[String]](dom.fetch(url, requestArgs).`then`[String](_.text())))
} yield result
}
}
3 changes: 3 additions & 0 deletions project/Deps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ object Deps {

val scalaReflect = dep("org.scala-lang" % "scala-reflect")
val cats = dep("org.typelevel" %%% "cats-core" % "2.12.0")
val catsEffect = dep("org.typelevel" %%% "cats-effect" % "3.5.4")
val chameleon = dep("com.github.cornerman" %%% "chameleon" % "0.4.1")

val zioJson = dep("dev.zio" %%% "zio-json" % "0.7.0")
Expand All @@ -27,4 +28,6 @@ object Deps {
val dsl = dep("org.http4s" %%% "http4s-dsl" % version)
val client = dep("org.http4s" %%% "http4s-client" % version)
}

val scalaJsDom = dep("org.scala-js" %%% "scalajs-dom" % "2.8.0")
}

0 comments on commit 92de6be

Please sign in to comment.