digs-common
A moatload of common objects and functions shared by various Digs-related packages
Usage
const common = ;const _ = commonutils; // `lodash` with some mixinsconst Promise = commonPromise; // `bluebird`const validator = commonvalidator; // `joi`const define = commondefine; // `stampit`const definitions = commondefinitions; // definitions ("stamps") // example of a mixin (from package `slug`)_ // 'hello-world'
API
Definitions
A "definition" is simply a "stamp". These are provided for use in plugins. They ease the addition of common functionality to any object.
const definitions = definitions;
The following factory functions ("stamps") can be combined in any way you can think of to create new objects.
DigsObject
The DigsObject
factory accepts a required second parameter, which is a Digs
instance. Of course, this is simply a Hapi
Server
.
Logging Functionality
The definition provides the following logging methods (configurable), for convenience:
error(tags, data)
warn(tags, data)
info(tags, data)
debug(tags, data)
ok(tags, data)
Each of these is an alias to
Server#log
. The "level"
used will be added as a tag (in addition to the namespace
and project
tags, and any other tags passed). For example:
const digs = ; ;
Additional tags must be specified as an Array
. This allows you to simply:
obj;
if no extra tags are necessary.
A log()
method is also available which bypasses the "level":
obj;// -> digsServer.log(['digs', 'home', 'baz'], 'Quux!');
Custom Logging Functions
To configure the "levels", provide a logColors
object when instantiating:
const obj = ;
Each color must be a valid chalk color.
Server Method Passthroughs
An object created with DigsObject()
also has the following methods which pass
through to the Server
instance:
expose()
register()
method()
handler()
route()
decorate()
Currently, this list is fixed, but it may be changed to support all Server
methods available in a plugin context.
DigsEmitter
DigsEmitter
is a handy way to add EventEmitter
functionality to any object.
The following code:
const obj = ;
is roughly equivalent to:
{ this; }; const obj = ;
See the stampit docs for more info.
DigsFSM
With DigsFSM
, any object can be composed into a Promise-driven finite state
machine.
const fsm = DigsFSM ; // triggers onbar() above; changes state to "baz"fsm ;
The Promise implementation used is bluebird.
See the fsm-as-promised docs for more information.
DigsIdentifier
DigsIdentifier
provides a way to identify objects. Each object is assigned
a unique identifier (property id
) if an identifier is not provided.
In addition, any definition composed from DigsIdentifier
can identify itself,
creating string representations (via Object.prototype.toString()
) convenient
for logging.
const HotDog = DigsIdentifier ; const hotDog = ;`: I need mustard.`; // HotDog<HotDog#1>: I need mustard.hotDogid; // HotDog#1 const anotherHotDog = ;`: I need ketchup.`; // HotDog<HotDog#2>: I need ketchup.anotherHotDogid; // HotDog#2 const bratwurst = ;`: I need kraut.`; // HotDog<bratwurst>: I need kraut.bratwurstid; // bratwurst
DigsParamValidator, DigsPrevalidator & DigsInitValidator
TODO; general idea is that method parameter, object state upon method call, and instantiation can all be easily validated via Joi.
Author
License
MIT