@ooneex/container 0.0.13 → 0.0.15
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/index.js +118 -2
- package/dist/index.js.map +2 -2
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,3 +1,119 @@
|
|
|
1
|
-
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { inject, injectable } from "inversify";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
// src/Container.ts
|
|
5
|
+
import { Container as InversifyContainer } from "inversify";
|
|
6
|
+
|
|
7
|
+
// src/ContainerException.ts
|
|
8
|
+
import { Exception } from "@ooneex/exception";
|
|
9
|
+
import { HttpStatus } from "@ooneex/http-status";
|
|
10
|
+
|
|
11
|
+
class ContainerException extends Exception {
|
|
12
|
+
constructor(message, data = {}) {
|
|
13
|
+
super(message, {
|
|
14
|
+
status: HttpStatus.Code.InternalServerError,
|
|
15
|
+
data
|
|
16
|
+
});
|
|
17
|
+
this.name = "ContainerException";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/types.ts
|
|
22
|
+
var EContainerScope;
|
|
23
|
+
((EContainerScope2) => {
|
|
24
|
+
EContainerScope2["Singleton"] = "singleton";
|
|
25
|
+
EContainerScope2["Transient"] = "transient";
|
|
26
|
+
EContainerScope2["Request"] = "request";
|
|
27
|
+
})(EContainerScope ||= {});
|
|
28
|
+
|
|
29
|
+
// src/Container.ts
|
|
30
|
+
var sharedDI = new InversifyContainer;
|
|
31
|
+
|
|
32
|
+
class Container {
|
|
33
|
+
alias = {};
|
|
34
|
+
add(target, scope = "singleton" /* Singleton */) {
|
|
35
|
+
try {
|
|
36
|
+
sharedDI.unbind(target);
|
|
37
|
+
} catch {}
|
|
38
|
+
const binding = sharedDI.bind(target).toSelf();
|
|
39
|
+
switch (scope) {
|
|
40
|
+
case "request" /* Request */:
|
|
41
|
+
binding.inRequestScope();
|
|
42
|
+
break;
|
|
43
|
+
case "transient" /* Transient */:
|
|
44
|
+
binding.inTransientScope();
|
|
45
|
+
break;
|
|
46
|
+
default:
|
|
47
|
+
binding.inSingletonScope();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
resolveAlias(target, throwOnMissing = true) {
|
|
51
|
+
if (typeof target === "string") {
|
|
52
|
+
const aliasedTarget = this.alias[target];
|
|
53
|
+
if (!aliasedTarget) {
|
|
54
|
+
if (throwOnMissing) {
|
|
55
|
+
throw new ContainerException(`Failed to resolve alias: ${target}`);
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return aliasedTarget;
|
|
60
|
+
}
|
|
61
|
+
return target;
|
|
62
|
+
}
|
|
63
|
+
get(target) {
|
|
64
|
+
const resolvedTarget = this.resolveAlias(target, true);
|
|
65
|
+
try {
|
|
66
|
+
return sharedDI.get(resolvedTarget);
|
|
67
|
+
} catch (_e) {
|
|
68
|
+
throw new ContainerException(`Failed to resolve dependency: ${typeof target === "string" ? target : target.name}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
has(target) {
|
|
72
|
+
const resolvedTarget = this.resolveAlias(target, false);
|
|
73
|
+
if (resolvedTarget === null) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return sharedDI.isBound(resolvedTarget);
|
|
77
|
+
}
|
|
78
|
+
remove(target) {
|
|
79
|
+
const resolvedTarget = this.resolveAlias(target, true);
|
|
80
|
+
if (resolvedTarget && sharedDI.isBound(resolvedTarget)) {
|
|
81
|
+
sharedDI.unbind(resolvedTarget);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
addConstant(identifier, value) {
|
|
85
|
+
try {
|
|
86
|
+
sharedDI.unbind(identifier);
|
|
87
|
+
} catch {}
|
|
88
|
+
sharedDI.bind(identifier).toConstantValue(value);
|
|
89
|
+
}
|
|
90
|
+
getConstant(identifier) {
|
|
91
|
+
try {
|
|
92
|
+
return sharedDI.get(identifier);
|
|
93
|
+
} catch (_e) {
|
|
94
|
+
throw new ContainerException(`Failed to resolve constant: ${identifier.toString()}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
hasConstant(identifier) {
|
|
98
|
+
return sharedDI.isBound(identifier);
|
|
99
|
+
}
|
|
100
|
+
removeConstant(identifier) {
|
|
101
|
+
if (sharedDI.isBound(identifier)) {
|
|
102
|
+
sharedDI.unbind(identifier);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
addAlias(alias, target) {
|
|
106
|
+
this.alias[alias] = target;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
var container = new Container;
|
|
110
|
+
export {
|
|
111
|
+
injectable,
|
|
112
|
+
inject,
|
|
113
|
+
container,
|
|
114
|
+
EContainerScope,
|
|
115
|
+
ContainerException,
|
|
116
|
+
Container
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
//# debugId=B6689203364C4C0664756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"import { Exception } from \"@ooneex/exception\";\nimport { HttpStatus } from \"@ooneex/http-status\";\n\nexport class ContainerException extends Exception {\n constructor(message: string, data: Record<string, unknown> = {}) {\n super(message, {\n status: HttpStatus.Code.InternalServerError,\n data,\n });\n this.name = \"ContainerException\";\n }\n}\n",
|
|
8
8
|
"/** biome-ignore-all lint/suspicious/noExplicitAny: trust me */\n\nexport enum EContainerScope {\n Singleton = \"singleton\",\n Transient = \"transient\",\n Request = \"request\",\n}\n\nexport interface IContainer {\n add: (target: new (...args: any[]) => any, scope?: EContainerScope) => void;\n get: <T>(target: (new (...args: any[]) => T) | string) => T;\n has: (target: (new (...args: any[]) => any) | string) => boolean;\n remove: (target: (new (...args: any[]) => any) | string) => void;\n addConstant: <T>(identifier: string | symbol, value: T) => void;\n getConstant: <T>(identifier: string | symbol) => T;\n hasConstant: (identifier: string | symbol) => boolean;\n removeConstant(identifier: string | symbol): void;\n addAlias<T>(alias: string,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0664756E2164756E21",
|
|
12
12
|
"names": []
|
|
13
13
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/container",
|
|
3
3
|
"description": "Lightweight dependency injection container built on Inversify for managing services and their lifecycle",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.15",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"npm:publish": "bun publish --tolerate-republish --access public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@ooneex/exception": "0.0.
|
|
32
|
-
"@ooneex/http-status": "0.0.
|
|
31
|
+
"@ooneex/exception": "0.0.13",
|
|
32
|
+
"@ooneex/http-status": "0.0.13",
|
|
33
33
|
"inversify": "^7.10.4"
|
|
34
34
|
},
|
|
35
35
|
"keywords": [
|