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

khosmo

v0.6.0

Published

Webhook api complete with integrated receiver server, notification trigger and file system observer

Downloads

22

Readme

Khosmo

This module allows you to easily implement a webhook service to monitor changes of state. They rely on the pre-configuration of triggers that when triggered send notifications and share data in real time. This module also has a built-in server for implementing a webhook receiving API.

Get started

Install module in your project

npm i --save khosmo

Basic usage

Start module:

const khosmo = require("khosmo");

Create webhook events pointing to one http data receptor:

// hook one
khosmo.create("hook_post",
  "http://localhost:8000/hook_posts"
);
// hook two
khosmo.create("error",
  "http://localhost:8000/error/log"
);

To trigger a hook call it by passing the data you want to send:

khosmo.send("hook_post", {
  name : "Pedro José",
  msg : "Hello hook"
});

You can also notify with a simple text, example:

khosmo.send("hook_post", "This is one message");

Send notification with custom headers using JSON object:

khosmo.send( [webhook_name], [data], [headers])

khosmo.send("hook_post", "This is message data", {
  "Content-Type": "application/json",
  "Authorization": "Bearer token_here",
});

Server api

Khosmo has an integrated server that can serve as a webhook. This way you can create services to receive data sent from any webhook sender.

Build one basic Khosmo receiver:

const khosmo = require("khosmo");
// Configure
khosmo.config({
  parser : true,
  route : "/"
});
// Defines a global service for receiving messages
khosmo.all(message => {
  console.log(`Message captured: ${JSON.stringify(message)}`);
});

Start the server with:

khosmo.listen(8000, (err)=> {
  if(err) throw new Error(`Not started server: ${err.message}`);
  // Server started
});

The webhook receiver is started and all messages sent to http: // localhost:8000 will be captured in khosmo.all().

The default service settings are set to: khosmo.config(). Check all the settings in the options session

Filter JSON data

Set data filters for the message receiver. All data sent in JSON will be filtered through a specific, preconfigured key contained in the first level of the object, example:

//  Configure the JSON key to perform the action filter
// "action_type" is filter custom key
khosmo.config({
  action : "action_type",
  parser : true,
  route : "/"
});
// Create one filter to action
khosmo.filter("payment_finish", (message) => {
  console.log(`Payment made by: ${message.user_name}`);
});
  1. The webhook sends a notification as the template below to be filtered in this action:
{
  "action_type": "payment_finish",
  "id": "5ASDFe5w6454asdf64fsa",
  "user_name": "Richard Peterson",
  "value": "US$ 486,25"
}

Server router

You can create a customized http api through the system of routes integrated in the Khosmo, example:

khosmo.route("/receiver/posts", message => {
  console.log(`Message received: ${message}`);
});
//-
khosmo.route("/receiver/report", message => {
  console.log(`Report notification: ${message}`);
});

File observer

Define a file monitor to identify and intercept actions that occur in a particular directory, for example:

khosmo.observe("./my_files", (fileName, action) => {
  console.log(`${action} : ${name}`); // > change : file.yml
}, {
  get_data: false
});

Check params: observe( [path], [callback], [options] )

Now run a hook trigger and notify a service whenever there are changes in states to any file.

// create one hook trigger
khosmo.create("file_changed",
  "http://localhost:8000/monitoring/files"
);
// create one file observer definindo ./my_files como diretório de monitoramento
khosmo.observe("./my_files", (fileName, action, data) => {
  // triggering notification via webhook
  khosmo.send("file_changed", {
    action: action,
    fileName: fileName,
    fileData: data
  });

}, {
  get_data: true
});

Options

All options configure of Khosmo.

{
  "action": "action_check_key",
  "parser": true,
  "route": "/",
  "debug": false
}

| key | Specifications | |--------|-----------------------------------------------------------| | action | String with action key to filter on receiver | | parser | Boolean to convert body request to json (true is default) | | route | Default route the receiver api | | debug | Boolean define if debug mod is active |

Current features

  1. Notifications trigger
  2. Webhook receptor service filter
  3. Api http service
  4. File observer

License

The MIT License (MIT)