@@ -18,8 +18,6 @@ import Data.Proxy (Proxy(..))
18import Data.Text (Text)
18import Data.Text (Text)
19import qualified Data.Text as T
19import qualified Data.Text as T
20import qualified Data.Text.Encoding as TE
20import qualified Data.Text.Encoding as TE
21−import qualified Data.Text.Lazy.Encoding as LTE
22−import qualified Data.Text.Lazy as LT
23import Reflex
21import Reflex
24import Reflex.Dom
22import Reflex.Dom
25import Servant.Common.BaseUrl
23import Servant.Common.BaseUrl
@@ -69,15 +67,15 @@ data Req t = Req
69 , authData :: Maybe (Dynamic t (Maybe BasicAuthData))
67 , authData :: Maybe (Dynamic t (Maybe BasicAuthData))
72−defReq :: Reflex t => Req t
70+defReq :: Req t
73defReq = Req "GET" [] [] Nothing [] Nothing
71defReq = Req "GET" [] [] Nothing [] Nothing
75−prependToPathParts :: Reflex t => Dynamic t (Either Text Text) -> Req t -> Req t
73+prependToPathParts :: Dynamic t (Either Text Text) -> Req t -> Req t
76prependToPathParts p req =
74prependToPathParts p req =
77 req { reqPathParts = p : reqPathParts req }
75 req { reqPathParts = p : reqPathParts req }
79addHeader :: (ToHttpApiData a, Reflex t) => Text -> Dynamic t (Either Text a) -> Req t -> Req t
77addHeader :: (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 }
83-- * performing requests
81-- * performing requests
@@ -144,26 +142,33 @@ performRequest reqMeth req reqHost trigger = do
145 mkConfigBody :: Either Text [(Text,Text)]
143 mkConfigBody :: Either Text [(Text,Text)]
146 -> (Either Text (BL.ByteString, Text))
144 -> (Either Text (BL.ByteString, Text))
147− -> Either Text (XhrRequestConfig LT.Text)
145+ -> Either Text (XhrRequestConfig XhrPayload)
148 mkConfigBody ehs rb = case (ehs, rb) of
146 mkConfigBody ehs rb = case (ehs, rb) of
149 (_, Left e) -> Left e
147 (_, Left e) -> Left e
150 (Left e, _) -> Left e
148 (Left e, _) -> Left e
151 (Right hs, Right (bBytes, bCT)) ->
149 (Right hs, Right (bBytes, bCT)) ->
152 Right $ XhrRequestConfig
150 Right $ XhrRequestConfig
153− { _xhrRequestConfig_sendData = Just (BL.unpack bBytes)
151+ { _xhrRequestConfig_sendData = bytesToPayload bBytes
154 , _xhrRequestConfig_headers =
152 , _xhrRequestConfig_headers =
155 Map.insert "Content-Type" bCT (Map.fromList hs)
153 Map.insert "Content-Type" bCT (Map.fromList hs)
154+ , _xhrRequestConfig_user = Nothing
156 , _xhrRequestConfig_password = Nothing
155 , _xhrRequestConfig_password = Nothing
157 , _xhrRequestConfig_responseType = Nothing
156 , _xhrRequestConfig_responseType = Nothing
158 , _xhrRequestConfig_withCredentials = False
157 , _xhrRequestConfig_withCredentials = False
159 , _xhrRequestConfig_responseHeaders = def
158 , _xhrRequestConfig_responseHeaders = def
162− xhrOpts :: Dynamic t (Either Text (XhrRequestConfig LT.Text))
161+ xhrOpts :: Dynamic t (Either Text (XhrRequestConfig XhrPayload))
163 xhrOpts = case reqBody req of
162 xhrOpts = case reqBody req of
164 Nothing -> ffor xhrHeaders $ \case
163 Nothing -> ffor xhrHeaders $ \case
165 Left e -> Left e
164 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
167 Just rBody -> liftA2 mkConfigBody xhrHeaders rBody
172 Just rBody -> liftA2 mkConfigBody xhrHeaders rBody
169 mkAuth :: Maybe BasicAuthData -> Either Text (XhrRequestConfig x) -> Either Text (XhrRequestConfig x)
174 mkAuth :: Maybe BasicAuthData -> Either Text (XhrRequestConfig x) -> Either Text (XhrRequestConfig x)
@@ -181,18 +186,23 @@ performRequest reqMeth req reqHost trigger = do
182 xhrReq = (liftA2 . liftA2) (\p opt -> XhrRequest reqMeth p opt) xhrUrl (addAuth xhrOpts)
187 xhrReq = (liftA2 . liftA2) (\p opt -> XhrRequest reqMeth p opt) xhrUrl (addAuth xhrOpts)
184− let reqs = tagDyn xhrReq trigger
189+ let reqs = tagPromptlyDyn xhrReq trigger
185 okReqs = fmapMaybe (either (const Nothing) Just) reqs
190 okReqs = fmapMaybe (either (const Nothing) Just) reqs
186 badReqs = fmapMaybe (either Just (const Nothing)) reqs
191 badReqs = fmapMaybe (either Just (const Nothing)) reqs
188−#ifndef ghcjs_HOST_OS
189− resps <- performRequestAsync (fmap LT.toStrict <$> okReqs)
191− resps <- performRequestAsync (fmap LT.unpack <$> okReqs)
193+ resps <- performRequestAsync okReqs
194 return (resps, badReqs)
195 return (resps, badReqs)
198+type XhrPayload = String
199+bytesToPayload :: BL.ByteString -> XhrPayload
200+bytesToPayload = BL.unpack
202+type XhrPayload = T.Text
203+bytesToPayload :: BL.ByteString -> XhrPayload
204+bytesToPayload = T.pack . BL.unpack
197-- TODO implement
207-- TODO implement
198-- => String -> Req -> BaseUrl -> ExceptT ServantError IO [HTTP.Header]
208-- => String -> Req -> BaseUrl -> ExceptT ServantError IO [HTTP.Header]