@@ -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+-- | 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
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
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 <> "\""
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"
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
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