@@ -3,6 +3,7 @@
33module Reflex.Dom.Xhr
44 ( module Reflex.Dom.Xhr
55 , XMLHttpRequest
6+ , XhrResponseBody(..)
67 , responseTextToText
78 , xmlHttpRequestGetReadyState
89 , xmlHttpRequestGetResponseText
@@ -49,18 +50,44 @@ data XhrRequestConfig
4950 = XhrRequestConfig { _xhrRequestConfig_headers :: Map String String
5051 , _xhrRequestConfig_user :: Maybe String
5152 , _xhrRequestConfig_password :: Maybe String
52− , _xhrRequestConfig_responseType :: Maybe String
53+ , _xhrRequestConfig_responseType :: Maybe XhrResponseType
5354 , _xhrRequestConfig_sendData :: Maybe String
5455 }
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 }
64+ deriving (Eq, Typeable)
65+
66+data XhrResponseType = XhrResponseType_ArrayBuffer
67+ | XhrResponseType_Blob
68+ | XhrResponseType_Document
69+ | XhrResponseType_JSON
70+ | XhrResponseType_Text
71+ | XhrResponseType_Other String
6272 deriving (Show, Read, Eq, Ord, Typeable)
6373
74+responseTypeString :: XhrResponseType -> String
75+responseTypeString rt = case rt of
76+ XhrResponseType_ArrayBuffer -> "arraybuffer"
77+ XhrResponseType_Blob -> "blob"
78+ XhrResponseType_Document -> "document"
79+ XhrResponseType_JSON -> "json"
80+ XhrResponseType_Text -> "text"
81+ XhrResponseType_Other r -> r
82+
83+{-# DEPRECATED _xhrResponse_body "Use _xhrResponse_response or _xhrResponse_responseText instead." #-}
84+_xhrResponse_body :: XhrResponse -> Maybe Text
85+_xhrResponse_body = _xhrResponse_responseText
86+
87+{-# DEPRECATED xhrResponse_body "Use xhrResponse_response or xhrResponse_responseText instead." #-}
88+xhrResponse_body :: Lens XhrResponse XhrResponse (Maybe Text) (Maybe Text)
89+xhrResponse_body = lens _xhrResponse_responseText (\r t -> r { _xhrResponse_responseText = t })
90+
6491instance Default XhrRequestConfig where
6592 def = XhrRequestConfig { _xhrRequestConfig_headers = Map.empty
6693 , _xhrRequestConfig_user = Nothing
@@ -80,6 +107,7 @@ newXMLHttpRequest req cb = do
80107 liftIO $ do
81108 xhr <- xmlHttpRequestNew wv
82109 let c = _xhrRequest_config req
110+ rt = _xhrRequestConfig_responseType c
83111 xmlHttpRequestOpen
84112 xhr
85113 (_xhrRequest_method req)
@@ -88,15 +116,22 @@ newXMLHttpRequest req cb = do
88116 (fromMaybe "" $ _xhrRequestConfig_user c)
89117 (fromMaybe "" $ _xhrRequestConfig_password c)
90118 iforM_ (_xhrRequestConfig_headers c) $ xmlHttpRequestSetRequestHeader xhr
91− maybe (return ()) (xmlHttpRequestSetResponseType xhr . toResponseType) (_xhrRequestConfig_responseType c)
119+ maybe (return ()) (xmlHttpRequestSetResponseType xhr . toResponseType . responseTypeString) rt
92120 _ <- xmlHttpRequestOnreadystatechange xhr $ do
93121 readyState <- liftIO $ xmlHttpRequestGetReadyState xhr
94122 status <- liftIO $ xmlHttpRequestGetStatus xhr
95123 statusText <- liftIO $ xmlHttpRequestGetStatusText xhr
96124 if readyState == 4
97125 then do
98− r <- liftIO $ xmlHttpRequestGetResponseText xhr
99− _ <- liftIO $ postGui $ cb $ XhrResponse (responseTextToText r) status (statusTextToText statusText)
126+ t <- if rt == Just XhrResponseType_Text || rt == Nothing
127+ then liftIO $ xmlHttpRequestGetResponseText xhr
128+ else return Nothing
129+ r <- liftIO $ xmlHttpRequestGetResponse xhr
130+ _ <- liftIO $ postGui $ cb $ XhrResponse { _xhrResponse_status = status
131+ , _xhrResponse_statusText = statusTextToText statusText
132+ , _xhrResponse_response = r
133+ , _xhrResponse_responseText = responseTextToText t
134+ }
100135 return ()
101136 else return ()
102137 _ <- xmlHttpRequestSend xhr (_xhrRequestConfig_sendData c)
@@ -143,5 +178,5 @@ decodeText = decode . BL.fromStrict . encodeUtf8
143178
144179-- | Convenience function to decode JSON-encoded responses.
145180decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
146−decodeXhrResponse = join . fmap decodeText . _xhrResponse_body
181+decodeXhrResponse = join . fmap decodeText . _xhrResponse_responseText
147182