npms-io/npms-api

Add a parameter for returning search suggestions in OpenSearch format

aminomancer opened this issue · 0 comments

Browsers can display search suggestions (see search_provider.suggest_url) from an OpenSearch definition, but they have to be formatted as a JSON array [input:string, packageNames:string[]] or an XML SearchSuggestion (slower).

Example (for Wiktionary):
Request: https://en.wiktionary.org/w/api.php?action=opensearch&search=test
Response:

[
  "test",
  [
    "test",
    "testa",
    "testis",
    "testament",
    "teste",
    "tester",
    "testicle",
    "testimonium",
    "testo",
    "testudo"
  ],
  // ...unnecessary implementation-specific members
]

If you add a parameter like format=osxs that will return this simple array type, the OpenSearch description on this repo can be extended so browsers can display npm search suggestions:

--- a/npms-www/web/opensearch.xml
+++ b/npms-www/web/opensearch.xml
@@ -1,11 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
 	<ShortName>npms</ShortName>
 	<Description>npms - a better and open sourced search for node packages</Description>
 	<Tags>npm search node module package score github</Tags>
 	<Contact>amdfcruz+npms@gmail.com</Contact>
 	<Developer>André Cruz</Developer>
 	<Url type="text/html" method="get" template="https://npms.io/search?q={searchTerms}"/>
+ 	<Url type="application/x-suggestions+json" method="get" template="https://npms.io/search?q={searchTerms}&format=osxs" />
	<InputEncoding>UTF-8</InputEncoding>
	<Image height="16" width="16" type="image/vnd.microsoft.icon"></Image>
</OpenSearchDescription>