@@ -18,8 +18,6 @@ import Data.Proxy (Proxy(..))
1818import Data.Text (Text)
1919import qualified Data.Text as T
2020import qualified Data.Text.Encoding as TE
21−import qualified Data.Text.Lazy.Encoding as LTE
22−import qualified Data.Text.Lazy as LT
2321import Reflex
2422import Reflex.Dom
2523import Servant.Common.BaseUrl
@@ -69,15 +67,15 @@ data Req t = Req
6967 , authData :: Maybe (Dynamic t (Maybe BasicAuthData))
7068 }
7169
72−defReq :: Reflex t => Req t
70+defReq :: Req t
7371defReq = Req "GET" [] [] Nothing [] Nothing
7472
75−prependToPathParts :: Reflex t => Dynamic t (Either Text Text) -> Req t -> Req t
73+prependToPathParts :: Dynamic t (Either Text Text) -> Req t -> Req t
7674prependToPathParts p req =
7775 req { reqPathParts = p : reqPathParts req }
7876
7977addHeader :: (ToHttpApiData a, Reflex t) => Text -> Dynamic t (Either Text a) -> Req t -> Req t
80−addHeader name val req = req { headers = (name, (fmap . fmap) toHeader val) : headers req }
78+addHeader name val req = req { headers = (name, (fmap . fmap) (TE.decodeUtf8 . toHeader) val) : headers req }
8179
8280
8381-- * performing requests
@@ -144,26 +142,33 @@ performRequest reqMeth req reqHost trigger = do
144142
145143 mkConfigBody :: Either Text [(Text,Text)]
146144 -> (Either Text (BL.ByteString, Text))
147− -> Either Text (XhrRequestConfig LT.Text)
145+ -> Either Text (XhrRequestConfig XhrPayload)
148146 mkConfigBody ehs rb = case (ehs, rb) of
149147 (_, Left e) -> Left e
150148 (Left e, _) -> Left e
151149 (Right hs, Right (bBytes, bCT)) ->
152150 Right $ XhrRequestConfig
153− { _xhrRequestConfig_sendData = Just (BL.unpack bBytes)
151+ { _xhrRequestConfig_sendData = bytesToPayload bBytes
154152 , _xhrRequestConfig_headers =
155153 Map.insert "Content-Type" bCT (Map.fromList hs)
154+ , _xhrRequestConfig_user = Nothing
156155 , _xhrRequestConfig_password = Nothing
157156 , _xhrRequestConfig_responseType = Nothing
158157 , _xhrRequestConfig_withCredentials = False
159158 , _xhrRequestConfig_responseHeaders = def
160159 }
161160
162− xhrOpts :: Dynamic t (Either Text (XhrRequestConfig LT.Text))
161+ xhrOpts :: Dynamic t (Either Text (XhrRequestConfig XhrPayload))
163162 xhrOpts = case reqBody req of
164163 Nothing -> ffor xhrHeaders $ \case
165164 Left e -> Left e
166− Right hs -> Right $ def { _xhrRequestConfig_headers = Map.fromList hs }
165+ Right hs -> Right $ def { _xhrRequestConfig_headers = Map.fromList hs
166+ , _xhrRequestConfig_user = Nothing
167+ , _xhrRequestConfig_password = Nothing
168+ , _xhrRequestConfig_responseType = Nothing
169+ , _xhrRequestConfig_sendData = ""
170+ , _xhrRequestConfig_withCredentials = False
171+ }
167172 Just rBody -> liftA2 mkConfigBody xhrHeaders rBody
168173
169174 mkAuth :: Maybe BasicAuthData -> Either Text (XhrRequestConfig x) -> Either Text (XhrRequestConfig x)
@@ -181,18 +186,23 @@ performRequest reqMeth req reqHost trigger = do
181186
182187 xhrReq = (liftA2 . liftA2) (\p opt -> XhrRequest reqMeth p opt) xhrUrl (addAuth xhrOpts)
183188
184− let reqs = tagDyn xhrReq trigger
189+ let reqs = tagPromptlyDyn xhrReq trigger
185190 okReqs = fmapMaybe (either (const Nothing) Just) reqs
186191 badReqs = fmapMaybe (either Just (const Nothing)) reqs
187192
188−#ifndef ghcjs_HOST_OS
189− resps <- performRequestAsync (fmap LT.toStrict <$> okReqs)
190−#else
191− resps <- performRequestAsync (fmap LT.unpack <$> okReqs)
192−#endif
193+ resps <- performRequestAsync okReqs
193194
194195 return (resps, badReqs)
195196
197+#ifdef ghcjs_HOST_OS
198+type XhrPayload = String
199+bytesToPayload :: BL.ByteString -> XhrPayload
200+bytesToPayload = BL.unpack
201+#else
202+type XhrPayload = T.Text
203+bytesToPayload :: BL.ByteString -> XhrPayload
204+bytesToPayload = T.pack . BL.unpack
205+#endif
196206
197207-- TODO implement
198208-- => String -> Req -> BaseUrl -> ExceptT ServantError IO [HTTP.Header]