Skip to content

Commit d4df29e

Browse files
committed
perf: cache org and maintainer registry requests client side
1 parent 83abe34 commit d4df29e

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

app/composables/npm/useNpmSearch.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,39 @@ export interface NpmSearchOptions {
66
from?: number
77
}
88

9-
async function checkOrgExists(name: string): Promise<boolean> {
10-
try {
11-
const scopePrefix = `@${name.toLowerCase()}/`
12-
const response = await $fetch<{
13-
total: number
14-
objects: Array<{ package: { name: string } }>
15-
}>(`${NPM_REGISTRY}/-/v1/search`, { query: { text: `@${name}`, size: 5 } })
16-
return response.objects.some(obj => obj.package.name.toLowerCase().startsWith(scopePrefix))
17-
} catch {
18-
return false
19-
}
20-
}
21-
22-
async function checkUserExists(name: string): Promise<boolean> {
23-
try {
24-
const response = await $fetch<{ total: number }>(`${NPM_REGISTRY}/-/v1/search`, {
25-
query: { text: `maintainer:${name}`, size: 1 },
26-
})
27-
return response.total > 0
28-
} catch {
29-
return false
30-
}
31-
}
32-
339
/**
3410
* Composable providing npm registry search.
3511
* Must be called during component setup.
3612
*/
3713
export function useNpmSearch() {
3814
const { $npmRegistry } = useNuxtApp()
3915

16+
async function checkOrgExists(name: string): Promise<boolean> {
17+
try {
18+
const scopePrefix = `@${name.toLowerCase()}/`
19+
const response = await $npmRegistry<{
20+
total: number
21+
objects: Array<{ package: { name: string } }>
22+
}>(`/-/v1/search`, { query: { text: `@${name}`, size: 5 } })
23+
return response.data.objects.some(obj =>
24+
obj.package.name.toLowerCase().startsWith(scopePrefix),
25+
)
26+
} catch {
27+
return false
28+
}
29+
}
30+
31+
async function checkUserExists(name: string): Promise<boolean> {
32+
try {
33+
const response = await $npmRegistry<{ total: number }>(`/-/v1/search`, {
34+
query: { text: `maintainer:${name}`, size: 1 },
35+
})
36+
return response.data.total > 0
37+
} catch {
38+
return false
39+
}
40+
}
41+
4042
/**
4143
* Search npm packages. Single-character queries fetch lightweight metadata
4244
* via a server proxy since the search API returns poor results for them.

0 commit comments

Comments
 (0)