2014-11-27 18:28:01 +01:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
2015-01-23 02:19:37 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2014-11-27 18:28:01 +01:00
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2015-01-04 16:38:50 +01:00
|
|
|
{-# LANGUAGE TupleSections #-}
|
2014-11-27 18:28:01 +01:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-01-04 16:38:50 +01:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2014-11-27 18:28:01 +01:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2015-01-04 16:38:50 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2014-11-27 18:28:01 +01:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2015-02-07 05:17:39 +01:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- | This module lets you get API docs for free. It lets generate
|
|
|
|
-- an 'API' from the type that represents your API using 'docs':
|
|
|
|
--
|
|
|
|
-- @docs :: 'HasDocs' api => 'Proxy' api -> 'API'@
|
|
|
|
--
|
2015-01-30 05:45:00 +01:00
|
|
|
-- Alternately, if you wish to add one or more introductions to your
|
|
|
|
-- documentation, use 'docsWithIntros':
|
|
|
|
--
|
|
|
|
-- @docsWithIntros :: 'HasDocs' api => [DocIntro] -> 'Proxy' api -> 'API'@
|
|
|
|
--
|
|
|
|
-- You can then call 'markdown' on the 'API' value:
|
2014-11-27 18:28:01 +01:00
|
|
|
--
|
|
|
|
-- @markdown :: 'API' -> String@
|
|
|
|
--
|
|
|
|
-- or define a custom pretty printer:
|
|
|
|
--
|
|
|
|
-- @yourPrettyDocs :: 'API' -> String -- or blaze-html's HTML, or ...@
|
|
|
|
--
|
|
|
|
-- The only thing you'll need to do will be to implement some classes
|
|
|
|
-- for your captures, get parameters and request or response bodies.
|
|
|
|
--
|
|
|
|
-- Here's a little (but complete) example that you can run to see the
|
|
|
|
-- markdown pretty printer in action:
|
|
|
|
--
|
2015-02-23 00:23:12 +01:00
|
|
|
-- > {-# LANGUAGE DataKinds #-}
|
|
|
|
-- > {-# LANGUAGE DeriveGeneric #-}
|
|
|
|
-- > {-# LANGUAGE FlexibleInstances #-}
|
|
|
|
-- > {-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
-- > {-# LANGUAGE OverloadedStrings #-}
|
|
|
|
-- > {-# LANGUAGE TypeOperators #-}
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > {-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
-- > import Data.Aeson
|
2014-11-27 18:28:01 +01:00
|
|
|
-- > import Data.Proxy
|
2015-02-23 00:23:12 +01:00
|
|
|
-- > import Data.String.Conversions
|
|
|
|
-- > import Data.Text (Text)
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > import GHC.Generics
|
|
|
|
-- > import Servant.API
|
|
|
|
-- > import Servant.Docs
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > -- * Example
|
|
|
|
-- >
|
|
|
|
-- > -- | A greet message data type
|
|
|
|
-- > newtype Greet = Greet Text
|
2014-11-27 18:28:01 +01:00
|
|
|
-- > deriving (Generic, Show)
|
|
|
|
-- >
|
2015-02-23 00:23:12 +01:00
|
|
|
-- > -- | We can get JSON support automatically. This will be used to parse
|
|
|
|
-- > -- and encode a Greeting as 'JSON'.
|
2014-11-27 18:28:01 +01:00
|
|
|
-- > instance FromJSON Greet
|
|
|
|
-- > instance ToJSON Greet
|
|
|
|
-- >
|
2015-02-23 00:23:12 +01:00
|
|
|
-- > -- | We can also implement 'MimeRender' for additional formats like 'PlainText'.
|
|
|
|
-- > instance MimeRender PlainText Greet where
|
|
|
|
-- > toByteString Proxy (Greet s) = "\"" <> cs s <> "\""
|
|
|
|
-- >
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > -- We add some useful annotations to our captures,
|
|
|
|
-- > -- query parameters and request body to make the docs
|
|
|
|
-- > -- really helpful.
|
|
|
|
-- > instance ToCapture (Capture "name" Text) where
|
|
|
|
-- > toCapture _ = DocCapture "name" "name of the person to greet"
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > instance ToCapture (Capture "greetid" Text) where
|
|
|
|
-- > toCapture _ = DocCapture "greetid" "identifier of the greet msg to remove"
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > instance ToParam (QueryParam "capital" Bool) where
|
|
|
|
-- > toParam _ =
|
|
|
|
-- > DocQueryParam "capital"
|
|
|
|
-- > ["true", "false"]
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > "Get the greeting message in uppercase (true) or not (false).\
|
|
|
|
-- > \Default is false."
|
|
|
|
-- > Normal
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
2015-01-30 05:57:56 +01:00
|
|
|
-- > instance ToParam (MatrixParam "lang" String) where
|
|
|
|
-- > toParam _ =
|
|
|
|
-- > DocQueryParam "lang"
|
|
|
|
-- > ["en", "sv", "fr"]
|
|
|
|
-- > "Get the greeting message selected language. Default is en."
|
|
|
|
-- > Normal
|
|
|
|
-- >
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > instance ToSample Greet where
|
|
|
|
-- > toSample = Just $ Greet "Hello, haskeller!"
|
|
|
|
-- >
|
|
|
|
-- > toSamples =
|
|
|
|
-- > [ ("If you use ?capital=true", Greet "HELLO, HASKELLER")
|
|
|
|
-- > , ("If you use ?capital=false", Greet "Hello, haskeller")
|
|
|
|
-- > ]
|
|
|
|
-- >
|
2015-02-18 02:49:08 +01:00
|
|
|
-- > -- We define some introductory sections, these will appear at the top of the
|
|
|
|
-- > -- documentation.
|
|
|
|
-- > --
|
|
|
|
-- > -- We pass them in with 'docsWith', below. If you only want to add
|
|
|
|
-- > -- introductions, you may use 'docsWithIntros'
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > intro1 :: DocIntro
|
|
|
|
-- > intro1 = DocIntro "On proper introductions." -- The title
|
|
|
|
-- > [ "Hello there."
|
|
|
|
-- > , "As documentation is usually written for humans, it's often useful \
|
|
|
|
-- > \to introduce concepts with a few words." ] -- Elements are paragraphs
|
|
|
|
-- >
|
|
|
|
-- > intro2 :: DocIntro
|
|
|
|
-- > intro2 = DocIntro "This title is below the last"
|
|
|
|
-- > [ "You'll also note that multiple intros are possible." ]
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- >
|
|
|
|
-- > -- API specification
|
|
|
|
-- > type TestApi =
|
2015-02-23 00:23:12 +01:00
|
|
|
-- > -- GET /hello/:name?capital={true, false} returns a Greet as JSON or PlainText
|
|
|
|
-- > "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, PlainText] Greet
|
2015-01-30 05:45:00 +01:00
|
|
|
-- >
|
|
|
|
-- > -- POST /greet with a Greet as JSON in the request body,
|
|
|
|
-- > -- returns a Greet as JSON
|
2015-02-19 00:59:24 +01:00
|
|
|
-- > :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
|
2015-01-30 05:45:00 +01:00
|
|
|
-- >
|
|
|
|
-- > -- DELETE /greet/:greetid
|
|
|
|
-- > :<|> "greet" :> Capture "greetid" Text :> Delete
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > testApi :: Proxy TestApi
|
|
|
|
-- > testApi = Proxy
|
|
|
|
-- >
|
2015-02-18 02:49:08 +01:00
|
|
|
-- > -- Build some extra information for the DELETE /greet/:greetid endpoint. We
|
|
|
|
-- > -- want to add documentation about a secret unicorn header and some extra
|
|
|
|
-- > -- notes.
|
|
|
|
-- > extra :: ExtraInfo TestApi
|
|
|
|
-- > extra =
|
2015-02-22 23:54:07 +01:00
|
|
|
-- > extraInfo (Proxy :: Proxy ("greet" :> Capture "greetid" Text :> Delete)) $
|
2015-02-18 02:49:08 +01:00
|
|
|
-- > defAction & headers <>~ ["unicorns"]
|
|
|
|
-- > & notes <>~ [ DocNote "Title" ["This is some text"]
|
|
|
|
-- > , DocNote "Second secton" ["And some more"]
|
|
|
|
-- > ]
|
|
|
|
-- >
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > -- Generate the data that lets us have API docs. This
|
|
|
|
-- > -- is derived from the type as well as from
|
|
|
|
-- > -- the 'ToCapture', 'ToParam' and 'ToSample' instances from above.
|
|
|
|
-- > --
|
2015-02-18 02:49:08 +01:00
|
|
|
-- > -- If you didn't want intros and extra information, you could just call:
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > --
|
2015-02-18 02:49:08 +01:00
|
|
|
-- > -- > docs testAPI :: API
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > docsGreet :: API
|
2015-02-18 02:49:08 +01:00
|
|
|
-- > docsGreet = docsWith [intro1, intro2] extra testApi
|
2014-11-27 18:28:01 +01:00
|
|
|
-- >
|
|
|
|
-- > main :: IO ()
|
2015-01-30 05:45:00 +01:00
|
|
|
-- > main = putStrLn $ markdown docsGreet
|
2014-11-27 18:28:01 +01:00
|
|
|
module Servant.Docs
|
|
|
|
( -- * 'HasDocs' class and key functions
|
2015-02-07 05:17:39 +01:00
|
|
|
HasDocs(..), docs, markdown
|
|
|
|
-- * Generating docs with extra information
|
2015-02-22 23:54:07 +01:00
|
|
|
, ExtraInfo(..), docsWith, docsWithIntros, extraInfo
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
, -- * Classes you need to implement for your types
|
|
|
|
ToSample(..)
|
|
|
|
, sampleByteString
|
2015-01-04 16:38:50 +01:00
|
|
|
, sampleByteStrings
|
2014-11-27 18:28:01 +01:00
|
|
|
, ToParam(..)
|
|
|
|
, ToCapture(..)
|
|
|
|
|
|
|
|
, -- * ADTs to represent an 'API'
|
|
|
|
Method(..)
|
|
|
|
, Endpoint, path, method, defEndpoint
|
|
|
|
, API, emptyAPI
|
|
|
|
, DocCapture(..), capSymbol, capDesc
|
|
|
|
, DocQueryParam(..), ParamKind(..), paramName, paramValues, paramDesc, paramKind
|
2015-01-23 02:19:37 +01:00
|
|
|
, DocNote(..), noteTitle, noteBody
|
2015-01-30 05:45:00 +01:00
|
|
|
, DocIntro(..)
|
2015-02-19 05:29:04 +01:00
|
|
|
, Response, respStatus, respTypes, respBody, defResponse
|
|
|
|
, Action, captures, headers, notes, params, rqtypes, rqbody, response, defAction
|
2014-11-27 18:28:01 +01:00
|
|
|
, single
|
|
|
|
) where
|
|
|
|
|
2015-02-19 02:55:00 +01:00
|
|
|
import Control.Applicative
|
2014-11-27 18:28:01 +01:00
|
|
|
import Control.Lens hiding (Action)
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.ByteString.Lazy.Char8 (ByteString)
|
|
|
|
import Data.Hashable
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
|
|
import Data.List
|
2015-02-19 02:55:00 +01:00
|
|
|
import Data.Maybe
|
2014-11-27 18:28:01 +01:00
|
|
|
import Data.Monoid
|
2015-02-19 02:55:00 +01:00
|
|
|
import Data.Ord (comparing)
|
2014-11-27 18:28:01 +01:00
|
|
|
import Data.Proxy
|
|
|
|
import Data.String.Conversions
|
2015-02-19 02:55:00 +01:00
|
|
|
import Data.Text (Text, pack, unpack)
|
2014-11-27 18:28:01 +01:00
|
|
|
import GHC.Generics
|
|
|
|
import GHC.TypeLits
|
2015-02-07 05:17:39 +01:00
|
|
|
import GHC.Exts(Constraint)
|
2014-12-10 16:43:43 +01:00
|
|
|
import Servant.API
|
2015-02-07 05:17:39 +01:00
|
|
|
import Servant.Utils.Links
|
2015-02-19 02:55:00 +01:00
|
|
|
import Servant.API.ContentTypes
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
import qualified Data.HashMap.Strict as HM
|
2015-02-19 02:55:00 +01:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Network.HTTP.Media as M
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | Supported HTTP request methods
|
|
|
|
data Method = DocDELETE -- ^ the DELETE method
|
|
|
|
| DocGET -- ^ the GET method
|
|
|
|
| DocPOST -- ^ the POST method
|
|
|
|
| DocPUT -- ^ the PUT method
|
2015-02-23 10:53:18 +01:00
|
|
|
deriving (Eq, Ord, Generic)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
instance Show Method where
|
|
|
|
show DocGET = "GET"
|
|
|
|
show DocPOST = "POST"
|
|
|
|
show DocDELETE = "DELETE"
|
|
|
|
show DocPUT = "PUT"
|
|
|
|
|
|
|
|
instance Hashable Method
|
|
|
|
|
|
|
|
-- | An 'Endpoint' type that holds the 'path' and the 'method'.
|
|
|
|
--
|
|
|
|
-- Gets used as the key in the 'API' hashmap. Modify 'defEndpoint'
|
|
|
|
-- or any 'Endpoint' value you want using the 'path' and 'method'
|
|
|
|
-- lenses to tweak.
|
|
|
|
--
|
|
|
|
-- @
|
|
|
|
-- λ> 'defEndpoint'
|
|
|
|
-- GET /
|
2015-01-02 19:06:34 +01:00
|
|
|
-- λ> 'defEndpoint' & 'path' '<>~' ["foo"]
|
2014-11-27 18:28:01 +01:00
|
|
|
-- GET /foo
|
2015-01-02 19:06:34 +01:00
|
|
|
-- λ> 'defEndpoint' & 'path' '<>~' ["foo"] & 'method' '.~' 'DocPOST'
|
2014-11-27 18:28:01 +01:00
|
|
|
-- POST /foo
|
|
|
|
-- @
|
|
|
|
data Endpoint = Endpoint
|
2015-01-02 19:06:34 +01:00
|
|
|
{ _path :: [String] -- type collected
|
|
|
|
, _method :: Method -- type collected
|
2015-02-23 10:53:18 +01:00
|
|
|
} deriving (Eq, Ord, Generic)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
instance Show Endpoint where
|
|
|
|
show (Endpoint p m) =
|
2015-01-02 19:06:34 +01:00
|
|
|
show m ++ " " ++ showPath p
|
|
|
|
|
|
|
|
-- |
|
|
|
|
-- Render a path as a '/'-delimited string
|
|
|
|
--
|
|
|
|
showPath :: [String] -> String
|
|
|
|
showPath [] = "/"
|
|
|
|
showPath ps = concatMap ('/' :) ps
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | An 'Endpoint' whose path is `"/"` and whose method is 'DocGET'
|
|
|
|
--
|
|
|
|
-- Here's how you can modify it:
|
|
|
|
--
|
|
|
|
-- @
|
|
|
|
-- λ> 'defEndpoint'
|
|
|
|
-- GET /
|
2015-01-02 19:06:34 +01:00
|
|
|
-- λ> 'defEndpoint' & 'path' '<>~' ["foo"]
|
2014-11-27 18:28:01 +01:00
|
|
|
-- GET /foo
|
2015-01-02 19:06:34 +01:00
|
|
|
-- λ> 'defEndpoint' & 'path' '<>~' ["foo"] & 'method' '.~' 'DocPOST'
|
2014-11-27 18:28:01 +01:00
|
|
|
-- POST /foo
|
|
|
|
-- @
|
|
|
|
defEndpoint :: Endpoint
|
2015-01-02 19:06:34 +01:00
|
|
|
defEndpoint = Endpoint [] DocGET
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
instance Hashable Endpoint
|
|
|
|
|
2015-01-23 02:19:37 +01:00
|
|
|
-- | Our API documentation type, a product of top-level information and a good
|
|
|
|
-- old hashmap from 'Endpoint' to 'Action'
|
|
|
|
data API = API
|
|
|
|
{ _apiIntros :: [DocIntro]
|
|
|
|
, _apiEndpoints :: HashMap Endpoint Action
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance Monoid API where
|
|
|
|
API a1 b1 `mappend` API a2 b2 = API (a1 <> a2) (b1 <> b2)
|
|
|
|
mempty = API mempty mempty
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- | An empty 'API'
|
|
|
|
emptyAPI :: API
|
2015-01-23 02:19:37 +01:00
|
|
|
emptyAPI = mempty
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | A type to represent captures. Holds the name of the capture
|
|
|
|
-- and a description.
|
|
|
|
--
|
|
|
|
-- Write a 'ToCapture' instance for your captured types.
|
|
|
|
data DocCapture = DocCapture
|
|
|
|
{ _capSymbol :: String -- type supplied
|
|
|
|
, _capDesc :: String -- user supplied
|
2015-02-23 10:53:18 +01:00
|
|
|
} deriving (Eq, Ord, Show)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | A type to represent a /GET/ parameter from the Query String. Holds its name,
|
|
|
|
-- the possible values (leave empty if there isn't a finite number of them),
|
|
|
|
-- and a description of how it influences the output or behavior.
|
|
|
|
--
|
|
|
|
-- Write a 'ToParam' instance for your GET parameter types
|
|
|
|
data DocQueryParam = DocQueryParam
|
|
|
|
{ _paramName :: String -- type supplied
|
|
|
|
, _paramValues :: [String] -- user supplied
|
|
|
|
, _paramDesc :: String -- user supplied
|
|
|
|
, _paramKind :: ParamKind
|
2015-02-23 10:53:18 +01:00
|
|
|
} deriving (Eq, Ord, Show)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-01-30 05:45:00 +01:00
|
|
|
-- | An introductory paragraph for your documentation. You can pass these to
|
|
|
|
-- 'docsWithIntros'.
|
2015-01-23 02:19:37 +01:00
|
|
|
data DocIntro = DocIntro
|
|
|
|
{ _introTitle :: String -- ^ Appears above the intro blob
|
|
|
|
, _introBody :: [String] -- ^ Each String is a paragraph.
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance Ord DocIntro where
|
|
|
|
compare = comparing _introTitle
|
|
|
|
|
|
|
|
-- | A type to represent extra notes that may be attached to an 'Action'.
|
|
|
|
--
|
|
|
|
-- This is intended to be used when writing your own HasDocs instances to
|
|
|
|
-- add extra sections to your endpoint's documentation.
|
|
|
|
data DocNote = DocNote
|
|
|
|
{ _noteTitle :: String
|
|
|
|
, _noteBody :: [String]
|
2015-02-23 10:53:18 +01:00
|
|
|
} deriving (Eq, Ord, Show)
|
2015-01-23 02:19:37 +01:00
|
|
|
|
2015-02-18 02:49:08 +01:00
|
|
|
-- | Type of extra information that a user may wish to "union" with their
|
|
|
|
-- documentation.
|
|
|
|
--
|
2015-02-22 23:54:07 +01:00
|
|
|
-- These are intended to be built using extraInfo.
|
2015-02-18 02:49:08 +01:00
|
|
|
-- Multiple ExtraInfo may be combined with the monoid instance.
|
|
|
|
newtype ExtraInfo layout = ExtraInfo (HashMap Endpoint Action)
|
|
|
|
instance Monoid (ExtraInfo a) where
|
|
|
|
mempty = ExtraInfo mempty
|
|
|
|
ExtraInfo a `mappend` ExtraInfo b =
|
|
|
|
ExtraInfo $ HM.unionWith combineAction a b
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- | Type of GET parameter:
|
|
|
|
--
|
|
|
|
-- - Normal corresponds to @QueryParam@, i.e your usual GET parameter
|
|
|
|
-- - List corresponds to @QueryParams@, i.e GET parameters with multiple values
|
|
|
|
-- - Flag corresponds to @QueryFlag@, i.e a value-less GET parameter
|
|
|
|
data ParamKind = Normal | List | Flag
|
2015-02-23 10:53:18 +01:00
|
|
|
deriving (Eq, Ord, Show)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-02-19 05:29:04 +01:00
|
|
|
-- | A type to represent an HTTP response. Has an 'Int' status, a list of
|
|
|
|
-- possible 'MediaType's, and a list of example 'ByteString' response bodies.
|
|
|
|
-- Tweak 'defResponse' using the 'respStatus', 'respTypes' and 'respBody'
|
|
|
|
-- lenses if you want.
|
2014-11-27 18:28:01 +01:00
|
|
|
--
|
|
|
|
-- If you want to respond with a non-empty response body, you'll most likely
|
|
|
|
-- want to write a 'ToSample' instance for the type that'll be represented
|
2015-02-19 05:29:04 +01:00
|
|
|
-- as encoded data in the response.
|
2014-11-27 18:28:01 +01:00
|
|
|
--
|
2015-02-19 05:29:04 +01:00
|
|
|
-- Can be tweaked with three lenses.
|
2014-11-27 18:28:01 +01:00
|
|
|
--
|
|
|
|
-- > λ> defResponse
|
2015-02-19 05:29:04 +01:00
|
|
|
-- > Response {_respStatus = 200, _respTypes = [], _respBody = []}
|
2015-01-04 16:38:50 +01:00
|
|
|
-- > λ> defResponse & respStatus .~ 204 & respBody .~ [("If everything goes well", "{ \"status\": \"ok\" }")]
|
2015-02-19 05:29:04 +01:00
|
|
|
-- > Response {_respStatus = 204, _respTypes = [], _respBody = [("If everything goes well", "{ \"status\": \"ok\" }")]}
|
2014-11-27 18:28:01 +01:00
|
|
|
data Response = Response
|
|
|
|
{ _respStatus :: Int
|
2015-02-19 05:29:04 +01:00
|
|
|
, _respTypes :: [M.MediaType]
|
2015-02-19 02:55:00 +01:00
|
|
|
, _respBody :: [(Text, M.MediaType, ByteString)]
|
2015-02-23 10:53:18 +01:00
|
|
|
} deriving (Eq, Ord, Show)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | Default response: status code 200, no response body.
|
|
|
|
--
|
|
|
|
-- Can be tweaked with two lenses.
|
|
|
|
--
|
|
|
|
-- > λ> defResponse
|
|
|
|
-- > Response {_respStatus = 200, _respBody = Nothing}
|
|
|
|
-- > λ> defResponse & respStatus .~ 204 & respBody .~ Just "[]"
|
|
|
|
-- > Response {_respStatus = 204, _respBody = Just "[]"}
|
|
|
|
defResponse :: Response
|
2015-02-19 05:29:04 +01:00
|
|
|
defResponse = Response 200 [] []
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- | A datatype that represents everything that can happen
|
|
|
|
-- at an endpoint, with its lenses:
|
|
|
|
--
|
|
|
|
-- - List of captures ('captures')
|
|
|
|
-- - List of GET parameters ('params')
|
|
|
|
-- - What the request body should look like, if any is requested ('rqbody')
|
|
|
|
-- - What the response should be if everything goes well ('response')
|
|
|
|
--
|
|
|
|
-- You can tweak an 'Action' (like the default 'defAction') with these lenses
|
|
|
|
-- to transform an action and add some information to it.
|
|
|
|
data Action = Action
|
2015-02-19 05:29:04 +01:00
|
|
|
{ _captures :: [DocCapture] -- type collected + user supplied info
|
|
|
|
, _headers :: [Text] -- type collected
|
|
|
|
, _params :: [DocQueryParam] -- type collected + user supplied info
|
|
|
|
, _notes :: [DocNote] -- user supplied
|
|
|
|
, _mxParams :: [(String, [DocQueryParam])] -- type collected + user supplied info
|
|
|
|
, _rqtypes :: [M.MediaType] -- type collected
|
|
|
|
, _rqbody :: [(M.MediaType, ByteString)] -- user supplied
|
|
|
|
, _response :: Response -- user supplied
|
2015-02-23 10:53:18 +01:00
|
|
|
} deriving (Eq, Ord, Show)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-02-07 05:17:39 +01:00
|
|
|
-- | Combine two Actions, we can't make a monoid as merging Response breaks the
|
|
|
|
-- laws.
|
|
|
|
--
|
|
|
|
-- As such, we invent a non-commutative, left associative operation
|
2015-02-23 00:23:12 +01:00
|
|
|
-- 'combineAction' to mush two together taking the response, body and content
|
|
|
|
-- types from the very left.
|
2015-02-07 05:17:39 +01:00
|
|
|
combineAction :: Action -> Action -> Action
|
2015-02-23 00:23:12 +01:00
|
|
|
Action c h p n m ts body resp `combineAction` Action c' h' p' n' m' _ _ _ =
|
|
|
|
Action (c <> c') (h <> h') (p <> p') (n <> n') (m <> m') ts body resp
|
2015-02-07 05:17:39 +01:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- Default 'Action'. Has no 'captures', no GET 'params', expects
|
|
|
|
-- no request body ('rqbody') and the typical response is 'defResponse'.
|
|
|
|
--
|
|
|
|
-- Tweakable with lenses.
|
|
|
|
--
|
|
|
|
-- > λ> defAction
|
2015-01-06 14:30:01 +01:00
|
|
|
-- > Action {_captures = [], _headers = [], _params = [], _mxParams = [], _rqbody = Nothing, _response = Response {_respStatus = 200, _respBody = Nothing}}
|
2014-11-27 18:28:01 +01:00
|
|
|
-- > λ> defAction & response.respStatus .~ 201
|
2015-01-06 14:30:01 +01:00
|
|
|
-- > Action {_captures = [], _headers = [], _params = [], _mxParams = [], _rqbody = Nothing, _response = Response {_respStatus = 201, _respBody = Nothing}}
|
2014-11-27 18:28:01 +01:00
|
|
|
defAction :: Action
|
|
|
|
defAction =
|
|
|
|
Action []
|
2015-01-23 02:19:37 +01:00
|
|
|
[]
|
2014-12-08 13:07:34 +01:00
|
|
|
[]
|
2014-11-27 18:28:01 +01:00
|
|
|
[]
|
2015-01-30 05:57:56 +01:00
|
|
|
[]
|
2015-02-19 05:29:04 +01:00
|
|
|
[]
|
|
|
|
[]
|
2014-11-27 18:28:01 +01:00
|
|
|
defResponse
|
|
|
|
|
|
|
|
-- | Create an API that's comprised of a single endpoint.
|
|
|
|
-- 'API' is a 'Monoid', so combine multiple endpoints with
|
|
|
|
-- 'mappend' or '<>'.
|
|
|
|
single :: Endpoint -> Action -> API
|
2015-01-23 02:19:37 +01:00
|
|
|
single e a = API mempty (HM.singleton e a)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- gimme some lenses
|
2015-01-23 02:19:37 +01:00
|
|
|
makeLenses ''API
|
2014-11-27 18:28:01 +01:00
|
|
|
makeLenses ''Endpoint
|
|
|
|
makeLenses ''DocCapture
|
|
|
|
makeLenses ''DocQueryParam
|
2015-01-23 02:19:37 +01:00
|
|
|
makeLenses ''DocIntro
|
|
|
|
makeLenses ''DocNote
|
2014-11-27 18:28:01 +01:00
|
|
|
makeLenses ''Response
|
|
|
|
makeLenses ''Action
|
|
|
|
|
2015-01-30 05:45:00 +01:00
|
|
|
-- | Generate the docs for a given API that implements 'HasDocs'. This is the
|
|
|
|
-- default way to create documentation.
|
2014-11-27 18:28:01 +01:00
|
|
|
docs :: HasDocs layout => Proxy layout -> API
|
|
|
|
docs p = docsFor p (defEndpoint, defAction)
|
|
|
|
|
2015-02-07 05:17:39 +01:00
|
|
|
-- | Closed type family, check if endpoint is exactly within API.
|
|
|
|
|
|
|
|
-- We aren't sure what affects how an Endpoint is built up, so we require an
|
|
|
|
-- exact match.
|
|
|
|
type family IsIn (endpoint :: *) (api :: *) :: Constraint where
|
|
|
|
IsIn e (sa :<|> sb) = Or (IsIn e sa) (IsIn e sb)
|
|
|
|
IsIn (e :> sa) (e :> sb) = IsIn sa sb
|
|
|
|
IsIn e e = ()
|
|
|
|
|
2015-02-18 02:49:08 +01:00
|
|
|
-- | Create an 'ExtraInfo' that is garunteed to be within the given API layout.
|
|
|
|
--
|
|
|
|
-- The safety here is to ensure that you only add custom documentation to an
|
|
|
|
-- endpoint that actually exists within your API.
|
|
|
|
--
|
|
|
|
-- > extra :: ExtraInfo TestApi
|
|
|
|
-- > extra =
|
2015-02-22 23:54:07 +01:00
|
|
|
-- > extraInfo (Proxy :: Proxy ("greet" :> Capture "greetid" Text :> Delete)) $
|
2015-02-18 02:49:08 +01:00
|
|
|
-- > defAction & headers <>~ ["unicorns"]
|
|
|
|
-- > & notes <>~ [ DocNote "Title" ["This is some text"]
|
|
|
|
-- > , DocNote "Second secton" ["And some more"]
|
|
|
|
-- > ]
|
2015-02-07 05:17:39 +01:00
|
|
|
|
2015-02-22 23:54:07 +01:00
|
|
|
extraInfo :: (IsIn endpoint layout, HasLink endpoint, HasDocs endpoint)
|
2015-02-23 00:23:12 +01:00
|
|
|
=> Proxy endpoint -> Action -> ExtraInfo layout
|
2015-02-22 23:54:07 +01:00
|
|
|
extraInfo p action =
|
2015-02-07 05:17:39 +01:00
|
|
|
let api = docsFor p (defEndpoint, defAction)
|
|
|
|
-- Assume one endpoint, HasLink constraint means that we should only ever
|
|
|
|
-- point at one endpoint.
|
|
|
|
in ExtraInfo $ api ^. apiEndpoints & traversed .~ action
|
|
|
|
|
2015-02-18 02:49:08 +01:00
|
|
|
-- | Generate documentation given some extra introductions (in the form of
|
|
|
|
-- 'DocInfo') and some extra endpoint documentation (in the form of
|
|
|
|
-- 'ExtraInfo'.
|
2015-02-07 05:17:39 +01:00
|
|
|
--
|
2015-02-18 02:49:08 +01:00
|
|
|
-- The extra introductions will be prepended to the top of the documentation,
|
|
|
|
-- before the specific endpoint documentation. The extra endpoint documentation
|
|
|
|
-- will be "unioned" with the automatically generated endpoint documentation.
|
|
|
|
--
|
|
|
|
-- You are expected to build up the ExtraInfo with the Monoid instance and
|
2015-02-22 23:54:07 +01:00
|
|
|
-- 'extraInfo'.
|
2015-02-07 05:17:39 +01:00
|
|
|
--
|
2015-02-18 02:49:08 +01:00
|
|
|
-- If you only want to add an introduction, use 'docsWithIntros'.
|
2015-02-07 05:17:39 +01:00
|
|
|
docsWith :: HasDocs layout
|
|
|
|
=> [DocIntro]
|
|
|
|
-> ExtraInfo layout
|
|
|
|
-> Proxy layout
|
|
|
|
-> API
|
|
|
|
docsWith intros (ExtraInfo endpoints) p =
|
|
|
|
docs p & apiIntros <>~ intros
|
|
|
|
& apiEndpoints %~ HM.unionWith combineAction endpoints
|
|
|
|
|
|
|
|
|
2015-01-30 05:45:00 +01:00
|
|
|
-- | Generate the docs for a given API that implements 'HasDocs' with with any
|
|
|
|
-- number of introduction(s)
|
|
|
|
docsWithIntros :: HasDocs layout => [DocIntro] -> Proxy layout -> API
|
2015-02-07 05:17:39 +01:00
|
|
|
docsWithIntros intros = docsWith intros mempty
|
2015-01-30 05:45:00 +01:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- | The class that abstracts away the impact of API combinators
|
|
|
|
-- on documentation generation.
|
|
|
|
class HasDocs layout where
|
|
|
|
docsFor :: Proxy layout -> (Endpoint, Action) -> API
|
|
|
|
|
|
|
|
-- | The class that lets us display a sample JSON input or output
|
|
|
|
-- when generating documentation for endpoints that either:
|
|
|
|
--
|
|
|
|
-- - expect a request body, or
|
|
|
|
-- - return a non empty response body
|
|
|
|
--
|
|
|
|
-- Example of an instance:
|
|
|
|
--
|
|
|
|
-- > {-# LANGUAGE DeriveGeneric #-}
|
|
|
|
-- > {-# LANGUAGE OverloadedStrings #-}
|
|
|
|
-- >
|
|
|
|
-- > import Data.Aeson
|
|
|
|
-- > import Data.Text
|
|
|
|
-- > import GHC.Generics
|
|
|
|
-- >
|
|
|
|
-- > data Greet = Greet { _msg :: Text }
|
|
|
|
-- > deriving (Generic, Show)
|
|
|
|
-- >
|
|
|
|
-- > instance FromJSON Greet
|
|
|
|
-- > instance ToJSON Greet
|
|
|
|
-- >
|
|
|
|
-- > instance ToSample Greet where
|
|
|
|
-- > toSample = Just g
|
|
|
|
-- >
|
|
|
|
-- > where g = Greet "Hello, haskeller!"
|
2015-01-04 16:38:50 +01:00
|
|
|
--
|
|
|
|
-- You can also instantiate this class using 'toSamples' instead of
|
|
|
|
-- 'toSample': it lets you specify different responses along with
|
|
|
|
-- some context (as 'Text') that explains when you're supposed to
|
2015-02-18 02:49:08 +01:00
|
|
|
-- get the corresponding response.
|
2014-11-27 18:28:01 +01:00
|
|
|
class ToJSON a => ToSample a where
|
2015-01-04 16:38:50 +01:00
|
|
|
{-# MINIMAL (toSample | toSamples) #-}
|
2014-11-27 18:28:01 +01:00
|
|
|
toSample :: Maybe a
|
2015-02-19 02:55:00 +01:00
|
|
|
toSample = snd <$> listToMaybe samples
|
2015-01-04 16:38:50 +01:00
|
|
|
where samples = toSamples :: [(Text, a)]
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-01-04 16:38:50 +01:00
|
|
|
toSamples :: [(Text, a)]
|
|
|
|
toSamples = maybe [] (return . ("",)) s
|
|
|
|
where s = toSample :: Maybe a
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-02-19 02:55:00 +01:00
|
|
|
-- | Synthesise a sample value of a type, encoded in the specified media types.
|
|
|
|
sampleByteString
|
|
|
|
:: forall ctypes a. (ToSample a, IsNonEmpty ctypes, AllMimeRender ctypes a)
|
|
|
|
=> Proxy ctypes
|
|
|
|
-> Proxy a
|
2015-02-19 05:29:04 +01:00
|
|
|
-> [(M.MediaType, ByteString)]
|
2015-02-19 02:55:00 +01:00
|
|
|
sampleByteString ctypes@Proxy Proxy =
|
2015-02-19 22:03:11 +01:00
|
|
|
maybe [] (allMimeRender ctypes) (toSample :: Maybe a)
|
2015-02-19 02:55:00 +01:00
|
|
|
|
|
|
|
-- | Synthesise a list of sample values of a particular type, encoded in the
|
|
|
|
-- specified media types.
|
|
|
|
sampleByteStrings
|
|
|
|
:: forall ctypes a. (ToSample a, IsNonEmpty ctypes, AllMimeRender ctypes a)
|
|
|
|
=> Proxy ctypes
|
|
|
|
-> Proxy a
|
|
|
|
-> [(Text, M.MediaType, ByteString)]
|
|
|
|
sampleByteStrings ctypes@Proxy Proxy =
|
|
|
|
let samples = toSamples :: [(Text, a)]
|
2015-02-19 22:03:11 +01:00
|
|
|
enc (t, s) = uncurry (t,,) <$> allMimeRender ctypes s
|
2015-02-19 02:55:00 +01:00
|
|
|
in concatMap enc samples
|
2015-01-04 16:38:50 +01:00
|
|
|
|
2015-02-19 05:29:04 +01:00
|
|
|
-- | Generate a list of 'MediaType' values describing the content types
|
|
|
|
-- accepted by an API component.
|
|
|
|
class SupportedTypes (list :: [*]) where
|
|
|
|
supportedTypes :: Proxy list -> [M.MediaType]
|
|
|
|
|
|
|
|
instance SupportedTypes '[] where
|
|
|
|
supportedTypes Proxy = []
|
|
|
|
|
2015-02-19 22:05:37 +01:00
|
|
|
instance (Accept ctype, SupportedTypes rest) => SupportedTypes (ctype ': rest)
|
|
|
|
where
|
2015-02-19 05:29:04 +01:00
|
|
|
supportedTypes Proxy =
|
2015-02-19 22:05:37 +01:00
|
|
|
contentType (Proxy :: Proxy ctype) : supportedTypes (Proxy :: Proxy rest)
|
2015-01-04 16:38:50 +01:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- | The class that helps us automatically get documentation
|
|
|
|
-- for GET parameters.
|
|
|
|
--
|
|
|
|
-- Example of an instance:
|
|
|
|
--
|
|
|
|
-- > instance ToParam (QueryParam "capital" Bool) where
|
|
|
|
-- > toParam _ =
|
|
|
|
-- > DocQueryParam "capital"
|
|
|
|
-- > ["true", "false"]
|
|
|
|
-- > "Get the greeting message in uppercase (true) or not (false). Default is false."
|
|
|
|
class ToParam t where
|
|
|
|
toParam :: Proxy t -> DocQueryParam
|
|
|
|
|
|
|
|
-- | The class that helps us automatically get documentation
|
|
|
|
-- for URL captures.
|
|
|
|
--
|
|
|
|
-- Example of an instance:
|
|
|
|
--
|
|
|
|
-- > instance ToCapture (Capture "name" Text) where
|
|
|
|
-- > toCapture _ = DocCapture "name" "name of the person to greet"
|
|
|
|
class ToCapture c where
|
|
|
|
toCapture :: Proxy c -> DocCapture
|
|
|
|
|
|
|
|
-- | Generate documentation in Markdown format for
|
|
|
|
-- the given 'API'.
|
|
|
|
markdown :: API -> String
|
2015-01-23 02:19:37 +01:00
|
|
|
markdown api = unlines $
|
|
|
|
introsStr (api ^. apiIntros)
|
2015-02-23 10:53:18 +01:00
|
|
|
++ (concatMap (uncurry printEndpoint) . sort . HM.toList $ api ^. apiEndpoints)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
where printEndpoint :: Endpoint -> Action -> [String]
|
|
|
|
printEndpoint endpoint action =
|
|
|
|
str :
|
|
|
|
"" :
|
2015-01-23 02:19:37 +01:00
|
|
|
notesStr (action ^. notes) ++
|
2014-11-27 18:28:01 +01:00
|
|
|
capturesStr (action ^. captures) ++
|
2015-01-06 14:30:01 +01:00
|
|
|
mxParamsStr (action ^. mxParams) ++
|
2014-12-08 13:07:34 +01:00
|
|
|
headersStr (action ^. headers) ++
|
2014-11-27 18:28:01 +01:00
|
|
|
paramsStr (action ^. params) ++
|
2015-02-19 05:29:04 +01:00
|
|
|
rqbodyStr (action ^. rqtypes) (action ^. rqbody) ++
|
2014-11-27 18:28:01 +01:00
|
|
|
responseStr (action ^. response) ++
|
|
|
|
[]
|
|
|
|
|
2015-01-23 02:19:37 +01:00
|
|
|
where str = "## " ++ show (endpoint^.method)
|
|
|
|
++ " " ++ showPath (endpoint^.path)
|
|
|
|
|
|
|
|
introsStr :: [DocIntro] -> [String]
|
|
|
|
introsStr = concatMap introStr
|
|
|
|
|
|
|
|
introStr :: DocIntro -> [String]
|
|
|
|
introStr i =
|
|
|
|
("#### " ++ i ^. introTitle) :
|
|
|
|
"" :
|
2015-02-19 05:29:04 +01:00
|
|
|
intersperse "" (i ^. introBody) ++
|
2015-01-23 02:19:37 +01:00
|
|
|
"" :
|
|
|
|
[]
|
|
|
|
|
|
|
|
notesStr :: [DocNote] -> [String]
|
|
|
|
notesStr = concatMap noteStr
|
|
|
|
|
|
|
|
noteStr :: DocNote -> [String]
|
|
|
|
noteStr nt =
|
|
|
|
("#### " ++ nt ^. noteTitle) :
|
|
|
|
"" :
|
|
|
|
intersperse "" (nt ^. noteBody) ++
|
|
|
|
"" :
|
|
|
|
[]
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
capturesStr :: [DocCapture] -> [String]
|
|
|
|
capturesStr [] = []
|
|
|
|
capturesStr l =
|
2015-01-23 02:19:37 +01:00
|
|
|
"#### Captures:" :
|
2014-11-27 18:28:01 +01:00
|
|
|
"" :
|
|
|
|
map captureStr l ++
|
|
|
|
"" :
|
|
|
|
[]
|
2015-02-19 05:29:04 +01:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
captureStr cap =
|
|
|
|
"- *" ++ (cap ^. capSymbol) ++ "*: " ++ (cap ^. capDesc)
|
|
|
|
|
2015-01-06 14:30:01 +01:00
|
|
|
mxParamsStr :: [(String, [DocQueryParam])] -> [String]
|
|
|
|
mxParamsStr [] = []
|
|
|
|
mxParamsStr l =
|
2015-01-30 05:57:56 +01:00
|
|
|
"#### Matrix Parameters:" :
|
2015-01-06 14:30:01 +01:00
|
|
|
"" :
|
2015-02-18 02:49:08 +01:00
|
|
|
map segmentStr l
|
2015-01-06 14:30:01 +01:00
|
|
|
segmentStr :: (String, [DocQueryParam]) -> String
|
|
|
|
segmentStr (segment, l) = unlines $
|
2015-01-30 05:57:56 +01:00
|
|
|
("**" ++ segment ++ "**:") :
|
2015-01-06 14:30:01 +01:00
|
|
|
"" :
|
|
|
|
map paramStr l ++
|
|
|
|
"" :
|
|
|
|
[]
|
|
|
|
|
2014-12-08 13:07:34 +01:00
|
|
|
headersStr :: [Text] -> [String]
|
|
|
|
headersStr [] = []
|
|
|
|
headersStr l = [""] ++ map headerStr l ++ [""]
|
|
|
|
|
|
|
|
where headerStr hname = "- This endpoint is sensitive to the value of the **"
|
|
|
|
++ unpack hname ++ "** HTTP header."
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
paramsStr :: [DocQueryParam] -> [String]
|
|
|
|
paramsStr [] = []
|
|
|
|
paramsStr l =
|
2015-01-23 02:19:37 +01:00
|
|
|
"#### GET Parameters:" :
|
2014-11-27 18:28:01 +01:00
|
|
|
"" :
|
|
|
|
map paramStr l ++
|
|
|
|
"" :
|
|
|
|
[]
|
2015-02-19 05:29:04 +01:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
paramStr param = unlines $
|
2015-02-19 05:29:04 +01:00
|
|
|
("- " ++ param ^. paramName) :
|
2014-11-27 18:28:01 +01:00
|
|
|
(if (not (null values) || param ^. paramKind /= Flag)
|
|
|
|
then [" - **Values**: *" ++ intercalate ", " values ++ "*"]
|
|
|
|
else []) ++
|
|
|
|
(" - **Description**: " ++ param ^. paramDesc) :
|
|
|
|
(if (param ^. paramKind == List)
|
|
|
|
then [" - This parameter is a **list**. All GET parameters with the name "
|
|
|
|
++ param ^. paramName ++ "[] will forward their values in a list to the handler."]
|
|
|
|
else []) ++
|
|
|
|
(if (param ^. paramKind == Flag)
|
|
|
|
then [" - This parameter is a **flag**. This means no value is expected to be associated to this parameter."]
|
|
|
|
else []) ++
|
|
|
|
[]
|
|
|
|
|
|
|
|
where values = param ^. paramValues
|
|
|
|
|
2015-02-19 05:29:04 +01:00
|
|
|
rqbodyStr :: [M.MediaType] -> [(M.MediaType, ByteString)]-> [String]
|
|
|
|
rqbodyStr [] [] = []
|
|
|
|
rqbodyStr types samples =
|
|
|
|
["#### Request:", ""]
|
|
|
|
<> formatTypes types
|
|
|
|
<> concatMap formatBody samples
|
|
|
|
|
|
|
|
formatTypes [] = []
|
2015-02-23 00:23:12 +01:00
|
|
|
formatTypes ts = ["- Supported content types are:", ""]
|
2015-02-19 05:29:04 +01:00
|
|
|
<> map (\t -> " - `" <> show t <> "`") ts
|
|
|
|
<> [""]
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2015-02-19 02:55:00 +01:00
|
|
|
formatBody (m, b) =
|
2015-02-22 23:38:19 +01:00
|
|
|
"- Example: `" <> cs (show m) <> "`" :
|
2015-02-19 02:55:00 +01:00
|
|
|
contentStr m b
|
|
|
|
|
|
|
|
markdownForType mime_type =
|
|
|
|
case (M.mainType mime_type, M.subType mime_type) of
|
|
|
|
("text", "html") -> "html"
|
|
|
|
("application", "xml") -> "xml"
|
|
|
|
("application", "json") -> "javascript"
|
|
|
|
("application", "javascript") -> "javascript"
|
2015-02-19 05:29:04 +01:00
|
|
|
("text", "css") -> "css"
|
2015-02-19 02:55:00 +01:00
|
|
|
(_, _) -> ""
|
|
|
|
|
|
|
|
contentStr mime_type body =
|
2014-11-27 18:28:01 +01:00
|
|
|
"" :
|
2015-02-23 00:23:12 +01:00
|
|
|
"```" <> markdownForType mime_type :
|
2015-02-19 02:55:00 +01:00
|
|
|
cs body :
|
2014-11-27 18:28:01 +01:00
|
|
|
"```" :
|
|
|
|
"" :
|
|
|
|
[]
|
|
|
|
|
|
|
|
responseStr :: Response -> [String]
|
|
|
|
responseStr resp =
|
2015-01-23 02:19:37 +01:00
|
|
|
"#### Response:" :
|
2014-11-27 18:28:01 +01:00
|
|
|
"" :
|
2015-02-19 05:29:04 +01:00
|
|
|
("- Status code " ++ show (resp ^. respStatus)) :
|
|
|
|
"" :
|
|
|
|
formatTypes (resp ^. respTypes) ++
|
2015-01-04 16:38:50 +01:00
|
|
|
bodies
|
|
|
|
|
|
|
|
where bodies = case resp ^. respBody of
|
2015-02-19 05:29:04 +01:00
|
|
|
[] -> ["- No response body\n"]
|
|
|
|
[("", t, r)] -> "- Response body as below." : contentStr t r
|
2015-01-04 16:38:50 +01:00
|
|
|
xs ->
|
2015-02-19 05:29:04 +01:00
|
|
|
concatMap (\(ctx, t, r) -> ("- " <> T.unpack ctx) : contentStr t r) xs
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- * Instances
|
|
|
|
|
|
|
|
-- | The generated docs for @a ':<|>' b@ just appends the docs
|
|
|
|
-- for @a@ with the docs for @b@.
|
|
|
|
instance (HasDocs layout1, HasDocs layout2)
|
|
|
|
=> HasDocs (layout1 :<|> layout2) where
|
|
|
|
|
|
|
|
docsFor Proxy (ep, action) = docsFor p1 (ep, action) <> docsFor p2 (ep, action)
|
|
|
|
|
|
|
|
where p1 :: Proxy layout1
|
|
|
|
p1 = Proxy
|
|
|
|
|
|
|
|
p2 :: Proxy layout2
|
|
|
|
p2 = Proxy
|
|
|
|
|
|
|
|
-- | @"books" :> 'Capture' "isbn" Text@ will appear as
|
|
|
|
-- @/books/:isbn@ in the docs.
|
|
|
|
instance (KnownSymbol sym, ToCapture (Capture sym a), HasDocs sublayout)
|
|
|
|
=> HasDocs (Capture sym a :> sublayout) where
|
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint', action')
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
|
|
|
captureP = Proxy :: Proxy (Capture sym a)
|
|
|
|
|
|
|
|
action' = over captures (|> toCapture captureP) action
|
2015-01-02 19:06:34 +01:00
|
|
|
endpoint' = over path (\p -> p ++ [":" ++ symbolVal symP]) endpoint
|
2014-11-27 18:28:01 +01:00
|
|
|
symP = Proxy :: Proxy sym
|
|
|
|
|
|
|
|
|
|
|
|
instance HasDocs Delete where
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
single endpoint' action'
|
|
|
|
|
|
|
|
where endpoint' = endpoint & method .~ DocDELETE
|
|
|
|
|
2015-01-04 16:38:50 +01:00
|
|
|
action' = action & response.respBody .~ []
|
2014-11-27 18:28:01 +01:00
|
|
|
& response.respStatus .~ 204
|
|
|
|
|
2015-02-19 05:29:04 +01:00
|
|
|
instance (ToSample a, IsNonEmpty cts, AllMimeRender cts a, SupportedTypes cts)
|
|
|
|
=> HasDocs (Get cts a) where
|
2014-11-27 18:28:01 +01:00
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
single endpoint' action'
|
|
|
|
|
|
|
|
where endpoint' = endpoint & method .~ DocGET
|
2015-02-19 02:55:00 +01:00
|
|
|
action' = action & response.respBody .~ sampleByteStrings t p
|
2015-02-19 05:29:04 +01:00
|
|
|
& response.respTypes .~ supportedTypes t
|
2015-02-19 02:55:00 +01:00
|
|
|
t = Proxy :: Proxy cts
|
2014-11-27 18:28:01 +01:00
|
|
|
p = Proxy :: Proxy a
|
|
|
|
|
2014-12-08 13:07:34 +01:00
|
|
|
instance (KnownSymbol sym, HasDocs sublayout)
|
|
|
|
=> HasDocs (Header sym a :> sublayout) where
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint, action')
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
|
|
|
action' = over headers (|> headername) action
|
|
|
|
headername = pack $ symbolVal (Proxy :: Proxy sym)
|
|
|
|
|
2015-02-19 05:29:04 +01:00
|
|
|
instance (ToSample a, IsNonEmpty cts, AllMimeRender cts a, SupportedTypes cts)
|
|
|
|
=> HasDocs (Post cts a) where
|
2014-11-27 18:28:01 +01:00
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
single endpoint' action'
|
|
|
|
|
|
|
|
where endpoint' = endpoint & method .~ DocPOST
|
2015-02-19 02:55:00 +01:00
|
|
|
action' = action & response.respBody .~ sampleByteStrings t p
|
2015-02-19 05:29:04 +01:00
|
|
|
& response.respTypes .~ supportedTypes t
|
2014-11-27 18:28:01 +01:00
|
|
|
& response.respStatus .~ 201
|
2015-02-19 02:55:00 +01:00
|
|
|
t = Proxy :: Proxy cts
|
2014-11-27 18:28:01 +01:00
|
|
|
p = Proxy :: Proxy a
|
|
|
|
|
2015-02-19 05:29:04 +01:00
|
|
|
instance (ToSample a, IsNonEmpty cts, AllMimeRender cts a, SupportedTypes cts)
|
|
|
|
=> HasDocs (Put cts a) where
|
2014-11-27 18:28:01 +01:00
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
single endpoint' action'
|
|
|
|
|
|
|
|
where endpoint' = endpoint & method .~ DocPUT
|
2015-02-19 02:55:00 +01:00
|
|
|
action' = action & response.respBody .~ sampleByteStrings t p
|
2015-02-19 05:29:04 +01:00
|
|
|
& response.respTypes .~ supportedTypes t
|
2014-11-27 18:28:01 +01:00
|
|
|
& response.respStatus .~ 200
|
2015-02-19 02:55:00 +01:00
|
|
|
t = Proxy :: Proxy cts
|
2014-11-27 18:28:01 +01:00
|
|
|
p = Proxy :: Proxy a
|
|
|
|
|
|
|
|
instance (KnownSymbol sym, ToParam (QueryParam sym a), HasDocs sublayout)
|
|
|
|
=> HasDocs (QueryParam sym a :> sublayout) where
|
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint, action')
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
|
|
|
paramP = Proxy :: Proxy (QueryParam sym a)
|
|
|
|
action' = over params (|> toParam paramP) action
|
|
|
|
|
|
|
|
instance (KnownSymbol sym, ToParam (QueryParams sym a), HasDocs sublayout)
|
|
|
|
=> HasDocs (QueryParams sym a :> sublayout) where
|
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint, action')
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
|
|
|
paramP = Proxy :: Proxy (QueryParams sym a)
|
|
|
|
action' = over params (|> toParam paramP) action
|
|
|
|
|
|
|
|
|
|
|
|
instance (KnownSymbol sym, ToParam (QueryFlag sym), HasDocs sublayout)
|
|
|
|
=> HasDocs (QueryFlag sym :> sublayout) where
|
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint, action')
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
|
|
|
paramP = Proxy :: Proxy (QueryFlag sym)
|
|
|
|
action' = over params (|> toParam paramP) action
|
|
|
|
|
2015-01-01 23:42:06 +01:00
|
|
|
|
2015-01-06 14:30:01 +01:00
|
|
|
instance (KnownSymbol sym, ToParam (MatrixParam sym a), HasDocs sublayout)
|
2015-01-01 23:42:06 +01:00
|
|
|
=> HasDocs (MatrixParam sym a :> sublayout) where
|
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
2015-01-06 14:30:01 +01:00
|
|
|
docsFor sublayoutP (endpoint', action')
|
2015-01-01 23:42:06 +01:00
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
2015-01-06 14:30:01 +01:00
|
|
|
paramP = Proxy :: Proxy (MatrixParam sym a)
|
|
|
|
segment = endpoint ^. (path._last)
|
|
|
|
segment' = action ^. (mxParams._last._1)
|
|
|
|
endpoint' = over (path._last) (\p -> p ++ ";" ++ symbolVal symP ++ "=<value>") endpoint
|
|
|
|
|
|
|
|
action' = if segment' /= segment
|
|
|
|
-- This is the first matrix parameter for this segment, insert a new entry into the mxParams list
|
|
|
|
then over mxParams (|> (segment, [toParam paramP])) action
|
|
|
|
-- We've already inserted a matrix parameter for this segment, append to the existing list
|
|
|
|
else action & mxParams._last._2 <>~ [toParam paramP]
|
2015-01-01 23:42:06 +01:00
|
|
|
symP = Proxy :: Proxy sym
|
|
|
|
|
|
|
|
|
|
|
|
instance (KnownSymbol sym, {- ToParam (MatrixParams sym a), -} HasDocs sublayout)
|
|
|
|
=> HasDocs (MatrixParams sym a :> sublayout) where
|
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint', action)
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
2015-01-06 14:30:01 +01:00
|
|
|
endpoint' = over path (\p -> p ++ [";" ++ symbolVal symP ++ "=<value>"]) endpoint
|
2015-01-01 23:42:06 +01:00
|
|
|
symP = Proxy :: Proxy sym
|
|
|
|
|
|
|
|
|
|
|
|
instance (KnownSymbol sym, {- ToParam (MatrixFlag sym), -} HasDocs sublayout)
|
|
|
|
=> HasDocs (MatrixFlag sym :> sublayout) where
|
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint', action)
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
|
|
|
|
2015-01-06 14:30:01 +01:00
|
|
|
endpoint' = over path (\p -> p ++ [";" ++ symbolVal symP]) endpoint
|
2015-01-01 23:42:06 +01:00
|
|
|
symP = Proxy :: Proxy sym
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
instance HasDocs Raw where
|
|
|
|
docsFor _proxy (endpoint, action) =
|
|
|
|
single endpoint action
|
|
|
|
|
2015-02-19 02:55:00 +01:00
|
|
|
-- TODO: We use 'AllMimeRender' here because we need to be able to show the
|
|
|
|
-- example data. However, there's no reason to believe that the instances of
|
|
|
|
-- 'AllMimeUnrender' and 'AllMimeRender' actually agree (or to suppose that
|
|
|
|
-- both are even defined) for any particular type.
|
2015-02-19 05:29:04 +01:00
|
|
|
instance (ToSample a, IsNonEmpty cts, AllMimeRender cts a, HasDocs sublayout, SupportedTypes cts)
|
2015-02-19 00:59:24 +01:00
|
|
|
=> HasDocs (ReqBody cts a :> sublayout) where
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint, action')
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
2015-02-19 02:55:00 +01:00
|
|
|
action' = action & rqbody .~ sampleByteString t p
|
2015-02-19 05:29:04 +01:00
|
|
|
& rqtypes .~ supportedTypes t
|
2015-02-19 02:55:00 +01:00
|
|
|
t = Proxy :: Proxy cts
|
2014-11-27 18:28:01 +01:00
|
|
|
p = Proxy :: Proxy a
|
|
|
|
|
|
|
|
instance (KnownSymbol path, HasDocs sublayout) => HasDocs (path :> sublayout) where
|
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint', action)
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
2015-01-02 19:06:34 +01:00
|
|
|
endpoint' = endpoint & path <>~ [symbolVal pa]
|
2014-11-27 18:28:01 +01:00
|
|
|
pa = Proxy :: Proxy path
|
|
|
|
|
|
|
|
{-
|
|
|
|
|
|
|
|
-- | Serve your API's docs as markdown embedded in an html \<pre> tag.
|
|
|
|
--
|
|
|
|
-- > type MyApi = "users" :> Get [User]
|
|
|
|
-- > :<|> "docs :> Raw
|
|
|
|
-- >
|
|
|
|
-- > apiProxy :: Proxy MyApi
|
|
|
|
-- > apiProxy = Proxy
|
|
|
|
-- >
|
|
|
|
-- > server :: Server MyApi
|
|
|
|
-- > server = listUsers
|
|
|
|
-- > :<|> serveDocumentation apiProxy
|
|
|
|
serveDocumentation :: HasDocs api => Proxy api -> Server Raw
|
|
|
|
serveDocumentation proxy _request respond =
|
|
|
|
respond $ responseLBS ok200 [] $ cs $ toHtml $ markdown $ docs proxy
|
|
|
|
|
|
|
|
toHtml :: String -> String
|
|
|
|
toHtml md =
|
|
|
|
"<html>" ++
|
|
|
|
"<body>" ++
|
|
|
|
"<pre>" ++
|
|
|
|
md ++
|
|
|
|
"</pre>" ++
|
|
|
|
"</body>" ++
|
|
|
|
"</html>"
|
2014-12-20 21:58:07 +01:00
|
|
|
-}
|