on master help sign in
Replace HTML with PlainText in examples
97ff49cThomas Sutton
README.md+5−8
@@ -27,16 +27,13 @@ import Servant
27data Greet = Greet { _msg :: Text }
27data Greet = Greet { _msg :: Text }
28 deriving (Generic, Show)
28 deriving (Generic, Show)
29
29
30−-- we get our JSON serialization for free
30+-- we get our JSON serialization for free. This will be used by the default
31+-- 'MimeRender' instance for 'JSON'.
31instance FromJSON Greet
32instance FromJSON Greet
32instance ToJSON Greet
33instance ToJSON Greet
33
34
34−-- we can render a Greeting into JSON using this ToJSON instance
35+-- We can also implement 'MimeRender' explicitly for additional formats.
35−instance MimeRender JSON Greet where
36− toByteString Proxy = encodePretty
37−
38−-- or we can render it to HTML
39−instance MimeRender HTML Greet where
36+instance MimeRender PlainText Greet where
40 toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
37 toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
41
38
42-- we provide a sample value for the 'Greet' type
39-- we provide a sample value for the 'Greet' type
@@ -59,7 +56,7 @@ instance ToCapture (Capture "greetid" Text) where
59
56
60-- API specification
57-- API specification
61type TestApi =
58type TestApi =
62− "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,HTML] Greet
59+ "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,PlainText] Greet
63 :<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
60 :<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
64 :<|> "delete" :> Capture "greetid" Text :> Delete
61 :<|> "delete" :> Capture "greetid" Text :> Delete
65
62
example/greet.hs+7−10
@@ -6,7 +6,6 @@
6{-# LANGUAGE TypeOperators #-}
6{-# LANGUAGE TypeOperators #-}
7{-# OPTIONS_GHC -fno-warn-orphans #-}
7{-# OPTIONS_GHC -fno-warn-orphans #-}
8import Data.Aeson
8import Data.Aeson
9−import Data.Aeson.Encode.Pretty (encodePretty)
10import Data.Proxy
9import Data.Proxy
11import Data.String.Conversions
10import Data.String.Conversions
12import Data.Text (Text)
11import Data.Text (Text)
@@ -20,16 +19,14 @@ import Servant.Docs
20newtype Greet = Greet Text
19newtype Greet = Greet Text
21 deriving (Generic, Show)
20 deriving (Generic, Show)
22
21
22+-- | We can get JSON support automatically. This will be used to parse
23+-- and encode a Greeting as 'JSON'.
23instance FromJSON Greet
24instance FromJSON Greet
24instance ToJSON Greet
25instance ToJSON Greet
25
26
26−-- | A 'Greet' value can be rendered to 'JSON'.
27+-- | We can also implement 'MimeRender' for additional formats like 'PlainText'.
27−instance MimeRender JSON Greet where
28− toByteString Proxy = encodePretty
29−
30−-- | A 'Greet' value can be rendered to 'HTML'.
31−instance MimeRender HTML Greet where
28+instance MimeRender PlainText Greet where
32− toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
29+ toByteString Proxy (Greet s) = "\"" <> cs s <> "\""
33
30
34-- We add some useful annotations to our captures,
31-- We add some useful annotations to our captures,
35-- query parameters and request body to make the docs
32-- query parameters and request body to make the docs
@@ -76,8 +73,8 @@ intro2 = DocIntro "This title is below the last"
76
73
77-- API specification
74-- API specification
78type TestApi =
75type TestApi =
79− -- GET /hello/:name?capital={true, false} returns a Greet as JSON or HTML
76+ -- GET /hello/:name?capital={true, false} returns a Greet as JSON or PlainText
80− "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
77+ "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, PlainText] Greet
81
78
82 -- POST /greet with a Greet as JSON in the request body,
79 -- POST /greet with a Greet as JSON in the request body,
83 -- returns a Greet as JSON
80 -- returns a Greet as JSON