decorator-dependency-injection 1.0.5 → 1.0.6
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/README.md +47 -1
- package/index.d.ts +29 -0
- package/index.js +27 -0
- package/package.json +1 -1
- package/src/Container.js +14 -0
package/README.md
CHANGED
|
@@ -353,6 +353,51 @@ import {clearContainer} from 'decorator-dependency-injection';
|
|
|
353
353
|
clearContainer(); // Removes all registered singletons, factories, and mocks
|
|
354
354
|
```
|
|
355
355
|
|
|
356
|
+
### Resolving Dependencies Without Decorators
|
|
357
|
+
|
|
358
|
+
The `resolve` function allows non-class code (plain functions, modules, callbacks, etc.) to retrieve instances from the DI container:
|
|
359
|
+
|
|
360
|
+
```javascript
|
|
361
|
+
import {Singleton, Factory, resolve} from 'decorator-dependency-injection';
|
|
362
|
+
|
|
363
|
+
@Singleton()
|
|
364
|
+
class UserService {
|
|
365
|
+
getUser(id) {
|
|
366
|
+
return { id, name: 'John' }
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
@Factory()
|
|
371
|
+
class Logger {
|
|
372
|
+
constructor(prefix) {
|
|
373
|
+
this.prefix = prefix
|
|
374
|
+
}
|
|
375
|
+
log(msg) {
|
|
376
|
+
console.log(`[${this.prefix}] ${msg}`)
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Use in plain functions
|
|
381
|
+
function handleRequest(req) {
|
|
382
|
+
const userService = resolve(UserService)
|
|
383
|
+
return userService.getUser(req.userId)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Use with factory parameters
|
|
387
|
+
function createLogger(moduleName) {
|
|
388
|
+
return resolve(Logger, moduleName)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Use with named registrations
|
|
392
|
+
const db = resolve('databaseConnection')
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
This is useful when:
|
|
396
|
+
- Integrating with frameworks that don't support decorators
|
|
397
|
+
- Writing utility functions that need DI access
|
|
398
|
+
- Bridging between decorator-based and non-decorator code
|
|
399
|
+
- Testing or debugging the container directly
|
|
400
|
+
|
|
356
401
|
### Validation Helpers
|
|
357
402
|
|
|
358
403
|
The library provides utilities to validate registrations at runtime, which is useful for catching configuration
|
|
@@ -529,4 +574,5 @@ npm test
|
|
|
529
574
|
- 1.0.2 - Added proxy option to @Mock decorator
|
|
530
575
|
- 1.0.3 - Added @InjectLazy decorator
|
|
531
576
|
- 1.0.4 - Added Container abstraction, clearContainer(), TypeScript definitions, improved proxy support
|
|
532
|
-
- 1.0.5 - Added private field and accessor support for @Inject and @InjectLazy, debug mode, validation helpers
|
|
577
|
+
- 1.0.5 - Added private field and accessor support for @Inject and @InjectLazy, debug mode, validation helpers
|
|
578
|
+
- 1.0.6 - Added resolve() function for non-decorator code
|
package/index.d.ts
CHANGED
|
@@ -60,6 +60,12 @@ export declare class Container {
|
|
|
60
60
|
*/
|
|
61
61
|
has<T>(clazzOrName: InjectionToken<T>): boolean
|
|
62
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Resolve and return an instance by class or name.
|
|
65
|
+
* This allows non-decorator code to retrieve instances from the container.
|
|
66
|
+
*/
|
|
67
|
+
resolve<T>(clazzOrName: InjectionToken<T>, ...params: any[]): T
|
|
68
|
+
|
|
63
69
|
/**
|
|
64
70
|
* Get or create an instance based on the context.
|
|
65
71
|
*/
|
|
@@ -217,6 +223,29 @@ export declare function isRegistered<T>(clazzOrName: InjectionToken<T>): boolean
|
|
|
217
223
|
*/
|
|
218
224
|
export declare function validateRegistrations<T extends InjectionToken[]>(...tokens: T): void
|
|
219
225
|
|
|
226
|
+
/**
|
|
227
|
+
* Resolve and return an instance by class or name.
|
|
228
|
+
* This allows non-decorator code (plain functions, modules, etc.) to retrieve
|
|
229
|
+
* instances from the DI container.
|
|
230
|
+
*
|
|
231
|
+
* @param clazzOrName The class or name to resolve
|
|
232
|
+
* @param params Optional parameters to pass to the constructor
|
|
233
|
+
* @returns The resolved instance
|
|
234
|
+
* @throws Error if the class or name is not registered
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* // In a plain function:
|
|
238
|
+
* function handleRequest(req: Request) {
|
|
239
|
+
* const userService = resolve(UserService)
|
|
240
|
+
* return userService.getUser(req.userId)
|
|
241
|
+
* }
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* // With a named registration:
|
|
245
|
+
* const db = resolve<Database>('database')
|
|
246
|
+
*/
|
|
247
|
+
export declare function resolve<T>(clazzOrName: InjectionToken<T>, ...params: any[]): T
|
|
248
|
+
|
|
220
249
|
/**
|
|
221
250
|
* Create a proxy that delegates to the mock first, then falls back to the original.
|
|
222
251
|
* This is an internal utility but exported for advanced use cases.
|
package/index.js
CHANGED
|
@@ -313,6 +313,33 @@ export function validateRegistrations(...tokens) {
|
|
|
313
313
|
}
|
|
314
314
|
}
|
|
315
315
|
|
|
316
|
+
/**
|
|
317
|
+
* Resolve and return an instance by class or name.
|
|
318
|
+
* This allows non-decorator code (plain functions, modules, etc.) to retrieve
|
|
319
|
+
* instances from the DI container.
|
|
320
|
+
*
|
|
321
|
+
* @template T
|
|
322
|
+
* @param {string|Function} clazzOrName The class or name to resolve
|
|
323
|
+
* @param {...*} params Parameters to pass to the constructor
|
|
324
|
+
* @returns {T} The resolved instance
|
|
325
|
+
* @throws {Error} If the class or name is not registered
|
|
326
|
+
* @example
|
|
327
|
+
* // In a plain function:
|
|
328
|
+
* function handleRequest(req) {
|
|
329
|
+
* const userService = resolve(UserService)
|
|
330
|
+
* return userService.getUser(req.userId)
|
|
331
|
+
* }
|
|
332
|
+
* @example
|
|
333
|
+
* // With a named registration:
|
|
334
|
+
* const db = resolve('database')
|
|
335
|
+
* @example
|
|
336
|
+
* // With factory parameters:
|
|
337
|
+
* const logger = resolve(Logger, 'my-module')
|
|
338
|
+
*/
|
|
339
|
+
export function resolve(clazzOrName, ...params) {
|
|
340
|
+
return defaultContainer.resolve(clazzOrName, ...params)
|
|
341
|
+
}
|
|
342
|
+
|
|
316
343
|
// Export Container class for advanced use cases (e.g., isolated containers)
|
|
317
344
|
export {Container}
|
|
318
345
|
|
package/package.json
CHANGED
package/src/Container.js
CHANGED
|
@@ -106,6 +106,20 @@ export class Container {
|
|
|
106
106
|
return this.#instances.has(clazzOrName)
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Resolve and return an instance by class or name.
|
|
111
|
+
* This allows non-decorator code to retrieve instances from the container.
|
|
112
|
+
* @template T
|
|
113
|
+
* @param {string|Function} clazzOrName The class or name to resolve
|
|
114
|
+
* @param {...*} params Parameters to pass to the constructor
|
|
115
|
+
* @returns {T} The resolved instance
|
|
116
|
+
* @throws {Error} If the class or name is not registered
|
|
117
|
+
*/
|
|
118
|
+
resolve(clazzOrName, ...params) {
|
|
119
|
+
const instanceContext = this.getContext(clazzOrName)
|
|
120
|
+
return this.getInstance(instanceContext, params)
|
|
121
|
+
}
|
|
122
|
+
|
|
109
123
|
/**
|
|
110
124
|
* Get or create an instance based on the context.
|
|
111
125
|
* @param {InstanceContext} instanceContext The instance context
|