Skip to content

Commit

Permalink
Introduce runWithOptions function
Browse files Browse the repository at this point in the history
  • Loading branch information
JackKelly-Bellroy committed Jan 16, 2024
1 parent b4beafb commit 0cb6ac5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
30 changes: 23 additions & 7 deletions src/Network/Wai/Handler/Hal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
-- @
module Network.Wai.Handler.Hal
( run,
runWithOptions,
runWithContext,
Options (..),
defaultOptions,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 0cb6ac5

Please sign in to comment.