koa-container
Dependency injection container for koa
Last updated a year ago by alxarch .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install koa-container 
SYNC missed versions from official npm registry.

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.

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |