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

elasticio-builder

v0.1.9

Published

Awesome tools for elastic.io

Downloads

24

Readme

Elasticio-builder

Awesome tools for Elastic.io

Getting started

npm i elasticio-node elasticio-sailor-nodejs elasticio-builder

Create action or polling trigger

For this purpose you can use:

request(options)

Basic usage

Create simple action or polling trigger:

exports.process = require('elasticio-builder').request({
  method: 'GET',
  url: 'https://{host}/api/users/{user_id}' //all other properties in input metadata schema will be added as query params
})

As you can see we can put input metadata variables or config variables with {name} notation. We cannot provide it as ${msg.body.name} or ${cfg.name} in template string, because we don't have access to this parametrs in this scope. When we send GET request, all input metadata properties will be added as query parameters. On POST, PUT, PATCH, DELETE requests it will be added in body. But what if we want to add some properties as query params in non-get request? Yes, we can add it :)

exports.process = require('elasticio-builder').request({
  method: 'POST',
  url: 'https://{host}/api/users?param={name}' //property name and host can be specified in input schema or config fields
  //all other properties in input metadata schema will be in body
})

Authorize our request

We can provide authorization function

exports.process = require('elasticio-builder').request({
  method: 'POST',
  url: 'https://{host}/api/users?param={name}',
  auth: {
    headerName: 'apllication-key',  //default value is Authorization
    func: require('../verifyCredentials.js') //this function should return authString property on resolve
  }
})

Example of auth function:

module.exports = async function(cfg) {
  return { authString: `Bearer ${cfg.key}`, ...cfg }
}

Auth function should return Promise with object on resolve. authString - required property in this object. Also we can return other properties, they will be used as additional input data.

Advance usage

What if our body has very complicated structure? Or we just want to add additional property to it. We can do it.

exports.process = require('elasticio-builder').request({
  method: 'POST',
  url: 'https://{host}/api/users?param={name}',
  auth: {
    func: require('../verifyCredentials.js')
  },
  spreadBody: {
    age: '{age} years',
    weight: '{weight}'
    additionalInfo: {
      nationality: '{nat}'
    }
  }
})

Again, we just use {name} notation to retrieve property from input schema, config's fields or even from auth Promise resolve. We can even split string and provide array with {name/separator} notation.

spreadBody: {
  hobbies: '{hobbies/, }' // "anime, eating sushi, manga" => hobbies: ['anime', 'eating sushi', 'manga']
}

Create webhook

Very easy, just look at example :)

const { request } = require('elasticio-builder')
exports.startup = request({
  method: 'POST',
  url: 'https://{host}/subscription
})

exports.shutdown = request({
  method: 'DELETE',
  url: 'https://{host}/subscription
})

exports.process = function(msg, cfg) {
  try {
    this.emit('data', { body: msg.body })
  } catch (err) {
    // do some stuff
  }
}

You can use all stuff from previous section.

Dynamic options in SelectView

We need some endpoint with schema for our SelectView control. For example api returns this schema:

{
  metadata: {
    size: 3
  },
  projects: [
    { 
      name: "elasticio", 
      display: { title: "Elastic.io project" }
    },
    {
      name: "star2star", 
      display: { title: "Star2Star project" }
    }
  ]
}

We can use it to create dynamic options for SelectView control like this:

{
  "elasticio": "Elastic.io project",
  "star2star": "Star2Star project"
}

Just use notation like this: {obj.arr[0].prop}. And provide all necessary info.

exports.genOptions = require('elasticio-builder').genSelectViewOptions({
  url: "https://{host}/schema",
  auth: {
    func: require('../verifyCredentials')
  },
  schemaPath: 'projects',
  valuePath: 'name', //relative to schemaPath
  titlePath: 'display.title' //relative to schemaPath
})

Dynamic metadata

Something similar we can do to generate dynamic metadata schema.

exports.getMetaModel = require('elasticio-builder').genMetadata({
  url: "https://{host}/schema",
  auth: {
    func: require('../verifyCredentials')
  },
  schemaPath: 'projects',
  //relative to schemaPath
  namePath: 'name', 
  typePath: 'meta.type',
  propsPath: 'fields',  //for nested objects
  requiredPath: 'isRequired', 
  titlePath: 'meta.title',
  exclude: ['createdAt', 'updatedAt'] //exclude some fields
})