/utils

Some common utils we use in our projects: Lodash, Bluebird, Bluemix logging/error reporting, debug ...

Primary LanguageJavaScriptMIT LicenseMIT

utils

Continuos integration NSP Status

Some common utils we use in our projects: Lodash, Bluebird, Bluemix logging/error reporting, debug ...

Install

We're not pushing it to npm because it's only for internal use.

npm i --save IBMResearch/utils

API

This library includes some full external libraries and another custom methods.

Libraries

Lodash

In the root we have all Lodash methods.

const utils = require('utils');

console.log(utils.map([1,2], (x) => x * 2));

require-directory

In the root we have also have the require-directory method.

const utils = require('utils');

const routes = utils.requireDir(module, './routes');

validator.js

The full validator.js object.

const utils = require('utils');

utils.validator.isEmail('foo@bar.com'); //=> true

Bluebird

Normally we prefer to use Node ES6/7 native stuff, but Bluebird gives us some useful (non-standard) methods like "Promise.map". Moreover LoopBack uses it as promise library. We also include the "bluebird-extra" library methods.

const Promise = require('utils').Promise;

const readFileP = Promise.promisify(require('fs').readFile);

Methods

debug(projectName, filePath) -> object

A wrapper around the debug module. It adds some stuff to print the tag to be consistent with LoopBack. The two options are related with it.

  • projectName (string) - The name of the project.
  • filePath (string) - The full path of the file.
const utils = require('utils');
const dbg = utils.debug(require('../package.json').name, __filename);

dbg('Starting with options', { host: '127.0.0.1', port: 7777 } );

info(message, obj) ->

A wrapper around "console.log" to print things that are not errors but we want them always printed, things we want to know that happened (ie: bad login). Keep it to the minimal, because it's also printed in production and "console.*" are sync operations. It prints using a Bluemix friendly format.

  • message (string) - Message to print.
  • obj (object) - Object with extra information. Optional.
const info = require('utils').info;

info('Starting with options', { host: '127.0.0.1', port: 7777 } );
info('End');

error(message, errObj) ->

A wrapper around "console.error" to print only critical errors. This way it's going to be correctly tagged in the IBM Monitoring and analytics addon for Bluemix.

  • message (string) - Message to print.
  • errObj (object) - Error object to include. Optional.
const error = require('utils').error;

const err = new Error('Simulating an error.')
error('User not found', err );

getAppEnv() -> object

A method to parse the important info from Bluemix app environments. Basically the "cfenv" result with some additions.

const appEnv = require('utils').getAppEnv();

// Provided by "cfenv".
console.log(appEnv.port);
console.log(appEnv.url);
console.log(appEnv.isLocal);

// Our additions.
console.log(appEnv.inBluemix);
console.log(appEnv.dbUris.mongo);
console.log(appEnv.dbUris.composeMongo);
console.log(appEnv.dbUris.composeElastic);
console.log(appEnv.dbUris.composeRethink);
console.log(appEnv.dbUris.cloudant]);

trickMongoUri(originalUri, dbName) -> string

A trim to the "composeMongo" field returned by the last method. The problem is that Compose includes the DB name (always "admin") in the URI, and we need to write to another DB.

  • originalUri (string) - Uri returned by method "getAppEnv". The one added in Bluemix as environment variable.
  • dbName (string) - Name of the database you want to use.
const utils = require('utils');
const appEnv = utils.getAppEnv();

console.log(utils.trickMongoUri(appEnv.dbUris.composeMongo, 'api-starter'));

loopback.getUserId(req) -> function

A method to get the user ID in LoppBack from a request object. Useful with this middleware.

  • req (object) - A Loopback "request" object.
...
const getUserId = require('utils').loopback.getUserId;

app.use(toDb(db, { geo: true, idFunc: getUserId, dbOpts: { type: 'elastic' } }));

loopback.createUser(app, opts) -> Promise

A method to get the user ID in LoppBack from a request object. Useful with this middleware.

  • app (object) - A Loopback "app" object.
  • opts (object) - An object with:
  • username (string) - Username of the user to create. (default: 'admin')
  • password (string) - Password of the user to create. (default: 'admin')
  • email (string) - Email of the user to create. (default: 'admin@admin@myapp.mybluemix.net')
...
utils.loopback.createUser(app, {
  username: 'test',
  password: 'test',
  email: 'test@api-starter.mybluemix.net',
})
.then(() => dbg('All user and roles related tasks finished.'))
.catch(err => utils.error('Creating the default user/roles', err));

Developer guide

Please check this link before a contribution.