github.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { query } from "@solidjs/router"
  2. import { config } from "~/config"
  3. export const github = query(async () => {
  4. "use server"
  5. const headers = {
  6. "User-Agent":
  7. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
  8. }
  9. const apiBaseUrl = config.github.repoUrl.replace("https://github.com/", "https://api.github.com/repos/")
  10. try {
  11. const [meta, releases, contributors] = await Promise.all([
  12. fetch(apiBaseUrl, { headers }).then((res) => res.json()),
  13. fetch(`${apiBaseUrl}/releases`, { headers }).then((res) => res.json()),
  14. fetch(`${apiBaseUrl}/contributors?per_page=1`, { headers }),
  15. ])
  16. const [release] = releases
  17. const contributorCount = Number.parseInt(
  18. contributors.headers
  19. .get("Link")!
  20. .match(/&page=(\d+)>; rel="last"/)!
  21. .at(1)!,
  22. )
  23. return {
  24. stars: meta.stargazers_count,
  25. release: {
  26. name: release.name,
  27. url: release.html_url,
  28. tag_name: release.tag_name,
  29. },
  30. contributors: contributorCount,
  31. }
  32. } catch (e) {
  33. console.error(e)
  34. }
  35. return undefined
  36. }, "github")