An adaptor for wrapping code in a format usable by Intravenous
See Adapting existing code for Intravenous
So, you have some sort of service that you want to use with Intravenous but its interface doesn't fit what is required by it. Here is an example.
// cakeService.js
'use strict';
exports = module.exports = class CakeService {
constructor(logger, cakeType) {
Assert(logger);
Assert(cakeType);
this.logger_ = logger;
this.cakeType_ = cakeType;
}
eat() {
this.logger_.info(`Nom, nom, nom - I love ${this.cakeType_} cake`);
}
get cakeType() {
return this.cakeType_;
}
};
In order to use it with Intravenous you would need to transpile the class to ES5
as Intravenous doesn't support ES6 classes. You would also need to add a
$inject
property. To do all this (and more) is what intravenous-code-adaptor
does.
Add the package to your application
npm install intravenous-code-adaptor --save
Then use it to create a wrapper
// cakeServiceWrapper.js
'use strict';
const CodeAdaptor = require('intravenous-code-adaptor');
const Cake = require('./cakeService');
exports = module.exports = CodeAdaptor(
['logger'],
['myLogger'],
(logger) => new Cake(logger, 'chocolate'),
['eat', 'cakeType']
);
and then add this service to your IOC container
// bootstrapper
'use strict';
const Intravenous = require('intravenous');
const Cake = require('./cakeServiceWrapper');
const container = Intravenous.create();
container.register('myLogger', console);
container.register('cake', Cake);
const cake = container.get('cake');
Please check the tests and the JSDocs in the code for more information.