purescript-httpurple/Library/HTTPure/Response.purs

38 lines
1.1 KiB
Haskell
Raw Normal View History

module HTTPure.Response
( ResponseM
, Response(..)
, send
) where
2017-07-10 10:17:13 +00:00
import Prelude (Unit, bind, discard, pure, unit)
2017-07-10 10:17:13 +00:00
import Node.Encoding as Encoding
import Node.HTTP as HTTP
import Node.Stream as Stream
import HTTPure.Body as Body
import HTTPure.Headers as Headers
import HTTPure.HTTPureM as HTTPureM
-- | A response is a status, and can have headers and a body. Different response
-- | codes will allow different response components to be sent.
data Response
= OK Headers.Headers Body.Body
-- | The ResponseM type simply conveniently wraps up an HTTPure monad that
-- | returns a response. This type is the return type of all router/route
-- | methods.
type ResponseM e = HTTPureM.HTTPureM e Response
-- | Given an HTTP response and a HTTPure response, this method will return a
-- | monad encapsulating writing the HTTPure response to the HTTP response and
-- | closing the HTTP response.
send :: forall e. HTTP.Response -> Response -> HTTPureM.HTTPureM e Unit
send response (OK headers body) = do
_ <- Stream.writeString stream Encoding.UTF8 body noop
Stream.end stream noop
2017-07-10 10:17:13 +00:00
noop
where
stream = HTTP.responseAsStream response
2017-07-10 10:17:13 +00:00
noop = pure unit