Skip to content

Commit

Permalink
Support xhr.timeout in Request (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
chekoopa committed Sep 6, 2020
1 parent 3eb5119 commit 8e1bd69
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Affjax.js
Expand Up @@ -79,6 +79,7 @@ exports._ajax = function () {
};
xhr.responseType = options.responseType;
xhr.withCredentials = options.withCredentials;
xhr.timeout = options.timeout;
xhr.send(options.content);

return function (error, cancelErrback, cancelCallback) {
Expand Down
7 changes: 6 additions & 1 deletion src/Affjax.purs
Expand Up @@ -34,8 +34,9 @@ import Data.Function.Uncurried (Fn2, runFn2)
import Data.HTTP.Method (Method(..), CustomMethod)
import Data.HTTP.Method as Method
import Data.List.NonEmpty as NEL
import Data.Maybe (Maybe(..))
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Nullable (Nullable, toNullable)
import Data.Time.Duration (Milliseconds(..))
import Effect.Aff (Aff, try)
import Effect.Aff.Compat as AC
import Effect.Exception as Exn
Expand All @@ -56,6 +57,7 @@ type Request a =
, password :: Maybe String
, withCredentials :: Boolean
, responseFormat :: ResponseFormat.ResponseFormat a
, timeout :: Maybe Milliseconds
}

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

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

extractContent :: RequestBody.RequestBody -> Either String Foreign
Expand Down Expand Up @@ -251,6 +255,7 @@ type AjaxRequest a =
, username :: Nullable String
, password :: Nullable String
, withCredentials :: Boolean
, timeout :: Number
}

foreign import _ajax
Expand Down
10 changes: 10 additions & 0 deletions test/Main.js
Expand Up @@ -39,6 +39,16 @@ exports.startServer = function (errback, callback) {
});
});

app.get('/slow', function(req, res) {
var date = Date.now();
var currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < 2000);
res.header({'content-type': 'text/plain'});
res.send('I hope I am not late!');
});

var server = app.listen(function () {
callback({ port: server.address().port, server: server });
});
Expand Down
7 changes: 7 additions & 0 deletions test/Main.purs
Expand Up @@ -10,6 +10,7 @@ import Control.Monad.Error.Class (throwError)
import Data.Argonaut.Core as J
import Data.Either (Either(..), either)
import Data.Maybe (Maybe(..))
import Data.Time.Duration (Milliseconds(..))
import Effect (Effect)
import Effect.Aff (Aff, finally, forkAff, killFiber, runAff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
Expand Down Expand Up @@ -62,6 +63,7 @@ main = void $ runAff (either (\e -> logShow e *> throwException e) (const $ log
let mirror = prefix "/mirror"
let doesNotExist = prefix "/does-not-exist"
let notJson = prefix "/not-json"
let slow = prefix "/slow"

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

A.log "GET /slow with timeout: should return an error"
(AX.request $ AX.defaultRequest { url = slow, timeout = Just (Milliseconds 100.0) }) >>= assertLeft >>= case _ of
AX.XHRError _ → pure unit
other → logAny' other *> assertFail "Expected a XHRError"

A.log "POST /mirror: should use the POST method"
AX.post ResponseFormat.json mirror (Just (RequestBody.string "test")) >>= assertRight >>= \res -> do
assertEq ok200 res.status
Expand Down

0 comments on commit 8e1bd69

Please sign in to comment.