Skip to content

Commit

Permalink
Added renderJsonWithStatusCode
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Oct 3, 2021
1 parent fd94c7b commit 7be4881
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions IHP/Controller/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module IHP.Controller.Render where
import ClassyPrelude
import Network.Wai (responseLBS, responseBuilder, responseFile)
import qualified Network.Wai
import Network.HTTP.Types (status200, status406)
import Network.HTTP.Types (Status, status200, status406)
import Network.HTTP.Types.Header
import IHP.ModelSupport
import qualified Data.ByteString.Lazy
Expand Down Expand Up @@ -73,13 +73,18 @@ renderFile filePath contentType = respondAndExit $ responseFile status200 [(hCon
{-# INLINABLE renderFile #-}

renderJson :: (?context :: ControllerContext) => Data.Aeson.ToJSON json => json -> IO ()
renderJson json = respondAndExit $ responseLBS status200 [(hContentType, "application/json")] (Data.Aeson.encode json)
renderJson json = renderJsonWithStatusCode status200 json
{-# INLINABLE renderJson #-}

renderJsonWithStatusCode :: (?context :: ControllerContext) => Data.Aeson.ToJSON json => Status -> json -> IO ()
renderJsonWithStatusCode statusCode json = respondAndExit $ responseLBS statusCode [(hContentType, "application/json")] (Data.Aeson.encode json)
{-# INLINABLE renderJsonWithStatusCode #-}

renderXml :: (?context :: ControllerContext) => LByteString -> IO ()
renderXml xml = respondAndExit $ responseLBS status200 [(hContentType, "application/xml")] xml
{-# INLINABLE renderXml #-}

-- | Use 'setHeader' intead
renderJson' :: (?context :: ControllerContext) => ResponseHeaders -> Data.Aeson.ToJSON json => json -> IO ()
renderJson' additionalHeaders json = respondAndExit $ responseLBS status200 ([(hContentType, "application/json")] <> additionalHeaders) (Data.Aeson.encode json)
{-# INLINABLE renderJson' #-}
Expand Down

0 comments on commit 7be4881

Please sign in to comment.