@ogre-tools/injectable
Advanced tools
Comparing version 5.1.1 to 5.1.2
@@ -6,2 +6,13 @@ # Change Log | ||
### [5.1.2](https://github.com/ogre-works/ogre-tools/compare/v5.1.1...v5.1.2) (2022-03-14) | ||
### Bug Fixes | ||
* Report misconfigured *.injectable.{js|ts} files ([8117f16](https://github.com/ogre-works/ogre-tools/commit/8117f1614a49b2202eb8620d280dfed6e6f1fcb6)) | ||
* Unit tests ([435690e](https://github.com/ogre-works/ogre-tools/commit/435690e1095c0214aece8bb81d54372e706102e7)) | ||
* Uppercase for errors ([8b48b69](https://github.com/ogre-works/ogre-tools/commit/8b48b69843a0c4e2994b2b498fd14a6a48870742)) | ||
### [5.1.1](https://github.com/ogre-works/ogre-tools/compare/v5.1.0...v5.1.1) (2022-03-11) | ||
@@ -8,0 +19,0 @@ |
@@ -169,4 +169,9 @@ /// <reference types="jest" /> | ||
export function createContainer(...getRequireContexts: any[]): DiContainer; | ||
export interface RequireContext { | ||
keys(): string[]; | ||
(key: string): any; | ||
} | ||
export function createContainer(...getRequireContexts: (() => RequireContext)[]): DiContainer; | ||
export function registerErrorMonitoring(di: DiContainer): void; | ||
@@ -173,0 +178,0 @@ |
{ | ||
"name": "@ogre-tools/injectable", | ||
"private": false, | ||
"version": "5.1.1", | ||
"version": "5.1.2", | ||
"description": "A brutal dependency injection container", | ||
@@ -19,6 +19,6 @@ "repository": { | ||
"dependencies": { | ||
"@ogre-tools/fp": "^5.1.1", | ||
"@ogre-tools/fp": "^5.1.2", | ||
"lodash": "^4.17.21" | ||
}, | ||
"gitHead": "3eca1cfa39e21c20cecd939bce7e578a498d5af1", | ||
"gitHead": "7a6a2d11c9b4ade8e16863b9e28586d217ca4fed", | ||
"bugs": { | ||
@@ -25,0 +25,0 @@ "url": "https://github.com/ogre-works/ogre-tools/issues" |
@@ -27,2 +27,3 @@ import conforms from 'lodash/fp/conforms'; | ||
import curry from 'lodash/fp/curry'; | ||
import isString from 'lodash/fp/isString'; | ||
import overSome from 'lodash/fp/overSome'; | ||
@@ -302,10 +303,7 @@ | ||
const autoRegisterInjectables = ({ getRequireContextForInjectables, di }) => { | ||
const requireContextForInjectables = getRequireContextForInjectables(); | ||
pipeline( | ||
requireContextForInjectables, | ||
invoke('keys'), | ||
map(requireContextForInjectables), | ||
map('default'), | ||
forEach(di.register), | ||
getRequireContextForInjectables(), | ||
fileNameAndDefaultExport, | ||
tap(forEach(verifyInjectable)), | ||
forEach(registerInjectableFor(di)), | ||
); | ||
@@ -336,11 +334,4 @@ }; | ||
}) => { | ||
if (!injectable.instantiate) { | ||
throw new Error( | ||
`Tried to inject "${injectable.id}" when instantiation is not defined.`, | ||
); | ||
} | ||
const newContext = [ | ||
...oldContext, | ||
{ | ||
@@ -445,18 +436,18 @@ injectable, | ||
({ injectMany }) => | ||
toBeDecorated => | ||
(alias, ...args) => { | ||
if (alias.decorable === false) { | ||
return toBeDecorated(alias, ...args); | ||
} | ||
toBeDecorated => | ||
(alias, ...args) => { | ||
if (alias.decorable === false) { | ||
return toBeDecorated(alias, ...args); | ||
} | ||
const isRelevantDecorator = isRelevantDecoratorFor(alias); | ||
const isRelevantDecorator = isRelevantDecoratorFor(alias); | ||
const decorators = pipeline( | ||
injectMany(injectionDecoratorToken), | ||
filter(isRelevantDecorator), | ||
map('decorate'), | ||
); | ||
const decorators = pipeline( | ||
injectMany(injectionDecoratorToken), | ||
filter(isRelevantDecorator), | ||
map('decorate'), | ||
); | ||
return pipeline(toBeDecorated, ...decorators)(alias, ...args); | ||
}; | ||
return pipeline(toBeDecorated, ...decorators)(alias, ...args); | ||
}; | ||
@@ -498,4 +489,3 @@ const isGlobalDecorator = not(has('target')); | ||
throw new Error( | ||
`Tried to inject single injectable for injection token "${ | ||
alias.id | ||
`Tried to inject single injectable for injection token "${alias.id | ||
}" but found multiple injectables: "${relatedInjectables | ||
@@ -507,1 +497,23 @@ .map(relatedInjectable => relatedInjectable.id) | ||
}; | ||
const hasInjectableSignature = conforms({ id: isString, instantiate: isFunction }); | ||
const verifyInjectable = ([fileName, injectable]) => { | ||
if (!injectable) { | ||
throw new Error( | ||
`Tried to register injectable from ${fileName}, but no default export`, | ||
); | ||
} | ||
if (!hasInjectableSignature(injectable)) { | ||
throw new Error( | ||
`Tried to register injectable from ${fileName}, but default export is of wrong shape`, | ||
); | ||
} | ||
}; | ||
const fileNameAndDefaultExport = (context) => ( | ||
context.keys() | ||
.map(key => [key, context(key).default]) | ||
); | ||
const registerInjectableFor = (di) => ([, injectable]) => di.register(injectable); |
@@ -40,2 +40,4 @@ import getDi from '../test-utils/getDiForUnitTesting'; | ||
}, | ||
instantiate: () => 'irrelavent', | ||
}); | ||
@@ -67,2 +69,4 @@ | ||
setup: () => {}, | ||
instantiate: () => 'irrelavent', | ||
}); | ||
@@ -88,2 +92,4 @@ | ||
}, | ||
instantiate: () => 'irrelavent', | ||
}); | ||
@@ -90,0 +96,0 @@ |
import getInjectable from '../getInjectable/getInjectable'; | ||
import getDi from '../test-utils/getDiForUnitTesting'; | ||
import createContainer from './createContainer'; | ||
@@ -18,2 +19,65 @@ describe('createContainer.registration', () => { | ||
it('given injectable file with no default export, when auto-registering, throws with name of faulty file', () => { | ||
const requireContextStub = Object.assign( | ||
() => ({ | ||
notDefault: 'irrelevant', | ||
}), | ||
{ | ||
keys: () => ['./some.injectable.js'], | ||
}, | ||
); | ||
expect(() => createContainer(() => requireContextStub)).toThrowError( | ||
'Tried to register injectable from ./some.injectable.js, but no default export', | ||
); | ||
}); | ||
it('given injectable file with default export without id, when auto-registering, throws with name of faulty file', () => { | ||
const requireContextStub = Object.assign( | ||
() => ({ | ||
default: 'irrelevant', | ||
}), | ||
{ | ||
keys: () => ['./some.injectable.js'], | ||
}, | ||
); | ||
expect(() => createContainer(() => requireContextStub)).toThrowError( | ||
'Tried to register injectable from ./some.injectable.js, but default export is of wrong shape', | ||
); | ||
}); | ||
it('given injectable file with default export with in but without instantiate, when auto-registering, throws with name of faulty file', () => { | ||
const requireContextStub = Object.assign( | ||
() => ({ | ||
default: { | ||
id: 'irrelevant', | ||
}, | ||
}), | ||
{ | ||
keys: () => ['./some.injectable.js'], | ||
}, | ||
); | ||
expect(() => createContainer(() => requireContextStub)).toThrowError( | ||
'Tried to register injectable from ./some.injectable.js, but default export is of wrong shape', | ||
); | ||
}); | ||
it('given injectable file with default export of correct shape, when auto-registering, does not throw', () => { | ||
const requireContextStub = Object.assign( | ||
() => ({ | ||
default: { | ||
id: 'some-injectable-id', | ||
instantiate: () => { }, | ||
}, | ||
}), | ||
{ | ||
keys: () => ['./some.injectable.js'], | ||
}, | ||
); | ||
expect(() => createContainer(() => requireContextStub)).not.toThrow(); | ||
}); | ||
it('given manually registered injectable, when injecting, injects', () => { | ||
@@ -20,0 +84,0 @@ const di = getDi(); |
@@ -87,2 +87,4 @@ import asyncFn from '@async-fn/jest'; | ||
}, | ||
instantiate: () => 'irrelavent', | ||
}); | ||
@@ -121,2 +123,4 @@ | ||
setup: asyncFn(), | ||
instantiate: () => 'irrelavent', | ||
}); | ||
@@ -127,2 +131,4 @@ | ||
setup: asyncFn(), | ||
instantiate: () => 'irrelavent', | ||
}); | ||
@@ -146,2 +152,4 @@ | ||
setup: asyncFn(), | ||
instantiate: () => 'irrelavent', | ||
}); | ||
@@ -152,2 +160,4 @@ | ||
setup: asyncFn(), | ||
instantiate: () => 'irrelavent', | ||
}; | ||
@@ -166,19 +176,2 @@ | ||
it('given injectable with setup but no way to instantiate, when injected, throws', async () => { | ||
const someInjectable = getInjectable({ | ||
id: 'some-injectable', | ||
setup: () => {}, | ||
}); | ||
const di = getDi(someInjectable); | ||
await di.runSetups(); | ||
expect(() => { | ||
di.inject(someInjectable); | ||
}).toThrow( | ||
'Tried to inject "some-injectable" when instantiation is not defined.', | ||
); | ||
}); | ||
it('given injectable with setup but setups have not been ran, when injected, throws', () => { | ||
@@ -185,0 +178,0 @@ const someInjectable = getInjectable({ |
@@ -26,7 +26,6 @@ import createContainer from '../dependency-injection-container/createContainer'; | ||
const contextStub = contextKey => contextDictionary[contextKey]; | ||
contextStub.keys = () => keys(contextDictionary); | ||
return () => contextStub; | ||
return () => | ||
Object.assign(contextKey => contextDictionary[contextKey], { | ||
keys: () => keys(contextDictionary), | ||
}); | ||
}; |
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
186426
2662
0
Updated@ogre-tools/fp@^5.1.2