on feef8ca help sign in
111 lines 3.9 kB history raw
1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE TypeOperators #-}
7{-# OPTIONS_GHC -fno-warn-orphans #-}
8import Control.Lens
9import Data.Aeson
10import Data.Proxy
11import Data.String.Conversions
12import Data.Text (Text)
13import GHC.Generics
14import Servant.API
15import Servant.Docs
17-- * Example
19-- | A greet message data type
20newtype Greet = Greet Text
21 deriving (Generic, Show)
23-- | We can get JSON support automatically. This will be used to parse
24-- and encode a Greeting as 'JSON'.
25instance FromJSON Greet
26instance ToJSON Greet
28-- | We can also implement 'MimeRender' for additional formats like 'PlainText'.
29instance MimeRender PlainText Greet where
30 mimeRender Proxy (Greet s) = "\"" <> cs s <> "\""
32-- We add some useful annotations to our captures,
33-- query parameters and request body to make the docs
34-- really helpful.
35instance ToCapture (Capture "name" Text) where
36 toCapture _ = DocCapture "name" "name of the person to greet"
38instance ToCapture (Capture "greetid" Text) where
39 toCapture _ = DocCapture "greetid" "identifier of the greet msg to remove"
41instance ToParam (QueryParam "capital" Bool) where
42 toParam _ =
43 DocQueryParam "capital"
44 ["true", "false"]
45 "Get the greeting message in uppercase (true) or not (false).\
46 \Default is false."
47 Normal
49instance ToSample Greet where
50 toSamples _ =
51 [ ("If you use ?capital=true", Greet "HELLO, HASKELLER")
52 , ("If you use ?capital=false", Greet "Hello, haskeller")
53 ]
55instance ToSample Int where
56 toSamples _ = singleSample 1729
58-- We define some introductory sections, these will appear at the top of the
59-- documentation.
60--
61-- We pass them in with 'docsWith', below. If you only want to add
62-- introductions, you may use 'docsWithIntros'
63intro1 :: DocIntro
64intro1 = DocIntro "On proper introductions." -- The title
65 [ "Hello there."
66 , "As documentation is usually written for humans, it's often useful \
67 \to introduce concepts with a few words." ] -- Elements are paragraphs
69intro2 :: DocIntro
70intro2 = DocIntro "This title is below the last"
71 [ "You'll also note that multiple intros are possible." ]
74-- API specification
75type TestApi =
76 -- GET /hello/:name?capital={true, false} returns a Greet as JSON or PlainText
77 "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, PlainText] Greet
79 -- POST /greet with a Greet as JSON in the request body,
80 -- returns a Greet as JSON
81 :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] (Headers '[Header "X-Example" Int] Greet)
83 -- DELETE /greet/:greetid
84 :<|> "greet" :> Capture "greetid" Text :> Delete '[JSON] ()
86testApi :: Proxy TestApi
87testApi = Proxy
89-- Build some extra information for the DELETE /greet/:greetid endpoint. We
90-- want to add documentation about a secret unicorn header and some extra
91-- notes.
92extra :: ExtraInfo TestApi
93extra =
94 extraInfo (Proxy :: Proxy ("greet" :> Capture "greetid" Text :> Delete '[JSON] ())) $
95 defAction & headers <>~ ["unicorns"]
96 & notes <>~ [ DocNote "Title" ["This is some text"]
97 , DocNote "Second secton" ["And some more"]
98 ]
100-- Generate the data that lets us have API docs. This
101-- is derived from the type as well as from
102-- the 'ToCapture', 'ToParam' and 'ToSample' instances from above.
103--
104-- If you didn't want intros and extra information, you could just call:
105--
106-- > docs testAPI :: API
107docsGreet :: API
108docsGreet = docsWith defaultDocOptions [intro1, intro2] extra testApi
110main :: IO ()
111main = putStrLn $ markdown docsGreet