Last active
July 23, 2023 08:22
-
-
Save faveoled/21649d685b008efea0c4ce8cce9345d6 to your computer and use it in GitHub Desktop.
Scala.js Fetch API dom sample
This file contains hidden or 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
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