@@ -3,6 +3,7 @@
33module Reflex.Dom.Xhr
44 ( module Reflex.Dom.Xhr
55 , XMLHttpRequest
6+ , XhrResponseBody(..)
67 , responseTextToText
78 , xmlHttpRequestGetReadyState
89 , xmlHttpRequestGetResponseText
@@ -55,11 +56,20 @@ data XhrRequestConfig
5556 deriving (Show, Read, Eq, Ord, Typeable)
5657
5758data XhrResponse
58− = XhrResponse { _xhrResponse_body :: Maybe Text
59− , _xhrResponse_status :: Word
59+ = XhrResponse { _xhrResponse_status :: Word
6060 , _xhrResponse_statusText :: Text
61+ , _xhrResponse_response :: Maybe XhrResponseBody
62+ , _xhrResponse_responseText :: Maybe Text
6163 }
62− deriving (Show, Read, Eq, Ord, Typeable)
64+ deriving (Eq, Typeable)
65+
66+{-# DEPRECATED _xhrResponse_body "Use _xhrResponse_response or _xhrResponse_responseText instead." #-}
67+_xhrResponse_body :: XhrResponse -> Maybe Text
68+_xhrResponse_body = _xhrResponse_responseText
69+
70+{-# DEPRECATED xhrResponse_body "Use xhrResponse_response or xhrResponse_responseText instead." #-}
71+xhrResponse_body :: Lens XhrResponse XhrResponse (Maybe Text) (Maybe Text)
72+xhrResponse_body = lens _xhrResponse_responseText (\r t -> r { _xhrResponse_responseText = t })
6373
6474instance Default XhrRequestConfig where
6575 def = XhrRequestConfig { _xhrRequestConfig_headers = Map.empty
@@ -80,6 +90,7 @@ newXMLHttpRequest req cb = do
8090 liftIO $ do
8191 xhr <- xmlHttpRequestNew wv
8292 let c = _xhrRequest_config req
93+ responseType = _xhrRequestConfig_responseType c
8394 xmlHttpRequestOpen
8495 xhr
8596 (_xhrRequest_method req)
@@ -95,8 +106,15 @@ newXMLHttpRequest req cb = do
95106 statusText <- liftIO $ xmlHttpRequestGetStatusText xhr
96107 if readyState == 4
97108 then do
98− r <- liftIO $ xmlHttpRequestGetResponseText xhr
99− _ <- liftIO $ postGui $ cb $ XhrResponse (responseTextToText r) status (statusTextToText statusText)
109+ t <- if responseType == Just "text" || responseType == Nothing
110+ then liftIO $ xmlHttpRequestGetResponseText xhr
111+ else return Nothing
112+ r <- liftIO $ xmlHttpRequestGetResponse xhr
113+ _ <- liftIO $ postGui $ cb $ XhrResponse { _xhrResponse_status = status
114+ , _xhrResponse_statusText = statusTextToText statusText
115+ , _xhrResponse_response = r
116+ , _xhrResponse_responseText = responseTextToText t
117+ }
100118 return ()
101119 else return ()
102120 _ <- xmlHttpRequestSend xhr (_xhrRequestConfig_sendData c)
@@ -143,5 +161,5 @@ decodeText = decode . BL.fromStrict . encodeUtf8
143161
144162-- | Convenience function to decode JSON-encoded responses.
145163decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
146−decodeXhrResponse = join . fmap decodeText . _xhrResponse_body
164+decodeXhrResponse = join . fmap decodeText . _xhrResponse_responseText
147165