@@ -3,6 +3,8 @@
33module Reflex.Dom.Xhr
44 ( module Reflex.Dom.Xhr
55 , XMLHttpRequest
6+ , XhrResponseBody(..)
7+ , XhrResponseType(..)
68 , responseTextToText
79 , xmlHttpRequestGetReadyState
810 , xmlHttpRequestGetResponseText
@@ -36,6 +38,7 @@ import Data.Traversable
3638import Reflex
3739import Reflex.Dom.Class
3840import Reflex.Dom.Xhr.Foreign
41+import Reflex.Dom.Xhr.ResponseType
3942import Data.Typeable
4043
4144data XhrRequest
@@ -49,17 +52,26 @@ data XhrRequestConfig
4952 = XhrRequestConfig { _xhrRequestConfig_headers :: Map String String
5053 , _xhrRequestConfig_user :: Maybe String
5154 , _xhrRequestConfig_password :: Maybe String
52− , _xhrRequestConfig_responseType :: Maybe XMLHttpRequestResponseType
55+ , _xhrRequestConfig_responseType :: Maybe XhrResponseType
5356 , _xhrRequestConfig_sendData :: Maybe String
5457 }
5558 deriving (Show, Read, Eq, Ord, Typeable)
5659
5760data XhrResponse
58− = XhrResponse { _xhrResponse_body :: Maybe Text
59− , _xhrResponse_status :: Word
61+ = XhrResponse { _xhrResponse_status :: Word
6062 , _xhrResponse_statusText :: Text
63+ , _xhrResponse_response :: Maybe XhrResponseBody
64+ , _xhrResponse_responseText :: Maybe Text
6165 }
62− deriving (Show, Read, Eq, Ord, Typeable)
66+ deriving (Typeable)
67+
68+{-# DEPRECATED _xhrResponse_body "Use _xhrResponse_response or _xhrResponse_responseText instead." #-}
69+_xhrResponse_body :: XhrResponse -> Maybe Text
70+_xhrResponse_body = _xhrResponse_responseText
71+
72+{-# DEPRECATED xhrResponse_body "Use xhrResponse_response or xhrResponse_responseText instead." #-}
73+xhrResponse_body :: Lens XhrResponse XhrResponse (Maybe Text) (Maybe Text)
74+xhrResponse_body = lens _xhrResponse_responseText (\r t -> r { _xhrResponse_responseText = t })
6375
6476instance Default XhrRequestConfig where
6577 def = XhrRequestConfig { _xhrRequestConfig_headers = Map.empty
@@ -80,6 +92,7 @@ newXMLHttpRequest req cb = do
8092 liftIO $ do
8193 xhr <- xmlHttpRequestNew wv
8294 let c = _xhrRequest_config req
95+ rt = _xhrRequestConfig_responseType c
8396 xmlHttpRequestOpen
8497 xhr
8598 (_xhrRequest_method req)
@@ -88,15 +101,22 @@ newXMLHttpRequest req cb = do
88101 (fromMaybe "" $ _xhrRequestConfig_user c)
89102 (fromMaybe "" $ _xhrRequestConfig_password c)
90103 iforM_ (_xhrRequestConfig_headers c) $ xmlHttpRequestSetRequestHeader xhr
91− maybe (return ()) (xmlHttpRequestSetResponseType xhr . toResponseType) (_xhrRequestConfig_responseType c)
104+ maybe (return ()) (xmlHttpRequestSetResponseType xhr . toResponseType) rt
92105 _ <- xmlHttpRequestOnreadystatechange xhr $ do
93106 readyState <- liftIO $ xmlHttpRequestGetReadyState xhr
94107 status <- liftIO $ xmlHttpRequestGetStatus xhr
95108 statusText <- liftIO $ xmlHttpRequestGetStatusText xhr
96109 if readyState == 4
97110 then do
98− r <- liftIO $ xmlHttpRequestGetResponseText xhr
99− _ <- liftIO $ postGui $ cb $ XhrResponse (responseTextToText r) status (statusTextToText statusText)
111+ t <- if rt == Just XhrResponseType_Text || rt == Nothing
112+ then liftIO $ xmlHttpRequestGetResponseText xhr
113+ else return Nothing
114+ r <- liftIO $ xmlHttpRequestGetResponse xhr
115+ _ <- liftIO $ postGui $ cb $ XhrResponse { _xhrResponse_status = status
116+ , _xhrResponse_statusText = statusTextToText statusText
117+ , _xhrResponse_response = r
118+ , _xhrResponse_responseText = responseTextToText t
119+ }
100120 return ()
101121 else return ()
102122 _ <- xmlHttpRequestSend xhr (_xhrRequestConfig_sendData c)
@@ -143,5 +163,5 @@ decodeText = decode . BL.fromStrict . encodeUtf8
143163
144164-- | Convenience function to decode JSON-encoded responses.
145165decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
146−decodeXhrResponse = join . fmap decodeText . _xhrResponse_body
166+decodeXhrResponse = join . fmap decodeText . _xhrResponse_responseText
147167