1- import type { Packument , NpmSearchResponse , NpmDownloadCount } from '#shared/types'
1+ import type { Packument } from '#shared/types'
22import { maxSatisfying , prerelease } from 'semver'
33
44const NPM_REGISTRY = 'https://registry.npmjs.org'
5- const NPM_API = 'https://api.npmjs.org'
65
76function encodePackageName ( name : string ) : string {
87 if ( name . startsWith ( '@' ) ) {
@@ -18,39 +17,12 @@ export const fetchNpmPackage = defineCachedFunction(
1817 } ,
1918 {
2019 maxAge : 60 * 5 ,
20+ swr : true ,
2121 name : 'npm-package' ,
2222 getKey : ( name : string ) => name ,
2323 } ,
2424)
2525
26- export const fetchNpmSearch = defineCachedFunction (
27- async ( query : string , size : number = 20 , from : number = 0 ) : Promise < NpmSearchResponse > => {
28- const params = new URLSearchParams ( {
29- text : query ,
30- size : String ( size ) ,
31- from : String ( from ) ,
32- } )
33- return await $fetch < NpmSearchResponse > ( `${ NPM_REGISTRY } /-/v1/search?${ params . toString ( ) } ` )
34- } ,
35- {
36- maxAge : 60 * 2 ,
37- name : 'npm-search' ,
38- getKey : ( query : string , size : number , from : number ) => `${ query } :${ size } :${ from } ` ,
39- } ,
40- )
41-
42- export const fetchNpmDownloads = defineCachedFunction (
43- async ( name : string , period : string = 'last-week' ) : Promise < NpmDownloadCount > => {
44- const encodedName = encodePackageName ( name )
45- return await $fetch < NpmDownloadCount > ( `${ NPM_API } /downloads/point/${ period } /${ encodedName } ` )
46- } ,
47- {
48- maxAge : 60 * 60 ,
49- name : 'npm-downloads' ,
50- getKey : ( name : string , period : string ) => `${ name } :${ period } ` ,
51- } ,
52- )
53-
5426/**
5527 * Check if a version constraint explicitly includes a prerelease tag.
5628 * e.g., "^1.0.0-alpha" or ">=2.0.0-beta.1" include prereleases
0 commit comments