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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jpbberry/opensearch-typed

v0.0.4

Published

Take an Opensearch request object and turn it into a fully-typed response

Readme

opensearch-typed

Take an Opensearch request object and turn it into a fully-typesafe Opensearch response.

Library WIP

Only the following aggregations have been implemented:

  • terms
  • filter
  • date_histogram
  • auto_date_histogram
  • avg
  • multi_terms

Usage

Create a function to wrap your Opensearch queries, using the @jpbberry/opensearch-typed type interpreters.

There is no dependency on the Opensearch library allowing you to use whatever version you would like, but you will need to combine the types using & in order to get a typed experience on the query writing

E.g

import {
  OpensearchRequest,
  OpensearchResponse,
} from "@jpbberry/opensearch-typed";

import {
  Search_Request,
  Search_Response,
} from "@opensearch-project/opensearch/api/index.js";

export type IndexList = [
  // Define your index list, more info below
  {
    pattern: `${string}-test`;
    schema: SchemaObject;
  }
];

export async function query<
  // You will need to store some generics
  N extends number,
  K extends string | number | symbol,
  I extends IndexList[number]["pattern"],
  // Main request type
  R extends OpensearchRequest<N, K, I> & Search_Request // Your own search request type for enforcing inputs types
>(
  request: T
): Promise<
  Search_Response & { body: OpensearchResponse<N, IndexList, T> } // Your own search response type for other Opensearch response fields // Override the body response type, this will only take over aggregations & hits, others types will come from Search_Response
> {
  return await client.search(request); // The request type maps directly with the @opensearch-project/opensearch library
}

// In your implementation
const data = await query({
  body: {
    aggs: {
      // or aggregations
      things: {
        terms: {
          // should be typed from opensearch-project
          field: "field.keyword",
        },
        aggs: {
          other_thing: {
            filter: {
              term: {
                "thing.keyword": "abc",
              },
            },
          },
        },
      },
    },
  },
});

data.body.aggregations.things.buckets[0].other_thing.doc_count; // fully typed!!

Hits source controlling

Depending on the schema you have defined the fields will be typed as well. E.g

interface SchemaObject {
  abc: string;
  def: string;
}

export type IndexList = [
  {
    pattern: `${string}-test`;
    schema: SchemaObject;
  }
];
// In your query function, use IndexList as the input for OpensearchResponse<N, IndexList, T>

const data = await query({
  index: "*-test",
  body: {
    query: {
      match_all: {},
    },
  },
});

data.hits.hits[0]._source; // { abc: string, def: string }

You can also control the field sources in the body:

const data = await query({
  index: "*-test",
  body: {
    _source: ["abc"], // typed
    query: {
      match_all: {},
    },
  },
});

data.hits.hits[0]._source; // { abc: string }

If defining size as 0, the hits array will also be never

const data = await query({
  size: 0,
  body: {...}
})

data.hits.hits // never

If the IndexList is defined as [], then the _source will be any, unless, _source is defined on the body, if it is, the source response will be defined as the keys in that array, with value any.

// With OpensearchResponse<N, [], T>

const data = await query({
  index: '*-unknown',
  body: {...}
})

data.hits.hits[0] // any


const data = await query({
  index: '*-unknown',
  body: {
    _source: ['abc']
  }
})

data.hits.hits[0] // { abc: any }