bda-util

0.1.2 • Public • Published

bda-util

commonly used tools for bda projects, current version 0.1.0

Install

npm install bda-util

Test

npm test

Usage

Logger

An easy way to declare winston daily rotate logger based on local timezone

// option could be a string or an object, if option is a string, it is treated as option.filename

const logger = require('bda-util/winston-rotate-local-timezone').getLogger(option)

  • option
    • filename: base file name of the log file
    • datePattern: A string representing the pattern to be used when appending the date to the filename (default 'yyyy-MM-dd'). The meta characters used in this string will dictate the frequency of the file rotation. For example, if your datePattern is simply 'HH' you will end up with 24 log files that are picked up and appended to every day
    • prepend: Defines if the rolling time of the log file should be prepended at the beginning of the filename (default 'false')
    • localTime: A boolean to define whether time stamps should be local (default 'True' means that Local time will be used)
    • logstash: A boolean to define whether logstash format log will be used (default 'false')
    • padLevels: A boolean to define whether padding should be added to log message (default 'false')
    • handleExceptions: A boolean to define whether uncaught exceptions should be handled by logger instance (default env === 'development' ? false : true)
    • level: log level (default env === 'development' ? 'debug' : 'info')

Mailer

Sendmail

Exports a function that allows you to send email with little effort

// option defines basic configuration of the sender MX server, it can be left blank, which results in using default configuration

const sendMail = require('bda-util/mailer').sendmail(option);
const Html = require('bda-util/mailer').template.Html;
const Table = require('bda-util/mailer').template.Table;

let html = new Html();
let table = new Table();
table.setHeader(['name','sex','age','height','weight','intro'])
    .addRow(['mike','male','25','180','60','smart'])
    .addRow(['patrick','male','25','180','60','strong'])
    .addRow(['honor','male','25','180','60','slim'])

html.append(table)
    .append('<br><p>Just do it!</p>');

sendMail({
    from : "monitor@authuir.com", // 如果选择默认配置,必须用monitor@authuir.com
    to   : "cheng.qu@bda.com",
    cc   : "cheng.qu@bda.com",
    subject: `Test table template`,
    html : html.toString()
}, (err, res) => {
    if(err) {
        console.log('Send mail failed at %s, err %s', new Date(), err);
    } else {
        console.log('Message sent at %s', new Date());
    }
});
  • option
    • logger: Pass a logger instance to sendmail to enable logging
    • host: Host of sender MX server
    • port: Port of sender MX server
    • auth
      • user: Username of a particular user registered on the sender MX server
      • pass: Password of a particular user registered on the sender MX server

Template

Html

const Html = require('bda-util/mailer').template.Html;

// title defines the title of the html, defualt value is 'This is an Auto Generated Email' if left blank

let html = new Html(title);

html.setTitle('I'm the newly set title')

html.append('<p>I'm a paragraph</p>')
    .append('<p>I'm another paragraph</p>')

console.log(html.toString())

methods are listed as below:

  • setTitle(String title): Set title of the html
  • append(Object element): Element could be anything that has a toString method, all elements appended to the html will be rendered in order
  • toString(): Turn the html object into string

Table

const Html = require('bda-util/mailer').template.Table;

let table = new Table();

table.setHeader(['name','sex','age','height','weight','intro'])
    .addRow(['mike','male','25','180','60','smart'])
    .addRow(['patrick','male','25','180','60','strong'])
    .addRow(['honor','male','25','180','60','slim'])

console.log(table.toString())

methods are listed as below:

  • setHeader(List headers): Set headers of the table
  • addRow(List row): The lenth of row should be the same as that of header
  • toString(): Turn the table object into string

Readme

Keywords

Package Sidebar

Install

npm i bda-util

Weekly Downloads

1

Version

0.1.2

License

ISC

Unpacked Size

12.7 kB

Total Files

13

Last publish

Collaborators

  • darrenqc