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

262 lines
8.0 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
2022-01-24 00:42:50 +03:00
import MySession
2022-02-10 20:55:20 +03:00
import Pages.Manifest as Manifest
import Route exposing (Route)
2021-12-28 19:32:25 +03:00
import Server.Request
2022-01-01 03:15:41 +03:00
import Server.Response
2022-01-28 20:27:43 +03:00
import Server.Session as Session
2022-02-10 20:55:20 +03:00
import Site
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
, DataSource.succeed manifest |> Manifest.generator Site.canonicalUrl
]
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 (Json.Decode.field "first" Json.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 ->
Server.Response.plainText ("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-04-07 02:29:38 +03:00
(Route.redirectTo Route.Index)
)
)
|> 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
2022-01-28 02:45:51 +03:00
"https://api.github.com/repos/dillonkearns/elm-pages"
(Json.Decode.field "stargazers_count" Json.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
2022-01-28 02:45:51 +03:00
("https://api.github.com/repos/dillonkearns/" ++ repoName)
(Json.Decode.field "stargazers_count" Json.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
2022-01-24 00:42:50 +03:00
(MySession.withSession
(Server.Request.succeed ())
(\() sessionResult ->
DataSource.succeed
( Session.empty
2022-04-07 02:29:38 +03:00
, Route.redirectTo Route.Login
2022-01-24 00:42:50 +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
2022-01-28 02:45:51 +03:00
("https://api.github.com/repos/dillonkearns/" ++ repoName)
(Json.Decode.field "stargazers_count" Json.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
2022-01-28 02:45:51 +03:00
("https://api.github.com/repos/dillonkearns/" ++ repoName)
(Json.Decode.field "stargazers_count" Json.Decode.int)
2021-12-21 20:08:17 +03:00
|> 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
2022-01-28 02:45:51 +03:00
("https://api.github.com/repos/dillonkearns/" ++ repoName)
(Json.Decode.field "stargazers_count" Json.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"
2022-02-10 20:55:20 +03:00
manifest : Manifest.Config
manifest =
Manifest.init
{ name = "Site Name"
, description = "Description"
, startUrl = Route.Index |> Route.toPath
, icons = []
}