Add helpers for using FormData and parsed cookies from DataSource.ServerRequest.

This commit is contained in:
Dillon Kearns 2021-12-22 21:29:01 -08:00
parent a7bc4226e9
commit 0501b4da4b

View File

@ -1,7 +1,7 @@
module DataSource.ServerRequest exposing
( IsAvailable
, ServerRequest, expectHeader, init, optionalHeader, staticData, toDataSource
, Method(..), withAllHeaders, withBody, withHost, withMethod, withProtocol, withQueryParams
, Method(..), withAllHeaders, withBody, withCookies, withFormData, withHost, withMethod, withProtocol, withQueryParams
)
{-|
@ -12,9 +12,11 @@ module DataSource.ServerRequest exposing
-}
import CookieParser
import DataSource
import DataSource.Http
import Dict exposing (Dict)
import FormData
import Internal.ServerRequest
import OptimizedDecoder
import QueryParams exposing (QueryParams)
@ -143,6 +145,23 @@ optionalHeader headerName (ServerRequest decoder) =
|> ServerRequest
{-| -}
withCookies : ServerRequest (Dict String String -> value) -> ServerRequest value
withCookies (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.optionalField "cookie" OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
|> OptimizedDecoder.map
(\cookie ->
cookie
|> Maybe.withDefault ""
|> CookieParser.parse
)
)
|> ServerRequest
{-| -}
withBody : ServerRequest (Maybe String -> value) -> ServerRequest value
withBody (ServerRequest decoder) =
@ -152,6 +171,38 @@ withBody (ServerRequest decoder) =
|> ServerRequest
{-| -}
withFormData :
ServerRequest
(Maybe (Dict String ( String, List String ))
-> value
)
-> ServerRequest value
withFormData (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.map2
(\contentType maybeBody ->
-- TODO parse content-type more robustly
if contentType == Just "application/x-www-form-urlencoded" then
maybeBody
|> Maybe.map FormData.parse
else
Nothing
)
(OptimizedDecoder.optionalField
("content-type"
|> String.toLower
)
OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
)
(OptimizedDecoder.optionalField "body" OptimizedDecoder.string)
)
|> ServerRequest
type Method
= Connect
| Delete