github.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  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. },
  29. contributors: contributorCount,
  30. }
  31. } catch (e) {
  32. console.error(e)
  33. }
  34. return undefined
  35. }, "github")