-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgithub.ts
145 lines (136 loc) · 3.39 KB
/
github.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { AIProvider } from '@/types/ai'
import { GitRepository, Tree } from '@/types/git'
import { getRepoNameAndOwnerFromUrl } from '@/utils/github'
export const getRepositoryStructure = async ({
owner,
repoName,
branch
}: {
owner: string
repoName: string
branch: string
}): Promise<{ data?: Tree[]; error: string | undefined }> => {
try {
const response = await fetch(
`api/github/structure?repo=${repoName}&owner=${owner}&branch=${branch}`
)
const repository = await response.json()
return repository
} catch (error) {
return { error: 'An error has ocurred' }
}
}
export const getFileContents = async ({
path,
owner,
repoName
}: {
path: string
owner: string
repoName: string
}) => {
try {
const response = await fetch(
`api/github/file-contents?owner=${owner}&repo=${repoName}&path=${path}`
)
const contents = await response.json()
return contents
} catch (error) {
return { error: 'An error has ocurred' }
}
}
export const getContributors = async ({
repoName,
owner,
page = 1
}: {
repoName: string
owner: string
page?: number
}) => {
try {
const response = await fetch(
`api/github/contributors?owner=${owner}&repo=${repoName}&page=${page}`
)
const contributors = await response.json()
return contributors.data
} catch (error) {
return
}
}
export const getLicense = async ({ repoName, owner }: { repoName: string; owner: string }) => {
try {
const response = await fetch(`api/github/license?owner=${owner}&repo=${repoName}`)
const license = await response.json()
return license
} catch (error) {
return { error: 'An error has ocurred', data: undefined }
}
}
export const getRepositoryData = async ({ urlRepository }: { urlRepository: string }) => {
try {
const { repoName, owner } = getRepoNameAndOwnerFromUrl({ urlRepository })
const response = await fetch(`api/github/repo-details?owner=${owner}&repo=${repoName}`)
const data = await response.json()
if (data.error) throw new Error(data.error)
return data.data as GitRepository
} catch (error) {
// console.error(error)
return
}
}
export const getGenerationAI = async ({
format,
prompt,
providerAISelected
}: {
format: 'json' | 'string'
prompt: string
providerAISelected: AIProvider
}) => {
try {
const request = await fetch('/api/ai', {
method: 'POST',
body: JSON.stringify({
format,
prompt,
providerAISelected
}),
headers: {
'Content-type': 'application/json'
}
})
const response = await request.json()
return response
} catch (error) {
return { error: 'An error has ocurred', data: undefined }
}
}
export const getLanguages = async ({ repoName, owner }: { repoName: string; owner: string }) => {
try {
const response = await fetch(`api/github/languages?owner=${owner}&repo=${repoName}`)
const languages = await response.json()
return languages
} catch (error) {
return { error: 'An error has ocurred', data: undefined }
}
}
export const getNestedPathsByDirectory = async ({
path,
owner,
repoName
}: {
path: string
owner: string
repoName: string
}) => {
try {
const response = await fetch(
`api/github/nested-paths?owner=${owner}&repo=${repoName}&path=${path}`
)
const contents = await response.json()
return contents.data
} catch (error) {
return
}
}