first-di 0.1.35 → 0.1.39
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.
- package/dist/classes/di.js +16 -12
- package/dist/classes/di.mjs +91 -0
- package/dist/decorators/reflection.js +5 -1
- package/dist/decorators/reflection.mjs +4 -0
- package/dist/index.js +19 -5
- package/dist/index.mjs +5 -0
- package/dist/models/autowired-lifetimes.js +5 -2
- package/dist/models/autowired-lifetimes.mjs +19 -0
- package/dist/models/autowired-options.js +2 -1
- package/dist/models/autowired-options.mjs +1 -0
- package/dist/models/override-options.js +2 -1
- package/dist/models/override-options.mjs +1 -0
- package/dist/typings/class-constructor.js +2 -1
- package/dist/typings/class-constructor.mjs +4 -0
- package/npm-shrinkwrap.json +613 -1475
- package/package.json +13 -10
package/dist/classes/di.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
2
3
|
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
|
|
3
4
|
/* eslint-disable max-statements */
|
|
@@ -5,8 +6,10 @@
|
|
|
5
6
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
7
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
7
8
|
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.DI = void 0;
|
|
11
|
+
const autowired_lifetimes_1 = require("../models/autowired-lifetimes");
|
|
12
|
+
class DI {
|
|
10
13
|
constructor() {
|
|
11
14
|
this.singletonsList = new Map();
|
|
12
15
|
this.overrideList = new Map();
|
|
@@ -15,8 +18,8 @@ export class DI {
|
|
|
15
18
|
this.makeReset();
|
|
16
19
|
};
|
|
17
20
|
this.resolve = (constructor, options, caller, propertyKey) => this.makeResolve(constructor, options, caller, propertyKey);
|
|
18
|
-
this.singleton = (constructor, options) => this.makeResolve(constructor, Object.assign(Object.assign({}, options), { lifeTime: AutowiredLifetimes.Singleton }));
|
|
19
|
-
this.instance = (constructor, options) => this.makeResolve(constructor, Object.assign(Object.assign({}, options), { lifeTime: AutowiredLifetimes.PerInstance }));
|
|
21
|
+
this.singleton = (constructor, options) => this.makeResolve(constructor, Object.assign(Object.assign({}, options), { lifeTime: autowired_lifetimes_1.AutowiredLifetimes.Singleton }));
|
|
22
|
+
this.instance = (constructor, options) => this.makeResolve(constructor, Object.assign(Object.assign({}, options), { lifeTime: autowired_lifetimes_1.AutowiredLifetimes.PerInstance }));
|
|
20
23
|
this.override = (from, to, options) => {
|
|
21
24
|
this.makeOverride(from, to, options);
|
|
22
25
|
};
|
|
@@ -44,18 +47,18 @@ export class DI {
|
|
|
44
47
|
constructor = overridOptions.to;
|
|
45
48
|
options = (_a = overridOptions.options) !== null && _a !== void 0 ? _a : options;
|
|
46
49
|
}
|
|
47
|
-
const lifeTime = (_b = options === null || options === void 0 ? void 0 : options.lifeTime) !== null && _b !== void 0 ? _b : AutowiredLifetimes.Singleton;
|
|
48
|
-
if (lifeTime === AutowiredLifetimes.Singleton) {
|
|
50
|
+
const lifeTime = (_b = options === null || options === void 0 ? void 0 : options.lifeTime) !== null && _b !== void 0 ? _b : autowired_lifetimes_1.AutowiredLifetimes.Singleton;
|
|
51
|
+
if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.Singleton) {
|
|
49
52
|
if (this.singletonsList.has(constructor)) {
|
|
50
53
|
return this.singletonsList.get(constructor);
|
|
51
54
|
}
|
|
52
55
|
}
|
|
53
|
-
else if (lifeTime === AutowiredLifetimes.PerOwned && propertyKey) {
|
|
56
|
+
else if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.PerOwned && propertyKey) {
|
|
54
57
|
if (Reflect.has(constructor, this.getDiKey(propertyKey))) {
|
|
55
58
|
return Reflect.get(constructor, this.getDiKey(propertyKey));
|
|
56
59
|
}
|
|
57
60
|
}
|
|
58
|
-
else if (lifeTime === AutowiredLifetimes.PerInstance && caller && propertyKey) {
|
|
61
|
+
else if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.PerInstance && caller && propertyKey) {
|
|
59
62
|
if (Reflect.has(caller, this.getDiKey(propertyKey))) {
|
|
60
63
|
return Reflect.get(caller, this.getDiKey(propertyKey));
|
|
61
64
|
}
|
|
@@ -63,13 +66,13 @@ export class DI {
|
|
|
63
66
|
const params = Reflect.getMetadata("design:paramtypes", constructor) || [];
|
|
64
67
|
const object = new constructor(...params
|
|
65
68
|
.map((paramConstructor) => this.makeResolve(paramConstructor, options)));
|
|
66
|
-
if (lifeTime === AutowiredLifetimes.Singleton) {
|
|
69
|
+
if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.Singleton) {
|
|
67
70
|
this.singletonsList.set(constructor, object);
|
|
68
71
|
}
|
|
69
|
-
else if (lifeTime === AutowiredLifetimes.PerOwned) {
|
|
72
|
+
else if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.PerOwned) {
|
|
70
73
|
Reflect.set(constructor, this.getDiKey(propertyKey), object);
|
|
71
74
|
}
|
|
72
|
-
else if (lifeTime === AutowiredLifetimes.PerInstance && caller) {
|
|
75
|
+
else if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.PerInstance && caller) {
|
|
73
76
|
Reflect.set(caller, this.getDiKey(propertyKey), object);
|
|
74
77
|
}
|
|
75
78
|
return object;
|
|
@@ -86,6 +89,7 @@ export class DI {
|
|
|
86
89
|
return `$_di_${String(propertyKey)}`; // Think about symbol
|
|
87
90
|
}
|
|
88
91
|
}
|
|
92
|
+
exports.DI = DI;
|
|
89
93
|
DI.defaultOptions = {
|
|
90
|
-
lifeTime: AutowiredLifetimes.Singleton
|
|
94
|
+
lifeTime: autowired_lifetimes_1.AutowiredLifetimes.Singleton
|
|
91
95
|
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
2
|
+
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
|
|
3
|
+
/* eslint-disable max-statements */
|
|
4
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
8
|
+
import { AutowiredLifetimes } from "../models/autowired-lifetimes";
|
|
9
|
+
export class DI {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.singletonsList = new Map();
|
|
12
|
+
this.overrideList = new Map();
|
|
13
|
+
this.autowired = (options) => this.makeAutowired(options);
|
|
14
|
+
this.reset = () => {
|
|
15
|
+
this.makeReset();
|
|
16
|
+
};
|
|
17
|
+
this.resolve = (constructor, options, caller, propertyKey) => this.makeResolve(constructor, options, caller, propertyKey);
|
|
18
|
+
this.singleton = (constructor, options) => this.makeResolve(constructor, Object.assign(Object.assign({}, options), { lifeTime: AutowiredLifetimes.Singleton }));
|
|
19
|
+
this.instance = (constructor, options) => this.makeResolve(constructor, Object.assign(Object.assign({}, options), { lifeTime: AutowiredLifetimes.PerInstance }));
|
|
20
|
+
this.override = (from, to, options) => {
|
|
21
|
+
this.makeOverride(from, to, options);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
makeAutowired(options) {
|
|
25
|
+
return (target, propertyKey) => {
|
|
26
|
+
const type = Reflect.getMetadata("design:type", target, propertyKey);
|
|
27
|
+
const { resolve } = this;
|
|
28
|
+
Reflect.defineProperty(target, propertyKey, {
|
|
29
|
+
configurable: false,
|
|
30
|
+
enumerable: false,
|
|
31
|
+
get() {
|
|
32
|
+
return resolve(type, options, this, propertyKey);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
makeResolve(inConstructor, inOptions, caller, propertyKey) {
|
|
38
|
+
var _a, _b;
|
|
39
|
+
let constructor = inConstructor;
|
|
40
|
+
let options = inOptions;
|
|
41
|
+
if (this.overrideList.has(constructor)) {
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
43
|
+
const overridOptions = this.overrideList.get(constructor);
|
|
44
|
+
constructor = overridOptions.to;
|
|
45
|
+
options = (_a = overridOptions.options) !== null && _a !== void 0 ? _a : options;
|
|
46
|
+
}
|
|
47
|
+
const lifeTime = (_b = options === null || options === void 0 ? void 0 : options.lifeTime) !== null && _b !== void 0 ? _b : AutowiredLifetimes.Singleton;
|
|
48
|
+
if (lifeTime === AutowiredLifetimes.Singleton) {
|
|
49
|
+
if (this.singletonsList.has(constructor)) {
|
|
50
|
+
return this.singletonsList.get(constructor);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (lifeTime === AutowiredLifetimes.PerOwned && propertyKey) {
|
|
54
|
+
if (Reflect.has(constructor, this.getDiKey(propertyKey))) {
|
|
55
|
+
return Reflect.get(constructor, this.getDiKey(propertyKey));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else if (lifeTime === AutowiredLifetimes.PerInstance && caller && propertyKey) {
|
|
59
|
+
if (Reflect.has(caller, this.getDiKey(propertyKey))) {
|
|
60
|
+
return Reflect.get(caller, this.getDiKey(propertyKey));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const params = Reflect.getMetadata("design:paramtypes", constructor) || [];
|
|
64
|
+
const object = new constructor(...params
|
|
65
|
+
.map((paramConstructor) => this.makeResolve(paramConstructor, options)));
|
|
66
|
+
if (lifeTime === AutowiredLifetimes.Singleton) {
|
|
67
|
+
this.singletonsList.set(constructor, object);
|
|
68
|
+
}
|
|
69
|
+
else if (lifeTime === AutowiredLifetimes.PerOwned) {
|
|
70
|
+
Reflect.set(constructor, this.getDiKey(propertyKey), object);
|
|
71
|
+
}
|
|
72
|
+
else if (lifeTime === AutowiredLifetimes.PerInstance && caller) {
|
|
73
|
+
Reflect.set(caller, this.getDiKey(propertyKey), object);
|
|
74
|
+
}
|
|
75
|
+
return object;
|
|
76
|
+
}
|
|
77
|
+
makeReset() {
|
|
78
|
+
this.singletonsList = new Map();
|
|
79
|
+
this.overrideList = new Map();
|
|
80
|
+
}
|
|
81
|
+
makeOverride(from, to, options) {
|
|
82
|
+
this.overrideList.set(from, { to,
|
|
83
|
+
options });
|
|
84
|
+
}
|
|
85
|
+
getDiKey(propertyKey) {
|
|
86
|
+
return `$_di_${String(propertyKey)}`; // Think about symbol
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
DI.defaultOptions = {
|
|
90
|
+
lifeTime: AutowiredLifetimes.Singleton
|
|
91
|
+
};
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.reflection = void 0;
|
|
5
|
+
const reflection = (..._params) => {
|
|
3
6
|
// For generation reflection by typescript
|
|
4
7
|
};
|
|
8
|
+
exports.reflection = reflection;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
var _a;
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.reset = exports.instance = exports.singleton = exports.resolve = exports.override = exports.autowired = void 0;
|
|
15
|
+
__exportStar(require("./classes/di"), exports);
|
|
16
|
+
__exportStar(require("./decorators/reflection"), exports);
|
|
17
|
+
__exportStar(require("./models/autowired-lifetimes"), exports);
|
|
18
|
+
const di_1 = require("./classes/di");
|
|
19
|
+
_a = new di_1.DI(), exports.autowired = _a.autowired, exports.override = _a.override, exports.resolve = _a.resolve, exports.singleton = _a.singleton, exports.instance = _a.instance, exports.reset = _a.reset; // Export as singleton
|
package/dist/index.mjs
ADDED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AutowiredLifetimes = void 0;
|
|
4
|
+
var AutowiredLifetimes;
|
|
2
5
|
(function (AutowiredLifetimes) {
|
|
3
6
|
/**
|
|
4
7
|
* Create one instance for all resolvers
|
|
@@ -16,4 +19,4 @@ export var AutowiredLifetimes;
|
|
|
16
19
|
* Recreate each dependency on each access to dependency
|
|
17
20
|
*/
|
|
18
21
|
AutowiredLifetimes[AutowiredLifetimes["PerAccess"] = 3] = "PerAccess";
|
|
19
|
-
})(AutowiredLifetimes || (AutowiredLifetimes = {}));
|
|
22
|
+
})(AutowiredLifetimes = exports.AutowiredLifetimes || (exports.AutowiredLifetimes = {}));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export var AutowiredLifetimes;
|
|
2
|
+
(function (AutowiredLifetimes) {
|
|
3
|
+
/**
|
|
4
|
+
* Create one instance for all resolvers
|
|
5
|
+
*/
|
|
6
|
+
AutowiredLifetimes[AutowiredLifetimes["Singleton"] = 0] = "Singleton";
|
|
7
|
+
/**
|
|
8
|
+
* Create one instance for each resolver
|
|
9
|
+
*/
|
|
10
|
+
AutowiredLifetimes[AutowiredLifetimes["PerInstance"] = 1] = "PerInstance";
|
|
11
|
+
/**
|
|
12
|
+
* Create one instance for each type of resolver
|
|
13
|
+
*/
|
|
14
|
+
AutowiredLifetimes[AutowiredLifetimes["PerOwned"] = 2] = "PerOwned";
|
|
15
|
+
/**
|
|
16
|
+
* Recreate each dependency on each access to dependency
|
|
17
|
+
*/
|
|
18
|
+
AutowiredLifetimes[AutowiredLifetimes["PerAccess"] = 3] = "PerAccess";
|
|
19
|
+
})(AutowiredLifetimes || (AutowiredLifetimes = {}));
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|