mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2025-01-06 14:26:35 +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 {
|
return {
|
||||||
mainModule: `port module TemplateModulesBeta exposing (..)
|
mainModule: `port module TemplateModulesBeta exposing (..)
|
||||||
|
|
||||||
|
import Api
|
||||||
import Browser.Navigation
|
import Browser.Navigation
|
||||||
import Route exposing (Route)
|
import Route exposing (Route)
|
||||||
import Document
|
import Document
|
||||||
@ -396,6 +397,7 @@ main =
|
|||||||
, fromJsPort = fromJsPort identity
|
, fromJsPort = fromJsPort identity
|
||||||
, data = dataForRoute
|
, data = dataForRoute
|
||||||
, sharedData = Shared.template.data
|
, sharedData = Shared.template.data
|
||||||
|
, apiRoutes = Api.routes
|
||||||
}
|
}
|
||||||
|
|
||||||
dataForRoute : Maybe Route -> DataSource PageData
|
dataForRoute : Maybe Route -> DataSource PageData
|
||||||
|
@ -41,6 +41,7 @@ init :
|
|||||||
, site : SiteConfig route siteData
|
, site : SiteConfig route siteData
|
||||||
, data : route -> DataSource.DataSource pageData
|
, data : route -> DataSource.DataSource pageData
|
||||||
, sharedData : DataSource.DataSource sharedData
|
, sharedData : DataSource.DataSource sharedData
|
||||||
|
, apiRoutes : List (ApiRoute.Done ApiRoute.Response)
|
||||||
}
|
}
|
||||||
-> StaticResponses
|
-> StaticResponses
|
||||||
init config =
|
init config =
|
||||||
@ -61,42 +62,35 @@ init config =
|
|||||||
|
|
||||||
buildTimeFilesRequest :
|
buildTimeFilesRequest :
|
||||||
{ config
|
{ config
|
||||||
| getStaticRoutes : DataSource (List route)
|
| apiRoutes : List (ApiRoute.Done ApiRoute.Response)
|
||||||
, site : SiteConfig route siteData
|
|
||||||
}
|
}
|
||||||
-> DataSource (List (Result String { path : List String, content : String }))
|
-> DataSource (List (Result String { path : List String, content : String }))
|
||||||
buildTimeFilesRequest config =
|
buildTimeFilesRequest config =
|
||||||
config.getStaticRoutes
|
config.apiRoutes
|
||||||
|> DataSource.andThen
|
|> List.map
|
||||||
(\allRoutes ->
|
(\handler ->
|
||||||
config.site
|
handler.buildTimeRoutes
|
||||||
allRoutes
|
|> DataSource.andThen
|
||||||
|> .apiRoutes
|
(\paths ->
|
||||||
|> List.map
|
paths
|
||||||
(\handler ->
|
|> List.map
|
||||||
handler.buildTimeRoutes
|
(\path ->
|
||||||
|> DataSource.andThen
|
handler.matchesToResponse path
|
||||||
(\paths ->
|
|> DataSource.map
|
||||||
paths
|
(\maybeResponse ->
|
||||||
|> List.map
|
case maybeResponse of
|
||||||
(\path ->
|
Nothing ->
|
||||||
handler.matchesToResponse path
|
Err ""
|
||||||
|> DataSource.map
|
|
||||||
(\maybeResponse ->
|
|
||||||
case maybeResponse of
|
|
||||||
Nothing ->
|
|
||||||
Err ""
|
|
||||||
|
|
||||||
Just response ->
|
Just response ->
|
||||||
Ok { path = path |> String.split "/", content = response.body }
|
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 :
|
renderSingleRoute :
|
||||||
@ -204,6 +198,7 @@ nextStep :
|
|||||||
, data : route -> DataSource.DataSource pageData
|
, data : route -> DataSource.DataSource pageData
|
||||||
, sharedData : DataSource.DataSource sharedData
|
, sharedData : DataSource.DataSource sharedData
|
||||||
, site : SiteConfig route siteData
|
, site : SiteConfig route siteData
|
||||||
|
, apiRoutes : List (ApiRoute.Done ApiRoute.Response)
|
||||||
}
|
}
|
||||||
->
|
->
|
||||||
{ model
|
{ model
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
module Pages.ProgramConfig exposing (..)
|
module Pages.ProgramConfig exposing (..)
|
||||||
|
|
||||||
|
import ApiRoute
|
||||||
import Browser.Navigation
|
import Browser.Navigation
|
||||||
import DataSource
|
import DataSource
|
||||||
import Head
|
import Head
|
||||||
@ -54,4 +55,5 @@ type alias ProgramConfig userMsg userModel route siteData pageData sharedData =
|
|||||||
, metadata : route
|
, metadata : route
|
||||||
}
|
}
|
||||||
-> userMsg
|
-> userMsg
|
||||||
|
, apiRoutes : List (ApiRoute.Done ApiRoute.Response)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user