Skip to content

Commit cb93d64

Browse files
refactor: move duplicated repositoryUrl in composable
1 parent 8976969 commit cb93d64

3 files changed

Lines changed: 43 additions & 32 deletions

File tree

app/components/OgImage/Package.vue

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script setup lang="ts">
2-
import { joinURL } from 'ufo'
3-
42
const props = withDefaults(
53
defineProps<{
64
name: string
@@ -38,16 +36,7 @@ const { data: pkg, refresh: refreshPkg } = usePackage(
3836
)
3937
const displayVersion = computed(() => pkg.value?.requestedVersion ?? null)
4038
41-
const repositoryUrl = computed(() => {
42-
const repo = displayVersion.value?.repository
43-
if (!repo?.url) return null
44-
let url = normalizeGitUrl(repo.url)
45-
// append `repository.directory` for monorepo packages
46-
if (repo.directory) {
47-
url = joinURL(`${url}/tree/HEAD`, repo.directory)
48-
}
49-
return url
50-
})
39+
const { repositoryUrl } = useRepositoryUrl(displayVersion)
5140
5241
const { data: likes, refresh: refreshLikes } = useFetch(() => `/api/social/likes/${name.value}`, {
5342
default: () => ({ totalLikes: 0, userHasLiked: false }),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { joinURL } from 'ufo'
2+
3+
function normalizeGitUrl(url: string): string {
4+
return url
5+
.replace(/^git\+/, '')
6+
.replace(/^git:\/\//, 'https://')
7+
.replace(/\.git$/, '')
8+
.replace(/^ssh:\/\/git@github\.com/, 'https://github.com')
9+
.replace(/^git@github\.com:/, 'https://github.com/')
10+
}
11+
12+
type RequestedVersion = SlimPackument['requestedVersion'] | null
13+
14+
type UseRepositoryUrlReturn = {
15+
repositoryUrl: ComputedRef<string | null>
16+
}
17+
18+
export function useRepositoryUrl(
19+
requestedVersion: MaybeRefOrGetter<RequestedVersion>,
20+
): UseRepositoryUrlReturn {
21+
const repositoryUrl = computed<string | null>(() => {
22+
const repo = toValue(requestedVersion)?.repository
23+
24+
if (!repo?.url) {
25+
return null
26+
}
27+
28+
let url = normalizeGitUrl(repo.url)
29+
30+
// append `repository.directory` for monorepo packages
31+
if (repo.directory) {
32+
url = joinURL(`${url}/tree/HEAD`, repo.directory)
33+
}
34+
35+
return url
36+
})
37+
38+
return {
39+
repositoryUrl,
40+
}
41+
}

app/pages/package/[[org]]/[name].vue

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {
1212
import type { JsrPackageInfo } from '#shared/types/jsr'
1313
import type { IconClass } from '~/types'
1414
import { assertValidPackageName } from '#shared/utils/npm'
15-
import { joinURL } from 'ufo'
1615
import { areUrlsEquivalent } from '#shared/utils/url'
1716
import { getDependencyCount } from '~/utils/npm/dependency-count'
1817
import { detectPublishSecurityDowngradeForVersion } from '~/utils/publish-security'
@@ -399,16 +398,7 @@ const totalDepsCount = computed(() => {
399398
return null
400399
})
401400
402-
const repositoryUrl = computed(() => {
403-
const repo = displayVersion.value?.repository
404-
if (!repo?.url) return null
405-
let url = normalizeGitUrl(repo.url)
406-
// append `repository.directory` for monorepo packages
407-
if (repo.directory) {
408-
url = joinURL(`${url}/tree/HEAD`, repo.directory)
409-
}
410-
return url
411-
})
401+
const { repositoryUrl } = useRepositoryUrl(displayVersion)
412402
413403
const { meta: repoMeta, repoRef, stars, starsLink, forks, forksLink } = useRepoMeta(repositoryUrl)
414404
@@ -454,15 +444,6 @@ const fundingUrl = computed(() => {
454444
return typeof funding === 'string' ? funding : funding.url
455445
})
456446
457-
function normalizeGitUrl(url: string): string {
458-
return url
459-
.replace(/^git\+/, '')
460-
.replace(/^git:\/\//, 'https://')
461-
.replace(/\.git$/, '')
462-
.replace(/^ssh:\/\/git@github\.com/, 'https://github.com')
463-
.replace(/^git@github\.com:/, 'https://github.com/')
464-
}
465-
466447
// Check if a version has provenance/attestations
467448
// The dist object may have attestations that aren't in the base type
468449
function hasProvenance(version: PackumentVersion | null): boolean {

0 commit comments

Comments
 (0)