1
- import axios from " axios" ;
2
- import { GithubAuthConfig } from " @/github/config" ;
1
+ import axios from ' axios'
2
+ import { GithubAuthConfig } from ' @/github/config'
3
3
4
4
interface RepoResponse {
5
- owner : {
6
- login : string ;
7
- } ;
8
- name : string ;
9
- html_url : string ;
10
- topics : string [ ] ;
11
- language : string ;
12
- description : string ;
13
- stargazers_count : number ;
14
- forks_count : number ;
15
- default_branch : string ;
16
- pushed_at : Date ;
5
+ owner : {
6
+ login : string
7
+ }
8
+ name : string
9
+ html_url : string
10
+ topics : string [ ]
11
+ language : string
12
+ description : string
13
+ stargazers_count : number
14
+ forks_count : number
15
+ default_branch : string
16
+ pushed_at : Date
17
17
}
18
18
19
19
interface TreeResponse {
20
- sha : string ;
21
- tree : TreeItem [ ] ;
20
+ sha : string
21
+ tree : TreeItem [ ]
22
22
}
23
23
24
24
interface TreeItem {
25
- path : string ;
26
- type : 'file' | 'dir' ;
27
- sha : string ;
28
- url : string ;
25
+ path : string
26
+ type : 'file' | 'dir'
27
+ sha : string
28
+ url : string
29
29
}
30
30
31
31
interface RepoDetails {
32
- repoOwner : string ;
33
- repoName : string ;
34
- url : string ;
35
- topics : string [ ] ;
36
- language : string ;
37
- description : string ;
38
- stars : number ;
39
- forks : number ;
40
- defaultBranch : string ;
41
- sha : string ;
42
- commitAt : Date ;
32
+ repoOwner : string
33
+ repoName : string
34
+ url : string
35
+ topics : string [ ]
36
+ language : string
37
+ description : string
38
+ stars : number
39
+ forks : number
40
+ defaultBranch : string
41
+ sha : string
42
+ commitAt : Date
43
43
}
44
44
45
45
export interface RepoTreeResult {
46
- path : string ;
47
- files : string [ ] ;
48
- subdirectories : RepoTreeResult [ ] ;
46
+ path : string
47
+ files : string [ ]
48
+ subdirectories : RepoTreeResult [ ]
49
49
}
50
50
51
51
/**
52
52
* Fetches details about a GitHub repository
53
53
* @param {string } owner - The repository owner
54
54
* @param {string } repo - The repository name
55
+ * @param {boolean } [useAuth=false] - Whether to use authentication for the request (for github rate limiting)
55
56
* @returns {Promise<{language: string, description: string, stars: number, forks: number, url: string, topics: string[], repo_owner: string, repo_name: string, default_branch: string, sha: string}> }
56
57
*/
57
58
export async function fetchGithubRepoDetails (
58
- owner : string ,
59
- repo : string
59
+ owner : string ,
60
+ repo : string ,
61
+ useAuth : boolean = false
60
62
) : Promise < RepoDetails > {
61
- const repoUrl = `https://api.github.com/repos/${ owner } /${ repo } ` ;
62
-
63
- try {
64
- const repoResp = await axios . get < RepoResponse > ( repoUrl , GithubAuthConfig ) ;
65
- const repoData = repoResp . data ;
66
-
67
- const treeUrl = `https://api.github.com/repos/${ owner } /${ repo } /git/trees/${ repoData . default_branch } ` ;
68
- const treeResp = await axios . get < TreeResponse > ( treeUrl , GithubAuthConfig ) ;
69
- const treeData = treeResp . data ;
70
-
71
- return {
72
- repoOwner : repoData . owner . login ,
73
- repoName : repoData . name ,
74
- url : repoData . html_url ,
75
- topics : repoData . topics ,
76
- language : repoData . language ,
77
- description : repoData . description ,
78
- stars : repoData . stargazers_count ,
79
- forks : repoData . forks_count ,
80
- defaultBranch : repoData . default_branch ,
81
- sha : treeData . sha ,
82
- commitAt : repoData . pushed_at
83
- } ;
84
- } catch ( error ) {
85
- if ( axios . isAxiosError ( error ) ) {
86
- throw new Error ( `GitHub API Error: ${ error . response ?. data ?. message || error . message } ` ) ;
63
+ const repoUrl = `https://api.github.com/repos/${ owner } /${ repo } `
64
+
65
+ try {
66
+ const repoResp = await axios . get < RepoResponse > (
67
+ repoUrl ,
68
+ useAuth ? GithubAuthConfig : undefined
69
+ )
70
+ const repoData = repoResp . data
71
+
72
+ const treeUrl = `https://api.github.com/repos/${ owner } /${ repo } /git/trees/${ repoData . default_branch } `
73
+ const treeResp = await axios . get < TreeResponse > (
74
+ treeUrl ,
75
+ useAuth ? GithubAuthConfig : undefined
76
+ )
77
+ const treeData = treeResp . data
78
+
79
+ return {
80
+ repoOwner : repoData . owner . login ,
81
+ repoName : repoData . name ,
82
+ url : repoData . html_url ,
83
+ topics : repoData . topics ,
84
+ language : repoData . language ,
85
+ description : repoData . description ,
86
+ stars : repoData . stargazers_count ,
87
+ forks : repoData . forks_count ,
88
+ defaultBranch : repoData . default_branch ,
89
+ sha : treeData . sha ,
90
+ commitAt : repoData . pushed_at ,
91
+ }
92
+ } catch ( error ) {
93
+ if ( axios . isAxiosError ( error ) ) {
94
+ throw new Error (
95
+ `GitHub API Error: ${
96
+ error . response ?. data ?. message || error . message
97
+ } `
98
+ )
99
+ }
100
+ throw error
87
101
}
88
- throw error ;
89
- }
90
102
}
91
103
92
104
/**
@@ -95,40 +107,54 @@ export async function fetchGithubRepoDetails(
95
107
* @param {string } repo - The repository name
96
108
* @param {string } sha - The commit sha of the repository
97
109
* @param {string } [path=''] - Optional path within repository
110
+ * @param {boolean } [useAuth=false] - Whether to use authentication for the request (for github rate limiting)
98
111
* @returns {Promise<{path: string, files: Array<string>, subdirectories: Array}> }
99
112
*/
100
113
export async function fetchGithubRepoTree (
101
- owner : string ,
102
- repo : string ,
103
- sha : string ,
104
- path : string = ''
114
+ owner : string ,
115
+ repo : string ,
116
+ sha : string ,
117
+ path : string = '' ,
118
+ useAuth : boolean = false
105
119
) : Promise < RepoTreeResult > {
106
- const contentsUrl = `https://api.github.com/repos/${ owner } /${ repo } /contents/${ path } ?ref=${ sha } ` ;
107
-
108
- try {
109
- const { data } = await axios . get < TreeItem [ ] > ( contentsUrl , GithubAuthConfig ) ;
110
- const result : RepoTreeResult = {
111
- path,
112
- files : [ ] ,
113
- subdirectories : [ ]
114
- } ;
115
-
116
- for ( const item of data ) {
117
- if ( item . type === 'file' ) {
118
- result . files . push ( item . path ) ;
119
- } else if ( item . type === 'dir' ) {
120
- const subDir = await fetchGithubRepoTree ( owner , repo , sha , item . path ) ;
121
- result . subdirectories . push ( subDir ) ;
122
- }
123
- }
124
-
125
- return result ;
126
- } catch ( error ) {
127
- if ( axios . isAxiosError ( error ) ) {
128
- throw new Error ( `Failed to fetch repo contents: ${ error . response ?. data ?. message || error . message } ` ) ;
120
+ const contentsUrl = `https://api.github.com/repos/${ owner } /${ repo } /contents/${ path } ?ref=${ sha } `
121
+
122
+ try {
123
+ const { data } = await axios . get < TreeItem [ ] > (
124
+ contentsUrl ,
125
+ useAuth ? GithubAuthConfig : undefined
126
+ )
127
+ const result : RepoTreeResult = {
128
+ path,
129
+ files : [ ] ,
130
+ subdirectories : [ ] ,
131
+ }
132
+
133
+ for ( const item of data ) {
134
+ if ( item . type === 'file' ) {
135
+ result . files . push ( item . path )
136
+ } else if ( item . type === 'dir' ) {
137
+ const subDir = await fetchGithubRepoTree (
138
+ owner ,
139
+ repo ,
140
+ sha ,
141
+ item . path
142
+ )
143
+ result . subdirectories . push ( subDir )
144
+ }
145
+ }
146
+
147
+ return result
148
+ } catch ( error ) {
149
+ if ( axios . isAxiosError ( error ) ) {
150
+ throw new Error (
151
+ `Failed to fetch repo contents: ${
152
+ error . response ?. data ?. message || error . message
153
+ } `
154
+ )
155
+ }
156
+ throw error
129
157
}
130
- throw error ;
131
- }
132
158
}
133
159
134
160
/**
@@ -137,24 +163,32 @@ export async function fetchGithubRepoTree(
137
163
* @param {string } repo - The repository name
138
164
* @param {string } sha - The commit sha of the file to fetch
139
165
* @param {string } path - The path of the file to fetch
166
+ * @param {boolean } [useAuth=false] - Whether to use authentication for the request (for github rate limiting)
140
167
* @returns {Promise<String> }
141
168
*/
142
169
export async function fetchGithubRepoFile (
143
- owner : string ,
144
- repo : string ,
145
- sha : string ,
146
- path : string
170
+ owner : string ,
171
+ repo : string ,
172
+ sha : string ,
173
+ path : string ,
174
+ useAuth : boolean = false
147
175
) : Promise < string > {
148
- const codeUrl = `https://raw.githubusercontent.com/${ owner } /${ repo } /${ sha } /${ path } ` ;
149
-
150
- try {
151
- const response = await axios . get < string > ( codeUrl , GithubAuthConfig ) ;
152
- return response . data ;
153
- } catch ( error ) {
154
- if ( axios . isAxiosError ( error ) ) {
155
- throw new Error ( `Failed to fetch file: ${ error . response ?. data ?. message || error . message } ` ) ;
176
+ const codeUrl = `https://raw.githubusercontent.com/${ owner } /${ repo } /${ sha } /${ path } `
177
+
178
+ try {
179
+ const response = await axios . get < string > (
180
+ codeUrl ,
181
+ useAuth ? GithubAuthConfig : undefined
182
+ )
183
+ return response . data
184
+ } catch ( error ) {
185
+ if ( axios . isAxiosError ( error ) ) {
186
+ throw new Error (
187
+ `Failed to fetch file: ${
188
+ error . response ?. data ?. message || error . message
189
+ } `
190
+ )
191
+ }
192
+ throw error
156
193
}
157
- throw error ;
158
- }
159
194
}
160
-
0 commit comments