Comparing version 0.3.2 to 0.4.0
66
index.js
const { DepGraph } = require('dependency-graph'); | ||
const { DepGraph } = require('dependency-graph'); | ||
const Type = require('./lib/type'); | ||
const ExpressConfig = require('./lib/express-config'); | ||
const { OverwriteError } = require('./lib/error'); | ||
const DEFAULT_NAMESPACE = 'yaldic'; | ||
function validateNode(node) { | ||
if (typeof node === 'string') | ||
throw new TypeError('Dependency node cannot be a string'); | ||
function throwOverwriteError(name) { | ||
throw new Error(`Cannot overwrite already existing node: ${name}`); | ||
} | ||
return node; | ||
function throwExpressNamespaceExistsError(namespace) { | ||
throw new Error(`Cannot overwrite already existing namespace: ${namespace}`); | ||
} | ||
function Yaldic({ allow_overwrite = false } = {}) { | ||
@@ -26,6 +25,11 @@ | ||
register: (name, node) => { | ||
register: (name, node, type = Type.VALUE) => { | ||
validateNode(node); | ||
node.$type = type; | ||
if (graph.hasNode(name)) { | ||
if (!allow_overwrite) throwOverwriteError(name); | ||
if (!allow_overwrite) OverwriteError.throw(name); | ||
@@ -47,22 +51,48 @@ graph.setNodeData(name, node); | ||
Yaldic.express = function(config = {}) { | ||
Yaldic.autoInject = function autoInject(context, container) { | ||
let { namespace, allow_overwrite, container } = config; | ||
return { | ||
register: container.register.bind(container) | ||
, get: (name) => { | ||
if (!namespace) namespace = DEFAULT_NAMESPACE; | ||
if(!container) container = Yaldic({ allow_overwrite }); | ||
const node = container.get(name); | ||
return typeof node === 'function' ? node(context) : node; | ||
} | ||
}; | ||
}; | ||
/** | ||
* Configuration: | ||
* | ||
* - namespace: This will be the `req` object attachment point. | ||
* - allow_overwrite: | ||
* Are you allowed to re-assign node values? (default = no) | ||
* - container: Your very own yaldic container | ||
*/ | ||
Yaldic.express = function(settings = {}) { | ||
const { | ||
namespace | ||
, container | ||
, auto_inject_req | ||
} = ExpressConfig(Yaldic, settings); | ||
return function(req, res, next) { | ||
if (req[namespace]) throwExpressNamespaceExistsError(namespace); | ||
if (req[namespace]) OverwriteError.throw(namespace); | ||
req[namespace] = container; | ||
req[namespace] = auto_inject_req ? | ||
Yaldic.autoInject(req, container) : container; | ||
return next(); | ||
} | ||
}; | ||
} | ||
Yaldic.Type = Type; | ||
module.exports = Yaldic; |
{ | ||
"name": "yaldic", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "Yet Another Lightweight Dependency Injection Container", | ||
@@ -49,3 +49,4 @@ "main": "index.js", | ||
"chai": "^3.5.0", | ||
"mocha": "^2.5.3" | ||
"mocha": "^2.5.3", | ||
"sinon": "^1.17.5" | ||
}, | ||
@@ -52,0 +53,0 @@ "dependencies": { |
/* eslint-env node, mocha */ | ||
const expect = require('chai').expect; | ||
const sinon = require('sinon'); | ||
const yaldic = require('../index'); | ||
@@ -29,2 +30,12 @@ | ||
it('should throw an error when trying to store a string', () => { | ||
const container = yaldic(); | ||
const test = () => container.register('foo', 'bar') | ||
expect(test).to.throw(/Dependency node cannot be a string/); | ||
}); | ||
it('should throw an error by default when attempting to overwrite a ' + | ||
@@ -35,9 +46,9 @@ 'node with a new value', () => { | ||
container.register('foo', 'bar'); | ||
container.register('foo', { bar: 'baz' }); | ||
const intermediate = container.get('foo'); | ||
expect(intermediate).to.eql('bar'); | ||
expect(intermediate.bar).to.eql('baz'); | ||
const test = () => container.register('foo', 'baz'); | ||
expect(test).to.throw(/Cannot overwrite already existing node: foo/); | ||
const test = () => container.register('foo', { bar: 'buzz'}); | ||
expect(test).to.throw(/Cannot overwrite already existing namespace foo/); | ||
@@ -47,14 +58,14 @@ }); | ||
it('should not allow an overwrite when configured to do so', () => { | ||
it('should allow an overwrite when configured to do so', () => { | ||
const container = yaldic({ allow_overwrite : true }); | ||
container.register('foo', 'bar'); | ||
container.register('foo', { bar: 'baz' }); | ||
const intermediate = container.get('foo'); | ||
expect(intermediate).to.eql('bar'); | ||
expect(intermediate.bar).to.eql('baz'); | ||
container.register('foo', 'baz'); | ||
container.register('foo', { bar: 'buzz' }); | ||
const actual = container.get('foo'); | ||
expect(actual).to.eql('baz'); | ||
expect(actual.bar).to.eql('buzz'); | ||
@@ -65,2 +76,61 @@ | ||
describe('node.$type', function() { | ||
it('should store the node type as Type.VALUE by default', () => { | ||
const plugin = yaldic(); | ||
plugin.register('foo', {}); | ||
const foo = plugin.get('foo'); | ||
expect(foo.$type).to.eql(yaldic.Type.VALUE); | ||
}); | ||
}); | ||
describe('Yaldic.autoInject', function() { | ||
it('should wrap the register method', () => { | ||
const container = yaldic.autoInject(null, yaldic()); | ||
container.register('foo', { bar: 'buzz' }); | ||
const node = container.get('foo'); | ||
expect(node.bar).to.eql('buzz'); | ||
}); | ||
it('should inject the given context into a stored function', () => { | ||
const context = { foo: 'bar' }; | ||
const container = yaldic.autoInject(context, yaldic()); | ||
const node_stub = sinon.stub().returns('blah'); | ||
container.register('test', node_stub); | ||
const node = container.get('test'); | ||
expect(node).to.eql('blah'); | ||
expect(node_stub.calledOnce).to.be.true; | ||
expect(node_stub.calledWith(context)).to.be.true; | ||
}); | ||
it('should just return an object value', () => { | ||
const context = { foo: 'bar' }; | ||
const container = yaldic.autoInject(context, yaldic()); | ||
container.register('blah', { boo: 'bah' }); | ||
const node = container.get('blah'); | ||
expect(node.boo).to.eql('bah'); | ||
}); | ||
}); | ||
describe('Yaldic.express', function() { | ||
@@ -128,6 +198,6 @@ | ||
req.yaldic.register('foo', 'bar'); | ||
req.yaldic.register('foo', 'baz'); | ||
req.yaldic.register('foo', { foo: 'bar' }); | ||
req.yaldic.register('foo', { foo: 'baz' }); | ||
expect(req.yaldic.get('foo')).to.eql('baz'); | ||
expect(req.yaldic.get('foo').foo).to.eql('baz'); | ||
@@ -143,3 +213,3 @@ done(); | ||
const mydic = yaldic(); | ||
mydic.register('foo', 'bar'); | ||
mydic.register('foo', { bar: 'baz' }); | ||
@@ -150,3 +220,3 @@ const plugin = yaldic.express({ container: mydic }); | ||
plugin(req, {}, () => { | ||
expect(req.yaldic.get('foo')).to.eql('bar'); | ||
expect(req.yaldic.get('foo').bar).to.eql('baz'); | ||
done(); | ||
@@ -157,4 +227,24 @@ }); | ||
it('should auto-inject the request object if the user sets the ' + | ||
'`auto_inject_req` setting to a truthy value', (done) => { | ||
const testdic = yaldic(); | ||
testdic.register('foo', (req) => req.bar); | ||
const plugin = yaldic.express({ | ||
container: testdic | ||
, auto_inject_req: true | ||
}); | ||
const req = { bar: 'success' }; | ||
plugin(req, {}, () => { | ||
expect(req.yaldic.get('foo')).to.eql('success'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13924
13
304
3
1