File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed
Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,14 @@ export default defineNuxtConfig({
2222 app : {
2323 head : {
2424 htmlAttrs : { lang : 'en' } ,
25+ link : [
26+ {
27+ rel : 'search' ,
28+ type : 'application/opensearchdescription+xml' ,
29+ title : 'npm' ,
30+ href : '/opensearch.xml' ,
31+ } ,
32+ ] ,
2533 } ,
2634 } ,
2735
@@ -39,6 +47,7 @@ export default defineNuxtConfig({
3947
4048 routeRules : {
4149 '/' : { prerender : true } ,
50+ '/opensearch.xml' : { isr : true } ,
4251 '/**' : { isr : 60 } ,
4352 '/package/**' : { isr : 60 } ,
4453 '/search' : { isr : false , cache : false } ,
Original file line number Diff line number Diff line change 1+ import type { NpmSearchResponse } from '#shared/types'
2+ import { NPM_REGISTRY } from '#shared/utils/constants'
3+
4+ export default defineCachedEventHandler (
5+ async event => {
6+ const query = getQuery ( event )
7+ const q = String ( query . q || '' ) . trim ( )
8+
9+ if ( ! q ) {
10+ return [ q , [ ] ]
11+ }
12+
13+ const params = new URLSearchParams ( { text : q , size : '10' } )
14+ const response = await $fetch < NpmSearchResponse > ( `${ NPM_REGISTRY } /-/v1/search?${ params } ` )
15+
16+ const suggestions = response . objects . map ( obj => obj . package . name )
17+ return [ q , suggestions ]
18+ } ,
19+ {
20+ maxAge : 60 ,
21+ swr : true ,
22+ getKey : event => {
23+ const query = getQuery ( event )
24+ return `opensearch-suggestions:${ query . q || '' } `
25+ } ,
26+ } ,
27+ )
Original file line number Diff line number Diff line change 1+ export default defineEventHandler ( async event => {
2+ const url = getRequestURL ( event )
3+ const origin = url . origin
4+ return `
5+ <?xml version="1.0" encoding="UTF-8"?>
6+ <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
7+ <ShortName>npm</ShortName>
8+ <Description>Search npm packages on npmx.dev</Description>
9+ <InputEncoding>UTF-8</InputEncoding>
10+ <Image width="16" height="16" type="image/svg+xml">${ origin } /favicon.svg</Image>
11+ <Url type="text/html" template="${ origin } /search?q={searchTerms}"/>
12+ <Url type="application/x-suggestions+json" template="${ origin } /api/opensearch/suggestions?q={searchTerms}"/>
13+ <Url type="application/opensearchdescription+xml" rel="self" template="${ origin } /opensearch.xml"/>
14+ </OpenSearchDescription>
15+ ` . trim ( )
16+ } )
You can’t perform that action at this time.
0 commit comments