nano-injector 1.0.3 → 1.0.5
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 -21
- package/README.md +240 -221
- package/lib/Binder.js +83 -86
- package/lib/Injector.js +158 -159
- package/lib/InjectorsStack.js +33 -33
- package/lib/Provider.js +60 -59
- package/lib/index.js +18 -14
- package/package.json +52 -57
- package/typings/Binder.d.ts +44 -47
- package/typings/Injector.d.ts +93 -96
- package/typings/InjectorsStack.d.ts +16 -16
- package/typings/Provider.d.ts +48 -50
- package/typings/index.d.ts +2 -2
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2021 Roman Pukhalskyi
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Roman Pukhalskyi
|
|
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
CHANGED
|
@@ -1,221 +1,240 @@
|
|
|
1
|
-
# Nano-injector
|
|
2
|
-
|
|
3
|
-
Miniature dependency injection library for TypeScript and JavaScript.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
**Nano-injector**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
()
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
//
|
|
146
|
-
injector.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
injector.
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
)
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
1
|
+
# Nano-injector
|
|
2
|
+
|
|
3
|
+
**Miniature, type-safe dependency injection library for TypeScript and JavaScript.** Zero dependencies. No decorators.
|
|
4
|
+
~1.4KB gzipped.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- **Type Safe**: leveraged by TypeScript's type inference.
|
|
9
|
+
- **Zero Dependencies**: fast cold starts and small footprint.
|
|
10
|
+
- **No Decorators**: works with plain functions and classes.
|
|
11
|
+
- **Flexible**: supports value, class, and factory bindings.
|
|
12
|
+
- **Hierarchical**: supports parent-child injector relationships.
|
|
13
|
+
- **Circular Dependency Detection**: automatically detects and warns about cycles.
|
|
14
|
+
|
|
15
|
+
## Motivation
|
|
16
|
+
|
|
17
|
+
Most dependency injection libraries in the TypeScript ecosystem rely heavily on decorators (e.g., `@Inject`). While
|
|
18
|
+
familiar, this approach has drawbacks:
|
|
19
|
+
|
|
20
|
+
1. **Type Safety**: Decorators often use string tokens (e.g., `@Inject('IStorage')`), decoupling the type from the
|
|
21
|
+
injection token.
|
|
22
|
+
2. **Complexity**: They don't work natively with interfaces or primitives without workarounds.
|
|
23
|
+
3. **Verbosity**: requires duplicating type definitions and injection tokens.
|
|
24
|
+
|
|
25
|
+
**Nano-injector** takes a different approach. It uses **Providers**—plain functions that act as both unique identity
|
|
26
|
+
tokens and accessors. This allows TypeScript to infer types automatically, reducing boilerplate and runtime errors.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install nano-injector
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Core Concepts
|
|
35
|
+
|
|
36
|
+
- **Provider**: A typed function created via `createProvider<T>()`. It acts as a unique key for the dependency and a way
|
|
37
|
+
to access it.
|
|
38
|
+
- **Injector**: The container that manages bindings and resolves dependencies.
|
|
39
|
+
- **Binder**: An intermediate object returned when you bind a provider, used to configure _how_ it resolves (to a value,
|
|
40
|
+
a class, or a factory).
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### 1. Define Interfaces and Providers
|
|
45
|
+
|
|
46
|
+
Define your interfaces (or types) and create a corresponding provider for each. By convention, providers often start
|
|
47
|
+
with `$` to distinguish them from values.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { createProvider } from 'nano-injector';
|
|
51
|
+
|
|
52
|
+
// Define interfaces
|
|
53
|
+
interface Logger {
|
|
54
|
+
log(msg: string): void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface Config {
|
|
58
|
+
apiKey: string;
|
|
59
|
+
port: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Create providers
|
|
63
|
+
export const $Logger = createProvider<Logger>();
|
|
64
|
+
export const $Config = createProvider<Config>();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 2. Create the Injector
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { Injector } from 'nano-injector';
|
|
71
|
+
|
|
72
|
+
const injector = new Injector();
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. Bind Dependencies
|
|
76
|
+
|
|
77
|
+
You can bind providers to specific values, classes, or factories.
|
|
78
|
+
|
|
79
|
+
#### Value Binding
|
|
80
|
+
|
|
81
|
+
Useful for configuration, primitives, or existing instances.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
injector.bindProvider($Config).toValue({
|
|
85
|
+
apiKey: 'secret-key',
|
|
86
|
+
port: 8080,
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Class Binding
|
|
91
|
+
|
|
92
|
+
Useful for services. You can also make them singletons.
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
class ConsoleLogger implements Logger {
|
|
96
|
+
log(msg: string) {
|
|
97
|
+
console.log(msg);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Bind to a class (created on demand)
|
|
102
|
+
injector.bindProvider($Logger).toConstructor(ConsoleLogger);
|
|
103
|
+
|
|
104
|
+
// Bind as a singleton (created once and reused)
|
|
105
|
+
injector.bindProvider($Logger).toConstructor(ConsoleLogger).asSingleton();
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### Factory Binding
|
|
109
|
+
|
|
110
|
+
Useful when creation logic is dynamic or depends on other factors.
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
injector.bindProvider($Logger).toFactory((injector) => {
|
|
114
|
+
// logic to determine which logger to return
|
|
115
|
+
return new ConsoleLogger();
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 4. Injection
|
|
120
|
+
|
|
121
|
+
Nano-injector leverages TypeScript's default parameter values for injection. This makes your code cleaner and testable
|
|
122
|
+
(you can simply pass mocks as arguments in tests).
|
|
123
|
+
|
|
124
|
+
#### Constructor Injection
|
|
125
|
+
|
|
126
|
+
Used in classes. Simply assign the provider call as a default value.
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
class UserService {
|
|
130
|
+
// Dependencies are automatically resolved when `new` is called by the injector
|
|
131
|
+
constructor(
|
|
132
|
+
private logger = $Logger(),
|
|
133
|
+
private config = $Config(),
|
|
134
|
+
) {}
|
|
135
|
+
|
|
136
|
+
doWork() {
|
|
137
|
+
this.logger.log(`Starting work on port ${this.config.port}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
To instantiate this class with dependencies resolved:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
// The injector resolves arguments automatically
|
|
146
|
+
const userService = injector.createInstance(UserService);
|
|
147
|
+
userService.doWork();
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### Mixing Manual and Injected Parameters
|
|
151
|
+
|
|
152
|
+
You can mix manual arguments with injected ones. Since injection relies on default parameter values, manual arguments
|
|
153
|
+
usually come first.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
class User {
|
|
157
|
+
constructor(
|
|
158
|
+
public name: string, // Manual argument
|
|
159
|
+
private logger = $Logger(), // Injected
|
|
160
|
+
) {}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Pass manual arguments to createInstance
|
|
164
|
+
const user = injector.createInstance(User, 'Alice');
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
#### Function Injection
|
|
168
|
+
|
|
169
|
+
You can also use providers inside functions called by the injector.
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
function startServer() {
|
|
173
|
+
const config = $Config(); // Resolve dependency
|
|
174
|
+
console.log(`Server started on ${config.port}`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Execute the function within the injection context
|
|
178
|
+
injector.callFunc(startServer);
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### Direct Retrieval
|
|
182
|
+
|
|
183
|
+
If you need to get a value imperatively:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
const config = injector.getValue($Config);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Advanced Features
|
|
190
|
+
|
|
191
|
+
### Multi-Binding (Intersection Types)
|
|
192
|
+
|
|
193
|
+
You can bind multiple providers to a single value that implements all of them.
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
interface Writer {
|
|
197
|
+
write(data: string): void;
|
|
198
|
+
}
|
|
199
|
+
interface Reader {
|
|
200
|
+
read(): string;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const $Writer = createProvider<Writer>();
|
|
204
|
+
const $Reader = createProvider<Reader>();
|
|
205
|
+
|
|
206
|
+
class FileSystem implements Writer, Reader {
|
|
207
|
+
write(data: string) {
|
|
208
|
+
/*...*/
|
|
209
|
+
}
|
|
210
|
+
read() {
|
|
211
|
+
return '';
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Bind both providers to the same singleton instance
|
|
216
|
+
injector.bindProvider($Writer, $Reader).toConstructor(FileSystem).asSingleton();
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Hierarchical Injectors
|
|
220
|
+
|
|
221
|
+
Injectors can have parents. If a provider isn't found in the current injector, it looks up the tree.
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
const parent = new Injector();
|
|
225
|
+
parent.bindProvider($Config).toValue({ apiKey: 'global', port: 80 });
|
|
226
|
+
|
|
227
|
+
const child = new Injector({ parent });
|
|
228
|
+
|
|
229
|
+
// Child resolves $Config from parent
|
|
230
|
+
const config = child.getValue($Config);
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Circular Dependencies
|
|
234
|
+
|
|
235
|
+
The library automatically detects circular dependencies during resolution and throws a specific error to help you debug
|
|
236
|
+
the structure of your application.
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
MIT
|