Socialify

Folder ..

Viewing repoQuery.ts
84 lines (80 loc) • 1.9 KB

 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { HOST_PREFIX } from '../helpers'

export const getRepoDetails = async (owner: string, name: string) => {
  const body = {
    query: `
      query repoQuery($_owner: String!, $_name: String!) {
        repository(owner: $_owner, name: $_name) {
          forkCount
          description
          createdAt
          name
          stargazerCount
          issues(states: OPEN) {
            totalCount
          }
          languages(first: 2, orderBy: { field: SIZE, direction: DESC }) {
            totalCount
            nodes {
              name
              color
            }
          }
          pullRequests(states: OPEN) {
            totalCount
          }
          releases(last: 1) {
            nodes {
              tagName
            }
          }
          owner {
            login
          }
        }
      }
    `,
    variables: {
      _owner: owner,
      _name: name
    }
  }

  const res = await fetch(`${HOST_PREFIX}/graphql`, {
    method: 'POST',
    headers: {
      'content-type': 'application/json'
    },
    body: JSON.stringify(body)
  })

  const json = await res.json()
  return json.data as RepoQueryResponse
}

export type RepoQueryResponse = {
  readonly repository: {
    readonly forkCount: number
    readonly description: string | null
    readonly createdAt: unknown
    readonly name: string
    readonly stargazerCount: number
    readonly issues: {
      readonly totalCount: number
    }
    readonly languages: {
      readonly totalCount: number
      readonly nodes: ReadonlyArray<{
        readonly name: string
        readonly color: string | null
      } | null> | null
    } | null
    readonly pullRequests: {
      readonly totalCount: number
    }
    readonly releases: {
      readonly nodes: ReadonlyArray<{
        readonly tagName: string
      } | null> | null
    }
    readonly owner: {
      readonly login: string
    }
  } | null
}