git.iko.soy/typeable/servant
on master help sign in
Merge pull request #289 from mitchellwrosen/master

Add 'pretty' to servant-docs

commit34298701208859a85bce0551327cd0d6e6689898 authorJulian Arni <jkarni@gmail.com> dateThu 10 Dec 2015, 23:48 (11 years ago) parentbf8959df1a6a2a
3 files changed+56−2
servant-docs/servant-docs.cabal+3 servant-docs/src/Servant/Docs.hs+3−2 servant-docs/src/Servant/Docs/Internal/Pretty.hs+50
servant-docs/servant-docs.cabal+3
@@ -29,8 +29,11 @@ library
2929 exposed-modules:
3030 Servant.Docs
3131 , Servant.Docs.Internal
32+ , Servant.Docs.Internal.Pretty
3233 build-depends:
3334 base >=4.7 && <5
35+ , aeson
36+ , aeson-pretty
3437 , bytestring
3538 , bytestring-conversion
3639 , case-insensitive
servant-docs/src/Servant/Docs.hs+3−2
@@ -23,7 +23,7 @@
2323-- See example/greet.hs for an example.
2424module Servant.Docs
2525 ( -- * 'HasDocs' class and key functions
26− HasDocs(..), docs, markdown
26+ HasDocs(..), docs, pretty, markdown
2727 -- * Generating docs with extra information
2828 , docsWith, docsWithIntros, docsWithOptions
2929 , ExtraInfo(..), extraInfo
@@ -53,4 +53,5 @@ module Servant.Docs
5353 , single
5454 ) where
5555
56−import Servant.Docs.Internal
56+import Servant.Docs.Internal
57+import Servant.Docs.Internal.Pretty
servant-docs/src/Servant/Docs/Internal/Pretty.hsnew file+50
@@ -0,0 +1,50 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE FlexibleInstances #-}
3+{-# LANGUAGE KindSignatures #-}
4+{-# LANGUAGE MultiParamTypeClasses #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE PolyKinds #-}
7+{-# LANGUAGE TypeOperators #-}
8+{-# LANGUAGE TypeFamilies #-}
9+
10+module Servant.Docs.Internal.Pretty where
11+
12+import Data.Aeson (ToJSON(..))
13+import Data.Aeson.Encode.Pretty (encodePretty)
14+import Data.Proxy (Proxy(Proxy))
15+import Network.HTTP.Media ((//))
16+import Servant.API
17+import Servant.API.ContentTypes
18+import Servant.Utils.Links
19+
20+-- | PrettyJSON content type.
21+data PrettyJSON
22+
23+instance Accept PrettyJSON where
24+ contentType _ = "application" // "json"
25+
26+instance ToJSON a => MimeRender PrettyJSON a where
27+ mimeRender _ = encodePretty
28+
29+-- | Prettify generated JSON documentation.
30+--
31+-- @
32+-- 'docs' ('pretty' ('Proxy' :: 'Proxy' MyAPI))
33+-- @
34+pretty :: Proxy layout -> Proxy (Pretty layout)
35+pretty Proxy = Proxy
36+
37+-- | Replace all JSON content types with PrettyJSON.
38+-- Kind-polymorphic so it can operate on kinds @*@ and @[*]@.
39+type family Pretty (layout :: k) :: k where
40+ Pretty (x :<|> y) = Pretty x :<|> Pretty y
41+ Pretty (x :> y) = Pretty x :> Pretty y
42+ Pretty (Get cs r) = Get (Pretty cs) r
43+ Pretty (Post cs r) = Post (Pretty cs) r
44+ Pretty (Put cs r) = Put (Pretty cs) r
45+ Pretty (Delete cs r) = Delete (Pretty cs) r
46+ Pretty (Patch cs r) = Patch (Pretty cs) r
47+ Pretty (ReqBody cs r) = ReqBody (Pretty cs) r
48+ Pretty (JSON ': xs) = PrettyJSON ': xs
49+ Pretty (x ': xs) = x ': Pretty xs
50+ Pretty x = x