1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE TypeOperators #-}
7{-# OPTIONS_GHC -fno-warn-orphans #-}
11import Data.String.Conversions
12import Data.Text (Text)
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
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
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
43 DocQueryParam "capital"
45 "Get the greeting message in uppercase (true) or not (false).\
49instance ToSample Greet where
51 [ ("If you use ?capital=true", Greet "HELLO, HASKELLER")
52 , ("If you use ?capital=false", Greet "Hello, haskeller")
55instance ToSample Int where
56 toSamples _ = singleSample 1729
58-- We define some introductory sections, these will appear at the top of the
61-- We pass them in with 'docsWith', below. If you only want to add
62-- introductions, you may use 'docsWithIntros'
64intro1 = DocIntro "On proper introductions." -- The title
66 , "As documentation is usually written for humans, it's often useful \
67 \to introduce concepts with a few words." ] -- Elements are paragraphs
70intro2 = DocIntro "This title is below the last"
71 [ "You'll also note that multiple intros are possible." ]
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
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
92extra :: ExtraInfo TestApi
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"]
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.
104-- If you didn't want intros and extra information, you could just call:
106-- > docs testAPI :: API
108docsGreet = docsWith defaultDocOptions [intro1, intro2] extra testApi
111main = putStrLn $ markdown docsGreet
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list