Skip to content

Instantly share code, notes, and snippets.

@faveoled
Last active July 23, 2023 08:22
Show Gist options
  • Save faveoled/21649d685b008efea0c4ce8cce9345d6 to your computer and use it in GitHub Desktop.
Save faveoled/21649d685b008efea0c4ce8cce9345d6 to your computer and use it in GitHub Desktop.
Scala.js Fetch API dom sample
import scala.concurrent.Future
import org.scalajs.dom
import org.scalajs.dom.Request
import org.scalajs.dom.Fetch
import org.scalajs.dom.HttpMethod
import org.scalajs.dom.RequestInit
import org.scalajs.dom.Headers
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global
object ApiClient {
def fetchGet(url: String): Future[String] = {
dom
.fetch(url, new RequestInit { method = HttpMethod.GET })
.toFuture
.flatMap(resp => {
if (resp.status != 200) {
throw Exception(s"Query failed with status ${resp.statusText}")
}
resp.text().toFuture
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment