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

nestjs-notifications

v0.1.17

Published

NestJs Notifications is a flexible multi-channel notification service inspired by Laravel Notifications designed for sending notifications across a variety of delivery channels.

Downloads

389

Readme

NestJs Notifications

NestJs Notifications is a flexible multi-channel notification service inspired by Laravel Notifications: https://github.com/illuminate/notifications

This module is designed for sending short informational messages across a variety of delivery channels that notify users of something that occurred in your application. For example, if you are writing a billing application, you might send an "Invoice Paid" notification to your users via email and SMS channels.

You can use pre-built delivery channels in this package, or you can create your own custom channels that can be easily integrated with this package.

Installation

$ npm install nestjs-notifications --save

Usage

To make use of this package, you will need a Notification and at least one Channel. You can create your own custom channels, or you can use pre-built ones that come with this package.

Example Channels and example Notifications are below.

Once this has been done, you can be trigger notifications to be sent like so:

import { HttpService, Injectable } from '@nestjs/common';
import { NestJsNotificationsService } from 'nestjs-notifications';
import { ExampleNotification } from 'xxxx';

@Injectable()
export class ExampleService {
  constructor(private readonly notifications: NestJsNotificationsService) {}

  /**
   * Send the given notification
   */
  public async send(): Promise<void> {
    const notification = new ExampleNotification();
    this.notifications.send(notification);
  }
}

Example Channel

All channels are resolved inside the IOC container, so you can import services, such as the HttpService as you need them.

import { HttpService, Injectable } from '@nestjs/common';
import { NestJsNotificationChannel, NestJsNotification } from 'nestjs-notifications';

@Injectable()
export class ExampleHttpChannel implements NestJsNotificationChannel {
  constructor(private readonly httpService: HttpService) {}

  /**
   * Send the given notification
   * @param notification
   */
  public async send(notification: NestJsNotification): Promise<void> {
    const data = this.getData(notification);
    await this.httpService.post(notification.httpUrl(), data).toPromise();
  }

  /**
   * Get the data for the notification.
   * @param notification
   */
  getData(notification: NestJsNotification) {
    return notification.toPayload();
  }
}

Example Notification

You can specify as many channels as you want to in the broadcastOn() function.

When constructing payloads, you can specify functions to create customised payloads for each channel, or fallback to the default toPayload() method.

You can also pass any data you need into the constructor of the notification to pass to the payload constructors.

import { Type } from '@nestjs/common';
import { HttpChannel, NestJsNotificationChannel, NestJsNotification } from 'nestjs-notifications';
import { CustomChannel } from './src/your-project/custom-channel';
import { EmailChannel } from './src/your-project/email-channel';

export class ExampleNotification implements NestJsNotification {

  /**
   * Data passed into the notification to be used when
   * constructing the different payloads
   */
  private data: any;

  constructor(data: any) {
    this.data = data;
  }

  /**
   * Get the channels the notification should broadcast on
   * @returns {Type<NestJsNotificationChannel>[]} array
   */
  public broadcastOn(): Type<NestJsNotificationChannel>[] {
    return [
      HttpChannel,
      CustomChannel,
      EmailChannel
    ];
  }

  toHttp() { }

  toCustom() { }

  toEmail() { }

  /**
   * Get the json representation of the notification.
   * @returns {}
   */
  toPayload(): any {
    return this.data;
  }
}

Channels Included in this Package

HttpChannel

The HttpChannel is designed to post data to an external URL/webhook. In order to utilise the channel correctly, you need specify the httpUrl() function and return the URL. You can then choose between using the standard toPayload() method or toHttp() method to the payload specifically for this channel.

You can implement the HttpNotification interface on your Notification Class to ensure you include the right methods.

HttpChannel Class

import {
  HttpService,
  Injectable,
  InternalServerErrorException,
} from '@nestjs/common';
import { HttpNotification, NestJsNotificationChannel } from 'nestjs-notifications';

@Injectable()
export class HttpChannel implements NestJsNotificationChannel {

  constructor(private readonly httpService: HttpService) {}

  /**
   * Send the given notification
   * @param notification
   */
  public async send(notification: HttpNotification): Promise<void> {
    const message = this.getData(notification);
    await this.httpService.post(notification.httpUrl(), message).toPromise();
  }

  /**
   * Get the data for the notification.
   * @param notification
   */
  getData(notification: HttpNotification) {
    if (typeof notification.toHttp === 'function') {
      return notification.toHttp();
    }

    if (typeof notification.toPayload === 'function') {
      return notification.toPayload();
    }

    throw new InternalServerErrorException(
      'Notification is missing toPayload method.',
    );
  }
}

HttpNotification Interface

import { NestJsNotification } from 'nestjs-notifications';

export interface HttpNotification extends NestJsNotification {
  /**
   * Define the Http url to send the notification to
   * @returns {string}
   */
  httpUrl(): string;

  /**
   * Get the Http representation of the notification.
   * @returns {any} http payload data
   */
  toHttp?(): any;
}

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Collaborating

Would appreciate any support anyone may wish to offer. Please get in contact.

License

NestJs Notifications is MIT licensed.