unoti
TypeScript icon, indicating that this package has built-in type declarations

0.5.1 • Public • Published

GitHub Workflow Status: CI Version Coverage License

uNoti

Unified Notification

Features

  • Flexible Interface: Can be used for any type of notification channels, e.g.: Email, SMS, Push Notification etc.
  • Supports Template: Can be used with any template engines, e.g.: pug, ejs, mjml etc.
  • Sending Strategy: Can use custom strategy to send notification using providers.
  • Lightweight: Gives you flexible system to build upon rather than trying to handle everything itself.

Installation

# using yarn:
yarn add unoti

# using npm:
npm install --save unoti

Usage

SMS Notification

import { NotiChannel, fallbackStrategy } from 'unoti'

type SMSParams = {
  to: string
  text: string
}

type SMSNotiProvider = import('unoti').NotiProvider<SMSParams>

const smsProviderOne: SMSNotiProvider = {
  id: 'sms-provider-one',
  send: async (params) => {
    let id
    // call the SMS provider service using `params` values
    return {
      id,
    }
  },
}

const smsChannel = NotiChannel<SMSParams>({
  id: 'sms',
  providers: [smsProviderOne],
  strategy: fallbackStrategy,
})

smsChannel
  .send({ to: '42', text: 'Hello World!' })
  .then((response) => console.log(response))
  .catch((err) => console.error(err))

SMS Notification with Template

import path from 'path'
import pug from 'pug'
import { NotiTemplate, renderRaw } from 'unoti'

type NotiTemplateRenderer = import('unoti').NotiTemplateRenderer

const pugRenderer: NotiTemplateRenderer = async (
  templatePath,
  data,
  options = {},
) => {
  const content = pug.renderFile(templatePath, { ...data, ...options.pug })
  return Promise.resolve(content)
}

const notiTemplate = NotiTemplate({
  path: path.resolve('templates'),
  data: {},
  options: {
    pug: {
      cache: true,
    },
  },
  renderer: {
    pug: pugRenderer,
    txt: renderRaw,
  },
})

type TemplateConfig<T extends string> = {
  topic: T
  data?: Record<string, any>
  options?: Record<string, any>
}

type Topic = 'hi' | 'bye'

type SendSMSOptions = {
  to: SMSParams['to']
  text?: SMSParams['text']
  template?: TemplateConfig<Topic>
}

function sendSms({ to, text = '', template }: SendSMSOptions) {
  const params: SMSParams = {
    to,
    text,
  }

  if (options.template) {
    params.text = await notiTemplate.render(
      { channel: 'sms', topic: template.topic, param: 'text' },
      template.data,
      template.options,
    )
  }

  return smsChannel.send(params)
}

sendSms({
  to: '42',
  template: { topic: 'hi', data: { name: 'John Doe' } },
})
  .then((response) => console.log(response))
  .catch((err) => console.error(err))

Templates Directory:

|--templates
|  |--sms
|  |  |--hi
|  |  |  |--text.pug
   |  |--bye
      |  |--text.txt

License

Licensed under the MIT License. Check the LICENSE file for details.

Readme

Keywords

Package Sidebar

Install

npm i unoti

Weekly Downloads

107

Version

0.5.1

License

MIT

Unpacked Size

44.8 kB

Total Files

24

Last publish

Collaborators

  • muniftanjim