diff --git a/examples/pokedex/src/Api.elm b/examples/pokedex/src/Api.elm index a08492a8..eb074afa 100644 --- a/examples/pokedex/src/Api.elm +++ b/examples/pokedex/src/Api.elm @@ -5,6 +5,7 @@ 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 @@ -28,9 +29,31 @@ routes getStaticRoutes htmlToString = , logout , greet , fileLength + , jsonError ] +jsonError : Server.Request.ServerRequest ServerResponse.ServerResponse +jsonError = + Server.Request.oneOf + [ Server.Request.jsonBodyResult (Decode.field "name" Decode.string) + |> Server.Request.map + (\result -> + case result of + Ok firstName -> + ServerResponse.stringBody + ("Hello " ++ firstName) + + Err decodeError -> + decodeError + |> Json.Decode.errorToString + |> ServerResponse.stringBody + |> ServerResponse.withStatusCode 400 + ) + , Server.Request.succeed (ServerResponse.stringBody "Hello anonymous!") + ] + + greet : ApiRoute ApiRoute.Response greet = ApiRoute.succeed @@ -46,7 +69,7 @@ greet = field "first" ) ] - |> Server.Request.thenRespond + |> Server.Request.map (\firstName -> ServerResponse.stringBody ("Hello " ++ firstName) |> DataSource.succeed @@ -65,7 +88,7 @@ fileLength = (\{ field, optionalField, fileField } -> fileField "file" ) - |> Server.Request.thenRespond + |> Server.Request.map (\file -> ServerResponse.json (Json.Encode.object @@ -94,12 +117,10 @@ fileLength = redirectRoute : ApiRoute ApiRoute.Response redirectRoute = ApiRoute.succeed - (Server.Request.succeed () - |> Server.Request.thenRespond - (\() -> - DataSource.succeed - (ServerResponse.temporaryRedirect "/") - ) + (Server.Request.succeed + (DataSource.succeed + (ServerResponse.temporaryRedirect "/") + ) ) |> ApiRoute.literal "api" |> ApiRoute.slash @@ -128,21 +149,19 @@ serverRequestDataSource isAvailable = noArgs : ApiRoute ApiRoute.Response noArgs = ApiRoute.succeed - (Server.Request.succeed () - |> Server.Request.thenRespond - (\() -> - DataSource.Http.get - (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") - (Decode.field "stargazers_count" Decode.int) - |> DataSource.map - (\stars -> - Json.Encode.object - [ ( "repo", Json.Encode.string "elm-pages" ) - , ( "stars", Json.Encode.int stars ) - ] - |> ServerResponse.json - ) - ) + (Server.Request.succeed + (DataSource.Http.get + (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") + (Decode.field "stargazers_count" Decode.int) + |> DataSource.map + (\stars -> + Json.Encode.object + [ ( "repo", Json.Encode.string "elm-pages" ) + , ( "stars", Json.Encode.int stars ) + ] + |> ServerResponse.json + ) + ) ) |> ApiRoute.literal "api" |> ApiRoute.slash @@ -179,20 +198,18 @@ nonHybridRoute = logout : ApiRoute ApiRoute.Response logout = ApiRoute.succeed - (Server.Request.succeed () - |> Server.Request.thenRespond - (\() -> - DataSource.succeed - (ServerResponse.stringBody "You are logged out" - |> ServerResponse.withHeader "Set-Cookie" - (SetCookie.setCookie "username" "" - |> SetCookie.httpOnly - |> SetCookie.withPath "/" - |> SetCookie.withImmediateExpiration - |> SetCookie.toString - ) + (Server.Request.succeed + (DataSource.succeed + (ServerResponse.stringBody "You are logged out" + |> ServerResponse.withHeader "Set-Cookie" + (SetCookie.setCookie "username" "" + |> SetCookie.httpOnly + |> SetCookie.withPath "/" + |> SetCookie.withImmediateExpiration + |> SetCookie.toString ) ) + ) ) |> ApiRoute.literal "api" |> ApiRoute.slash @@ -204,21 +221,19 @@ repoStars : ApiRoute ApiRoute.Response repoStars = ApiRoute.succeed (\repoName -> - Server.Request.succeed () - |> Server.Request.thenRespond - (\() -> - DataSource.Http.get - (Secrets.succeed ("https://api.github.com/repos/dillonkearns/" ++ repoName)) - (Decode.field "stargazers_count" Decode.int) - |> DataSource.map - (\stars -> - Json.Encode.object - [ ( "repo", Json.Encode.string repoName ) - , ( "stars", Json.Encode.int stars ) - ] - |> ServerResponse.json - ) - ) + Server.Request.succeed + (DataSource.Http.get + (Secrets.succeed ("https://api.github.com/repos/dillonkearns/" ++ repoName)) + (Decode.field "stargazers_count" Decode.int) + |> DataSource.map + (\stars -> + Json.Encode.object + [ ( "repo", Json.Encode.string repoName ) + , ( "stars", Json.Encode.int stars ) + ] + |> ServerResponse.json + ) + ) ) |> ApiRoute.literal "api" |> ApiRoute.slash diff --git a/examples/pokedex/src/Page/FileUpload.elm b/examples/pokedex/src/Page/FileUpload.elm index d798c9d5..9805d82a 100644 --- a/examples/pokedex/src/Page/FileUpload.elm +++ b/examples/pokedex/src/Page/FileUpload.elm @@ -39,22 +39,19 @@ type alias Data = Maybe Request.File -data : RouteParams -> Request.Handler (PageServerResponse Data) +data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data)) data routeParams = - Request.oneOfHandler + Request.oneOf [ Request.expectMultiPartFormPost (\{ field, optionalField, fileField } -> fileField "file" ) - |> Request.thenRespond + |> Request.map (\file -> DataSource.succeed (PageServerResponse.RenderPage (Just file)) ) - , Request.succeed () - |> Request.thenRespond - (\() -> - DataSource.succeed (PageServerResponse.RenderPage Nothing) - ) + , Request.succeed + (DataSource.succeed (PageServerResponse.RenderPage Nothing)) ] diff --git a/examples/pokedex/src/Page/Greet.elm b/examples/pokedex/src/Page/Greet.elm index c22016f5..8565ef8b 100644 --- a/examples/pokedex/src/Page/Greet.elm +++ b/examples/pokedex/src/Page/Greet.elm @@ -39,13 +39,13 @@ page = |> Page.buildNoState { view = view } -data : RouteParams -> Request.Handler (PageServerResponse Data) +data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data)) data routeParams = - Request.oneOfHandler + Request.oneOf [ Request.map2 Data (Request.expectQueryParam "name") Request.requestTime - |> Request.thenRespond + |> Request.map (\requestData -> requestData |> PageServerResponse.RenderPage @@ -54,18 +54,16 @@ data routeParams = , Request.map2 Data (Request.expectCookie "username") Request.requestTime - |> Request.thenRespond + |> Request.map (\requestData -> requestData |> PageServerResponse.RenderPage |> DataSource.succeed ) - , Request.succeed () - |> Request.thenRespond - (\() -> - DataSource.succeed - (PageServerResponse.ServerResponse (ServerResponse.temporaryRedirect "/login")) - ) + , Request.succeed + (DataSource.succeed + (PageServerResponse.ServerResponse (ServerResponse.temporaryRedirect "/login")) + ) ] diff --git a/examples/pokedex/src/Page/Login.elm b/examples/pokedex/src/Page/Login.elm index 0606303f..5f9cc3e7 100644 --- a/examples/pokedex/src/Page/Login.elm +++ b/examples/pokedex/src/Page/Login.elm @@ -44,11 +44,11 @@ type alias Request = } -data : RouteParams -> Request.Handler (PageServerResponse Data) +data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data)) data routeParams = - Request.oneOfHandler + Request.oneOf [ Request.expectFormPost (\{ field } -> field "name") - |> Request.thenRespond + |> Request.map (\name -> PageServerResponse.ServerResponse ("/greet" @@ -63,7 +63,7 @@ data routeParams = |> DataSource.succeed ) , Request.cookie "username" - |> Request.thenRespond + |> Request.map (\name -> name |> Data diff --git a/examples/pokedex/src/Page/Secret.elm b/examples/pokedex/src/Page/Secret.elm index 12d23388..c25cb092 100644 --- a/examples/pokedex/src/Page/Secret.elm +++ b/examples/pokedex/src/Page/Secret.elm @@ -49,11 +49,11 @@ type alias LoggedInInfo = } -data : RouteParams -> Request.Handler (PageServerResponse Data) +data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data)) data routeParams = - Request.oneOfHandler + Request.oneOf [ Request.expectCookie "username" - |> Request.thenRespond + |> Request.map (\username -> username |> LoggedInInfo @@ -62,18 +62,16 @@ data routeParams = |> DataSource.map LoggedIn |> DataSource.map PageServerResponse.RenderPage ) - , Request.succeed () - |> Request.thenRespond - (\() -> - NotLoggedIn - |> DataSource.succeed - |> DataSource.map PageServerResponse.RenderPage - --"/login" - -- |> ServerResponse.temporaryRedirect - -- --|> ServerResponse.withStatusCode 404 - -- |> PageServerResponse.ServerResponse - -- |> DataSource.succeed - ) + , Request.succeed + (NotLoggedIn + |> DataSource.succeed + |> DataSource.map PageServerResponse.RenderPage + --"/login" + -- |> ServerResponse.temporaryRedirect + -- --|> ServerResponse.withStatusCode 404 + -- |> PageServerResponse.ServerResponse + -- |> DataSource.succeed + ) ] diff --git a/examples/pokedex/src/Page/Time.elm b/examples/pokedex/src/Page/Time.elm index 04ebf324..1044a8f4 100644 --- a/examples/pokedex/src/Page/Time.elm +++ b/examples/pokedex/src/Page/Time.elm @@ -95,10 +95,10 @@ type alias Request = -- |> DataSource.map PageServerResponse.RenderPage -data : RouteParams -> Request.Handler (PageServerResponse Data) +data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data)) data routeParams = Request.succeed () - |> Request.thenRespond + |> Request.map (\() -> PageServerResponse.ServerResponse (ServerResponse.stringBody "Hello, this is a string") |> DataSource.succeed diff --git a/generator/src/Page.elm b/generator/src/Page.elm index 79a8308d..6fb26f63 100644 --- a/generator/src/Page.elm +++ b/generator/src/Page.elm @@ -335,7 +335,7 @@ preRenderWithFallback { data, head, pages } = {-| -} serverRender : - { data : routeParams -> Server.Request.Handler (PageServerResponse data) + { data : routeParams -> Server.Request.ServerRequest (DataSource (PageServerResponse data)) , head : StaticPayload data routeParams -> List Head.Tag } -> Builder routeParams data diff --git a/generator/src/codegen-template-module.js b/generator/src/codegen-template-module.js index 48f4f609..8b9dc785 100755 --- a/generator/src/codegen-template-module.js +++ b/generator/src/codegen-template-module.js @@ -182,7 +182,7 @@ type alias Data = ${ serverRender - ? `data : RouteParams -> Request.Handler (PageServerResponse Data) + ? `data : RouteParams -> Request.ServerRequest (DataSource (PageServerResponse Data)) data routeParams =` : withFallback ? `data : RouteParams -> DataSource (PageServerResponse Data) diff --git a/src/ApiRoute.elm b/src/ApiRoute.elm index 0e2cca79..c7f33956 100644 --- a/src/ApiRoute.elm +++ b/src/ApiRoute.elm @@ -87,7 +87,7 @@ stripTrailingSlash path = {-| -} -serverRender : ApiRouteBuilder (Server.Request.Handler ServerResponse) constructor -> ApiRoute Response +serverRender : ApiRouteBuilder (Server.Request.ServerRequest (DataSource ServerResponse)) constructor -> ApiRoute Response serverRender ((ApiRouteBuilder patterns pattern _ toString constructor) as fullHandler) = ApiRoute { regex = Regex.fromString ("^" ++ pattern ++ "$") |> Maybe.withDefault Regex.never diff --git a/src/Server/Request.elm b/src/Server/Request.elm index b4599508..0f2fb118 100644 --- a/src/Server/Request.elm +++ b/src/Server/Request.elm @@ -2,8 +2,7 @@ module Server.Request exposing ( ServerRequest(..) , Method(..) , succeed - , Handler, Handlers - , oneOfHandler, requestTime, thenRespond, optionalHeader, expectContentType, expectJsonBody, acceptMethod, jsonBodyResult + , requestTime, optionalHeader, expectContentType, expectJsonBody, acceptMethod, jsonBodyResult , map, map2, oneOf, andMap , expectQueryParam , cookie, expectCookie @@ -162,33 +161,16 @@ succeed value = ServerRequest (OptimizedDecoder.succeed ( Ok value, [] )) -{-| -} -type Handlers response - = Handlers response - - -{-| -} -type Handler response - = Handler (OptimizedDecoder.Decoder (Result ( ValidationError, List ValidationError ) (DataSource response))) - - {-| TODO internal only -} -getDecoder : Handler response -> OptimizedDecoder.Decoder (Result ( ValidationError, List ValidationError ) (DataSource response)) -getDecoder (Handler decoder) = +getDecoder : ServerRequest (DataSource response) -> OptimizedDecoder.Decoder (Result ( ValidationError, List ValidationError ) (DataSource response)) +getDecoder (ServerRequest decoder) = decoder - - -{-| -} -thenRespond : (request -> DataSource response) -> ServerRequest request -> Handler response -thenRespond thenDataSource (ServerRequest requestDecoder) = - requestDecoder |> OptimizedDecoder.map (\( result, validationErrors ) -> case ( result, validationErrors ) of ( Ok value, [] ) -> value - |> thenDataSource |> Ok ( Err fatalError, errors ) -> @@ -197,7 +179,6 @@ thenRespond thenDataSource (ServerRequest requestDecoder) = ( Ok _, firstError :: rest ) -> Err ( firstError, rest ) ) - |> Handler type ValidationError @@ -275,18 +256,6 @@ oneOf serverRequests = ) -{-| -} -oneOfHandler : List (Handler a) -> Handler a -oneOfHandler serverRequests = - Handler - (oneOfInternalHandler [] - (List.map - (\(Handler decoder) -> decoder) - serverRequests - ) - ) - - {-| Decode an argument and provide it to a function in a decoder. decoder : Decoder String