Skip to content

Commit 24b7e7e

Browse files
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>
1 parent 6a41d10 commit 24b7e7e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

app/composables/useNpmRegistry.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ export function useNpmSearch(
306306
() => `search:incremental:${toValue(query)}`,
307307
async (_nuxtApp, { signal }) => {
308308
const q = toValue(query)
309+
309310
if (!q.trim()) {
310311
return emptySearchResponse
311312
}
@@ -321,6 +322,35 @@ export function useNpmSearch(
321322
// Use requested size for initial fetch
322323
params.set('size', String(opts.size ?? 25))
323324

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+
}
353+
324354
const { data: response, isStale } = await cachedFetch<NpmSearchResponse>(
325355
`${NPM_REGISTRY}/-/v1/search?${params.toString()}`,
326356
{ signal },

0 commit comments

Comments
 (0)