Skip to content

Commit 793ee16

Browse files
committed
feat: fetch versions data
1 parent 2a4a4e8 commit 793ee16

2 files changed

Lines changed: 43 additions & 67 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { PackageVersionInfo } from '#shared/types'
2+
import { getVersions } from 'fast-npm-meta'
3+
import { compare } from 'semver'
4+
5+
export function usePackageVersionHistory(name: MaybeRefOrGetter<string>) {
6+
return useLazyAsyncData(
7+
() => `package-versions:${toValue(name)}`,
8+
async () => {
9+
const data = await getVersions(toValue(name), { metadata: true })
10+
11+
const versions: PackageVersionInfo[] = Object.entries(data.versionsMeta)
12+
.map(([version, meta]) => ({
13+
version,
14+
time: meta.time,
15+
hasProvenance: meta.provenance === 'trustedPublisher' || meta.provenance === true,
16+
deprecated: meta.deprecated,
17+
}))
18+
.sort((a, b) => compare(b.version, a.version))
19+
20+
return {
21+
distTags: data.distTags as Record<string, string>,
22+
versions,
23+
}
24+
},
25+
)
26+
}

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

Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script setup lang="ts">
2-
import type { PackageVersionInfo } from '#shared/types'
3-
import { compare } from 'semver'
42
import { buildVersionToTagsMap, buildTaggedVersionRows } from '~/utils/versions'
53
64
definePageMeta({
@@ -22,81 +20,33 @@ const orgName = computed(() => {
2220
return match ? match[1] : null
2321
})
2422
25-
// ─── Mock data ────────────────────────────────────────────────────────────────
26-
// TODO: Replace distTags with pkg['dist-tags'] from usePackage()
27-
// TODO: Replace versionHistory with data from useAllPackageVersions()
28-
// TODO: Replace mockChangelogs with pre-rendered HTML from the server
29-
// (GitHub releases body or CHANGELOG.md, parsed server-side like README)
23+
// ─── Data ─────────────────────────────────────────────────────────────────────
3024
31-
const distTags: Record<string, string> = {
32-
latest: '3.4.21',
33-
next: '3.5.0-beta.3',
34-
beta: '3.5.0-beta.3',
35-
rc: '3.5.0-rc.1',
36-
alpha: '3.5.0-alpha.5',
37-
csp: '3.4.21',
38-
legacy: '2.7.16',
39-
}
25+
const { data: versionHistoryData } = usePackageVersionHistory(packageName)
4026
41-
const versionHistory: PackageVersionInfo[] = [
42-
{ version: '3.5.0-beta.3', time: '2024-12-18T10:00:00Z', hasProvenance: true },
43-
{ version: '3.5.0-rc.1', time: '2024-12-10T10:00:00Z', hasProvenance: true },
44-
{ version: '3.5.0-alpha.5', time: '2024-11-28T10:00:00Z', hasProvenance: true },
45-
{ version: '3.5.0-alpha.4', time: '2024-11-10T10:00:00Z', hasProvenance: false },
46-
{ version: '3.5.0-alpha.3', time: '2024-10-22T10:00:00Z', hasProvenance: false },
47-
{ version: '3.4.21', time: '2024-12-05T10:00:00Z', hasProvenance: true },
48-
{ version: '3.4.20', time: '2024-11-20T10:00:00Z', hasProvenance: true },
49-
{ version: '3.4.19', time: '2024-11-08T10:00:00Z', hasProvenance: true },
50-
{ version: '3.4.18', time: '2024-10-25T10:00:00Z', hasProvenance: true },
51-
{ version: '3.4.17', time: '2024-10-01T10:00:00Z', hasProvenance: true },
52-
{ version: '3.4.0', time: '2024-02-15T10:00:00Z', hasProvenance: false },
53-
{ version: '3.3.13', time: '2024-01-10T10:00:00Z', hasProvenance: false },
54-
{ version: '3.3.0', time: '2023-05-11T10:00:00Z', hasProvenance: false },
55-
{ version: '3.2.47', time: '2023-03-30T10:00:00Z', hasProvenance: false },
56-
{ version: '3.0.0', time: '2022-09-29T10:00:00Z', hasProvenance: false },
57-
{ version: '2.7.16', time: '2023-12-08T10:00:00Z', hasProvenance: false },
58-
{ version: '2.7.15', time: '2023-09-12T10:00:00Z', hasProvenance: false },
59-
{ version: '2.7.14', time: '2023-06-01T10:00:00Z', hasProvenance: false },
60-
{ version: '2.7.0', time: '2022-07-01T10:00:00Z', hasProvenance: false },
61-
{ version: '2.6.14', time: '2022-03-14T10:00:00Z', hasProvenance: false },
62-
{
63-
version: '2.5.22',
64-
time: '2018-03-20T10:00:00Z',
65-
hasProvenance: false,
66-
deprecated: 'Use vue@2.6.x or later',
67-
},
68-
{
69-
version: '2.5.0',
70-
time: '2017-10-13T10:00:00Z',
71-
hasProvenance: false,
72-
deprecated: 'Use vue@2.6.x or later',
73-
},
74-
{ version: '1.0.28', time: '2016-12-15T10:00:00Z', hasProvenance: false },
75-
]
76-
77-
// TODO: Replace with pre-rendered HTML from the server
78-
const mockChangelogs: Record<string, string> = {
79-
'3.5.0-beta.3': 'Hello world',
80-
}
27+
// TODO: Replace mockChangelogs with pre-rendered HTML from the server
28+
// (GitHub releases body or CHANGELOG.md, parsed server-side like README)
29+
const mockChangelogs: Record<string, string> = {}
8130
8231
// ─── Derived data ─────────────────────────────────────────────────────────────
8332
84-
const versionToTagsMap = computed(() => buildVersionToTagsMap(distTags))
33+
const distTags = computed(() => versionHistoryData.value?.distTags ?? {})
34+
const versionHistory = computed(() => versionHistoryData.value?.versions ?? [])
35+
36+
const versionToTagsMap = computed(() => buildVersionToTagsMap(distTags.value))
8537
8638
const sortedVersions = computed(() =>
87-
[...versionHistory]
88-
.sort((a, b) => compare(b.version, a.version))
89-
.map(v => ({
90-
...v,
91-
tags: versionToTagsMap.value.get(v.version),
92-
hasChangelog: v.version in mockChangelogs,
93-
})),
39+
versionHistory.value.map(v => ({
40+
...v,
41+
tags: versionToTagsMap.value.get(v.version),
42+
hasChangelog: v.version in mockChangelogs,
43+
})),
9444
)
9545
96-
const tagRows = computed(() => buildTaggedVersionRows(distTags))
46+
const tagRows = computed(() => buildTaggedVersionRows(distTags.value))
9747
9848
function getVersionTime(version: string): string | undefined {
99-
return versionHistory.find(v => v.version === version)?.time
49+
return versionHistory.value.find(v => v.version === version)?.time
10050
}
10151
10252
// ─── Changelog side panel ─────────────────────────────────────────────────────
@@ -120,7 +70,7 @@ const jumpError = ref('')
12070
function navigateToVersion() {
12171
const v = jumpVersion.value.trim()
12272
if (!v) return
123-
if (!versionHistory.some(entry => entry.version === v)) {
73+
if (!versionHistory.value.some(entry => entry.version === v)) {
12474
jumpError.value = `"${v}" not found`
12575
return
12676
}

0 commit comments

Comments
 (0)