Skip to content

Commit 8e1bd69

Browse files
authored
Support xhr.timeout in Request (#151)
1 parent 3eb5119 commit 8e1bd69

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

src/Affjax.js

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ exports._ajax = function () {
7979
};
8080
xhr.responseType = options.responseType;
8181
xhr.withCredentials = options.withCredentials;
82+
xhr.timeout = options.timeout;
8283
xhr.send(options.content);
8384

8485
return function (error, cancelErrback, cancelCallback) {

src/Affjax.purs

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ import Data.Function.Uncurried (Fn2, runFn2)
3434
import Data.HTTP.Method (Method(..), CustomMethod)
3535
import Data.HTTP.Method as Method
3636
import Data.List.NonEmpty as NEL
37-
import Data.Maybe (Maybe(..))
37+
import Data.Maybe (Maybe(..), fromMaybe)
3838
import Data.Nullable (Nullable, toNullable)
39+
import Data.Time.Duration (Milliseconds(..))
3940
import Effect.Aff (Aff, try)
4041
import Effect.Aff.Compat as AC
4142
import Effect.Exception as Exn
@@ -56,6 +57,7 @@ type Request a =
5657
, password :: Maybe String
5758
, withCredentials :: Boolean
5859
, responseFormat :: ResponseFormat.ResponseFormat a
60+
, timeout :: Maybe Milliseconds
5961
}
6062

6163
-- | A record of the type `Request` that has all fields set to default
@@ -79,6 +81,7 @@ defaultRequest =
7981
, password: Nothing
8082
, withCredentials: false
8183
, responseFormat: ResponseFormat.ignore
84+
, timeout: Nothing
8285
}
8386

8487
-- | The possible errors that can occur when making an Affjax request.
@@ -197,6 +200,7 @@ request req =
197200
, username: toNullable req.username
198201
, password: toNullable req.password
199202
, withCredentials: req.withCredentials
203+
, timeout: fromMaybe 0.0 $ (\(Milliseconds x) -> x) <$> req.timeout
200204
}
201205

202206
extractContent :: RequestBody.RequestBody -> Either String Foreign
@@ -251,6 +255,7 @@ type AjaxRequest a =
251255
, username :: Nullable String
252256
, password :: Nullable String
253257
, withCredentials :: Boolean
258+
, timeout :: Number
254259
}
255260

256261
foreign import _ajax

test/Main.js

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ exports.startServer = function (errback, callback) {
3939
});
4040
});
4141

42+
app.get('/slow', function(req, res) {
43+
var date = Date.now();
44+
var currentDate = null;
45+
do {
46+
currentDate = Date.now();
47+
} while (currentDate - date < 2000);
48+
res.header({'content-type': 'text/plain'});
49+
res.send('I hope I am not late!');
50+
});
51+
4252
var server = app.listen(function () {
4353
callback({ port: server.address().port, server: server });
4454
});

test/Main.purs

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Control.Monad.Error.Class (throwError)
1010
import Data.Argonaut.Core as J
1111
import Data.Either (Either(..), either)
1212
import Data.Maybe (Maybe(..))
13+
import Data.Time.Duration (Milliseconds(..))
1314
import Effect (Effect)
1415
import Effect.Aff (Aff, finally, forkAff, killFiber, runAff)
1516
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
@@ -62,6 +63,7 @@ main = void $ runAff (either (\e -> logShow e *> throwException e) (const $ log
6263
let mirror = prefix "/mirror"
6364
let doesNotExist = prefix "/does-not-exist"
6465
let notJson = prefix "/not-json"
66+
let slow = prefix "/slow"
6567

6668
A.log "GET /mirror: should be 200 OK"
6769
(AX.request $ AX.defaultRequest { url = mirror }) >>= assertRight >>= \res -> do
@@ -80,6 +82,11 @@ main = void $ runAff (either (\e -> logShow e *> throwException e) (const $ log
8082
AX.get ResponseFormat.string notJson >>= assertRight >>= \res -> do
8183
assertEq ok200 res.status
8284

85+
A.log "GET /slow with timeout: should return an error"
86+
(AX.request $ AX.defaultRequest { url = slow, timeout = Just (Milliseconds 100.0) }) >>= assertLeft >>= case _ of
87+
AX.XHRError _ → pure unit
88+
other → logAny' other *> assertFail "Expected a XHRError"
89+
8390
A.log "POST /mirror: should use the POST method"
8491
AX.post ResponseFormat.json mirror (Just (RequestBody.string "test")) >>= assertRight >>= \res -> do
8592
assertEq ok200 res.status

0 commit comments

Comments
 (0)