0.6.0 • Published 4 years ago

khosmo v0.6.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

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
}
keySpecifications
actionString with action key to filter on receiver
parserBoolean to convert body request to json (true is default)
routeDefault route the receiver api
debugBoolean 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)

0.6.0

4 years ago

0.5.8

4 years ago

0.5.6

4 years ago

0.5.4

4 years ago

0.5.5

4 years ago

0.5.0

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago