hume utils
Some helpers shared among the rest of the packages.
Install
:coffee: Install Node.js v8 and then:
npm i @hume/utils
Use
:pencil: You can visit the complete example in this tests.
const utils = require('@hume/utils');
console.log('Version');
console.log(utils.version);
API
:eyes: Full specification. All Lodash methods are in the root, so you can call them directly, ie: utils.map()
.
version
The actual version of the library.
version
(string) - Version number.
validator
A wrapper for validator.js.
appEnv
The result of this method to parse the important info from Bluemix app environments (from the package "cfenv"). This package doesn't randomize the port, which is a problem to run all microserices locally in parallel. So we trick it in this library if the env var Q_MICRO
is set to true.
async error(msg, error, opts) -> id
To report errors to logger and APM (if enabled).
msg
(string) - Custom message explaining the error.error
(Error) - JavaScript error.opts
(object) - Optional parameters:
userId
(string) - The user ID generating the error. If you want to track also the user in the APM (default: null)custom
(object) - some arbitrary details (default: {})
async lb.getUserId(req) -> id
To get the user ID in LoopBack from a request object. To use outside HTTP APIs, there LoopBack has its own mechanism, use next point method instead.
req
(object) - A Loopback "request" object.id
(string) - The user identifierm null
if not found.
async lb.addUserId(model) -> null
To add the current user ID to the actual model when present. Thought to be used in the models with endpoints that need it. So, thanks to this LoopBack mechanism we have the field "currentUserId" once this is called.
model
(object) - A Loopback "Model" object.
async lb.createUser(app, opts) -> null
To create a first user and the proper roles.
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')
logger(name) -> log
A wrapper around Pino (with prettystream), but only the next 3 levels are allowed. If the environment variable NODE_ENV
value is production
, prettystream is disable due to performance reasons.
name
(string) - Project name to tag the messages. (default: 'app')log
(object) - Including next points methods.
log.error(msg, err, data)-> null
Function used to register errors in log. It acts as adapter of the function pino.error
.
msg
(string) - A message with the explanationerr
(object) - An error to register in the logdata
(object) - An object with additional data to include in error log entry
log.info(msg, data) -> null
Function used to log debug messages. It acts as adapter of the function pino.info
signature.Keep it to the minimal, because it's also printed in production and console.*
methods are sync operations.
msg
(string) - A message with the explanationdata
(object) - An object with additional data to include in info log entry
log.debug(msg, data) -> null
Function used to log debug messages. It acts as adapter of the function pino.debug
signature.
msg
(string) - A message with the explanationdata
(object) - An object with additional data to include in debug log entry
promise
Some promises related helpers.
promise
(object) - Including next points methods.
promise.map()
A wrapper around p-map
.
promise.mapSeries()
A wrapper around p-map-series
.
promise.eachSeries()
A wrapper around p-each-series
.
sleep()
A wrapper for system-sleep.
randomInt(min, max)
Returns a random integer between min (inclusive) and max (inclusive).
min
(number) - Lower limit.max
(number) - Higher limit.
requireDir(module)
A wrapper for node-require-directory.