Fetch Client

The FetchClientBuilder creates a standard http4s Client that is described in the http4s documentation.

Example

libraryDependencies += "org.http4s" %%% "http4s-circe" % "0.23.16"
libraryDependencies += "io.circe" %%% "circe-generic" % "0.14.2"
import cats.effect._
import cats.effect.unsafe.implicits._
import io.circe.generic.auto._
import org.http4s.circe.CirceEntityCodec._
import org.http4s.dom._
import org.scalajs.dom._

val client = FetchClientBuilder[IO].create

val repoName = document.getElementById("repo").asInstanceOf[HTMLInputElement]
val repoStars = document.getElementById("stars").asInstanceOf[HTMLElement]

case class Repo(stargazers_count: Int)

val fetchRepo: IO[Unit] = for {
  _ <- IO(repoStars.innerHTML = "<i>fetching...</i>")
  name <- IO(repoName.value)
  repo <- client.expect[Repo](s"https://api.github.com/repos/$name").attempt
  _ <- IO {
    repo match {
      case Right(Repo(stars)) => repoStars.innerHTML = s"$stars ★"
      case Left(_) => repoStars.innerHTML = s"Not found :("
    }
  }
} yield ()

val button = document.getElementById("button").asInstanceOf[HTMLButtonElement]

button.onclick = _ => fetchRepo.unsafeRunAndForget()

How many stars?