@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 TypeWireTS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# TypeWire Inversify Adapter
|
|
2
|
+
|
|
3
|
+
The InversifyJS adapter for TypeWire, allowing you to use TypeWire with InversifyJS containers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @typewirets/inversify inversify
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
The adapter allows you to use TypeWire definitions with an existing InversifyJS container:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Container } from 'inversify';
|
|
17
|
+
import { createInversifyAdapter } from '@typewirets/inversify';
|
|
18
|
+
import { typeWireOf } from '@typewirets/core';
|
|
19
|
+
|
|
20
|
+
// Define your services
|
|
21
|
+
class Logger {
|
|
22
|
+
log(message: string): void {
|
|
23
|
+
console.log(`[LOG] ${message}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class UserService {
|
|
28
|
+
constructor(private logger: Logger) {}
|
|
29
|
+
|
|
30
|
+
getUser(id: string): void {
|
|
31
|
+
this.logger.log(`Getting user with id: ${id}`);
|
|
32
|
+
// Implementation...
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Create an Inversify container
|
|
37
|
+
const container = new Container();
|
|
38
|
+
|
|
39
|
+
// Create an adapter to use with TypeWire
|
|
40
|
+
const adapter = createInversifyAdapter(container);
|
|
41
|
+
|
|
42
|
+
// Create TypeWire definitions
|
|
43
|
+
const loggerWire = typeWireOf({
|
|
44
|
+
token: 'Logger',
|
|
45
|
+
creator: () => new Logger()
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const userServiceWire = typeWireOf({
|
|
49
|
+
token: 'UserService',
|
|
50
|
+
creator: (ctx) => new UserService(loggerWire.getInstance(ctx))
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Register with the Inversify container (using async/await)
|
|
54
|
+
async function setup() {
|
|
55
|
+
await loggerWire.apply(adapter);
|
|
56
|
+
await userServiceWire.apply(adapter);
|
|
57
|
+
|
|
58
|
+
// Resolve using the TypeWire API
|
|
59
|
+
const userService = userServiceWire.getInstance(adapter);
|
|
60
|
+
userService.getUser('123');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
setup();
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Using with Existing Inversify Code
|
|
67
|
+
|
|
68
|
+
You can also use TypeWire alongside existing Inversify bindings:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { Container, injectable, inject } from 'inversify';
|
|
72
|
+
import { createInversifyAdapter, adaptToResolutionContext } from '@typewirets/inversify';
|
|
73
|
+
import { typeWireOf, typedSymbolOf } from '@typewirets/core';
|
|
74
|
+
|
|
75
|
+
// Existing Inversify setup
|
|
76
|
+
const TYPES = {
|
|
77
|
+
Database: Symbol.for('Database')
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
@injectable()
|
|
81
|
+
class Database {
|
|
82
|
+
connect() { /* ... */ }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// TypeWire definition
|
|
86
|
+
const loggerWire = typeWireOf({
|
|
87
|
+
token: 'Logger',
|
|
88
|
+
creator: () => new Logger()
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Hybrid service that uses both
|
|
92
|
+
const userRepoWire = typeWireOf({
|
|
93
|
+
token: 'UserRepository',
|
|
94
|
+
creator: (ctx) => {
|
|
95
|
+
// Get TypeWire dependency
|
|
96
|
+
const logger = loggerWire.getInstance(ctx);
|
|
97
|
+
|
|
98
|
+
// Get Inversify dependency (through the adapter)
|
|
99
|
+
const db = ctx.get(typedSymbolOf('Database'));
|
|
100
|
+
|
|
101
|
+
return new UserRepository(logger, db);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Setup (using async/await)
|
|
106
|
+
async function setup() {
|
|
107
|
+
const container = new Container();
|
|
108
|
+
container.bind(TYPES.Database).to(Database).inSingletonScope();
|
|
109
|
+
|
|
110
|
+
const adapter = createInversifyAdapter(container);
|
|
111
|
+
await loggerWire.apply(adapter);
|
|
112
|
+
await userRepoWire.apply(adapter);
|
|
113
|
+
|
|
114
|
+
// Use the repository
|
|
115
|
+
const repo = userRepoWire.getInstance(adapter);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
setup();
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Adapter Functions
|
|
122
|
+
|
|
123
|
+
The adapter provides three main functions:
|
|
124
|
+
|
|
125
|
+
### adaptToResolutionContext
|
|
126
|
+
|
|
127
|
+
Creates a TypeWire `ResolutionContext` adapter for an Inversify container:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { adaptToResolutionContext } from '@typewirets/inversify';
|
|
131
|
+
|
|
132
|
+
const resolutionContext = adaptToResolutionContext(container);
|
|
133
|
+
const logger = loggerWire.getInstance(resolutionContext);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### adaptToBindingContext
|
|
137
|
+
|
|
138
|
+
Creates a TypeWire `BindingContext` adapter for an Inversify container:
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import { adaptToBindingContext } from '@typewirets/inversify';
|
|
142
|
+
|
|
143
|
+
const bindingContext = adaptToBindingContext(container);
|
|
144
|
+
await loggerWire.apply(bindingContext); // Note the await
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### createInversifyAdapter
|
|
148
|
+
|
|
149
|
+
Creates a combined adapter that implements both `ResolutionContext` and `BindingContext`:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { createInversifyAdapter } from '@typewirets/inversify';
|
|
153
|
+
|
|
154
|
+
const adapter = createInversifyAdapter(container);
|
|
155
|
+
await loggerWire.apply(adapter); // Note the await
|
|
156
|
+
const logger = loggerWire.getInstance(adapter);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Scope Mapping and Behavior
|
|
160
|
+
|
|
161
|
+
The adapter automatically maps TypeWire scopes to Inversify binding scopes:
|
|
162
|
+
|
|
163
|
+
| TypeWire Scope | Inversify Scope |
|
|
164
|
+
|----------------|-------------------|
|
|
165
|
+
| 'singleton' | 'Singleton' |
|
|
166
|
+
| 'transient' | 'Transient' |
|
|
167
|
+
| 'request' | 'Request' |
|
|
168
|
+
|
|
169
|
+
### Important Notes About Scoping:
|
|
170
|
+
|
|
171
|
+
- **Singleton and Transient Scopes**: These work as expected in all scenarios and are the recommended scopes to use with TypeWire. See the [core package documentation](../core/README.md) for more details on our recommended scoping strategy.
|
|
172
|
+
|
|
173
|
+
- **Request Scope**: While supported for compatibility, this requires special attention:
|
|
174
|
+
- Request scope in Inversify is designed to work with container hierarchies
|
|
175
|
+
- For request scope to work correctly, you must:
|
|
176
|
+
1. Use Inversify's container hierarchy properly (typically with a parent container and child containers per request)
|
|
177
|
+
2. Ensure your components using request-scoped dependencies are managed by Inversify (usually with `@injectable()` decorators)
|
|
178
|
+
- In standalone applications (non-web) or when not using container hierarchies, request scope may behave like transient scope
|
|
179
|
+
- We generally recommend using singleton scopes with explicit parameter passing instead of relying on request scopes
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
MIT
|
|
@@ -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,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _InversifyAdapter_instances, _InversifyAdapter_isSingleton;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.InversifyAdapter = void 0;
|
|
10
|
+
exports.adoptScope = adoptScope;
|
|
11
|
+
exports.createInversifyAdapter = createInversifyAdapter;
|
|
12
|
+
/**
|
|
13
|
+
* Converts a TypeWire scope string to the equivalent InversifyJS BindingScope.
|
|
14
|
+
*
|
|
15
|
+
* @param scope - The TypeWire scope string (e.g., 'singleton', 'transient', 'request')
|
|
16
|
+
* @returns The equivalent InversifyJS BindingScope or undefined if no conversion is possible
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // Convert a TypeWire scope to InversifyJS scope
|
|
21
|
+
* const inversifyScope = adoptScope('singleton'); // returns 'Singleton'
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function adoptScope(scope) {
|
|
25
|
+
if (!scope) {
|
|
26
|
+
return "Singleton";
|
|
27
|
+
}
|
|
28
|
+
switch (scope.toLowerCase()) {
|
|
29
|
+
case "singleton":
|
|
30
|
+
return "Singleton";
|
|
31
|
+
case "transient":
|
|
32
|
+
return "Transient";
|
|
33
|
+
case "request":
|
|
34
|
+
return "Request";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Adapter class that provides both TypeWire ResolutionContext and BindingContext
|
|
39
|
+
* interfaces on top of an InversifyJS container.
|
|
40
|
+
*
|
|
41
|
+
* This adapter allows TypeWire definitions to work with an existing InversifyJS
|
|
42
|
+
* container, providing a bridge between the two dependency injection systems.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const container = new Container();
|
|
47
|
+
* const adapter = new InversifyAdapter(container);
|
|
48
|
+
*
|
|
49
|
+
* // Now you can use TypeWire with your InversifyJS container
|
|
50
|
+
* await serviceWire.apply(adapter);
|
|
51
|
+
* const service = await serviceWire.getInstance(adapter);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
class InversifyAdapter {
|
|
55
|
+
/**
|
|
56
|
+
* Creates a new InversifyAdapter for the specified container.
|
|
57
|
+
*
|
|
58
|
+
* @param container - The InversifyJS container to adapt
|
|
59
|
+
*/
|
|
60
|
+
constructor(container) {
|
|
61
|
+
_InversifyAdapter_instances.add(this);
|
|
62
|
+
this.container = container;
|
|
63
|
+
/**
|
|
64
|
+
* Set of symbols for which instances have been loaded/initialized.
|
|
65
|
+
* Used to track which singleton instances have already been created.
|
|
66
|
+
*/
|
|
67
|
+
this.loaded = new Set();
|
|
68
|
+
/**
|
|
69
|
+
* Set of symbols that represent singleton-scoped dependencies.
|
|
70
|
+
*/
|
|
71
|
+
this.singletons = new Set();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Gets an instance of type T synchronously.
|
|
75
|
+
* For singleton-scoped dependencies, also tracks that they have been loaded.
|
|
76
|
+
*
|
|
77
|
+
* @template T - The type of instance to get
|
|
78
|
+
* @param typeSymbol - The type symbol identifying the dependency
|
|
79
|
+
* @returns An instance of type T
|
|
80
|
+
* @throws Error if the dependency cannot be resolved synchronously
|
|
81
|
+
*/
|
|
82
|
+
getSync(typeSymbol) {
|
|
83
|
+
if (!__classPrivateFieldGet(this, _InversifyAdapter_instances, "m", _InversifyAdapter_isSingleton).call(this, typeSymbol.symbol)) {
|
|
84
|
+
return this.container.get(typeSymbol.symbol);
|
|
85
|
+
}
|
|
86
|
+
const result = this.container.get(typeSymbol.symbol);
|
|
87
|
+
this.loaded.add(typeSymbol.symbol);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Gets an instance of type T asynchronously.
|
|
92
|
+
* For singleton-scoped dependencies, also tracks that they have been loaded.
|
|
93
|
+
*
|
|
94
|
+
* @template T - The type of instance to get
|
|
95
|
+
* @param typeSymbol - The type symbol identifying the dependency
|
|
96
|
+
* @returns A promise that resolves to an instance of type T
|
|
97
|
+
* @throws Error if the dependency is not found
|
|
98
|
+
*/
|
|
99
|
+
async get(typeSymbol) {
|
|
100
|
+
if (!__classPrivateFieldGet(this, _InversifyAdapter_instances, "m", _InversifyAdapter_isSingleton).call(this, typeSymbol.symbol)) {
|
|
101
|
+
return this.container.getAsync(typeSymbol.symbol);
|
|
102
|
+
}
|
|
103
|
+
const result = await this.container.getAsync(typeSymbol.symbol);
|
|
104
|
+
this.loaded.add(typeSymbol.symbol);
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Binds a TypeWire to the InversifyJS container.
|
|
109
|
+
* Configures the binding with the appropriate scope and creator function.
|
|
110
|
+
*
|
|
111
|
+
* @param typeWire - The TypeWire to bind
|
|
112
|
+
*/
|
|
113
|
+
bind(typeWire) {
|
|
114
|
+
const bound = this.container
|
|
115
|
+
.bind(typeWire.type.symbol)
|
|
116
|
+
.toDynamicValue((_ctx) => {
|
|
117
|
+
return typeWire.creator(this);
|
|
118
|
+
});
|
|
119
|
+
const scope = adoptScope(typeWire.scope);
|
|
120
|
+
if (scope) {
|
|
121
|
+
switch (scope) {
|
|
122
|
+
case "Singleton":
|
|
123
|
+
this.singletons.add(typeWire.type.symbol);
|
|
124
|
+
bound.inSingletonScope();
|
|
125
|
+
break;
|
|
126
|
+
case "Transient":
|
|
127
|
+
bound.inTransientScope();
|
|
128
|
+
break;
|
|
129
|
+
case "Request":
|
|
130
|
+
bound.inRequestScope();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Removes a binding from the InversifyJS container.
|
|
136
|
+
* Also removes any tracking information for the binding.
|
|
137
|
+
*
|
|
138
|
+
* @param typeSymbol - The type symbol identifying the binding to remove
|
|
139
|
+
* @returns A promise that resolves when the unbinding is complete
|
|
140
|
+
*/
|
|
141
|
+
async unbind(typeSymbol) {
|
|
142
|
+
this.loaded.delete(typeSymbol.symbol);
|
|
143
|
+
this.singletons.delete(typeSymbol.symbol);
|
|
144
|
+
return this.container.unbind(typeSymbol.symbol);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Checks if a binding exists in the InversifyJS container.
|
|
148
|
+
*
|
|
149
|
+
* @param typeSymbol - The type symbol to check
|
|
150
|
+
* @returns True if the binding exists, false otherwise
|
|
151
|
+
*/
|
|
152
|
+
isBound(typeSymbol) {
|
|
153
|
+
return this.container.isBound(typeSymbol.symbol);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.InversifyAdapter = InversifyAdapter;
|
|
157
|
+
_InversifyAdapter_instances = new WeakSet(), _InversifyAdapter_isSingleton = function _InversifyAdapter_isSingleton(symbol) {
|
|
158
|
+
return this.singletons.has(symbol);
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Creates an adapter that provides both TypeWire ResolutionContext and BindingContext
|
|
162
|
+
* interfaces using an InversifyJS container.
|
|
163
|
+
*
|
|
164
|
+
* This is a convenience function for creating an InversifyAdapter.
|
|
165
|
+
*
|
|
166
|
+
* @param container - The InversifyJS container to adapt
|
|
167
|
+
* @returns An object implementing both TypeWire interfaces
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* const container = new Container();
|
|
172
|
+
* const adapter = createInversifyAdapter(container);
|
|
173
|
+
*
|
|
174
|
+
* // Now you can use TypeWire with your InversifyJS container
|
|
175
|
+
* await serviceWire.apply(adapter);
|
|
176
|
+
* const service = await serviceWire.getInstance(adapter);
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
function createInversifyAdapter(container) {
|
|
180
|
+
return new InversifyAdapter(container);
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAqBA,gCAaC;AAgKD,wDAIC;AA7LD;;;;;;;;;;;GAWG;AACH,SAAgB,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,MAAa,gBAAgB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,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;;;;;;;;;;;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