npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

opensearch-browser

v2.0.0-alpha.7

Published

An OpenSearch client supporting the geo and time extensions.

Downloads

123

Readme

OpenSearch JavaScript client

NPM Coverage Status

The full documentation is available here.

Setup

To install the client framework perform the following steps:

npm install opensearch-browser

Usage

The easiest way to use the library is by using the discover function, which takes a single parameter, the URL of the OpenSearch service:

import { discover } from 'opensearch-browser';
// or: var discover = require('opensearch-browser').discover;

discover('http://example.com/search').then((service) => {
  service.search({ searchTerms: 'Test', startIndex: 1 }).then((results) => {
    // your results:
  });
});

If you already have the OpenSearch description document locally, you can also use the fromXml function to create the service class:

import { fromXml } from 'opensearch-browser';

const osddDocumentString = `
<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
...
</OpenSearchDescription>`;

const service = fromXml(osddDocumentString);

This OpenSearch library requires Promises. If you are not sure whether you have it available use the following polyfill:

require('es6-promise').polyfill();

Configuration

This library uses a global configuration interface, provided by the config function, which is used for getting and setting configuration values:

import { config } from 'opensearch-browser';

// getting the config
const { useXHR, Promise } = config();

// setting the config
config({
  useXHR: true,
  Promise: Promise,
});

Currently supported are the following config values:

  • useXHR: Whether to use the XMLHttpRequest or the fetch API. The former has the advantage that the requests can be aborted. This is exposed when a Promise type is used that supports cancelling, like the great bluebird library.
  • Promise: If set, overrides default ES6 Promise with a custom implementation, for example bluebird. However bluebird is not set as a dependency to avoid bloating the library.

Request parameters

Request parameters are supplied as an object whose attribute names shall either be the URL parameter names or their types. For example, if the OpenSearch service provides a URL like the following example:

<Url type="text/html"
  template="http://example.com/search?q={searchTerms}&amp;pw={startPage?}"
/>

then the following request parameters are possible:

// Using the types
service.search({ searchTerms: 'Test', startPage: 1 }).then( ... );

// Using the parameter names
service.search({ q: 'Test', pw: 1 }).then( ... );

// Omitting the optional parameter 'startPage'
service.search({ searchTerms: 'Test' }).then( ... );

An exception will be raised when mandatory parameters are not supplied.

Some parameter types will be automatically translated from their object to their string representation:

| Parameter type | Object | Value | | -------------------------------------- | ---------------------------------- | ---------------------------------- | | time:start and time:end | Date | an ISO 8601 string representation | | geo:box | [left, bottom, right, top] | a string "left,bottom,right,top" | | geo:geometry | GeoJSON Geometry Object | the WKT representation | | all numeric types + datetime from eo | Number | "<value>" | | | [value1, value2, ...] | "{<value1>,<value2>,...}" | | | { min: minValue, max: maxValue } | "[<minValue>,<maxValue>]" | | | { min: minValue } | "[<minValue>" | | | { max: maxValue } | "<maxValue>]" | | | { minExclusive: minValue } | "]<minValue>" | | | { maxExclusive: maxValue } | "<maxValue>[" | | | ... | |

Search Results

By default, the library is able to parse RSS, Atom and GeoJSON responses. They are parsed to a structure based upon the GeoJSON format.

It is possible to extend the supported formats by adding additional format handlers:

import { registerFormat } from 'opensearch-browser';

const format = {
  parse: function(text) {
    // insert parsing logic here...
    return ...;
  }
};

// register the format under the given mime-type
registerFormat('application/vnd.special+xml', format);

When a search URL is used with that mime-type, the response is now parsed with the registered handler.

Alternatively, raw responses can be used, and parsing be performed outside of this library:

const mimeType = null;
const raw = true;
service.search({ searchTerms: 'Test', startIndex: 1 }, mimeType, raw)
  .then(function(response) {
    // do something with the response
  });

For both cases, the response is a Response object from the fetch API.

Suggestions

This library also supports the Suggestions extension of OpenSearch. This is implemented on the Service via the getSuggestions method:

service.getSuggestions({ searchTerms: 'someth' })
  .then(function(suggestions) {
    for (let i = 0; i < suggestions.length; ++i) {
      console.log(
        suggestion.completion,
        suggestion.description,
        suggestion.url
      );
    }
  });

For this to work, the server must have a search url with the type application/x-suggestions+json defined.

Testing

To run the unit tests do

npm test

To run the unit tests continuously, run the following command:

npm run test:watch

Documentation

To generate the API documentation run:

npm run docs

Notes

This library aims to provide a broad support of the most common OpenSearch functionality and exchange formats. It also supports the Geo, Time, EO Products, Parameters, and Suggestions extensions and adheres to various points of the CEOS OpenSearch best practice paper.