git.iko.soy/typeable/servant
on master help sign in
Add 'pretty'
702c2ceMitchell Rosen
servant-docs/servant-docs.cabal+2
@@ -31,6 +31,8 @@ library
31 , Servant.Docs.Internal
31 , Servant.Docs.Internal
32 build-depends:
32 build-depends:
33 base >=4.7 && <5
33 base >=4.7 && <5
34+ , aeson
35+ , aeson-pretty
34 , bytestring
36 , bytestring
35 , bytestring-conversion
37 , bytestring-conversion
36 , case-insensitive
38 , case-insensitive
servant-docs/src/Servant/Docs.hs+1−1
@@ -23,7 +23,7 @@
23-- See example/greet.hs for an example.
23-- See example/greet.hs for an example.
24module Servant.Docs
24module Servant.Docs
25 ( -- * 'HasDocs' class and key functions
25 ( -- * 'HasDocs' class and key functions
26− HasDocs(..), docs, markdown
26+ HasDocs(..), docs, pretty, markdown
27 -- * Generating docs with extra information
27 -- * Generating docs with extra information
28 , docsWith, docsWithIntros, docsWithOptions
28 , docsWith, docsWithIntros, docsWithOptions
29 , ExtraInfo(..), extraInfo
29 , ExtraInfo(..), extraInfo
servant-docs/src/Servant/Docs/Internal.hs+33
@@ -26,6 +26,8 @@ import Control.Arrow (second)
26import Control.Lens (makeLenses, over, traversed, (%~),
26import Control.Lens (makeLenses, over, traversed, (%~),
27 (&), (.~), (<>~), (^.), (|>))
27 (&), (.~), (<>~), (^.), (|>))
28import qualified Control.Monad.Omega as Omega
28import qualified Control.Monad.Omega as Omega
29+import Data.Aeson (ToJSON(..))
30+import Data.Aeson.Encode.Pretty (encodePretty)
29import Data.ByteString.Conversion (ToByteString, toByteString)
31import Data.ByteString.Conversion (ToByteString, toByteString)
30import Data.ByteString.Lazy.Char8 (ByteString)
32import Data.ByteString.Lazy.Char8 (ByteString)
31import qualified Data.CaseInsensitive as CI
33import qualified Data.CaseInsensitive as CI
@@ -367,6 +369,37 @@ docsWith opts intros (ExtraInfo endpoints) p =
367docsWithIntros :: HasDocs layout => [DocIntro] -> Proxy layout -> API
369docsWithIntros :: HasDocs layout => [DocIntro] -> Proxy layout -> API
368docsWithIntros intros = docsWith defaultDocOptions intros mempty
370docsWithIntros intros = docsWith defaultDocOptions intros mempty
369
371
372+-- | Prettify generated JSON documentation.
373+--
374+-- @
375+-- 'docs' ('pretty' ('Proxy' :: 'Proxy' MyAPI))
376+-- @
377+pretty :: Proxy layout -> Proxy (Pretty layout)
378+pretty Proxy = Proxy
379+
380+data PrettyJSON
381+
382+instance Accept PrettyJSON where
383+ contentType _ = "application" M.// "json"
384+
385+instance ToJSON a => MimeRender PrettyJSON a where
386+ mimeRender _ = encodePretty
387+
388+-- | Replace all JSON content types with PrettyJSON.
389+-- Kind-polymorphic so it can operate on kinds * and [*].
390+type family Pretty (layout :: k) :: k where
391+ Pretty (x :<|> y) = Pretty x :<|> Pretty y
392+ Pretty (x :> y) = Pretty x :> Pretty y
393+ Pretty (Get cs r) = Get (Pretty cs) r
394+ Pretty (Post cs r) = Post (Pretty cs) r
395+ Pretty (Put cs r) = Put (Pretty cs) r
396+ Pretty (Delete cs r) = Delete (Pretty cs) r
397+ Pretty (Patch cs r) = Patch (Pretty cs) r
398+ Pretty (ReqBody cs r) = ReqBody (Pretty cs) r
399+ Pretty (JSON ': xs) = PrettyJSON ': xs
400+ Pretty (x ': xs) = x ': Pretty xs
401+ Pretty x = x
402+
370-- | The class that abstracts away the impact of API combinators
403-- | The class that abstracts away the impact of API combinators
371-- on documentation generation.
404-- on documentation generation.
372class HasDocs layout where
405class HasDocs layout where