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

161 lines
4.9 KiB
Elm
Raw Normal View History

module Api exposing (routes)
import ApiRoute
2021-05-22 21:53:14 +03:00
import Article
import DataSource exposing (DataSource)
import DataSource.Http
import Head
import Html exposing (Html)
import Json.Decode as Decode
import Json.Encode
import Manifest
2021-05-22 21:53:14 +03:00
import Pages
import Pages.Manifest
import Route exposing (Route)
2021-05-22 21:53:14 +03:00
import Rss
import Site
2021-05-22 21:53:14 +03:00
import SiteOld
import Sitemap
2021-05-22 21:53:14 +03:00
import Time
routes :
DataSource (List Route)
-> (Html Never -> String)
2021-07-31 01:35:02 +03:00
-> List (ApiRoute.ApiRoute ApiRoute.Response)
routes getStaticRoutes htmlToString =
[ ApiRoute.succeed
(\userId ->
DataSource.succeed
(Json.Encode.object
[ ( "id", Json.Encode.string userId )
, ( "name"
, Html.p [] [ Html.text <| "Data for user " ++ userId ]
|> htmlToString
|> Json.Encode.string
)
]
|> Json.Encode.encode 2
)
)
|> ApiRoute.literal "users"
|> ApiRoute.slash
|> ApiRoute.capture
|> ApiRoute.literal ".json"
|> ApiRoute.preRender
(\route ->
DataSource.succeed
[ route "1"
, route "2"
, route "3"
]
)
, ApiRoute.succeed
(\repoName ->
DataSource.Http.get
2022-01-28 02:45:51 +03:00
("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"
|> ApiRoute.preRender
(\route ->
DataSource.succeed
[ route "elm-graphql"
]
)
2021-05-22 21:53:14 +03:00
, rss
{ siteTagline = SiteOld.tagline
, siteUrl = SiteOld.canonicalUrl
, title = "elm-pages Blog"
, builtAt = Pages.builtAt
, indexPage = [ "blog" ]
}
postsDataSource
, ApiRoute.succeed
(getStaticRoutes
|> DataSource.map
(\allRoutes ->
allRoutes
|> List.map
(\route ->
{ path = Route.routeToPath route |> String.join "/"
, lastMod = Nothing
}
)
|> Sitemap.build { siteUrl = "https://elm-pages.com" }
)
)
|> ApiRoute.literal "sitemap.xml"
2021-06-24 20:05:16 +03:00
|> ApiRoute.single
|> ApiRoute.withGlobalHeadTags (DataSource.succeed [ Head.sitemapLink "/sitemap.xml" ])
, Pages.Manifest.generator Site.canonicalUrl Manifest.config
]
2021-05-22 21:53:14 +03:00
postsDataSource : DataSource.DataSource (List Rss.Item)
postsDataSource =
Article.allMetadata
|> DataSource.map
(List.map
(\( route, article ) ->
{ title = article.title
, description = article.description
, url =
2021-05-23 20:20:09 +03:00
route
2021-05-22 21:53:14 +03:00
|> Route.routeToPath
|> String.join "/"
, categories = []
, author = "Dillon Kearns"
, pubDate = Rss.Date article.published
, content = Nothing
2021-06-05 05:33:06 +03:00
, contentEncoded = Nothing
, enclosure = Nothing
2021-05-22 21:53:14 +03:00
}
)
)
rss :
{ siteTagline : String
, siteUrl : String
, title : String
, builtAt : Time.Posix
, indexPage : List String
}
-> DataSource.DataSource (List Rss.Item)
2021-07-31 01:35:02 +03:00
-> ApiRoute.ApiRoute ApiRoute.Response
2021-05-22 21:53:14 +03:00
rss options itemsRequest =
ApiRoute.succeed
(itemsRequest
|> DataSource.map
(\items ->
Rss.generate
{ title = options.title
, description = options.siteTagline
, url = options.siteUrl ++ "/" ++ String.join "/" options.indexPage
, lastBuildTime = options.builtAt
, generator = Just "elm-pages"
, items = items
, siteUrl = options.siteUrl
}
2021-05-22 21:53:14 +03:00
)
)
|> ApiRoute.literal "blog/feed.xml"
2021-06-24 20:05:16 +03:00
|> ApiRoute.single
|> ApiRoute.withGlobalHeadTags
(DataSource.succeed
[ Head.rssLink "/blog/feed.xml"
]
)