@@ -18,12 +18,6 @@ import type { CachedFetchFunction } from '#shared/utils/fetch-cache-config'
1818const NPM_REGISTRY = 'https://registry.npmjs.org'
1919const NPM_API = 'https://api.npmjs.org'
2020
21- interface NpmSearchError {
22- data : {
23- code : string
24- }
25- }
26-
2721// Cache for packument fetches to avoid duplicate requests across components
2822const packumentCache = new Map < string , Promise < Packument | null > > ( )
2923
@@ -328,57 +322,48 @@ export function useNpmSearch(
328322 // Use requested size for initial fetch
329323 params . set ( 'size' , String ( opts . size ?? 25 ) )
330324
331- try {
332- const { data : response , isStale } = await cachedFetch < NpmSearchResponse > (
333- `${ NPM_REGISTRY } /-/v1/search?${ params . toString ( ) } ` ,
334- { signal } ,
335- 60 ,
336- )
325+ if ( q . length === 1 ) {
326+ const encodedName = encodePackageName ( q )
327+ const [ { data : pkg , isStale } , { data : downloads } ] = await Promise . all ( [
328+ cachedFetch < Packument > ( `${ NPM_REGISTRY } /${ encodedName } ` , { signal } ) ,
329+ cachedFetch < NpmDownloadCount > ( `${ NPM_API } /downloads/point/last-week/${ encodedName } ` , {
330+ signal,
331+ } ) ,
332+ ] )
337333
338- cache . value = {
339- query : q ,
340- objects : response . objects ,
341- total : response . total ,
334+ if ( ! pkg ) {
335+ return emptySearchResponse
342336 }
343337
344- return { ...response , isStale }
345- } catch ( error ) {
346- if ( ( error as NpmSearchError ) ?. data ?. code === 'ERR_TEXT_LENGTH' ) {
347- try {
348- const encodedName = encodePackageName ( q )
349- const [ { data : pkg , isStale } , { data : downloads } ] = await Promise . all ( [
350- cachedFetch < Packument > ( `${ NPM_REGISTRY } /${ encodedName } ` , { signal } ) ,
351- cachedFetch < NpmDownloadCount > ( `${ NPM_API } /downloads/point/last-week/${ encodedName } ` , {
352- signal,
353- } ) ,
354- ] )
355-
356- if ( ! pkg ) {
357- throw error
358- }
359-
360- const result = packumentToSearchResult ( pkg , downloads ?. downloads )
338+ const result = packumentToSearchResult ( pkg , downloads ?. downloads )
361339
362- cache . value = {
363- query : q ,
364- objects : [ result ] ,
365- total : 1 ,
366- }
340+ cache . value = {
341+ query : q ,
342+ objects : [ result ] ,
343+ total : 1 ,
344+ }
367345
368- return {
369- objects : [ result ] ,
370- total : 1 ,
371- isStale,
372- time : new Date ( ) . toISOString ( ) ,
373- }
374- } catch {
375- // If exact lookup also fails, throw original error
376- throw error
377- }
346+ return {
347+ objects : [ result ] ,
348+ total : 1 ,
349+ isStale,
350+ time : new Date ( ) . toISOString ( ) ,
378351 }
352+ }
379353
380- throw error
354+ const { data : response , isStale } = await cachedFetch < NpmSearchResponse > (
355+ `${ NPM_REGISTRY } /-/v1/search?${ params . toString ( ) } ` ,
356+ { signal } ,
357+ 60 ,
358+ )
359+
360+ cache . value = {
361+ query : q ,
362+ objects : response . objects ,
363+ total : response . total ,
381364 }
365+
366+ return { ...response , isStale }
382367 } ,
383368 { default : ( ) => lastSearch || emptySearchResponse } ,
384369 )
0 commit comments