Replace HTML with PlainText in examples
This commit is contained in:
parent
bdf61e4df9
commit
97ff49c3c4
2 changed files with 12 additions and 18 deletions
13
README.md
13
README.md
|
@ -27,16 +27,13 @@ import Servant
|
||||||
data Greet = Greet { _msg :: Text }
|
data Greet = Greet { _msg :: Text }
|
||||||
deriving (Generic, Show)
|
deriving (Generic, Show)
|
||||||
|
|
||||||
-- we get our JSON serialization for free
|
-- we get our JSON serialization for free. This will be used by the default
|
||||||
|
-- 'MimeRender' instance for 'JSON'.
|
||||||
instance FromJSON Greet
|
instance FromJSON Greet
|
||||||
instance ToJSON Greet
|
instance ToJSON Greet
|
||||||
|
|
||||||
-- we can render a Greeting into JSON using this ToJSON instance
|
-- We can also implement 'MimeRender' explicitly for additional formats.
|
||||||
instance MimeRender JSON Greet where
|
instance MimeRender PlainText Greet where
|
||||||
toByteString Proxy = encodePretty
|
|
||||||
|
|
||||||
-- or we can render it to HTML
|
|
||||||
instance MimeRender HTML Greet where
|
|
||||||
toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
|
toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
|
||||||
|
|
||||||
-- we provide a sample value for the 'Greet' type
|
-- we provide a sample value for the 'Greet' type
|
||||||
|
@ -59,7 +56,7 @@ instance ToCapture (Capture "greetid" Text) where
|
||||||
|
|
||||||
-- API specification
|
-- API specification
|
||||||
type TestApi =
|
type TestApi =
|
||||||
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,HTML] Greet
|
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,PlainText] Greet
|
||||||
:<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
|
:<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
|
||||||
:<|> "delete" :> Capture "greetid" Text :> Delete
|
:<|> "delete" :> Capture "greetid" Text :> Delete
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
{-# LANGUAGE TypeOperators #-}
|
{-# LANGUAGE TypeOperators #-}
|
||||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Aeson.Encode.Pretty (encodePretty)
|
|
||||||
import Data.Proxy
|
import Data.Proxy
|
||||||
import Data.String.Conversions
|
import Data.String.Conversions
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
|
@ -20,16 +19,14 @@ import Servant.Docs
|
||||||
newtype Greet = Greet Text
|
newtype Greet = Greet Text
|
||||||
deriving (Generic, Show)
|
deriving (Generic, Show)
|
||||||
|
|
||||||
|
-- | We can get JSON support automatically. This will be used to parse
|
||||||
|
-- and encode a Greeting as 'JSON'.
|
||||||
instance FromJSON Greet
|
instance FromJSON Greet
|
||||||
instance ToJSON Greet
|
instance ToJSON Greet
|
||||||
|
|
||||||
-- | A 'Greet' value can be rendered to 'JSON'.
|
-- | We can also implement 'MimeRender' for additional formats like 'PlainText'.
|
||||||
instance MimeRender JSON Greet where
|
instance MimeRender PlainText Greet where
|
||||||
toByteString Proxy = encodePretty
|
toByteString Proxy (Greet s) = "\"" <> cs s <> "\""
|
||||||
|
|
||||||
-- | A 'Greet' value can be rendered to 'HTML'.
|
|
||||||
instance MimeRender HTML Greet where
|
|
||||||
toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
|
|
||||||
|
|
||||||
-- We add some useful annotations to our captures,
|
-- We add some useful annotations to our captures,
|
||||||
-- query parameters and request body to make the docs
|
-- query parameters and request body to make the docs
|
||||||
|
@ -76,8 +73,8 @@ intro2 = DocIntro "This title is below the last"
|
||||||
|
|
||||||
-- API specification
|
-- API specification
|
||||||
type TestApi =
|
type TestApi =
|
||||||
-- GET /hello/:name?capital={true, false} returns a Greet as JSON or HTML
|
-- GET /hello/:name?capital={true, false} returns a Greet as JSON or PlainText
|
||||||
"hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
|
"hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, PlainText] Greet
|
||||||
|
|
||||||
-- POST /greet with a Greet as JSON in the request body,
|
-- POST /greet with a Greet as JSON in the request body,
|
||||||
-- returns a Greet as JSON
|
-- returns a Greet as JSON
|
||||||
|
|
Loading…
Reference in a new issue