diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c98408..fccc418 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Revision history for wai-handler-hal +## 0.4.0.0 -- 2024-01-?? + +- New function: `Wai.Handler.Hal.runWithOptions :: Options -> + Application -> ProxyRequest NoAuthorizer -> ProxyResponse`. This + provides a convenient way to pass custom `Options` without all the + bells and whistles of `runWithContext`. ## 0.3.0.0 -- 2023-12-17 - Accidental breaking change: more elaborate `Content-Type` headers diff --git a/src/Network/Wai/Handler/Hal.hs b/src/Network/Wai/Handler/Hal.hs index a227517..c09e0ea 100644 --- a/src/Network/Wai/Handler/Hal.hs +++ b/src/Network/Wai/Handler/Hal.hs @@ -31,6 +31,7 @@ -- @ module Network.Wai.Handler.Hal ( run, + runWithOptions, runWithContext, Options (..), defaultOptions, @@ -105,13 +106,7 @@ run :: Wai.Application -> HalRequest.ProxyRequest HalRequest.NoAuthorizer -> m HalResponse.ProxyResponse -run app req = liftIO $ do - waiReq <- toWaiRequest defaultOptions req - responseRef <- IORef.newIORef Nothing - Wai.ResponseReceived <- app waiReq $ \waiResp -> - Wai.ResponseReceived <$ IORef.writeIORef responseRef (Just waiResp) - Just waiResp <- IORef.readIORef responseRef - fromWaiResponse defaultOptions waiResp +run = runWithOptions defaultOptions -- | Options that can be used to customize the behaviour of 'runWithContext'. -- 'defaultOptions' provides sensible defaults. @@ -152,6 +147,27 @@ defaultOptions = _ -> True } +-- | A variant of 'run' with configurable 'Options'. Useful if you +-- just want to override the 'binaryMediaTypes' setting but don't need +-- the rest of 'runWithContext''s features. +-- +-- @since 0.4.0.0 +runWithOptions :: + (MonadIO m) => + -- | Configuration options. 'defaultOptions' provides sensible defaults. + Options -> + Wai.Application -> + HalRequest.ProxyRequest HalRequest.NoAuthorizer -> + m HalResponse.ProxyResponse +runWithOptions opts app req = + liftIO $ do + waiReq <- toWaiRequest opts req + responseRef <- IORef.newIORef Nothing + Wai.ResponseReceived <- app waiReq $ \waiResp -> + Wai.ResponseReceived <$ IORef.writeIORef responseRef (Just waiResp) + Just waiResp <- IORef.readIORef responseRef + fromWaiResponse defaultOptions waiResp + -- | Convert a WAI 'Wai.Application' into a function that can -- be run by hal's 'AWS.Lambda.Runtime.mRuntimeWithContext''. This -- function exposes all the configurable knobs.