@@ -67,15 +67,16 @@ data QueryPart t = QueryPartParam (Behavior t [String])
6767 | QueryPartFlag (Behavior t Bool)
6868
6969data Req t = Req
70− { reqPathParts :: [Behavior t (Maybe String)]
70+ { reqMethod :: String
71+ , reqPathParts :: [Behavior t (Maybe String)]
7172 , qParams :: [(String, QueryPart t)]
72− , reqBody :: Behavior t (Maybe (BL.ByteString, String))
73+ , reqBody :: Maybe (Behavior t (Maybe (BL.ByteString, String)))
7374 -- , reqAccept :: [MediaType] -- TODO ?
7475 , headers :: [(String, Behavior t String)]
7576 }
7677
7778defReq :: Reflex t => Req t
78−defReq = Req [] [] (constant Nothing) []
79+defReq = Req "GET" [] [] Nothing []
7980
8081prependToPathParts :: Reflex t => Behavior t (Maybe String) -> Req t -> Req t
8182prependToPathParts p req =
@@ -117,22 +118,27 @@ performRequest reqMethod req _ trigger = do
117118
118119 queryPartStrings = map queryPartString (qParams req)
119120 queryPartStrings' = sequence queryPartStrings :: Behavior t [Maybe String]
120− queryString :: Behavior t (Maybe String ) =
121+ queryString :: Behavior t (Maybe String) =
121122 ffor queryPartStrings' $ \qs -> Just (L.intercalate "&" (catMaybes qs))
122− xhrUrl = (liftA2 . liftA2) (\p q -> p ++ '?' : q) urlPath queryString
123+ xhrUrl = (liftA2 . liftA2) (\p q -> if null q then p else p ++ '?' : q) urlPath queryString
123124
124125 xhrHeaders :: Behavior t [(String, String)]
125126 xhrHeaders = sequence $ ffor (headers req) $ \(hName, hVal) -> fmap (hName,) hVal
126127
127128
128− mkConfig hs rb = case rb of
129− Nothing -> def { _xhrRequestConfig_headers = Map.fromList hs }
130− (Just (bBytes, bCT)) -> def { _xhrRequestConfig_sendData = Just (BL.unpack bBytes)
131− , _xhrRequestConfig_headers =
132− Map.insert "Content-Type" bCT (_xhrRequestConfig_headers def)}
129+ mkConfigBody :: [(String,String)] -> (Maybe (BL.ByteString, String)) -> Maybe XhrRequestConfig
130+ mkConfigBody hs rb = case rb of
131+ Nothing -> Nothing
132+ (Just (bBytes, bCT)) ->
133+ Just $ def { _xhrRequestConfig_sendData = Just (BL.unpack bBytes)
134+ , _xhrRequestConfig_headers =
135+ Map.insert "Content-Type" bCT (_xhrRequestConfig_headers def)}
133136
134− xhrOpts = liftA2 mkConfig xhrHeaders (reqBody req)
135− xhrReq = (fmap . fmap) (\p -> XhrRequest reqMethod p def) xhrUrl
137+ xhrOpts :: Behavior t (Maybe XhrRequestConfig)
138+ xhrOpts = case reqBody req of
139+ Nothing -> fmap (\h -> Just $ def { _xhrRequestConfig_headers = Map.fromList h }) xhrHeaders
140+ Just rBody -> liftA2 mkConfigBody xhrHeaders rBody
141+ xhrReq = (liftA2 . liftA2) (\p opt -> XhrRequest reqMethod p opt) xhrUrl xhrOpts
136142
137143 performRequestAsync (fmapMaybe id $ tag xhrReq trigger)
138144