github.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. if (!Array.isArray(releases) || releases.length === 0) {
  17. return undefined
  18. }
  19. const [release] = releases
  20. const linkHeader = contributors.headers.get("Link")
  21. const contributorCount = linkHeader
  22. ? Number.parseInt(linkHeader.match(/&page=(\d+)>; rel="last"/)?.at(1) ?? "0")
  23. : 0
  24. return {
  25. stars: meta.stargazers_count,
  26. release: {
  27. name: release.name,
  28. url: release.html_url,
  29. tag_name: release.tag_name,
  30. },
  31. contributors: contributorCount,
  32. }
  33. } catch (e) {
  34. console.error(e)
  35. }
  36. return undefined
  37. }, "github")