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

@elastic/request-converter

v8.17.0

Published

Elasticsearch request converter

Downloads

377,607

Readme

request-converter

Library that converts Elasticsearch requests in Dev Console syntax to other formats.

Check out the documentation.

Installation

npm install @elastic/request-converter

Usage

import { convertRequests } from "@elastic/request-converter";

const devConsoleScript = `GET /my-index-000001/_search?from=40&size=20
{
  "query": {
    "term": {
      "user.id": "kimchy"
    }
  }
}`

async function main() {
  const code = await convertRequests(devConsoleScript, "python", {
    checkOnly: false,
    printResponse: true,
    complete: true,
    elasticsearchUrl: "http://localhost:9200",
  });
  console.log(code);
}

main();

The list of available formats that can be passed in the second argument can be obtained as follows:

import { listFormats } from "@elastic/request-converter";

const formats = listFormats();

The ouput code in the example above would look like this:

import os
from elasticsearch import Elasticsearch

client = Elasticsearch(
    hosts=["http://localhost:9200"],
    api_key=os.getenv("ELASTIC_API_KEY"),
)

resp = client.search(
    index="my-index-000001",
    from_="40",
    size="20",
    query={
        "term": {
            "user.id": "kimchy"
        }
    },
)

When using Node and JavaScript, you can import the functions in this library as follows:

const { convertRequests, listFormats } = require("@elastic/request-converter");

Available Formats

At this time the converter supports curl and python. Work is currently in progress to add support for javascript, ruby and php.

curl

The curl exporter generates commands for the terminal using the curl command line HTTP client.

Supported options:

| Option name | Type | Required | Description | | ----------- | ---- | -------- | ----------- | | elasticsearchUrl | string | no | The Elasticsearch endpoint to use in the generated commands. The default is http://localhost:9200. | | otherUrls | Record<string, string> | no | URLs for other services. For Kibana, use {kbn: "http://localhost:5601"} | | windows | boolean | no | If true, use PowerShell escaping rules for quotes. If false, use bash/zsh escaping rules. The default is false. |

python

The Python exporter generates code for the Elasticsearch Python client.

Supported options:

| Option name | Type | Required | Description | | ----------- | ---- | -------- | ----------- | | printResponse | boolean | no | If true, add code to print the response. The default is false. | | complete | boolean | no | If true, generate a complete script. If false, only generate the request code. The default is false. | | elasticsearchUrl | string | no | The Elasticsearch endpoint to use. The default is http://localhost:9200. |

Command-Line Interface

For convenience, a CLI that wraps the convertRequests function is also available.

$ echo GET / > request.txt
$ node_modules/.bin/es-request-converter --format python --complete < request.txt
import os
from elasticsearch import Elasticsearch

client = Elasticsearch(
    hosts=[os.getenv("ELASTICSEARCH_URL")],
    api_key=os.getenv("ELASTIC_API_KEY"),
)

resp = client.info()

Using a Custom Exporter

Instead of passing the name of one of the available exporters, you can pass a custom exporter instance.

To define a custom exporter format, create a class that implements the FormatExporter interface. Here is an example exporter that outputs the name of the API used in the request:

import { FormatExporter, convertRequests } from "@elastic/request-converter";

class MyExporter implements FormatExporter {
  async check(requests: ParsedRequest[]): Promise<boolean> { return true; }
  async convert(requests: ParsedRequest[], options: ConvertOptions): Promise<string> {
    return requests.map(req => req.api).join("\n");
  }
}

const apis = await convertRequests("GET /my-index/_search\nGET /\n", new MyExporter(), {});
console.log(apis); // outputs "search\ninfo"