Skip to content

Commit 534f068

Browse files
committed
refactor: create useSearch and extract npm logic
1 parent 3880138 commit 534f068

File tree

8 files changed

+331
-351
lines changed

8 files changed

+331
-351
lines changed

app/components/Compare/PackageSelector.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const maxPackages = computed(() => props.max ?? 4)
1414
const inputValue = shallowRef('')
1515
const isInputFocused = shallowRef(false)
1616
17-
// Use the shared npm search composable
18-
const { data: searchData, status } = useNpmSearch(inputValue, { size: 15 })
17+
// Use the shared search composable (supports both npm and Algolia providers)
18+
const { data: searchData, status } = useSearch(inputValue, { size: 15 })
1919
2020
const isSearching = computed(() => status.value === 'pending')
2121
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { NpmSearchResponse, NpmSearchResult, MinimalPackument } from '#shared/types'
2+
3+
/**
4+
* Convert packument to search result format for display
5+
*/
6+
export function packumentToSearchResult(
7+
pkg: MinimalPackument,
8+
weeklyDownloads?: number,
9+
): NpmSearchResult {
10+
let latestVersion = ''
11+
if (pkg['dist-tags']) {
12+
latestVersion = pkg['dist-tags'].latest || Object.values(pkg['dist-tags'])[0] || ''
13+
}
14+
const modified = pkg.time.modified || pkg.time[latestVersion] || ''
15+
16+
return {
17+
package: {
18+
name: pkg.name,
19+
version: latestVersion,
20+
description: pkg.description,
21+
keywords: pkg.keywords,
22+
date: pkg.time[latestVersion] || modified,
23+
links: {
24+
npm: `https://www.npmjs.com/package/${pkg.name}`,
25+
},
26+
maintainers: pkg.maintainers,
27+
},
28+
score: { final: 0, detail: { quality: 0, popularity: 0, maintenance: 0 } },
29+
searchScore: 0,
30+
downloads: weeklyDownloads !== undefined ? { weekly: weeklyDownloads } : undefined,
31+
updated: pkg.time[latestVersion] || modified,
32+
}
33+
}
34+
35+
export function emptySearchResponse(): NpmSearchResponse {
36+
return {
37+
objects: [],
38+
total: 0,
39+
isStale: false,
40+
time: new Date().toISOString(),
41+
}
42+
}

0 commit comments

Comments
 (0)