forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseRepoMeta.ts
More file actions
185 lines (153 loc) · 4.51 KB
/
useRepoMeta.ts
File metadata and controls
185 lines (153 loc) · 4.51 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
type ProviderId = 'github' // Could be extended to support other providers (gitlab, codeforge, tangled...)
export type RepoRef = { provider: ProviderId; owner: string; repo: string }
export type RepoMetaLinks = {
repo: string
stars: string
forks: string
watchers?: string
}
export type RepoMeta = {
provider: ProviderId
url: string
stars: number
forks: number
watchers?: number
description?: string | null
defaultBranch?: string
links: RepoMetaLinks
}
type UnghRepoResponse = {
repo: {
description?: string | null
stars?: number
forks?: number
watchers?: number
defaultBranch?: string
} | null
}
function normalizeInputToUrl(input: string): string | null {
const raw = input.trim()
if (!raw) return null
const normalized = raw.replace(/^git\+/, '')
if (!/^https?:\/\//i.test(normalized)) {
const scp = normalized.match(/^(?:git@)?([^:/]+):(.+)$/i)
if (scp?.[1] && scp?.[2]) {
const host = scp[1]
const path = scp[2].replace(/^\/*/, '')
return `https://${host}/${path}`
}
}
return normalized
}
type ProviderAdapter = {
id: ProviderId
parse(url: URL): RepoRef | null
links(ref: RepoRef): RepoMetaLinks
fetchMeta(ref: RepoRef, links: RepoMetaLinks): Promise<RepoMeta | null>
}
const githubAdapter: ProviderAdapter = {
id: 'github',
parse(url) {
const host = url.hostname.toLowerCase()
if (host !== 'github.com' && host !== 'www.github.com') return null
const parts = url.pathname.split('/').filter(Boolean)
if (parts.length < 2) return null
const owner = decodeURIComponent(parts[0] ?? '').trim()
const repo = decodeURIComponent(parts[1] ?? '')
.trim()
.replace(/\.git$/i, '')
if (!owner || !repo) return null
return { provider: 'github', owner, repo }
},
links(ref) {
const base = `https://github.com/${ref.owner}/${ref.repo}`
return {
repo: base,
stars: `${base}/stargazers`,
forks: `${base}/forks`,
watchers: `${base}/watchers`,
}
},
async fetchMeta(ref, links) {
// Using UNGH to avoid API limitations of the Github API
const res = await $fetch<UnghRepoResponse>(`https://ungh.cc/repos/${ref.owner}/${ref.repo}`, {
headers: { 'User-Agent': 'npmx' },
}).catch(() => null)
const repo = res?.repo
if (!repo) return null
return {
provider: 'github',
url: links.repo,
stars: repo.stars ?? 0,
forks: repo.forks ?? 0,
watchers: repo.watchers ?? 0,
description: repo.description ?? null,
defaultBranch: repo.defaultBranch,
links,
}
},
}
const providers: readonly ProviderAdapter[] = [githubAdapter] as const
function parseRepoFromUrl(input: string): RepoRef | null {
const normalized = normalizeInputToUrl(input)
if (!normalized) return null
try {
const url = new URL(normalized)
for (const provider of providers) {
const ref = provider.parse(url)
if (ref) return ref
}
return null
} catch {
return null
}
}
async function fetchRepoMeta(ref: RepoRef): Promise<RepoMeta | null> {
const adapter = providers.find(provider => provider.id === ref.provider)
if (!adapter) return null
const links = adapter.links(ref)
return await adapter.fetchMeta(ref, links)
}
export function useRepoMeta(repositoryUrl: MaybeRefOrGetter<string | null | undefined>) {
const repoRef = computed(() => {
const url = toValue(repositoryUrl)
if (!url) return null
return parseRepoFromUrl(url)
})
const requestKey = computed(() => {
const ref = repoRef.value
if (!ref) return 'repo-meta:none'
return `repo-meta:${ref.provider}:${ref.owner}/${ref.repo}`
})
const { data, pending, error, refresh } = useLazyAsyncData<RepoMeta | null>(
requestKey,
async () => {
const ref = repoRef.value
if (!ref) return null
return await fetchRepoMeta(ref)
},
{ default: () => null },
)
watch(
repoRef,
ref => {
if (ref) refresh()
},
{ immediate: true },
)
const meta = computed<RepoMeta | null>(() => data.value ?? null)
return {
repoRef,
meta,
stars: computed(() => meta.value?.stars ?? 0),
forks: computed(() => meta.value?.forks ?? 0),
watchers: computed(() => meta.value?.watchers ?? 0),
starsLink: computed(() => meta.value?.links.stars ?? null),
forksLink: computed(() => meta.value?.links.forks ?? null),
watchersLink: computed(() => meta.value?.links.watchers ?? null),
repoLink: computed(() => meta.value?.links.repo ?? null),
pending,
error,
refresh,
}
}