Remove obsolete server request code from initial prototype.

This commit is contained in:
Dillon Kearns 2021-12-31 11:52:37 -08:00
parent 1c922f23b1
commit 86142cb6d8
18 changed files with 110 additions and 450 deletions

File diff suppressed because one or more lines are too long

View File

@ -3,12 +3,10 @@ module Api exposing (routes)
import ApiRoute exposing (ApiRoute)
import DataSource exposing (DataSource)
import DataSource.Http
import DataSource.ServerRequest as ServerRequest exposing (ServerRequest)
import Html exposing (Html)
import Json.Decode
import Json.Encode
import OptimizedDecoder as Decode
import QueryParams
import Route exposing (Route)
import Secrets
import Server.Request
@ -135,24 +133,6 @@ redirectRoute =
|> ApiRoute.serverRender
serverRequestDataSource isAvailable =
ServerRequest.init
(\language method queryParams protocol allHeaders ->
{ language = language
, method = method
, queryParams = queryParams |> QueryParams.toDict
, protocol = protocol
, allHeaders = allHeaders
}
)
|> ServerRequest.optionalHeader "accept-language"
|> ServerRequest.withMethod
|> ServerRequest.withQueryParams
|> ServerRequest.withProtocol
|> ServerRequest.withAllHeaders
|> ServerRequest.toDataSource isAvailable
noArgs : ApiRoute ApiRoute.Response
noArgs =
ApiRoute.succeed

View File

@ -39,7 +39,7 @@ type alias Data =
Maybe Request.File
data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data))
data : RouteParams -> Request.Request (DataSource (PageServerResponse Data))
data routeParams =
Request.oneOf
[ Request.expectMultiPartFormPost

View File

@ -39,7 +39,7 @@ page =
|> Page.buildNoState { view = view }
data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data))
data : RouteParams -> Request.Request (DataSource (PageServerResponse Data))
data routeParams =
Request.oneOf
[ Request.map2 Data

View File

@ -44,7 +44,7 @@ type alias Request =
}
data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data))
data : RouteParams -> Request.Request (DataSource (PageServerResponse Data))
data routeParams =
Request.oneOf
[ Request.expectFormPost (\{ field } -> field "name")

View File

@ -49,7 +49,7 @@ type alias LoggedInInfo =
}
data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data))
data : RouteParams -> Request.Request (DataSource (PageServerResponse Data))
data routeParams =
Request.oneOf
[ Request.expectCookie "username"

View File

@ -1,7 +1,6 @@
module Page.Time exposing (Data, Model, Msg, page)
import DataSource exposing (DataSource)
import DataSource.ServerRequest as ServerRequest exposing (ServerRequest)
import Dict exposing (Dict)
import Head
import Head.Seo as Seo
@ -10,7 +9,6 @@ import Page exposing (Page, PageWithState, StaticPayload)
import PageServerResponse exposing (PageServerResponse)
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
import QueryParams exposing (QueryParams)
import Server.Request as Request
import ServerResponse
import Shared
@ -41,7 +39,7 @@ page =
type alias Request =
{ language : Maybe String
, method : ServerRequest.Method
, method : Request.Method
, queryParams : Dict String (List String)
, protocol : Url.Protocol
, allHeaders : Dict String String
@ -95,7 +93,7 @@ type alias Request =
-- |> DataSource.map PageServerResponse.RenderPage
data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data))
data : RouteParams -> Request.Request (DataSource (PageServerResponse Data))
data routeParams =
Request.succeed ()
|> Request.map

View File

@ -85,7 +85,6 @@ When there are Dynamic Route Segments, you need to tell `elm-pages` which pages
import Browser.Navigation
import DataSource exposing (DataSource)
import DataSource.Http
import DataSource.ServerRequest as ServerRequest exposing (ServerRequest(..))
import Head
import PageServerResponse exposing (PageServerResponse)
import Pages.Internal.NotFoundReason exposing (NotFoundReason)
@ -100,7 +99,7 @@ import View exposing (View)
{-| -}
type alias PageWithState routeParams data model msg =
{ data : ServerRequest.IsAvailable -> routeParams -> DataSource (PageServerResponse data)
{ data : routeParams -> DataSource (PageServerResponse data)
, staticRoutes : DataSource (List routeParams)
, view :
Maybe PageUrl
@ -136,7 +135,7 @@ type alias StaticPayload data routeParams =
{-| -}
type Builder routeParams data
= WithData
{ data : ServerRequest.IsAvailable -> routeParams -> DataSource (PageServerResponse data)
{ data : routeParams -> DataSource (PageServerResponse data)
, staticRoutes : DataSource (List routeParams)
, head :
StaticPayload data routeParams
@ -267,7 +266,7 @@ single :
-> Builder {} data
single { data, head } =
WithData
{ data = \_ _ -> data |> DataSource.map PageServerResponse.RenderPage
{ data = \_ -> data |> DataSource.map PageServerResponse.RenderPage
, staticRoutes = DataSource.succeed [ {} ]
, head = head
, serverless = False
@ -285,7 +284,7 @@ preRender :
-> Builder routeParams data
preRender { data, head, pages } =
WithData
{ data = \_ -> data >> DataSource.map PageServerResponse.RenderPage
{ data = data >> DataSource.map PageServerResponse.RenderPage
, staticRoutes = pages
, head = head
, serverless = False
@ -322,7 +321,7 @@ preRenderWithFallback :
-> Builder routeParams data
preRenderWithFallback { data, head, pages } =
WithData
{ data = \_ -> data
{ data = data
, staticRoutes = pages
, head = head
, serverless = False
@ -335,14 +334,14 @@ preRenderWithFallback { data, head, pages } =
{-| -}
serverRender :
{ data : routeParams -> Server.Request.ServerRequest (DataSource (PageServerResponse data))
{ data : routeParams -> Server.Request.Request (DataSource (PageServerResponse data))
, head : StaticPayload data routeParams -> List Head.Tag
}
-> Builder routeParams data
serverRender { data, head } =
WithData
{ data =
\_ routeParams ->
\routeParams ->
DataSource.Http.get
(Secrets.succeed "$$elm-pages$$headers")
(routeParams

View File

@ -174,7 +174,7 @@ init config flags url key =
pageDataResult : Result BuildError (PageServerResponse pageData)
pageDataResult =
StaticHttpRequest.resolve ApplicationType.Browser
(config.data Internal.ServerRequest.IsAvailable (config.urlToRoute url))
(config.data (config.urlToRoute url))
justContentJson
|> Result.mapError (StaticHttpRequest.toBuildError url.path)
@ -466,7 +466,7 @@ update config appMsg model =
updatedPageStaticData : Result String pageData
updatedPageStaticData =
StaticHttpRequest.resolve ApplicationType.Browser
(config.data Internal.ServerRequest.IsAvailable (config.urlToRoute url))
(config.data (config.urlToRoute url))
contentJson.staticData
|> Result.mapError
(\error ->
@ -552,7 +552,7 @@ update config appMsg model =
pageDataResult : Result BuildError (PageServerResponse pageData)
pageDataResult =
StaticHttpRequest.resolve ApplicationType.Browser
(config.data Internal.ServerRequest.IsAvailable (config.urlToRoute model.url))
(config.data (config.urlToRoute model.url))
contentJson.staticData
|> Result.mapError (StaticHttpRequest.toBuildError model.url.path)

View File

@ -18,7 +18,6 @@ import Html exposing (Html)
import HtmlPrinter
import Http
import Internal.ApiRoute exposing (ApiRoute(..))
import Internal.ServerRequest
import Json.Decode as Decode
import Json.Encode
import PageServerResponse exposing (PageServerResponse)
@ -426,7 +425,7 @@ initLegacy site renderRequest { secrets, staticHttpCache, isDevServer } contentC
StaticResponses.renderSingleRoute config
serverRequestPayload
(DataSource.map2 (\_ _ -> ())
(config.data Internal.ServerRequest.IsAvailable serverRequestPayload.frontmatter)
(config.data serverRequestPayload.frontmatter)
config.sharedData
)
(if isDevServer then
@ -788,7 +787,7 @@ nextStepToEffect site contentCache config model ( updatedStaticResponsesModel, n
pageDataResult : Result BuildError (PageServerResponse pageData)
pageDataResult =
StaticHttpRequest.resolve ApplicationType.Browser
(config.data Internal.ServerRequest.IsAvailable (config.urlToRoute currentUrl))
(config.data (config.urlToRoute currentUrl))
(staticData |> Dict.map (\_ v -> Just v))
|> Result.mapError (StaticHttpRequest.toBuildError currentUrl.path)
@ -961,7 +960,7 @@ sendSinglePageProgress site contentJson config model =
pageDataResult : Result BuildError (PageServerResponse pageData)
pageDataResult =
StaticHttpRequest.resolve ApplicationType.Browser
(config.data Internal.ServerRequest.IsAvailable (config.urlToRoute currentUrl))
(config.data (config.urlToRoute currentUrl))
(contentJson |> Dict.map (\_ v -> Just v))
|> Result.mapError (StaticHttpRequest.toBuildError currentUrl.path)

View File

@ -9,7 +9,6 @@ import Dict.Extra
import Html exposing (Html)
import HtmlPrinter exposing (htmlToString)
import Internal.ApiRoute exposing (ApiRoute(..))
import Internal.ServerRequest
import Json.Encode
import Pages.Internal.ApplicationType as ApplicationType
import Pages.Internal.NotFoundReason exposing (NotFoundReason)
@ -189,7 +188,7 @@ nextStep :
{ config
| getStaticRoutes : DataSource (List route)
, routeToPath : route -> List String
, data : Internal.ServerRequest.IsAvailable -> route -> DataSource pageData
, data : route -> DataSource pageData
, sharedData : DataSource sharedData
, site : Maybe (SiteConfig siteData)
, apiRoutes : (Html Never -> String) -> List (ApiRoute.ApiRoute ApiRoute.Response)

View File

@ -41,7 +41,6 @@ function generateTemplateModuleConnector(basePath, phase) {
mainModule: `port module Main exposing (..)
import Api
import DataSource.ServerRequest
import Pattern
import PageServerResponse exposing (PageServerResponse)
import ApiRoute
@ -456,8 +455,8 @@ main =
.join(", ")} ]
}
dataForRoute : DataSource.ServerRequest.IsAvailable -> Maybe Route -> DataSource (PageServerResponse PageData)
dataForRoute isAvailable route =
dataForRoute : Maybe Route -> DataSource (PageServerResponse PageData)
dataForRoute route =
case route of
Nothing ->
DataSource.succeed (PageServerResponse.RenderPage Data404NotFoundPage____)
@ -470,7 +469,7 @@ dataForRoute isAvailable route =
: `(Route.${routeHelpers.routeVariant(name)} routeParams)`
} ->\n Page.${name.join(
"."
)}.page.data isAvailable ${routeHelpers.referenceRouteParams(
)}.page.data ${routeHelpers.referenceRouteParams(
name,
"routeParams"
)} |> DataSource.map (PageServerResponse.map Data${routeHelpers.routeVariant(

View File

@ -36,12 +36,9 @@ DataSources dynamically.
import DataSource exposing (DataSource)
import DataSource.Http
import DataSource.ServerRequest as ServerRequest
import Internal.ApiRoute exposing (ApiRoute(..), ApiRouteBuilder(..))
import Internal.ServerRequest
import Json.Encode
import OptimizedDecoder
import PageServerResponse
import Pattern exposing (Pattern)
import Regex
import Secrets
@ -87,7 +84,7 @@ stripTrailingSlash path =
{-| -}
serverRender : ApiRouteBuilder (Server.Request.ServerRequest (DataSource ServerResponse)) constructor -> ApiRoute Response
serverRender : ApiRouteBuilder (Server.Request.Request (DataSource ServerResponse)) constructor -> ApiRoute Response
serverRender ((ApiRouteBuilder patterns pattern _ toString constructor) as fullHandler) =
ApiRoute
{ regex = Regex.fromString ("^" ++ pattern ++ "$") |> Maybe.withDefault Regex.never

View File

@ -1,310 +0,0 @@
module DataSource.ServerRequest exposing
( ServerRequest, IsAvailable
, Method(..)
, init
, expectHeader, optionalHeader, withFormData, withCookies, withBody, withHost, withAllHeaders, withMethod, withProtocol, withQueryParams, withRequestTime
, toDataSource
)
{-|
@docs ServerRequest, IsAvailable
@docs Method
@docs init
@docs expectHeader, optionalHeader, withFormData, withCookies, withBody, withHost, withAllHeaders, withMethod, withProtocol, withQueryParams, withRequestTime
@docs toDataSource
-}
import CookieParser
import DataSource
import DataSource.Http
import Dict exposing (Dict)
import FormData
import Internal.ServerRequest
import OptimizedDecoder
import QueryParams exposing (QueryParams)
import Secrets
import Time
import Url
{-| -}
type ServerRequest decodesTo
= ServerRequest (OptimizedDecoder.Decoder decodesTo)
{-| -}
init : constructor -> ServerRequest constructor
init constructor =
ServerRequest (OptimizedDecoder.succeed constructor)
{-| In order to access the ServerRequest data, you first need to turn it into a DataSource.
Note that you can only access it in the context of a serverless request because there is no request
to access for pre-rendered pages (requests are when a user hits a page, but pre-rendering happens before
users try to access your page, that's what the "pre-" part means).
The `IsAvailable` argument gives you a compile-time guarantee that you won't accidentally try to
access ServerRequest data in a context where it won't be available, so you can safely use this if you
have an `IsAvailable` value.
-}
toDataSource : IsAvailable -> ServerRequest decodesTo -> DataSource.DataSource decodesTo
toDataSource _ (ServerRequest decoder) =
DataSource.Http.get (Secrets.succeed "$$elm-pages$$headers") decoder
{-| -}
expectHeader : String -> ServerRequest (String -> value) -> ServerRequest value
expectHeader headerName (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.field (headerName |> String.toLower) OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
)
|> ServerRequest
{-| -}
withRequestTime : ServerRequest (Time.Posix -> value) -> ServerRequest value
withRequestTime (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.field "requestTime"
(OptimizedDecoder.int |> OptimizedDecoder.map Time.millisToPosix)
)
|> ServerRequest
{-| -}
withAllHeaders : ServerRequest (Dict String String -> value) -> ServerRequest value
withAllHeaders (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.dict OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
)
|> ServerRequest
{-| -}
withMethod : ServerRequest (Method -> value) -> ServerRequest value
withMethod (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.field "method" OptimizedDecoder.string
|> OptimizedDecoder.map methodFromString
)
|> ServerRequest
{-| -}
withHost : ServerRequest (String -> value) -> ServerRequest value
withHost (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.field "host" OptimizedDecoder.string)
|> ServerRequest
{-| -}
withProtocol : ServerRequest (Url.Protocol -> value) -> ServerRequest value
withProtocol (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.field "protocol" OptimizedDecoder.string
|> OptimizedDecoder.andThen
(\protocol ->
if protocol |> String.startsWith "https" then
OptimizedDecoder.succeed Url.Https
else if protocol |> String.startsWith "http" then
OptimizedDecoder.succeed Url.Http
else
OptimizedDecoder.fail <| "Unexpected protocol: " ++ protocol
)
)
|> ServerRequest
{-| -}
withQueryParams : ServerRequest (QueryParams -> value) -> ServerRequest value
withQueryParams (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.field "query" OptimizedDecoder.string
|> OptimizedDecoder.map QueryParams.fromString
)
|> ServerRequest
{-| -}
optionalHeader : String -> ServerRequest (Maybe String -> value) -> ServerRequest value
optionalHeader headerName (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.optionalField (headerName |> String.toLower) OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
)
|> 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) =
decoder
|> OptimizedDecoder.andMap bodyDecoder
|> ServerRequest
bodyDecoder : OptimizedDecoder.Decoder (Maybe String)
bodyDecoder =
OptimizedDecoder.field "body" (OptimizedDecoder.nullable OptimizedDecoder.string)
{-| -}
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"
)
bodyDecoder
)
|> ServerRequest
{-| -}
type Method
= Connect
| Delete
| Get
| Head
| Options
| Patch
| Post
| Put
| Trace
| NonStandard String
methodFromString : String -> Method
methodFromString rawMethod =
case rawMethod |> String.toLower of
"connect" ->
Connect
"delete" ->
Delete
"get" ->
Get
"head" ->
Head
"options" ->
Options
"patch" ->
Patch
"post" ->
Post
"put" ->
Put
"trace" ->
Trace
_ ->
NonStandard rawMethod
{-| Gets the HTTP Method as a String, like 'GET', 'PUT', etc.
-}
methodToString : Method -> String
methodToString method =
case method of
Connect ->
"CONNECT"
Delete ->
"DELETE"
Get ->
"GET"
Head ->
"HEAD"
Options ->
"OPTIONS"
Patch ->
"PATCH"
Post ->
"POST"
Put ->
"PUT"
Trace ->
"TRACE"
NonStandard nonStandardMethod ->
nonStandardMethod
{-| This will be passed in wherever it's possible to access the DataSource.ServerRequest, like in a serverless request. This data, like the query params or incoming request headers,
do not exist for pre-rendered pages since they are not responding to a user request. They are built in advance. This value ensures that the compiler will make sure you can only use
the DataSource.ServerRequest API when it will actually be there for you to use.
-}
type alias IsAvailable =
Internal.ServerRequest.IsAvailable

View File

@ -3,7 +3,6 @@ module Pages.ProgramConfig exposing (ProgramConfig)
import ApiRoute
import Browser.Navigation
import DataSource
import DataSource.ServerRequest
import Head
import Html exposing (Html)
import Json.Decode as Decode
@ -38,7 +37,7 @@ type alias ProgramConfig userMsg userModel route siteData pageData sharedData =
, update : sharedData -> pageData -> Maybe Browser.Navigation.Key -> userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : route -> Path -> userModel -> Sub userMsg
, sharedData : DataSource.DataSource sharedData
, data : DataSource.ServerRequest.IsAvailable -> route -> DataSource.DataSource (PageServerResponse pageData)
, data : route -> DataSource.DataSource (PageServerResponse pageData)
, view :
{ path : Path
, route : route

View File

@ -1,5 +1,5 @@
module Server.Request exposing
( ServerRequest(..)
( Request(..)
, Method(..), methodToString
, succeed
, requestTime, optionalHeader, expectContentType, expectJsonBody, acceptMethod, jsonBodyResult
@ -14,7 +14,7 @@ module Server.Request exposing
{-|
@docs ServerRequest
@docs Request
@docs Method, methodToString
@ -70,8 +70,8 @@ import Time
{-| -}
type ServerRequest decodesTo
= ServerRequest (OptimizedDecoder.Decoder ( Result ValidationError decodesTo, List ValidationError ))
type Request decodesTo
= Request (OptimizedDecoder.Decoder ( Result ValidationError decodesTo, List ValidationError ))
oneOfInternal : List ValidationError -> List (OptimizedDecoder.Decoder ( Result ValidationError decodesTo, List ValidationError )) -> OptimizedDecoder.Decoder ( Result ValidationError decodesTo, List ValidationError )
@ -110,15 +110,15 @@ oneOfInternal previousErrors optimizedDecoders =
{-| -}
succeed : value -> ServerRequest value
succeed : value -> Request value
succeed value =
ServerRequest (OptimizedDecoder.succeed ( Ok value, [] ))
Request (OptimizedDecoder.succeed ( Ok value, [] ))
{-| TODO internal only
-}
getDecoder : ServerRequest (DataSource response) -> OptimizedDecoder.Decoder (Result ( ValidationError, List ValidationError ) (DataSource response))
getDecoder (ServerRequest decoder) =
getDecoder : Request (DataSource response) -> OptimizedDecoder.Decoder (Result ( ValidationError, List ValidationError ) (DataSource response))
getDecoder (Request decoder) =
decoder
|> OptimizedDecoder.map
(\( result, validationErrors ) ->
@ -188,9 +188,9 @@ errorToString validationError =
{-| -}
map : (a -> b) -> ServerRequest a -> ServerRequest b
map mapFn (ServerRequest decoder) =
ServerRequest
map : (a -> b) -> Request a -> Request b
map mapFn (Request decoder) =
Request
(OptimizedDecoder.map
(\( result, errors ) ->
( Result.map mapFn result, errors )
@ -200,12 +200,12 @@ map mapFn (ServerRequest decoder) =
{-| -}
oneOf : List (ServerRequest a) -> ServerRequest a
oneOf : List (Request a) -> Request a
oneOf serverRequests =
ServerRequest
Request
(oneOfInternal []
(List.map
(\(ServerRequest decoder) -> decoder)
(\(Request decoder) -> decoder)
serverRequests
)
)
@ -225,13 +225,13 @@ oneOf serverRequests =
--> Success "hihihi"
-}
andMap : ServerRequest a -> ServerRequest (a -> b) -> ServerRequest b
andMap : Request a -> Request (a -> b) -> Request b
andMap =
map2 (|>)
andThen : (a -> ServerRequest b) -> ServerRequest a -> ServerRequest b
andThen toRequestB (ServerRequest requestA) =
andThen : (a -> Request b) -> Request a -> Request b
andThen toRequestB (Request requestA) =
OptimizedDecoder.andThen
(\value ->
case value of
@ -244,18 +244,18 @@ andThen toRequestB (ServerRequest requestA) =
OptimizedDecoder.succeed ( Err error, errors )
)
requestA
|> ServerRequest
|> Request
unwrap : ServerRequest a -> Decoder ( Result ValidationError a, List ValidationError )
unwrap (ServerRequest decoder_) =
unwrap : Request a -> Decoder ( Result ValidationError a, List ValidationError )
unwrap (Request decoder_) =
decoder_
{-| -}
map2 : (a -> b -> c) -> ServerRequest a -> ServerRequest b -> ServerRequest c
map2 f (ServerRequest jdA) (ServerRequest jdB) =
ServerRequest
map2 : (a -> b -> c) -> Request a -> Request b -> Request c
map2 f (Request jdA) (Request jdB) =
Request
(OptimizedDecoder.map2
(\( result1, errors1 ) ( result2, errors2 ) ->
( Result.map2 f result1 result2
@ -268,22 +268,22 @@ map2 f (ServerRequest jdA) (ServerRequest jdB) =
{-| -}
expectHeader : String -> ServerRequest String
expectHeader : String -> Request String
expectHeader headerName =
OptimizedDecoder.optionalField (headerName |> String.toLower) OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
|> OptimizedDecoder.andThen (\value -> OptimizedDecoder.fromResult (value |> Result.fromMaybe "Missing field headers"))
|> noErrors
|> ServerRequest
|> Request
{-| -}
requestTime : ServerRequest Time.Posix
requestTime : Request Time.Posix
requestTime =
OptimizedDecoder.field "requestTime"
(OptimizedDecoder.int |> OptimizedDecoder.map Time.millisToPosix)
|> noErrors
|> ServerRequest
|> Request
okOrInternalError : OptimizedDecoder.Decoder a -> OptimizedDecoder.Decoder (Result ValidationError a)
@ -293,13 +293,13 @@ okOrInternalError decoder =
{-| -}
method : ServerRequest Method
method : Request Method
method =
(OptimizedDecoder.field "method" OptimizedDecoder.string
|> OptimizedDecoder.map methodFromString
)
|> noErrors
|> ServerRequest
|> Request
noErrors : OptimizedDecoder.Decoder value -> OptimizedDecoder.Decoder ( Result ValidationError value, List ValidationError )
@ -309,8 +309,8 @@ noErrors decoder =
{-| -}
acceptMethod : ( Method, List Method ) -> ServerRequest value -> ServerRequest value
acceptMethod ( accepted1, accepted ) (ServerRequest decoder) =
acceptMethod : ( Method, List Method ) -> Request value -> Request value
acceptMethod ( accepted1, accepted ) (Request decoder) =
(OptimizedDecoder.field "method" OptimizedDecoder.string
|> OptimizedDecoder.map methodFromString
|> OptimizedDecoder.andThen
@ -327,11 +327,11 @@ acceptMethod ( accepted1, accepted ) (ServerRequest decoder) =
)
)
)
|> ServerRequest
|> Request
{-| -}
matchesMethod : ( Method, List Method ) -> ServerRequest Bool
matchesMethod : ( Method, List Method ) -> Request Bool
matchesMethod ( accepted1, accepted ) =
(OptimizedDecoder.field "method" OptimizedDecoder.string
|> OptimizedDecoder.map methodFromString
@ -341,7 +341,7 @@ matchesMethod ( accepted1, accepted ) =
)
)
|> noErrors
|> ServerRequest
|> Request
appendError : ValidationError -> OptimizedDecoder.Decoder ( value, List ValidationError ) -> OptimizedDecoder.Decoder ( value, List ValidationError )
@ -351,25 +351,25 @@ appendError error decoder =
{-| -}
allQueryParams : ServerRequest QueryParams
allQueryParams : Request QueryParams
allQueryParams =
OptimizedDecoder.field "query" OptimizedDecoder.string
|> OptimizedDecoder.map QueryParams.fromString
|> noErrors
|> ServerRequest
|> Request
{-| -}
queryParam : String -> ServerRequest (Maybe String)
queryParam : String -> Request (Maybe String)
queryParam name =
OptimizedDecoder.optionalField name OptimizedDecoder.string
|> OptimizedDecoder.field "query"
|> noErrors
|> ServerRequest
|> Request
{-| -}
expectQueryParam : String -> ServerRequest String
expectQueryParam : String -> Request String
expectQueryParam name =
OptimizedDecoder.optionalField name OptimizedDecoder.string
|> OptimizedDecoder.field "query"
@ -382,20 +382,20 @@ expectQueryParam name =
Nothing ->
( Err (ValidationError ("Missing query param \"" ++ name ++ "\"")), [] )
)
|> ServerRequest
|> Request
{-| -}
optionalHeader : String -> ServerRequest (Maybe String)
optionalHeader : String -> Request (Maybe String)
optionalHeader headerName =
OptimizedDecoder.optionalField (headerName |> String.toLower) OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
|> noErrors
|> ServerRequest
|> Request
{-| -}
allCookies : ServerRequest (Dict String String)
allCookies : Request (Dict String String)
allCookies =
OptimizedDecoder.optionalField "cookie" OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
@ -406,11 +406,11 @@ allCookies =
|> CookieParser.parse
)
|> noErrors
|> ServerRequest
|> Request
{-| -}
expectCookie : String -> ServerRequest String
expectCookie : String -> Request String
expectCookie name =
OptimizedDecoder.optionalField name OptimizedDecoder.string
|> OptimizedDecoder.field "cookies"
@ -423,19 +423,19 @@ expectCookie name =
Nothing ->
( Err (ValidationError ("Missing cookie " ++ name)), [] )
)
|> ServerRequest
|> Request
{-| -}
cookie : String -> ServerRequest (Maybe String)
cookie : String -> Request (Maybe String)
cookie name =
OptimizedDecoder.optionalField name OptimizedDecoder.string
|> OptimizedDecoder.field "cookies"
|> noErrors
|> ServerRequest
|> Request
formField_ : String -> ServerRequest String
formField_ : String -> Request String
formField_ name =
OptimizedDecoder.optionalField name OptimizedDecoder.string
|> OptimizedDecoder.map
@ -447,14 +447,14 @@ formField_ name =
Nothing ->
( Err (ValidationError ("Missing form field " ++ name)), [] )
)
|> ServerRequest
|> Request
optionalFormField_ : String -> ServerRequest (Maybe String)
optionalFormField_ : String -> Request (Maybe String)
optionalFormField_ name =
OptimizedDecoder.optionalField name OptimizedDecoder.string
|> noErrors
|> ServerRequest
|> Request
{-| -}
@ -465,7 +465,7 @@ type alias File =
}
fileField_ : String -> ServerRequest File
fileField_ : String -> Request File
fileField_ name =
OptimizedDecoder.optionalField name
(OptimizedDecoder.oneOf
@ -484,17 +484,17 @@ fileField_ name =
Nothing ->
( Err (ValidationError ("Missing form field " ++ name)), [] )
)
|> ServerRequest
|> Request
{-| -}
expectFormPost :
({ field : String -> ServerRequest String
, optionalField : String -> ServerRequest (Maybe String)
({ field : String -> Request String
, optionalField : String -> Request (Maybe String)
}
-> ServerRequest decodedForm
-> Request decodedForm
)
-> ServerRequest decodedForm
-> Request decodedForm
expectFormPost toForm =
map2 Tuple.pair
(matchesContentType "application/x-www-form-urlencoded")
@ -511,11 +511,11 @@ expectFormPost toForm =
)
, []
)
|> ServerRequest
|> Request
else
toForm { field = formField_, optionalField = optionalFormField_ }
|> (\(ServerRequest decoder) -> decoder)
|> (\(Request decoder) -> decoder)
|> OptimizedDecoder.optionalField "formData"
|> OptimizedDecoder.map
(\value ->
@ -526,19 +526,19 @@ expectFormPost toForm =
Nothing ->
( Err (ValidationError "Expected form data"), [] )
)
|> ServerRequest
|> Request
)
{-| -}
expectMultiPartFormPost :
({ field : String -> ServerRequest String
, optionalField : String -> ServerRequest (Maybe String)
, fileField : String -> ServerRequest File
({ field : String -> Request String
, optionalField : String -> Request (Maybe String)
, fileField : String -> Request File
}
-> ServerRequest decodedForm
-> Request decodedForm
)
-> ServerRequest decodedForm
-> Request decodedForm
expectMultiPartFormPost toForm =
map2 (\_ value -> value)
(expectContentType "multipart/form-data")
@ -547,23 +547,23 @@ expectMultiPartFormPost toForm =
, optionalField = optionalFormField_
, fileField = fileField_
}
|> (\(ServerRequest decoder) -> decoder)
|> (\(Request decoder) -> decoder)
|> OptimizedDecoder.field "multiPartFormData"
|> ServerRequest
|> Request
|> acceptMethod ( Post, [] )
)
{-| -}
body : ServerRequest (Maybe String)
body : Request (Maybe String)
body =
bodyDecoder
|> noErrors
|> ServerRequest
|> Request
{-| -}
expectContentType : String -> ServerRequest Bool
expectContentType : String -> Request Bool
expectContentType expectedContentType =
OptimizedDecoder.optionalField ("content-type" |> String.toLower) OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
@ -580,10 +580,10 @@ expectContentType expectedContentType =
else
( Ok False, [ ValidationError ("Expected content-type to be " ++ expectedContentType ++ " but it was " ++ contentType) ] )
)
|> ServerRequest
|> Request
matchesContentType : String -> ServerRequest (Maybe Bool)
matchesContentType : String -> Request (Maybe Bool)
matchesContentType expectedContentType =
OptimizedDecoder.optionalField ("content-type" |> String.toLower) OptimizedDecoder.string
|> OptimizedDecoder.field "headers"
@ -601,7 +601,7 @@ matchesContentType expectedContentType =
Just False
)
|> noErrors
|> ServerRequest
|> Request
parseContentType : String -> String
@ -613,7 +613,7 @@ parseContentType rawContentType =
{-| -}
expectJsonBody : OptimizedDecoder.Decoder value -> ServerRequest value
expectJsonBody : OptimizedDecoder.Decoder value -> Request value
expectJsonBody jsonBodyDecoder =
map2 (\_ secondValue -> secondValue)
(expectContentType "application/json")
@ -625,12 +625,12 @@ expectJsonBody jsonBodyDecoder =
|> OptimizedDecoder.map (Result.mapError JsonDecodeError)
]
|> OptimizedDecoder.map (\value -> ( value, [] ))
|> ServerRequest
|> Request
)
{-| -}
jsonBodyResult : OptimizedDecoder.Decoder value -> ServerRequest (Result Json.Decode.Error value)
jsonBodyResult : OptimizedDecoder.Decoder value -> Request (Result Json.Decode.Error value)
jsonBodyResult jsonBodyDecoder =
map2 (\_ secondValue -> secondValue)
(expectContentType "application/json")
@ -641,7 +641,7 @@ jsonBodyResult jsonBodyDecoder =
|> OptimizedDecoder.map (OptimizedDecoder.decodeValue jsonBodyDecoder)
]
|> noErrors
|> ServerRequest
|> Request
)

View File

@ -97,8 +97,8 @@ type alias Request =
}
expectMatch : Request -> Request.ServerRequest value -> Expectation
expectMatch request (Request.ServerRequest decoder) =
expectMatch : Request -> Request.Request value -> Expectation
expectMatch request (Request.Request decoder) =
case
request
|> requestToJson
@ -125,8 +125,8 @@ expectMatch request (Request.ServerRequest decoder) =
Expect.fail (Json.Decode.errorToString error)
expectNoMatch : Request -> String -> Request.ServerRequest value -> Expectation
expectNoMatch request expectedErrorString (Request.ServerRequest decoder) =
expectNoMatch : Request -> String -> Request.Request value -> Expectation
expectNoMatch request expectedErrorString (Request.Request decoder) =
case
request
|> requestToJson

View File

@ -1027,7 +1027,7 @@ startLowLevel apiRoutes staticHttpCache pages =
, update = \_ _ _ _ _ -> ( (), Cmd.none )
, basePath = []
, data =
\isAvailable (Route pageRoute) ->
\(Route pageRoute) ->
let
thing : Maybe (DataSource a)
thing =
@ -1184,7 +1184,7 @@ startWithRoutes pageToLoad staticRoutes staticHttpCache pages =
, update = \_ _ _ _ _ -> ( (), Cmd.none )
, basePath = []
, data =
\isAvailable (Route pageRoute) ->
\(Route pageRoute) ->
let
thing : Maybe (DataSource a)
thing =