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

158 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 Html exposing (Html)
import Json.Encode
import OptimizedDecoder as Decode
2021-05-22 21:53:14 +03:00
import Pages
import Route exposing (Route)
2021-05-22 21:53:14 +03:00
import Rss
import Secrets
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)
-> List (ApiRoute.Done ApiRoute.Response)
routes getStaticRoutes htmlToString =
[ ApiRoute.succeed
(\userId ->
DataSource.succeed
{ body =
Json.Encode.object
[ ( "id", Json.Encode.int userId )
, ( "name"
, Html.p [] [ Html.text <| "Data for user " ++ String.fromInt userId ]
|> htmlToString
|> Json.Encode.string
)
]
|> 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"
]
)
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 ->
{ body =
allRoutes
|> List.map
(\route ->
2021-05-23 20:20:09 +03:00
{ 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
]
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)
-> ApiRoute.Done ApiRoute.Response
rss options itemsRequest =
ApiRoute.succeed
(itemsRequest
|> DataSource.map
(\items ->
{ body =
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
}
}
)
)
|> ApiRoute.literal "blog/feed.xml"
2021-06-24 20:05:16 +03:00
|> ApiRoute.single