@@ -1,18 +1,14 @@
1{-# LANGUAGE AllowAmbiguousTypes #-}
1{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DataKinds #-}
4−{-# LANGUAGE DefaultSignatures #-}
5−{-# LANGUAGE ExtendedDefaultRules #-}
6{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
7{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
8−{-# LANGUAGE FunctionalDependencies #-}
9{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10{-# LANGUAGE InstanceSigs #-}
7{-# LANGUAGE InstanceSigs #-}
11−{-# LANGUAGE LambdaCase #-}
12{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
13{-# LANGUAGE OverloadedStrings #-}
9{-# LANGUAGE OverloadedStrings #-}
14{-# LANGUAGE PolyKinds #-}
10{-# LANGUAGE PolyKinds #-}
15−{-# LANGUAGE RecordWildCards #-}
11+{-# LANGUAGE Rank2Types #-}
16{-# LANGUAGE ScopedTypeVariables #-}
12{-# LANGUAGE ScopedTypeVariables #-}
17{-# LANGUAGE TupleSections #-}
13{-# LANGUAGE TupleSections #-}
18{-# LANGUAGE TypeFamilies #-}
14{-# LANGUAGE TypeFamilies #-}
@@ -30,9 +26,10 @@
30module Servant.Reflex
26module Servant.Reflex
32 , clientWithOpts
28 , clientWithOpts
29+ , clientWithOptsAndResultHandler
33 , clientWithRoute
30 , clientWithRoute
31+ , clientWithRouteAndResultHandler
34 , BuildHeaderKeysTo(..)
32 , BuildHeaderKeysTo(..)
35− , GHCJS'MimeRender(..)
36 , toHeaders
33 , toHeaders
37 , HasClient
34 , HasClient
@@ -42,56 +39,56 @@ module Servant.Reflex
43------------------------------------------------------------------------------
40------------------------------------------------------------------------------
44import Control.Applicative
41import Control.Applicative
45−import qualified Data.ByteString as BS
42+import qualified Data.CaseInsensitive as CI
46−import qualified Data.ByteString.Lazy as BL
47−import Data.CaseInsensitive (mk)
48import Data.Functor.Identity
43import Data.Functor.Identity
49−import Data.Kind (Type)
50−import qualified Data.Map as Map
44+import qualified Data.Map as Map
51−import Data.Monoid ((<>))
45+import Data.Monoid ((<>))
52−import Data.Proxy (Proxy (..))
46+import Data.Proxy (Proxy (..))
53−import qualified Data.Set as Set
47+import qualified Data.Set as Set
54−import Data.Text (Text)
48+import Data.Text (Text)
55−import qualified Data.Text as T
49+import qualified Data.Text as T
56−import qualified Data.Text.Encoding as E
50+import qualified Data.Text.Encoding as E
57−import qualified Data.Text.Lazy as TL
58−import GHC.Exts (Constraint)
51+import GHC.Exts (Constraint)
59−import GHC.TypeLits (KnownSymbol, symbolVal)
52+import GHC.TypeLits (KnownSymbol, symbolVal)
60−import GHCJS.DOM.Types (Blob)
61−import Servant.API ((:<|>) (..), (:>), Accept (..),
53+import Servant.API ((:<|>) (..), (:>), BasicAuth,
62− BasicAuth, BasicAuthData,
63− BuildHeadersTo (..), Capture,
54+ BasicAuthData, BuildHeadersTo (..),
64− FormUrlEncoded, Header, Headers (..),
55+ Capture, Header, Headers (..),
65− HttpVersion, IsSecure, JSON,
56+ HttpVersion, IsSecure,
66− MimeRender (..), NoContent,
57+ MimeRender (..), MimeUnrender,
67− OctetStream, PlainText, QueryFlag,
58+ NoContent, QueryFlag, QueryParam,
68− QueryParam, QueryParams, Raw,
59+ QueryParams, Raw, ReflectMethod (..),
69− ReflectMethod (..), RemoteHost,
60+ RemoteHost, ReqBody,
70− ReqBody, ToHttpApiData (..), Vault,
61+ ToHttpApiData (..), Vault, Verb,
71− Verb, contentType)
62+ contentType)
63+import Servant.API.Description (Summary)
72−import qualified Servant.Auth as Auth
64+import qualified Servant.Auth as Auth
74−import Reflex.Dom.Core (Dynamic, Event, IsXhrPayload, Reflex,
66+import Reflex.Dom.Core (Dynamic, Event, Reflex,
75− XhrRequest (..), XhrResponse (..),
67+ XhrRequest (..), XhrResponse (..),
76− XhrResponseHeaders (..),
68+ XhrResponseHeaders (..),
77− attachPromptlyDynWith, constDyn, ffor,
69+ attachPromptlyDynWith, constDyn, ffor,
78− fmapMaybe, leftmost,
70+ fmapMaybe, leftmost,
79− performRequestsAsync)
71+ performRequestsAsync)
80------------------------------------------------------------------------------
72------------------------------------------------------------------------------
81−import Servant.Common.BaseUrl (BaseUrl (..), Scheme (..),
73+import Servant.Common.BaseUrl (BaseUrl(..), Scheme(..), baseUrlWidget,
82− SupportsServantReflex, baseUrlWidget,
83− showBaseUrl)
74+ showBaseUrl,
75+ SupportsServantReflex)
84−import Servant.Common.Req (ClientOptions (..), MimeUnrender (..),
76+import Servant.Common.Req (ClientOptions(..),
85− QParam (..), QueryPart (..), Req (..),
77+ defaultClientOptions,
78+ Req, ReqResult(..), QParam(..),
86− ReqResult (..), addHeader, authData,
79+ QueryPart(..), addHeader, authData,
87− defReq, defaultClientOptions,
80+ defReq, evalResponse, prependToPathParts,
88− evalResponse, performRequestsCT,
82+ performRequestsCT,
83+ -- performRequestNoBody,
89− performRequestsNoBody,
84+ performRequestsNoBody,
90− performSomeRequestsAsync,
85+ performSomeRequestsAsync,
91− prependToPathParts, qParamToQueryPart,
86+ qParamToQueryPart, reqBody,
92− qParams, reqBody, reqFailure,
87+ reqSuccess, reqFailure,
93− reqMethod, reqSuccess, reqTag,
88+ reqMethod, respHeaders,
94− respHeaders, response, withCredentials)
91+ qParams, withCredentials)
97-- * Accessing APIs as a Client
94-- * Accessing APIs as a Client
@@ -106,7 +103,7 @@ import Servant.Common.Req (ClientOptions (..), MimeUnrender (..),
107-- > getAllBooks :: Event t l -> m (Event t (l, ReqResult [Book]))
104-- > getAllBooks :: Event t l -> m (Event t (l, ReqResult [Book]))
108-- > postNewBook :: Dynamic t (Maybe Book) -> Event t l
105-- > postNewBook :: Dynamic t (Maybe Book) -> Event t l
109−-- -> m (Event t (l, ReqResult Book)))
106+-- > -> m (Event t (l, ReqResult Book)))
110-- > (getAllBooks :<|> postNewBook) = client myApi host
107-- > (getAllBooks :<|> postNewBook) = client myApi host
111-- > where host = constDyn $ BaseUrl Http "localhost" 8080
108-- > where host = constDyn $ BaseUrl Http "localhost" 8080
@@ -128,21 +125,53 @@ clientWithOpts
128 -> Client t m layout tag
125 -> Client t m layout tag
129clientWithOpts p q t baseurl = clientWithRoute p q t defReq baseurl
126clientWithOpts p q t baseurl = clientWithRoute p q t defReq baseurl
128+-- | Like 'clientWithOpts' but allows passing a function which will process the
129+-- result event in some way. This can be used to handle errors in a uniform way
130+-- across call sites.
131+clientWithOptsAndResultHandler
132+ :: (HasClient t m layout tag)
136+ -> Dynamic t BaseUrl
138+ -> (forall a. Event t (ReqResult tag a) -> m (Event t (ReqResult tag a)))
139+ -> Client t m layout tag
140+clientWithOptsAndResultHandler p q t = clientWithRouteAndResultHandler p q t defReq
132-- | This class lets us define how each API combinator
143-- | This class lets us define how each API combinator
133-- influences the creation of an HTTP request. It's mostly
144-- influences the creation of an HTTP request. It's mostly
134-- an internal class, you can just use 'client'.
145-- an internal class, you can just use 'client'.
135−class HasClient t m layout (tag :: Type) where
146+class Monad m => HasClient t m layout (tag :: *) where
136− type Client t m layout tag :: Type
147+ type Client t m layout tag :: *
137− clientWithRoute :: Proxy layout -> Proxy m -> Proxy tag -> Req t -> Dynamic t BaseUrl -> ClientOptions -> Client t m layout tag
148+ clientWithRoute
153+ -> Dynamic t BaseUrl
155+ -> Client t m layout tag
156+ clientWithRoute l m t r b o = clientWithRouteAndResultHandler l m t r b o return
158+ clientWithRouteAndResultHandler
163+ -> Dynamic t BaseUrl
165+ -> (forall a. Event t (ReqResult tag a) -> m (Event t (ReqResult tag a)))
166+ -> Client t m layout tag
140instance (HasClient t m a tag, HasClient t m b tag) => HasClient t m (a :<|> b) tag where
169instance (HasClient t m a tag, HasClient t m b tag) => HasClient t m (a :<|> b) tag where
141 type Client t m (a :<|> b) tag = Client t m a tag :<|> Client t m b tag
170 type Client t m (a :<|> b) tag = Client t m a tag :<|> Client t m b tag
143− clientWithRoute Proxy q pTag req baseurl opts =
172+ clientWithRouteAndResultHandler Proxy q pTag req baseurl opts wrap =
144− clientWithRoute (Proxy :: Proxy a) q pTag req baseurl opts :<|>
173+ clientWithRouteAndResultHandler (Proxy :: Proxy a) q pTag req baseurl opts wrap :<|>
145− clientWithRoute (Proxy :: Proxy b) q pTag req baseurl opts
174+ clientWithRouteAndResultHandler (Proxy :: Proxy b) q pTag req baseurl opts wrap
148-- Capture. Example:
177-- Capture. Example:
@@ -163,11 +192,9 @@ instance (SupportsServantReflex t m, ToHttpApiData a, HasClient t m sublayout ta
163 type Client t m (Capture capture a :> sublayout) tag =
192 type Client t m (Capture capture a :> sublayout) tag =
164 Dynamic t (Either Text a) -> Client t m sublayout tag
193 Dynamic t (Either Text a) -> Client t m sublayout tag
166− clientWithRoute Proxy q t req baseurl opts val =
195+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap val =
167− clientWithRoute (Proxy :: Proxy sublayout)
196+ clientWithRouteAndResultHandler
169− (prependToPathParts p req)
197+ (Proxy :: Proxy sublayout) q t (prependToPathParts p req) baseurl opts wrap
171 where p = (fmap . fmap) (toUrlPiece) val
198 where p = (fmap . fmap) (toUrlPiece) val
@@ -180,8 +207,8 @@ instance {-# OVERLAPPABLE #-}
180 Event t tag -> m (Event t (ReqResult tag a))
207 Event t tag -> m (Event t (ReqResult tag a))
181 -- TODO how to access input types here?
208 -- TODO how to access input types here?
182 -- ExceptT ServantError IO a
209 -- ExceptT ServantError IO a
183− clientWithRoute Proxy _ _ req baseurl opts trigs =
210+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs =
184− fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl opts trigs
211+ wrap =<< fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl opts trigs
185 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
212 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
186 req' = req { reqMethod = method }
213 req' = req { reqMethod = method }
@@ -194,29 +221,33 @@ instance {-# OVERLAPPING #-}
194 Event t tag -> m (Event t (ReqResult tag NoContent))
221 Event t tag -> m (Event t (ReqResult tag NoContent))
195 -- TODO: how to access input types here?
222 -- TODO: how to access input types here?
196 -- ExceptT ServantError IO NoContent
223 -- ExceptT ServantError IO NoContent
197− clientWithRoute Proxy _ _ req baseurl opts =
224+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs =
198− (fmap . fmap) runIdentity . performRequestsNoBody method (constDyn $ Identity req) baseurl opts
225+ wrap =<< fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req) baseurl opts trigs
199 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
226 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
202toHeaders :: BuildHeadersTo ls => ReqResult tag a -> ReqResult tag (Headers ls a)
229toHeaders :: BuildHeadersTo ls => ReqResult tag a -> ReqResult tag (Headers ls a)
203toHeaders r =
230toHeaders r =
204− let toBS = E.encodeUtf8
205− hdrs = maybe []
231+ let hdrs = maybe []
206− (\xhr -> fmap (\(h,v) -> (mk (toBS h), toBS v))
232+ (\xhr -> fmap (\(h,v) -> (CI.map E.encodeUtf8 h, E.encodeUtf8 v))
207 (Map.toList $ _xhrResponse_headers xhr))
233 (Map.toList $ _xhrResponse_headers xhr))
208 (response r)
234 (response r)
209 in ffor r $ \a -> Headers {getResponse = a ,getHeadersHList = buildHeadersTo hdrs}
235 in ffor r $ \a -> Headers {getResponse = a ,getHeadersHList = buildHeadersTo hdrs}
211class BuildHeaderKeysTo hs where
238class BuildHeaderKeysTo hs where
212− buildHeaderKeysTo :: Proxy hs -> [T.Text]
239+ buildHeaderKeysTo :: Proxy hs -> [CI.CI T.Text]
214instance {-# OVERLAPPABLE #-} BuildHeaderKeysTo '[]
241instance {-# OVERLAPPABLE #-} BuildHeaderKeysTo '[]
215 where buildHeaderKeysTo _ = []
242 where buildHeaderKeysTo _ = []
217instance {-# OVERLAPPABLE #-} (BuildHeaderKeysTo xs, KnownSymbol h)
244instance {-# OVERLAPPABLE #-} (BuildHeaderKeysTo xs, KnownSymbol h)
218 => BuildHeaderKeysTo ((Header h v) ': xs) where
245 => BuildHeaderKeysTo ((Header h v) ': xs) where
246+ buildHeaderKeysTo _ =
248+ thisKey = CI.mk $ T.pack (symbolVal (Proxy :: Proxy h))
219− buildHeaderKeysTo _ = T.pack (symbolVal (Proxy :: Proxy h)) : buildHeaderKeysTo (Proxy :: Proxy xs)
249+ in thisKey : buildHeaderKeysTo (Proxy :: Proxy xs)
221-- HEADERS Verb (Content) --
252-- HEADERS Verb (Content) --
222-- Headers combinator not treated in fully general case,
253-- Headers combinator not treated in fully general case,
@@ -231,10 +262,10 @@ instance {-# OVERLAPPABLE #-}
231 ) => HasClient t m (Verb method status cts' (Headers ls a)) tag where
262 ) => HasClient t m (Verb method status cts' (Headers ls a)) tag where
232 type Client t m (Verb method status cts' (Headers ls a)) tag =
263 type Client t m (Verb method status cts' (Headers ls a)) tag =
233 Event t tag -> m (Event t (ReqResult tag (Headers ls a)))
264 Event t tag -> m (Event t (ReqResult tag (Headers ls a)))
234− clientWithRoute Proxy _ _ req baseurl opts trigs = do
265+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
235 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
266 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
236 resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl opts trigs
267 resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl opts trigs
237− return $ toHeaders <$> resp
268+ wrap $ toHeaders <$> resp
238 where req' = req { respHeaders =
269 where req' = req { respHeaders =
239 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
270 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
@@ -247,10 +278,10 @@ instance {-# OVERLAPPABLE #-}
247 ) => HasClient t m (Verb method status cts (Headers ls NoContent)) tag where
278 ) => HasClient t m (Verb method status cts (Headers ls NoContent)) tag where
248 type Client t m (Verb method status cts (Headers ls NoContent)) tag
279 type Client t m (Verb method status cts (Headers ls NoContent)) tag
249 = Event t tag -> m (Event t (ReqResult tag (Headers ls NoContent)))
280 = Event t tag -> m (Event t (ReqResult tag (Headers ls NoContent)))
250− clientWithRoute Proxy _ _ req baseurl opts trigs = do
281+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
251 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
282 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
252 resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl opts trigs
283 resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl opts trigs
253− return $ toHeaders <$> resp
284+ wrap $ toHeaders <$> resp
254 where req' = req {respHeaders =
285 where req' = req {respHeaders =
255 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
286 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
@@ -277,11 +308,11 @@ instance (KnownSymbol sym, ToHttpApiData a,
277 type Client t m (Header sym a :> sublayout) tag =
308 type Client t m (Header sym a :> sublayout) tag =
278 Dynamic t (Either Text a) -> Client t m sublayout tag
309 Dynamic t (Either Text a) -> Client t m sublayout tag
280− clientWithRoute Proxy q t req baseurl opts eVal =
311+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap eVal =
312+ clientWithRouteAndResultHandler
281− clientWithRoute (Proxy :: Proxy sublayout)
313+ (Proxy :: Proxy sublayout) q t
283− (Servant.Common.Req.addHeader hname eVal req)
314+ (Servant.Common.Req.addHeader hname eVal req)
284− baseurl opts
315+ baseurl opts wrap
285 where hname = T.pack $ symbolVal (Proxy :: Proxy sym)
316 where hname = T.pack $ symbolVal (Proxy :: Proxy sym)
@@ -295,8 +326,21 @@ instance HasClient t m sublayout tag
295 type Client t m (HttpVersion :> sublayout) tag =
326 type Client t m (HttpVersion :> sublayout) tag =
296 Client t m sublayout tag
327 Client t m sublayout tag
298− clientWithRoute Proxy q t =
329+ clientWithRouteAndResultHandler Proxy =
330+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout)
332+-- | Using a 'Summary' combinator in your API doesn't affect the client
334+instance (HasClient t m sublayout tag, KnownSymbol sym)
335+ => HasClient t m (Summary sym :> sublayout) tag where
337+ type Client t m (Summary sym :> sublayout) tag =
338+ Client t m sublayout tag
340+ clientWithRouteAndResultHandler Proxy =
299− clientWithRoute (Proxy :: Proxy sublayout) q t
341+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout)
302-- | If you use a 'QueryParam' in one of your endpoints in your API,
346-- | If you use a 'QueryParam' in one of your endpoints in your API,
@@ -332,9 +376,9 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
332 Dynamic t (QParam a) -> Client t m sublayout tag
376 Dynamic t (QParam a) -> Client t m sublayout tag
334 -- if mparam = Nothing, we don't add it to the query string
378 -- if mparam = Nothing, we don't add it to the query string
335− clientWithRoute Proxy q t req baseurl opts mparam =
379+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap mparam =
336− clientWithRoute (Proxy :: Proxy sublayout) q t
380+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t
337− (req {qParams = paramPair : qParams req}) baseurl opts
381+ (req {qParams = paramPair : qParams req}) baseurl opts wrap
339 where pname = symbolVal (Proxy :: Proxy sym)
383 where pname = symbolVal (Proxy :: Proxy sym)
340 --p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
384 --p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
@@ -378,8 +422,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
378 type Client t m (QueryParams sym a :> sublayout) tag =
422 type Client t m (QueryParams sym a :> sublayout) tag =
379 Dynamic t [a] -> Client t m sublayout tag
423 Dynamic t [a] -> Client t m sublayout tag
381− clientWithRoute Proxy q t req baseurl opts paramlist =
425+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap paramlist =
382− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
426+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
384 where req' = req { qParams = (T.pack pname, params') : qParams req }
428 where req' = req { qParams = (T.pack pname, params') : qParams req }
385 pname = symbolVal (Proxy :: Proxy sym)
429 pname = symbolVal (Proxy :: Proxy sym)
@@ -418,8 +462,8 @@ instance (KnownSymbol sym, HasClient t m sublayout tag, Reflex t)
418 type Client t m (QueryFlag sym :> sublayout) tag =
462 type Client t m (QueryFlag sym :> sublayout) tag =
419 Dynamic t Bool -> Client t m sublayout tag
463 Dynamic t Bool -> Client t m sublayout tag
421− clientWithRoute Proxy q t req baseurl opts flag =
465+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap flag =
422− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
466+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
424 where req' = req { qParams = thisPair : qParams req }
468 where req' = req { qParams = thisPair : qParams req }
425 thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
469 thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
@@ -433,7 +477,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
433 -> Event t tag
477 -> Event t tag
434 -> m (Event t (ReqResult tag ()))
478 -> m (Event t (ReqResult tag ()))
436− clientWithRoute _ _ _ _ baseurl _ xhrs triggers = do
480+ clientWithRouteAndResultHandler _ _ _ _ baseurl _ wrap xhrs triggers = do
438 let xhrs' = liftA2 (\x path -> case x of
482 let xhrs' = liftA2 (\x path -> case x of
439 Left e -> Left e
483 Left e -> Left e
@@ -444,7 +488,7 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
444 okReq = fmapMaybe (\(t,x) -> either (const Nothing) (Just . (t,)) x) xhrs'' :: Event t (tag, XhrRequest ())
488 okReq = fmapMaybe (\(t,x) -> either (const Nothing) (Just . (t,)) x) xhrs'' :: Event t (tag, XhrRequest ())
446 resps <- performRequestsAsync okReq
490 resps <- performRequestsAsync okReq
447− return $ leftmost [ uncurry RequestFailure <$> badReq
491+ wrap $ leftmost [ uncurry RequestFailure <$> badReq
448 , evalResponse (const $ Right ()) <$> resps
492 , evalResponse (const $ Right ()) <$> resps
@@ -469,73 +513,30 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
469-- > where host = BaseUrl Http "localhost" 8080
513-- > where host = BaseUrl Http "localhost" 8080
470-- > -- then you can just use "addBook" to query that endpoint
514-- > -- then you can just use "addBook" to query that endpoint
472−instance (GHCJS'MimeRender ct a, IsXhrPayload (ToSend ct a), Show (ToSend ct a),
473− HasClient t m sublayout tag, Reflex t)
516+instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
474− => HasClient t m (ReqBody (ct ': cts) (a :: Type) :> sublayout) tag where
517+ => HasClient t m (ReqBody (ct ': cts) a :> sublayout) tag where
476 type Client t m (ReqBody (ct ': cts) a :> sublayout) tag =
519 type Client t m (ReqBody (ct ': cts) a :> sublayout) tag =
477− Dynamic t (Either Text (ToConvert ct a)) -> Client t m sublayout tag
520+ Dynamic t (Either Text a) -> Client t m sublayout tag
479− clientWithRoute Proxy q t Req{..} baseurl opts body =
522+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap body =
480− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl opts
523+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
481− where req' = Req { reqBody = Just $ (fmap . fmap)
524+ where req' = req { reqBody = bodyBytesCT }
482− (\b -> (ghcjsMimeRender ctProxy atProxy b, ctString)) body
484 ctProxy = Proxy :: Proxy ct
525 ctProxy = Proxy :: Proxy ct
485− atProxy = Proxy :: Proxy a
486 ctString = T.pack $ show $ contentType ctProxy
526 ctString = T.pack $ show $ contentType ctProxy
527+ bodyBytesCT = Just $ (fmap . fmap)
528+ (\b -> (mimeRender ctProxy b, ctString))
488−class Accept ctype => GHCJS'MimeRender (ctype :: Type) (a :: Type) where
489− type ToConvert ctype a :: Type
490− type ToConvert ctype a = a
492− type ToSend ctype a :: Type
493− type ToSend ctype a = BS.ByteString
496− :: IsXhrPayload (ToSend ctype a)
497− => Proxy ctype -> Proxy a -> ToConvert ctype a -> ToSend ctype a
498− default ghcjsMimeRender
499− :: (MimeRender ctype a,
500− MimeRender ctype (ToConvert ctype a), ToSend ctype a ~ BS.ByteString)
501− => Proxy ctype -> Proxy a -> ToConvert ctype a -> ToSend ctype a
502− ghcjsMimeRender ctype _ = BL.toStrict . mimeRender ctype
504−instance MimeRender JSON x => GHCJS'MimeRender JSON x
506−instance MimeRender FormUrlEncoded x => GHCJS'MimeRender FormUrlEncoded x
508−instance GHCJS'MimeRender OctetStream BL.ByteString where
509− type ToConvert OctetStream BL.ByteString = Either BL.ByteString Blob
510− type ToSend OctetStream BL.ByteString = Either BS.ByteString Blob
511− ghcjsMimeRender _ _ = \case Left bl -> Left $ BL.toStrict bl; Right x -> Right x
513−instance GHCJS'MimeRender PlainText String where
514− type ToSend PlainText String = Text
515− ghcjsMimeRender _ _ = T.pack
517−instance GHCJS'MimeRender PlainText Text where
518− type ToSend PlainText Text = Text
519− ghcjsMimeRender _ _ = id
521−instance GHCJS'MimeRender PlainText TL.Text where
522− type ToSend PlainText TL.Text = Text
523− ghcjsMimeRender _ _ = TL.toStrict
525−instance GHCJS'MimeRender PlainText BL.ByteString where
526− type ToSend PlainText BL.ByteString = BS.ByteString
527− ghcjsMimeRender _ _ = BL.toStrict
529−instance GHCJS'MimeRender PlainText BS.ByteString where
530− ghcjsMimeRender _ _ = id
532-- | Make the querying function append @path@ to the request path.
533-- | Make the querying function append @path@ to the request path.
533instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient t m (path :> sublayout) tag where
534instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient t m (path :> sublayout) tag where
534 type Client t m (path :> sublayout) tag = Client t m sublayout tag
535 type Client t m (path :> sublayout) tag = Client t m sublayout tag
536− clientWithRoute Proxy q t req baseurl opts =
537+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap =
537− clientWithRoute (Proxy :: Proxy sublayout) q t
538+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t
538− (prependToPathParts (pure (Right $ T.pack p)) req) baseurl opts
539+ (prependToPathParts (pure (Right $ T.pack p)) req) baseurl opts wrap
540 where p = symbolVal (Proxy :: Proxy path)
541 where p = symbolVal (Proxy :: Proxy path)
@@ -543,23 +544,23 @@ instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient
543instance HasClient t m api tag => HasClient t m (Vault :> api) tag where
544instance HasClient t m api tag => HasClient t m (Vault :> api) tag where
544 type Client t m (Vault :> api) tag = Client t m api tag
545 type Client t m (Vault :> api) tag = Client t m api tag
546− clientWithRoute Proxy q t req baseurl =
547+ clientWithRouteAndResultHandler Proxy =
547− clientWithRoute (Proxy :: Proxy api) q t req baseurl
548+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
550instance HasClient t m api tag => HasClient t m (RemoteHost :> api) tag where
551instance HasClient t m api tag => HasClient t m (RemoteHost :> api) tag where
551 type Client t m (RemoteHost :> api) tag = Client t m api tag
552 type Client t m (RemoteHost :> api) tag = Client t m api tag
553− clientWithRoute Proxy q t req baseurl =
554+ clientWithRouteAndResultHandler Proxy =
554− clientWithRoute (Proxy :: Proxy api) q t req baseurl
555+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
558instance HasClient t m api tag => HasClient t m (IsSecure :> api) tag where
559instance HasClient t m api tag => HasClient t m (IsSecure :> api) tag where
559 type Client t m (IsSecure :> api) tag = Client t m api tag
560 type Client t m (IsSecure :> api) tag = Client t m api tag
561− clientWithRoute Proxy q t req baseurl =
562+ clientWithRouteAndResultHandler Proxy =
562− clientWithRoute (Proxy :: Proxy api) q t req baseurl
563+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
565instance (HasClient t m api tag, Reflex t)
566instance (HasClient t m api tag, Reflex t)
@@ -568,8 +569,8 @@ instance (HasClient t m api tag, Reflex t)
568 type Client t m (BasicAuth realm usr :> api) tag = Dynamic t (Maybe BasicAuthData)
569 type Client t m (BasicAuth realm usr :> api) tag = Dynamic t (Maybe BasicAuthData)
569 -> Client t m api tag
570 -> Client t m api tag
571− clientWithRoute Proxy q t req baseurl opts authdata =
572+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap authdata =
572− clientWithRoute (Proxy :: Proxy api) q t req' baseurl opts
573+ clientWithRouteAndResultHandler (Proxy :: Proxy api) q t req' baseurl opts wrap
574 req' = req { authData = Just authdata }
575 req' = req { authData = Just authdata }
@@ -612,7 +613,7 @@ for empty and one for non-empty lists).
612instance (HasCookieAuth auths, HasClient t m api tag) => HasClient t m (Auth.Auth auths a :> api) tag where
613instance (HasCookieAuth auths, HasClient t m api tag) => HasClient t m (Auth.Auth auths a :> api) tag where
614 type Client t m (Auth.Auth auths a :> api) tag = Client t m api tag
615 type Client t m (Auth.Auth auths a :> api) tag = Client t m api tag
615− clientWithRoute Proxy = clientWithRoute (Proxy :: Proxy api)
616+ clientWithRouteAndResultHandler Proxy = clientWithRouteAndResultHandler (Proxy :: Proxy api)
618type family HasCookieAuth xs :: Constraint where
619type family HasCookieAuth xs :: Constraint where
@@ -621,3 +622,4 @@ type family HasCookieAuth xs :: Constraint where
621 HasCookieAuth '[] = CookieAuthNotEnabled
622 HasCookieAuth '[] = CookieAuthNotEnabled
623class CookieAuthNotEnabled
624class CookieAuthNotEnabled