-
Notifications
You must be signed in to change notification settings - Fork 14
/
Docs.hs
63 lines (53 loc) · 2.13 KB
/
Docs.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Main where
import Data.Proxy (Proxy(Proxy))
import Data.Text (Text)
import Servant.API (Capture)
import Servant.Docs
(DocCapture(DocCapture), ToCapture(toCapture), ToSample(toSamples),
docs, markdown)
import Servant.Checked.Exceptions ()
import Api
(Api, BadSearchTermErr(BadSearchTermErr),
IncorrectCapitalization(IncorrectCapitalization), SearchQuery,
SearchResponse)
-- This module prints out documentation for 'Api'.
--
-- Notice how we only need 'ToSample' instances for the two errors we are
-- throwing with 'Throws': 'BadSearchTermErr' and 'IncorrectCapitialization'.
-- We don't have to directly worry about writing instances for 'Envelope'.
instance ToSample SearchResponse where
toSamples :: Proxy SearchResponse -> [(Text, SearchResponse)]
toSamples Proxy = [("This is a successful response.", "good")]
instance ToCapture (Capture "query" SearchQuery) where
toCapture :: Proxy (Capture "query" SearchQuery) -> DocCapture
toCapture Proxy =
DocCapture "query" "a search string like \"hello\" or \"bye\""
instance ToSample BadSearchTermErr where
toSamples :: Proxy BadSearchTermErr -> [(Text, BadSearchTermErr)]
toSamples Proxy =
[("a completely incorrect search term was used", BadSearchTermErr)]
instance ToSample IncorrectCapitalization where
toSamples :: Proxy IncorrectCapitalization -> [(Text, IncorrectCapitalization)]
toSamples Proxy =
[ ( "the search term \"Hello\" has not been capitalized correctly"
, IncorrectCapitalization)
]
-- | Print the documentation rendered as markdown to stdout.
main :: IO ()
main = putStrLn . markdown $ docs (Proxy :: Proxy Api)