-
-
Notifications
You must be signed in to change notification settings - Fork 424
feat: add github stars and forks #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8fad0b6
feat: add github stars and forks counts, position npm to top right
graphieros 1a205d0
Merge branch 'main' of https://github.com/graphieros/npmx.dev
graphieros 2ea3707
chore: remove unused file and other fixes
graphieros 2d56a50
merge: resolve conflicts with main
danielroe 8507bf0
fix: small niggles
danielroe 00171b3
refactor: simplify slightly
danielroe dbed4e6
fix: use npm logo icon, hide label on mobile
danielroe 6f812b5
refactor: simplify version display with muted text instead of badge
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this should link to
starsLinkrather than torepositoryUrlThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this replaces the previous repo link (in interests of saving space)