Add expectQueryParam helper.

This commit is contained in:
Dillon Kearns 2022-04-10 16:32:26 -07:00
parent 2934da1958
commit 8800c8a16f
2 changed files with 26 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@ module Server.Request exposing
, requestTime, optionalHeader, expectContentType, expectJsonBody
, acceptMethod, acceptContentTypes
, map, map2, oneOf, andMap, andThen
, expectQueryParam
, queryParam, expectQueryParam
, cookie, expectCookie
, expectHeader
, File, expectMultiPartFormPost
@ -39,7 +39,7 @@ module Server.Request exposing
## Query Parameters
@docs expectQueryParam
@docs queryParam, expectQueryParam
## Cookies
@ -688,6 +688,29 @@ expectQueryParam name =
)
{-| -}
queryParam : String -> Parser (Maybe String)
queryParam name =
rawUrl
|> andThen
(\url_ ->
url_
|> Url.fromString
|> Maybe.andThen .query
|> Maybe.andThen (findFirstQueryParam name)
|> succeed
)
findFirstQueryParam : String -> String -> Maybe String
findFirstQueryParam name queryString =
queryString
|> QueryParams.fromString
|> QueryParams.toDict
|> Dict.get name
|> Maybe.andThen List.head
{-| -}
queryParams : Parser (Dict String (List String))
queryParams =