diff --git a/README.md b/README.md index 8adaf0b8..cc27510b 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,14 @@ data Greet = Greet { _msg :: Text } instance FromJSON Greet instance ToJSON Greet +-- we can render a Greeting into JSON using this ToJSON instance +instance MimeRender JSON Greet where + toByteString Proxy = encodePretty + +-- or we can render it to HTML +instance MimeRender HTML Greet where + toByteString Proxy (Greet s) = "

" <> cs s <> "

" + -- we provide a sample value for the 'Greet' type instance ToSample Greet where toSample = Just g @@ -51,8 +59,8 @@ instance ToCapture (Capture "greetid" Text) where -- API specification type TestApi = - "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet - :<|> "greet" :> RQBody Greet :> Post Greet + "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,HTML] Greet + :<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet :<|> "delete" :> Capture "greetid" Text :> Delete testApi :: Proxy TestApi diff --git a/example/greet.hs b/example/greet.hs index 8e1efaeb..3aca7b24 100644 --- a/example/greet.hs +++ b/example/greet.hs @@ -23,9 +23,11 @@ newtype Greet = Greet Text instance FromJSON Greet instance ToJSON Greet +-- | A 'Greet' value can be rendered to 'JSON'. instance MimeRender JSON Greet where toByteString Proxy = encodePretty +-- | A 'Greet' value can be rendered to 'HTML'. instance MimeRender HTML Greet where toByteString Proxy (Greet s) = "

" <> cs s <> "

" @@ -74,12 +76,12 @@ intro2 = DocIntro "This title is below the last" -- API specification type TestApi = - -- GET /hello/:name?capital={true, false} returns a Greet as JSON + -- GET /hello/:name?capital={true, false} returns a Greet as JSON or HTML "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,HTML] Greet :> Post '[JSON] Greet + :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet -- DELETE /greet/:greetid :<|> "greet" :> Capture "greetid" Text :> Delete