GithubHelp home page GithubHelp logo

eduardoboucas / notification-bus Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 0.0 91 KB

๐ŸšŒ A Node.js library for loading and rendering notifications from a remote API

Home Page: https://www.npmjs.com/package/notification-bus

TypeScript 100.00%

notification-bus's Introduction

๐ŸšŒ Notification Bus

A Node.js library for loading and rendering notifications from a remote API.

Installation

To install notification-bus in your project, run:

npm install notification-bus

The API is distributed as a Netlify Function, although you should be able to change the implementation to fit any serverless function provider with support for Node.js.

For a one-click deploy to Netlify, use the button below.

Deploy to Netlify

Usage

Client

To use notification-bus, you first need to create an instance of the library and set the API endpoint for loading notifications.

import { NotificationBus } from "notification-bus"

const bus = new NotificationBus({
  name: "my-awesome-cli",
  version: "1.0.0",
  url: "https://notification-bus.netlify.app"
})

Once you have an instance, you can show all notifications relevant to your application using the render function.

bus.render()

This uses the default renderer to show notifications using the format below.

Screenshot 2023-02-07 at 00 01 31

If you want to use your own renderer, you can supply a renderer property.

bus.render({
  renderer: (items) => {
    console.log('Notifications:', items)
  }
})

You can also get direct access to the notification objects and process them in any way you like, using the getItems function.

const items = await bus.getItems()

console.log(items)

Server

To create new notifications, edit the data/events.json file and add entries to the array. Each entry can have the following properties:

  • body (required): The body text of the notification
  • title (optional): The title of the notification
  • severity (optional): A severity level for the notification (1: critical, 2: warning, 3: information), which will influence the way the notification is rendered
  • display_interval (optional): If set, defines the minimum interval at which a notification can be shown; for example, if set to "30 min", that notification will be shown at most once every 30 minutes
  • inputs (optional): A list of inputs that should be sent in the render/getItems call for the notification to match (see Inputs)
  • min_app_version (optional): The minimum version of the app required for the notification to match
  • max_app_version (optional): The maximum version of the app required for the notification to match
  • min_node_version (optional): The minimum version of Node.js required for the notification to match
  • max_node_version (optional): The maximum version of Node.js required for the notification to match
  • arch (optional): A list of CPU types (in the format of process.arch) required for the notification to match
  • platform (optional): A list of operating systems (in the format of process.platform) required for the notification to match

For more information about each property, see their type definition.

Configuration options

The NotificationBus constructor accepts an options object with the following properties:

  • url (required): The base URL of the API endpoint for loading notifications
  • name (required): The name of the application consuming the notifications
  • version (required): The version of the application consuming the notifications (must follow semver)
  • cachePath (optional, default: OS-specific location): The path to the local file where notification data will be cached
  • fetchInterval (optional, default: 30 minutes): How often to poll the remote API for new notifications (value in milliseconds)
  • renderer (optional, default: built-in renderer): Custom function to display notifications in the terminal

Inputs

Both the getItems and the render functions accept an optional inputs option. This is a great way to display messages only when certain events happen in your application (e.g. when a specific command of your CLI is used).

If an event has an inputs array defined, it will be discarded if the getItems/render call doesn't have an inputs value with at least one matching entry.

For example, imagine the following event:

{
  "title": "Deprecated API",
  "body": "The version of the `foobar` API you're using has been deprecated and will stop working with the next release.\n\nPlease visit https://example.com for more information.",
  "inputs": ["command:one", "command:three"]
}

And the following render calls:

// The event will not be rendered
bus.render({
  inputs: new Set("command:two")
})

// The event will be rendered
bus.render({
  inputs: new Set("command:one")
})

notification-bus's People

Contributors

eduardoboucas avatar

Stargazers

Michael Rory avatar Rafael Barbosa avatar Andrejs Agejevs avatar Sharmila S avatar  avatar Nick Taylor avatar

Watchers

Nick Taylor avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.