Add any function for handling all HTTP methods with the same handler

This commit is contained in:
Rashad Gover 2023-11-14 05:36:47 -08:00
parent 13291db5a5
commit 13c22c26ea
2 changed files with 18 additions and 5 deletions

View File

@ -329,6 +329,18 @@ endpoint ::
endpoint stdMethod transformation handler =
route @a $ method @env @(r :-> a) stdMethod transformation handler
any ::
forall env (r :: [Type]).
(Typeable.Typeable r) =>
(env Natural.~> IO) ->
Handler r env ->
Node r
any transformation handler =
choice
[ Method @env @r stdMethod transformation handler
| stdMethod <- [minBound ..]
]
data HList (l :: [Type]) where
HNil :: HList '[]
HCons :: e -> HList l -> HList (e ': l)

View File

@ -167,13 +167,14 @@ makeResponder ::
forall (status :: Natural.Natural) (headerKeys :: [Exts.Symbol]) (contentType :: Type) (resultType :: Type).
(Nat.KnownNat status, ContentType contentType, ToContentType contentType resultType, Typeable.Typeable headerKeys, Typeable.Typeable resultType) =>
(Headers headerKeys -> resultType -> Wai.Response)
makeResponder _ result =
makeResponder headerMap result =
let status = natToStatus $ Nat.natVal @status Typeable.Proxy
contentType = toContentType @contentType @resultType result
bodyType = contentTypeBody @contentType contentType
name = contentTypeName @contentType
headers = toWaiResponseHeaders headerMap
in case bodyType of
BodyBytes bytes -> Wai.responseLBS status [] bytes
BodyBuilder builder -> Wai.responseBuilder status [] builder
BodyStream stream -> Wai.responseStream status [] stream
BodyFile path part -> Wai.responseFile status [] path part
BodyBytes bytes -> Wai.responseLBS status headers bytes
BodyBuilder builder -> Wai.responseBuilder status headers builder
BodyStream stream -> Wai.responseStream status headers stream
BodyFile path part -> Wai.responseFile status headers path part