1- import type { FastNpmMetaResponse , Packument } from '#shared/types'
1+ import type { Packument } from '#shared/types'
2+ import { encodePackageName , fetchLatestVersion as _fetchLatestVersion } from '#shared/utils/npm'
23import { maxSatisfying , prerelease } from 'semver'
34
45const NPM_REGISTRY = 'https://registry.npmjs.org'
5- const FAST_NPM_META_API = 'https://npm.antfu.dev'
6-
7- function encodePackageName ( name : string ) : string {
8- if ( name . startsWith ( '@' ) ) {
9- return `@${ encodeURIComponent ( name . slice ( 1 ) ) } `
10- }
11- return encodeURIComponent ( name )
12- }
136
147export const fetchNpmPackage = defineCachedFunction (
158 async ( name : string ) : Promise < Packument > => {
@@ -24,31 +17,6 @@ export const fetchNpmPackage = defineCachedFunction(
2417 } ,
2518)
2619
27- /**
28- * Fetch lightweight package metadata from fast-npm-meta API.
29- * Much smaller payload than full packument - ideal for just getting latest version.
30- *
31- * @param name Package name
32- * @param specifier Optional version specifier (tag like "alpha", or range like "^2.1.0")
33- * @returns Resolved version info
34- * @see https://github.com/antfu/fast-npm-meta
35- */
36- export const fetchFastNpmMeta = defineCachedFunction (
37- async ( name : string , specifier ?: string ) : Promise < FastNpmMetaResponse > => {
38- const encodedName = encodePackageName ( name )
39- const url = specifier
40- ? `${ FAST_NPM_META_API } /${ encodedName } @${ encodeURIComponent ( specifier ) } `
41- : `${ FAST_NPM_META_API } /${ encodedName } `
42- return await $fetch < FastNpmMetaResponse > ( url )
43- } ,
44- {
45- maxAge : 60 * 5 ,
46- swr : true ,
47- name : 'fast-npm-meta' ,
48- getKey : ( name : string , specifier ?: string ) => ( specifier ? `${ name } @${ specifier } ` : name ) ,
49- } ,
50- )
51-
5220/**
5321 * Get the latest version of a package using fast-npm-meta API.
5422 * Falls back to full packument if fast-npm-meta fails.
@@ -57,17 +25,15 @@ export const fetchFastNpmMeta = defineCachedFunction(
5725 * @returns Latest version string or null if not found
5826 */
5927export async function fetchLatestVersion ( name : string ) : Promise < string | null > {
28+ const version = await _fetchLatestVersion ( name )
29+ if ( version ) return version
30+
31+ // Fallback to full packument (also cached)
6032 try {
61- const meta = await fetchFastNpmMeta ( name )
62- return meta . version
33+ const packument = await fetchNpmPackage ( name )
34+ return packument [ 'dist-tags' ] ?. latest ?? null
6335 } catch {
64- // Fallback to full packument
65- try {
66- const packument = await fetchNpmPackage ( name )
67- return packument [ 'dist-tags' ] ?. latest ?? null
68- } catch {
69- return null
70- }
36+ return null
7137 }
7238}
7339
0 commit comments