servant-0.11: A family of combinators for defining webservices APIs

Safe Haskell | None
Language | Haskell2010

Servant.API.ResponseHeaders

Description

This module provides facilities for adding headers to a response.

    >>> let headerVal = addHeader "some-url" 5 :: Headers '[Header "Location" String] Int

The value is added to the header specified by the type (`Location` in the example above).

Synopsis

- data Headers ls a = Headers {
  - getResponse :: a
  - getHeadersHList :: HList ls
  }
- class AddHeader h v orig new | h v orig -> new, new -> h, new -> v, new -> orig
- addHeader :: AddHeader h v orig new => v -> orig -> new
- noHeader :: AddHeader h v orig new => orig -> new
- class BuildHeadersTo hs where
- class GetHeaders ls where
- type family HeaderValMap (f :: * -> *) (xs :: [*]) where ...
- data HList a where
  - HNil :: HList '[]
  - HCons :: Header h x -> HList xs -> HList (Header h x ': xs)

# Documentation

data Headers ls a Source #

Response Header objects. You should never need to construct one directly. Instead, use `addOptionalHeader`.

Constructors

Headers |

Fields

- getResponse :: a

  The underlying value of a `Headers`

- getHeadersHList :: HList ls

  HList of headers.

Instances

(KnownSymbol h, ToHttpApiData v) => AddHeader h v (Headers ((:) * fst rest) a) (Headers ((:) * (Header h v) ((:) * fst rest)) a) Source # |
Methods addOptionalHeader :: Header h v -> Headers ((* ': fst) rest) a -> Headers ((* ': Header h v) ((* ': fst) rest)) a
Functor (Headers ls) Source # |
Methods fmap :: (a -> b) -> Headers ls a -> Headers ls b # (<$) :: a -> Headers ls b -> Headers ls a #
(KnownSymbol h, GetHeaders (HList rest), ToHttpApiData v) => GetHeaders (Headers ((:) * (Header h v) rest) a) Source # |
Methods getHeaders :: Headers ((* ': Header h v) rest) a -> [Header] Source #
GetHeaders (Headers ([] *) a) Source # |
Methods getHeaders :: Headers [*] a -> [Header] Source #

class AddHeader h v orig new | h v orig -> new, new -> h, new -> v, new -> orig Source #

Minimal complete definition

addOptionalHeader

Instances

(KnownSymbol h, ToHttpApiData v, (~) * new (Headers ((:) * (Header h v) ([] *)) a)) => AddHeader h v a new Source # |
Methods addOptionalHeader :: Header h v -> a -> new
(KnownSymbol h, ToHttpApiData v) => AddHeader h v (Headers ((:) * fst rest) a) (Headers ((:) * (Header h v) ((:) * fst rest)) a) Source # |
Methods addOptionalHeader :: Header h v -> Headers ((* ': fst) rest) a -> Headers ((* ': Header h v) ((* ': fst) rest)) a

addHeader :: AddHeader h v orig new => v -> orig -> new Source #

`addHeader` adds a header to a response. Note that it changes the type of the value in the following ways:

1. A simple value is wrapped in "Headers '[hdr]":

    >>> let example1 = addHeader 5 "hi" :: Headers '[Header "someheader" Int] String;
    >>> getHeaders example1
    [("someheader","5")]

1. A value that already has a header has its new header *prepended* to the existing list:

    >>> let example1 = addHeader 5 "hi" :: Headers '[Header "someheader" Int] String;
    >>> let example2 = addHeader True example1 :: Headers '[Header "1st" Bool, Header "someheader" Int] String
    >>> getHeaders example2
    [("1st","true"),("someheader","5")]

Note that while in your handlers type annotations are not required, since the type can be inferred from the API type, in other cases you may find yourself needing to add annotations.

noHeader :: AddHeader h v orig new => orig -> new Source #

Deliberately do not add a header to a value.

    >>> let example1 = noHeader "hi" :: Headers '[Header "someheader" Int] String
    >>> getHeaders example1
    []

class BuildHeadersTo hs where Source #

Minimal complete definition

buildHeadersTo

Methods

buildHeadersTo :: [Header] -> HList hs Source #

Note: if there are multiple occurences of a header in the argument, the values are interspersed with commas before deserialization (see RFC2616 Sec 4.2)

Instances

BuildHeadersTo ([] *) Source # |
Methods buildHeadersTo :: [Header] -> HList [*] Source #
(FromHttpApiData v, BuildHeadersTo xs, KnownSymbol h) => BuildHeadersTo ((:) * (Header h v) xs) Source # |
Methods buildHeadersTo :: [Header] -> HList ((* ': Header h v) xs) Source #

class GetHeaders ls where Source #

Minimal complete definition

getHeaders

Methods

getHeaders :: ls -> [Header] Source #

Instances

(KnownSymbol h, ToHttpApiData x, GetHeaders (HList xs)) => GetHeaders (HList ((:) * (Header h x) xs)) Source # |
Methods getHeaders :: HList ((* ': Header h x) xs) -> [Header] Source #
GetHeaders (HList ([] *)) Source # |
Methods getHeaders :: HList [*] -> [Header] Source #
(KnownSymbol h, GetHeaders (HList rest), ToHttpApiData v) => GetHeaders (Headers ((:) * (Header h v) rest) a) Source # |
Methods getHeaders :: Headers ((* ': Header h v) rest) a -> [Header] Source #
GetHeaders (Headers ([] *) a) Source # |
Methods getHeaders :: Headers [*] a -> [Header] Source #

type family HeaderValMap (f :: * -> *) (xs :: [*]) where ... Source #

Equations

HeaderValMap f '[] = '[] |
HeaderValMap f (Header h x ': xs) = Header h (f x) ': HeaderValMap f xs |

data HList a where Source #

Constructors

HNil :: HList '[] |
HCons :: Header h x -> HList xs -> HList (Header h x ': xs) |

Instances

(KnownSymbol h, ToHttpApiData x, GetHeaders (HList xs)) => GetHeaders (HList ((:) * (Header h x) xs)) Source # |
Methods getHeaders :: HList ((* ': Header h x) xs) -> [Header] Source #
GetHeaders (HList ([] *)) Source # |
Methods getHeaders :: HList [*] -> [Header] Source #