Comparing version 3.2.1 to 3.2.3
@@ -1,8 +0,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.container = exports.ContainerImpl = void 0; | ||
const decorators_1 = require("./decorators"); | ||
const singleton_1 = require("./decorators/singleton"); | ||
const errors_1 = require("./errors"); | ||
const utils_1 = require("./utils"); | ||
import { getDependencies, getInjectedParams, setInjectedParams, isSingleton, di, } from './decorators'; | ||
import { modifySingleton } from './decorators/singleton'; | ||
import { CircularDependencyError } from './errors'; | ||
import { Trace } from './utils'; | ||
class ContainerImpl { | ||
@@ -17,7 +14,7 @@ constructor() { | ||
const withInjection = (...injectionParams) => { | ||
(0, decorators_1.setInjectedParams)(implementationType, injectionParams); | ||
setInjectedParams(implementationType, injectionParams); | ||
}; | ||
this.dependencies.set(implementationType, implementationType); | ||
if (!(0, decorators_1.getDependencies)(implementationType).length) { | ||
(0, decorators_1.di)(implementationType); | ||
if (!getDependencies(implementationType).length) { | ||
di(implementationType); | ||
} | ||
@@ -40,4 +37,4 @@ return Object.assign({ | ||
} | ||
if (!(0, decorators_1.isSingleton)(implementationType)) { | ||
(0, singleton_1.modifySingleton)(implementationType); | ||
if (!isSingleton(implementationType)) { | ||
modifySingleton(implementationType); | ||
} | ||
@@ -69,3 +66,3 @@ return { | ||
resolve(type, ...args) { | ||
const trace = new utils_1.Trace(type.name); | ||
const trace = new Trace(type.name); | ||
try { | ||
@@ -78,3 +75,3 @@ return this.internalResolve(type, trace, ...args); | ||
console.warn('cheap-di, circular dependencies tree', tree); | ||
throw new errors_1.CircularDependencyError(`Circular dependencies during resolve ${type.name}. ${error.message}`); | ||
throw new CircularDependencyError(`Circular dependencies during resolve ${type.name}. ${error.message}`); | ||
} | ||
@@ -102,3 +99,3 @@ throw error; | ||
} | ||
if ((0, decorators_1.isSingleton)(implementation)) { | ||
if (isSingleton(implementation)) { | ||
const singletons = this.getSingletons(); | ||
@@ -112,3 +109,3 @@ if (singletons.has(implementation)) { | ||
const injectionParams = [ | ||
...(0, decorators_1.getInjectedParams)(implementation), | ||
...getInjectedParams(implementation), | ||
...args, | ||
@@ -118,3 +115,3 @@ ]; | ||
let injectionParamsIndex = 0; | ||
const dependencies = (0, decorators_1.getDependencies)(implementation); | ||
const dependencies = getDependencies(implementation); | ||
if (dependencies.length) { | ||
@@ -154,3 +151,3 @@ let has = true; | ||
const target = new implementation(...injectableArguments); | ||
if ((0, decorators_1.isSingleton)(implementation)) { | ||
if (isSingleton(implementation)) { | ||
const singletons = this.getSingletons(); | ||
@@ -183,3 +180,2 @@ singletons.set(implementation, target); | ||
} | ||
exports.ContainerImpl = ContainerImpl; | ||
function isImplementationType(value) { | ||
@@ -189,3 +185,3 @@ return typeof value === 'function'; | ||
const container = new ContainerImpl(); | ||
exports.container = container; | ||
export { ContainerImpl, container, }; | ||
//# sourceMappingURL=ContainerImpl.js.map |
@@ -1,18 +0,14 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getDependencies = exports.dependencies = void 0; | ||
const InheritancePreserver_1 = require("./InheritancePreserver"); | ||
const symbols_1 = require("../symbols"); | ||
import { InheritancePreserver } from './InheritancePreserver'; | ||
import { dependenciesSymbol, dependenciesSymbol as dependencies_s } from '../symbols'; | ||
function dependencies(...dependencies) { | ||
return function (constructor) { | ||
constructor[symbols_1.dependenciesSymbol] = dependencies || []; | ||
InheritancePreserver_1.InheritancePreserver.constructorModified(constructor); | ||
constructor[dependencies_s] = dependencies || []; | ||
InheritancePreserver.constructorModified(constructor); | ||
return constructor; | ||
}; | ||
} | ||
exports.dependencies = dependencies; | ||
function getDependencies(constructor) { | ||
const implementation = constructor; | ||
const modifiedConstructor = InheritancePreserver_1.InheritancePreserver.getModifiedConstructor(constructor); | ||
const _dependencies = implementation[symbols_1.dependenciesSymbol]; | ||
const modifiedConstructor = InheritancePreserver.getModifiedConstructor(constructor); | ||
const _dependencies = implementation[dependenciesSymbol]; | ||
if (!modifiedConstructor || modifiedConstructor !== constructor || !Array.isArray(_dependencies)) { | ||
@@ -23,3 +19,3 @@ return []; | ||
} | ||
exports.getDependencies = getDependencies; | ||
export { dependencies, getDependencies }; | ||
//# sourceMappingURL=dependencies.js.map |
@@ -1,6 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.di = void 0; | ||
require("reflect-metadata"); | ||
const inject_1 = require("./inject"); | ||
import 'reflect-metadata'; | ||
import { inject } from './inject'; | ||
const primitiveConstructors = [ | ||
@@ -20,3 +17,3 @@ Object, | ||
if (type instanceof Object && !primitiveConstructors.includes(type)) { | ||
(0, inject_1.inject)(type)(constructor, type.name, index); | ||
inject(type)(constructor, type.name, index); | ||
} | ||
@@ -26,3 +23,3 @@ }); | ||
} | ||
exports.di = di; | ||
export { di }; | ||
//# sourceMappingURL=di.js.map |
@@ -1,11 +0,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isSingleton = exports.singleton = void 0; | ||
const tslib_1 = require("tslib"); | ||
(0, tslib_1.__exportStar)(require("./dependencies"), exports); | ||
(0, tslib_1.__exportStar)(require("./di"), exports); | ||
(0, tslib_1.__exportStar)(require("./inject"), exports); | ||
var singleton_1 = require("./singleton"); | ||
Object.defineProperty(exports, "singleton", { enumerable: true, get: function () { return singleton_1.singleton; } }); | ||
Object.defineProperty(exports, "isSingleton", { enumerable: true, get: function () { return singleton_1.isSingleton; } }); | ||
export * from './dependencies'; | ||
export * from './di'; | ||
export * from './inject'; | ||
export { singleton, isSingleton } from './singleton'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,14 +0,11 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InheritancePreserver = void 0; | ||
const symbols_1 = require("../symbols"); | ||
import { inheritancePreserveSymbol } from '../symbols'; | ||
class InheritancePreserver { | ||
static constructorModified(constructor) { | ||
constructor[symbols_1.inheritancePreserveSymbol] = constructor; | ||
constructor[inheritancePreserveSymbol] = constructor; | ||
} | ||
static getModifiedConstructor(constructor) { | ||
return constructor[symbols_1.inheritancePreserveSymbol]; | ||
return constructor[inheritancePreserveSymbol]; | ||
} | ||
} | ||
exports.InheritancePreserver = InheritancePreserver; | ||
export { InheritancePreserver }; | ||
//# sourceMappingURL=InheritancePreserver.js.map |
@@ -1,30 +0,25 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getInjectedParams = exports.setInjectedParams = exports.inject = void 0; | ||
const InheritancePreserver_1 = require("./InheritancePreserver"); | ||
const symbols_1 = require("../symbols"); | ||
import { InheritancePreserver } from './InheritancePreserver'; | ||
import { dependenciesSymbol, injectionSymbol } from '../symbols'; | ||
function inject(dependency) { | ||
return function (constructor, propertyKey, parameterIndex) { | ||
const implementation = constructor; | ||
// if class inherited from another one the static property will be contain the same array ref, | ||
// if class inherited from another one the static property will contain the same array ref, | ||
// and we shouldn't change dependencies of base class | ||
const modifiedConstructor = InheritancePreserver_1.InheritancePreserver.getModifiedConstructor(constructor); | ||
const modifiedConstructor = InheritancePreserver.getModifiedConstructor(constructor); | ||
const injectInNewClass = modifiedConstructor !== constructor; | ||
if (!implementation[symbols_1.dependenciesSymbol] || injectInNewClass) { | ||
implementation[symbols_1.dependenciesSymbol] = []; | ||
if (!implementation[dependenciesSymbol] || injectInNewClass) { | ||
implementation[dependenciesSymbol] = []; | ||
} | ||
implementation[symbols_1.dependenciesSymbol][parameterIndex] = dependency; | ||
InheritancePreserver_1.InheritancePreserver.constructorModified(constructor); | ||
implementation[dependenciesSymbol][parameterIndex] = dependency; | ||
InheritancePreserver.constructorModified(constructor); | ||
}; | ||
} | ||
exports.inject = inject; | ||
function setInjectedParams(implementationType, injectionParams) { | ||
implementationType[symbols_1.injectionSymbol] = injectionParams; | ||
InheritancePreserver_1.InheritancePreserver.constructorModified(implementationType); | ||
implementationType[injectionSymbol] = injectionParams; | ||
InheritancePreserver.constructorModified(implementationType); | ||
} | ||
exports.setInjectedParams = setInjectedParams; | ||
function getInjectedParams(constructor) { | ||
const implementation = constructor; | ||
const modifiedConstructor = InheritancePreserver_1.InheritancePreserver.getModifiedConstructor(constructor); | ||
const injected = implementation[symbols_1.injectionSymbol]; | ||
const modifiedConstructor = InheritancePreserver.getModifiedConstructor(constructor); | ||
const injected = implementation[injectionSymbol]; | ||
if (!modifiedConstructor || modifiedConstructor !== constructor || !Array.isArray(injected)) { | ||
@@ -35,3 +30,3 @@ return []; | ||
} | ||
exports.getInjectedParams = getInjectedParams; | ||
export { inject, setInjectedParams, getInjectedParams }; | ||
//# sourceMappingURL=inject.js.map |
@@ -1,25 +0,20 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isSingleton = exports.modifySingleton = exports.singleton = void 0; | ||
const di_1 = require("./di"); | ||
const InheritancePreserver_1 = require("./InheritancePreserver"); | ||
const symbols_1 = require("../symbols"); | ||
import { di } from './di'; | ||
import { InheritancePreserver } from './InheritancePreserver'; | ||
import { singletonSymbol as singleton_s } from '../symbols'; | ||
function singleton(constructor) { | ||
(0, di_1.di)(constructor); | ||
di(constructor); | ||
modifySingleton(constructor); | ||
return constructor; | ||
} | ||
exports.singleton = singleton; | ||
function modifySingleton(constructor) { | ||
constructor[symbols_1.singletonSymbol] = true; | ||
InheritancePreserver_1.InheritancePreserver.constructorModified(constructor); | ||
constructor[singleton_s] = true; | ||
InheritancePreserver.constructorModified(constructor); | ||
} | ||
exports.modifySingleton = modifySingleton; | ||
function isSingleton(constructor) { | ||
const modifiedConstructor = InheritancePreserver_1.InheritancePreserver.getModifiedConstructor(constructor); | ||
const modifiedConstructor = InheritancePreserver.getModifiedConstructor(constructor); | ||
return !!modifiedConstructor | ||
&& modifiedConstructor === constructor | ||
&& constructor[symbols_1.singletonSymbol] === true; | ||
&& constructor[singleton_s] === true; | ||
} | ||
exports.isSingleton = isSingleton; | ||
export { singleton, modifySingleton, isSingleton }; | ||
//# sourceMappingURL=singleton.js.map |
@@ -1,7 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CircularDependencyError = void 0; | ||
class CircularDependencyError extends Error { | ||
export class CircularDependencyError extends Error { | ||
} | ||
exports.CircularDependencyError = CircularDependencyError; | ||
//# sourceMappingURL=CircularDependencyError.js.map |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
(0, tslib_1.__exportStar)(require("./CircularDependencyError"), exports); | ||
export * from './CircularDependencyError'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,9 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
(0, tslib_1.__exportStar)(require("./ContainerImpl"), exports); | ||
(0, tslib_1.__exportStar)(require("./decorators"), exports); | ||
(0, tslib_1.__exportStar)(require("./errors"), exports); | ||
(0, tslib_1.__exportStar)(require("./types"), exports); | ||
(0, tslib_1.__exportStar)(require("./symbols"), exports); | ||
export * from './ContainerImpl'; | ||
export * from './decorators'; | ||
export * from './errors'; | ||
export * from './types'; | ||
export * from './symbols'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,12 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.inheritancePreserveSymbol = exports.injectionSymbol = exports.singletonSymbol = exports.dependenciesSymbol = void 0; | ||
const dependenciesSymbol = Symbol('cheap-di dependencies'); | ||
exports.dependenciesSymbol = dependenciesSymbol; | ||
const singletonSymbol = Symbol('cheap-di singleton'); | ||
exports.singletonSymbol = singletonSymbol; | ||
const injectionSymbol = Symbol('cheap-di injection params'); | ||
exports.injectionSymbol = injectionSymbol; | ||
const inheritancePreserveSymbol = Symbol('cheap-di injected constructor'); | ||
exports.inheritancePreserveSymbol = inheritancePreserveSymbol; | ||
export { dependenciesSymbol, singletonSymbol, injectionSymbol, inheritancePreserveSymbol, }; | ||
//# sourceMappingURL=symbols.js.map |
@@ -1,4 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const symbols_1 = require("./symbols"); | ||
import { singletonSymbol, dependenciesSymbol, injectionSymbol, inheritancePreserveSymbol } from './symbols'; | ||
//# sourceMappingURL=types.js.map |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
(0, tslib_1.__exportStar)(require("./Trace"), exports); | ||
export * from './Trace'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,4 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Trace = void 0; | ||
class Trace { | ||
@@ -48,3 +45,3 @@ constructor(type) { | ||
} | ||
exports.Trace = Trace; | ||
export { Trace }; | ||
//# sourceMappingURL=Trace.js.map |
{ | ||
"name": "cheap-di", | ||
"version": "3.2.1", | ||
"version": "3.2.3", | ||
"description": "JavaScript dependency injection like Autofac in .Net", | ||
@@ -5,0 +5,0 @@ "scripts": { |
# cheap-di | ||
JavaScript dependency injection like Autofac in .Net | ||
JavaScript's dependency injection like Autofac in .Net | ||
## How to use | ||
* [Minimal Sample](#minimal-sample) | ||
* [How to use](#how-to-use) | ||
* [TypeScript](#type-script) | ||
* [Decorators](#decorators) | ||
* [dependencies](#dependencies) | ||
* [inject](#inject) | ||
* [di](#di) | ||
* [singleton](#singleton) | ||
## <a name="minimal-sample"></a> Minimal Sample | ||
```ts | ||
abstract class Logger { | ||
abstract debug: (message: string) => void; | ||
} | ||
class ConsoleLogger implements Logger { | ||
constructor(prefix) { | ||
this.prefix = prefix; | ||
} | ||
debug(message: string) { | ||
console.log(`${this.prefix}: ${message}`); | ||
} | ||
} | ||
const metadata = <T>(constructor: T): T => constructor; | ||
@metadata | ||
class Service { | ||
constructor(private logger: Logger) {} | ||
doSome() { | ||
this.logger.debug('Hello world!'); | ||
} | ||
} | ||
// somewhere | ||
import { container } from 'cheap-di'; | ||
const myLogPrefix = 'INFO: '; | ||
container.registerType(ConsoleLogger).as(Logger).with(myLogPrefix); | ||
container.registerType(Service); | ||
const service = container.resolve(Service); | ||
service.doSome(); | ||
``` | ||
## <a name="how-to-use"></a> How to use | ||
You have an interface (base/abstract class) and its implementation (derived class) | ||
@@ -157,3 +206,3 @@ | ||
## TypeScript | ||
## <a name="type-script"></a> TypeScript | ||
@@ -227,3 +276,3 @@ `logger.ts` | ||
### Decorators | ||
## <a name="decorators"></a> Decorators | ||
@@ -235,3 +284,3 @@ If you want to use any of next decorators, you should add line below to your `tsconfig.json`: | ||
#### Dependencies | ||
### <a name="dependencies"></a> dependencies | ||
@@ -264,3 +313,3 @@ `@dependencies` decorator can be used to simplify dependency syntax | ||
#### Inject | ||
### <a name="inject"></a> inject | ||
@@ -297,3 +346,3 @@ `@inject` decorator can be used instead of `@dependencies` like below: | ||
#### Di | ||
### <a name="di"></a> di | ||
@@ -334,3 +383,3 @@ This decorator uses typescript reflection, so you should add line below to your `tsconfig.json`: | ||
#### Singleton | ||
### <a name="singleton"></a> singleton | ||
@@ -337,0 +386,0 @@ `@singleton` decorator allows you to inject the same instance everywhere. |
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
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
66668
409
45
484