@spinajs/di 1.0.18 → 1.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,6 @@
1
- export declare const DI_DESCRIPTION_SYMBOL: unique symbol;
1
+ import { Class } from './types';
2
+ import { TypedArray } from './array';
3
+ export declare const DI_DESCRIPTION_SYMBOL = "__DI_INJECTION_DESCRIPTOR__";
2
4
  /**
3
5
  *
4
6
  * Class with this decorator is automatically registered in DI container an can be resolved.
@@ -6,7 +8,7 @@ export declare const DI_DESCRIPTION_SYMBOL: unique symbol;
6
8
  * to register implementation that can be resolved by framework or other parts without knowing about specific implementations eg.
7
9
  * avaible database drivers.
8
10
  *
9
- * @param as register class in DI container as something else.
11
+ * @param as - register class in DI container as something else.
10
12
  *
11
13
  * @example
12
14
  * ```typescript
@@ -25,7 +27,7 @@ export declare const DI_DESCRIPTION_SYMBOL: unique symbol;
25
27
  * ```
26
28
  *
27
29
  */
28
- export declare function Injectable(as?: Class | string): (target: any) => void;
30
+ export declare function Injectable(as?: Class<unknown> | string): (target: Class<unknown>) => void;
29
31
  /**
30
32
  * Sets dependency injection guidelines - what to inject for specified class. If multiple instances are registered at specified type,
31
33
  * only first one is resolved and injected
@@ -52,34 +54,12 @@ export declare function Injectable(as?: Class | string): (target: any) => void;
52
54
  *
53
55
  * ```
54
56
  */
55
- export declare function Inject(...args: Class[]): any;
56
- /**
57
- * Sets dependency injection guidelines - what to inject for specified class. If multiple instances are registered at specified type,
58
- * all of them are resolved and injected
59
- * @param args - what to inject - class definitions
60
- * @example
61
- * ```javascript
62
- *
63
- * @InjectAll(Bar)
64
- * class Foo{
65
- *
66
- * barInstances : Bar[];
67
- *
68
- * constructor(bars : Bar[]){
69
- * // all Bar implementations are injected when Foo is created via DI container
70
- * this.barInstances = bar;
71
- * }
72
- * }
73
- *
74
- * ```
75
- */
76
- export declare function InjectAll(...args: Class[]): any;
57
+ export declare function Inject(...args: (Class<any> | TypedArray<any>)[]): any;
77
58
  /**
78
59
  * Automatically injects dependency based on reflected property type. Uses experimental typescript reflection api
79
60
  * If decorator is applied to array property all registered type instances are injected, otherwise only first / only that exists
80
61
  *
81
- * @param target
82
- * @param key
62
+ * @param injectType - when injecting array of some type, type must be explicitly provided. Typescript reflection cant reflect declared array types
83
63
  * @example
84
64
  * ```javascript
85
65
  * class Foo{
@@ -100,7 +80,7 @@ export declare function InjectAll(...args: Class[]): any;
100
80
  *
101
81
  * ```
102
82
  */
103
- export declare function Autoinject(injectType?: Class): any;
83
+ export declare function Autoinject(injectType?: Class<unknown>): any;
104
84
  /**
105
85
  * Lazy injects service to object. Use only with class properties
106
86
  *
@@ -123,7 +103,7 @@ export declare function Autoinject(injectType?: Class): any;
123
103
  *
124
104
  * ```
125
105
  */
126
- export declare function LazyInject(service: Class | string): (target?: any, key?: string) => void;
106
+ export declare function LazyInject(service: Class<any> | string): (target?: any, key?: string) => void;
127
107
  /**
128
108
  * Per child instance injection decorator - object is resolved once per container - child containers have own instances.
129
109
  */
package/lib/decorators.js CHANGED
@@ -1,18 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Singleton = exports.NewInstance = exports.PerChildInstance = exports.LazyInject = exports.Autoinject = exports.InjectAll = exports.Inject = exports.Injectable = exports.DI_DESCRIPTION_SYMBOL = void 0;
3
+ exports.Singleton = exports.NewInstance = exports.PerChildInstance = exports.LazyInject = exports.Autoinject = exports.Inject = exports.Injectable = exports.DI_DESCRIPTION_SYMBOL = void 0;
4
4
  const enums_1 = require("./enums");
5
- const root_1 = require("./root");
6
- exports.DI_DESCRIPTION_SYMBOL = Symbol.for('DI_INJECTION_DESCRIPTOR');
5
+ const DI = require("./root");
6
+ exports.DI_DESCRIPTION_SYMBOL = '__DI_INJECTION_DESCRIPTOR__';
7
7
  function AddDependency(callback) {
8
8
  return (target, propertyKey, indexOrDescriptor) => {
9
- let descriptor = target[exports.DI_DESCRIPTION_SYMBOL];
9
+ let descriptor = target.__DI_INJECTION_DESCRIPTOR__;
10
10
  if (!descriptor) {
11
11
  descriptor = {
12
12
  inject: [],
13
13
  resolver: enums_1.ResolveType.Singleton,
14
14
  };
15
- target[exports.DI_DESCRIPTION_SYMBOL] = descriptor;
15
+ target[`${exports.DI_DESCRIPTION_SYMBOL}`] = descriptor;
16
16
  }
17
17
  if (callback) {
18
18
  callback(descriptor, target, propertyKey, indexOrDescriptor);
@@ -26,7 +26,7 @@ function AddDependency(callback) {
26
26
  * to register implementation that can be resolved by framework or other parts without knowing about specific implementations eg.
27
27
  * avaible database drivers.
28
28
  *
29
- * @param as register class in DI container as something else.
29
+ * @param as - register class in DI container as something else.
30
30
  *
31
31
  * @example
32
32
  * ```typescript
@@ -48,10 +48,10 @@ function AddDependency(callback) {
48
48
  function Injectable(as) {
49
49
  return (target) => {
50
50
  if (as) {
51
- root_1.DI.register(target).as(as);
51
+ DI.register(target).as(as);
52
52
  }
53
53
  else {
54
- root_1.DI.register(target).asSelf();
54
+ DI.register(target).asSelf();
55
55
  }
56
56
  };
57
57
  }
@@ -86,7 +86,6 @@ function Inject(...args) {
86
86
  return AddDependency((descriptor) => {
87
87
  for (const a of args) {
88
88
  descriptor.inject.push({
89
- all: false,
90
89
  autoinject: false,
91
90
  autoinjectKey: '',
92
91
  inject: a,
@@ -95,45 +94,11 @@ function Inject(...args) {
95
94
  });
96
95
  }
97
96
  exports.Inject = Inject;
98
- /**
99
- * Sets dependency injection guidelines - what to inject for specified class. If multiple instances are registered at specified type,
100
- * all of them are resolved and injected
101
- * @param args - what to inject - class definitions
102
- * @example
103
- * ```javascript
104
- *
105
- * @InjectAll(Bar)
106
- * class Foo{
107
- *
108
- * barInstances : Bar[];
109
- *
110
- * constructor(bars : Bar[]){
111
- * // all Bar implementations are injected when Foo is created via DI container
112
- * this.barInstances = bar;
113
- * }
114
- * }
115
- *
116
- * ```
117
- */
118
- function InjectAll(...args) {
119
- return AddDependency((descriptor) => {
120
- for (const a of args) {
121
- descriptor.inject.push({
122
- all: true,
123
- autoinject: false,
124
- autoinjectKey: '',
125
- inject: a,
126
- });
127
- }
128
- });
129
- }
130
- exports.InjectAll = InjectAll;
131
97
  /**
132
98
  * Automatically injects dependency based on reflected property type. Uses experimental typescript reflection api
133
99
  * If decorator is applied to array property all registered type instances are injected, otherwise only first / only that exists
134
100
  *
135
- * @param target
136
- * @param key
101
+ * @param injectType - when injecting array of some type, type must be explicitly provided. Typescript reflection cant reflect declared array types
137
102
  * @example
138
103
  * ```javascript
139
104
  * class Foo{
@@ -162,10 +127,9 @@ function Autoinject(injectType) {
162
127
  throw new Error('you must provide inject type when injecting array');
163
128
  }
164
129
  descriptor.inject.push({
165
- all: isArray ? true : false,
166
130
  autoinject: true,
167
131
  autoinjectKey: propertyKey,
168
- inject: isArray ? injectType : type,
132
+ inject: isArray ? Array.ofType(injectType) : type,
169
133
  });
170
134
  });
171
135
  }
@@ -197,10 +161,10 @@ function LazyInject(service) {
197
161
  // property getter
198
162
  const getter = () => {
199
163
  if (typeof service === 'string') {
200
- return root_1.DI.get(service);
164
+ return DI.get(service);
201
165
  }
202
166
  else {
203
- return root_1.DI.resolve(service);
167
+ return DI.resolve(service);
204
168
  }
205
169
  };
206
170
  // Create new property with getter and setter
@@ -1 +1 @@
1
- {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":";;;AAAA,mCAAsC;AAEtC,iCAA4B;AAEf,QAAA,qBAAqB,GAAG,MAAM,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;AAE3E,SAAS,aAAa,CACpB,QAKS;IAET,OAAO,CAAC,MAAW,EAAE,WAA4B,EAAE,iBAA8C,EAAE,EAAE;QACnG,IAAI,UAAU,GAA2B,MAAM,CAAC,6BAAqB,CAAC,CAAC;QACvE,IAAI,CAAC,UAAU,EAAE;YACf,UAAU,GAAG;gBACX,MAAM,EAAE,EAAE;gBACV,QAAQ,EAAE,mBAAW,CAAC,SAAS;aAChC,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declare enum ResolveType {
6
5
  /**
package/lib/enums.js CHANGED
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ResolveType = void 0;
4
4
  /**
5
5
  * How to resolve class
6
- * @enum
7
6
  */
8
7
  var ResolveType;
9
8
  (function (ResolveType) {
package/lib/enums.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"enums.js","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,IAAY,WAeX;AAfD,WAAY,WAAW;IACrB;;OAEG;IACH,uDAAS,CAAA;IAET;;OAEG;IACH,2DAAW,CAAA;IAEX;;OAEG;IACH,uEAAiB,CAAA;AACnB,CAAC,EAfW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAetB"}
1
+ {"version":3,"file":"enums.js","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,IAAY,WAeX;AAfD,WAAY,WAAW;IACrB;;OAEG;IACH,uDAAS,CAAA;IAET;;OAEG;IACH,2DAAW,CAAA;IAEX;;OAEG;IACH,uEAAiB,CAAA;AACnB,CAAC,EAfW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAetB"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Exception thrown when cannot resolve type
3
+ */
4
+ export declare class ResolveException extends Error {
5
+ /**
6
+ * Constructs new exception with message
7
+ * @param message - error message
8
+ */
9
+ constructor(message?: string);
10
+ }
11
+ export declare class BindException extends Error {
12
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BindException = exports.ResolveException = void 0;
4
+ /**
5
+ * Exception thrown when cannot resolve type
6
+ */
7
+ class ResolveException extends Error {
8
+ /**
9
+ * Constructs new exception with message
10
+ * @param message - error message
11
+ */
12
+ constructor(message) {
13
+ super(message);
14
+ }
15
+ }
16
+ exports.ResolveException = ResolveException;
17
+ class BindException extends Error {
18
+ }
19
+ exports.BindException = BindException;
20
+ //# sourceMappingURL=exceptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exceptions.js","sourceRoot":"","sources":["../src/exceptions.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,gBAAiB,SAAQ,KAAK;IACzC;;;OAGG;IACH,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF;AARD,4CAQC;AAED,MAAa,aAAc,SAAQ,KAAK;CAAG;AAA3C,sCAA2C"}
package/lib/helpers.d.ts CHANGED
@@ -1,7 +1,18 @@
1
+ import { TypedArray } from './array';
2
+ import { AsyncModule, SyncModule } from './interfaces';
3
+ import { Factory, Class } from './types';
1
4
  /**
2
5
  * Checks if value is constructable type.
3
6
  * Checks for [[Construct]] internal function in object.
4
7
  *
5
8
  * @param value - value to test
6
9
  */
7
- export declare function isConstructor(value: any): boolean;
10
+ export declare function isConstructor(value: any): value is Class<unknown>;
11
+ export declare function isFactory(value: any): value is Factory<any>;
12
+ export declare function isObject(value: any): value is object;
13
+ export declare function isAsyncModule(value: any): value is AsyncModule;
14
+ export declare function isPromise(value: any): value is Promise<any>;
15
+ export declare function isSyncModule(value: any): value is SyncModule;
16
+ export declare function isTypedArray(value: any): value is TypedArray<any>;
17
+ export declare function uniqBy<T>(arr: T[], comparator: (a: T, b: T) => boolean): T[];
18
+ export declare function getTypeName(type: TypedArray<any> | Class<any> | string | object): string;
package/lib/helpers.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isConstructor = void 0;
3
+ exports.getTypeName = exports.uniqBy = exports.isTypedArray = exports.isSyncModule = exports.isPromise = exports.isAsyncModule = exports.isObject = exports.isFactory = exports.isConstructor = void 0;
4
+ const array_1 = require("./array");
5
+ const interfaces_1 = require("./interfaces");
4
6
  /**
5
7
  * Checks if value is constructable type.
6
8
  * Checks for [[Construct]] internal function in object.
@@ -17,4 +19,42 @@ function isConstructor(value) {
17
19
  return true;
18
20
  }
19
21
  exports.isConstructor = isConstructor;
22
+ function isFactory(value) {
23
+ return !isConstructor(value) && typeof value === 'function';
24
+ }
25
+ exports.isFactory = isFactory;
26
+ function isObject(value) {
27
+ return typeof value === 'object';
28
+ }
29
+ exports.isObject = isObject;
30
+ function isAsyncModule(value) {
31
+ return value instanceof interfaces_1.AsyncModule;
32
+ }
33
+ exports.isAsyncModule = isAsyncModule;
34
+ function isPromise(value) {
35
+ return Boolean(value && typeof value.then === 'function');
36
+ }
37
+ exports.isPromise = isPromise;
38
+ function isSyncModule(value) {
39
+ return value instanceof interfaces_1.SyncModule;
40
+ }
41
+ exports.isSyncModule = isSyncModule;
42
+ function isTypedArray(value) {
43
+ return value instanceof array_1.TypedArray;
44
+ }
45
+ exports.isTypedArray = isTypedArray;
46
+ function uniqBy(arr, comparator) {
47
+ const uniques = [];
48
+ for (const a of arr) {
49
+ if (uniques.findIndex((u) => comparator(a, u)) === -1) {
50
+ uniques.push(a);
51
+ }
52
+ }
53
+ return uniques;
54
+ }
55
+ exports.uniqBy = uniqBy;
56
+ function getTypeName(type) {
57
+ return typeof type === 'string' ? type : type instanceof array_1.TypedArray ? type.Type.name : isConstructor(type) ? type.name : type.constructor.name;
58
+ }
59
+ exports.getTypeName = getTypeName;
20
60
  //# sourceMappingURL=helpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,SAAgB,aAAa,CAAC,KAAU;IACtC,IAAI;QACF,OAAO,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;KACtC;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,KAAK,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AARD,sCAQC"}
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":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F;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AARD,wBAQC;AAED,SAAgB,WAAW,CAAC,IAAoD;IAC9E,OAAO,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,YAAY,kBAAU,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;AACjJ,CAAC;AAFD,kCAEC"}
package/lib/index.d.ts CHANGED
@@ -4,5 +4,9 @@ export * from './decorators';
4
4
  export * from './enums';
5
5
  export * from './types';
6
6
  export * from './helpers';
7
+ export * from './container-cache';
8
+ export * from './binder';
9
+ export * from './registry';
7
10
  export * from './container';
8
- export * from './root';
11
+ export * as DI from './root';
12
+ export * from './exceptions';
package/lib/index.js CHANGED
@@ -7,15 +7,20 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
7
7
  o[k2] = m[k];
8
8
  }));
9
9
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.DI = void 0;
13
14
  __exportStar(require("./interfaces"), exports);
14
15
  __exportStar(require("./array"), exports);
15
16
  __exportStar(require("./decorators"), exports);
16
17
  __exportStar(require("./enums"), exports);
17
18
  __exportStar(require("./types"), exports);
18
19
  __exportStar(require("./helpers"), exports);
20
+ __exportStar(require("./container-cache"), exports);
21
+ __exportStar(require("./binder"), exports);
22
+ __exportStar(require("./registry"), exports);
19
23
  __exportStar(require("./container"), exports);
20
- __exportStar(require("./root"), exports);
24
+ exports.DI = require("./root");
25
+ __exportStar(require("./exceptions"), exports);
21
26
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAA6B;AAC7B,0CAAwB;AACxB,+CAA6B;AAC7B,0CAAwB;AACxB,0CAAwB;AACxB,4CAA0B;AAC1B,8CAA4B;AAC5B,yCAAuB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,0CAAwB;AACxB,+CAA6B;AAC7B,0CAAwB;AACxB,0CAAwB;AACxB,4CAA0B;AAC1B,oDAAkC;AAClC,2CAAyB;AACzB,6CAA2B;AAC3B,8CAA4B;AAC5B,+BAA6B;AAC7B,+CAA6B"}
@@ -1,62 +1,83 @@
1
+ /// <reference types="node" />
1
2
  import { ResolveType } from './enums';
2
3
  import { Class, Factory } from './types';
4
+ import { EventEmitter } from 'events';
5
+ import { TypedArray } from './array';
6
+ import { Registry } from './registry';
7
+ import { ContainerCache } from './container-cache';
3
8
  /**
4
9
  * Interface to describe DI binding behaviour
5
10
  */
6
11
  export interface IBind {
7
- /**
8
- * Private var for holding implementation type
9
- */
10
- _impl: any;
11
12
  /**
12
13
  * `as` binding (alias)
13
14
  *
14
15
  * @param type - base class that is being registered
15
16
  */
16
- as<T>(type: Class<T> | string): this;
17
+ as(type: Class<any> | string): this;
17
18
  /**
18
19
  * self bind, class should be resolved by its name. Its default behaviour.
19
20
  */
20
21
  asSelf(): this;
22
+ /**
23
+ * Registers as value, it wont be resolved or called, just stored as is in container
24
+ *
25
+ * @param type - name of type / key after whitch implementation will be resolved
26
+ */
27
+ asValue(type: string): this;
28
+ /**
29
+ * Add value as array entry, and push to array if already value exists.
30
+ * Usefull if for eg. user wants to add multiple values and not override its contents.
31
+ *
32
+ * @param type - name / or type to add
33
+ */
34
+ asArrayValue(type: string): this;
21
35
  /**
22
36
  * Registers object as single instance ( singleton )
23
37
  */
24
38
  singleInstance(): this;
25
39
  }
26
- export interface IContainer {
27
- Cache: Map<string, any[] | any>;
28
- Registry: Map<string, any[] | any>;
40
+ export interface ResolvableObject {
41
+ [key: string]: any;
42
+ }
43
+ export interface IContainer extends EventEmitter {
44
+ Cache: ContainerCache;
45
+ Registry: Registry;
46
+ Parent: IContainer;
29
47
  clear(): void;
30
- register<T>(implementation: Class<T> | Factory<T>): IBind;
48
+ clearRegistry(): void;
49
+ clearCache(): void;
50
+ register<T>(implementation: Class<T> | Factory<T> | ResolvableObject): IBind;
31
51
  child(): IContainer;
32
- get<T>(service: TypedArray<T>, parent?: boolean): T[];
33
- get<T>(service: string | Class<T>, parent?: boolean): T;
34
- get<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): T | T[];
35
- getRegistered<T>(service: string | Class<T>, parent: boolean): Array<Class<any>>;
36
- has<T>(service: string | Class<T>, parent?: boolean): boolean;
37
- hasRegistered<T>(service: Class<T> | string, parent?: boolean): boolean;
38
- resolve<T>(type: string, options?: any[], check?: boolean): T;
52
+ get<T>(service: TypedArray<T>, parent?: boolean): T[] | null;
53
+ get<T>(service: string | Class<T>, parent?: boolean): T | null;
54
+ get<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): T | T[] | null;
55
+ getRegisteredTypes<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): Array<Class<unknown> | Factory<unknown>>;
56
+ isResolved<T>(service: string | Class<T>, parent?: boolean): boolean;
57
+ hasRegistered<T>(service: Class<T> | string | TypedArray<T>, parent?: boolean): boolean;
58
+ hasRegisteredType<T>(source: Class<any> | string | TypedArray<any>, type: Class<T> | string | TypedArray<T> | object, parent?: boolean): boolean;
59
+ resolve<T>(type: string, options?: unknown[], check?: boolean): T;
39
60
  resolve<T>(type: string, check?: boolean): T;
40
- resolve<T>(type: Class<T> | Factory<T>, options?: any[] | boolean, check?: boolean): T extends AsyncModule ? Promise<T> : T;
41
- resolve<T>(type: TypedArray<T>, options?: any[] | boolean, check?: boolean): T extends AsyncModule ? Promise<T[]> : T[];
61
+ resolve<T>(type: Class<T> | Factory<T>, options?: unknown[] | boolean, check?: boolean): T extends AsyncModule ? Promise<T> : T;
62
+ resolve<T>(type: TypedArray<T>, options?: unknown[] | boolean, check?: boolean): T extends AsyncModule ? Promise<T[]> : T[];
42
63
  resolve<T>(type: Class<T> | Factory<T>, check?: boolean): T extends AsyncModule ? Promise<T> : T;
43
64
  resolve<T>(type: TypedArray<T>, check?: boolean): T extends AsyncModule ? Promise<T[]> : T[];
65
+ resolve<T>(type: Class<T> | TypedArray<T> | string, options?: unknown[] | boolean, check?: boolean): Promise<T | T[]> | T | T[];
44
66
  }
45
67
  /**
46
68
  * Injection description definition structure
47
69
  */
48
- export interface IInjectDescriptor<T = any> {
49
- inject: Array<IToInject<T>>;
70
+ export interface IInjectDescriptor<T> {
71
+ inject: IToInject<T>[];
50
72
  resolver: ResolveType;
51
73
  }
52
- export interface IToInject<T = any> {
53
- inject: Class<T>;
74
+ export interface IToInject<T> {
75
+ inject: Class<T> | TypedArray<T>;
54
76
  autoinject: boolean;
55
- all: boolean;
56
77
  autoinjectKey: string;
57
78
  }
58
79
  export interface IResolvedInjection {
59
- instance: any;
80
+ instance: unknown;
60
81
  autoinject: boolean;
61
82
  autoinjectKey: string;
62
83
  }
@@ -68,9 +89,16 @@ export interface IResolvedInjection {
68
89
  *
69
90
  * @see FrameworkModuleSyncModule implementation
70
91
  */
71
- export declare abstract class SyncModule {
72
- abstract resolve(container: IContainer): void;
92
+ export declare class Module {
93
+ protected resolved: boolean;
94
+ get Resolved(): boolean;
95
+ }
96
+ export declare abstract class SyncModule extends Module {
97
+ resolve(): void;
98
+ }
99
+ export declare class AsyncModule extends Module {
100
+ resolveAsync(): Promise<void>;
73
101
  }
74
- export declare abstract class AsyncModule {
75
- abstract resolveAsync(container: IContainer): Promise<void>;
102
+ export declare abstract class Bootstrapper {
103
+ abstract bootstrap(): Promise<void> | void;
76
104
  }
package/lib/interfaces.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AsyncModule = exports.SyncModule = void 0;
3
+ exports.Bootstrapper = exports.AsyncModule = exports.SyncModule = exports.Module = void 0;
4
4
  /**
5
5
  * Interface to describe DI resolve strategies. Strategies are used do
6
6
  * do some work at object creation eg. initialize objects that inherits from same class
@@ -15,10 +15,29 @@ exports.AsyncModule = exports.SyncModule = void 0;
15
15
  // export interface IAsyncStrategy {
16
16
  // resolveA: (target: any, container: IContainer) => Promise<void>;
17
17
  // }
18
- class SyncModule {
18
+ class Module {
19
+ constructor() {
20
+ this.resolved = false;
21
+ }
22
+ get Resolved() {
23
+ return this.resolved;
24
+ }
25
+ }
26
+ exports.Module = Module;
27
+ class SyncModule extends Module {
28
+ resolve() {
29
+ this.resolved = true;
30
+ }
19
31
  }
20
32
  exports.SyncModule = SyncModule;
21
- class AsyncModule {
33
+ class AsyncModule extends Module {
34
+ /* eslint-disable */
35
+ async resolveAsync() {
36
+ this.resolved = true;
37
+ }
22
38
  }
23
39
  exports.AsyncModule = AsyncModule;
40
+ class Bootstrapper {
41
+ }
42
+ exports.Bootstrapper = Bootstrapper;
24
43
  //# sourceMappingURL=interfaces.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../src/interfaces.ts"],"names":[],"mappings":";;;AAkFA;;;;;;;GAOG;AACH,+BAA+B;AAC/B,6DAA6D;AAC7D,IAAI;AAEJ,oCAAoC;AACpC,uEAAuE;AACvE,IAAI;AAEJ,MAAsB,UAAU;CAE/B;AAFD,gCAEC;AAED,MAAsB,WAAW;CAEhC;AAFD,kCAEC"}
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../src/interfaces.ts"],"names":[],"mappings":";;;AAkGA;;;;;;;GAOG;AACH,+BAA+B;AAC/B,6DAA6D;AAC7D,IAAI;AAEJ,oCAAoC;AACpC,uEAAuE;AACvE,IAAI;AAEJ,MAAa,MAAM;IAAnB;QACY,aAAQ,GAAG,KAAK,CAAC;IAI7B,CAAC;IAHC,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF;AALD,wBAKC;AAED,MAAsB,UAAW,SAAQ,MAAM;IACtC,OAAO;QACZ,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACvB,CAAC;CACF;AAJD,gCAIC;AAED,MAAa,WAAY,SAAQ,MAAM;IACrC,oBAAoB;IACb,KAAK,CAAC,YAAY;QACvB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACvB,CAAC;CACF;AALD,kCAKC;AAED,MAAsB,YAAY;CAEjC;AAFD,oCAEC"}
@@ -0,0 +1,13 @@
1
+ import { TypedArray } from './array';
2
+ import { IContainer } from './interfaces';
3
+ import { Class, Factory } from './types';
4
+ export declare class Registry {
5
+ protected container: IContainer;
6
+ protected registry: Map<string, any[]>;
7
+ constructor(container: IContainer);
8
+ clear(): void;
9
+ register(name: string | Class<any> | TypedArray<any>, type: any): void;
10
+ hasRegisteredType(source: Class<any> | string | TypedArray<any>, type: Class<any> | string | TypedArray<any> | object, parent?: boolean): boolean;
11
+ getTypes<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): Array<Class<unknown> | Factory<unknown>>;
12
+ hasRegistered<T>(service: TypedArray<T> | Class<T> | string, parent?: boolean): boolean;
13
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Registry = void 0;
4
+ const exceptions_1 = require("@spinajs/exceptions");
5
+ const helpers_1 = require("./helpers");
6
+ class Registry {
7
+ constructor(container) {
8
+ this.container = container;
9
+ this.registry = new Map();
10
+ }
11
+ clear() {
12
+ this.registry.clear();
13
+ }
14
+ register(name, type) {
15
+ if (!(0, helpers_1.isConstructor)(type) && !(0, helpers_1.isFactory)(type) && !(0, helpers_1.isObject)(type)) {
16
+ throw new exceptions_1.InvalidOperation('cannot register type if its not an class or factory function');
17
+ }
18
+ else {
19
+ const tname = (0, helpers_1.getTypeName)(name);
20
+ if (!this.hasRegisteredType(name, type)) {
21
+ const value = this.registry.get(tname);
22
+ if (value) {
23
+ value.push(type);
24
+ }
25
+ else {
26
+ this.registry.set(tname, [type]);
27
+ }
28
+ }
29
+ }
30
+ }
31
+ hasRegisteredType(source, type, parent) {
32
+ const sourceName = (0, helpers_1.getTypeName)(source);
33
+ const targetName = (0, helpers_1.getTypeName)(type);
34
+ if (this.registry.has(sourceName)) {
35
+ return this.registry.get(sourceName).find((s) => s.name === targetName) !== undefined;
36
+ }
37
+ else if (parent && this.container.Parent) {
38
+ return this.container.Parent.hasRegisteredType(source, type, parent);
39
+ }
40
+ return false;
41
+ }
42
+ getTypes(service, parent = true) {
43
+ if (!service) {
44
+ throw new exceptions_1.InvalidArgument('argument "service" cannot be null or empty');
45
+ }
46
+ const name = (0, helpers_1.getTypeName)(service);
47
+ if (this.registry.has(name)) {
48
+ return this.registry.get(name);
49
+ }
50
+ if (this.container.Parent && parent) {
51
+ return this.container.Parent.getRegisteredTypes(service, parent);
52
+ }
53
+ return null;
54
+ }
55
+ hasRegistered(service, parent = true) {
56
+ if (this.registry.has((0, helpers_1.getTypeName)(service))) {
57
+ return true;
58
+ }
59
+ else if (parent && this.container.Parent) {
60
+ return this.container.Parent.hasRegistered(service, parent);
61
+ }
62
+ return false;
63
+ }
64
+ }
65
+ exports.Registry = Registry;
66
+ //# sourceMappingURL=registry.js.map