knifecycle
Advanced tools
Comparing version 9.1.1 to 10.0.0
@@ -0,1 +1,11 @@ | ||
# [10.0.0](https://github.com/nfroidure/knifecycle/compare/v9.1.1...v10.0.0) (2020-09-14) | ||
### Features | ||
* **utils:** allow object rest spread in autodected injections ([846fde9](https://github.com/nfroidure/knifecycle/commit/846fde91977d9a19c9a91d3b869e81866e870631)) | ||
* **utils:** allow to reuse initializers injections ([bc6873f](https://github.com/nfroidure/knifecycle/commit/bc6873f68db1edeebe1c5015cd4f1128a5a508a8)) | ||
## [9.1.1](https://github.com/nfroidure/knifecycle/compare/v9.1.0...v9.1.1) (2020-04-02) | ||
@@ -2,0 +12,0 @@ |
@@ -240,5 +240,5 @@ "use strict"; | ||
}) => ({ | ||
service: async dependenciesDeclarations => _buildFinalHash((await this._initializeDependencies($siloContext, $siloContext.name, dependenciesDeclarations, { | ||
service: async (dependenciesDeclarations) => _buildFinalHash(await this._initializeDependencies($siloContext, $siloContext.name, dependenciesDeclarations, { | ||
injectorContext: true | ||
})), dependenciesDeclarations) | ||
}), dependenciesDeclarations) | ||
}))); | ||
@@ -245,0 +245,0 @@ } |
@@ -48,3 +48,3 @@ "use strict"; | ||
_assert.default.deepEqual((await $.run(['TEST'])), { | ||
_assert.default.deepEqual(await $.run(['TEST']), { | ||
TEST: 2 | ||
@@ -56,3 +56,3 @@ }); | ||
_assert.default.deepEqual((await $.run(['TEST'])), { | ||
_assert.default.deepEqual(await $.run(['TEST']), { | ||
TEST: 1 | ||
@@ -59,0 +59,0 @@ }); |
@@ -9,2 +9,4 @@ "use strict"; | ||
exports.inject = inject; | ||
exports.useInject = useInject; | ||
exports.mergeInject = mergeInject; | ||
exports.autoInject = autoInject; | ||
@@ -162,2 +164,24 @@ exports.parseInjections = parseInjections; | ||
/** | ||
* Apply injected dependencies from the given initializer to another one | ||
* @param {Function} from The initialization function in which to pick the dependencies | ||
* @param {Function} to The destination initialization function | ||
* @return {Function} The newly built initialization function | ||
*/ | ||
function useInject(from, to) { | ||
return inject(from[SPECIAL_PROPS.INJECT] || [], to); | ||
} | ||
/** | ||
* Merge injected dependencies of the given initializer with another one | ||
* @param {Function} from The initialization function in which to pick the dependencies | ||
* @param {Function} to The destination initialization function | ||
* @return {Function} The newly built initialization function | ||
*/ | ||
function mergeInject(from, to) { | ||
return alsoInject(from[SPECIAL_PROPS.INJECT] || [], to); | ||
} | ||
/** | ||
* Decorator creating a new initializer with different | ||
@@ -200,3 +224,3 @@ * dependencies declarations set to it according to the | ||
}) { | ||
const matches = source.match(/^\s*(?:async\s+function(?:\s+\w+)?|async)\s*\(\s*\{\s*([^{}}]+)\s*\}/); | ||
const matches = source.match(/^\s*(?:async\s+function(?:\s+\w+)?|async)\s*\(\s*\{\s*([^{}]+)(\s*\.\.\.[^{}]+|)\s*\}/); | ||
@@ -215,3 +239,3 @@ if (!matches) { | ||
return matches[1].trim().replace(/,$/, '').split(/\s*,\s*/).map(injection => (injection.includes('=') ? '?' : '') + injection.split(/\s*=\s*/).shift().split(/\s*:\s*/).shift()).filter(injection => !/[)(\][]/.test(injection)); | ||
return matches[1].trim().replace(/,$/, '').split(/\s*,\s*/).map(s => s.trim()).filter(s => !s.startsWith('...')).map(injection => (injection.includes('=') ? '?' : '') + injection.split(/\s*=\s*/).shift().split(/\s*:\s*/).shift()).filter(injection => !/[)(\][]/.test(injection)); | ||
} | ||
@@ -241,17 +265,30 @@ /** | ||
function alsoInject(dependencies, initializer) { | ||
const dedupedDependencies = (initializer[SPECIAL_PROPS.INJECT] || []).concat(dependencies).map(parseDependencyDeclaration).reduce((currentDedupedDepencencies, dependencyDeclaration) => { | ||
const sameDependencyDeclaration = currentDedupedDepencencies.find(maybeSameDependencyDeclaration => maybeSameDependencyDeclaration.serviceName === dependencyDeclaration.serviceName); | ||
if (!sameDependencyDeclaration) { | ||
return currentDedupedDepencencies.concat(dependencyDeclaration); | ||
} | ||
if (sameDependencyDeclaration.mappedName !== dependencyDeclaration.mappedName) { | ||
return currentDedupedDepencencies.concat(dependencyDeclaration); | ||
} | ||
sameDependencyDeclaration.optional = dependencyDeclaration.optional && sameDependencyDeclaration.optional ? true : false; | ||
return currentDedupedDepencencies; | ||
}, []).map(stringifyDependencyDeclaration); // dedupe | ||
const currentDependencies = (initializer[SPECIAL_PROPS.INJECT] || []).map(parseDependencyDeclaration); | ||
const addedDependencies = dependencies.map(parseDependencyDeclaration); | ||
const dedupedDependencies = currentDependencies.filter(({ | ||
mappedName | ||
}) => { | ||
const declarationIsOverridden = addedDependencies.some(({ | ||
mappedName: addedMappedName | ||
}) => { | ||
return addedMappedName === mappedName; | ||
}); | ||
return !declarationIsOverridden; | ||
}).concat(addedDependencies.map(({ | ||
serviceName, | ||
mappedName, | ||
optional | ||
}) => { | ||
const isOptionalDependency = currentDependencies.some(({ | ||
optional, | ||
mappedName: addedMappedName | ||
}) => { | ||
return addedMappedName === mappedName && optional; | ||
}); | ||
return { | ||
serviceName, | ||
mappedName, | ||
optional: isOptionalDependency && optional | ||
}; | ||
})).map(stringifyDependencyDeclaration); | ||
return inject(dedupedDependencies, initializer); | ||
@@ -258,0 +295,0 @@ } |
@@ -11,8 +11,2 @@ "use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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() {} | ||
@@ -148,2 +142,36 @@ | ||
}); | ||
describe('useInject', () => { | ||
it('should set the right dependencies', () => { | ||
const fromDependencies = ['ENV', 'CORS']; | ||
const fromInitializer = (0, _util.inject)(fromDependencies, aProvider); | ||
const toDependencies = ['db', 'log']; | ||
const toInitializer = (0, _util.inject)(toDependencies, aProvider); | ||
const newInitializer = (0, _util.useInject)(fromInitializer, toInitializer); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], fromDependencies); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], toDependencies); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], [...fromDependencies]); | ||
}); | ||
}); | ||
describe('mergeInject', () => { | ||
it('should amend dependencies', () => { | ||
const fromDependencies = ['ENV', 'CORS']; | ||
const fromInitializer = (0, _util.inject)(fromDependencies, aProvider); | ||
const toDependencies = ['db', 'log']; | ||
const toInitializer = (0, _util.inject)(toDependencies, aProvider); | ||
const newInitializer = (0, _util.mergeInject)(fromInitializer, toInitializer); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], fromDependencies); | ||
_assert.default.notEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], toDependencies); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], [...toDependencies, ...fromDependencies]); | ||
}); | ||
}); | ||
describe('autoInject', () => { | ||
@@ -306,7 +334,16 @@ it('should allow to decorate an initializer with dependencies', () => { | ||
it('should dedupe dependencies', () => { | ||
const newInitializer = (0, _util.alsoInject)(['ENV', '?NODE_ENV', '?TEST', 'mysql>db'], (0, _util.alsoInject)(['ENV', 'NODE_ENV', '?TEST', 'mysql'], aProvider)); | ||
const baseProvider = (0, _util.inject)(['?TEST'], aProvider); | ||
const newInitializer = (0, _util.alsoInject)(['ENV', '?NODE_ENV', '?TEST', 'TEST2', 'mysql>db'], (0, _util.alsoInject)(['ENV', 'NODE_ENV', '?TEST', '?TEST2', 'mysql'], baseProvider)); | ||
_assert.default.notEqual(newInitializer, baseProvider); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], ['mysql', 'ENV', 'NODE_ENV', '?TEST', 'TEST2', 'mysql>db']); | ||
}); | ||
it('should solve final dependencies name clash', () => { | ||
const baseProvider = (0, _util.inject)(['?TEST'], aProvider); | ||
const newInitializer = (0, _util.alsoInject)(['ENV', '?NODE_ENV', '?TEST', 'mysql>db', 'log'], (0, _util.alsoInject)(['ENV', 'NODE_ENV', '?TEST', 'db', 'logger>log'], baseProvider)); | ||
_assert.default.notEqual(newInitializer, aProvider); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], ['ENV', 'NODE_ENV', '?TEST', 'mysql', 'mysql>db']); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.INJECT], ['ENV', 'NODE_ENV', '?TEST', 'mysql>db', 'log']); | ||
}); | ||
@@ -373,3 +410,5 @@ }); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], _objectSpread({}, rootOptions, {}, baseOptions)); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.OPTIONS], { ...rootOptions, | ||
...baseOptions | ||
}); | ||
}); | ||
@@ -527,3 +566,5 @@ }); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.EXTRA], _objectSpread({}, baseExtraInformations, {}, baseExtraInformations)); | ||
_assert.default.deepEqual(newInitializer[_util.SPECIAL_PROPS.EXTRA], { ...baseExtraInformations, | ||
...baseExtraInformations | ||
}); | ||
}); | ||
@@ -614,3 +655,3 @@ }); | ||
_assert.default.equal((await newInitializer()), baseValue); | ||
_assert.default.equal(await newInitializer(), baseValue); | ||
}); | ||
@@ -873,2 +914,36 @@ it('should fail with dependencies since it makes no sense', () => { | ||
}); | ||
it('should work with spread services', 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, | ||
...services | ||
}, ...args) { | ||
return Promise.resolve({ | ||
deps: { | ||
kikooo, | ||
lol, | ||
...services | ||
}, | ||
args | ||
}); | ||
} | ||
}); | ||
it('should fail for anonymous functions', () => { | ||
@@ -875,0 +950,0 @@ _assert.default.throws(() => { |
{ | ||
"name": "knifecycle", | ||
"version": "9.1.1", | ||
"version": "10.0.0", | ||
"description": "Manage your NodeJS processes's lifecycle automatically with an unobtrusive dependency injection implementation.", | ||
@@ -185,5 +185,3 @@ "main": "dist/index", | ||
"babel": { | ||
"plugins": [ | ||
"@babel/plugin-proposal-object-rest-spread" | ||
], | ||
"plugins": [], | ||
"presets": [ | ||
@@ -222,3 +220,3 @@ [ | ||
}, | ||
"modules": "false" | ||
"modules": false | ||
} | ||
@@ -225,0 +223,0 @@ ] |
@@ -434,2 +434,8 @@ [//]: # ( ) | ||
</dd> | ||
<dt><a href="#useInject">useInject(from, to)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Apply injected dependencies from the given initializer to another one</p> | ||
</dd> | ||
<dt><a href="#mergeInject">mergeInject(from, to)</a> ⇒ <code>function</code></dt> | ||
<dd><p>Merge injected dependencies of the given initializer with another one</p> | ||
</dd> | ||
<dt><a href="#autoInject">autoInject(initializer)</a> ⇒ <code>function</code></dt> | ||
@@ -763,2 +769,28 @@ <dd><p>Decorator creating a new initializer with different | ||
``` | ||
<a name="useInject"></a> | ||
## useInject(from, to) ⇒ <code>function</code> | ||
Apply injected dependencies from the given initializer to another one | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - The newly built initialization function | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| from | <code>function</code> | The initialization function in which to pick the dependencies | | ||
| to | <code>function</code> | The destination initialization function | | ||
<a name="mergeInject"></a> | ||
## mergeInject(from, to) ⇒ <code>function</code> | ||
Merge injected dependencies of the given initializer with another one | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - The newly built initialization function | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| from | <code>function</code> | The initialization function in which to pick the dependencies | | ||
| to | <code>function</code> | The destination initialization function | | ||
<a name="autoInject"></a> | ||
@@ -765,0 +797,0 @@ |
@@ -28,3 +28,9 @@ export type DependencyName = string; | ||
} | ||
export interface HandlerInitializer<D extends Dependencies, U extends any[], R, P = Parameters, S = Handler<P, U, R>> { | ||
export interface HandlerInitializer< | ||
D extends Dependencies, | ||
U extends any[], | ||
R, | ||
P = Parameters, | ||
S = Handler<P, U, R> | ||
> { | ||
(dependencies?: D): Promise<S>; | ||
@@ -65,7 +71,7 @@ } | ||
export interface SiloContext<S extends Service> { | ||
name: string, | ||
servicesDescriptors: Map<DependencyDeclaration, S>, | ||
servicesSequence: DependencyDeclaration[], | ||
servicesShutdownsPromises: Map<DependencyDeclaration, Promise<void>>, | ||
errorsPromises: Promise<void>[], | ||
name: string; | ||
servicesDescriptors: Map<DependencyDeclaration, S>; | ||
servicesSequence: DependencyDeclaration[]; | ||
servicesShutdownsPromises: Map<DependencyDeclaration, Promise<void>>; | ||
errorsPromises: Promise<void>[]; | ||
} | ||
@@ -80,21 +86,26 @@ | ||
): Knifecycle; | ||
toMermaidGraph({ shapes, styles, classes } : { | ||
shapes: ({ | ||
pattern: RegExp, | ||
template: string, | ||
})[], | ||
styles: ({ | ||
pattern: RegExp, | ||
className: string, | ||
})[], | ||
toMermaidGraph({ | ||
shapes, | ||
styles, | ||
classes, | ||
}: { | ||
shapes: { | ||
pattern: RegExp; | ||
template: string; | ||
}[]; | ||
styles: { | ||
pattern: RegExp; | ||
className: string; | ||
}[]; | ||
classes: { | ||
[name : string]: string, | ||
}, | ||
}): string | ||
[name: string]: string; | ||
}; | ||
}): string; | ||
} | ||
export function initializer<D extends Dependencies, S, T extends Initializer<D, S>>( | ||
declaration: InitializerDeclaration, | ||
initializer: T | ||
): T; | ||
export function initializer< | ||
D extends Dependencies, | ||
S, | ||
T extends Initializer<D, S> | ||
>(declaration: InitializerDeclaration, initializer: T): T; | ||
@@ -110,2 +121,3 @@ export function name<D extends Dependencies, S, T extends Initializer<D, S>>( | ||
>(initializer: T): T; | ||
export function type<D extends Dependencies, S, T extends Initializer<D, S>>( | ||
@@ -115,2 +127,3 @@ type: InitializerType, | ||
): T; | ||
export function inject<D extends Dependencies, S, T extends Initializer<D, S>>( | ||
@@ -120,2 +133,23 @@ dependencies: DependenciesDeclarations, | ||
): T; | ||
export function mergeInject<FD extends Dependencies, T>( | ||
from: Initializer<FD, unknown>, | ||
to: T, | ||
): T extends HandlerInitializer<infer D, infer U, infer R, infer P, infer TS> ? | ||
HandlerInitializer<FD & D, U, R, P, TS> : | ||
T extends ProviderInitializer<infer D, infer S> ? | ||
ProviderInitializer<FD & D, S> : | ||
T extends ServiceInitializer<infer D, infer S> ? | ||
ServiceInitializer<FD & D, S> : | ||
never; | ||
export function useInject<FD extends Dependencies, T>( | ||
from: Initializer<FD, unknown>, | ||
to: T, | ||
): T extends HandlerInitializer<any, infer U, infer R, infer P, infer TS> ? | ||
HandlerInitializer<FD, U, R, P, TS> : | ||
T extends ProviderInitializer<any, infer S> ? | ||
ProviderInitializer<FD, S> : | ||
T extends ServiceInitializer<any, infer S> ? | ||
ServiceInitializer<FD, S> : | ||
never; | ||
export function autoInject< | ||
@@ -127,6 +161,15 @@ D extends Dependencies, | ||
export function alsoInject< | ||
D extends Dependencies, | ||
S, | ||
T extends Initializer<D, S> | ||
>(dependencies: DependenciesDeclarations, initializer: T): T; | ||
ND extends Dependencies, | ||
T, | ||
>( | ||
dependencies: DependenciesDeclarations, | ||
initializer: T, | ||
): T extends HandlerInitializer<infer D, infer U, infer R, infer P, infer TS> ? | ||
HandlerInitializer<ND & D, U, R, P, TS> : | ||
T extends ProviderInitializer<infer D, infer S> ? | ||
ProviderInitializer<ND & D, S> : | ||
T extends ServiceInitializer<infer D, infer S> ? | ||
ServiceInitializer<ND & D, S> : | ||
never; | ||
export function options<D extends Dependencies, S, T extends Initializer<D, S>>( | ||
@@ -137,2 +180,3 @@ options: InitializerOptions, | ||
): T; | ||
export function extra<D extends Dependencies, S, T extends Initializer<D, S>>( | ||
@@ -142,7 +186,18 @@ data: any, | ||
): T; | ||
export function reuseSpecialProps< | ||
D extends Dependencies, | ||
S, | ||
T extends Initializer<D, S> | ||
>(from: T, to: T, amend?: InitializerOptions): T; | ||
FD extends Dependencies, | ||
T, | ||
>( | ||
from: Initializer<FD, unknown>, | ||
to: T, | ||
amend?: InitializerOptions, | ||
): T extends HandlerInitializer<infer D, infer U, infer R, infer P, infer TS> ? | ||
HandlerInitializer<FD & D, U, R, P, TS> : | ||
T extends ProviderInitializer<infer D, infer S> ? | ||
ProviderInitializer<FD & D, S> : | ||
T extends ServiceInitializer<infer D, infer S> ? | ||
ServiceInitializer<FD & D, S> : | ||
never; | ||
export function wrapInitializer< | ||
@@ -204,13 +259,11 @@ D extends Dependencies, | ||
R | ||
>( | ||
handlerFunction: HandlerFunction<D, P, U, R>, | ||
): HandlerInitializer<D, U, R, P>; | ||
>(handlerFunction: HandlerFunction<D, P, U, R>): HandlerInitializer<D, U, R, P>; | ||
export const SPECIAL_PROPS: { | ||
INJECT: string, | ||
OPTIONS: string, | ||
NAME: string, | ||
TYPE: string, | ||
EXTRA: string, | ||
VALUE: string, | ||
INJECT: string; | ||
OPTIONS: string; | ||
NAME: string; | ||
TYPE: string; | ||
EXTRA: string; | ||
VALUE: string; | ||
}; | ||
@@ -217,0 +270,0 @@ export const DECLARATION_SEPARATOR: string; |
@@ -131,3 +131,24 @@ import YError from 'yerror'; | ||
} | ||
/** | ||
* Apply injected dependencies from the given initializer to another one | ||
* @param {Function} from The initialization function in which to pick the dependencies | ||
* @param {Function} to The destination initialization function | ||
* @return {Function} The newly built initialization function | ||
*/ | ||
export function useInject(from, to) { | ||
return inject(from[SPECIAL_PROPS.INJECT] || [], to); | ||
} | ||
/** | ||
* Merge injected dependencies of the given initializer with another one | ||
* @param {Function} from The initialization function in which to pick the dependencies | ||
* @param {Function} to The destination initialization function | ||
* @return {Function} The newly built initialization function | ||
*/ | ||
export function mergeInject(from, to) { | ||
return alsoInject(from[SPECIAL_PROPS.INJECT] || [], to); | ||
} | ||
/** | ||
* Decorator creating a new initializer with different | ||
@@ -169,3 +190,3 @@ * dependencies declarations set to it according to the | ||
const matches = source.match( | ||
/^\s*(?:async\s+function(?:\s+\w+)?|async)\s*\(\s*\{\s*([^{}}]+)\s*\}/, | ||
/^\s*(?:async\s+function(?:\s+\w+)?|async)\s*\(\s*\{\s*([^{}]+)(\s*\.\.\.[^{}]+|)\s*\}/, | ||
); | ||
@@ -190,2 +211,4 @@ | ||
.split(/\s*,\s*/) | ||
.map((s) => s.trim()) | ||
.filter((s) => !s.startsWith('...')) | ||
.map( | ||
@@ -224,32 +247,32 @@ (injection) => | ||
export function alsoInject(dependencies, initializer) { | ||
const dedupedDependencies = (initializer[SPECIAL_PROPS.INJECT] || []) | ||
.concat(dependencies) | ||
.map(parseDependencyDeclaration) | ||
.reduce((currentDedupedDepencencies, dependencyDeclaration) => { | ||
const sameDependencyDeclaration = currentDedupedDepencencies.find( | ||
(maybeSameDependencyDeclaration) => | ||
maybeSameDependencyDeclaration.serviceName === | ||
dependencyDeclaration.serviceName, | ||
const currentDependencies = (initializer[SPECIAL_PROPS.INJECT] || []).map( | ||
parseDependencyDeclaration, | ||
); | ||
const addedDependencies = dependencies.map(parseDependencyDeclaration); | ||
const dedupedDependencies = currentDependencies | ||
.filter(({ mappedName }) => { | ||
const declarationIsOverridden = addedDependencies.some( | ||
({ mappedName: addedMappedName }) => { | ||
return addedMappedName === mappedName; | ||
}, | ||
); | ||
if (!sameDependencyDeclaration) { | ||
return currentDedupedDepencencies.concat(dependencyDeclaration); | ||
} | ||
return !declarationIsOverridden; | ||
}) | ||
.concat( | ||
addedDependencies.map(({ serviceName, mappedName, optional }) => { | ||
const isOptionalDependency = currentDependencies.some( | ||
({ optional, mappedName: addedMappedName }) => { | ||
return addedMappedName === mappedName && optional; | ||
}, | ||
); | ||
return { | ||
serviceName, | ||
mappedName, | ||
optional: isOptionalDependency && optional, | ||
}; | ||
}), | ||
) | ||
.map(stringifyDependencyDeclaration); | ||
if ( | ||
sameDependencyDeclaration.mappedName !== | ||
dependencyDeclaration.mappedName | ||
) { | ||
return currentDedupedDepencencies.concat(dependencyDeclaration); | ||
} | ||
sameDependencyDeclaration.optional = | ||
dependencyDeclaration.optional && sameDependencyDeclaration.optional | ||
? true | ||
: false; | ||
return currentDedupedDepencencies; | ||
}, []) | ||
.map(stringifyDependencyDeclaration); | ||
// dedupe | ||
return inject(dedupedDependencies, initializer); | ||
@@ -256,0 +279,0 @@ } |
@@ -13,2 +13,4 @@ import assert from 'assert'; | ||
alsoInject, | ||
useInject, | ||
mergeInject, | ||
parseInjections, | ||
@@ -126,3 +128,37 @@ options, | ||
}); | ||
describe('useInject', () => { | ||
it('should set the right dependencies', () => { | ||
const fromDependencies = ['ENV', 'CORS']; | ||
const fromInitializer = inject(fromDependencies, aProvider); | ||
const toDependencies = ['db', 'log']; | ||
const toInitializer = inject(toDependencies, aProvider); | ||
const newInitializer = useInject(fromInitializer, toInitializer); | ||
assert.notEqual(newInitializer, aProvider); | ||
assert.notEqual(newInitializer[SPECIAL_PROPS.INJECT], fromDependencies); | ||
assert.notEqual(newInitializer[SPECIAL_PROPS.INJECT], toDependencies); | ||
assert.deepEqual(newInitializer[SPECIAL_PROPS.INJECT], [ | ||
...fromDependencies, | ||
]); | ||
}); | ||
}); | ||
describe('mergeInject', () => { | ||
it('should amend dependencies', () => { | ||
const fromDependencies = ['ENV', 'CORS']; | ||
const fromInitializer = inject(fromDependencies, aProvider); | ||
const toDependencies = ['db', 'log']; | ||
const toInitializer = inject(toDependencies, aProvider); | ||
const newInitializer = mergeInject(fromInitializer, toInitializer); | ||
assert.notEqual(newInitializer, aProvider); | ||
assert.notEqual(newInitializer[SPECIAL_PROPS.INJECT], fromDependencies); | ||
assert.notEqual(newInitializer[SPECIAL_PROPS.INJECT], toDependencies); | ||
assert.deepEqual(newInitializer[SPECIAL_PROPS.INJECT], [ | ||
...toDependencies, | ||
...fromDependencies, | ||
]); | ||
}); | ||
}); | ||
describe('autoInject', () => { | ||
@@ -254,7 +290,29 @@ it('should allow to decorate an initializer with dependencies', () => { | ||
it('should dedupe dependencies', () => { | ||
const baseProvider = inject(['?TEST'], aProvider); | ||
const newInitializer = alsoInject( | ||
['ENV', '?NODE_ENV', '?TEST', 'mysql>db'], | ||
alsoInject(['ENV', 'NODE_ENV', '?TEST', 'mysql'], aProvider), | ||
['ENV', '?NODE_ENV', '?TEST', 'TEST2', 'mysql>db'], | ||
alsoInject(['ENV', 'NODE_ENV', '?TEST', '?TEST2', 'mysql'], baseProvider), | ||
); | ||
assert.notEqual(newInitializer, baseProvider); | ||
assert.deepEqual(newInitializer[SPECIAL_PROPS.INJECT], [ | ||
'mysql', | ||
'ENV', | ||
'NODE_ENV', | ||
'?TEST', | ||
'TEST2', | ||
'mysql>db', | ||
]); | ||
}); | ||
it('should solve final dependencies name clash', () => { | ||
const baseProvider = inject(['?TEST'], aProvider); | ||
const newInitializer = alsoInject( | ||
['ENV', '?NODE_ENV', '?TEST', 'mysql>db', 'log'], | ||
alsoInject( | ||
['ENV', 'NODE_ENV', '?TEST', 'db', 'logger>log'], | ||
baseProvider, | ||
), | ||
); | ||
assert.notEqual(newInitializer, aProvider); | ||
@@ -265,4 +323,4 @@ assert.deepEqual(newInitializer[SPECIAL_PROPS.INJECT], [ | ||
'?TEST', | ||
'mysql', | ||
'mysql>db', | ||
'log', | ||
]); | ||
@@ -774,2 +832,24 @@ }); | ||
it('should work with spread services', async () => { | ||
const services = { | ||
kikooo: 'kikooo', | ||
lol: 'lol', | ||
}; | ||
const theInitializer = autoHandler(sampleHandler); | ||
assert.deepEqual(theInitializer.$name, sampleHandler.name); | ||
assert.deepEqual(theInitializer.$inject, ['kikooo', 'lol']); | ||
const theHandler = await theInitializer(services); | ||
const result = await theHandler('test'); | ||
assert.deepEqual(result, { | ||
deps: services, | ||
args: ['test'], | ||
}); | ||
async function sampleHandler({ kikooo, lol, ...services }, ...args) { | ||
return Promise.resolve({ deps: { kikooo, lol, ...services }, args }); | ||
} | ||
}); | ||
it('should fail for anonymous functions', () => { | ||
@@ -776,0 +856,0 @@ assert.throws(() => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
1050866
11514
1239