Skip to content

Commit eedfa7f

Browse files
Removed dependency on scalajs-dom
1 parent a0c6ff3 commit eedfa7f

File tree

4 files changed

+90
-19
lines changed

4 files changed

+90
-19
lines changed

dom/src/main/scala/scommons/api/http/dom/DomApiHttpClient.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package scommons.api.http.dom
22

3-
import org.scalajs.dom
43
import scommons.api.http.ApiHttpData._
54
import scommons.api.http.dom.DomApiHttpClient._
65
import scommons.api.http.{ApiHttpClient, ApiHttpData, ApiHttpResponse}
@@ -48,12 +47,12 @@ class DomApiHttpClient(baseUrl: String, defaultTimeout: FiniteDuration = 30.seco
4847
}
4948
}
5049

51-
private[dom] def createRequest(): dom.XMLHttpRequest = new dom.XMLHttpRequest()
50+
private[dom] def createRequest(): raw.XMLHttpRequest = new raw.XMLHttpRequest()
5251

53-
private def execute(req: dom.XMLHttpRequest, body: Option[String]): Future[dom.XMLHttpRequest] = {
54-
val promise = Promise[dom.XMLHttpRequest]()
52+
private def execute(req: raw.XMLHttpRequest, body: Option[String]): Future[raw.XMLHttpRequest] = {
53+
val promise = Promise[raw.XMLHttpRequest]()
5554

56-
req.onreadystatechange = { (_: dom.Event) =>
55+
req.onreadystatechange = { (_: js.Object) =>
5756
if (req.readyState == 4) {
5857
promise.success(req)
5958
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package scommons.api.http.dom.raw
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSGlobal
5+
6+
@js.native
7+
@JSGlobal
8+
class XMLHttpRequest extends js.Object {
9+
10+
/**
11+
* Initializes a request. This method is to be used from JavaScript code; to
12+
* initialize a request from native code, use openRequest()instead.
13+
*
14+
* MDN
15+
*/
16+
def open(method: String, url: String, async: Boolean = js.native,
17+
user: String = js.native, password: String = js.native): Unit = js.native
18+
19+
/**
20+
* The number of milliseconds a request can take before automatically being
21+
* terminated. A value of 0 (which is the default) means there is no timeout. Note: You
22+
* may not use a timeout for synchronous requests with an owning window.
23+
*
24+
* MDN
25+
*/
26+
var timeout: Double = js.native
27+
28+
var onreadystatechange: js.Function1[js.Object, _] = js.native
29+
30+
/**
31+
* The state of the request: Value State Description 0 UNSENT open()has not been
32+
* called yet. 1 OPENED send()has not been called yet. 2 HEADERS_RECEIVED send() has
33+
* been called, and headers and status are available. 3 LOADING Downloading;
34+
* responseText holds partial data. 4 DONE The operation is complete.
35+
*
36+
* MDN
37+
*/
38+
def readyState: Int = js.native
39+
40+
/**
41+
* Sends the request. If the request is asynchronous (which is the default), this
42+
* method returns as soon as the request is sent. If the request is synchronous, this
43+
* method doesn't return until the response has arrived.
44+
*
45+
* MDN
46+
*/
47+
def send(data: js.Any = js.native): Unit = js.native
48+
49+
/**
50+
* Sets the value of an HTTP request header. You must call setRequestHeader()
51+
* after open(), but before send(). If this method is called several times with the
52+
* same header, the values are merged into one single request header.
53+
*
54+
* MDN
55+
*/
56+
def setRequestHeader(header: String, value: String): Unit = js.native
57+
58+
/**
59+
* The status of the response to the request. This is the HTTP result code (for example,
60+
* status is 200 for a successful request).
61+
*
62+
* MDN
63+
*/
64+
def status: Int = js.native
65+
66+
def getAllResponseHeaders(): String = js.native
67+
68+
/**
69+
* The response to the request as text, or null if the request was unsuccessful or has
70+
* not yet been sent.
71+
*
72+
* MDN
73+
*/
74+
def responseText: String = js.native
75+
}

dom/src/test/scala/scommons/api/http/dom/DomApiHttpClientSpec.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package scommons.api.http.dom
22

3-
import org.scalajs.dom
4-
import org.scalajs.dom.raw.Event
53
import org.scalamock.scalatest.AsyncMockFactory
64
import org.scalatest.{AsyncFlatSpec, Matchers}
75
import scommons.api.http.ApiHttpData.{StringData, UrlEncodedFormData}
@@ -25,7 +23,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
2523

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

28-
override private[dom] def createRequest(): dom.XMLHttpRequest = req.asInstanceOf[dom.XMLHttpRequest]
26+
override private[dom] def createRequest(): raw.XMLHttpRequest = req.asInstanceOf[raw.XMLHttpRequest]
2927
}
3028

3129
private val params = List("p1" -> "1", "p2" -> "2")
@@ -44,7 +42,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
4442
(req.open _).when(*, *).returns(())
4543
(req.timeout_= _).when(*).returns(())
4644
(req.setRequestHeader _).when(*, *).returns(())
47-
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
45+
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
4846
value(null)
4947
()
5048
}
@@ -81,7 +79,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
8179
(req.open _).when(*, *).returns(())
8280
(req.timeout_= _).when(*).returns(())
8381
(req.setRequestHeader _).when(*, *).returns(())
84-
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
82+
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
8583
value(null)
8684
()
8785
}
@@ -118,7 +116,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
118116
(req.open _).when(*, *).returns(())
119117
(req.timeout_= _).when(*).returns(())
120118
(req.setRequestHeader _).when(*, *).returns(())
121-
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
119+
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
122120
value(null)
123121
()
124122
}
@@ -157,7 +155,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
157155
(req.open _).when(*, *).returns(())
158156
(req.timeout_= _).when(*).returns(())
159157
(req.setRequestHeader _).when(*, *).returns(())
160-
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
158+
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
161159
value(null)
162160
()
163161
}
@@ -190,7 +188,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
190188
(req.open _).when(*, *).returns(())
191189
(req.timeout_= _).when(*).returns(())
192190
(req.setRequestHeader _).when(*, *).returns(())
193-
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[Event, _] =>
191+
(req.onreadystatechange_= _).when(*).onCall { value: js.Function1[js.Object, _] =>
194192
value(null)
195193
()
196194
}
@@ -236,7 +234,7 @@ object DomApiHttpClientSpec {
236234

237235
def timeout_= (value: Double): Unit
238236

239-
def onreadystatechange_= (value: js.Function1[Event, _]): Unit
237+
def onreadystatechange_= (value: js.Function1[js.Object, _]): Unit
240238

241239
def readyState: Int
242240

project/src/main/scala/definitions/ApiDom.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package definitions
22

3-
import common.{Libs, TestLibs}
3+
import common.TestLibs
44
import org.scalajs.sbtplugin.ScalaJSPlugin
55
import sbt.Keys._
66
import sbt._
@@ -16,11 +16,12 @@ object ApiDom extends ApiModule {
1616
super.definition
1717
.enablePlugins(ScalaJSPlugin)
1818
.settings(
19-
description := "Common Scala ApiHttpClient implementation using dom XMLHttpRequest",
19+
description := "Common Scala ApiHttpClient implementation using JavaScript XMLHttpRequest",
2020

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

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

34-
override val runtimeDependencies: Def.Initialize[Seq[ModuleID]] = Def.setting(Seq(
35-
Libs.scalajsDom.value
36-
))
35+
override val runtimeDependencies: Def.Initialize[Seq[ModuleID]] = Def.setting(Nil)
3736

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

0 commit comments

Comments
 (0)