on 641ee69 help sign in
43 lines 1.3 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 case isMismatch mResponse of
20 True -> route pb b request $ \mResponse' ->
21 respond (mResponse <> mResponse')
22 False -> respond mResponse
25 where pa = Proxy :: Proxy a
26 pb = Proxy :: Proxy b
28instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
29 type Client (a :<|> b) = Client a :<|> Client b
30 clientWithRoute Proxy req =
31 clientWithRoute (Proxy :: Proxy a) req :<|>
32 clientWithRoute (Proxy :: Proxy b) req
34instance (HasDocs layout1, HasDocs layout2)
35 => HasDocs (layout1 :<|> layout2) where
37 docsFor Proxy (ep, action) = docsFor p1 (ep, action) <> docsFor p2 (ep, action)
39 where p1 :: Proxy layout1
40 p1 = Proxy
42 p2 :: Proxy layout2
43 p2 = Proxy