elm-pages-v3-beta/examples/end-to-end/app/Api.elm

194 lines
5.8 KiB
Elm
Raw Normal View History

2022-02-25 21:04:19 +03:00
module Api exposing (routes)
import ApiRoute exposing (ApiRoute)
import DataSource exposing (DataSource)
2022-07-09 03:56:52 +03:00
import Form.Field as Field
2022-07-09 03:51:26 +03:00
import Form.Validation as Validation
2022-02-25 21:04:19 +03:00
import Html exposing (Html)
import Json.Decode as Decode
import Json.Encode as Encode
2022-03-01 04:45:16 +03:00
import Pages
2022-06-30 19:38:49 +03:00
import Pages.Form as Form
2022-03-01 04:45:16 +03:00
import Random
2022-03-09 19:49:46 +03:00
import Result.Extra
2022-02-25 21:04:19 +03:00
import Route exposing (Route)
import Server.Request as Request
2022-03-01 04:45:16 +03:00
import Server.Response as Response exposing (Response)
2022-03-01 01:47:22 +03:00
import Test.Glob
2022-03-01 04:45:16 +03:00
import Test.Runner.Html
import Time
2022-03-09 19:49:46 +03:00
import Xml.Decode
2022-02-25 21:04:19 +03:00
routes :
DataSource (List Route)
-> (Html Never -> String)
-> List (ApiRoute.ApiRoute ApiRoute.Response)
routes getStaticRoutes htmlToString =
2022-03-01 04:45:16 +03:00
let
html : Html Never -> Response data Never
2022-03-01 04:45:16 +03:00
html htmlValue =
htmlToString htmlValue
|> Response.body
|> Response.withHeader "Content-Type" "text/html; charset=UTF-8"
2022-03-01 04:45:16 +03:00
in
2022-02-25 21:04:19 +03:00
[ greet
2022-03-01 04:45:16 +03:00
, ApiRoute.succeed
(Request.succeed
(Test.Glob.all
|> DataSource.map viewHtmlResults
|> DataSource.map html
)
)
|> ApiRoute.literal "tests"
|> ApiRoute.serverRender
, requestPrinter
2022-03-09 19:49:46 +03:00
, xmlDecoder
, multipleContentTypes
2022-02-25 21:04:19 +03:00
]
2022-03-09 19:49:46 +03:00
xmlDecoder : ApiRoute ApiRoute.Response
xmlDecoder =
let
dataDecoder : Xml.Decode.Decoder String
dataDecoder =
Xml.Decode.path [ "path", "to", "string", "value" ] (Xml.Decode.single Xml.Decode.string)
in
ApiRoute.succeed
(Request.map2
(\_ xmlString ->
xmlString
|> Xml.Decode.run dataDecoder
|> Result.Extra.merge
|> Response.plainText
|> DataSource.succeed
)
(Request.expectContentType "application/xml")
Request.expectBody
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "xml"
|> ApiRoute.serverRender
multipleContentTypes : ApiRoute ApiRoute.Response
multipleContentTypes =
let
dataDecoder : Xml.Decode.Decoder String
dataDecoder =
Xml.Decode.path [ "path", "to", "string", "value" ] (Xml.Decode.single Xml.Decode.string)
in
ApiRoute.succeed
(Request.oneOf
[ Request.map2
(\_ xmlString ->
xmlString
|> Xml.Decode.run dataDecoder
|> Result.Extra.merge
|> Response.plainText
|> DataSource.succeed
)
(Request.expectContentType "application/xml")
Request.expectBody
, Request.map
(\decodedValue ->
decodedValue
|> Response.plainText
|> DataSource.succeed
)
(Request.expectJsonBody (Decode.at [ "path", "to", "string", "value" ] Decode.string))
]
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "multiple-content-types"
|> ApiRoute.serverRender
requestPrinter : ApiRoute ApiRoute.Response
requestPrinter =
ApiRoute.succeed
(Request.map4
(\rawBody method cookies queryParams ->
Encode.object
[ ( "rawBody"
, Maybe.map Encode.string rawBody
|> Maybe.withDefault Encode.null
)
, ( "method"
, method |> Request.methodToString |> Encode.string
)
, ( "cookies"
, cookies |> Encode.dict identity Encode.string
)
, ( "queryParams"
, queryParams |> Encode.dict identity (Encode.list Encode.string)
)
]
|> Response.json
|> DataSource.succeed
)
Request.rawBody
Request.method
Request.allCookies
Request.queryParams
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "request-test"
|> ApiRoute.serverRender
2022-03-01 04:45:16 +03:00
config : Test.Runner.Html.Config
config =
Random.initialSeed (Pages.builtAt |> Time.posixToMillis)
|> Test.Runner.Html.defaultConfig
|> Test.Runner.Html.hidePassedTests
viewHtmlResults tests =
Html.div []
[ Html.h1 [] [ Html.text "My Test Suite" ]
, Html.div [] [ Test.Runner.Html.viewResults config tests ]
]
2022-02-25 21:04:19 +03:00
greet : ApiRoute ApiRoute.Response
greet =
ApiRoute.succeed
(Request.oneOf
2022-06-30 19:38:49 +03:00
[ Request.formParserResultNew
[ Form.init
(\bar ->
Validation.succeed identity
|> Validation.withField bar
)
(\_ _ -> ())
|> Form.field "first" (Field.text |> Field.required "Required")
]
|> Request.andThen
(\result ->
result
|> Result.mapError (\_ -> "")
|> Request.fromResult
)
2022-02-25 21:04:19 +03:00
, Request.expectJsonBody (Decode.field "first" Decode.string)
, Request.expectQueryParam "first"
, Request.expectMultiPartFormPost
(\{ field, optionalField } ->
field "first"
)
]
|> Request.map
(\firstName ->
Response.plainText ("Hello " ++ firstName)
|> DataSource.succeed
)
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "greet"
|> ApiRoute.serverRender