summaryrefslogtreecommitdiffhomepage
path: root/packages/console/app/src/lib/github.ts
blob: 80d3f308f32be7f4e4e8317af9d08ecacb449e34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { query } from "@solidjs/router"

export const github = query(async () => {
  "use server"
  const headers = {
    UserAgent:
      "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",
  }
  try {
    const [meta, releases, contributors] = await Promise.all([
      fetch("https://api.github.com/repos/sst/opencode", { headers }).then(async (res) => {
        const text = await res.text()
        console.log(text)
        const json = JSON.parse(text)
        return json
      }),
      fetch("https://api.github.com/repos/sst/opencode/releases", { headers }).then(async (res) => {
        const text = await res.text()
        console.log(text)
        const json = JSON.parse(text)
        return json
      }),
      fetch("https://api.github.com/repos/sst/opencode/contributors?per_page=1", { headers }),
    ])
    const [release] = releases
    const contributorCount = Number.parseInt(
      contributors.headers
        .get("Link")!
        .match(/&page=(\d+)>; rel="last"/)!
        .at(1)!,
    )
    return {
      stars: meta.stargazers_count,
      release: {
        name: release.name,
        url: release.html_url,
      },
      contributors: contributorCount,
    }
  } catch (e) {
    console.error(e)
  }
  return undefined
}, "github")