hapi-moleculer
Hapi plugin for the Moleculer Microservices Framework.
Install
npm i hapi-moleculer --save
or
yarn add hapi-moleculer
Usage
'use strict' const Hapi = ;const HapiMoleculer = ;const ServiceBroker = ; { // Create a server const server = host: 'localhost' port: 3000 ; // Register the plugin await server; // Starting server serverstart;};
API
Options
name
- (optional) Service name that will be used to create the context action names. Default toapi
.broker
- (optional) Either a ServiceBroker seeting object or a ServiceBroker instance. Default to{}
. Eg:
// Register the pluginawait server
aliases
- (optional) Array of alias objects where:-
method
- (optional) Either a string / array of strings representing the method name within (GET
,POST
,UPDATE
,DELETE
,PATCH
,OPTIONS
) or a single option within (REST
,*
). Use*
to match against any HTTP method.REST
will create all RESTful paths ( get, list, create, update and remove ) for the action. Default to*
. Eg.:method: 'REST' path: '/user' action: 'users'// same asmethod: 'GET' path: '/user/{id}' action: 'users.get'method: 'GET' path: '/user' action: 'users.list'method: 'POST' path: '/user' action: 'users.creare'method: 'PUT' path: '/user/{id}' action: 'users.update'method: 'DELETE' path: '/user/{id}' action: 'users.remove'To use REST shorthand alias you need to create a service which has
list
,get
,create
,update
andremove
actions. -
path
- (required) the absolute path used to match incoming requests ( must begin with '/' ). -
action
- (required) Moleculer action name. -
blacklist
- (optional) A list of actions that will be cut out from the RESTful paths. Valid values:list
,get
,create
,update
andremove
. Only valid ifmethod
isREST
. Eg.:
method: 'REST'path: '/user'action: 'users'// it will create just list, get and removeblacklist: 'create' 'update'routeOpts
- (optional) additional route options. When the method name is equal toREST
the routeOpts has all 5 RESTful options plus oneall
properties:method: 'POST'path: '/users/login'action: 'users.login'routeOpts:// route options heremethod: 'REST'path: '/user'action: 'users'routeOpts:all: // route options here },get: // route options here },list: // route options here },create: // route options here },update: // route options here },delete: // route options here },Assign values in the following order: inner default opts, routeOpts.all and routeOpts.[actionType] using lodash.assign.
-
Decorations
hapi-moleculer decorates the Hapi server and request with server.broker
and request.broker
which is the instance of the ServiceBroker created in the register function. It can be used to call the actions in the route handler, plugins, etc. Eg.:
const route = method: 'POST' path: '/user/logout' options: validate: headers: Joiobject authorization: Joi options allowUnknown: true { const token = requestauthcredentials; return requestbroker; };
Context
hapi-moleculer creates a Moleculer Context to wrap the call in every request. It uses the Hapi request lifecyle workflow to first create the Context in the onPreAuth
step and then set the params in the onPreHandler
step. The context can be accessed with request.ctx
. Hence, you can use it to set the ctx.meta
in the autencation step or any other step you need. Eg.:
"use strict"; const Bcrypt = ;const Hapi = ; const users = john: username: "john" password: "$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm" // 'secret' name: "John Doe" id: "2133d32a" ; const validate = async { const user = usersusername; if !user return credentials: null isValid: false ; const isValid = await Bcrypt; const credentials = id: userid name: username ; // Set the meta if the credentials are valid. if isValid requestctxmetacredentials = credentials; return isValid credentials ;}; const start = async { const server = Hapi; await server; await server; serverauth; server; await serverstart; console;}; ;
License
ISC