git.iko.soy/typeable/servant
on master help sign in
Use NoContent and fix content-type lists in docs.
commitefbe6fd4983d4fc43f7f54d660f30e166da1726f authorJulian K. Arni <jkarni@gmail.com> dateTue 10 May 2016, 13:21 (10 years ago) parent4f4ca69
1 file changed+26−26
doc/tutorial/Server.lhs+26−26
doc/tutorial/Server.lhs+26−26
@@ -809,7 +809,7 @@ type UserAPI3 = -- view the user with given userid, in JSON
809809 Capture "userid" Int :> Get '[JSON] User
810810
811811 :<|> -- delete the user with given userid. empty response
812− Capture "userid" Int :> Delete '[] ()
812+ Capture "userid" Int :> DeleteNoContent '[JSON] NoContent
813813```
814814
815815We can instead factor out the `userid`:
@@ -817,7 +817,7 @@ We can instead factor out the `userid`:
817817``` haskell
818818type UserAPI4 = Capture "userid" Int :>
819819 ( Get '[JSON] User
820− :<|> Delete '[] ()
820+ :<|> DeleteNoContent '[JSON] NoContent
821821 )
822822```
823823
@@ -826,10 +826,10 @@ corresponding `Server`:
826826
827827``` haskell ignore
828828Server UserAPI3 = (Int -> Handler User)
829− :<|> (Int -> Handler ())
829+ :<|> (Int -> Handler NoContent)
830830
831831Server UserAPI4 = Int -> ( Handler User
832− :<|> Handler ()
832+ :<|> Handler NoContent
833833 )
834834```
835835
@@ -844,7 +844,7 @@ server8 = getUser :<|> deleteUser
844844 where getUser :: Int -> Handler User
845845 getUser _userid = error "..."
846846
847− deleteUser :: Int -> Handler ()
847+ deleteUser :: Int -> Handler NoContent
848848 deleteUser _userid = error "..."
849849
850850-- notice how getUser and deleteUser
@@ -856,7 +856,7 @@ server9 userid = getUser userid :<|> deleteUser userid
856856 where getUser :: Int -> Handler User
857857 getUser = error "..."
858858
859− deleteUser :: Int -> Handler ()
859+ deleteUser :: Int -> Handler NoContent
860860 deleteUser = error "..."
861861```
862862
@@ -875,13 +875,13 @@ type API1 = "users" :>
875875-- we factor out the Request Body
876876type API2 = ReqBody '[JSON] User :>
877877 ( Get '[JSON] User -- just display the same user back, don't register it
878− :<|> Post '[JSON] () -- register the user. empty response
878+ :<|> PostNoContent '[JSON] NoContent -- register the user. empty response
879879 )
880880
881881-- we factor out a Header
882882type API3 = Header "Authorization" Token :>
883883 ( Get '[JSON] SecretData -- get some secret data, if authorized
884− :<|> ReqBody '[JSON] SecretData :> Post '[] () -- add some secret data, if authorized
884+ :<|> ReqBody '[JSON] SecretData :> PostNoContent '[JSON] NoContent -- add some secret data, if authorized
885885 )
886886
887887newtype Token = Token ByteString
@@ -894,11 +894,11 @@ API type only at the end.
894894``` haskell
895895type UsersAPI =
896896 Get '[JSON] [User] -- list users
897− :<|> ReqBody '[JSON] User :> Post '[] () -- add a user
897+ :<|> ReqBody '[JSON] User :> PostNoContent '[JSON] NoContent -- add a user
898898 :<|> Capture "userid" Int :>
899899 ( Get '[JSON] User -- view a user
900− :<|> ReqBody '[JSON] User :> Put '[] () -- update a user
901− :<|> Delete '[] () -- delete a user
900+ :<|> ReqBody '[JSON] User :> PutNoContent '[JSON] NoContent -- update a user
901+ :<|> DeleteNoContent '[JSON] NoContent -- delete a user
902902 )
903903
904904usersServer :: Server UsersAPI
@@ -907,7 +907,7 @@ usersServer = getUsers :<|> newUser :<|> userOperations
907907 where getUsers :: Handler [User]
908908 getUsers = error "..."
909909
910− newUser :: User -> Handler ()
910+ newUser :: User -> Handler NoContent
911911 newUser = error "..."
912912
913913 userOperations userid =
@@ -917,21 +917,21 @@ usersServer = getUsers :<|> newUser :<|> userOperations
917917 viewUser :: Int -> Handler User
918918 viewUser = error "..."
919919
920− updateUser :: Int -> User -> Handler ()
920+ updateUser :: Int -> User -> Handler NoContent
921921 updateUser = error "..."
922922
923− deleteUser :: Int -> Handler ()
923+ deleteUser :: Int -> Handler NoContent
924924 deleteUser = error "..."
925925```
926926
927927``` haskell
928928type ProductsAPI =
929929 Get '[JSON] [Product] -- list products
930− :<|> ReqBody '[JSON] Product :> Post '[] () -- add a product
930+ :<|> ReqBody '[JSON] Product :> PostNoContent '[JSON] NoContent -- add a product
931931 :<|> Capture "productid" Int :>
932932 ( Get '[JSON] Product -- view a product
933− :<|> ReqBody '[JSON] Product :> Put '[] () -- update a product
934− :<|> Delete '[] () -- delete a product
933+ :<|> ReqBody '[JSON] Product :> PutNoContent '[JSON] NoContent -- update a product
934+ :<|> DeleteNoContent '[JSON] NoContent -- delete a product
935935 )
936936
937937data Product = Product { productId :: Int }
@@ -942,7 +942,7 @@ productsServer = getProducts :<|> newProduct :<|> productOperations
942942 where getProducts :: Handler [Product]
943943 getProducts = error "..."
944944
945− newProduct :: Product -> Handler ()
945+ newProduct :: Product -> Handler NoContent
946946 newProduct = error "..."
947947
948948 productOperations productid =
@@ -952,10 +952,10 @@ productsServer = getProducts :<|> newProduct :<|> productOperations
952952 viewProduct :: Int -> Handler Product
953953 viewProduct = error "..."
954954
955− updateProduct :: Int -> Product -> Handler ()
955+ updateProduct :: Int -> Product -> Handler NoContent
956956 updateProduct = error "..."
957957
958− deleteProduct :: Int -> Handler ()
958+ deleteProduct :: Int -> Handler NoContent
959959 deleteProduct = error "..."
960960```
961961
@@ -975,20 +975,20 @@ abstract that away:
975975-- indexed by values of type 'i'
976976type APIFor a i =
977977 Get '[JSON] [a] -- list 'a's
978− :<|> ReqBody '[JSON] a :> Post '[] () -- add an 'a'
978+ :<|> ReqBody '[JSON] a :> PostNoContent '[JSON] NoContent -- add an 'a'
979979 :<|> Capture "id" i :>
980980 ( Get '[JSON] a -- view an 'a' given its "identifier" of type 'i'
981− :<|> ReqBody '[JSON] a :> Put '[] () -- update an 'a'
982− :<|> Delete '[] () -- delete an 'a'
981+ :<|> ReqBody '[JSON] a :> PutNoContent '[JSON] NoContent -- update an 'a'
982+ :<|> DeleteNoContent '[JSON] NoContent -- delete an 'a'
983983 )
984984
985985-- Build the appropriate 'Server'
986986-- given the handlers of the right type.
987987serverFor :: Handler [a] -- handler for listing of 'a's
988− -> (a -> Handler ()) -- handler for adding an 'a'
988+ -> (a -> Handler NoContent) -- handler for adding an 'a'
989989 -> (i -> Handler a) -- handler for viewing an 'a' given its identifier of type 'i'
990− -> (i -> a -> Handler ()) -- updating an 'a' with given id
991− -> (i -> Handler ()) -- deleting an 'a' given its id
990+ -> (i -> a -> Handler NoContent) -- updating an 'a' with given id
991+ -> (i -> Handler NoContent) -- deleting an 'a' given its id
992992 -> Server (APIFor a i)
993993serverFor = error "..."
994994-- implementation left as an exercise. contact us on IRC