Comparing version 3.0.1 to 3.1.0
{ | ||
"name": "diogenes", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "A dependency injection framework.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -280,2 +280,26 @@ Diogenes | ||
getMetadata | ||
------------ | ||
It returns the metadata of all services: | ||
```js | ||
registry.getMetadata(); | ||
/* returns | ||
{ | ||
'A': { | ||
name: 'A', // service name | ||
cached: true/false, // true if the service was successfully called | ||
deps: [], // list of deps | ||
doc: '...', // service documentation string | ||
debugInfo: { | ||
fileName: ... // file name where service is defined | ||
line: ..., // line of code where the service is defined | ||
functionName: ..., // service function name (if defined) | ||
parentFunctionName: ..., // the function containing the service definition | ||
} | ||
}, | ||
... | ||
} | ||
*/ | ||
``` | ||
Service | ||
@@ -319,2 +343,31 @@ ======= | ||
doc | ||
--- | ||
set/get the documentation string. | ||
```js | ||
service.doc(); // returns documentation string | ||
service.doc('... service description ...'); // set the doc string | ||
``` | ||
getMetadata | ||
------------ | ||
It returns the metadata of this service: | ||
```js | ||
service.getMetadata(); | ||
/* returns | ||
{ | ||
name: 'A', // service name | ||
cached: true/false, // true if the service was successfully called | ||
deps: [], // list of deps | ||
doc: '...', // service documentation string | ||
debugInfo: { | ||
fileName: ... // file name where service is defined | ||
line: ..., // line of code where the service is defined | ||
functionName: ..., // service function name (if defined) | ||
parentFunctionName: ..., // the function containing the service definition | ||
} | ||
} | ||
*/ | ||
``` | ||
Compatibility | ||
@@ -321,0 +374,0 @@ ============= |
@@ -49,2 +49,6 @@ var assign = require('object-assign') | ||
Registry.prototype.getMetadata = function registryGetAdjList () { | ||
return this.map(function (service) { return service.getMetadata() }) | ||
} | ||
Registry.prototype._run = function registryRun (name) { | ||
@@ -51,0 +55,0 @@ var registry = this |
@@ -6,2 +6,23 @@ /* | ||
function getDebugInfo (func) { | ||
try { | ||
var orig = Error.prepareStackTrace | ||
Error.prepareStackTrace = function (_, stack) { | ||
return stack | ||
} | ||
var err = new Error() | ||
var stack = err.stack | ||
Error.prepareStackTrace = orig | ||
var stackItem = stack[2] | ||
return { | ||
line: stackItem.getLineNumber(), | ||
fileName: stackItem.getFileName(), | ||
parentFunctionName: stackItem.getFunctionName(), | ||
functionName: typeof func === 'function' ? func.name : null | ||
} | ||
} catch (e) { | ||
return {} | ||
} | ||
} | ||
function Service (name, registry) { | ||
@@ -13,2 +34,3 @@ this.name = name | ||
this._cache = undefined | ||
this._doc = '' | ||
} | ||
@@ -20,2 +42,20 @@ | ||
Service.prototype.doc = function serviceDoc (text) { | ||
if (typeof text === 'undefined') { | ||
return this._doc | ||
} | ||
this._doc = text | ||
return this | ||
} | ||
Service.prototype.getMetadata = function serviceGetMetadata () { | ||
return { | ||
name: this.name, | ||
deps: this._deps(), | ||
doc: this.doc(), | ||
cached: !!this._cache, | ||
debugInfo: this._debugInfo | ||
} | ||
} | ||
Service.prototype.dependsOn = function serviceDependsOn (deps) { | ||
@@ -27,2 +67,3 @@ this._deps = typeof deps === 'function' ? deps : function () { return deps } | ||
Service.prototype.provides = function serviceProvides (func) { | ||
this._debugInfo = getDebugInfo(func) | ||
if (typeof func !== 'function') { | ||
@@ -29,0 +70,0 @@ this._func = function () { Promise.resolve(func) } // plain value |
@@ -27,2 +27,61 @@ /* eslint-env node, mocha */ | ||
describe('metadata', function () { | ||
var registry, service1 | ||
beforeEach(function () { | ||
registry = Diogenes.getRegistry() | ||
service1 = registry | ||
.service('answer').provides(42).doc('to all the questions') | ||
registry.service('question') | ||
.dependsOn(['answer']) | ||
.provides(function theanswer () {}) | ||
.doc('the important bit') | ||
}) | ||
it('must return services metadata', function () { | ||
assert.deepEqual(service1.getMetadata(), { | ||
name: 'answer', | ||
cached: false, | ||
deps: [], | ||
doc: 'to all the questions', | ||
debugInfo: { | ||
line: 33, | ||
functionName: null, | ||
parentFunctionName: null, | ||
fileName: __filename | ||
} | ||
}) | ||
}) | ||
it('must return registry metadata', function () { | ||
assert.deepEqual(registry.getMetadata(), { | ||
answer: { | ||
name: 'answer', | ||
cached: false, | ||
deps: [], | ||
doc: 'to all the questions', | ||
debugInfo: { | ||
line: 33, | ||
functionName: null, | ||
parentFunctionName: null, | ||
fileName: __filename | ||
} | ||
}, | ||
question: { | ||
name: 'question', | ||
cached: false, | ||
deps: ['answer'], | ||
doc: 'the important bit', | ||
debugInfo: { | ||
line: 37, | ||
functionName: 'theanswer', | ||
parentFunctionName: null, | ||
fileName: __filename | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
describe('registry', function () { | ||
@@ -29,0 +88,0 @@ var registry |
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
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
380
153442
16
561