{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE ScopedTypeVariables #-} module Servant.API.Union where import Data.Proxy import Servant.Client import Servant.Server -- | Union of two APIs, first takes precedence in case of overlap. data a :<|> b = a :<|> b infixr 8 :<|> instance (HasServer a, HasServer b) => HasServer (a :<|> b) where type Server (a :<|> b) = Server a :<|> Server b route Proxy (a :<|> b) request = do m <- route (Proxy :: Proxy a) a request case m of Nothing -> route (Proxy :: Proxy b) b request Just response -> return $ Just response instance (HasClient a, HasClient b) => HasClient (a :<|> b) where type Client (a :<|> b) = Client a :<|> Client b clientWithRoute Proxy req = clientWithRoute (Proxy :: Proxy a) req :<|> clientWithRoute (Proxy :: Proxy b) req