[READ-ONLY] a fast, modern browser for the npm registry

fix: allow queries to `/api/opensearch/suggestions` (#1016)

authored by

Daniel Roe and committed by
GitHub
29137bcb dbb4d739

+26 -30
+7
nuxt.config.ts
··· 98 98 '/search': { isr: false, cache: false }, 99 99 '/api/auth/**': { isr: false, cache: false }, 100 100 '/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 + }, 101 108 // infinite cache (versioned - doesn't change) 102 109 '/package-code/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } }, 103 110 '/package-docs/:pkg/v/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } },
+19 -30
server/api/opensearch/suggestions.get.ts
··· 1 1 import * as v from 'valibot' 2 2 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' 4 4 5 - export default defineCachedEventHandler( 6 - async event => { 7 - const query = getQuery(event) 5 + export default defineEventHandler(async event => { 6 + const query = getQuery(event) 8 7 9 - try { 10 - const q = v.parse(SearchQuerySchema, query.q) 8 + try { 9 + const q = v.parse(SearchQuerySchema, query.q) 11 10 12 - if (!q) { 13 - return [q, []] 14 - } 11 + if (!q) { 12 + return [q, []] 13 + } 15 14 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}`) 18 17 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 + })