@@ -1,24 +1,23 @@
1−{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE CPP #-}
3−{-# LANGUAGE DataKinds #-}
4−{-# LANGUAGE FlexibleContexts #-}
5−{-# LANGUAGE FlexibleInstances #-}
6−{-# LANGUAGE InstanceSigs #-}
7−{-# LANGUAGE OverloadedStrings #-}
8−{-# LANGUAGE PolyKinds #-}
9−{-# LANGUAGE ScopedTypeVariables #-}
10−{-# LANGUAGE TupleSections #-}
11−{-# LANGUAGE TypeFamilies #-}
12−{-# LANGUAGE TypeOperators #-}
13−{-# LANGUAGE UndecidableInstances #-}
14−{-# LANGUAGE MultiParamTypeClasses #-}
15−{-# LANGUAGE OverloadedStrings #-}
16−{-# LANGUAGE PolyKinds #-}
17−{-# LANGUAGE ScopedTypeVariables #-}
18−{-# LANGUAGE TupleSections #-}
19−{-# LANGUAGE TypeFamilies #-}
20−{-# LANGUAGE TypeOperators #-}
21−{-# LANGUAGE UndecidableInstances #-}
1+{-# LANGUAGE AllowAmbiguousTypes #-}
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE DataKinds #-}
4+{-# LANGUAGE FlexibleContexts #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7+{-# LANGUAGE InstanceSigs #-}
8+{-# LANGUAGE MultiParamTypeClasses #-}
9+{-# LANGUAGE OverloadedStrings #-}
10+{-# LANGUAGE PolyKinds #-}
11+{-# LANGUAGE Rank2Types #-}
12+{-# LANGUAGE ScopedTypeVariables #-}
13+{-# LANGUAGE TupleSections #-}
14+{-# LANGUAGE TypeFamilies #-}
15+{-# LANGUAGE TypeOperators #-}
16+{-# LANGUAGE UndecidableInstances #-}
17+
18+#if MIN_VERSION_base(4,9,0)
19+{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
20+#endif
2221
2322-- #include "overlapping-compat.h"
2423-- | This module provides 'client' which can automatically generate
@@ -26,56 +25,70 @@
2625-- API.
2726module Servant.Reflex
2827 ( client
28+ , clientWithOpts
29+ , clientWithOptsAndResultHandler
30+ , clientWithRoute
31+ , clientWithRouteAndResultHandler
2932 , BuildHeaderKeysTo(..)
3033 , toHeaders
31− , HasClient(..)
34+ , HasClient
35+ , Client
3236 , module Servant.Common.Req
3337 , module Servant.Common.BaseUrl
3438 ) where
3539
3640------------------------------------------------------------------------------
3741import Control.Applicative
42+import qualified Data.CaseInsensitive as CI
43+import Data.Functor.Identity
44+import qualified Data.Map as Map
3845import Data.Monoid ((<>))
39−import qualified Data.Set as Set
40−import qualified Data.Text.Encoding as E
41−import Data.CaseInsensitive (mk)
4246import Data.Proxy (Proxy (..))
43−import qualified Data.Map as Map
47+import qualified Data.Set as Set
4448import Data.Text (Text)
4549import qualified Data.Text as T
50+import qualified Data.Text.Encoding as E
51+import GHC.Exts (Constraint)
4652import GHC.TypeLits (KnownSymbol, symbolVal)
47−import Servant.API ((:<|>)(..),(:>), BasicAuth,
48− BasicAuthData, BuildHeadersTo(..),
49− Capture, contentType, Header,
50− Headers(..), HttpVersion, IsSecure,
51− MimeRender(..), MimeUnrender,
52− NoContent, QueryFlag, QueryParam,
53− QueryParams, Raw, ReflectMethod(..),
53+import Servant.API ((:<|>) (..), (:>), BasicAuth,
54+ BasicAuthData, BuildHeadersTo (..),
55+ Capture, Header, Headers (..),
56+ HttpVersion, IsSecure,
57+ MimeRender (..), MimeUnrender,
58+ NoContent, QueryFlag, QueryParam',
59+ QueryParams, Raw, ReflectMethod (..),
5460 RemoteHost, ReqBody,
55− ToHttpApiData(..), Vault, Verb)
56−
57−import Reflex.Dom (Dynamic, Event, Reflex,
58− XhrRequest(..),
59− XhrResponseHeaders(..),
60− XhrResponse(..), attachPromptlyDynWith, ffor, fmapMaybe,
61− leftmost, performRequestsAsync,
62− )
61+ ToHttpApiData (..), Vault, Verb,
62+ contentType)
63+import Servant.API.Description (Summary)
64+import qualified Servant.Auth as Auth
65+
66+import Reflex.Dom.Core (Dynamic, Event, Reflex,
67+ XhrRequest (..), XhrResponse (..),
68+ XhrResponseHeaders (..),
69+ attachPromptlyDynWith, constDyn, ffor,
70+ fmapMaybe, leftmost,
71+ performRequestsAsync)
6372------------------------------------------------------------------------------
6473import Servant.Common.BaseUrl (BaseUrl(..), Scheme(..), baseUrlWidget,
6574 showBaseUrl,
6675 SupportsServantReflex)
67−import Servant.Common.Req (Req, ReqResult(..), QParam(..),
76+import Servant.Common.Req (ClientOptions(..),
77+ defaultClientOptions,
78+ Req, ReqResult(..), QParam(..),
6879 QueryPart(..), addHeader, authData,
69− defReq, prependToPathParts,
70− performRequestCT,
80+ defReq, evalResponse, prependToPathParts,
81+ -- performRequestCT,
7182 performRequestsCT,
72− performRequestNoBody,
83+ -- performRequestNoBody,
7384 performRequestsNoBody,
7485 performSomeRequestsAsync,
7586 qParamToQueryPart, reqBody,
7687 reqSuccess, reqFailure,
77− reqMethod, respHeaders, response,
78− qParams)
88+ reqMethod, respHeaders,
89+ response,
90+ reqTag,
91+ qParams, withCredentials)
7992
8093
8194-- * Accessing APIs as a Client
@@ -90,28 +103,75 @@ import Servant.Common.Req (Req, ReqResult(..), QParam(..),
90103-- >
91104-- > getAllBooks :: Event t l -> m (Event t (l, ReqResult [Book]))
92105-- > postNewBook :: Dynamic t (Maybe Book) -> Event t l
93−-- -> m (Event t (l, ReqResult Book)))
106+-- > -> m (Event t (l, ReqResult Book)))
94107-- > (getAllBooks :<|> postNewBook) = client myApi host
95108-- > where host = constDyn $ BaseUrl Http "localhost" 8080
96−client :: (HasClient t m layout tag)
97− => Proxy layout -> Proxy m -> Proxy tag -> Dynamic t BaseUrl -> Client t m layout tag
98−client p q t baseurl = clientWithRoute p q t defReq baseurl
109+client
110+ :: (HasClient t m layout tag)
111+ => Proxy layout
112+ -> Proxy m
113+ -> Proxy tag
114+ -> Dynamic t BaseUrl
115+ -> Client t m layout tag
116+client p q t baseurl = clientWithRoute p q t defReq baseurl defaultClientOptions
117+
118+clientWithOpts
119+ :: (HasClient t m layout tag)
120+ => Proxy layout
121+ -> Proxy m
122+ -> Proxy tag
123+ -> Dynamic t BaseUrl
124+ -> ClientOptions
125+ -> Client t m layout tag
126+clientWithOpts p q t baseurl = clientWithRoute p q t defReq baseurl
127+
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)
133+ => Proxy layout
134+ -> Proxy m
135+ -> Proxy tag
136+ -> Dynamic t BaseUrl
137+ -> ClientOptions
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
99141
100142
101143-- | This class lets us define how each API combinator
102144-- influences the creation of an HTTP request. It's mostly
103145-- an internal class, you can just use 'client'.
104−class HasClient t m layout (tag :: *) where
146+class Monad m => HasClient t m layout (tag :: *) where
105147 type Client t m layout tag :: *
106− clientWithRoute :: Proxy layout -> Proxy m -> Proxy tag -> Req t -> Dynamic t BaseUrl -> Client t m layout tag
148+ clientWithRoute
149+ :: Proxy layout
150+ -> Proxy m
151+ -> Proxy tag
152+ -> Req t
153+ -> Dynamic t BaseUrl
154+ -> ClientOptions
155+ -> Client t m layout tag
156+ clientWithRoute l m t r b o = clientWithRouteAndResultHandler l m t r b o return
157+
158+ clientWithRouteAndResultHandler
159+ :: Proxy layout
160+ -> Proxy m
161+ -> Proxy tag
162+ -> Req t
163+ -> Dynamic t BaseUrl
164+ -> ClientOptions
165+ -> (forall a. Event t (ReqResult tag a) -> m (Event t (ReqResult tag a)))
166+ -> Client t m layout tag
107167
108168
109169instance (HasClient t m a tag, HasClient t m b tag) => HasClient t m (a :<|> b) tag where
110170 type Client t m (a :<|> b) tag = Client t m a tag :<|> Client t m b tag
111171
112− clientWithRoute Proxy q pTag req baseurl =
113− clientWithRoute (Proxy :: Proxy a) q pTag req baseurl :<|>
114− clientWithRoute (Proxy :: Proxy b) q pTag req baseurl
172+ clientWithRouteAndResultHandler Proxy q pTag req baseurl opts wrap =
173+ clientWithRouteAndResultHandler (Proxy :: Proxy a) q pTag req baseurl opts wrap :<|>
174+ clientWithRouteAndResultHandler (Proxy :: Proxy b) q pTag req baseurl opts wrap
115175
116176
117177-- Capture. Example:
@@ -132,11 +192,9 @@ instance (SupportsServantReflex t m, ToHttpApiData a, HasClient t m sublayout ta
132192 type Client t m (Capture capture a :> sublayout) tag =
133193 Dynamic t (Either Text a) -> Client t m sublayout tag
134194
135− clientWithRoute Proxy q t req baseurl val =
136− clientWithRoute (Proxy :: Proxy sublayout)
137− q t
138− (prependToPathParts p req)
139− baseurl
195+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap val =
196+ clientWithRouteAndResultHandler
197+ (Proxy :: Proxy sublayout) q t (prependToPathParts p req) baseurl opts wrap
140198 where p = (fmap . fmap) (toUrlPiece) val
141199
142200
@@ -146,11 +204,11 @@ instance {-# OVERLAPPABLE #-}
146204 (MimeUnrender ct a, ReflectMethod method, cts' ~ (ct ': cts), SupportsServantReflex t m
147205 ) => HasClient t m (Verb method status cts' a) tag where
148206 type Client t m (Verb method status cts' a) tag =
149− Event t tag -> m (Event t (tag, ReqResult a))
207+ Event t tag -> m (Event t (ReqResult tag a))
150208 -- TODO how to access input types here?
151209 -- ExceptT ServantError IO a
152− clientWithRoute Proxy _ _ req baseurl trigs =
153− performRequestCT (Proxy :: Proxy ct) method req' baseurl trigs
210+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs =
211+ wrap =<< fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity $ req') baseurl opts trigs
154212 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
155213 req' = req { reqMethod = method }
156214
@@ -160,32 +218,36 @@ instance {-# OVERLAPPING #-}
160218 (ReflectMethod method, SupportsServantReflex t m) =>
161219 HasClient t m (Verb method status cts NoContent) tag where
162220 type Client t m (Verb method status cts NoContent) tag =
163− Event t tag -> m (Event t (tag, ReqResult NoContent))
221+ Event t tag -> m (Event t (ReqResult tag NoContent))
164222 -- TODO: how to access input types here?
165223 -- ExceptT ServantError IO NoContent
166− clientWithRoute Proxy _ _ req baseurl =
167− performRequestNoBody method req baseurl
224+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs =
225+ wrap =<< fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req) baseurl opts trigs
168226 where method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
169227
170228
171−toHeaders :: BuildHeadersTo ls => ReqResult a -> ReqResult (Headers ls a)
229+toHeaders :: BuildHeadersTo ls => ReqResult tag a -> ReqResult tag (Headers ls a)
172230toHeaders r =
173− let toBS = E.encodeUtf8
174− hdrs = maybe []
175− (\xhr -> fmap (\(h,v) -> (mk (toBS h), toBS v))
231+ let hdrs = maybe []
232+ (\xhr -> fmap (\(h,v) -> (CI.map E.encodeUtf8 h, E.encodeUtf8 v))
176233 (Map.toList $ _xhrResponse_headers xhr))
177234 (response r)
178235 in ffor r $ \a -> Headers {getResponse = a ,getHeadersHList = buildHeadersTo hdrs}
179236
237+
180238class BuildHeaderKeysTo hs where
181− buildHeaderKeysTo :: Proxy hs -> [T.Text]
239+ buildHeaderKeysTo :: Proxy hs -> [CI.CI T.Text]
182240
183241instance {-# OVERLAPPABLE #-} BuildHeaderKeysTo '[]
184242 where buildHeaderKeysTo _ = []
185243
186244instance {-# OVERLAPPABLE #-} (BuildHeaderKeysTo xs, KnownSymbol h)
187− => BuildHeaderKeysTo '[(Header h v) ': xs] where
188− buildHeaderKeysTo _ = T.pack (symbolVal (Proxy :: Proxy h)) : buildHeaderKeysTo (Proxy :: Proxy xs)
245+ => BuildHeaderKeysTo ((Header h v) ': xs) where
246+ buildHeaderKeysTo _ =
247+ let
248+ thisKey = CI.mk $ T.pack (symbolVal (Proxy :: Proxy h))
249+ in thisKey : buildHeaderKeysTo (Proxy :: Proxy xs)
250+
189251
190252-- HEADERS Verb (Content) --
191253-- Headers combinator not treated in fully general case,
@@ -199,11 +261,11 @@ instance {-# OVERLAPPABLE #-}
199261 SupportsServantReflex t m
200262 ) => HasClient t m (Verb method status cts' (Headers ls a)) tag where
201263 type Client t m (Verb method status cts' (Headers ls a)) tag =
202− Event t tag -> m (Event t (tag, ReqResult (Headers ls a)))
203− clientWithRoute Proxy _ _ req baseurl = do
264+ Event t tag -> m (Event t (ReqResult tag (Headers ls a)))
265+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
204266 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
205− resp <- performRequestCT (Proxy :: Proxy ct) method req' baseurl
206− return $ (fmap . fmap) toHeaders <$> resp
267+ resp <- fmap runIdentity <$> performRequestsCT (Proxy :: Proxy ct) method (constDyn $ Identity req') baseurl opts trigs
268+ wrap $ toHeaders <$> resp
207269 where req' = req { respHeaders =
208270 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
209271 }
@@ -215,11 +277,11 @@ instance {-# OVERLAPPABLE #-}
215277 SupportsServantReflex t m
216278 ) => HasClient t m (Verb method status cts (Headers ls NoContent)) tag where
217279 type Client t m (Verb method status cts (Headers ls NoContent)) tag
218− = Event t tag -> m (Event t (tag, ReqResult (Headers ls NoContent)))
219− clientWithRoute Proxy _ _ req baseurl = do
280+ = Event t tag -> m (Event t (ReqResult tag (Headers ls NoContent)))
281+ clientWithRouteAndResultHandler Proxy _ _ req baseurl opts wrap trigs = do
220282 let method = E.decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
221− resp <- performRequestNoBody method req' baseurl
222− return $ (fmap . fmap) toHeaders <$> resp
283+ resp <- fmap runIdentity <$> performRequestsNoBody method (constDyn $ Identity req') baseurl opts trigs
284+ wrap $ toHeaders <$> resp
223285 where req' = req {respHeaders =
224286 OnlyHeaders (Set.fromList (buildHeaderKeysTo (Proxy :: Proxy ls)))
225287 }
@@ -246,11 +308,11 @@ instance (KnownSymbol sym, ToHttpApiData a,
246308 type Client t m (Header sym a :> sublayout) tag =
247309 Dynamic t (Either Text a) -> Client t m sublayout tag
248310
249− clientWithRoute Proxy q t req baseurl eVal =
250− clientWithRoute (Proxy :: Proxy sublayout)
251− q t
252− (Servant.Common.Req.addHeader hname eVal req)
253− baseurl
311+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap eVal =
312+ clientWithRouteAndResultHandler
313+ (Proxy :: Proxy sublayout) q t
314+ (Servant.Common.Req.addHeader hname eVal req)
315+ baseurl opts wrap
254316 where hname = T.pack $ symbolVal (Proxy :: Proxy sym)
255317
256318
@@ -264,8 +326,21 @@ instance HasClient t m sublayout tag
264326 type Client t m (HttpVersion :> sublayout) tag =
265327 Client t m sublayout tag
266328
267− clientWithRoute Proxy q t =
268− clientWithRoute (Proxy :: Proxy sublayout) q t
329+ clientWithRouteAndResultHandler Proxy =
330+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout)
331+
332+-- | Using a 'Summary' combinator in your API doesn't affect the client
333+-- functions.
334+instance (HasClient t m sublayout tag, KnownSymbol sym)
335+ => HasClient t m (Summary sym :> sublayout) tag where
336+
337+ type Client t m (Summary sym :> sublayout) tag =
338+ Client t m sublayout tag
339+
340+ clientWithRouteAndResultHandler Proxy =
341+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout)
342+
343+
269344
270345
271346-- | If you use a 'QueryParam' in one of your endpoints in your API,
@@ -295,15 +370,15 @@ instance HasClient t m sublayout tag
295370-- > -- 'getBooksBy Nothing' for all books
296371-- > -- 'getBooksBy (Just "Isaac Asimov")' to get all books by Isaac Asimov
297372instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex t)
298− => HasClient t m (QueryParam sym a :> sublayout) tag where
373+ => HasClient t m (QueryParam' mods sym a :> sublayout) tag where
299374
300− type Client t m (QueryParam sym a :> sublayout) tag =
375+ type Client t m (QueryParam' mods sym a :> sublayout) tag =
301376 Dynamic t (QParam a) -> Client t m sublayout tag
302377
303378 -- if mparam = Nothing, we don't add it to the query string
304− clientWithRoute Proxy q t req baseurl mparam =
305− clientWithRoute (Proxy :: Proxy sublayout) q t
306− (req {qParams = paramPair : qParams req}) baseurl
379+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap mparam =
380+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t
381+ (req {qParams = paramPair : qParams req}) baseurl opts wrap
307382
308383 where pname = symbolVal (Proxy :: Proxy sym)
309384 --p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
@@ -347,8 +422,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout tag, Reflex
347422 type Client t m (QueryParams sym a :> sublayout) tag =
348423 Dynamic t [a] -> Client t m sublayout tag
349424
350− clientWithRoute Proxy q t req baseurl paramlist =
351− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl
425+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap paramlist =
426+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
352427
353428 where req' = req { qParams = (T.pack pname, params') : qParams req }
354429 pname = symbolVal (Proxy :: Proxy sym)
@@ -387,8 +462,8 @@ instance (KnownSymbol sym, HasClient t m sublayout tag, Reflex t)
387462 type Client t m (QueryFlag sym :> sublayout) tag =
388463 Dynamic t Bool -> Client t m sublayout tag
389464
390− clientWithRoute Proxy q t req baseurl flag =
391− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl
465+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap flag =
466+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
392467
393468 where req' = req { qParams = thisPair : qParams req }
394469 thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
@@ -400,9 +475,9 @@ instance (KnownSymbol sym, HasClient t m sublayout tag, Reflex t)
400475instance SupportsServantReflex t m => HasClient t m Raw tag where
401476 type Client t m Raw tag = Dynamic t (Either Text (XhrRequest ()))
402477 -> Event t tag
403− -> m (Event t (tag, ReqResult ()))
478+ -> m (Event t (ReqResult tag ()))
404479
405− clientWithRoute _ _ _ _ baseurl xhrs triggers = do
480+ clientWithRouteAndResultHandler _ _ _ _ baseurl _ wrap xhrs triggers = do
406481
407482 let xhrs' = liftA2 (\x path -> case x of
408483 Left e -> Left e
@@ -413,8 +488,9 @@ instance SupportsServantReflex t m => HasClient t m Raw tag where
413488 okReq = fmapMaybe (\(t,x) -> either (const Nothing) (Just . (t,)) x) xhrs'' :: Event t (tag, XhrRequest ())
414489
415490 resps <- performRequestsAsync okReq
416− return $ leftmost [ fmap (ResponseSuccess ()) <$> resps
417− , fmap (RequestFailure) <$> badReq ]
491+ wrap $ leftmost [ uncurry RequestFailure <$> badReq
492+ , evalResponse (const $ Right ()) <$> resps
493+ ]
418494
419495
420496-- | If you use a 'ReqBody' in one of your endpoints in your API,
@@ -443,8 +519,8 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
443519 type Client t m (ReqBody (ct ': cts) a :> sublayout) tag =
444520 Dynamic t (Either Text a) -> Client t m sublayout tag
445521
446− clientWithRoute Proxy q t req baseurl body =
447− clientWithRoute (Proxy :: Proxy sublayout) q t req' baseurl
522+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap body =
523+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t req' baseurl opts wrap
448524 where req' = req { reqBody = bodyBytesCT }
449525 ctProxy = Proxy :: Proxy ct
450526 ctString = T.pack $ show $ contentType ctProxy
@@ -458,9 +534,9 @@ instance (MimeRender ct a, HasClient t m sublayout tag, Reflex t)
458534instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient t m (path :> sublayout) tag where
459535 type Client t m (path :> sublayout) tag = Client t m sublayout tag
460536
461− clientWithRoute Proxy q t req baseurl =
462− clientWithRoute (Proxy :: Proxy sublayout) q t
463− (prependToPathParts (pure (Right $ T.pack p)) req) baseurl
537+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap =
538+ clientWithRouteAndResultHandler (Proxy :: Proxy sublayout) q t
539+ (prependToPathParts (pure (Right $ T.pack p)) req) baseurl opts wrap
464540
465541 where p = symbolVal (Proxy :: Proxy path)
466542
@@ -468,23 +544,23 @@ instance (KnownSymbol path, HasClient t m sublayout tag, Reflex t) => HasClient
468544instance HasClient t m api tag => HasClient t m (Vault :> api) tag where
469545 type Client t m (Vault :> api) tag = Client t m api tag
470546
471− clientWithRoute Proxy q t req baseurl =
472− clientWithRoute (Proxy :: Proxy api) q t req baseurl
547+ clientWithRouteAndResultHandler Proxy =
548+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
473549
474550
475551instance HasClient t m api tag => HasClient t m (RemoteHost :> api) tag where
476552 type Client t m (RemoteHost :> api) tag = Client t m api tag
477553
478− clientWithRoute Proxy q t req baseurl =
479− clientWithRoute (Proxy :: Proxy api) q t req baseurl
554+ clientWithRouteAndResultHandler Proxy =
555+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
480556
481557
482558
483559instance HasClient t m api tag => HasClient t m (IsSecure :> api) tag where
484560 type Client t m (IsSecure :> api) tag = Client t m api tag
485561
486− clientWithRoute Proxy q t req baseurl =
487− clientWithRoute (Proxy :: Proxy api) q t req baseurl
562+ clientWithRouteAndResultHandler Proxy =
563+ clientWithRouteAndResultHandler (Proxy :: Proxy api)
488564
489565
490566instance (HasClient t m api tag, Reflex t)
@@ -493,8 +569,8 @@ instance (HasClient t m api tag, Reflex t)
493569 type Client t m (BasicAuth realm usr :> api) tag = Dynamic t (Maybe BasicAuthData)
494570 -> Client t m api tag
495571
496− clientWithRoute Proxy q t req baseurl authdata =
497− clientWithRoute (Proxy :: Proxy api) q t req' baseurl
572+ clientWithRouteAndResultHandler Proxy q t req baseurl opts wrap authdata =
573+ clientWithRouteAndResultHandler (Proxy :: Proxy api) q t req' baseurl opts wrap
498574 where
499575 req' = req { authData = Just authdata }
500576
@@ -520,3 +596,29 @@ non-empty lists, but is otherwise more specific, no instance will be overall
520596more specific. This in turn generally means adding yet another instance (one
521597for empty and one for non-empty lists).
522598-}
599+
600+
601+-- SUPPORT FOR servant-auth --
602+
603+-- For JavaScript clients we should be sending/storing JSON web tokens in a
604+-- way that is inaccessible to JavaScript.
605+--
606+-- For @servant-auth@ this is done with HTTP-only cookies. In a Reflex-DOM
607+-- app this means the @servant-auth@ client should only verify that the API
608+-- supports Cookie-based authentication but do nothing with the token
609+-- directly.
610+
611+-- @HasCookieAuth auths@ is nominally a redundant constraint, but ensures
612+-- we're not trying to rely on cookies when the API does not use them.
613+instance (HasCookieAuth auths, HasClient t m api tag) => HasClient t m (Auth.Auth auths a :> api) tag where
614+
615+ type Client t m (Auth.Auth auths a :> api) tag = Client t m api tag
616+ clientWithRouteAndResultHandler Proxy = clientWithRouteAndResultHandler (Proxy :: Proxy api)
617+
618+
619+type family HasCookieAuth xs :: Constraint where
620+ HasCookieAuth (Auth.Cookie ': xs) = ()
621+ HasCookieAuth (x ': xs) = HasCookieAuth xs
622+ HasCookieAuth '[] = CookieAuthNotEnabled
623+
624+class CookieAuthNotEnabled