[READ-ONLY] a fast, modern browser for the npm registry
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(ui): fallback to exact package lookup for short queries (#729)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

authored by

Matteo Gabriele
autofix-ci[bot]
and committed by
GitHub
24b7e7e6 6a41d106

+30
+30
app/composables/useNpmRegistry.ts
··· 306 306 () => `search:incremental:${toValue(query)}`, 307 307 async (_nuxtApp, { signal }) => { 308 308 const q = toValue(query) 309 + 309 310 if (!q.trim()) { 310 311 return emptySearchResponse 311 312 } ··· 320 321 params.set('text', q) 321 322 // Use requested size for initial fetch 322 323 params.set('size', String(opts.size ?? 25)) 324 + 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 + ]) 333 + 334 + if (!pkg) { 335 + return emptySearchResponse 336 + } 337 + 338 + const result = packumentToSearchResult(pkg, downloads?.downloads) 339 + 340 + cache.value = { 341 + query: q, 342 + objects: [result], 343 + total: 1, 344 + } 345 + 346 + return { 347 + objects: [result], 348 + total: 1, 349 + isStale, 350 + time: new Date().toISOString(), 351 + } 352 + } 323 353 324 354 const { data: response, isStale } = await cachedFetch<NpmSearchResponse>( 325 355 `${NPM_REGISTRY}/-/v1/search?${params.toString()}`,