@@ -3,6 +3,7 @@
3module Reflex.Dom.Xhr
3module Reflex.Dom.Xhr
4 ( module Reflex.Dom.Xhr
4 ( module Reflex.Dom.Xhr
5 , XMLHttpRequest
5 , XMLHttpRequest
6 , responseTextToText
7 , responseTextToText
7 , xmlHttpRequestGetReadyState
8 , xmlHttpRequestGetReadyState
8 , xmlHttpRequestGetResponseText
9 , xmlHttpRequestGetResponseText
@@ -49,18 +50,44 @@ data XhrRequestConfig
49 = XhrRequestConfig { _xhrRequestConfig_headers :: Map String String
50 = XhrRequestConfig { _xhrRequestConfig_headers :: Map String String
50 , _xhrRequestConfig_user :: Maybe String
51 , _xhrRequestConfig_user :: Maybe String
51 , _xhrRequestConfig_password :: Maybe String
52 , _xhrRequestConfig_password :: Maybe String
52− , _xhrRequestConfig_responseType :: Maybe String
53+ , _xhrRequestConfig_responseType :: Maybe XhrResponseType
53 , _xhrRequestConfig_sendData :: Maybe String
54 , _xhrRequestConfig_sendData :: Maybe String
55 deriving (Show, Read, Eq, Ord, Typeable)
56 deriving (Show, Read, Eq, Ord, Typeable)
57data XhrResponse
58data XhrResponse
58− = XhrResponse { _xhrResponse_body :: Maybe Text
59− , _xhrResponse_status :: Word
59+ = XhrResponse { _xhrResponse_status :: Word
60 , _xhrResponse_statusText :: Text
60 , _xhrResponse_statusText :: Text
61+ , _xhrResponse_response :: Maybe XhrResponseBody
62+ , _xhrResponse_responseText :: Maybe Text
64+ deriving (Eq, Typeable)
66+data XhrResponseType = XhrResponseType_ArrayBuffer
67+ | XhrResponseType_Blob
68+ | XhrResponseType_Document
69+ | XhrResponseType_JSON
70+ | XhrResponseType_Text
71+ | XhrResponseType_Other String
62 deriving (Show, Read, Eq, Ord, Typeable)
72 deriving (Show, Read, Eq, Ord, Typeable)
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
83+{-# DEPRECATED _xhrResponse_body "Use _xhrResponse_response or _xhrResponse_responseText instead." #-}
84+_xhrResponse_body :: XhrResponse -> Maybe Text
85+_xhrResponse_body = _xhrResponse_responseText
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 })
64instance Default XhrRequestConfig where
91instance Default XhrRequestConfig where
65 def = XhrRequestConfig { _xhrRequestConfig_headers = Map.empty
92 def = XhrRequestConfig { _xhrRequestConfig_headers = Map.empty
66 , _xhrRequestConfig_user = Nothing
93 , _xhrRequestConfig_user = Nothing
@@ -80,6 +107,7 @@ newXMLHttpRequest req cb = do
80 liftIO $ do
107 liftIO $ do
81 xhr <- xmlHttpRequestNew wv
108 xhr <- xmlHttpRequestNew wv
82 let c = _xhrRequest_config req
109 let c = _xhrRequest_config req
110+ rt = _xhrRequestConfig_responseType c
83 xmlHttpRequestOpen
111 xmlHttpRequestOpen
85 (_xhrRequest_method req)
113 (_xhrRequest_method req)
@@ -88,15 +116,22 @@ newXMLHttpRequest req cb = do
88 (fromMaybe "" $ _xhrRequestConfig_user c)
116 (fromMaybe "" $ _xhrRequestConfig_user c)
89 (fromMaybe "" $ _xhrRequestConfig_password c)
117 (fromMaybe "" $ _xhrRequestConfig_password c)
90 iforM_ (_xhrRequestConfig_headers c) $ xmlHttpRequestSetRequestHeader xhr
118 iforM_ (_xhrRequestConfig_headers c) $ xmlHttpRequestSetRequestHeader xhr
91− maybe (return ()) (xmlHttpRequestSetResponseType xhr . toResponseType) (_xhrRequestConfig_responseType c)
119+ maybe (return ()) (xmlHttpRequestSetResponseType xhr . toResponseType . responseTypeString) rt
92 _ <- xmlHttpRequestOnreadystatechange xhr $ do
120 _ <- xmlHttpRequestOnreadystatechange xhr $ do
93 readyState <- liftIO $ xmlHttpRequestGetReadyState xhr
121 readyState <- liftIO $ xmlHttpRequestGetReadyState xhr
94 status <- liftIO $ xmlHttpRequestGetStatus xhr
122 status <- liftIO $ xmlHttpRequestGetStatus xhr
95 statusText <- liftIO $ xmlHttpRequestGetStatusText xhr
123 statusText <- liftIO $ xmlHttpRequestGetStatusText xhr
96 if readyState == 4
124 if readyState == 4
126+ t <- if rt == Just XhrResponseType_Text || rt == Nothing
98− r <- liftIO $ xmlHttpRequestGetResponseText xhr
127+ then liftIO $ xmlHttpRequestGetResponseText xhr
129+ r <- liftIO $ xmlHttpRequestGetResponse xhr
99− _ <- liftIO $ postGui $ cb $ XhrResponse (responseTextToText r) status (statusTextToText statusText)
130+ _ <- liftIO $ postGui $ cb $ XhrResponse { _xhrResponse_status = status
131+ , _xhrResponse_statusText = statusTextToText statusText
132+ , _xhrResponse_response = r
133+ , _xhrResponse_responseText = responseTextToText t
100 return ()
135 return ()
101 else return ()
136 else return ()
102 _ <- xmlHttpRequestSend xhr (_xhrRequestConfig_sendData c)
137 _ <- xmlHttpRequestSend xhr (_xhrRequestConfig_sendData c)
@@ -143,5 +178,5 @@ decodeText = decode . BL.fromStrict . encodeUtf8
144-- | Convenience function to decode JSON-encoded responses.
179-- | Convenience function to decode JSON-encoded responses.
145decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
180decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
146−decodeXhrResponse = join . fmap decodeText . _xhrResponse_body
181+decodeXhrResponse = join . fmap decodeText . _xhrResponse_responseText