on 4ebc494 help sign in
38 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) globalPathInfo request respond = do
18 route (Proxy :: Proxy a) a globalPathInfo request $ \ mResponse ->
19 case mResponse of
20 Nothing -> route (Proxy :: Proxy b) b globalPathInfo request respond
21 Just resp -> respond $ Just resp
23instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
24 type Client (a :<|> b) = Client a :<|> Client b
25 clientWithRoute Proxy req =
26 clientWithRoute (Proxy :: Proxy a) req :<|>
27 clientWithRoute (Proxy :: Proxy b) req
29instance (HasDocs layout1, HasDocs layout2)
30 => HasDocs (layout1 :<|> layout2) where
32 docsFor Proxy (ep, action) = docsFor p1 (ep, action) <> docsFor p2 (ep, action)
34 where p1 :: Proxy layout1
35 p1 = Proxy
37 p2 :: Proxy layout2
38 p2 = Proxy