More haddocks for :<|>
This commit is contained in:
parent
d461726b9a
commit
407349c245
1 changed files with 31 additions and 0 deletions
|
@ -9,9 +9,25 @@ import Servant.Docs
|
||||||
import Servant.Server
|
import Servant.Server
|
||||||
|
|
||||||
-- | Union of two APIs, first takes precedence in case of overlap.
|
-- | Union of two APIs, first takes precedence in case of overlap.
|
||||||
|
--
|
||||||
|
-- Example:
|
||||||
|
--
|
||||||
|
-- > type MyApi = "books" :> Get [Book] -- GET /books
|
||||||
|
-- > :<|> "books" :> ReqBody Book :> Post Book -- POST /books
|
||||||
data a :<|> b = a :<|> b
|
data a :<|> b = a :<|> b
|
||||||
infixr 8 :<|>
|
infixr 8 :<|>
|
||||||
|
|
||||||
|
-- | A server for @a ':<|>' b@ first tries to match the request again the route
|
||||||
|
-- represented by @a@ and if it fails tries @b@. You must provide a request
|
||||||
|
-- handler for each route.
|
||||||
|
--
|
||||||
|
-- > type MyApi = "books" :> Get [Book] -- GET /books
|
||||||
|
-- > :<|> "books" :> ReqBody Book :> Post Book -- POST /books
|
||||||
|
-- >
|
||||||
|
-- > server :: Server MyApi
|
||||||
|
-- > server = listAllBooks :<|> postBook
|
||||||
|
-- > where listAllBooks = ...
|
||||||
|
-- > postBook book = ...
|
||||||
instance (HasServer a, HasServer b) => HasServer (a :<|> b) where
|
instance (HasServer a, HasServer b) => HasServer (a :<|> b) where
|
||||||
type Server (a :<|> b) = Server a :<|> Server b
|
type Server (a :<|> b) = Server a :<|> Server b
|
||||||
route Proxy (a :<|> b) request respond =
|
route Proxy (a :<|> b) request respond =
|
||||||
|
@ -23,12 +39,27 @@ instance (HasServer a, HasServer b) => HasServer (a :<|> b) where
|
||||||
where pa = Proxy :: Proxy a
|
where pa = Proxy :: Proxy a
|
||||||
pb = Proxy :: Proxy b
|
pb = Proxy :: Proxy b
|
||||||
|
|
||||||
|
-- | A client querying function for @a ':<|>' b@ will actually hand you
|
||||||
|
-- one function for querying @a@ and another one for querying @b@,
|
||||||
|
-- stitching them together with ':<|>', which really is just like a pair.
|
||||||
|
--
|
||||||
|
-- > type MyApi = "books" :> Get [Book] -- GET /books
|
||||||
|
-- > :<|> "books" :> ReqBody Book :> Post Book -- POST /books
|
||||||
|
-- >
|
||||||
|
-- > myApi :: Proxy MyApi
|
||||||
|
-- > myApi = Proxy
|
||||||
|
-- >
|
||||||
|
-- > getAllBooks :: BaseUrl -> EitherT String IO [Book]
|
||||||
|
-- > postNewBook :: Book -> BaseUrl -> EitherT String IO Book
|
||||||
|
-- > (getAllBooks :<|> postNewBook) = client myApi
|
||||||
instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
|
instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
|
||||||
type Client (a :<|> b) = Client a :<|> Client b
|
type Client (a :<|> b) = Client a :<|> Client b
|
||||||
clientWithRoute Proxy req =
|
clientWithRoute Proxy req =
|
||||||
clientWithRoute (Proxy :: Proxy a) req :<|>
|
clientWithRoute (Proxy :: Proxy a) req :<|>
|
||||||
clientWithRoute (Proxy :: Proxy b) req
|
clientWithRoute (Proxy :: Proxy b) req
|
||||||
|
|
||||||
|
-- | The generated docs for @a ':<|>' b@ just append the docs
|
||||||
|
-- for @a@ with the docs for @b@.
|
||||||
instance (HasDocs layout1, HasDocs layout2)
|
instance (HasDocs layout1, HasDocs layout2)
|
||||||
=> HasDocs (layout1 :<|> layout2) where
|
=> HasDocs (layout1 :<|> layout2) where
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue