Utils module

This module contains a bunch of utility methods to be used in the application.

loadModuleFromFile

Loads a module from a file with require. It asumes that is a Microbase module so it initializes it with module(base) if it's a function, or new module(base) if it's a class.

const module = base.utils.loadModuleFromFile('/afolder/test.js');
console.log(module);

In any case it returns an object with the created module and the resolver file.

{ module: [Function],
  filePath: '/afolder/test.js' }

If the filename starts with a dot, the file is relative to the application root.

const module = base.utils.loadModuleFromFile('./afolder/test.js');
console.log(module);
{ module: [Function],
  filePath: '/projectFolder/afolder/test.js' }

It also support config variables in the argument.

const module = base.utils.loadModuleFromFile('${basepath}/afolder/test.js');
console.log(module);
{ module: [Function],
  filePath: '/projectFolder/node_modules/microbase/afolder/test.js' }

loadModulesFromFolder

Utilizes loadModuleFromFile with all *.js files in a folder. Returns an array with the modules loaded.

const modules = base.utils.loadModulesFromFolder('./afolder');
console.log(module);
[ { module: [Function],
    filePath: '/projectFolder/afolder/test.js' },
  { module: [Function],
    filePath: '/projectFolder/afolder/anotherTest.js' } ]

loadModuleFromKey

Loads a module with loadModuleFromFile, but taking the filename from a configuration key.

Given this config file:

{
  "myapp": {
    "mymodulelocation": "./afolder/test.js"
  }
}
const module = base.utils.loadModuleFromKey('myapp:mymodulelocation');
console.log(module);
{ module: [Function],
  filePath: '/projectFolder/afolder/test.js' }

loadModulesFromKey

Utilizes loadModuleFromKey to load all modules from an object in a configuration key.

Given this config file:

{
  "mymoduleslocation": {
    "filea": "./afolder/test.js",
    "fileb": "./afolder/anotherTest.js"
  }
}
const module = base.utils.loadModuleFromKey('mymodulelocation');
console.log(module);
[ { module: [Function],
    filePath: '/projectFolder/afolder/test.js' },
  { module: [Function],
    filePath: '/projectFolder/afolder/anotherTest.js' } ]

Error

Returns an object representingg an error to be returned in a service call. The first argument is the error code, and the second the associated data.

if (response.ok === false) 
  throw base.utils.Error('tax_not_found', tax.id, true);
}

genericResponse

Returns an object representing a response to be returned in a service call. The first argument is the payload and the second the error, if any.

If there is an error the property ok will be set to false otherwise it will be set to true.

{ "ok": true,
  "methods":
   [ { "id": "S1C4E9Jfi0f",
       "title": "UPS Same Day",
       "taxCode": "default",
       "rateId": "ryT4Nq1Go0G"
     } 
   ] 
}
{ 
  "ok": false,
  "error": "validation_error",
  "data": [ "payload: should have required property 'lastName'" ] 
}

Chain

Chain of Responsibility implementation.

Each module in the chain returns a function that receives a shared context and a next function to continue to the next module:

module.exports = (base) => {
  return (context, next) => {
    if (context.newData.id !== '99') {
      return next()
    }
    return next(base.utils.Error('code_invalid', context.newData.id));
  };
}

To instantiate and execute the chain, you need to use a config key whith the ordered list of the modules to be execute:

const createProductChain = new base.utils.Chain(base).use('createProductChain');
const ctx = {
  newData: {
    id: '001'
  }
};
this.createProductChain
  .exec(ctx)
  .then(() => reply(this.base.utils.genericResponse({ product: ctx.savedProduct.toClient() })))
  .catch(error => reply(this.base.utils.genericResponse(null, error)));

The config key contains the filenames of the modules:

"createProductChain": {
  "checkPrices": "./operations/chains/createProduct/checkPrices",
  "checkCategories": "./operations/chains/createProduct/checkCategories",
  "checkClassifications": "./operations/chains/createProduct/checkClassifications",
  "checkVariants": "./operations/chains/createProduct/checkVariants",
  "saveProduct": "./operations/chains/createProduct/saveProduct",
  "postSaveProduct": "./operations/chains/createProduct/postSaveProduct"
}

Evaluator

JSON evaluator with pluggable functions.

Take a look at the evaluator example in the examples folder or dig into the micro-promotions-service ecomm service.

const rulesEvaluator = new base.utils.Evaluator(base).use('promotions:default:rules');
const result = rulesEvaluator.evaluate(context, opContext, 0, context.promotion.if);