elm-pages-v3-beta/examples/pokedex/src/Api.elm

283 lines
9.1 KiB
Elm
Raw Normal View History

module Api exposing (routes)
import ApiRoute exposing (ApiRoute)
import DataSource exposing (DataSource)
import DataSource.Http
import Html exposing (Html)
import Json.Decode
import Json.Encode
import OptimizedDecoder as Decode
import Route exposing (Route)
import Secrets
2021-12-28 19:32:25 +03:00
import Server.Request
2022-01-01 03:15:41 +03:00
import Server.Response
2021-12-24 06:25:51 +03:00
import Server.SetCookie as SetCookie
routes :
DataSource (List Route)
-> (Html Never -> String)
2021-07-31 01:35:02 +03:00
-> List (ApiRoute.ApiRoute ApiRoute.Response)
routes getStaticRoutes htmlToString =
[ nonHybridRoute
, noArgs
, redirectRoute
, repoStars
2021-12-21 20:08:17 +03:00
, repoStars2
2021-12-23 08:29:10 +03:00
, logout
2021-12-28 22:53:33 +03:00
, greet
, fileLength
, jsonError
]
2021-12-31 22:17:12 +03:00
jsonError : ApiRoute ApiRoute.Response
jsonError =
2021-12-31 22:17:12 +03:00
ApiRoute.succeed
(Server.Request.oneOf
[ Server.Request.jsonBodyResult (Decode.field "name" Decode.string)
|> Server.Request.map
(\result ->
case result of
Ok firstName ->
2022-01-01 03:15:41 +03:00
Server.Response.stringBody
2021-12-31 22:17:12 +03:00
("Hello " ++ firstName)
2021-12-31 22:17:12 +03:00
Err decodeError ->
decodeError
|> Json.Decode.errorToString
2022-01-01 03:15:41 +03:00
|> Server.Response.stringBody
|> Server.Response.withStatusCode 400
2021-12-31 22:17:12 +03:00
)
2022-01-01 03:15:41 +03:00
, Server.Request.succeed (Server.Response.stringBody "Hello anonymous!")
2021-12-31 22:17:12 +03:00
]
|> Server.Request.map DataSource.succeed
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "validate-json"
|> ApiRoute.serverRender
2021-12-28 22:53:33 +03:00
greet : ApiRoute ApiRoute.Response
greet =
ApiRoute.succeed
(Server.Request.oneOf
[ Server.Request.expectFormPost
(\{ field, optionalField } ->
field "first"
)
, Server.Request.expectJsonBody (Decode.field "first" Decode.string)
, Server.Request.expectQueryParam "first"
, Server.Request.expectMultiPartFormPost
(\{ field, optionalField } ->
field "first"
)
2021-12-28 22:53:33 +03:00
]
|> Server.Request.map
(\firstName ->
2022-01-01 03:15:41 +03:00
Server.Response.stringBody ("Hello " ++ firstName)
|> DataSource.succeed
)
2021-12-28 22:53:33 +03:00
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "greet"
|> ApiRoute.serverRender
fileLength : ApiRoute ApiRoute.Response
fileLength =
ApiRoute.succeed
(Server.Request.expectMultiPartFormPost
(\{ field, optionalField, fileField } ->
fileField "file"
)
|> Server.Request.map
(\file ->
2022-01-01 03:15:41 +03:00
Server.Response.json
(Json.Encode.object
[ ( "File name: ", Json.Encode.string file.name )
, ( "Length", Json.Encode.int (String.length file.body) )
, ( "mime-type", Json.Encode.string file.mimeType )
, ( "First line"
, Json.Encode.string
(file.body
|> String.split "\n"
|> List.head
|> Maybe.withDefault ""
)
)
]
)
|> DataSource.succeed
)
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "file"
|> ApiRoute.serverRender
redirectRoute : ApiRoute ApiRoute.Response
redirectRoute =
ApiRoute.succeed
(Server.Request.succeed
(DataSource.succeed
2022-01-01 03:15:41 +03:00
(Server.Response.temporaryRedirect "/")
)
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "redirect"
2021-12-24 00:34:19 +03:00
|> ApiRoute.serverRender
noArgs : ApiRoute ApiRoute.Response
noArgs =
ApiRoute.succeed
(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 )
]
2022-01-01 03:15:41 +03:00
|> Server.Response.json
)
)
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "stars"
2021-12-24 00:34:19 +03:00
|> ApiRoute.serverRender
nonHybridRoute =
ApiRoute.succeed
(\repoName ->
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 )
]
|> Json.Encode.encode 2
)
)
|> ApiRoute.literal "repo"
|> ApiRoute.slash
|> ApiRoute.capture
|> ApiRoute.preRender
(\route ->
DataSource.succeed
[ route "elm-graphql"
]
)
2021-12-23 08:29:10 +03:00
logout : ApiRoute ApiRoute.Response
logout =
ApiRoute.succeed
(Server.Request.succeed
(DataSource.succeed
2022-01-01 03:15:41 +03:00
(Server.Response.stringBody "You are logged out"
|> Server.Response.withHeader "Set-Cookie"
(SetCookie.setCookie "username" ""
|> SetCookie.httpOnly
|> SetCookie.withPath "/"
|> SetCookie.withImmediateExpiration
|> SetCookie.toString
2021-12-24 06:25:51 +03:00
)
2021-12-23 08:29:10 +03:00
)
)
2021-12-23 08:29:10 +03:00
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "logout"
2021-12-24 00:34:19 +03:00
|> ApiRoute.serverRender
2021-12-23 08:29:10 +03:00
repoStars : ApiRoute ApiRoute.Response
repoStars =
ApiRoute.succeed
2021-12-28 19:32:25 +03:00
(\repoName ->
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 )
]
2022-01-01 03:15:41 +03:00
|> Server.Response.json
)
)
)
|> ApiRoute.literal "api"
|> ApiRoute.slash
|> ApiRoute.literal "repo"
|> ApiRoute.slash
|> ApiRoute.capture
--|> ApiRoute.literal ".json"
2021-12-24 00:34:19 +03:00
|> ApiRoute.serverRender
2021-12-21 20:08:17 +03:00
repoStars2 : ApiRoute ApiRoute.Response
repoStars2 =
ApiRoute.succeed
(\repoName ->
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 )
]
2022-01-01 03:15:41 +03:00
|> Server.Response.json
2021-12-21 20:08:17 +03:00
)
)
|> ApiRoute.literal "api2"
|> ApiRoute.slash
|> ApiRoute.literal "repo"
|> ApiRoute.slash
|> ApiRoute.capture
2021-12-24 00:34:19 +03:00
|> ApiRoute.preRenderWithFallback
2021-12-21 20:08:17 +03:00
(\route ->
DataSource.succeed
[ route "elm-graphql"
, route "elm-pages"
]
)
route1 =
ApiRoute.succeed
(\repoName ->
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 )
]
|> Json.Encode.encode 2
)
)
|> ApiRoute.literal "repo"
|> ApiRoute.slash
|> ApiRoute.capture
|> ApiRoute.literal ".json"