@@ -3,6 +3,8 @@
3module Reflex.Dom.Xhr
3module Reflex.Dom.Xhr
4 ( module Reflex.Dom.Xhr
4 ( module Reflex.Dom.Xhr
5 , XMLHttpRequest
5 , XMLHttpRequest
6 , responseTextToText
8 , responseTextToText
7 , xmlHttpRequestGetReadyState
9 , xmlHttpRequestGetReadyState
8 , xmlHttpRequestGetResponseText
10 , xmlHttpRequestGetResponseText
@@ -36,6 +38,7 @@ import Data.Traversable
36import Reflex
38import Reflex
37import Reflex.Dom.Class
39import Reflex.Dom.Class
38import Reflex.Dom.Xhr.Foreign
40import Reflex.Dom.Xhr.Foreign
41+import Reflex.Dom.Xhr.ResponseType
39import Data.Typeable
42import Data.Typeable
41data XhrRequest
44data XhrRequest
@@ -49,17 +52,26 @@ data XhrRequestConfig
49 = XhrRequestConfig { _xhrRequestConfig_headers :: Map String String
52 = XhrRequestConfig { _xhrRequestConfig_headers :: Map String String
50 , _xhrRequestConfig_user :: Maybe String
53 , _xhrRequestConfig_user :: Maybe String
51 , _xhrRequestConfig_password :: Maybe String
54 , _xhrRequestConfig_password :: Maybe String
52− , _xhrRequestConfig_responseType :: Maybe XMLHttpRequestResponseType
55+ , _xhrRequestConfig_responseType :: Maybe XhrResponseType
53 , _xhrRequestConfig_sendData :: Maybe String
56 , _xhrRequestConfig_sendData :: Maybe String
55 deriving (Show, Read, Eq, Ord, Typeable)
58 deriving (Show, Read, Eq, Ord, Typeable)
57data XhrResponse
60data XhrResponse
58− = XhrResponse { _xhrResponse_body :: Maybe Text
59− , _xhrResponse_status :: Word
61+ = XhrResponse { _xhrResponse_status :: Word
60 , _xhrResponse_statusText :: Text
62 , _xhrResponse_statusText :: Text
63+ , _xhrResponse_response :: Maybe XhrResponseBody
64+ , _xhrResponse_responseText :: Maybe Text
62− deriving (Show, Read, Eq, Ord, Typeable)
66+ deriving (Typeable)
68+{-# DEPRECATED _xhrResponse_body "Use _xhrResponse_response or _xhrResponse_responseText instead." #-}
69+_xhrResponse_body :: XhrResponse -> Maybe Text
70+_xhrResponse_body = _xhrResponse_responseText
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 })
64instance Default XhrRequestConfig where
76instance Default XhrRequestConfig where
65 def = XhrRequestConfig { _xhrRequestConfig_headers = Map.empty
77 def = XhrRequestConfig { _xhrRequestConfig_headers = Map.empty
@@ -80,6 +92,7 @@ newXMLHttpRequest req cb = do
80 liftIO $ do
92 liftIO $ do
81 xhr <- xmlHttpRequestNew wv
93 xhr <- xmlHttpRequestNew wv
82 let c = _xhrRequest_config req
94 let c = _xhrRequest_config req
95+ rt = _xhrRequestConfig_responseType c
83 xmlHttpRequestOpen
96 xmlHttpRequestOpen
85 (_xhrRequest_method req)
98 (_xhrRequest_method req)
@@ -88,15 +101,22 @@ newXMLHttpRequest req cb = do
88 (fromMaybe "" $ _xhrRequestConfig_user c)
101 (fromMaybe "" $ _xhrRequestConfig_user c)
89 (fromMaybe "" $ _xhrRequestConfig_password c)
102 (fromMaybe "" $ _xhrRequestConfig_password c)
90 iforM_ (_xhrRequestConfig_headers c) $ xmlHttpRequestSetRequestHeader xhr
103 iforM_ (_xhrRequestConfig_headers c) $ xmlHttpRequestSetRequestHeader xhr
91− maybe (return ()) (xmlHttpRequestSetResponseType xhr . toResponseType) (_xhrRequestConfig_responseType c)
104+ maybe (return ()) (xmlHttpRequestSetResponseType xhr . toResponseType) rt
92 _ <- xmlHttpRequestOnreadystatechange xhr $ do
105 _ <- xmlHttpRequestOnreadystatechange xhr $ do
93 readyState <- liftIO $ xmlHttpRequestGetReadyState xhr
106 readyState <- liftIO $ xmlHttpRequestGetReadyState xhr
94 status <- liftIO $ xmlHttpRequestGetStatus xhr
107 status <- liftIO $ xmlHttpRequestGetStatus xhr
95 statusText <- liftIO $ xmlHttpRequestGetStatusText xhr
108 statusText <- liftIO $ xmlHttpRequestGetStatusText xhr
96 if readyState == 4
109 if readyState == 4
111+ t <- if rt == Just XhrResponseType_Text || rt == Nothing
98− r <- liftIO $ xmlHttpRequestGetResponseText xhr
112+ then liftIO $ xmlHttpRequestGetResponseText xhr
114+ r <- liftIO $ xmlHttpRequestGetResponse xhr
99− _ <- liftIO $ postGui $ cb $ XhrResponse (responseTextToText r) status (statusTextToText statusText)
115+ _ <- liftIO $ postGui $ cb $ XhrResponse { _xhrResponse_status = status
116+ , _xhrResponse_statusText = statusTextToText statusText
117+ , _xhrResponse_response = r
118+ , _xhrResponse_responseText = responseTextToText t
100 return ()
120 return ()
101 else return ()
121 else return ()
102 _ <- xmlHttpRequestSend xhr (_xhrRequestConfig_sendData c)
122 _ <- xmlHttpRequestSend xhr (_xhrRequestConfig_sendData c)
@@ -143,5 +163,5 @@ decodeText = decode . BL.fromStrict . encodeUtf8
144-- | Convenience function to decode JSON-encoded responses.
164-- | Convenience function to decode JSON-encoded responses.
145decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
165decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
146−decodeXhrResponse = join . fmap decodeText . _xhrResponse_body
166+decodeXhrResponse = join . fmap decodeText . _xhrResponse_responseText