Skip to content

Commit

Permalink
Removed dependency on scalajs-dom
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Feb 12, 2020
1 parent a0c6ff3 commit eedfa7f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package scommons.api.http.dom

import org.scalajs.dom
import scommons.api.http.ApiHttpData._
import scommons.api.http.dom.DomApiHttpClient._
import scommons.api.http.{ApiHttpClient, ApiHttpData, ApiHttpResponse}
Expand Down Expand Up @@ -48,12 +47,12 @@ class DomApiHttpClient(baseUrl: String, defaultTimeout: FiniteDuration = 30.seco
}
}

private[dom] def createRequest(): dom.XMLHttpRequest = new dom.XMLHttpRequest()
private[dom] def createRequest(): raw.XMLHttpRequest = new raw.XMLHttpRequest()

private def execute(req: dom.XMLHttpRequest, body: Option[String]): Future[dom.XMLHttpRequest] = {
val promise = Promise[dom.XMLHttpRequest]()
private def execute(req: raw.XMLHttpRequest, body: Option[String]): Future[raw.XMLHttpRequest] = {
val promise = Promise[raw.XMLHttpRequest]()

req.onreadystatechange = { (_: dom.Event) =>
req.onreadystatechange = { (_: js.Object) =>
if (req.readyState == 4) {
promise.success(req)
}
Expand Down
75 changes: 75 additions & 0 deletions dom/src/main/scala/scommons/api/http/dom/raw/XMLHttpRequest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package scommons.api.http.dom.raw

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal

@js.native
@JSGlobal
class XMLHttpRequest extends js.Object {

/**
* Initializes a request. This method is to be used from JavaScript code; to
* initialize a request from native code, use openRequest()instead.
*
* MDN
*/
def open(method: String, url: String, async: Boolean = js.native,
user: String = js.native, password: String = js.native): Unit = js.native

/**
* The number of milliseconds a request can take before automatically being
* terminated. A value of 0 (which is the default) means there is no timeout. Note: You
* may not use a timeout for synchronous requests with an owning window.
*
* MDN
*/
var timeout: Double = js.native

var onreadystatechange: js.Function1[js.Object, _] = js.native

/**
* The state of the request: Value State Description 0 UNSENT open()has not been
* called yet. 1 OPENED send()has not been called yet. 2 HEADERS_RECEIVED send() has
* been called, and headers and status are available. 3 LOADING Downloading;
* responseText holds partial data. 4 DONE The operation is complete.
*
* MDN
*/
def readyState: Int = js.native

/**
* Sends the request. If the request is asynchronous (which is the default), this
* method returns as soon as the request is sent. If the request is synchronous, this
* method doesn't return until the response has arrived.
*
* MDN
*/
def send(data: js.Any = js.native): Unit = js.native

/**
* Sets the value of an HTTP request header. You must call setRequestHeader()
* after open(), but before send(). If this method is called several times with the
* same header, the values are merged into one single request header.
*
* MDN
*/
def setRequestHeader(header: String, value: String): Unit = js.native

/**
* The status of the response to the request. This is the HTTP result code (for example,
* status is 200 for a successful request).
*
* MDN
*/
def status: Int = js.native

def getAllResponseHeaders(): String = js.native

/**
* The response to the request as text, or null if the request was unsuccessful or has
* not yet been sent.
*
* MDN
*/
def responseText: String = js.native
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package scommons.api.http.dom

import org.scalajs.dom
import org.scalajs.dom.raw.Event
import org.scalamock.scalatest.AsyncMockFactory
import org.scalatest.{AsyncFlatSpec, Matchers}
import scommons.api.http.ApiHttpData.{StringData, UrlEncodedFormData}
Expand All @@ -25,7 +23,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec

private class TestDomClient(req: MockXMLHttpRequest) extends DomApiHttpClient(baseUrl) {

override private[dom] def createRequest(): dom.XMLHttpRequest = req.asInstanceOf[dom.XMLHttpRequest]
override private[dom] def createRequest(): raw.XMLHttpRequest = req.asInstanceOf[raw.XMLHttpRequest]
}

private val params = List("p1" -> "1", "p2" -> "2")
Expand All @@ -44,7 +42,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
(req.setRequestHeader _).when(*, *).returns(())
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
value(null)
()
}
Expand Down Expand Up @@ -81,7 +79,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
(req.setRequestHeader _).when(*, *).returns(())
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
value(null)
()
}
Expand Down Expand Up @@ -118,7 +116,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
(req.setRequestHeader _).when(*, *).returns(())
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
value(null)
()
}
Expand Down Expand Up @@ -157,7 +155,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
(req.setRequestHeader _).when(*, *).returns(())
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
value(null)
()
}
Expand Down Expand Up @@ -190,7 +188,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
(req.setRequestHeader _).when(*, *).returns(())
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
value(null)
()
}
Expand Down Expand Up @@ -236,7 +234,7 @@ object DomApiHttpClientSpec {

def timeout_= (value: Double): Unit

def onreadystatechange_= (value: js.Function1[Event, _]): Unit
def onreadystatechange_= (value: js.Function1[js.Object, _]): Unit

def readyState: Int

Expand Down
9 changes: 4 additions & 5 deletions project/src/main/scala/definitions/ApiDom.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package definitions

import common.{Libs, TestLibs}
import common.TestLibs
import org.scalajs.sbtplugin.ScalaJSPlugin
import sbt.Keys._
import sbt._
Expand All @@ -16,11 +16,12 @@ object ApiDom extends ApiModule {
super.definition
.enablePlugins(ScalaJSPlugin)
.settings(
description := "Common Scala ApiHttpClient implementation using dom XMLHttpRequest",
description := "Common Scala ApiHttpClient implementation using JavaScript XMLHttpRequest",

// disable scoverage, until the following issue is fixed:
// https://github.com/scoverage/scalac-scoverage-plugin/issues/196
coverageEnabled := false,
coverageExcludedPackages := "scommons.api.http.dom.raw",

//Opt-in @ScalaJSDefined by default
scalacOptions += "-P:scalajs:sjsDefinedByDefault"
Expand All @@ -31,9 +32,7 @@ object ApiDom extends ApiModule {
ApiCore.js
)

override val runtimeDependencies: Def.Initialize[Seq[ModuleID]] = Def.setting(Seq(
Libs.scalajsDom.value
))
override val runtimeDependencies: Def.Initialize[Seq[ModuleID]] = Def.setting(Nil)

override val testDependencies: Def.Initialize[Seq[ModuleID]] = Def.setting(Seq(
TestLibs.scalaTestJs.value,
Expand Down

0 comments on commit eedfa7f

Please sign in to comment.