git.iko.soy/typeable/servant
on master help sign in
haddocks for ReqBody
c47f4aeAlp Mestanogullari
src/Servant/API/Capture.hs+1−1
@@ -43,7 +43,7 @@ captured _ = fromText
43-- >
43-- >
44-- > server :: Server MyApi
44-- > server :: Server MyApi
45-- > server = getBook
45-- > server = getBook
46−-- > where getBook :: Text -> 'EitherT' (Int, String) IO Book
46+-- > where getBook :: Text -> EitherT (Int, String) IO Book
47-- > getBook isbn = ...
47-- > getBook isbn = ...
48instance (KnownSymbol capture, FromText a, HasServer sublayout)
48instance (KnownSymbol capture, FromText a, HasServer sublayout)
49 => HasServer (Capture capture a :> sublayout) where
49 => HasServer (Capture capture a :> sublayout) where
src/Servant/API/ReqBody.hs+40−1
@@ -15,9 +15,30 @@ import Servant.Common.Req
15import Servant.Docs
15import Servant.Docs
16import Servant.Server
16import Servant.Server
17
17
18−-- * Request Body support
18+-- | Extract the request body as a value of type @a@.
19+--
20+-- Example:
21+--
22+-- > -- POST /books
23+-- > type MyApi = "books" :> ReqBody Book :> Post Book
19data ReqBody a
24data ReqBody a
20
25
26+-- | If you use 'ReqBody' in one of the endpoints for your API,
27+-- this automatically requires your server-side handler to be a function
28+-- that takes an argument of the type specified by 'ReqBody'.
29+-- This lets servant worry about extracting it from the request and turning
30+-- it into a value of the type you specify.
31+--
32+-- All it asks is for a 'FromJSON' instance.
33+--
34+-- Example:
35+--
36+-- > type MyApi = "books" :> ReqBody Book :> Post Book
37+-- >
38+-- > server :: Server MyApi
39+-- > server = postBook
40+-- > where postBook :: Book -> EitherT (Int, String) IO Book
41+-- > postBook book = ...insert into your db...
21instance (FromJSON a, HasServer sublayout)
42instance (FromJSON a, HasServer sublayout)
22 => HasServer (ReqBody a :> sublayout) where
43 => HasServer (ReqBody a :> sublayout) where
23
44
@@ -30,6 +51,24 @@ instance (FromJSON a, HasServer sublayout)
30 Nothing -> respond $ failWith InvalidBody
51 Nothing -> respond $ failWith InvalidBody
31 Just v -> route (Proxy :: Proxy sublayout) (subserver v) request respond
52 Just v -> route (Proxy :: Proxy sublayout) (subserver v) request respond
32
53
54+-- | If you use a 'ReqBody' in one of your endpoints in your API,
55+-- the corresponding querying function will automatically take
56+-- an additional argument of the type specified by your 'ReqBody'.
57+-- That function will take care of encoding this argument as JSON and
58+-- of using it as the request body.
59+--
60+-- All you need is for your type to have a 'ToJSON' instance.
61+--
62+-- Example:
63+--
64+-- > type MyApi = "books" :> ReqBody Book :> Post Book
65+-- >
66+-- > myApi :: Proxy MyApi
67+-- > myApi = Proxy
68+-- >
69+-- > addBook :: Book -> BaseUrl -> EitherT String IO Book
70+-- > addBook = client myApi
71+-- > -- then you can just use "addBook" to query that endpoint
33instance (ToJSON a, HasClient sublayout)
72instance (ToJSON a, HasClient sublayout)
34 => HasClient (ReqBody a :> sublayout) where
73 => HasClient (ReqBody a :> sublayout) where
35
74