Skip to content

Commit fd40a49

Browse files
committed
🐞 more secure tsconfig settings, fixed ts issues
1 parent 8155421 commit fd40a49

File tree

4 files changed

+165
-163
lines changed

4 files changed

+165
-163
lines changed

src/container.ts

Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,113 @@
1-
import {Definition, FactoryMethod, DefinitionObjectType} from './definition';
2-
import {ContainerResolver} from "./resolver";
3-
import {ContainerFactoryInterface} from "./containerfactoryinterface";
4-
import {ContainerFactory} from "./containerfactory";
1+
import { Definition, FactoryMethod, DefinitionObjectType } from './definition';
2+
import { ContainerResolver } from "./resolver";
3+
import { ContainerFactoryInterface } from "./containerfactoryinterface";
4+
import { ContainerFactory } from "./containerfactory";
55

66
/**
77
* DI Container class
88
*/
99
export class Container {
1010

11-
/**
12-
* Resolver
13-
*/
14-
private resolver: ContainerResolver;
11+
/**
12+
* Resolver
13+
*/
14+
private resolver: ContainerResolver;
1515

1616

17-
/**
18-
* If true allows to resolve unregistered definitions
19-
* @type {boolean}
20-
*/
21-
private allowUnregisteredResolve: Boolean = false;
17+
/**
18+
* If true allows to resolve unregistered definitions
19+
* @type {boolean}
20+
*/
21+
private allowUnregisteredResolve: Boolean = false;
2222

23-
/**
24-
* @constructor
25-
*/
26-
public constructor() {
27-
this.resolver = new ContainerResolver();
28-
let containerFactory = new ContainerFactory(this.resolver);
23+
/**
24+
* @constructor
25+
*/
26+
public constructor() {
27+
this.resolver = new ContainerResolver();
28+
let containerFactory = new ContainerFactory(this.resolver);
2929

30-
this.register(ContainerFactoryInterface, containerFactory);
31-
}
30+
this.register(ContainerFactoryInterface, containerFactory);
31+
}
3232

33-
/**
34-
* Sets if unregistered definitions are allowed to be resolved
35-
* This is useful to avoid many stub container.register(Class) class, but you need to be careful to not create
36-
* interface instead of implementation
37-
* @param allow true to allow, false to disallow
38-
*/
39-
public setAllowUnregisteredResolving(allow: Boolean) {
40-
this.allowUnregisteredResolve = allow;
41-
}
33+
/**
34+
* Sets if unregistered definitions are allowed to be resolved
35+
* This is useful to avoid many stub container.register(Class) class, but you need to be careful to not create
36+
* interface instead of implementation
37+
* @param allow true to allow, false to disallow
38+
*/
39+
public setAllowUnregisteredResolving(allow: Boolean) {
40+
this.allowUnregisteredResolve = allow;
41+
}
4242

43-
/**
44-
* Register implementation to interface object
45-
* @param definition
46-
* @param implementationOrConstructorArgs
47-
* @param constructorArgs
48-
*/
49-
public register(definition: string|Function, implementationOrConstructorArgs?: Object|Function|Array<any>, constructorArgs?: Array<any>): Definition {
50-
let def: Definition;
51-
if (!implementationOrConstructorArgs) {
52-
// specific case: register(Class)
53-
if (typeof definition === "string") {
54-
throw new Error("Can't register just symbol");
55-
}
56-
def = new Definition(definition, definition);
43+
/**
44+
* Register implementation to interface object
45+
* @param definition
46+
* @param implementationOrConstructorArgs
47+
* @param constructorArgs
48+
*/
49+
public register(definition: string | Function, implementationOrConstructorArgs?: Object | Function | Array<any>, constructorArgs?: Array<any>): Definition {
50+
let def: Definition;
51+
if (!implementationOrConstructorArgs) {
52+
// specific case: register(Class)
53+
if (typeof definition === "string") {
54+
throw new Error("Can't register just symbol");
55+
}
56+
def = new Definition(definition, definition);
57+
} else {
58+
if (typeof definition === "function") {
59+
// Cases:
60+
// 1. register(Class, Class)
61+
// 2. register(Class, [constructorArgs])
62+
// 3. register(Class, Class, [constructorArgs])
63+
// 4. register(Class, object)
64+
if (implementationOrConstructorArgs instanceof Array) {
65+
// Case 2.
66+
def = new Definition(definition, definition, implementationOrConstructorArgs);
5767
} else {
58-
if (typeof definition === "function") {
59-
// Cases:
60-
// 1. register(Class, Class)
61-
// 2. register(Class, [constructorArgs])
62-
// 3. register(Class, Class, [constructorArgs])
63-
// 4. register(Class, object)
64-
if (implementationOrConstructorArgs instanceof Array) {
65-
// Case 2.
66-
def = new Definition(definition, definition, implementationOrConstructorArgs);
67-
} else {
68-
if (typeof implementationOrConstructorArgs == "object") {
69-
// Case 4
70-
def = new Definition(definition, implementationOrConstructorArgs, null, FactoryMethod.OBJECT);
71-
} else {
72-
// Cases 1, 3
73-
def = new Definition(definition, implementationOrConstructorArgs, constructorArgs);
74-
}
75-
}
76-
} else {
77-
// Cases:
78-
// 1. register('string', Class)
79-
// 2. register('string', Class, [constructorArgs])
80-
// 3. register('string', 'number|string|object|boolean')
81-
if (typeof implementationOrConstructorArgs === "function") {
82-
// Case 1,2
83-
def = new Definition(definition, implementationOrConstructorArgs, constructorArgs);
84-
} else {
85-
// Case 3
86-
def = new Definition(definition, implementationOrConstructorArgs, null, FactoryMethod.OBJECT);
87-
}
88-
89-
}
68+
if (typeof implementationOrConstructorArgs == "object") {
69+
// Case 4
70+
def = new Definition(definition, implementationOrConstructorArgs, undefined, FactoryMethod.OBJECT);
71+
} else {
72+
// Cases 1, 3
73+
def = new Definition(definition, implementationOrConstructorArgs, constructorArgs);
74+
}
75+
}
76+
} else {
77+
// Cases:
78+
// 1. register('string', Class)
79+
// 2. register('string', Class, [constructorArgs])
80+
// 3. register('string', 'number|string|object|boolean')
81+
if (typeof implementationOrConstructorArgs === "function") {
82+
// Case 1,2
83+
def = new Definition(definition, implementationOrConstructorArgs, constructorArgs);
84+
} else {
85+
// Case 3
86+
def = new Definition(definition, implementationOrConstructorArgs, undefined, FactoryMethod.OBJECT);
9087
}
91-
this.resolver.addDefinition(definition, def);
92-
return def;
93-
}
9488

95-
/**
96-
* Register definition as callable. The callable will be invoked instead calling via new()
97-
*/
98-
public registerCallable(definition: string|Function, callable: () => any): Definition {
99-
let def: Definition;
100-
def = new Definition(definition, callable, null, FactoryMethod.SINGLETON, DefinitionObjectType.CALLABLE);
101-
this.resolver.addDefinition(definition, def);
102-
return def;
89+
}
10390
}
91+
this.resolver.addDefinition(definition, def);
92+
return def;
93+
}
10494

105-
/**
106-
* Resolves definition
107-
* @param definition
108-
* @param method
109-
*/
110-
public resolve(definition: string|Function, method?: FactoryMethod): any {
111-
return this.resolver.resolve(definition, method, undefined, !this.allowUnregisteredResolve);
112-
}
95+
/**
96+
* Register definition as callable. The callable will be invoked instead calling via new()
97+
*/
98+
public registerCallable(definition: string | Function, callable: () => any): Definition {
99+
let def: Definition;
100+
def = new Definition(definition, callable, undefined, FactoryMethod.SINGLETON, DefinitionObjectType.CALLABLE);
101+
this.resolver.addDefinition(definition, def);
102+
return def;
103+
}
104+
105+
/**
106+
* Resolves definition
107+
* @param definition
108+
* @param method
109+
*/
110+
public resolve(definition: string | Function, method?: FactoryMethod): any {
111+
return this.resolver.resolve(definition, method, undefined, !this.allowUnregisteredResolve);
112+
}
113113
}

src/definition.ts

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
11
export enum FactoryMethod {
2-
SINGLETON,
3-
FACTORY,
4-
OBJECT
2+
SINGLETON,
3+
FACTORY,
4+
OBJECT
55
}
66

77
export enum DefinitionObjectType {
8-
CLASS,
9-
CALLABLE
8+
CLASS,
9+
CALLABLE
1010
}
1111

1212
export class Definition {
1313

14-
/**
15-
* Definition key
16-
* @type {string|Function}
17-
*/
18-
public key: string|Function;
14+
/**
15+
* Definition key
16+
* @type {string|Function}
17+
*/
18+
public key: string | Function;
1919

20-
/**
21-
* Method to create objects. Factory by default
22-
* @type {FactoryMethod}
23-
*/
24-
public method: FactoryMethod = FactoryMethod.FACTORY;
20+
/**
21+
* Method to create objects. Factory by default
22+
* @type {FactoryMethod}
23+
*/
24+
public method: FactoryMethod = FactoryMethod.FACTORY;
2525

26-
/**
27-
* Constructor arguments to be passed when creating object
28-
* type {Array}
29-
*/
30-
public constructorArgs: Array<any>;
26+
/**
27+
* Constructor arguments to be passed when creating object
28+
* type {Array}
29+
*/
30+
public constructorArgs?: Array<any>;
3131

32-
/**
33-
* Constructor function or callable
34-
* type {Function}
35-
*/
36-
public definitionConstructor: any;
32+
/**
33+
* Constructor function or callable
34+
* type {Function}
35+
*/
36+
public definitionConstructor: any;
3737

38-
/**
39-
* Object type
40-
* @type {DefinitionObjectType}
41-
*/
42-
public definitionObjectType: DefinitionObjectType = DefinitionObjectType.CLASS;
38+
/**
39+
* Object type
40+
* @type {DefinitionObjectType}
41+
*/
42+
public definitionObjectType: DefinitionObjectType = DefinitionObjectType.CLASS;
4343

44-
/**
45-
* @constructor
46-
* @param key
47-
* @param definitionConstructor
48-
* @param constructorArgs
49-
* @param factoryMethod
50-
* @param objectType
51-
*/
52-
public constructor(key: string|Function, definitionConstructor: any, constructorArgs?: Array<any>, factoryMethod?: FactoryMethod, objectType?: DefinitionObjectType) {
53-
this.key = key;
54-
this.definitionConstructor = definitionConstructor;
55-
if (constructorArgs) {
56-
this.constructorArgs = constructorArgs;
57-
}
58-
if (factoryMethod) {
59-
this.method = factoryMethod;
60-
}
61-
if (objectType) {
62-
this.definitionObjectType = objectType;
63-
}
44+
/**
45+
* @constructor
46+
* @param key
47+
* @param definitionConstructor
48+
* @param constructorArgs
49+
* @param factoryMethod
50+
* @param objectType
51+
*/
52+
public constructor(key: string | Function, definitionConstructor: any, constructorArgs?: Array<any> | undefined, factoryMethod?: FactoryMethod, objectType?: DefinitionObjectType) {
53+
this.key = key;
54+
this.definitionConstructor = definitionConstructor;
55+
if (constructorArgs) {
56+
this.constructorArgs = constructorArgs;
6457
}
58+
if (factoryMethod) {
59+
this.method = factoryMethod;
60+
}
61+
if (objectType) {
62+
this.definitionObjectType = objectType;
63+
}
64+
}
6565

66-
/**
67-
* Changes factory method
68-
* @param method
69-
* @return {Definition}
70-
*/
71-
public as(method: FactoryMethod) {
72-
if (this.method == FactoryMethod.OBJECT) {
73-
throw new Error("You're trying to override factory method for object");
74-
}
75-
this.method = method;
76-
return this;
66+
/**
67+
* Changes factory method
68+
* @param method
69+
* @return {Definition}
70+
*/
71+
public as(method: FactoryMethod) {
72+
if (this.method == FactoryMethod.OBJECT) {
73+
throw new Error("You're trying to override factory method for object");
7774
}
75+
this.method = method;
76+
return this;
77+
}
7878
}

src/resolver.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ export class ContainerResolver {
6060
* @param strict
6161
*/
6262
public resolve(definition: string | Function, method?: FactoryMethod, constructorArgs?: Array<any>, strict = true): any {
63-
let internalDefinition: Definition = null;
63+
let internalDefinition: Definition | null = null;
6464
// bind autofactories to factory auto proxy instance
6565
if (typeof definition === "function" && Reflect.hasOwnMetadata("inject:autofactory", definition)) {
6666
let factoryProxy = new FactoryAutoProxy(this, definition);
67-
internalDefinition = new Definition(definition, factoryProxy, null, FactoryMethod.OBJECT);
67+
internalDefinition = new Definition(definition, factoryProxy, undefined, FactoryMethod.OBJECT);
6868
} else {
6969
internalDefinition = this.definitions.get(definition);
7070
}
@@ -78,7 +78,7 @@ export class ContainerResolver {
7878
}
7979

8080
let constructor = this.resolveDefinition(internalDefinition);
81-
let constructorArguments = [];
81+
let constructorArguments: any[] = [];
8282

8383
if (internalDefinition.definitionObjectType !== DefinitionObjectType.CALLABLE && internalDefinition.method !== FactoryMethod.OBJECT) {
8484
if (typeof constructorArgs !== 'undefined' && constructorArgs.length > 0) {
@@ -182,11 +182,11 @@ export class ContainerResolver {
182182
* @param strict
183183
*/
184184
private resolveConstructorArguments(definition: Definition, constructor: Function, strict = true): Array<any> {
185-
let constructorArguments = [];
185+
let constructorArguments: any[] | undefined = [];
186186
if (Reflect.hasOwnMetadata("inject:constructor", constructor)) {
187187
// Resolve constructor dependencies
188188
let dependencies = Reflect.getOwnMetadata("design:paramtypes", constructor);
189-
let resolvedDeps = [];
189+
let resolvedDeps: any[] = [];
190190
if (dependencies) {
191191
for (let i = 0; i < dependencies.length; i++) {
192192
let dep = dependencies[i];
@@ -227,8 +227,8 @@ export class ContainerResolver {
227227
* @param definition
228228
* @returns {Array<any>}
229229
*/
230-
private resolveConstructorArgumentsFromDefinition(definition: Definition): Array<any> {
231-
let constructorArgs = definition.constructorArgs;
230+
private resolveConstructorArgumentsFromDefinition(definition: Definition): Array<any> | undefined {
231+
let constructorArgs: any[] | undefined = definition.constructorArgs;
232232
if (!constructorArgs && this.definitions.has(definition.definitionConstructor) && (definition.definitionConstructor != definition.key)) {
233233
constructorArgs = this.resolveConstructorArgumentsFromDefinition(this.definitions.get(definition.definitionConstructor));
234234
}

0 commit comments

Comments
 (0)