purescript-httpurple/src/HTTPure/Headers.purs

35 lines
1.0 KiB
Haskell
Raw Normal View History

module HTTPure.Headers
( Headers
2017-07-18 01:51:43 +00:00
, lookup
, write
) where
import Prelude
2017-07-18 05:25:14 +00:00
import Control.Monad.Eff as Eff
import Data.Maybe as Maybe
2017-07-18 01:51:43 +00:00
import Data.String as StringUtil
import Data.StrMap as StrMap
import Data.Traversable as Traversable
import Node.HTTP as HTTP
-- | The Headers type is just sugar for a StrMap of Strings that represents the
-- | set of headers sent or received in an HTTP request or response.
type Headers = StrMap.StrMap String
2017-07-18 01:51:43 +00:00
-- | Return the value of the given header.
lookup :: Headers -> String -> String
lookup headers =
Maybe.fromMaybe "" <<< flip StrMap.lookup headers <<< StringUtil.toLower
-- | Write a set of headers to the given HTTP Response.
2017-07-18 05:25:14 +00:00
write :: forall e.
HTTP.Response ->
Headers ->
Eff.Eff (http :: HTTP.HTTP | e) Unit
2017-07-23 06:13:47 +00:00
write response headers =
void $ Traversable.traverse writeHeader $ StrMap.keys headers
where
getHeader header = Maybe.fromMaybe "" $ StrMap.lookup header headers
writeHeader header = HTTP.setHeader response header $ getHeader header