1export async function getViewerRepos(
2 viewer_token: string,
3 cursor: string | null = null,
4): Promise<{ data: ViewerRepos | null; error: BadDataGitHubError | null }> {
5
6 const query = `
7 query($first: Int!,$after: String) {
8 viewer {
9 repositories(
10 first: $first
11 after: $after
12 isFork: false
13 orderBy: {field: PUSHED_AT, direction: DESC}
14 ) {
15
16 edges {
17 node {
18 id
19 name
20 nameWithOwner
21 languages(first: 10) {
22 edges {
23 node {
24 id
25 name
26 color
27 }
28 }
29 }
30 }
31 }
32 totalCount
33 pageInfo {
34 endCursor
35 startCursor
36 }
37 }
38 }
39}
40`;
41 try {
42 const response = await fetch("https://api.github.com/graphql", {
43 method: "POST",
44 headers: {
45 "Authorization": `bearer ${viewer_token}`,
46 "Content-Type": "application/json",
47 "accept": "application/vnd.github.hawkgirl-preview+json",
48 },
49 body: JSON.stringify({
50 query,
51 variables: {
52 first: 50,
53 after: cursor,
54 },
55
56 }),
57 });
58 const data = await response.json() as unknown as ViewerRepos;
59
60 if ("message" in data) {
61 console.log("throw error fetching viewer repos ==> ", data);
62 return { data: null, error: data as unknown as BadDataGitHubError };
63 }
64
65 return { data, error: null };
66 } catch (err) {
67 console.log("catch error fetching viewer repos ==> ", err);
68 return { data: null, error: err as BadDataGitHubError };
69 }
70}
71
72export interface ViewerRepos {
73 data: Data;
74}
75
76export interface Data {
77 viewer: Viewer;
78}
79
80export interface Viewer {
81 repositories: Repositories;
82}
83
84export interface Repositories {
85 edges: Edge[];
86 totalCount: number;
87 pageInfo: PageInfo;
88}
89
90export interface Edge {
91 cursor: string;
92 node: Node;
93}
94
95export interface Node {
96 id: string;
97 name: string;
98 nameWithOwner: string;
99 languages: Languages;
100}
101
102export interface Languages {
103 edges: LanguageEdge[];
104}
105
106export interface LanguageEdge {
107 node: LanguageNode;
108}
109
110export interface LanguageNode {
111 id: string;
112 name: string;
113 color: string;
114}
115
116export interface PageInfo {
117 endCursor: string;
118 startCursor: string;
119}
120
121export interface BadDataGitHubError {
122 message: string;
123 documentation_url: string;
124}
125