servant-docs-0.9.1.1: generate API docs for your servant webservice

Safe HaskellNone
LanguageHaskell2010

Servant.Docs.Internal

Contents

Synopsis

Documentation

data Endpoint Source #

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 /
λ> defEndpoint & path <>~ ["foo"]
GET /foo
λ> defEndpoint & path <>~ ["foo"] & method .~ methodPost
POST /foo

Constructors

Endpoint 

Fields

showPath :: [String] -> String Source #

Render a path as a /-delimited string

defEndpoint :: Endpoint Source #

An Endpoint whose path is `"/"` and whose method is GET

Here's how you can modify it:

λ> defEndpoint
GET /
λ> defEndpoint & path <>~ ["foo"]
GET /foo
λ> defEndpoint & path <>~ ["foo"] & method .~ methodPost
POST /foo

data API Source #

Our API documentation type, a product of top-level information and a good old hashmap from Endpoint to Action

Instances

Eq API Source # 

Methods

(==) :: API -> API -> Bool #

(/=) :: API -> API -> Bool #

Show API Source # 

Methods

showsPrec :: Int -> API -> ShowS #

show :: API -> String #

showList :: [API] -> ShowS #

Monoid API Source # 

Methods

mempty :: API #

mappend :: API -> API -> API #

mconcat :: [API] -> API #

emptyAPI :: API Source #

An empty API

data DocCapture Source #

A type to represent captures. Holds the name of the capture and a description.

Write a ToCapture instance for your captured types.

Constructors

DocCapture 

data DocQueryParam Source #

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 DocIntro Source #

An introductory paragraph for your documentation. You can pass these to docsWithIntros.

Constructors

DocIntro 

Fields

data DocNote Source #

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.

Constructors

DocNote 

newtype ExtraInfo api Source #

Type of extra information that a user may wish to "union" with their documentation.

These are intended to be built using extraInfo. Multiple ExtraInfo may be combined with the monoid instance.

Instances

Monoid (ExtraInfo k a) Source # 

Methods

mempty :: ExtraInfo k a #

mappend :: ExtraInfo k a -> ExtraInfo k a -> ExtraInfo k a #

mconcat :: [ExtraInfo k a] -> ExtraInfo k a #

data DocOptions Source #

Documentation options.

Constructors

DocOptions 

Fields

defaultDocOptions :: DocOptions Source #

Default documentation options.

data ParamKind Source #

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

Constructors

Normal 
List 
Flag 

data Response Source #

A type to represent an HTTP response. Has an Int status, a list of possible MediaTypes, and a list of example ByteString response bodies. Tweak defResponse using the respStatus, respTypes and respBody lenses if you want.

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 as encoded data in the response.

Can be tweaked with three lenses.

λ> defResponse
Response {_respStatus = 200, _respTypes = [], _respBody = []}
λ> defResponse & respStatus .~ 204 & respBody .~ [("If everything goes well", "{ \"status\": \"ok\" }")]
Response {_respStatus = 204, _respTypes = [], _respBody = [("If everything goes well", "{ \"status\": \"ok\" }")]}

defResponse :: Response Source #

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 "[]"}

data Action Source #

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.

combineAction :: Action -> Action -> Action Source #

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 combineAction to mush two together taking the response, body and content types from the very left.

single :: Endpoint -> Action -> API Source #

Create an API that's comprised of a single endpoint. API is a Monoid, so combine multiple endpoints with mappend or <>.

docs :: HasDocs api => Proxy api -> API Source #

Generate the docs for a given API that implements HasDocs. This is the default way to create documentation.

docs == docsWithOptions defaultDocOptions

docsWithOptions :: HasDocs api => Proxy api -> DocOptions -> API Source #

Generate the docs for a given API that implements HasDocs.

type family IsIn (endpoint :: *) (api :: *) :: Constraint where ... Source #

Closed type family, check if endpoint is exactly within API.

Equations

IsIn e (sa :<|> sb) = Or (IsIn e sa) (IsIn e sb) 
IsIn (e :> sa) (e :> sb) = IsIn sa sb 
IsIn e e = () 

extraInfo :: (IsIn endpoint api, HasLink endpoint, HasDocs endpoint) => Proxy endpoint -> Action -> ExtraInfo api Source #

Create an ExtraInfo that is guaranteed 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 =
    extraInfo (Proxy :: Proxy ("greet" :> Capture "greetid" Text :> Delete)) $
             defAction & headers <>~ ["unicorns"]
                       & notes   <>~ [ DocNote "Title" ["This is some text"]
                                     , DocNote "Second secton" ["And some more"]
                                     ]

docsWith :: HasDocs api => DocOptions -> [DocIntro] -> ExtraInfo api -> Proxy api -> API Source #

Generate documentation given some extra introductions (in the form of DocInfo) and some extra endpoint documentation (in the form of ExtraInfo.

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 extraInfo.

If you only want to add an introduction, use docsWithIntros.

docsWithIntros :: HasDocs api => [DocIntro] -> Proxy api -> API Source #

Generate the docs for a given API that implements HasDocs with with any number of introduction(s)

class HasDocs api where Source #

The class that abstracts away the impact of API combinators on documentation generation.

Minimal complete definition

docsFor

Methods

docsFor :: Proxy api -> (Endpoint, Action) -> DocOptions -> API Source #

Instances

HasDocs * Raw Source # 

Methods

docsFor :: Proxy Raw api -> (Endpoint, Action) -> DocOptions -> API Source #

(HasDocs * a, HasDocs * b) => HasDocs * ((:<|>) a b) Source #

The generated docs for a :<|> b just appends the docs for a with the docs for b.

Methods

docsFor :: Proxy (a :<|> b) api -> (Endpoint, Action) -> DocOptions -> API Source #

HasDocs * api => HasDocs * (WithNamedContext name context api) Source # 

Methods

docsFor :: Proxy (WithNamedContext name context api) api -> (Endpoint, Action) -> DocOptions -> API Source #

(ToAuthInfo * (BasicAuth realm usr), HasDocs k1 api) => HasDocs * ((:>) * k1 (BasicAuth realm usr) api) Source # 

Methods

docsFor :: Proxy ((* :> k1) (BasicAuth realm usr) api) api -> (Endpoint, Action) -> DocOptions -> API Source #

HasDocs k1 api => HasDocs * ((:>) * k1 Vault api) Source # 

Methods

docsFor :: Proxy ((* :> k1) Vault api) api -> (Endpoint, Action) -> DocOptions -> API Source #

HasDocs k1 api => HasDocs * ((:>) * k1 HttpVersion api) Source # 

Methods

docsFor :: Proxy ((* :> k1) HttpVersion api) api -> (Endpoint, Action) -> DocOptions -> API Source #

HasDocs k1 api => HasDocs * ((:>) * k1 IsSecure api) Source # 

Methods

docsFor :: Proxy ((* :> k1) IsSecure api) api -> (Endpoint, Action) -> DocOptions -> API Source #

HasDocs k1 api => HasDocs * ((:>) * k1 RemoteHost api) Source # 

Methods

docsFor :: Proxy ((* :> k1) RemoteHost api) api -> (Endpoint, Action) -> DocOptions -> API Source #

(ToSample a, AllMimeRender ((:) * ct cts) a, HasDocs k1 api) => HasDocs * ((:>) * k1 (ReqBody * ((:) * ct cts) a) api) Source # 

Methods

docsFor :: Proxy ((* :> k1) (ReqBody * ((* ': ct) cts) a) api) api -> (Endpoint, Action) -> DocOptions -> API Source #

(KnownSymbol sym, ToParam * (QueryFlag sym), HasDocs k1 api) => HasDocs * ((:>) * k1 (QueryFlag sym) api) Source # 

Methods

docsFor :: Proxy ((* :> k1) (QueryFlag sym) api) api -> (Endpoint, Action) -> DocOptions -> API Source #

(KnownSymbol sym, ToParam * (QueryParams k sym a), HasDocs k1 api) => HasDocs * ((:>) * k1 (QueryParams k sym a) api) Source # 

Methods

docsFor :: Proxy ((* :> k1) (QueryParams k sym a) api) api -> (Endpoint, Action) -> DocOptions -> API Source #

(KnownSymbol sym, ToParam * (QueryParam k sym a), HasDocs k1 api) => HasDocs * ((:>) * k1 (QueryParam k sym a) api) Source # 

Methods

docsFor :: Proxy ((* :> k1) (QueryParam k sym a) api) api -> (Endpoint, Action) -> DocOptions -> API Source #

(KnownSymbol sym, HasDocs k1 api) => HasDocs * ((:>) * k1 (Header sym a) api) Source # 

Methods

docsFor :: Proxy ((* :> k1) (Header sym a) api) api -> (Endpoint, Action) -> DocOptions -> API Source #

(KnownSymbol sym, ToCapture * (CaptureAll k sym a), HasDocs k1 sublayout) => HasDocs * ((:>) * k1 (CaptureAll k sym a) sublayout) Source #

"books" :> CaptureAll "isbn" Text will appear as books:isbn in the docs.

Methods

docsFor :: Proxy ((* :> k1) (CaptureAll k sym a) sublayout) api -> (Endpoint, Action) -> DocOptions -> API Source #

(KnownSymbol sym, ToCapture * (Capture k sym a), HasDocs k1 api) => HasDocs * ((:>) * k1 (Capture k sym a) api) Source #

"books" :> Capture "isbn" Text will appear as books:isbn in the docs.

Methods

docsFor :: Proxy ((* :> k1) (Capture k sym a) api) api -> (Endpoint, Action) -> DocOptions -> API Source #

(KnownSymbol path, HasDocs k1 api) => HasDocs * ((:>) Symbol k1 path api) Source # 

Methods

docsFor :: Proxy ((Symbol :> k1) path api) api -> (Endpoint, Action) -> DocOptions -> API Source #

(ToSample a, AllMimeRender ((:) * ct cts) a, KnownNat status, ReflectMethod k1 method, AllHeaderSamples [*] ls, GetHeaders (HList ls)) => HasDocs * (Verb k1 * method status ((:) * ct cts) (Headers ls a)) Source # 

Methods

docsFor :: Proxy (Verb k1 * method status ((* ': ct) cts) (Headers ls a)) api -> (Endpoint, Action) -> DocOptions -> API Source #

(ToSample a, AllMimeRender ((:) * ct cts) a, KnownNat status, ReflectMethod k1 method) => HasDocs * (Verb k1 * method status ((:) * ct cts) a) Source # 

Methods

docsFor :: Proxy (Verb k1 * method status ((* ': ct) cts) a) api -> (Endpoint, Action) -> DocOptions -> API Source #

class ToSample a where Source #

The class that lets us display a sample input or output in the supported content-types 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
  toSamples _ = singleSample g

    where g = Greet "Hello, haskeller!"

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 get the corresponding response.

Methods

toSamples :: Proxy a -> [(Text, a)] Source #

toSamples :: (Generic a, GToSample (Rep a)) => Proxy a -> [(Text, a)] Source #

Instances

ToSample Bool Source # 

Methods

toSamples :: Proxy * Bool -> [(Text, Bool)] Source #

ToSample Ordering Source # 
ToSample All Source # 

Methods

toSamples :: Proxy * All -> [(Text, All)] Source #

ToSample Any Source # 

Methods

toSamples :: Proxy * Any -> [(Text, Any)] Source #

ToSample NoContent Source # 
ToSample a => ToSample [a] Source # 

Methods

toSamples :: Proxy * [a] -> [(Text, [a])] Source #

ToSample a => ToSample (Maybe a) Source # 

Methods

toSamples :: Proxy * (Maybe a) -> [(Text, Maybe a)] Source #

ToSample a => ToSample (ZipList a) Source # 

Methods

toSamples :: Proxy * (ZipList a) -> [(Text, ZipList a)] Source #

ToSample a => ToSample (Dual a) Source # 

Methods

toSamples :: Proxy * (Dual a) -> [(Text, Dual a)] Source #

ToSample a => ToSample (Sum a) Source # 

Methods

toSamples :: Proxy * (Sum a) -> [(Text, Sum a)] Source #

ToSample a => ToSample (Product a) Source # 

Methods

toSamples :: Proxy * (Product a) -> [(Text, Product a)] Source #

ToSample a => ToSample (First a) Source # 

Methods

toSamples :: Proxy * (First a) -> [(Text, First a)] Source #

ToSample a => ToSample (Last a) Source # 

Methods

toSamples :: Proxy * (Last a) -> [(Text, Last a)] Source #

(ToSample a, ToSample b) => ToSample (Either a b) Source # 

Methods

toSamples :: Proxy * (Either a b) -> [(Text, Either a b)] Source #

(ToSample a, ToSample b) => ToSample (a, b) Source # 

Methods

toSamples :: Proxy * (a, b) -> [(Text, (a, b))] Source #

(ToSample a, ToSample b, ToSample c) => ToSample (a, b, c) Source # 

Methods

toSamples :: Proxy * (a, b, c) -> [(Text, (a, b, c))] Source #

ToSample a => ToSample (Const k a b) Source # 

Methods

toSamples :: Proxy * (Const k a b) -> [(Text, Const k a b)] Source #

(ToSample a, ToSample b, ToSample c, ToSample d) => ToSample (a, b, c, d) Source # 

Methods

toSamples :: Proxy * (a, b, c, d) -> [(Text, (a, b, c, d))] Source #

(ToSample a, ToSample b, ToSample c, ToSample d, ToSample e) => ToSample (a, b, c, d, e) Source # 

Methods

toSamples :: Proxy * (a, b, c, d, e) -> [(Text, (a, b, c, d, e))] Source #

(ToSample a, ToSample b, ToSample c, ToSample d, ToSample e, ToSample f) => ToSample (a, b, c, d, e, f) Source # 

Methods

toSamples :: Proxy * (a, b, c, d, e, f) -> [(Text, (a, b, c, d, e, f))] Source #

(ToSample a, ToSample b, ToSample c, ToSample d, ToSample e, ToSample f, ToSample g) => ToSample (a, b, c, d, e, f, g) Source # 

Methods

toSamples :: Proxy * (a, b, c, d, e, f, g) -> [(Text, (a, b, c, d, e, f, g))] Source #

toSample :: forall a. ToSample a => Proxy a -> Maybe a Source #

Sample input or output (if there is at least one).

noSamples :: [(Text, a)] Source #

No samples.

singleSample :: a -> [(Text, a)] Source #

Single sample without description.

samples :: [a] -> [(Text, a)] Source #

Samples without documentation.

defaultSamples :: forall a. (Generic a, GToSample (Rep a)) => Proxy a -> [(Text, a)] Source #

Default sample Generic-based inputs/outputs.

class GToSample t where Source #

ToSample for Generics.

The use of Omega allows for more productive sample generation.

Minimal complete definition

gtoSamples

Methods

gtoSamples :: proxy t -> Omega (Text, t x) Source #

Instances

GToSample * V1 Source # 

Methods

gtoSamples :: proxy t -> Omega (Text, t x) Source #

GToSample * U1 Source # 

Methods

gtoSamples :: proxy t -> Omega (Text, t x) Source #

ToSample a => GToSample * (K1 i a) Source # 

Methods

gtoSamples :: proxy t -> Omega (Text, t x) Source #

(GToSample * p, GToSample * q) => GToSample * ((:+:) p q) Source # 

Methods

gtoSamples :: proxy t -> Omega (Text, t x) Source #

(GToSample * p, GToSample * q) => GToSample * ((:*:) p q) Source # 

Methods

gtoSamples :: proxy t -> Omega (Text, t x) Source #

GToSample * f => GToSample * (M1 i a f) Source # 

Methods

gtoSamples :: proxy t -> Omega (Text, t x) Source #

class AllHeaderSamples ls where Source #

Minimal complete definition

allHeaderToSample

Instances

AllHeaderSamples [k] ([] k) Source # 

Methods

allHeaderToSample :: Proxy [k] ls -> [Header] Source #

(ToHttpApiData l, AllHeaderSamples [*] ls, ToSample l, KnownSymbol h) => AllHeaderSamples [*] ((:) * (Header h l) ls) Source # 

Methods

allHeaderToSample :: Proxy ((* ': Header h l) ls) ls -> [Header] Source #

sampleByteString :: forall ct cts a. (ToSample a, AllMimeRender (ct ': cts) a) => Proxy (ct ': cts) -> Proxy a -> [(MediaType, ByteString)] Source #

Synthesise a sample value of a type, encoded in the specified media types.

sampleByteStrings :: forall ct cts a. (ToSample a, AllMimeRender (ct ': cts) a) => Proxy (ct ': cts) -> Proxy a -> [(Text, MediaType, ByteString)] Source #

Synthesise a list of sample values of a particular type, encoded in the specified media types.

class ToParam t where Source #

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."

Minimal complete definition

toParam

class ToCapture c where Source #

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"

Minimal complete definition

toCapture

class ToAuthInfo a where Source #

The class that helps us get documentation for authenticated endpoints

Minimal complete definition

toAuthInfo

markdown :: API -> String Source #

Generate documentation in Markdown format for the given API.

Instances