2015-01-21 08:47:23 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE KindSignatures #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
|
2015-07-22 12:55:44 +02:00
|
|
|
module Servant.JSSpec.CustomHeaders where
|
2015-01-21 08:47:23 +01:00
|
|
|
|
2015-08-17 23:56:29 +02:00
|
|
|
import Control.Lens
|
|
|
|
import Data.Monoid
|
|
|
|
import Data.Proxy
|
|
|
|
import GHC.TypeLits
|
|
|
|
import Servant.JS.Internal
|
2015-01-21 08:47:23 +01:00
|
|
|
|
|
|
|
-- | This is a hypothetical combinator that fetches an Authorization header.
|
|
|
|
-- The symbol in the header denotes what kind of authentication we are
|
|
|
|
-- using -- Basic, Digest, whatever.
|
|
|
|
data Authorization (sym :: Symbol) a
|
|
|
|
|
2015-09-21 12:31:00 +02:00
|
|
|
instance (KnownSymbol sym, HasForeign sublayout)
|
|
|
|
=> HasForeign (Authorization sym a :> sublayout) where
|
|
|
|
type Foreign (Authorization sym a :> sublayout) = Foreign sublayout
|
2015-01-21 08:47:23 +01:00
|
|
|
|
2015-09-21 12:31:00 +02:00
|
|
|
foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
|
2015-01-21 08:47:23 +01:00
|
|
|
req & reqHeaders <>~ [ ReplaceHeaderArg "Authorization" $
|
2015-01-21 09:32:06 +01:00
|
|
|
tokenType (symbolVal (Proxy :: Proxy sym)) ]
|
2015-01-21 08:47:23 +01:00
|
|
|
where
|
2015-01-21 09:32:06 +01:00
|
|
|
tokenType t = t <> " {Authorization}"
|
|
|
|
|
|
|
|
-- | This is a combinator that fetches an X-MyLovelyHorse header.
|
|
|
|
data MyLovelyHorse a
|
|
|
|
|
2015-09-21 12:31:00 +02:00
|
|
|
instance (HasForeign sublayout)
|
|
|
|
=> HasForeign (MyLovelyHorse a :> sublayout) where
|
|
|
|
type Foreign (MyLovelyHorse a :> sublayout) = Foreign sublayout
|
2015-01-21 09:32:06 +01:00
|
|
|
|
2015-09-21 12:31:00 +02:00
|
|
|
foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
|
2015-01-21 09:32:06 +01:00
|
|
|
req & reqHeaders <>~ [ ReplaceHeaderArg "X-MyLovelyHorse" tpl ]
|
|
|
|
where
|
|
|
|
tpl = "I am good friends with {X-MyLovelyHorse}"
|
2015-01-22 01:41:06 +01:00
|
|
|
|
|
|
|
-- | This is a combinator that fetches an X-WhatsForDinner header.
|
|
|
|
data WhatsForDinner a
|
|
|
|
|
2015-09-21 12:31:00 +02:00
|
|
|
instance (HasForeign sublayout)
|
|
|
|
=> HasForeign (WhatsForDinner a :> sublayout) where
|
|
|
|
type Foreign (WhatsForDinner a :> sublayout) = Foreign sublayout
|
2015-01-22 01:41:06 +01:00
|
|
|
|
2015-09-21 12:31:00 +02:00
|
|
|
foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
|
2015-01-22 01:41:06 +01:00
|
|
|
req & reqHeaders <>~ [ ReplaceHeaderArg "X-WhatsForDinner" tpl ]
|
|
|
|
where
|
|
|
|
tpl = "I would like {X-WhatsForDinner} with a cherry on top."
|