Flexible async IOC dependency injection container for javascript
installation
Install using npm
npm install krate
quick start
configure krate.js
All krate.js dependencies are injectable via the core configure
function.
This allows you to use your own Promise library, for example.
const Krate =
create a container
Your container will be used to define and resolve your app's components.
const container =
define some components
Each component is defined using a factory
that can be synchronous or asynchronous.
Synchronous factories can return a value or a promise, while async factories will be passed
a hybrid node.js-style callback/deferred resolver.
container
define components with dependencies
In addition to a factory, each component may define their depends
. These will be resolved to an object and injected as the first parameter of the factory. Basic depends can be defined as strings (with "as" aliases), arrays of strings, or objects with keys representing aliases, and values representing dependency names.
container container container
define complex dependencies with a reducer
For more complex scenarios, you can use a function to reduce the array of all defined component keys into an array of strings to be resolved as your component's depends
.
For example, we can use a reducer to build an index of all components starting with "combos/".
container
resolve components
You can resolve your components using the same format you used for defining
your component's depends
. The resolve
method returns a promised object
with each of your resolved components, keyed by their name or their provided alias.
container