on d2f7e12 help sign in
41 lines 1.2 kB history raw
1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE TypeOperators #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4module Servant.API.Union where
6import Data.Proxy
7import Servant.Client
8import Servant.Docs
9import Servant.Server
11-- | Union of two APIs, first takes precedence in case of overlap.
12data a :<|> b = a :<|> b
13infixr 8 :<|>
15instance (HasServer a, HasServer b) => HasServer (a :<|> b) where
16 type Server (a :<|> b) = Server a :<|> Server b
17 route Proxy (a :<|> b) request respond =
18 route pa a request $ \ mResponse ->
19 if isMismatch mResponse
20 then route pb b request $ \mResponse' -> respond (mResponse <> mResponse')
21 else respond mResponse
23 where pa = Proxy :: Proxy a
24 pb = Proxy :: Proxy b
26instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
27 type Client (a :<|> b) = Client a :<|> Client b
28 clientWithRoute Proxy req =
29 clientWithRoute (Proxy :: Proxy a) req :<|>
30 clientWithRoute (Proxy :: Proxy b) req
32instance (HasDocs layout1, HasDocs layout2)
33 => HasDocs (layout1 :<|> layout2) where
35 docsFor Proxy (ep, action) = docsFor p1 (ep, action) <> docsFor p2 (ep, action)
37 where p1 :: Proxy layout1
38 p1 = Proxy
40 p2 :: Proxy layout2
41 p2 = Proxy