-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathsuggestions.get.ts
More file actions
26 lines (21 loc) · 728 Bytes
/
suggestions.get.ts
File metadata and controls
26 lines (21 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import * as v from 'valibot'
import { SearchQuerySchema } from '#shared/schemas/package'
import { NPM_REGISTRY } from '#shared/utils/constants'
export default defineEventHandler(async event => {
const query = getQuery(event)
try {
const q = v.parse(SearchQuerySchema, query.q)
if (!q) {
return [q, []]
}
const params = new URLSearchParams({ text: q, size: '10' })
const response = await $fetch<NpmSearchResponse>(`${NPM_REGISTRY}/-/v1/search?${params}`)
const suggestions = response.objects.map(obj => obj.package.name)
return [q, suggestions]
} catch (error: unknown) {
handleApiError(error, {
statusCode: 502,
message: ERROR_SUGGESTIONS_FETCH_FAILED,
})
}
})