1.0.3 • Published 10 years ago

be-utils v1.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

NPM version NPM downloads

Installation

npm install --save be-utils
// Then include in a node.js file
var utils = require('be-utils');

Contributing

Clone the repo, then

npm install
node_modules/typings/dist/bin.js install 

and here we go. Develop you new features or fixes, test it using npm test and create a pull request.

Features

Error Class and Interface

utils.Error and utils.IError (TypeScript Interface)

Schema

{
    code: number,
    name: string,
    message: string,
    key?: string,
    innerError?: any
}

Instanciate

var error = new Error(123, 'mySpecialError', 'something went terribly wrong', 'MyClass.myMethod', err)

Http-Request

utils.HTTPRequest

Usage

var request = new utils.HTTPRequest('GET', 'http://mycrazydomain.com/path/to/endpoint/', { 'Content-Type': 'application/json'}, '{ "testkey": "test payload" }');
request.send(function(error, result) {
    if (error) {
        // Handle Error
    } else {
        // handle http incoming message
        if (result.body) {
            // handle response body
        }
    }
});

Logger

utils.LoggerFactory or utils.getWinstonLogger()

Usage

var config = {
    baseDir: '/path/to/logs',
    console: {
        logLevel: 'debug'
    },
    file: {
        logLevel: 'info',
        executionFile: 'server.log',
        exceptionsFile: 'error.log'
    }
};
var factory = new utils.LoggerFactory(config);
var logger = factory.createWinstonLogger();

OR

var logger = utils.getWinstonLogger(config);

Utils

Helper.fileExists()

var exists = utils.fileExists('./path/to/file.ext');

Helper.regexReplaceInFile()

utils.regexReplace('./path/to/file.ext', /string\s*to\s*be\s*replaced/ig, 'replacement string');

Helper.unsetEmptySubObjects()

// The following should result in { b: { c: 'value' } }
let prunedObject = utils.unsetEmptyObjects({
    a: '',
    b: {
        a: {},
        b: null,
        c: 'value'
    }
}); 

TypeScript Hints

The following typings might need to be installed using tsd install <package> --save, to ensure typing convenience while programming:

  • lodash
  • mocha
  • mongoose
  • request
  • winston
  • node

Combined: tsd install lodash mocha mongoose request winston node --save

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago