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

express-notification

v2.1.0

Published

express notification system to send notification easer

Downloads

8

Readme

express-notificaiton

express notification system

you can send notification with diffrent channels

Installation

first of all you must install the package

run this command first :

npm i express-notification

How To Use

  • you can see ./example directory for a real example

Step One : Register Express Notification Middleware

after you install the package you can require express notification and then you must register a middleware for the package

const express = require('express')
const app = express()
const notification = require('express-notification');

// define config of notification package
const notificationConfig = {
    config : {}
}


// register notification system
app.use(notification.register(notificationConfig))

Step Two: Config of Default Channels

list of default channels:

  1. mail channel

you have one channel by defualt named 'mail channel', you must set config to use it

// define config of notification package
const notificationConfig = {
    config : {
        mail : {
            // you can config your smtp server
            host: 'smtp.mailtrap.io',
            port: 2525,
            auth: {
              user: '',
              pass: '',
            }
        }
    }
}

// register notification system
app.use(notification.register(notificationConfig))

Step Three : You Must Define A Notification

now you must define a notification to send.

maybe in ./notifications/exampleNotification.js

class ExampleNotification {

    /**
     * return list of channels that you want to send notification to
     * @returns {string[]}
     */
    via() {
      return ['mail'];
    }
  
    /**
     *
     * @param {{ email : string }} notifiable
     * @returns {{subject: string, from: string, html: string, to}}
     */
    toMail(notifiable) {
      return {
        from : '[email protected]',
        to : notifiable.email,
        subject: 'hello world',
        html : `
            <h2>Hello World</h2>
            `
      }
    }
  }
  
  
  module.exports = () => new ExampleNotification()

Step Four : Send Notificaiton

now you can easily send notifition in Route Handler

const exampleNotification = require('./notifications/exampleNotification');

app.get('/', (req, res) => {
  // send notificaiton
  res.notify(exampleNotification()).to({ email : '[email protected]' })
  res.send('Hello World!')
});

Define a Custom Channel

you can define custom channels for sending your notifications

Step One : you must write the channel class

maybe in ./notifications/channels/smsChannel.js


class SmsChannel {
  // init MailChannel 
  constructor (config) {
    // check mail config is exsits in config
    if(! ('sms' in config)) {
      throw new Error('sms config is mixing')
    }
  }
  /**
   *
   * @param notifiable
   * @param {Notification} notification
   */
  async send(notifiable , notification ) {
    // this message comes from Notification classes
    let message = notification.toSms(notifiable);
    let { phoneNumber } = notifiable;

    // can require an api to send sms here
  }
}

module.exports = SmsChannel;

Step Two : you must register the custom channel

when you registered express notification middleware, you can set custom channels in notification config

// we could define the channel in everywhere we want 
const SmsChannel = require('./notifications/channels/smsChannel.js')

// define config of notification package
const notificationConfig = {
    config : {
        mail : {
            // you can config your smtp server
            host: 'smtp.mailtrap.io',
            port: 2525,
            auth: {
              user: '',
              pass: '',
            }
        },
        sms : {
            // sms channel config
        }
    },
    channels : {
        'sms' : SmsChannel
    }
}

// register notification system
app.use(notification.register(notificationConfig))

Step Three : You everywhere You want

you can use the channel in every notification class you want

class ExampleNotification {

    /**
     * return list of channels that you want to send notification to
     * @returns {string[]}
     */
    via() {
      return ['mail' , 'sms'];
    }
  
    /**
     *
     * @param {{ email : string }} notifiable
     * @returns {{subject: string, from: string, html: string, to}}
     */
    toMail(notifiable) {
      return {
        from : '[email protected]',
        to : notifiable.email,
        subject: 'hello world',
        html : `
            <h2>Hello World</h2>
            `
      }
    }

    toSms(notifiable) {
        return {
            message : 'hello roocket'
        }
    }
  }
  
  
  module.exports = () => new ExampleNotification()