@@ -14,6 +14,7 @@ module Aws.Lambda.Wai
1414 ALBWaiHandler ,
1515 ignoreALBPathPart ,
1616 ignoreNothing ,
17+ waiHandler
1718 )
1819where
1920
@@ -48,6 +49,8 @@ type ApiGatewayWaiHandler = ApiGatewayRequest Text -> Context Application -> IO
4849
4950type ALBWaiHandler = ALBRequest Text -> Context Application -> IO (Either (ALBResponse Text ) (ALBResponse Text ))
5051
52+ type GenericWaiHandler = Value -> Context Application -> IO (Either Value Value )
53+
5154newtype ALBIgnoredPathPortion = ALBIgnoredPathPortion { unALBIgnoredPathPortion :: Text }
5255
5356data WaiLambdaProxyType
@@ -62,21 +65,26 @@ runWaiAsProxiedHttpLambda ::
6265 IO ()
6366runWaiAsProxiedHttpLambda options ignoredAlbPath handlerName mkApp =
6467 runLambdaHaskellRuntime options mkApp id $
65- addStandaloneLambdaHandler handlerName $ \ (request :: Value ) context ->
66- case parse parseIsAlb request of
67- Success isAlb -> do
68- if isAlb
69- then case fromJSON @ (ALBRequest Text ) request of
70- Success albRequest ->
71- bimap toJSON toJSON <$> albWaiHandler ignoredAlbPath albRequest context
72- Error err -> error $ " Could not parse the request as a valid ALB request: " <> err
73- else case fromJSON @ (ApiGatewayRequest Text ) request of
74- Success apiGwRequest ->
75- bimap toJSON toJSON <$> apiGatewayWaiHandler apiGwRequest context
76- Error err -> error $ " Could not parse the request as a valid API Gateway request: " <> err
77- Error err ->
78- error $
79- " Could not parse the request as a valid API Gateway or ALB proxy request: " <> err
68+ addStandaloneLambdaHandler handlerName (waiHandler ignoredAlbPath)
69+
70+ waiHandler ::
71+ Maybe ALBIgnoredPathPortion ->
72+ GenericWaiHandler
73+ waiHandler ignoredAlbPath request context =
74+ case parse parseIsAlb request of
75+ Success isAlb -> do
76+ if isAlb
77+ then case fromJSON @ (ALBRequest Text ) request of
78+ Success albRequest ->
79+ bimap toJSON toJSON <$> albWaiHandler ignoredAlbPath albRequest context
80+ Error err -> error $ " Could not parse the request as a valid ALB request: " <> err
81+ else case fromJSON @ (ApiGatewayRequest Text ) request of
82+ Success apiGwRequest ->
83+ bimap toJSON toJSON <$> apiGatewayWaiHandler apiGwRequest context
84+ Error err -> error $ " Could not parse the request as a valid API Gateway request: " <> err
85+ Error err ->
86+ error $
87+ " Could not parse the request as a valid API Gateway or ALB proxy request: " <> err
8088 where
8189 parseIsAlb :: Value -> Parser Bool
8290 parseIsAlb = withObject " Request" $ \ obj -> do
0 commit comments