@typewirets/inversify 0.1.0
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/LICENSE +21 -0
- package/README.md +183 -0
- package/dist/cjs/index.d.ts +114 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +182 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.d.ts +114 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +176 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/types/index.d.ts +114 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +176 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +69 -0
- package/src/index.ts +199 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _InversifyAdapter_instances, _InversifyAdapter_isSingleton;
|
|
7
|
+
/**
|
|
8
|
+
* Converts a TypeWire scope string to the equivalent InversifyJS BindingScope.
|
|
9
|
+
*
|
|
10
|
+
* @param scope - The TypeWire scope string (e.g., 'singleton', 'transient', 'request')
|
|
11
|
+
* @returns The equivalent InversifyJS BindingScope or undefined if no conversion is possible
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Convert a TypeWire scope to InversifyJS scope
|
|
16
|
+
* const inversifyScope = adoptScope('singleton'); // returns 'Singleton'
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function adoptScope(scope) {
|
|
20
|
+
if (!scope) {
|
|
21
|
+
return "Singleton";
|
|
22
|
+
}
|
|
23
|
+
switch (scope.toLowerCase()) {
|
|
24
|
+
case "singleton":
|
|
25
|
+
return "Singleton";
|
|
26
|
+
case "transient":
|
|
27
|
+
return "Transient";
|
|
28
|
+
case "request":
|
|
29
|
+
return "Request";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Adapter class that provides both TypeWire ResolutionContext and BindingContext
|
|
34
|
+
* interfaces on top of an InversifyJS container.
|
|
35
|
+
*
|
|
36
|
+
* This adapter allows TypeWire definitions to work with an existing InversifyJS
|
|
37
|
+
* container, providing a bridge between the two dependency injection systems.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const container = new Container();
|
|
42
|
+
* const adapter = new InversifyAdapter(container);
|
|
43
|
+
*
|
|
44
|
+
* // Now you can use TypeWire with your InversifyJS container
|
|
45
|
+
* await serviceWire.apply(adapter);
|
|
46
|
+
* const service = await serviceWire.getInstance(adapter);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export class InversifyAdapter {
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new InversifyAdapter for the specified container.
|
|
52
|
+
*
|
|
53
|
+
* @param container - The InversifyJS container to adapt
|
|
54
|
+
*/
|
|
55
|
+
constructor(container) {
|
|
56
|
+
_InversifyAdapter_instances.add(this);
|
|
57
|
+
this.container = container;
|
|
58
|
+
/**
|
|
59
|
+
* Set of symbols for which instances have been loaded/initialized.
|
|
60
|
+
* Used to track which singleton instances have already been created.
|
|
61
|
+
*/
|
|
62
|
+
this.loaded = new Set();
|
|
63
|
+
/**
|
|
64
|
+
* Set of symbols that represent singleton-scoped dependencies.
|
|
65
|
+
*/
|
|
66
|
+
this.singletons = new Set();
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Gets an instance of type T synchronously.
|
|
70
|
+
* For singleton-scoped dependencies, also tracks that they have been loaded.
|
|
71
|
+
*
|
|
72
|
+
* @template T - The type of instance to get
|
|
73
|
+
* @param typeSymbol - The type symbol identifying the dependency
|
|
74
|
+
* @returns An instance of type T
|
|
75
|
+
* @throws Error if the dependency cannot be resolved synchronously
|
|
76
|
+
*/
|
|
77
|
+
getSync(typeSymbol) {
|
|
78
|
+
if (!__classPrivateFieldGet(this, _InversifyAdapter_instances, "m", _InversifyAdapter_isSingleton).call(this, typeSymbol.symbol)) {
|
|
79
|
+
return this.container.get(typeSymbol.symbol);
|
|
80
|
+
}
|
|
81
|
+
const result = this.container.get(typeSymbol.symbol);
|
|
82
|
+
this.loaded.add(typeSymbol.symbol);
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Gets an instance of type T asynchronously.
|
|
87
|
+
* For singleton-scoped dependencies, also tracks that they have been loaded.
|
|
88
|
+
*
|
|
89
|
+
* @template T - The type of instance to get
|
|
90
|
+
* @param typeSymbol - The type symbol identifying the dependency
|
|
91
|
+
* @returns A promise that resolves to an instance of type T
|
|
92
|
+
* @throws Error if the dependency is not found
|
|
93
|
+
*/
|
|
94
|
+
async get(typeSymbol) {
|
|
95
|
+
if (!__classPrivateFieldGet(this, _InversifyAdapter_instances, "m", _InversifyAdapter_isSingleton).call(this, typeSymbol.symbol)) {
|
|
96
|
+
return this.container.getAsync(typeSymbol.symbol);
|
|
97
|
+
}
|
|
98
|
+
const result = await this.container.getAsync(typeSymbol.symbol);
|
|
99
|
+
this.loaded.add(typeSymbol.symbol);
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Binds a TypeWire to the InversifyJS container.
|
|
104
|
+
* Configures the binding with the appropriate scope and creator function.
|
|
105
|
+
*
|
|
106
|
+
* @param typeWire - The TypeWire to bind
|
|
107
|
+
*/
|
|
108
|
+
bind(typeWire) {
|
|
109
|
+
const bound = this.container
|
|
110
|
+
.bind(typeWire.type.symbol)
|
|
111
|
+
.toDynamicValue((_ctx) => {
|
|
112
|
+
return typeWire.creator(this);
|
|
113
|
+
});
|
|
114
|
+
const scope = adoptScope(typeWire.scope);
|
|
115
|
+
if (scope) {
|
|
116
|
+
switch (scope) {
|
|
117
|
+
case "Singleton":
|
|
118
|
+
this.singletons.add(typeWire.type.symbol);
|
|
119
|
+
bound.inSingletonScope();
|
|
120
|
+
break;
|
|
121
|
+
case "Transient":
|
|
122
|
+
bound.inTransientScope();
|
|
123
|
+
break;
|
|
124
|
+
case "Request":
|
|
125
|
+
bound.inRequestScope();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Removes a binding from the InversifyJS container.
|
|
131
|
+
* Also removes any tracking information for the binding.
|
|
132
|
+
*
|
|
133
|
+
* @param typeSymbol - The type symbol identifying the binding to remove
|
|
134
|
+
* @returns A promise that resolves when the unbinding is complete
|
|
135
|
+
*/
|
|
136
|
+
async unbind(typeSymbol) {
|
|
137
|
+
this.loaded.delete(typeSymbol.symbol);
|
|
138
|
+
this.singletons.delete(typeSymbol.symbol);
|
|
139
|
+
return this.container.unbind(typeSymbol.symbol);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Checks if a binding exists in the InversifyJS container.
|
|
143
|
+
*
|
|
144
|
+
* @param typeSymbol - The type symbol to check
|
|
145
|
+
* @returns True if the binding exists, false otherwise
|
|
146
|
+
*/
|
|
147
|
+
isBound(typeSymbol) {
|
|
148
|
+
return this.container.isBound(typeSymbol.symbol);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
_InversifyAdapter_instances = new WeakSet(), _InversifyAdapter_isSingleton = function _InversifyAdapter_isSingleton(symbol) {
|
|
152
|
+
return this.singletons.has(symbol);
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Creates an adapter that provides both TypeWire ResolutionContext and BindingContext
|
|
156
|
+
* interfaces using an InversifyJS container.
|
|
157
|
+
*
|
|
158
|
+
* This is a convenience function for creating an InversifyAdapter.
|
|
159
|
+
*
|
|
160
|
+
* @param container - The InversifyJS container to adapt
|
|
161
|
+
* @returns An object implementing both TypeWire interfaces
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* const container = new Container();
|
|
166
|
+
* const adapter = createInversifyAdapter(container);
|
|
167
|
+
*
|
|
168
|
+
* // Now you can use TypeWire with your InversifyJS container
|
|
169
|
+
* await serviceWire.apply(adapter);
|
|
170
|
+
* const service = await serviceWire.getInstance(adapter);
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export function createInversifyAdapter(container) {
|
|
174
|
+
return new InversifyAdapter(container);
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AASA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,QAAQ,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC;QAC5B,KAAK,WAAW;YACd,OAAO,WAAW,CAAC;QACrB,KAAK,WAAW;YACd,OAAO,WAAW,CAAC;QACrB,KAAK,SAAS;YACZ,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,gBAAgB;IAc3B;;;;OAIG;IACH,YAAqB,SAAoB;;QAApB,cAAS,GAAT,SAAS,CAAW;QAhBzC;;;WAGG;QACc,WAAM,GAAgB,IAAI,GAAG,EAAE,CAAC;QAEjD;;WAEG;QACc,eAAU,GAAgB,IAAI,GAAG,EAAE,CAAC;IAOT,CAAC;IAY7C;;;;;;;;OAQG;IACH,OAAO,CAAI,UAAyB;QAClC,IAAI,CAAC,uBAAA,IAAI,kEAAa,MAAjB,IAAI,EAAc,UAAU,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I,CAAC,MAAM,CAAC,CAAC;oBAC1C,KAAK,CAAC,gBAAgB,EAAE,CAAC;oBACzB,MAAM;gBACR,KAAK,WAAW;oBACd,KAAK,CAAC,gBAAgB,EAAE,CAAC;oBACzB,MAAM;gBACR,KAAK,SAAS;oBACZ,KAAK,CAAC,cAAc,EAAE,CAAC;YAC3B,CAAC;QACH,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,MAAM,CAAC,UAAyB;QACpC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QACtC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QAC1C,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IAClD,CAAC;IAED;;;;;OAKG;IACH,OAAO,CAAC,UAAyB;QAC/B,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACnD,CAAC;CACF;oHA7Fc,MAAc;IACzB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC;AA6FH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,sBAAsB,CACpC,SAAoB;IAEpB,OAAO,IAAI,gBAAgB,CAAC,SAAS,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { AnyTypeSymbol, AnyTypeWire, BindingContext, TypeSymbol, ResolutionContext as TypeWireResolutionContext } from "@typewirets/core";
|
|
2
|
+
import type { BindingScope, Container } from "inversify";
|
|
3
|
+
/**
|
|
4
|
+
* Converts a TypeWire scope string to the equivalent InversifyJS BindingScope.
|
|
5
|
+
*
|
|
6
|
+
* @param scope - The TypeWire scope string (e.g., 'singleton', 'transient', 'request')
|
|
7
|
+
* @returns The equivalent InversifyJS BindingScope or undefined if no conversion is possible
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Convert a TypeWire scope to InversifyJS scope
|
|
12
|
+
* const inversifyScope = adoptScope('singleton'); // returns 'Singleton'
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function adoptScope(scope?: string): BindingScope | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Adapter class that provides both TypeWire ResolutionContext and BindingContext
|
|
18
|
+
* interfaces on top of an InversifyJS container.
|
|
19
|
+
*
|
|
20
|
+
* This adapter allows TypeWire definitions to work with an existing InversifyJS
|
|
21
|
+
* container, providing a bridge between the two dependency injection systems.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const container = new Container();
|
|
26
|
+
* const adapter = new InversifyAdapter(container);
|
|
27
|
+
*
|
|
28
|
+
* // Now you can use TypeWire with your InversifyJS container
|
|
29
|
+
* await serviceWire.apply(adapter);
|
|
30
|
+
* const service = await serviceWire.getInstance(adapter);
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare class InversifyAdapter implements TypeWireResolutionContext, BindingContext {
|
|
34
|
+
#private;
|
|
35
|
+
readonly container: Container;
|
|
36
|
+
/**
|
|
37
|
+
* Set of symbols for which instances have been loaded/initialized.
|
|
38
|
+
* Used to track which singleton instances have already been created.
|
|
39
|
+
*/
|
|
40
|
+
private readonly loaded;
|
|
41
|
+
/**
|
|
42
|
+
* Set of symbols that represent singleton-scoped dependencies.
|
|
43
|
+
*/
|
|
44
|
+
private readonly singletons;
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new InversifyAdapter for the specified container.
|
|
47
|
+
*
|
|
48
|
+
* @param container - The InversifyJS container to adapt
|
|
49
|
+
*/
|
|
50
|
+
constructor(container: Container);
|
|
51
|
+
/**
|
|
52
|
+
* Gets an instance of type T synchronously.
|
|
53
|
+
* For singleton-scoped dependencies, also tracks that they have been loaded.
|
|
54
|
+
*
|
|
55
|
+
* @template T - The type of instance to get
|
|
56
|
+
* @param typeSymbol - The type symbol identifying the dependency
|
|
57
|
+
* @returns An instance of type T
|
|
58
|
+
* @throws Error if the dependency cannot be resolved synchronously
|
|
59
|
+
*/
|
|
60
|
+
getSync<T>(typeSymbol: AnyTypeSymbol): T;
|
|
61
|
+
/**
|
|
62
|
+
* Gets an instance of type T asynchronously.
|
|
63
|
+
* For singleton-scoped dependencies, also tracks that they have been loaded.
|
|
64
|
+
*
|
|
65
|
+
* @template T - The type of instance to get
|
|
66
|
+
* @param typeSymbol - The type symbol identifying the dependency
|
|
67
|
+
* @returns A promise that resolves to an instance of type T
|
|
68
|
+
* @throws Error if the dependency is not found
|
|
69
|
+
*/
|
|
70
|
+
get<T>(typeSymbol: TypeSymbol<T>): Promise<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Binds a TypeWire to the InversifyJS container.
|
|
73
|
+
* Configures the binding with the appropriate scope and creator function.
|
|
74
|
+
*
|
|
75
|
+
* @param typeWire - The TypeWire to bind
|
|
76
|
+
*/
|
|
77
|
+
bind(typeWire: AnyTypeWire): void | Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Removes a binding from the InversifyJS container.
|
|
80
|
+
* Also removes any tracking information for the binding.
|
|
81
|
+
*
|
|
82
|
+
* @param typeSymbol - The type symbol identifying the binding to remove
|
|
83
|
+
* @returns A promise that resolves when the unbinding is complete
|
|
84
|
+
*/
|
|
85
|
+
unbind(typeSymbol: AnyTypeSymbol): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Checks if a binding exists in the InversifyJS container.
|
|
88
|
+
*
|
|
89
|
+
* @param typeSymbol - The type symbol to check
|
|
90
|
+
* @returns True if the binding exists, false otherwise
|
|
91
|
+
*/
|
|
92
|
+
isBound(typeSymbol: AnyTypeSymbol): boolean;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Creates an adapter that provides both TypeWire ResolutionContext and BindingContext
|
|
96
|
+
* interfaces using an InversifyJS container.
|
|
97
|
+
*
|
|
98
|
+
* This is a convenience function for creating an InversifyAdapter.
|
|
99
|
+
*
|
|
100
|
+
* @param container - The InversifyJS container to adapt
|
|
101
|
+
* @returns An object implementing both TypeWire interfaces
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const container = new Container();
|
|
106
|
+
* const adapter = createInversifyAdapter(container);
|
|
107
|
+
*
|
|
108
|
+
* // Now you can use TypeWire with your InversifyJS container
|
|
109
|
+
* await serviceWire.apply(adapter);
|
|
110
|
+
* const service = await serviceWire.getInstance(adapter);
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export declare function createInversifyAdapter(container: Container): TypeWireResolutionContext & BindingContext;
|
|
114
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,cAAc,EACd,UAAU,EACV,iBAAiB,IAAI,yBAAyB,EAC/C,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEzD;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAanE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,gBACX,YAAW,yBAAyB,EAAE,cAAc;;IAkBxC,QAAQ,CAAC,SAAS,EAAE,SAAS;IAhBzC;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0B;IAEjD;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA0B;IAErD;;;;OAIG;gBACkB,SAAS,EAAE,SAAS;IAYzC;;;;;;;;OAQG;IACH,OAAO,CAAC,CAAC,EAAE,UAAU,EAAE,aAAa,GAAG,CAAC;IAUxC;;;;;;;;OAQG;IACG,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAUnD;;;;;OAKG;IACH,IAAI,CAAC,QAAQ,EAAE,WAAW,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAuBjD;;;;;;OAMG;IACG,MAAM,CAAC,UAAU,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAMtD;;;;;OAKG;IACH,OAAO,CAAC,UAAU,EAAE,aAAa,GAAG,OAAO;CAG5C;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,SAAS,GACnB,yBAAyB,GAAG,cAAc,CAE5C"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _InversifyAdapter_instances, _InversifyAdapter_isSingleton;
|
|
7
|
+
/**
|
|
8
|
+
* Converts a TypeWire scope string to the equivalent InversifyJS BindingScope.
|
|
9
|
+
*
|
|
10
|
+
* @param scope - The TypeWire scope string (e.g., 'singleton', 'transient', 'request')
|
|
11
|
+
* @returns The equivalent InversifyJS BindingScope or undefined if no conversion is possible
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Convert a TypeWire scope to InversifyJS scope
|
|
16
|
+
* const inversifyScope = adoptScope('singleton'); // returns 'Singleton'
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function adoptScope(scope) {
|
|
20
|
+
if (!scope) {
|
|
21
|
+
return "Singleton";
|
|
22
|
+
}
|
|
23
|
+
switch (scope.toLowerCase()) {
|
|
24
|
+
case "singleton":
|
|
25
|
+
return "Singleton";
|
|
26
|
+
case "transient":
|
|
27
|
+
return "Transient";
|
|
28
|
+
case "request":
|
|
29
|
+
return "Request";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Adapter class that provides both TypeWire ResolutionContext and BindingContext
|
|
34
|
+
* interfaces on top of an InversifyJS container.
|
|
35
|
+
*
|
|
36
|
+
* This adapter allows TypeWire definitions to work with an existing InversifyJS
|
|
37
|
+
* container, providing a bridge between the two dependency injection systems.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const container = new Container();
|
|
42
|
+
* const adapter = new InversifyAdapter(container);
|
|
43
|
+
*
|
|
44
|
+
* // Now you can use TypeWire with your InversifyJS container
|
|
45
|
+
* await serviceWire.apply(adapter);
|
|
46
|
+
* const service = await serviceWire.getInstance(adapter);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export class InversifyAdapter {
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new InversifyAdapter for the specified container.
|
|
52
|
+
*
|
|
53
|
+
* @param container - The InversifyJS container to adapt
|
|
54
|
+
*/
|
|
55
|
+
constructor(container) {
|
|
56
|
+
_InversifyAdapter_instances.add(this);
|
|
57
|
+
this.container = container;
|
|
58
|
+
/**
|
|
59
|
+
* Set of symbols for which instances have been loaded/initialized.
|
|
60
|
+
* Used to track which singleton instances have already been created.
|
|
61
|
+
*/
|
|
62
|
+
this.loaded = new Set();
|
|
63
|
+
/**
|
|
64
|
+
* Set of symbols that represent singleton-scoped dependencies.
|
|
65
|
+
*/
|
|
66
|
+
this.singletons = new Set();
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Gets an instance of type T synchronously.
|
|
70
|
+
* For singleton-scoped dependencies, also tracks that they have been loaded.
|
|
71
|
+
*
|
|
72
|
+
* @template T - The type of instance to get
|
|
73
|
+
* @param typeSymbol - The type symbol identifying the dependency
|
|
74
|
+
* @returns An instance of type T
|
|
75
|
+
* @throws Error if the dependency cannot be resolved synchronously
|
|
76
|
+
*/
|
|
77
|
+
getSync(typeSymbol) {
|
|
78
|
+
if (!__classPrivateFieldGet(this, _InversifyAdapter_instances, "m", _InversifyAdapter_isSingleton).call(this, typeSymbol.symbol)) {
|
|
79
|
+
return this.container.get(typeSymbol.symbol);
|
|
80
|
+
}
|
|
81
|
+
const result = this.container.get(typeSymbol.symbol);
|
|
82
|
+
this.loaded.add(typeSymbol.symbol);
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Gets an instance of type T asynchronously.
|
|
87
|
+
* For singleton-scoped dependencies, also tracks that they have been loaded.
|
|
88
|
+
*
|
|
89
|
+
* @template T - The type of instance to get
|
|
90
|
+
* @param typeSymbol - The type symbol identifying the dependency
|
|
91
|
+
* @returns A promise that resolves to an instance of type T
|
|
92
|
+
* @throws Error if the dependency is not found
|
|
93
|
+
*/
|
|
94
|
+
async get(typeSymbol) {
|
|
95
|
+
if (!__classPrivateFieldGet(this, _InversifyAdapter_instances, "m", _InversifyAdapter_isSingleton).call(this, typeSymbol.symbol)) {
|
|
96
|
+
return this.container.getAsync(typeSymbol.symbol);
|
|
97
|
+
}
|
|
98
|
+
const result = await this.container.getAsync(typeSymbol.symbol);
|
|
99
|
+
this.loaded.add(typeSymbol.symbol);
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Binds a TypeWire to the InversifyJS container.
|
|
104
|
+
* Configures the binding with the appropriate scope and creator function.
|
|
105
|
+
*
|
|
106
|
+
* @param typeWire - The TypeWire to bind
|
|
107
|
+
*/
|
|
108
|
+
bind(typeWire) {
|
|
109
|
+
const bound = this.container
|
|
110
|
+
.bind(typeWire.type.symbol)
|
|
111
|
+
.toDynamicValue((_ctx) => {
|
|
112
|
+
return typeWire.creator(this);
|
|
113
|
+
});
|
|
114
|
+
const scope = adoptScope(typeWire.scope);
|
|
115
|
+
if (scope) {
|
|
116
|
+
switch (scope) {
|
|
117
|
+
case "Singleton":
|
|
118
|
+
this.singletons.add(typeWire.type.symbol);
|
|
119
|
+
bound.inSingletonScope();
|
|
120
|
+
break;
|
|
121
|
+
case "Transient":
|
|
122
|
+
bound.inTransientScope();
|
|
123
|
+
break;
|
|
124
|
+
case "Request":
|
|
125
|
+
bound.inRequestScope();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Removes a binding from the InversifyJS container.
|
|
131
|
+
* Also removes any tracking information for the binding.
|
|
132
|
+
*
|
|
133
|
+
* @param typeSymbol - The type symbol identifying the binding to remove
|
|
134
|
+
* @returns A promise that resolves when the unbinding is complete
|
|
135
|
+
*/
|
|
136
|
+
async unbind(typeSymbol) {
|
|
137
|
+
this.loaded.delete(typeSymbol.symbol);
|
|
138
|
+
this.singletons.delete(typeSymbol.symbol);
|
|
139
|
+
return this.container.unbind(typeSymbol.symbol);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Checks if a binding exists in the InversifyJS container.
|
|
143
|
+
*
|
|
144
|
+
* @param typeSymbol - The type symbol to check
|
|
145
|
+
* @returns True if the binding exists, false otherwise
|
|
146
|
+
*/
|
|
147
|
+
isBound(typeSymbol) {
|
|
148
|
+
return this.container.isBound(typeSymbol.symbol);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
_InversifyAdapter_instances = new WeakSet(), _InversifyAdapter_isSingleton = function _InversifyAdapter_isSingleton(symbol) {
|
|
152
|
+
return this.singletons.has(symbol);
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Creates an adapter that provides both TypeWire ResolutionContext and BindingContext
|
|
156
|
+
* interfaces using an InversifyJS container.
|
|
157
|
+
*
|
|
158
|
+
* This is a convenience function for creating an InversifyAdapter.
|
|
159
|
+
*
|
|
160
|
+
* @param container - The InversifyJS container to adapt
|
|
161
|
+
* @returns An object implementing both TypeWire interfaces
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* const container = new Container();
|
|
166
|
+
* const adapter = createInversifyAdapter(container);
|
|
167
|
+
*
|
|
168
|
+
* // Now you can use TypeWire with your InversifyJS container
|
|
169
|
+
* await serviceWire.apply(adapter);
|
|
170
|
+
* const service = await serviceWire.getInstance(adapter);
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export function createInversifyAdapter(container) {
|
|
174
|
+
return new InversifyAdapter(container);
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AASA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,QAAQ,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC;QAC5B,KAAK,WAAW;YACd,OAAO,WAAW,CAAC;QACrB,KAAK,WAAW;YACd,OAAO,WAAW,CAAC;QACrB,KAAK,SAAS;YACZ,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,gBAAgB;IAc3B;;;;OAIG;IACH,YAAqB,SAAoB;;QAApB,cAAS,GAAT,SAAS,CAAW;QAhBzC;;;WAGG;QACc,WAAM,GAAgB,IAAI,GAAG,EAAE,CAAC;QAEjD;;WAEG;QACc,eAAU,GAAgB,IAAI,GAAG,EAAE,CAAC;IAOT,CAAC;IAY7C;;;;;;;;OAQG;IACH,OAAO,CAAI,UAAyB;QAClC,IAAI,CAAC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CAAC,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;oBAC1C,KAAK,CAAC,gBAAgB,EAAE,CAAC;oBACzB,MAAM;gBACR,KAAK,WAAW;oBACd,KAAK,CAAC,gBAAgB,EAAE,CAAC;oBACzB,MAAM;gBACR,KAAK,SAAS;oBACZ,KAAK,CAAC,cAAc,EAAE,CAAC;YAC3B,CAAC;QACH,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,MAAM,CAAC,UAAyB;QACpC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QACtC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QAC1C,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IAClD,CAAC;IAED;;;;;OAKG;IACH,OAAO,CAAC,UAAyB;QAC/B,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACnD,CAAC;CACF;oHA7Fc,MAAc;IACzB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC;AA6FH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,sBAAsB,CACpC,SAAoB;IAEpB,OAAO,IAAI,gBAAgB,CAAC,SAAS,CAAC,CAAC;AACzC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@typewirets/inversify",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "InversifyJS adapter for TypeWire dependency injection",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/cjs/index.js",
|
|
7
|
+
"module": "dist/esm/index.js",
|
|
8
|
+
"types": "dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"import": "./dist/esm/index.js",
|
|
13
|
+
"require": "./dist/cjs/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src/*.ts",
|
|
19
|
+
"!src/**/*.test.ts",
|
|
20
|
+
"!src/**/__tests__/**",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"typescript",
|
|
26
|
+
"dependency-injection",
|
|
27
|
+
"di",
|
|
28
|
+
"inversify",
|
|
29
|
+
"adapter",
|
|
30
|
+
"typewire"
|
|
31
|
+
],
|
|
32
|
+
"author": "Rex Kim",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/typewirets/typewirets.git",
|
|
37
|
+
"directory": "packages/inversify"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/typewirets/typewirets/issues"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/typewirets/typewirets#readme",
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^22.13.10",
|
|
45
|
+
"rimraf": "^6.0.1",
|
|
46
|
+
"typescript": "5.8.2",
|
|
47
|
+
"vitest": "^3.0.8",
|
|
48
|
+
"@typewirets/typescript-config": "0.0.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"inversify": "^7.1.0",
|
|
52
|
+
"@typewirets/core": "0.1.0"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "pnpm clean && pnpm --filter '@typewirets/inversify' --parallel '/^build:.*/'",
|
|
56
|
+
"build:cjs": "tsc --project tsconfig.build.json --outDir dist/cjs --module Commonjs --moduleResolution Node",
|
|
57
|
+
"build:esm": "tsc --project tsconfig.build.json --outDir dist/esm --module ES2020 --moduleResolution Node",
|
|
58
|
+
"build:types": "tsc --project tsconfig.build.json --outDir dist/types --declaration --declarationMap",
|
|
59
|
+
"dev": "pnpm clean && pnpm --filter '@typewirets/inversify' --parallel '/^dev:.*/'",
|
|
60
|
+
"dev:cjs": "tsc --watch --project tsconfig.build.json --outDir dist/cjs --module Commonjs --moduleResolution Node",
|
|
61
|
+
"dev:esm": "tsc --watch --project tsconfig.build.json --outDir dist/esm --module ES2020 --moduleResolution Node",
|
|
62
|
+
"dev:types": "tsc --watch --project tsconfig.build.json --outDir dist/types --declaration --declarationMap",
|
|
63
|
+
"clean": "rimraf dist",
|
|
64
|
+
"check-types": "tsc --noEmit",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"test:watch": "vitest",
|
|
67
|
+
"test:coverage": "vitest run --coverage"
|
|
68
|
+
}
|
|
69
|
+
}
|