1.0.1 • Published 7 years ago
@krab/op v1.0.1
op
v1.0.0
We follow breaking.feature.fix versioning
npm install --save @krab/op
This module provides a building-block for domain logic.
Any software can be writtent using op in such a way that
all of the domain logic in the software will be completely unit-testable.
Usage given below.
Usage
import Op from '@krab/op'; // in projects with ES7 modules
const Op = require('@krab/op/common'); // in projects with common JS modules
const domainOp = new Op(({db, api}) => {
return db.getResults() // returns a promise
.then((dbResults) => api.processData(dbResults))
;
});
// OR
const domainOp = new Op(async ({db, api}) => {
const dbResults = await db.getResults();
const apiResults = await api.processData(dbResults);
return apiResults;
}, { // this param is optional
doc: 'Does somthing with db and api',
types: {
db: (db) => db instanceof Db,
api: (api) => api instanceof Api
}
});
domainOp.mount({db: new Db(config.db), api: new Api(config.api)});
const results = await domainOp.run();
domainOp.run().then((results) => {});
// OR
domainOp.run({db: new Db(config.db), api: new Api(config.api)});Features
- An
opis either async, or returns a Promise. - Failure to comply with
1.will result in a runtime-error. - An
opcan have adocstring attached to it. - An
opcan havetypesassociated with it, which will be used to validate the params supplied to theop. - An
opcan be run in 2 ways:op.run(params)orop.mount(params).run(). op.mountmounts a set ofparamsin the op, so thatop.runcan be called without any arguments.- An
opcan be cloned usingop.clone(). - An
op.ventis an event-aggregator on which the events'start','err','done', and'end'are emitted at appropriate times in the lifecyclt of theop.
Op Events
op uses @krab/vent(link). Each instance of class Op has a property vent, which is an event aggregator attached to that instance.
Usage of op.vent given below.
const op = new Op(...);
op.vent.on('start', ({op, params}) => {...});
op.vent.on('err', ({op, err, params}) => {...});
op.vent.on('done', ({op, result, params}) => {...});
op.vent.on('end', ({op, success, params}) => {...});