1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE TypeOperators #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4module Servant.API.Alternative where
11-- | Union of two APIs, first takes precedence in case of overlap.
15-- > type MyApi = "books" :> Get [Book] -- GET /books
16-- > :<|> "books" :> ReqBody Book :> Post Book -- POST /books
17data a :<|> b = a :<|> b
20-- | A server for @a ':<|>' b@ first tries to match the request again the route
21-- represented by @a@ and if it fails tries @b@. You must provide a request
22-- handler for each route.
24-- > type MyApi = "books" :> Get [Book] -- GET /books
25-- > :<|> "books" :> ReqBody Book :> Post Book -- POST /books
27-- > server :: Server MyApi
28-- > server = listAllBooks :<|> postBook
29-- > where listAllBooks = ...
30-- > postBook book = ...
31instance (HasServer a, HasServer b) => HasServer (a :<|> b) where
32 type Server (a :<|> b) = Server a :<|> Server b
33 route Proxy (a :<|> b) request respond =
34 route pa a request $ \ mResponse ->
35 if isMismatch mResponse
36 then route pb b request $ \mResponse' -> respond (mResponse <> mResponse')
37 else respond mResponse
39 where pa = Proxy :: Proxy a
42-- | A client querying function for @a ':<|>' b@ will actually hand you
43-- one function for querying @a@ and another one for querying @b@,
44-- stitching them together with ':<|>', which really is just like a pair.
46-- > type MyApi = "books" :> Get [Book] -- GET /books
47-- > :<|> "books" :> ReqBody Book :> Post Book -- POST /books
49-- > myApi :: Proxy MyApi
52-- > getAllBooks :: BaseUrl -> EitherT String IO [Book]
53-- > postNewBook :: Book -> BaseUrl -> EitherT String IO Book
54-- > (getAllBooks :<|> postNewBook) = client myApi
55instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
56 type Client (a :<|> b) = Client a :<|> Client b
57 clientWithRoute Proxy req =
58 clientWithRoute (Proxy :: Proxy a) req :<|>
59 clientWithRoute (Proxy :: Proxy b) req
61-- | The generated docs for @a ':<|>' b@ just appends the docs
62-- for @a@ with the docs for @b@.
63instance (HasDocs layout1, HasDocs layout2)
64 => HasDocs (layout1 :<|> layout2) where
66 docsFor Proxy (ep, action) = docsFor p1 (ep, action) <> docsFor p2 (ep, action)
68 where p1 :: Proxy layout1
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list