-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lightweight fetch-based http request-transport for the browser (#244
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters