From 6d85885b4219a80bcc21cc972dba05477350aafe Mon Sep 17 00:00:00 2001 From: Thomas Sutton Date: Thu, 19 Feb 2015 12:48:10 +1100 Subject: [PATCH] Add content types to the example --- example/greet.hs | 29 +++++++++++++++++++++-------- servant-docs.cabal | 3 ++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/example/greet.hs b/example/greet.hs index 977ee8bf..28b5e64f 100644 --- a/example/greet.hs +++ b/example/greet.hs @@ -1,14 +1,19 @@ -{-# LANGUAGE DataKinds #-} -{-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE TypeOperators #-} -{-# LANGUAGE FlexibleInstances #-} -{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeOperators #-} {-# OPTIONS_GHC -fno-warn-orphans #-} import Data.Aeson +import Data.Aeson.Encode.Pretty (encodePretty) import Data.Proxy -import Data.Text(Text) +import Data.Text (Text) +import qualified Data.Text.Lazy as T +import qualified Data.Text.Lazy.Encoding as T import GHC.Generics import Servant.API +import Servant.API.ContentTypes import Servant.Docs -- * Example @@ -20,6 +25,14 @@ newtype Greet = Greet Text instance FromJSON Greet instance ToJSON Greet +instance MimeRender JSON Greet where + toByteString Proxy v = encodePretty v + +instance MimeRender HTML Greet where + toByteString Proxy (Greet s) = "

" <> (c s) <> "

" + where + c = T.encodeUtf8 . T.fromStrict + -- We add some useful annotations to our captures, -- query parameters and request body to make the docs -- really helpful. @@ -66,11 +79,11 @@ intro2 = DocIntro "This title is below the last" -- API specification type TestApi = -- GET /hello/:name?capital={true, false} returns a Greet as JSON - "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON] Greet + "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet -- POST /greet with a Greet as JSON in the request body, -- returns a Greet as JSON - :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet + :<|> "greet" :> ReqBody '[JSON,HTML] Greet :> Post '[JSON] Greet -- DELETE /greet/:greetid :<|> "greet" :> Capture "greetid" Text :> Delete diff --git a/servant-docs.cabal b/servant-docs.cabal index 52c06b5b..37de47c4 100644 --- a/servant-docs.cabal +++ b/servant-docs.cabal @@ -32,6 +32,7 @@ library , aeson-pretty < 0.8 , bytestring , hashable + , http-media , lens , servant >= 0.2.1 , string-conversions @@ -46,5 +47,5 @@ executable greet-docs main-is: greet.hs hs-source-dirs: example ghc-options: -Wall - build-depends: base, aeson, servant, servant-docs, text + build-depends: base, aeson, aeson-pretty, servant, servant-docs, text default-language: Haskell2010