mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2025-01-06 05:34:48 +03:00
Add helpers for using FormData and parsed cookies from DataSource.ServerRequest.
This commit is contained in:
parent
a7bc4226e9
commit
0501b4da4b
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user