github.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { query } from "@solidjs/router"
  2. export const github = query(async () => {
  3. "use server"
  4. const headers = {
  5. "User-Agent":
  6. "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",
  7. }
  8. try {
  9. const [meta, releases, contributors] = await Promise.all([
  10. fetch("https://api.github.com/repos/sst/opencode", { headers }).then(async (res) => {
  11. const text = await res.text()
  12. console.log(text)
  13. const json = JSON.parse(text)
  14. return json
  15. }),
  16. fetch("https://api.github.com/repos/sst/opencode/releases", { headers }).then(async (res) => {
  17. const text = await res.text()
  18. console.log(text)
  19. const json = JSON.parse(text)
  20. return json
  21. }),
  22. fetch("https://api.github.com/repos/sst/opencode/contributors?per_page=1", { headers }),
  23. ])
  24. const [release] = releases
  25. const contributorCount = Number.parseInt(
  26. contributors.headers
  27. .get("Link")!
  28. .match(/&page=(\d+)>; rel="last"/)!
  29. .at(1)!,
  30. )
  31. return {
  32. stars: meta.stargazers_count,
  33. release: {
  34. name: release.name,
  35. url: release.html_url,
  36. },
  37. contributors: contributorCount,
  38. }
  39. } catch (e) {
  40. console.error(e)
  41. }
  42. return undefined
  43. }, "github")