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

gennotif

v1.0.2

Published

A generic interface that uses transports to deliver notifications to clients.

Downloads

13

Readme

Generic Notification Interface

A generic interface that uses transports to deliver notifications to clients.

Examples

Sending a simple email:

var config = {
  transports: ['email'],
  data: {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'A new email message',
    text: 'Lorem ipsum.',
    html: '<b>Lorem</b> ipsum.'
  }
}
var notification = new Notification(config);
notification.send();

Using fallback transports:

This will use multiple transports to send the notification. It will each of them (first to last) until one of them will successfully deliver the notification.

var config = {
  transports: ['email', 'console'],
  data: {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'A new email message',
    text: 'Lorem ipsum.',
    html: '<b>Lorem</b> ipsum.'
  }
}
var notification = new Notification(config);
notification.send();

The console transport uses the only the "text" field from the data object.

Using multiple transports:

This notification will be send using all of the available transports. It does this synchronously.

var config = {
  transports: ['email', 'console'],
  data: {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'A new email message',
    text: 'Lorem ipsum.',
    html: '<b>Lorem</b> ipsum.'
  }
}

var notification = new Notification(config);
notification.setStrategy(Notification.STRATEGY_ALL);

notification.send();

Reusing the "notification" object:

The config of the notification object can be change for individual notifications in order to not instantiate a new Notification instance.

var config = {
  transports: ['email', 'console'],
  data: {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'A new email message',
    text: 'Lorem ipsum.',
    html: '<b>Lorem</b> ipsum.'
  }
}

var notification = new Notification(config);
notification.setStrategy(Notification.STRATEGY_ALL);

notification.send();

notification.send({
  data: {
    text: 'Different text for this notification',
    html: '<b>Different</b> <i>text</i> for this notification'
  }
});

Creating a custom transport:

Custom transports can be created by inheriting the NotificationTransportBase and implementing a "handle" method.

var Notification = require('gennotif').Notification;
var NotificationTransportBase = require('gennotif').NotificationTransportBase;
var util = require('util');

var CustomTransport = function() {
  // execute the super_ constructor
  NotificationTransportBase.apply(this, arguments);

  // do your constructor magic
}

util.inherits(CustomTransport, NotificationTransportBase);
module.exports = CustomTransport;

CustomTransport.prototype.handle = function(data, cb) {
  // you get the data part of the config
  
  // send the notification using this transport and than call cb(err, results) when ready
  
  cb();
}

Using the new transport:

var CustomTransport = require('./path/to/CustomTransport');

// Register this new transport
Notification.registerTransport('MyCustomTransport', CustomTransport);

// Use it
var config = {
  transports: ['MyCustomTransport', 'console'],
  data: {
    text: 'Using my custom transport'
  }
}

var new_notification = new Notification(config);

new_notification.send();

API

Class: Notification

Notification.registerTransport(name, TransportObject)

Registers a new transport with the Notification interface.

  • name String the name of the transport that will be used in the config
  • TransportObject NotificationTransportBase the object that extends the NotificationTransportBase used to send the notification

Notification.STRATEGY_LR

Default delivery strategy. Tries to deliver the notification using each transport starting with the first and stops after the first successful delivery.

Notification.STRATEGY_ALL

Tries to deliver the notification using all configured transports. This is done synchronously.

Notification.STRATEGY_RL

Same as Notification.STRATEGY_LR but starts with the last added transport.

new Notification(config)

Create a new notification instance.

  • config Object
    • notifications Array of notification names that could be used
    • data Object basic configuration for the transports

notification.setStrategy(strategy)

Set the delivery strategy for this notification. Possible values:

  • Notification.STRATEGY_LR
  • Notification.STRATEGY_ALL
  • Notification.STRATEGY_RL

notification.addTransport(name);

Adds a transport to the list of used transports. Has to be an already registered transport.

  • name String Name of the transport.

notification.removeTransport(name);

Removes a transport from the list of used transports.

  • name String Name of the transport.

notification.send(data)

Send the notification. The data part of the config used at construction time can be changed by using the data arg.

  • data Object Optional. Overwrites the data config.