Skip to content

Commit 8f9ebba

Browse files
danielroetaskylizard
authored andcommitted
fix: use fast-npm-meta to get latest versions (#664)
1 parent 955cb1b commit 8f9ebba

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

app/pages/package-docs/[...path].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ if (import.meta.server && !requestedVersion.value && packageName.value) {
5151
navigateTo(
5252
{ name: 'docs', params: { path: pathSegments as [string, ...string[]] } },
5353
{ redirectCode: 302 },
54-
),
54+
)
5555
)
5656
}
5757
}

server/utils/npm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Packument, NpmSearchResponse } from '#shared/types'
22
import { encodePackageName, fetchLatestVersion } from '#shared/utils/npm'
33
import { maxSatisfying, prerelease } from 'semver'
4-
import { CACHE_MAX_AGE_FIVE_MINUTES } from '#shared/utils/constants'
4+
import { CACHE_MAX_AGE_FIVE_MINUTES, CACHE_MAX_AGE_ONE_DAY } from '#shared/utils/constants'
55

66
const NPM_REGISTRY = 'https://registry.npmjs.org'
77

shared/utils/npm.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,34 @@ export async function fetchLatestVersion(name: string): Promise<string | null> {
3333
}
3434
}
3535

36+
/**
37+
* Encode package name for URL usage.
38+
* Scoped packages need special handling (@scope/name → @scope%2Fname)
39+
*/
40+
export function encodePackageName(name: string): string {
41+
if (name.startsWith('@')) {
42+
return `@${encodeURIComponent(name.slice(1))}`
43+
}
44+
return encodeURIComponent(name)
45+
}
46+
47+
/**
48+
* Fetch the latest version of a package using fast-npm-meta API.
49+
* This is a lightweight alternative to fetching the full packument.
50+
*
51+
* @param name Package name
52+
* @returns Latest version string or null if not found
53+
* @see https://github.com/antfu/fast-npm-meta
54+
*/
55+
export async function fetchLatestVersion(name: string): Promise<string | null> {
56+
try {
57+
const meta = await getLatestVersion(name)
58+
return meta.version
59+
} catch {
60+
return null
61+
}
62+
}
63+
3664
/**
3765
* Validate an npm package name and throw an HTTP error if invalid.
3866
* Uses validate-npm-package-name to check against npm naming rules.

0 commit comments

Comments
 (0)