Services module

WORK IN PROGRESS

The module use the Hapi framework to expose and call the service operations.

Use

You must register your operation in the framework with the add or the addModule methods.

add method

const operation = {
  name: 'set',
  schema: require('myJsonSchema'),
  handler: (msg, reply) => {
    // Do something
    reply();
  }
};

base.services.add(operation);

The framework creates a Hapi route with the following details:

  • method: ['GET', 'POST', 'PUT']
  • path: The path is built with the concatenation of the following data:

    • services base path: base.config.get('services:path') Default: /services
    • service name: base.config.get('services:name')
    • service version: base.config.get('services:version')
    • operation name: the provided name

    In example: /services/cart/v1/new

  • handler: The provided handler

  • config: The operation is also configured with the ratify json schema validator
    • schema: the schema provided

addModule method

If all your operations are inside a module, you can add all of them at once:

function cartService(base) {
  const new = {
    name: 'set'
    ...
  };
  const get = {
    name: 'get'
    ...
  };
  return [set, get]
}
const cartFactory = require('./modules/cartService');
base.services.addModule(stockFactory(base));

Calling another service

You can call another microbase services using the call method

base.services.call('stock:reserve', {
  productId,
  quantity,
  warehouse,
  reserveStockForMinutes
}).then(response => {
  // Do something
});
  • If there is only one word in the name of the service to call (ie: 'get' ), or the name of the service is the same as the name of the current service (ie 'cart:get') it's asumed as an internal call (an operation hosted in the same application) and not an http connection.
  • If the service name is not the one defined for this application (ie: 'stock:reserve')
  • If you want to specify the service version to call, put the version after the service name (ie 'stock:v2:reserve'). The default is v1.