Comparing version 0.1.3 to 0.1.4
import { Constructor } from "./interfaces/constructor"; | ||
import { ConstructorParams } from "./interfaces/constructor-params"; | ||
import { RegisteredInstanceType } from "./interfaces/registered-instance-type"; | ||
import { RegisteredType } from "./interfaces/registered-type"; | ||
declare const container: { | ||
registerType: (implementationType: RegisteredInstanceType) => { | ||
as: (instanceType: InstanceType<any>) => { | ||
registerType: (implementationType: InstanceType<RegisteredType>) => { | ||
as: (type: any) => { | ||
with: (...injectionParams: any[]) => void; | ||
@@ -11,6 +11,6 @@ }; | ||
registerInstance: (instance: any) => { | ||
as: (instanceType: InstanceType<any>) => void; | ||
as: (type: any) => void; | ||
}; | ||
resolve: <TType extends Constructor & Partial<ConstructorParams> = any>(instanceType: TType, ...args: any[]) => InstanceType<TType>; | ||
resolve: <TType extends Constructor & Partial<ConstructorParams> = any>(type: TType, ...args: any[]) => InstanceType<TType>; | ||
}; | ||
export { container }; |
@@ -19,7 +19,7 @@ "use strict"; | ||
return { | ||
as: (instanceType) => { | ||
if (dependencies.has(instanceType)) { | ||
throw new Error(`The instance type (${instanceType.name}) is already registered`); | ||
as: (type) => { | ||
if (dependencies.has(type)) { | ||
throw new Error(`The instance type (${type.name}) is already registered`); | ||
} | ||
dependencies.set(instanceType, implementationType); | ||
dependencies.set(type, implementationType); | ||
return { | ||
@@ -35,23 +35,23 @@ with: (...injectionParams) => { | ||
return { | ||
as: (instanceType) => { | ||
if (dependencies.has(instanceType)) { | ||
throw new Error(`The instance type (${instanceType.name}) is already registered`); | ||
as: (type) => { | ||
if (dependencies.has(type)) { | ||
throw new Error(`The instance type (${type.name}) is already registered`); | ||
} | ||
dependencies.set(instanceType, instance); | ||
dependencies.set(type, instance); | ||
}, | ||
}; | ||
}, | ||
resolve: function (instanceType, ...args) { | ||
if (dependencies.has(instanceType)) { | ||
return makeInstance(instanceType, args); | ||
resolve: function (type, ...args) { | ||
if (dependencies.has(type)) { | ||
return makeInstance(type, args); | ||
} | ||
if (!instanceType.__constructorParams || !Array.isArray(instanceType.__constructorParams)) { | ||
throw new Error(`The ${instanceType.name} cannot be resolved by Dependency Injection`); | ||
if (!type.__constructorParams || !Array.isArray(type.__constructorParams)) { | ||
throw new Error(`The ${type.name} cannot be resolved by Dependency Injection`); | ||
} | ||
const dependencyArguments = []; | ||
instanceType.__constructorParams.forEach((type) => { | ||
type.__constructorParams.forEach((type) => { | ||
const instance = container.resolve(type); | ||
dependencyArguments.push(instance); | ||
}); | ||
return new instanceType(...[ | ||
return new type(...[ | ||
...dependencyArguments, | ||
@@ -58,0 +58,0 @@ ...args, |
{ | ||
"name": "cheap-di", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "JavaScript dependency injection like Autofac in .Net", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
12177
27
116