Dependency injection Awilix 2 helpers and scope-instantiating plugin for hapi
Based directly on awlix-express
npm install --sae awilix-hapi
or
yarn add awilix-hapi
Requires Node v6 or above
Decorate your hapi server
const { createContainer, Lifetime } = require('awilix');
const { scopePerRequest } = require('awilix-hapi');
const container = createContainer();
container.registerClass({
// Scoped lifetime = new instance per request
// Imagine the TodosService needs a `user`.
// class TodosService { constructor({ user }) { } }
todosService: [TodosService, Lifetime.SCOPED]
});
container.registerValue({ theAnswer: 42 });
// Decorate the request, passing it your Awilix container
// This will attach a scoped container on each request
server.decorate('request', scopePerRequest(container), { apply: true });
// `onRequest` occurs prior to routing
server.ext('onRequest', function(request, reply) {
request.container.registerValue({
user: request.user // Some value in the request
})
reply.continue()
});
Then in your route handlers...
const { inject } = require('awilix-hapi');
// `inject` accepts multiple parameters, not an array
server.route({
method: 'GET'.
path: '/todos',
config: {
pre: [ inject('todosService', 'theAnswer') ]
},
handler: function(request, reply) {
request.pre.todosService.find().then((result) => {
reply({
result,
answer: request.pre.theAnswer
});
});
}
});
Register the plugin
const { createContainer, Lifetime } = require('awilix');
const { scopePerRequest } = require('awilix-hapi');
const container = createContainer();
container.registerClass({
// Scoped lifetime = new instance per request
// Imagine the TodosService needs a `user`.
// class TodosService { constructor({ user }) { } }
todosService: [TodosService, Lifetime.SCOPED]
});
container.registerValue({ theAnswer: 42 });
const awilixHapi = require('awilix-hapi')
console.log(awilixHapi)
server.register({
register: ,
options: {
container,
register: function(request, reply) {
request.container.registerValue({
user: request.user // Some value in the request
})
reply.continue()
}
}
});
Then in your route handlers...
server.route({
method: 'GET'.
path: '/todos',
config: {
pre: [ server.inject('todosService', 'theAnswer') ]
},
handler: function(request, reply) {
request.pre.todosService.find().then((result) => {
reply({
result,
answer: request.pre.theAnswer
});
});
}
});