koa-container

0.2.0 • Public • Published

koa-container

Dependency injection container with twist of koa-compose

Extends Jimple

Usage

 
var Container = require('koa-container');
 
var c = new Container();
 
c.register({
    register: (c) => {
        c.set('foo', 'bar');
    },
    run: (c) => {
        return function *(next) {
            console.log('A');
            yield next;
            console.log('B');
        }
    }
});
c.register({
    register: (c) => {
        c.set('foo', 'baz');
    },
    run: (c) => {
        return function *(next) {
            console.log('C');
            yield next;
            console.log('D');
        }
    }
});
 
c.run().then(() => {
    console.log('done');
});
 
// Will log
// > A
// > C
// > D
// > B
// > done
 

Differences with Jimple API

container.matchKeys(pattern, values)

Use path-to-regexp to match keys.

Arguments

  • pattern: A key pattern
  • values: If true, results will also have a value key set to container.get(key)
cnt.matchKeys('foo.:bar.:baz?');

Returns an Array of objects with keys:

  • key: key that matched
  • params: key parameters as matched by pattern
  • value(optional): If values is true the result of container.get(key)

container.setdefault(key, value)

Similar to Python's dict.setdefault(key, value) method. It gets the value of a key or sets it to value if not defined.

Package Sidebar

Install

npm i koa-container

Weekly Downloads

1

Version

0.2.0

License

MIT

Last publish

Collaborators

  • alxarch