servant/servant-js/test/Servant/JSSpec/CustomHeaders.hs

59 lines
2.2 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}
2015-11-29 05:53:50 +01:00
{-# LANGUAGE MultiParamTypeClasses #-}
module Servant.JSSpec.CustomHeaders where
import Control.Lens
import Data.Monoid
import Data.Proxy
import Data.Text (pack)
import GHC.TypeLits
import Servant.JS.Internal
-- | 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-11-29 05:53:50 +01:00
instance (KnownSymbol sym, HasForeign lang sublayout)
=> HasForeign lang (Authorization sym a :> sublayout) where
type Foreign (Authorization sym a :> sublayout) = Foreign sublayout
2015-11-29 05:53:50 +01:00
foreignFor lang Proxy req = foreignFor lang (Proxy :: Proxy sublayout) $
2016-02-17 22:47:30 +01:00
req & reqHeaders <>~
[ ReplaceHeaderArg (Arg "Authorization" "")
$ tokenType (pack . symbolVal $ (Proxy :: Proxy sym)) ]
where
tokenType t = t <> " {Authorization}"
-- | This is a combinator that fetches an X-MyLovelyHorse header.
data MyLovelyHorse a
2015-11-29 05:53:50 +01:00
instance (HasForeign lang sublayout)
=> HasForeign lang (MyLovelyHorse a :> sublayout) where
type Foreign (MyLovelyHorse a :> sublayout) = Foreign sublayout
2015-11-29 05:53:50 +01:00
foreignFor lang Proxy req = foreignFor lang (Proxy :: Proxy sublayout) $
2016-02-17 22:47:30 +01:00
req & reqHeaders <>~ [ ReplaceHeaderArg (Arg "X-MyLovelyHorse" "") tpl ]
where
tpl = "I am good friends with {X-MyLovelyHorse}"
-- | This is a combinator that fetches an X-WhatsForDinner header.
data WhatsForDinner a
2015-11-29 05:53:50 +01:00
instance (HasForeign lang sublayout)
=> HasForeign lang (WhatsForDinner a :> sublayout) where
type Foreign (WhatsForDinner a :> sublayout) = Foreign sublayout
2015-11-29 05:53:50 +01:00
foreignFor lang Proxy req = foreignFor lang (Proxy :: Proxy sublayout) $
2016-02-17 22:47:30 +01:00
req & reqHeaders <>~ [ ReplaceHeaderArg (Arg "X-WhatsForDinner" "") tpl ]
where
tpl = "I would like {X-WhatsForDinner} with a cherry on top."