mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2025-01-04 09:56:44 +03:00
Use standalone Api.elm module for ApiRoutes.
This commit is contained in:
parent
f5287d3419
commit
efddae6bba
79
examples/docs/src/Api.elm
Normal file
79
examples/docs/src/Api.elm
Normal file
@ -0,0 +1,79 @@
|
||||
module Api exposing (routes)
|
||||
|
||||
import ApiRoute
|
||||
import DataSource
|
||||
import DataSource.Http
|
||||
import Json.Encode
|
||||
import OptimizedDecoder as Decode
|
||||
import Secrets
|
||||
|
||||
|
||||
routes : List (ApiRoute.Done ApiRoute.Response)
|
||||
routes =
|
||||
[ ApiRoute.succeed
|
||||
(\userId ->
|
||||
DataSource.succeed
|
||||
{ body =
|
||||
Json.Encode.object
|
||||
[ ( "id", Json.Encode.int userId )
|
||||
, ( "name", Json.Encode.string ("Data for user " ++ String.fromInt userId) )
|
||||
]
|
||||
|> Json.Encode.encode 2
|
||||
}
|
||||
)
|
||||
|> ApiRoute.literal "users"
|
||||
|> ApiRoute.slash
|
||||
|> ApiRoute.int
|
||||
|> ApiRoute.literal ".json"
|
||||
|> ApiRoute.buildTimeRoutes
|
||||
(\route ->
|
||||
DataSource.succeed
|
||||
[ route 1
|
||||
, route 2
|
||||
, route 3
|
||||
]
|
||||
)
|
||||
, ApiRoute.succeed
|
||||
(\repoName ->
|
||||
DataSource.Http.get
|
||||
(Secrets.succeed ("https://api.github.com/repos/dillonkearns/" ++ repoName))
|
||||
(Decode.field "stargazers_count" Decode.int)
|
||||
|> DataSource.map
|
||||
(\stars ->
|
||||
{ body =
|
||||
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"
|
||||
|> ApiRoute.buildTimeRoutes
|
||||
(\route ->
|
||||
DataSource.succeed
|
||||
[ route "elm-graphql"
|
||||
]
|
||||
)
|
||||
|
||||
--, ApiRoute.succeed
|
||||
-- (DataSource.succeed
|
||||
-- { body =
|
||||
-- allRoutes
|
||||
-- |> List.filterMap identity
|
||||
-- |> List.map
|
||||
-- (\route ->
|
||||
-- { path = Route.routeToPath (Just route) |> String.join "/"
|
||||
-- , lastMod = Nothing
|
||||
-- }
|
||||
-- )
|
||||
-- |> Sitemap.build { siteUrl = "https://elm-pages.com" }
|
||||
-- }
|
||||
-- )
|
||||
-- |> ApiRoute.literal "sitemap.xml"
|
||||
-- |> ApiRoute.singleRoute
|
||||
]
|
@ -39,6 +39,7 @@ function generateTemplateModuleConnector(phase) {
|
||||
return {
|
||||
mainModule: `port module TemplateModulesBeta exposing (..)
|
||||
|
||||
import Api
|
||||
import Browser.Navigation
|
||||
import Route exposing (Route)
|
||||
import Document
|
||||
@ -396,6 +397,7 @@ main =
|
||||
, fromJsPort = fromJsPort identity
|
||||
, data = dataForRoute
|
||||
, sharedData = Shared.template.data
|
||||
, apiRoutes = Api.routes
|
||||
}
|
||||
|
||||
dataForRoute : Maybe Route -> DataSource PageData
|
||||
|
@ -41,6 +41,7 @@ init :
|
||||
, site : SiteConfig route siteData
|
||||
, data : route -> DataSource.DataSource pageData
|
||||
, sharedData : DataSource.DataSource sharedData
|
||||
, apiRoutes : List (ApiRoute.Done ApiRoute.Response)
|
||||
}
|
||||
-> StaticResponses
|
||||
init config =
|
||||
@ -61,42 +62,35 @@ init config =
|
||||
|
||||
buildTimeFilesRequest :
|
||||
{ config
|
||||
| getStaticRoutes : DataSource (List route)
|
||||
, site : SiteConfig route siteData
|
||||
| apiRoutes : List (ApiRoute.Done ApiRoute.Response)
|
||||
}
|
||||
-> DataSource (List (Result String { path : List String, content : String }))
|
||||
buildTimeFilesRequest config =
|
||||
config.getStaticRoutes
|
||||
|> DataSource.andThen
|
||||
(\allRoutes ->
|
||||
config.site
|
||||
allRoutes
|
||||
|> .apiRoutes
|
||||
|> List.map
|
||||
(\handler ->
|
||||
handler.buildTimeRoutes
|
||||
|> DataSource.andThen
|
||||
(\paths ->
|
||||
paths
|
||||
|> List.map
|
||||
(\path ->
|
||||
handler.matchesToResponse path
|
||||
|> DataSource.map
|
||||
(\maybeResponse ->
|
||||
case maybeResponse of
|
||||
Nothing ->
|
||||
Err ""
|
||||
config.apiRoutes
|
||||
|> List.map
|
||||
(\handler ->
|
||||
handler.buildTimeRoutes
|
||||
|> DataSource.andThen
|
||||
(\paths ->
|
||||
paths
|
||||
|> List.map
|
||||
(\path ->
|
||||
handler.matchesToResponse path
|
||||
|> DataSource.map
|
||||
(\maybeResponse ->
|
||||
case maybeResponse of
|
||||
Nothing ->
|
||||
Err ""
|
||||
|
||||
Just response ->
|
||||
Ok { path = path |> String.split "/", content = response.body }
|
||||
)
|
||||
Just response ->
|
||||
Ok { path = path |> String.split "/", content = response.body }
|
||||
)
|
||||
|> DataSource.combine
|
||||
)
|
||||
|> DataSource.combine
|
||||
)
|
||||
|> DataSource.combine
|
||||
|> DataSource.map List.concat
|
||||
)
|
||||
|> DataSource.combine
|
||||
|> DataSource.map List.concat
|
||||
|
||||
|
||||
renderSingleRoute :
|
||||
@ -204,6 +198,7 @@ nextStep :
|
||||
, data : route -> DataSource.DataSource pageData
|
||||
, sharedData : DataSource.DataSource sharedData
|
||||
, site : SiteConfig route siteData
|
||||
, apiRoutes : List (ApiRoute.Done ApiRoute.Response)
|
||||
}
|
||||
->
|
||||
{ model
|
||||
|
@ -1,5 +1,6 @@
|
||||
module Pages.ProgramConfig exposing (..)
|
||||
|
||||
import ApiRoute
|
||||
import Browser.Navigation
|
||||
import DataSource
|
||||
import Head
|
||||
@ -54,4 +55,5 @@ type alias ProgramConfig userMsg userModel route siteData pageData sharedData =
|
||||
, metadata : route
|
||||
}
|
||||
-> userMsg
|
||||
, apiRoutes : List (ApiRoute.Done ApiRoute.Response)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user