UNPKG

@webgap/notifier

Version:

Webgap abstraction notification module.

103 lines (82 loc) 3.09 kB
# WebGAP Notifier [![Build Status](https://travis-ci.org/webgap/notifier.svg)](https://travis-ci.org/webgap/notifier) [![Test Coverage](https://codeclimate.com/github/webgap/notifier/badges/coverage.svg)](https://codeclimate.com/github/webgap/notifier/coverage) [![Code Climate](https://codeclimate.com/github/webgap/notifier/badges/gpa.svg)](https://codeclimate.com/github/webgap/notifier) [![Dependency Status](https://gemnasium.com/webgap/notifier.png)](https://gemnasium.com/webgap/notifier) [![NPM version](http://img.shields.io/npm/v/@webgap/notifier.svg?style=flat)](https://www.npmjs.com/package/@webgap/notifier) [![NPM downloads](http://img.shields.io/npm/dm/@webgap/notifier.svg?style=flat)](https://www.npmjs.com/package/@webgap/notifier) # README This is the WebGAP Notifications Management module.<br/> Handles abstraction for multiple notification mechanisms. # Dependencies Handles most common object utilities with [**underscore.js**](https://github.com/jashkenas/underscore).<br/> Handles async implementation using [**async**](https://github.com/caolan/async).<br/> # API ## Installation ```bash npm install @webgap/notifier --save ``` ## Usage ```javascript var notifier = require('@webgap/notifier'); // create Email Notification System Class function EmailSystem() {} // EmailSystem must implement a notify function, doesn't matter how you implement it EmailSystem.prototype.notify = function (options, callback) { console.log('\nEmail system send message: ' + JSON.stringify(options.notification)); return callback(); }; // register the system notifier.register({name: 'EMAIL', instance: new EmailSystem(), defaultSystem: true}); ... // register as many systems as you want - notifier.register({name, instance, defaultSystem}); ... // create notification object var options = { notification: { userId: 'id of the user to notify', message: { key: 'messages.error.text', data: {errorCode: 111} } } }; // notify using the default system notifier.notify(options, function callback(err) { err && console.log(err); }); ... // notify using specific system(s) var options = { notification: { userId: 'id of the user to notify', message: { key: 'messages.error.text', data: {errorCode: 111} } }, notificationSystems: [notifier.systems.EMAIL, notifier.systems.OTHER] }; notifier.notify(options, function callback(err) { err && console.log(err); }); ... ``` ## Options Register method accepts the following options: ```javascript var options = { name: 'name', // the name of the system instance: new SomeClass(), // the instance with a notify method defaultSystem: true // true or false, defaults to false }; ``` Notify method accepts the following options: ```javascript var options = { notification: {}, // an object with the notification that the instance can handle notificationSystems: [SystemA, SystemB] // an array with the system you want to notify with, if none is provided the default will be used. If none an error is returned }; ``` # License Apache License, Version 2.0