knifecycle
Advanced tools
Comparing version 4.3.1 to 5.0.0
@@ -0,1 +1,23 @@ | ||
# [5.0.0](https://github.com/nfroidure/knifecycle/compare/v4.3.1...v5.0.0) (2018-11-18) | ||
### Code Refactoring | ||
* **handler:** Split the handler utils in autoHandler and handler ([cae9b2f](https://github.com/nfroidure/knifecycle/commit/cae9b2f)), closes [#80](https://github.com/nfroidure/knifecycle/issues/80) | ||
* **Utils:** Change the service/provider utils signature ([84e52a6](https://github.com/nfroidure/knifecycle/commit/84e52a6)) | ||
### Features | ||
* **auto(Service/Provider):** Add a way to automatically set provider/service ([d08dca7](https://github.com/nfroidure/knifecycle/commit/d08dca7)) | ||
### BREAKING CHANGES | ||
* **Utils:** service/provider utils call will need to be changed everywhere | ||
* **handler:** handler calls will need to be converted for the new handler signature or use the | ||
newly created autoHandler function. | ||
## [4.3.1](https://github.com/nfroidure/knifecycle/compare/v4.3.0...v4.3.1) (2018-11-10) | ||
@@ -2,0 +24,0 @@ |
@@ -96,2 +96,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "autoService", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.autoService; | ||
} | ||
}); | ||
Object.defineProperty(exports, "provider", { | ||
@@ -103,2 +109,8 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(exports, "autoProvider", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.autoProvider; | ||
} | ||
}); | ||
Object.defineProperty(exports, "handler", { | ||
@@ -110,2 +122,8 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(exports, "autoHandler", { | ||
enumerable: true, | ||
get: function () { | ||
return _util.autoHandler; | ||
} | ||
}); | ||
Object.defineProperty(exports, "wrapInitializer", { | ||
@@ -112,0 +130,0 @@ enumerable: true, |
@@ -69,7 +69,7 @@ "use strict"; | ||
it('should work with a service', () => { | ||
$.register((0, _index.service)('time', timeService)); | ||
$.register((0, _index.service)(timeService, 'time')); | ||
}); | ||
it('should work when overriding a previously set service', async () => { | ||
$.register((0, _index.service)('test', async () => () => 1)); | ||
$.register((0, _index.service)('test', async () => () => 2)); | ||
$.register((0, _index.service)(async () => () => 1, 'test')); | ||
$.register((0, _index.service)(async () => () => 2, 'test')); | ||
const { | ||
@@ -82,3 +82,3 @@ test | ||
it('should fail when overriding an initialized service', async () => { | ||
$.register((0, _index.service)('test', async () => () => 1)); | ||
$.register((0, _index.service)(async () => () => 1, 'test')); | ||
const { | ||
@@ -91,3 +91,3 @@ test | ||
try { | ||
$.register((0, _index.service)('test', async () => () => 2)); | ||
$.register((0, _index.service)(async () => () => 2, 'test')); | ||
throw new _yerror.default('E_UNEXPECTED_SUCCESS'); | ||
@@ -101,3 +101,3 @@ } catch (err) { | ||
it('should work with a provider', () => { | ||
$.register((0, _index.service)('hash', hashProvider)); | ||
$.register((0, _index.service)(hashProvider, 'hash')); | ||
}); | ||
@@ -288,7 +288,7 @@ it('should work when overriding a previously set provider', async () => { | ||
it('should register provider', () => { | ||
$.register((0, _index.provider)('hash', hashProvider)); | ||
$.register((0, _index.provider)(hashProvider, 'hash')); | ||
}); | ||
it('should fail with direct circular dependencies', () => { | ||
_assert.default.throws(() => { | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['hash'])); | ||
}, err => { | ||
@@ -304,3 +304,3 @@ _assert.default.deepEqual(err.code, 'E_CIRCULAR_DEPENDENCY'); | ||
_assert.default.throws(() => { | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['hash>lol'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['hash>lol'])); | ||
}, err => { | ||
@@ -316,6 +316,6 @@ _assert.default.deepEqual(err.code, 'E_CIRCULAR_DEPENDENCY'); | ||
_assert.default.throws(() => { | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash3'], hashProvider), 'hash')); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash'], hashProvider), 'hash1')); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash1'], hashProvider), 'hash2')); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash'], hashProvider), 'hash3')); | ||
}, err => { | ||
@@ -331,6 +331,6 @@ _assert.default.deepEqual(err.code, 'E_CIRCULAR_DEPENDENCY'); | ||
_assert.default.throws(() => { | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash1'], hashProvider), 'hash')); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash2'], hashProvider), 'hash1')); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash3'], hashProvider), 'hash2')); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash'], hashProvider), 'hash3')); | ||
}, err => { | ||
@@ -346,6 +346,6 @@ _assert.default.deepEqual(err.code, 'E_CIRCULAR_DEPENDENCY'); | ||
_assert.default.throws(() => { | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['hash3>aHash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash>aHash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1>aHash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash>aHash'], hashProvider))); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash3>aHash3'], hashProvider), 'hash')); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash>aHash'], hashProvider), 'hash1')); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash1>aHash1'], hashProvider), 'hash2')); | ||
$.register((0, _index.provider)((0, _index.inject)(['hash>aHash'], hashProvider), 'hash3')); | ||
}, err => { | ||
@@ -379,7 +379,7 @@ _assert.default.deepEqual(err.code, 'E_CIRCULAR_DEPENDENCY'); | ||
it('should work with service dependencies', async () => { | ||
$.register((0, _index.service)('sample', (0, _index.inject)(['time'], function sampleService({ | ||
$.register((0, _index.service)((0, _index.inject)(['time'], function sampleService({ | ||
time | ||
}) { | ||
return Promise.resolve(typeof time); | ||
}))); | ||
}), 'sample')); | ||
$.register((0, _index.constant)('time', time)); | ||
@@ -397,3 +397,3 @@ const dependencies = await $.run(['sample']); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
const dependencies = await $.run(['time', 'hash']); | ||
@@ -414,3 +414,3 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', '?DEBUG'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV', '?DEBUG'])); | ||
const dependencies = await $.run(['time', 'hash']); | ||
@@ -431,3 +431,3 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', '?DEBUG'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV', '?DEBUG'])); | ||
const dependencies = await $.run(['time', 'hash']); | ||
@@ -448,8 +448,8 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['hash'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['hash1'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash3', ['hash2'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash4', ['hash3'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash5', ['hash4'])); | ||
const dependencies = await $.run(['hash5', 'time']); | ||
@@ -463,6 +463,6 @@ | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.service)('time', timeServiceStub)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', 'time'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV', 'time'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['ENV', 'time'], hashProvider))); | ||
$.register((0, _index.service)(timeServiceStub, 'time')); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV', 'time'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['ENV', 'time'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash3', ['ENV', 'time'])); | ||
const dependencies = await $.run(['hash', 'hash2', 'hash3', 'time']); | ||
@@ -483,4 +483,4 @@ | ||
$.register((0, _index.provider)('mappedStub', (0, _index.inject)(['stub2>mappedStub2'], providerStub))); | ||
$.register((0, _index.provider)('mappedStub2', providerStub2)); | ||
$.register((0, _index.provider)(providerStub, 'mappedStub', ['stub2>mappedStub2'])); | ||
$.register((0, _index.provider)(providerStub2, 'mappedStub2')); | ||
const dependencies = await $.run(['stub>mappedStub']); | ||
@@ -500,6 +500,6 @@ | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.service)('aTime', timeServiceStub)); | ||
$.register((0, _index.provider)('aHash', (0, _index.inject)(['ENV', 'time>aTime'], hashProvider))); | ||
$.register((0, _index.provider)('aHash2', (0, _index.inject)(['ENV', 'hash>aHash'], hashProvider))); | ||
$.register((0, _index.provider)('aHash3', (0, _index.inject)(['ENV', 'hash>aHash'], hashProvider))); | ||
$.register((0, _index.service)(timeServiceStub, 'aTime')); | ||
$.register((0, _index.provider)(hashProvider, 'aHash', ['ENV', 'time>aTime'])); | ||
$.register((0, _index.provider)(hashProvider, 'aHash2', ['ENV', 'hash>aHash'])); | ||
$.register((0, _index.provider)(hashProvider, 'aHash3', ['ENV', 'hash>aHash'])); | ||
const dependencies = await $.run(['hash2>aHash2', 'hash3>aHash3', 'time>aTime']); | ||
@@ -512,3 +512,3 @@ | ||
it('should fail with bad service', async () => { | ||
$.register((0, _index.service)('lol', () => {})); | ||
$.register((0, _index.service)(() => {}, 'lol')); | ||
@@ -525,3 +525,3 @@ try { | ||
it('should fail with bad provider', async () => { | ||
$.register((0, _index.provider)('lol', () => {})); | ||
$.register((0, _index.provider)(() => {}, 'lol')); | ||
@@ -538,3 +538,3 @@ try { | ||
it('should fail with bad service in a provider', async () => { | ||
$.register((0, _index.provider)('lol', () => Promise.resolve())); | ||
$.register((0, _index.provider)(() => Promise.resolve(), 'lol')); | ||
@@ -563,4 +563,4 @@ try { | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', 'hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV', 'lol'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV', 'hash2'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['ENV', 'lol'])); | ||
@@ -579,5 +579,5 @@ try { | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('db', (0, _index.inject)(['ENV'], dbProvider))); | ||
$.register((0, _index.provider)('process', (0, _index.inject)(['$fatalError'], processProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(dbProvider, 'db', ['ENV'])); | ||
$.register((0, _index.provider)(processProvider, 'process', ['$fatalError'])); | ||
@@ -644,3 +644,3 @@ function processProvider({ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV', '?DEBUG'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV', '?DEBUG'])); | ||
const dependencies = await $.run(['time', 'hash']); | ||
@@ -675,6 +675,6 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['hash'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash3', ['hash2'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash5', ['hash4'])); | ||
const dependencies = await $.run(['hash5', 'time']); | ||
@@ -685,4 +685,4 @@ | ||
it('should work with various dependencies', async () => { | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['?ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['hash2'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash3', ['?ENV'])); | ||
$.register((0, _index.constant)('DEBUG', 1)); | ||
@@ -727,5 +727,5 @@ $.register((0, _index.initializer)({ | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.service)('time', timeServiceStub)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['hash1', 'hash2', 'hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash_', (0, _index.inject)(['hash1', 'hash2', 'hash3'], hashProvider))); | ||
$.register((0, _index.service)(timeServiceStub, 'time')); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['hash1', 'hash2', 'hash3'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash_', ['hash1', 'hash2', 'hash3'])); | ||
const dependencies = await $.run(['hash', 'hash_', 'hash3']); | ||
@@ -842,3 +842,3 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
const dependencies = await $.run(['time', 'hash', '$injector']); | ||
@@ -857,3 +857,3 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
const dependencies = await $.run(['time', 'hash', '$injector']); | ||
@@ -877,3 +877,3 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
const dependencies = await $.run(['time', '$injector']); | ||
@@ -892,3 +892,3 @@ | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
const [{ | ||
@@ -910,8 +910,8 @@ hash | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'], { | ||
singleton: true | ||
})); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV'], (0, _index.options)({ | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['ENV'], { | ||
singleton: true | ||
}, hashProvider)))); | ||
})); | ||
const [{ | ||
@@ -947,7 +947,7 @@ hash, | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'], { | ||
singleton: true | ||
})); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['ENV'])); | ||
const [dependencies] = await Promise.all([$.run(['$destroy']), $.run(['ENV', 'hash', 'hash1', 'time']), $.run(['ENV', 'hash', 'hash2'])]); | ||
@@ -962,5 +962,5 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['ENV'])); | ||
const dependenciesBuckets = await Promise.all([$.run(['$destroy']), $.run(['$destroy', 'ENV', 'hash', 'hash1', 'time']), $.run(['$destroy', 'ENV', 'hash', 'hash2'])]); | ||
@@ -972,5 +972,5 @@ await Promise.all(dependenciesBuckets.map(dependencies => dependencies.$destroy())); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['ENV'])); | ||
const [dependencies1, dependencies2] = await Promise.all([$.run(['$destroy']), $.run(['$dispose', 'ENV', 'hash', 'hash1', 'time']), $.run(['ENV', 'hash', 'hash2'])]); | ||
@@ -982,4 +982,4 @@ await Promise.all([dependencies2.$dispose(), dependencies1.$destroy()]); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['ENV'])); | ||
const dependencies = await $.run(['$destroy']); | ||
@@ -1019,3 +1019,3 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
const dependencies = await $.run(['time', 'hash', '$dispose']); | ||
@@ -1043,9 +1043,9 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
$.register((0, _index.provider)('shutdownChecker', (0, _index.inject)(['hash4'], () => Promise.resolve({ | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['hash'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['hash1'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash3', ['hash2'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash4', ['hash3'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash5', ['hash4'])); | ||
$.register((0, _index.provider)(() => Promise.resolve({ | ||
service: { | ||
@@ -1056,3 +1056,3 @@ shutdownStub, | ||
dispose: shutdownStub | ||
})))); | ||
}), 'shutdownChecker', ['hash4'])); | ||
const dependencies = await $.run(['hash5', 'time', '$dispose', 'shutdownChecker']); | ||
@@ -1085,4 +1085,4 @@ | ||
$.register((0, _index.constant)('ENV', ENV)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('shutdownChecker', (0, _index.inject)(['hash'], () => Promise.resolve({ | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(() => Promise.resolve({ | ||
service: { | ||
@@ -1093,5 +1093,5 @@ shutdownStub, | ||
dispose: shutdownStub | ||
})))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['shutdownChecker'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['shutdownChecker'], hashProvider))); | ||
}), 'shutdownChecker', ['hash'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['shutdownChecker'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['shutdownChecker'])); | ||
const dependencies = await $.run(['hash1', 'hash2', '$dispose', 'shutdownChecker']); | ||
@@ -1112,14 +1112,14 @@ | ||
$.register((0, _index.provider)('hash', () => Promise.resolve({ | ||
$.register((0, _index.provider)(() => Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash') | ||
}))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], () => Promise.resolve({ | ||
}), 'hash')); | ||
$.register((0, _index.provider)(() => Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash1') | ||
})))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1', 'hash'], () => Promise.resolve({ | ||
}), 'hash1', ['hash'])); | ||
$.register((0, _index.provider)(() => Promise.resolve({ | ||
service: {}, | ||
dispose: servicesShutdownCalls.bind(null, 'hash2') | ||
})))); | ||
}), 'hash2', ['hash1', 'hash'])); | ||
const dependencies = await $.run(['hash2', '$dispose']); | ||
@@ -1136,3 +1136,3 @@ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'], { | ||
singleton: true | ||
@@ -1155,3 +1155,3 @@ })); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider), { | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'], { | ||
singleton: true | ||
@@ -1179,8 +1179,8 @@ })); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['hash'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['hash1'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash3', ['hash2'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash4', ['hash3'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash5', ['hash4'])); | ||
@@ -1192,8 +1192,8 @@ _assert.default.equal($.toMermaidGraph(), 'graph TD\n' + ' hash-->ENV\n' + ' hash1-->hash\n' + ' hash2-->hash1\n' + ' hash3-->hash2\n' + ' hash4-->hash3\n' + ' hash5-->hash4'); | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['hash'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['hash1'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash3', ['hash2'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash4', ['hash3'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash5', ['hash4'])); | ||
@@ -1216,8 +1216,8 @@ _assert.default.equal($.toMermaidGraph({ | ||
$.register((0, _index.constant)('time', time)); | ||
$.register((0, _index.provider)('hash', (0, _index.inject)(['ENV'], hashProvider))); | ||
$.register((0, _index.provider)('hash1', (0, _index.inject)(['hash'], hashProvider))); | ||
$.register((0, _index.provider)('hash2', (0, _index.inject)(['hash1'], hashProvider))); | ||
$.register((0, _index.provider)('hash3', (0, _index.inject)(['hash2'], hashProvider))); | ||
$.register((0, _index.provider)('hash4', (0, _index.inject)(['hash3'], hashProvider))); | ||
$.register((0, _index.provider)('hash5', (0, _index.inject)(['hash4'], hashProvider))); | ||
$.register((0, _index.provider)(hashProvider, 'hash', ['ENV'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash1', ['hash'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash2', ['hash1'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash3', ['hash2'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash4', ['hash3'])); | ||
$.register((0, _index.provider)(hashProvider, 'hash5', ['hash4'])); | ||
@@ -1224,0 +1224,0 @@ _assert.default.equal($.toMermaidGraph({ |
228
dist/util.js
@@ -19,4 +19,7 @@ "use strict"; | ||
exports.service = service; | ||
exports.autoService = autoService; | ||
exports.provider = provider; | ||
exports.autoProvider = autoProvider; | ||
exports.handler = handler; | ||
exports.autoHandler = autoHandler; | ||
exports.parseDependencyDeclaration = parseDependencyDeclaration; | ||
@@ -120,3 +123,3 @@ exports.ALLOWED_INITIALIZER_TYPES = exports.OPTIONAL_FLAG = exports.DECLARATION_SEPARATOR = exports.ALLOWED_SPECIAL_PROPS = exports.SPECIAL_PROPS = exports.SPECIAL_PROPS_PREFIX = void 0; | ||
* dependencies declarations set to it. | ||
* @param {String[]} dependenciesDeclarations | ||
* @param {Array<String>} dependencies | ||
* List of dependencies declarations to declare which | ||
@@ -137,4 +140,4 @@ * services the initializer needs to resolve its | ||
* service( | ||
* inject(['ENV'], myServiceInitializer) | ||
* 'myService', | ||
* inject(['ENV'], myServiceInitializer) | ||
* ) | ||
@@ -146,11 +149,11 @@ * ) | ||
function inject(dependenciesDeclarations, initializer) { | ||
function inject(dependencies, initializer) { | ||
if ('constant' === initializer[SPECIAL_PROPS.TYPE]) { | ||
throw new _yerror.default(E_BAD_INJECT_IN_CONSTANT, initializer[SPECIAL_PROPS.NAME], dependenciesDeclarations); | ||
throw new _yerror.default(E_BAD_INJECT_IN_CONSTANT, initializer[SPECIAL_PROPS.NAME], dependencies); | ||
} | ||
const uniqueInitializer = reuseSpecialProps(initializer, initializer, { | ||
[SPECIAL_PROPS.INJECT]: dependenciesDeclarations | ||
[SPECIAL_PROPS.INJECT]: dependencies | ||
}); | ||
debug('Wrapped an initializer with dependencies:', dependenciesDeclarations); | ||
debug('Wrapped an initializer with dependencies:', dependencies); | ||
return uniqueInitializer; | ||
@@ -187,10 +190,14 @@ } | ||
const source = initializer.toString(); | ||
const matches = source.match(/^\s*async\s+(?:function)?\s*\w*\s*\(\{\s*([^{}}]+)\s*\}[^()]*\)/); | ||
const matches = source.match(/^\s*(?:async\s+function(?:\s+\w+)?|async)\s*\(\{\s*([^{}}]+)\s*\}[^()]*\)/); | ||
if (!matches) { | ||
if (!source.match(/^\s*async/)) { | ||
throw new _yerror.default('E_NON_ASYNC_INITIALIZER', source); | ||
} | ||
throw new _yerror.default('E_AUTO_INJECTION_FAILURE', source); | ||
} | ||
const dependenciesDeclarations = matches[1].trim().split(/\s*,\s*/).map(injection => (injection.includes('=') ? '?' : '') + injection.split(/\s*=\s*/).shift().split(/\s*:\s*/).shift()); | ||
return inject(dependenciesDeclarations, initializer); | ||
const dependencies = matches[1].trim().split(/\s*,\s*/).map(injection => (injection.includes('=') ? '?' : '') + injection.split(/\s*=\s*/).shift().split(/\s*:\s*/).shift()).filter(injection => !/[)(\][]/.test(injection)); | ||
return inject(dependencies, initializer); | ||
} | ||
@@ -200,3 +207,3 @@ /** | ||
* more dependencies declarations appended to it. | ||
* @param {String[]} dependenciesDeclarations | ||
* @param {Array<String>} dependencies | ||
* List of dependencies declarations to append | ||
@@ -213,4 +220,5 @@ * @param {Function} initializer | ||
* new Knifecycle() | ||
* .register(service('myService', | ||
* alsoInject(['ENV'], myServiceInitializer) | ||
* .register(service( | ||
* alsoInject(['ENV'], myServiceInitializer), | ||
* 'myService', | ||
* )); | ||
@@ -220,4 +228,4 @@ */ | ||
function alsoInject(dependenciesDeclarations, initializer) { | ||
return inject((initializer[SPECIAL_PROPS.INJECT] || []).concat(dependenciesDeclarations), initializer); | ||
function alsoInject(dependencies, initializer) { | ||
return inject((initializer[SPECIAL_PROPS.INJECT] || []).concat(dependencies), initializer); | ||
} | ||
@@ -245,4 +253,5 @@ /** | ||
* new Knifecycle() | ||
* .register(service('myService', | ||
* extra({ httpHandler: true }, myServiceInitializer) | ||
* .register(service( | ||
* extra({ httpHandler: true }, myServiceInitializer), | ||
* 'myService', | ||
* )); | ||
@@ -278,6 +287,7 @@ */ | ||
* new Knifecycle() | ||
* .register(service('myService', | ||
* .register(service( | ||
* inject(['ENV'], | ||
* options({ singleton: true}, myServiceInitializer) | ||
* ) | ||
* ), | ||
* 'myService', | ||
* )); | ||
@@ -287,5 +297,5 @@ */ | ||
function options(options, initializer, merge = false) { | ||
function options(options, initializer, merge = true) { | ||
const uniqueInitializer = reuseSpecialProps(initializer, initializer, { | ||
[SPECIAL_PROPS.OPTIONS]: merge ? options : Object.assign({}, initializer[SPECIAL_PROPS.OPTIONS] || {}, options) | ||
[SPECIAL_PROPS.OPTIONS]: merge ? Object.assign({}, initializer[SPECIAL_PROPS.OPTIONS] || {}, options) : options | ||
}); | ||
@@ -427,7 +437,6 @@ debug('Wrapped an initializer with options:', options); | ||
* .register(service( | ||
* async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
* 'printAnswer', | ||
* async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
* { | ||
* inject: ['THE_NUMBER', 'log'], | ||
* } | ||
* ['THE_NUMBER', 'log'], | ||
* )) | ||
* .run(['printAnswer']); | ||
@@ -460,8 +469,10 @@ * | ||
* Decorator that creates an initializer for a service | ||
* @param {String} name | ||
* The service's name. | ||
* @param {Function} initializer | ||
* @param {Function} builder | ||
* An initializer returning the service promise | ||
* @param {Object} options | ||
* Options attached to the initializer | ||
* @param {String} [name] | ||
* The service's name | ||
* @param {Array<String>} [dependencies] | ||
* The service's dependencies | ||
* @param {Object} [options] | ||
* Options attached to the built initializer | ||
* @return {Function} | ||
@@ -476,7 +487,7 @@ * Returns a new initializer | ||
* .register(service( | ||
* async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
* 'printAnswer', | ||
* async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
* { | ||
* inject: ['THE_NUMBER', 'log'], | ||
* } | ||
* ['THE_NUMBER', 'log'], | ||
* { singleton: true } | ||
* )) | ||
* .run(['printAnswer']); | ||
@@ -488,19 +499,42 @@ * | ||
function service(serviceName, serviceBuilder, options = {}) { | ||
const uniqueInitializer = reuseSpecialProps(serviceBuilder, serviceBuilder, { | ||
[SPECIAL_PROPS.NAME]: serviceName, | ||
function service(builder, name, dependencies, options) { | ||
if (!builder) { | ||
throw new _yerror.default('E_NO_SERVICE_BUILDER'); | ||
} | ||
const uniqueInitializer = reuseSpecialProps(builder, builder, { | ||
[SPECIAL_PROPS.NAME]: name, | ||
[SPECIAL_PROPS.TYPE]: 'service', | ||
[SPECIAL_PROPS.INJECT]: dependencies, | ||
[SPECIAL_PROPS.OPTIONS]: options | ||
}); | ||
debug(`Created an initializer from a service builder: ${name}.`); | ||
debug(`Created an initializer from a service builder: ${name || 'anonymous'}.`); | ||
return uniqueInitializer; | ||
} | ||
/** | ||
* Decorator that auto creates a service | ||
* @param {Function} initializer | ||
* An initializer returning the service promise | ||
* @return {Function} | ||
* Returns a new initializer | ||
*/ | ||
function autoService(serviceBuilder) { | ||
return initializer({ | ||
name: autoName(serviceBuilder)[SPECIAL_PROPS.NAME], | ||
type: 'service', | ||
inject: autoInject(serviceBuilder)[SPECIAL_PROPS.INJECT] | ||
}, serviceBuilder); | ||
} | ||
/** | ||
* Decorator that creates an initializer for a provider | ||
* @param {String} name | ||
* The provider's name. | ||
* @param {Function} provider | ||
* A provider returning the service builder promise | ||
* @param {Object} options | ||
* Options attached to the initializer | ||
* @param {Function} builder | ||
* A builder returning the provider promise | ||
* @param {String} [name] | ||
* The service's name | ||
* @param {Array<String>} [dependencies] | ||
* The service's dependencies | ||
* @param {Object} [options] | ||
* Options attached to the built initializer | ||
* @return {Function} | ||
@@ -515,3 +549,5 @@ * Returns a new initializer | ||
* | ||
* $.register(provider('config', async function configProvider() { | ||
* $.register(provider(configProvider, 'config')); | ||
* | ||
* async function configProvider() { | ||
* return new Promise((resolve, reject) { | ||
@@ -538,16 +574,37 @@ * fs.readFile('config.js', function(err, data) { | ||
* }); | ||
* })); | ||
* } | ||
*/ | ||
function provider(providerName, provider, options = {}) { | ||
const uniqueInitializer = reuseSpecialProps(provider, provider, { | ||
[SPECIAL_PROPS.NAME]: providerName, | ||
function provider(builder, name, dependencies, options) { | ||
if (!builder) { | ||
throw new _yerror.default('E_NO_PROVIDER_BUILDER'); | ||
} | ||
const uniqueInitializer = reuseSpecialProps(builder, builder, { | ||
[SPECIAL_PROPS.NAME]: name, | ||
[SPECIAL_PROPS.TYPE]: 'provider', | ||
[SPECIAL_PROPS.INJECT]: dependencies, | ||
[SPECIAL_PROPS.OPTIONS]: options | ||
}); | ||
debug(`Created an initializer from a provider builder: ${name}.`); | ||
debug(`Created an initializer from a provider builder: ${name || 'anonymous'}.`); | ||
return uniqueInitializer; | ||
} | ||
/** | ||
* Decorator that auto creates a provider | ||
* @param {Function} initializer | ||
* An initializer returning the provider promise | ||
* @return {Function} | ||
* Returns a new initializer | ||
*/ | ||
function autoProvider(baseInitializer) { | ||
return initializer({ | ||
name: autoName(baseInitializer)[SPECIAL_PROPS.NAME], | ||
type: 'provider', | ||
inject: autoInject(baseInitializer)[SPECIAL_PROPS.INJECT] | ||
}, baseInitializer); | ||
} | ||
async function deliverConstantValue(value) { | ||
@@ -560,13 +617,15 @@ return value; | ||
* The handler function | ||
* @param {Array} [dependencies=[]] | ||
* @param {String} [name] | ||
* The name of the handler. Default to the DI prop if exists | ||
* @param {Array<String>} [dependencies=[]] | ||
* The dependencies to inject in it | ||
* @param {Object} [extra] | ||
* Optional extra data to associate with the handler | ||
* @param {Object} [options] | ||
* Options attached to the built initializer | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* import Knifecycle, { initializer } from 'knifecycle'; | ||
* import Knifecycle, { handler } from 'knifecycle'; | ||
* | ||
* new Knifecycle() | ||
* .register(handler(getUser, ['db', '?log'])); | ||
* .register(handler(getUser, 'getUser', ['db', '?log'])); | ||
* | ||
@@ -582,18 +641,45 @@ * const QUERY = `SELECT * FROM users WHERE id=$1` | ||
function handler(handlerFunction, dependencies, extra) { | ||
try { | ||
return initializer({ | ||
name: autoName(handlerFunction)[SPECIAL_PROPS.NAME], | ||
type: 'service', | ||
inject: dependencies || autoInject(handlerFunction)[SPECIAL_PROPS.INJECT], | ||
extra: extra || handlerFunction[SPECIAL_PROPS.EXTRA] | ||
}, async (...args) => handlerFunction.bind(null, ...args)); | ||
} catch (err) { | ||
if (err.code === 'E_AUTO_NAMING_FAILURE') { | ||
throw _yerror.default.wrap(err, 'E_NO_HANDLER_NAME', handlerFunction); | ||
} | ||
function handler(handlerFunction, name, dependencies, options) { | ||
name = name || handlerFunction[SPECIAL_PROPS.NAME]; | ||
dependencies = dependencies || handlerFunction[SPECIAL_PROPS.INJECT]; | ||
throw err; | ||
if (!name) { | ||
throw new _yerror.default('E_NO_HANDLER_NAME', handlerFunction); | ||
} | ||
return initializer({ | ||
name, | ||
type: 'service', | ||
inject: dependencies, | ||
options | ||
}, async (...args) => handlerFunction.bind(null, ...args)); | ||
} | ||
/** | ||
* Allows to create an initializer with a simple handler automagically | ||
* @param {Function} handlerFunction | ||
* The handler function | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* import Knifecycle, { autoHandler } from 'knifecycle'; | ||
* | ||
* new Knifecycle() | ||
* .register(autoHandler(getUser)); | ||
* | ||
* const QUERY = `SELECT * FROM users WHERE id=$1` | ||
* async function getUser({ db }, userId) { | ||
* const [row] = await db.query(QUERY, userId); | ||
* | ||
* return row; | ||
* } | ||
*/ | ||
function autoHandler(handlerFunction) { | ||
return initializer({ | ||
name: autoName(handlerFunction)[SPECIAL_PROPS.NAME], | ||
type: 'service', | ||
inject: autoInject(handlerFunction)[SPECIAL_PROPS.INJECT] | ||
}, async (...args) => handlerFunction.bind(null, ...args)); | ||
} | ||
/* Architecture Note #1.2.1: Dependencies declaration syntax | ||
@@ -603,6 +689,6 @@ | ||
`?serviceName>mappedName` | ||
The `?` flag indicates an optionnal dependencies. | ||
`:mappedName` is optional and says to the container to | ||
inject `serviceName` but to inject it as `mappedName`. | ||
It allows to write generic services with fixed | ||
The `?` flag indicates an optional dependency. | ||
`>mappedName` is optional and allows to inject | ||
`mappedName` as `serviceName`. | ||
It allows to write generic services with fixed | ||
dependencies and remap their name at injection time. | ||
@@ -609,0 +695,0 @@ */ |
@@ -11,2 +11,6 @@ "use strict"; | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function aProvider() {} | ||
@@ -136,2 +140,7 @@ | ||
}); | ||
it('should fail with a constant', () => { | ||
_assert.default.throws(() => { | ||
(0, _util.inject)(['test'], (0, _util.constant)('test', 'test')); | ||
}, /E_BAD_INJECT_IN_CONSTANT/); | ||
}); | ||
}); | ||
@@ -183,3 +192,3 @@ describe('autoInject', () => { | ||
log = noop, | ||
debug: aDebug = () => '' | ||
debug: aDebug = noop | ||
}) => async () => ({ | ||
@@ -206,3 +215,3 @@ ENV, | ||
log = noop, | ||
debug: aDebug = () => '' | ||
debug: aDebug = noop | ||
}, { | ||
@@ -226,2 +235,13 @@ userId | ||
}); | ||
it('should fail with non async initializers', () => { | ||
_assert.default.throws(() => { | ||
(0, _util.autoInject)(({ | ||
foo: bar = { | ||
bar: 'foo' | ||
} | ||
}) => { | ||
return bar; | ||
}); | ||
}, /E_NON_ASYNC_INITIALIZER/); | ||
}); | ||
it('should fail with too complex injections', () => { | ||
@@ -252,2 +272,9 @@ _assert.default.throws(() => { | ||
}); | ||
it('should allow to decorate an initializer with dependencies', () => { | ||
const newInitializer = (0, _util.alsoInject)(['ENV'], aProvider); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], ['ENV']); | ||
}); | ||
}); | ||
@@ -260,3 +287,3 @@ describe('options', () => { | ||
}; | ||
const newInitializer = (0, _util.inject)(dependencies, (0, _util.options)(baseOptions, aProvider)); | ||
const newInitializer = (0, _util.inject)(dependencies, (0, _util.options)(baseOptions, aProvider, false)); | ||
@@ -273,2 +300,25 @@ _assert.default.notEqual(newInitializer, aProvider); | ||
}); | ||
it('should allow to decorate an initializer with options', () => { | ||
const dependencies = ['ANOTHER_ENV>ENV']; | ||
const rootOptions = { | ||
yolo: true, | ||
singleton: false | ||
}; | ||
const baseOptions = { | ||
singleton: true | ||
}; | ||
const newInitializer = (0, _util.inject)(dependencies, (0, _util.options)(baseOptions, (0, _util.options)(rootOptions, aProvider), true)); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], rootOptions); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], baseOptions); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], _objectSpread({}, rootOptions, baseOptions)); | ||
}); | ||
}); | ||
@@ -397,2 +447,32 @@ describe('name', () => { | ||
}); | ||
it('should allow to decorate an initializer with extra infos', () => { | ||
const extraInformations = { | ||
httpHandler: true | ||
}; | ||
const newInitializer = (0, _util.extra)(extraInformations, aProvider, true); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.EXTRA], extraInformations); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.EXTRA], extraInformations); | ||
}); | ||
it('should allow to decorate an initializer with additional extra infos', () => { | ||
const baseExtraInformations = { | ||
yolo: true, | ||
httpHandler: false | ||
}; | ||
const additionalExtraInformations = { | ||
httpHandler: true | ||
}; | ||
const newInitializer = (0, _util.extra)(baseExtraInformations, (0, _util.extra)(additionalExtraInformations, aProvider), true); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.EXTRA], baseExtraInformations); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.EXTRA], baseExtraInformations); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.EXTRA], _objectSpread({}, baseExtraInformations, baseExtraInformations)); | ||
}); | ||
}); | ||
@@ -453,2 +533,10 @@ describe('type', () => { | ||
}); | ||
it('should fail with bad properties', () => { | ||
_assert.default.throws(() => { | ||
(0, _util.initializer)({ | ||
name: 'yolo', | ||
yolo: '' | ||
}, async () => {}); | ||
}, /E_BAD_PROPERTY/); | ||
}); | ||
}); | ||
@@ -493,3 +581,3 @@ describe('constant', () => { | ||
const baseType = 'service'; | ||
const newInitializer = (0, _util.service)(baseName, (0, _util.inject)(dependencies, aServiceBuilder), baseOptions); | ||
const newInitializer = (0, _util.service)(aServiceBuilder, baseName, dependencies, baseOptions); | ||
@@ -510,5 +598,131 @@ _assert.default.notEqual(newInitializer, aProvider); | ||
}); | ||
it('should allow to create an initializer from a service builder', async () => { | ||
const aServiceBuilder = async () => {}; | ||
const dependencies = ['ANOTHER_ENV>ENV']; | ||
const baseOptions = { | ||
singleton: true | ||
}; | ||
const baseName = 'hash'; | ||
const baseType = 'service'; | ||
const newInitializer = (0, _util.service)((0, _util.name)(baseName, (0, _util.inject)(dependencies, (0, _util.options)(baseOptions, aServiceBuilder)))); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], baseOptions); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], baseOptions); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.NAME], baseName); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.TYPE], baseType); | ||
}); | ||
it('should fail with no service builder', () => { | ||
_assert.default.throws(() => { | ||
(0, _util.service)(); | ||
}, /E_NO_SERVICE_BUILDER/); | ||
}); | ||
}); | ||
describe('autoService', () => { | ||
it('should detect the provider details', () => { | ||
const baseServiceBuilder = async function initializeMySQL({ | ||
ENV | ||
}) { | ||
return ENV; | ||
}; | ||
const newInitializer = (0, _util.autoService)(baseServiceBuilder); | ||
_assert.default.notEqual(newInitializer, baseServiceBuilder); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], ['ENV']); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.NAME], 'mySQL'); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.TYPE], 'service'); | ||
}); | ||
}); | ||
describe('provider', () => { | ||
it('should allow to create an initializer from a provider builder', async () => { | ||
const aServiceBuilder = async () => {}; | ||
const dependencies = ['ANOTHER_ENV>ENV']; | ||
const baseOptions = { | ||
singleton: true | ||
}; | ||
const baseName = 'hash'; | ||
const baseType = 'provider'; | ||
const newInitializer = (0, _util.provider)(aServiceBuilder, baseName, dependencies, baseOptions); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], baseOptions); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], baseOptions); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.NAME], baseName); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.TYPE], baseType); | ||
}); | ||
it('should allow to create an initializer from a provider builder', async () => { | ||
const aServiceBuilder = async () => {}; | ||
const dependencies = ['ANOTHER_ENV>ENV']; | ||
const baseOptions = { | ||
singleton: true | ||
}; | ||
const baseName = 'hash'; | ||
const baseType = 'provider'; | ||
const newInitializer = (0, _util.provider)((0, _util.name)(baseName, (0, _util.inject)(dependencies, (0, _util.options)(baseOptions, aServiceBuilder)))); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], dependencies); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], baseOptions); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], baseOptions); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.NAME], baseName); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.TYPE], baseType); | ||
}); | ||
it('should fail with no provider builder', () => { | ||
_assert.default.throws(() => { | ||
(0, _util.provider)(); | ||
}, /E_NO_PROVIDER_BUILDER/); | ||
}); | ||
}); | ||
describe('autoProvider', () => { | ||
it('should detect the provider details', () => { | ||
const baseInitializer = async function initializeMySQL({ | ||
ENV | ||
}) { | ||
return ENV; | ||
}; | ||
const newInitializer = (0, _util.autoProvider)(baseInitializer); | ||
_assert.default.notEqual(newInitializer, baseInitializer); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], ['ENV']); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.NAME], 'mySQL'); | ||
_assert.default.equal(newInitializer[_util.SPECIAL_PROPS.TYPE], 'provider'); | ||
}); | ||
}); | ||
describe('handler', () => { | ||
it('should work', async () => { | ||
const baseName = 'sampleHandler'; | ||
const injectedServices = ['kikooo', 'lol']; | ||
@@ -519,7 +733,7 @@ const services = { | ||
}; | ||
const theInitializer = (0, _util.handler)(sampleHandler, injectedServices); | ||
const theInitializer = (0, _util.handler)(sampleHandler, baseName, injectedServices); | ||
_assert.default.deepEqual(theInitializer.$name, sampleHandler.name); | ||
_assert.default.deepEqual(theInitializer.$name, baseName); | ||
_assert.default.deepEqual(theInitializer.$inject, ['kikooo', 'lol']); | ||
_assert.default.deepEqual(theInitializer.$inject, injectedServices); | ||
@@ -541,3 +755,3 @@ const theHandler = await theInitializer(services); | ||
}); | ||
it('should fail for anonymous functions', () => { | ||
it('should fail with no name', () => { | ||
_assert.default.throws(() => { | ||
@@ -548,2 +762,41 @@ (0, _util.handler)(() => {}); | ||
}); | ||
describe('autoHandler', () => { | ||
it('should work', async () => { | ||
const services = { | ||
kikooo: 'kikooo', | ||
lol: 'lol' | ||
}; | ||
const theInitializer = (0, _util.autoHandler)(sampleHandler); | ||
_assert.default.deepEqual(theInitializer.$name, sampleHandler.name); | ||
_assert.default.deepEqual(theInitializer.$inject, ['kikooo', 'lol']); | ||
const theHandler = await theInitializer(services); | ||
const result = await theHandler('test'); | ||
_assert.default.deepEqual(result, { | ||
deps: services, | ||
args: ['test'] | ||
}); | ||
async function sampleHandler({ | ||
kikooo, | ||
lol | ||
}, ...args) { | ||
return Promise.resolve({ | ||
deps: { | ||
kikooo, | ||
lol | ||
}, | ||
args | ||
}); | ||
} | ||
}); | ||
it('should fail for anonymous functions', () => { | ||
_assert.default.throws(() => { | ||
(0, _util.autoHandler)(() => {}); | ||
}, /E_AUTO_NAMING_FAILURE/); | ||
}); | ||
}); | ||
describe('parseDependencyDeclaration', () => { | ||
@@ -550,0 +803,0 @@ it('should work', () => { |
{ | ||
"name": "knifecycle", | ||
"version": "4.3.1", | ||
"version": "5.0.0", | ||
"description": "Manage your NodeJS processes's lifecycle.", | ||
@@ -76,9 +76,9 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.2", | ||
"@babel/core": "^7.1.2", | ||
"@babel/cli": "^7.1.5", | ||
"@babel/core": "^7.1.6", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", | ||
"@babel/preset-env": "^7.1.0", | ||
"@babel/preset-env": "^7.1.6", | ||
"@babel/register": "^7.0.0", | ||
"babel-eslint": "^10.0.1", | ||
"browserify": "^16.2.2", | ||
"browserify": "^16.2.3", | ||
"commitizen": "^3.0.4", | ||
@@ -88,7 +88,7 @@ "conventional-changelog-cli": "^2.0.11", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"eslint": "^5.8.0", | ||
"eslint": "^5.9.0", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"jsarch": "^1.3.0", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"karma": "^3.0.0", | ||
"karma": "^3.1.1", | ||
"karma-browserify": "^5.3.0", | ||
@@ -100,3 +100,3 @@ "karma-chrome-launcher": "^2.2.0", | ||
"metapak": "^2.0.0", | ||
"metapak-nfroidure": "9.0.1", | ||
"metapak-nfroidure": "9.0.3", | ||
"mocha": "^5.2.0", | ||
@@ -135,2 +135,9 @@ "nyc": "^13.0.1", | ||
}, | ||
"nyc": { | ||
"exclude": [ | ||
"**/*.mocha.js", | ||
"**/*.conf.js", | ||
"dist" | ||
] | ||
}, | ||
"contributors": [], | ||
@@ -137,0 +144,0 @@ "eslintConfig": { |
195
README.md
@@ -26,3 +26,3 @@ [//]: # ( ) | ||
**The code dependencies** are fully covered by require/system | ||
**The code dependencies** are fully covered by JavaScript | ||
modules in a testable manner (with `mockery` or `System` | ||
@@ -73,5 +73,5 @@ directly). There is no need for another dependency management | ||
- build raw initialization modules to avoid | ||
embedding Knifecycle in your builds. | ||
embedding Knifecycle in your builds; | ||
- optionally autoload services dependencies with custom | ||
logic | ||
logic. | ||
@@ -98,3 +98,3 @@ ## Usage | ||
// let's inject it as a constant instead of directly | ||
// pickking en vars in `process.env` to make our code | ||
// pickking env vars in `process.env` to make our code | ||
// easily testable | ||
@@ -104,3 +104,3 @@ $.register(constant('ENV', process.env)); | ||
// Let's do so for CLI args with another constant | ||
// in real world apps we would have create a service | ||
// in real world apps we would have created a service | ||
// that would parse args in a complexer way | ||
@@ -324,2 +324,43 @@ $.register(constant('ARGS', process.argv)); | ||
## Auto detection | ||
Knifecycle also provide some utility function to automatically assign | ||
the initializer property declarations, the following 3 ways to declare | ||
the `getUser` service are equivalent: | ||
```js | ||
import noop from 'noop'; | ||
import { autoInject, inject, initializer, autoService } from 'knifecycle'; | ||
initializer({ | ||
name: 'getUser', | ||
inject: ['db', '?log'], | ||
type: 'service', | ||
}, getUser); | ||
service('getUser', autoInject(getUser))); | ||
autoService(getUser); | ||
async function getUser({ db, log = noop}) {} | ||
``` | ||
That said, if your need to build your code with `webpack`/`babel` you may | ||
have to convert auto-detections to raw declarations with the | ||
[babel-plugin-knifecycle](https://github.com/nfroidure/babel-plugin-knifecycle) | ||
plugin. | ||
Also, keep in mind that the auto-detection is based on a simple regular | ||
expression so you should care to keep initializer signatures simple to | ||
avoid having a `E_AUTO_INJECTION_FAILURE` error. As a rule of thumb, | ||
avoid setting complex default values. | ||
```js | ||
// Won't work | ||
autoInject(async ({ log = () => {} }) => {}); | ||
// Will work | ||
function noop() {} | ||
autoInject(async ({ log = noop }) => {}); | ||
``` | ||
## Debugging | ||
@@ -377,3 +418,3 @@ | ||
</dd> | ||
<dt><a href="#inject">inject(dependenciesDeclarations, initializer)</a> ⇒ <code>function</code></dt> | ||
<dt><a href="#inject">inject(dependencies, initializer)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorator creating a new initializer with different | ||
@@ -387,3 +428,3 @@ dependencies declarations set to it.</p> | ||
</dd> | ||
<dt><a href="#alsoInject">alsoInject(dependenciesDeclarations, initializer)</a> ⇒ <code>function</code></dt> | ||
<dt><a href="#alsoInject">alsoInject(dependencies, initializer)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorator creating a new initializer with some | ||
@@ -417,11 +458,20 @@ more dependencies declarations appended to it.</p> | ||
</dd> | ||
<dt><a href="#service">service(name, initializer, options)</a> ⇒ <code>function</code></dt> | ||
<dt><a href="#service">service(builder, [name], [dependencies], [options])</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorator that creates an initializer for a service</p> | ||
</dd> | ||
<dt><a href="#provider">provider(name, provider, options)</a> ⇒ <code>function</code></dt> | ||
<dt><a href="#autoService">autoService(initializer)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorator that auto creates a service</p> | ||
</dd> | ||
<dt><a href="#provider">provider(builder, [name], [dependencies], [options])</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorator that creates an initializer for a provider</p> | ||
</dd> | ||
<dt><a href="#handler">handler(handlerFunction, [dependencies], [extra])</a> ⇒ <code>function</code></dt> | ||
<dt><a href="#autoProvider">autoProvider(initializer)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Decorator that auto creates a provider</p> | ||
</dd> | ||
<dt><a href="#handler">handler(handlerFunction, [name], [dependencies], [options])</a> ⇒ <code>function</code></dt> | ||
<dd><p>Shortcut to create an initializer with a simple handler</p> | ||
</dd> | ||
<dt><a href="#autoHandler">autoHandler(handlerFunction)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Allows to create an initializer with a simple handler automagically</p> | ||
</dd> | ||
<dt><a href="#parseDependencyDeclaration">parseDependencyDeclaration(dependencyDeclaration)</a> ⇒ <code>Object</code></dt> | ||
@@ -651,3 +701,3 @@ <dd><p>Explode a dependency declaration an returns its parts.</p> | ||
## inject(dependenciesDeclarations, initializer) ⇒ <code>function</code> | ||
## inject(dependencies, initializer) ⇒ <code>function</code> | ||
Decorator creating a new initializer with different | ||
@@ -661,3 +711,3 @@ dependencies declarations set to it. | ||
| --- | --- | --- | | ||
| dependenciesDeclarations | <code>Array.<String></code> | List of dependencies declarations to declare which services the initializer needs to resolve its own service | | ||
| dependencies | <code>Array.<String></code> | List of dependencies declarations to declare which services the initializer needs to resolve its own service | | ||
| initializer | <code>function</code> | The initializer to tweak | | ||
@@ -673,4 +723,4 @@ | ||
service( | ||
inject(['ENV'], myServiceInitializer) | ||
'myService', | ||
inject(['ENV'], myServiceInitializer) | ||
) | ||
@@ -713,3 +763,3 @@ ) | ||
## alsoInject(dependenciesDeclarations, initializer) ⇒ <code>function</code> | ||
## alsoInject(dependencies, initializer) ⇒ <code>function</code> | ||
Decorator creating a new initializer with some | ||
@@ -723,3 +773,3 @@ more dependencies declarations appended to it. | ||
| --- | --- | --- | | ||
| dependenciesDeclarations | <code>Array.<String></code> | List of dependencies declarations to append | | ||
| dependencies | <code>Array.<String></code> | List of dependencies declarations to append | | ||
| initializer | <code>function</code> | The initializer to tweak | | ||
@@ -733,4 +783,5 @@ | ||
new Knifecycle() | ||
.register(service('myService', | ||
alsoInject(['ENV'], myServiceInitializer) | ||
.register(service( | ||
alsoInject(['ENV'], myServiceInitializer), | ||
'myService', | ||
)); | ||
@@ -762,4 +813,5 @@ ``` | ||
new Knifecycle() | ||
.register(service('myService', | ||
extra({ httpHandler: true }, myServiceInitializer) | ||
.register(service( | ||
extra({ httpHandler: true }, myServiceInitializer), | ||
'myService', | ||
)); | ||
@@ -788,6 +840,7 @@ ``` | ||
new Knifecycle() | ||
.register(service('myService', | ||
.register(service( | ||
inject(['ENV'], | ||
options({ singleton: true}, myServiceInitializer) | ||
) | ||
), | ||
'myService', | ||
)); | ||
@@ -909,7 +962,6 @@ ``` | ||
.register(service( | ||
async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
'printAnswer', | ||
async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
{ | ||
inject: ['THE_NUMBER', 'log'], | ||
} | ||
['THE_NUMBER', 'log'], | ||
)) | ||
.run(['printAnswer']); | ||
@@ -921,3 +973,3 @@ | ||
## service(name, initializer, options) ⇒ <code>function</code> | ||
## service(builder, [name], [dependencies], [options]) ⇒ <code>function</code> | ||
Decorator that creates an initializer for a service | ||
@@ -930,5 +982,6 @@ | ||
| --- | --- | --- | | ||
| name | <code>String</code> | The service's name. | | ||
| initializer | <code>function</code> | An initializer returning the service promise | | ||
| options | <code>Object</code> | Options attached to the initializer | | ||
| builder | <code>function</code> | An initializer returning the service promise | | ||
| [name] | <code>String</code> | The service's name | | ||
| [dependencies] | <code>Array.<String></code> | The service's dependencies | | ||
| [options] | <code>Object</code> | Options attached to the built initializer | | ||
@@ -943,7 +996,7 @@ **Example** | ||
.register(service( | ||
async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
'printAnswer', | ||
async ({ THE_NUMBER, log }) => () => log(THE_NUMBER), | ||
{ | ||
inject: ['THE_NUMBER', 'log'], | ||
} | ||
['THE_NUMBER', 'log'], | ||
{ singleton: true } | ||
)) | ||
.run(['printAnswer']); | ||
@@ -953,5 +1006,17 @@ | ||
``` | ||
<a name="autoService"></a> | ||
## autoService(initializer) ⇒ <code>function</code> | ||
Decorator that auto creates a service | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - Returns a new initializer | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| initializer | <code>function</code> | An initializer returning the service promise | | ||
<a name="provider"></a> | ||
## provider(name, provider, options) ⇒ <code>function</code> | ||
## provider(builder, [name], [dependencies], [options]) ⇒ <code>function</code> | ||
Decorator that creates an initializer for a provider | ||
@@ -964,5 +1029,6 @@ | ||
| --- | --- | --- | | ||
| name | <code>String</code> | The provider's name. | | ||
| provider | <code>function</code> | A provider returning the service builder promise | | ||
| options | <code>Object</code> | Options attached to the initializer | | ||
| builder | <code>function</code> | A builder returning the provider promise | | ||
| [name] | <code>String</code> | The service's name | | ||
| [dependencies] | <code>Array.<String></code> | The service's dependencies | | ||
| [options] | <code>Object</code> | Options attached to the built initializer | | ||
@@ -976,3 +1042,5 @@ **Example** | ||
$.register(provider('config', async function configProvider() { | ||
$.register(provider(configProvider, 'config')); | ||
async function configProvider() { | ||
return new Promise((resolve, reject) { | ||
@@ -999,7 +1067,19 @@ fs.readFile('config.js', function(err, data) { | ||
}); | ||
})); | ||
} | ||
``` | ||
<a name="autoProvider"></a> | ||
## autoProvider(initializer) ⇒ <code>function</code> | ||
Decorator that auto creates a provider | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - Returns a new initializer | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| initializer | <code>function</code> | An initializer returning the provider promise | | ||
<a name="handler"></a> | ||
## handler(handlerFunction, [dependencies], [extra]) ⇒ <code>function</code> | ||
## handler(handlerFunction, [name], [dependencies], [options]) ⇒ <code>function</code> | ||
Shortcut to create an initializer with a simple handler | ||
@@ -1013,11 +1093,12 @@ | ||
| handlerFunction | <code>function</code> | | The handler function | | ||
| [dependencies] | <code>Array</code> | <code>[]</code> | The dependencies to inject in it | | ||
| [extra] | <code>Object</code> | | Optional extra data to associate with the handler | | ||
| [name] | <code>String</code> | | The name of the handler. Default to the DI prop if exists | | ||
| [dependencies] | <code>Array.<String></code> | <code>[]</code> | The dependencies to inject in it | | ||
| [options] | <code>Object</code> | | Options attached to the built initializer | | ||
**Example** | ||
```js | ||
import Knifecycle, { initializer } from 'knifecycle'; | ||
import Knifecycle, { handler } from 'knifecycle'; | ||
new Knifecycle() | ||
.register(handler(getUser, ['db', '?log'])); | ||
.register(handler(getUser, 'getUser', ['db', '?log'])); | ||
@@ -1031,2 +1112,28 @@ const QUERY = `SELECT * FROM users WHERE id=$1` | ||
``` | ||
<a name="autoHandler"></a> | ||
## autoHandler(handlerFunction) ⇒ <code>function</code> | ||
Allows to create an initializer with a simple handler automagically | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - Returns a new initializer | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| handlerFunction | <code>function</code> | The handler function | | ||
**Example** | ||
```js | ||
import Knifecycle, { autoHandler } from 'knifecycle'; | ||
new Knifecycle() | ||
.register(autoHandler(getUser)); | ||
const QUERY = `SELECT * FROM users WHERE id=$1` | ||
async function getUser({ db }, userId) { | ||
const [row] = await db.query(QUERY, userId); | ||
return row; | ||
} | ||
``` | ||
<a name="parseDependencyDeclaration"></a> | ||
@@ -1033,0 +1140,0 @@ |
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
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
198891
3509
1141