Skip to content

Commit 29137bc

Browse files
authored
fix: allow queries to /api/opensearch/suggestions (#1016)
1 parent dbb4d73 commit 29137bc

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

nuxt.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ export default defineNuxtConfig({
9898
'/search': { isr: false, cache: false },
9999
'/api/auth/**': { isr: false, cache: false },
100100
'/api/social/**': { isr: false, cache: false },
101+
'/api/opensearch/suggestions': {
102+
isr: {
103+
expiration: 60 * 60 * 24 /* one day */,
104+
passQuery: true,
105+
allowQuery: ['q'],
106+
},
107+
},
101108
// infinite cache (versioned - doesn't change)
102109
'/package-code/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } },
103110
'/package-docs/:pkg/v/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } },
Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
11
import * as v from 'valibot'
22
import { SearchQuerySchema } from '#shared/schemas/package'
3-
import { CACHE_MAX_AGE_ONE_MINUTE, NPM_REGISTRY } from '#shared/utils/constants'
3+
import { NPM_REGISTRY } from '#shared/utils/constants'
44

5-
export default defineCachedEventHandler(
6-
async event => {
7-
const query = getQuery(event)
5+
export default defineEventHandler(async event => {
6+
const query = getQuery(event)
87

9-
try {
10-
const q = v.parse(SearchQuerySchema, query.q)
8+
try {
9+
const q = v.parse(SearchQuerySchema, query.q)
1110

12-
if (!q) {
13-
return [q, []]
14-
}
11+
if (!q) {
12+
return [q, []]
13+
}
1514

16-
const params = new URLSearchParams({ text: q, size: '10' })
17-
const response = await $fetch<NpmSearchResponse>(`${NPM_REGISTRY}/-/v1/search?${params}`)
15+
const params = new URLSearchParams({ text: q, size: '10' })
16+
const response = await $fetch<NpmSearchResponse>(`${NPM_REGISTRY}/-/v1/search?${params}`)
1817

19-
const suggestions = response.objects.map(obj => obj.package.name)
20-
return [q, suggestions]
21-
} catch (error: unknown) {
22-
handleApiError(error, {
23-
statusCode: 502,
24-
message: ERROR_SUGGESTIONS_FETCH_FAILED,
25-
})
26-
}
27-
},
28-
{
29-
maxAge: CACHE_MAX_AGE_ONE_MINUTE,
30-
swr: true,
31-
getKey: event => {
32-
const query = getQuery(event)
33-
const q = String(query.q || '').trim()
34-
return `opensearch-suggestions:${q}`
35-
},
36-
},
37-
)
18+
const suggestions = response.objects.map(obj => obj.package.name)
19+
return [q, suggestions]
20+
} catch (error: unknown) {
21+
handleApiError(error, {
22+
statusCode: 502,
23+
message: ERROR_SUGGESTIONS_FETCH_FAILED,
24+
})
25+
}
26+
})

0 commit comments

Comments
 (0)