@di-framework/di-framework 0.0.0-prerelease-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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Geoff Seemueller
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,505 @@
1
+ # di-framework
2
+
3
+ [![CI](https://github.com/geoffsee/di-framework/actions/workflows/ci.yml/badge.svg)](https://github.com/geoffsee/di-framework/actions/workflows/ci.yml)
4
+ [![npm version](https://img.shields.io/npm/v/di-framework.svg)](https://www.npmjs.com/package/di-framework)
5
+ [![license](https://img.shields.io/github/license/geoffsee/di-framework.svg)](LICENSE)
6
+
7
+ A lightweight, type-safe Dependency Injection framework for TypeScript using decorators. This framework automatically manages service instantiation, dependency resolution, and lifecycle management.
8
+
9
+ ## Installation
10
+
11
+ No external dependencies required! The framework works with SWC and TypeScript's native decorator support.
12
+
13
+ Just ensure you have:
14
+
15
+ - TypeScript 5.0+
16
+ - SWC or TypeScript compiler with `experimentalDecorators` and `emitDecoratorMetadata` enabled
17
+
18
+ The decorators are fully integrated with SWC's native support - no need for `reflect-metadata` or any other polyfill.
19
+
20
+ ## Quick Start
21
+
22
+ ### 1. Basic Service
23
+
24
+ ```typescript
25
+ import { Container } from "@di-framework/di-framework/decorators";
26
+
27
+ @Container()
28
+ export class DatabaseService {
29
+ connect(): void {
30
+ console.log("Connected to database");
31
+ }
32
+ }
33
+ ```
34
+
35
+ ### 2. Service with Dependencies
36
+
37
+ ```typescript
38
+ import { Container, Component } from "@di-framework/di-framework/decorators";
39
+ import { DatabaseService } from "./services/DatabaseService";
40
+
41
+ @Container()
42
+ export class UserService {
43
+ @Component(DatabaseService)
44
+ private db!: DatabaseService;
45
+
46
+ constructor() {}
47
+
48
+ getUser(id: string) {
49
+ return this.db.query(`SELECT * FROM users WHERE id = '${id}'`);
50
+ }
51
+ }
52
+ ```
53
+
54
+ Note: Property injection is used for all dependencies. This works seamlessly with SWC and TypeScript's native decorator support.
55
+
56
+ ### 3. Resolve Services
57
+
58
+ ```typescript
59
+ import { useContainer } from "@di-framework/di-framework/container";
60
+ import { UserService } from "./services/UserService";
61
+
62
+ const container = useContainer();
63
+ const userService = container.resolve<UserService>(UserService);
64
+
65
+ // All dependencies are automatically injected!
66
+ userService.getUser("123");
67
+ ```
68
+
69
+ ## API Reference
70
+
71
+ ### `@Container(options?)`
72
+
73
+ Marks a class as injectable and automatically registers it with the DI container.
74
+
75
+ **Options:**
76
+
77
+ - `singleton?: boolean` (default: `true`) - Create a new instance each time or reuse the same instance
78
+ - `container?: DIContainer` - Specify a custom container (defaults to global container)
79
+ - Note: Import as `import { Container as DIContainer } from '@di-framework/di-framework/container'` to avoid name collision with the `@Container` decorator.
80
+
81
+ **Example:**
82
+
83
+ ```typescript
84
+ @Container({ singleton: false })
85
+ export class RequestScopedService {
86
+ // New instance created for each resolution
87
+ }
88
+ ```
89
+
90
+ ### `@Component(target)`
91
+
92
+ Marks a constructor parameter or property for dependency injection.
93
+
94
+ **Parameters:**
95
+
96
+ - `target` - The class to inject or a string identifier for factory-registered services
97
+
98
+ **Example - Constructor Parameter:**
99
+
100
+ ```typescript
101
+ @Container()
102
+ export class OrderService {
103
+ constructor(@Component(DatabaseService) private db: DatabaseService) {}
104
+ }
105
+ ```
106
+
107
+ **Example - Property Injection:**
108
+
109
+ ```typescript
110
+ @Container()
111
+ export class ReportService {
112
+ @Component(DatabaseService)
113
+ private db: DatabaseService;
114
+ }
115
+ ```
116
+
117
+ ### `@Telemetry(options?)`
118
+
119
+ Marks a method for telemetry tracking. When called, it emits a `telemetry` event on the container. Works with both synchronous and asynchronous methods.
120
+
121
+ **Options:**
122
+
123
+ - `logging?: boolean` (default: `false`) - If true, logs the method execution details (status and duration) to the console.
124
+
125
+ **Example:**
126
+
127
+ ```typescript
128
+ @Container()
129
+ export class ApiService {
130
+ @Telemetry({ logging: true })
131
+ async fetchData(id: string) {
132
+ // ...
133
+ }
134
+ }
135
+ ```
136
+
137
+ ### `@TelemetryListener()`
138
+
139
+ Marks a method as a listener for telemetry events. The method will be automatically registered to the container's `telemetry` event when the service is instantiated.
140
+
141
+ **Example:**
142
+
143
+ ```typescript
144
+ @Container()
145
+ export class MonitoringService {
146
+ @TelemetryListener()
147
+ onTelemetry(event: any) {
148
+ console.log(
149
+ `Method ${event.className}.${event.methodName} took ${event.endTime - event.startTime}ms`,
150
+ );
151
+ }
152
+ }
153
+ ```
154
+
155
+ ### `useContainer()`
156
+
157
+ Returns the global DI container instance.
158
+
159
+ ```typescript
160
+ import { useContainer } from "@di-framework/di-framework/container";
161
+
162
+ const container = useContainer();
163
+ ```
164
+
165
+ ### `container.register(serviceClass, options?)`
166
+
167
+ Manually register a service class.
168
+
169
+ ```typescript
170
+ container.register(UserService, { singleton: true });
171
+ ```
172
+
173
+ ### `container.registerFactory(name, factory, options?)`
174
+
175
+ Register a service using a factory function.
176
+
177
+ ```typescript
178
+ container.registerFactory(
179
+ "config",
180
+ () => ({
181
+ apiKey: process.env.API_KEY,
182
+ dbUrl: process.env.DATABASE_URL,
183
+ }),
184
+ { singleton: true },
185
+ );
186
+ ```
187
+
188
+ ### `container.resolve(serviceClass)`
189
+
190
+ Resolve and get an instance of a service.
191
+
192
+ ```typescript
193
+ const userService = container.resolve<UserService>(UserService);
194
+ // or by name
195
+ const config = container.resolve("config");
196
+ ```
197
+
198
+ ### `container.has(serviceClass)`
199
+
200
+ Check if a service is registered.
201
+
202
+ ```typescript
203
+ if (container.has(UserService)) {
204
+ const service = container.resolve(UserService);
205
+ }
206
+ ```
207
+
208
+ ### `container.getServiceNames()`
209
+
210
+ Get all registered service names.
211
+
212
+ ```typescript
213
+ const names = container.getServiceNames();
214
+ console.log(names); // ['DatabaseService', 'UserService', ...]
215
+ ```
216
+
217
+ ### `container.on(event, listener)`
218
+
219
+ Subscribe to DI container lifecycle events (observer pattern).
220
+
221
+ **Events:**
222
+
223
+ - `registered` - fired when a class or factory is registered
224
+ - `resolved` - fired whenever a service is resolved (cached or fresh)
225
+ - `constructed` - fired when `construct()` creates a new instance
226
+ - `cleared` - fired when the container is cleared
227
+
228
+ **Example:**
229
+
230
+ ```typescript
231
+ const unsubscribe = container.on("resolved", ({ key, fromCache }) => {
232
+ console.log(
233
+ `Resolved ${typeof key === "string" ? key : key.name} (fromCache=${fromCache})`,
234
+ );
235
+ });
236
+
237
+ unsubscribe(); // stop listening
238
+ ```
239
+
240
+ ### `container.construct(serviceClass, overrides?)`
241
+
242
+ Create a fresh instance without registering it, while still honoring dependency injection. Useful for constructor-pattern scenarios where you need to supply specific primitives/config values.
243
+
244
+ ```typescript
245
+ import { Component } from "@di-framework/di-framework/decorators";
246
+ import { LoggerService } from "@di-framework/di-framework/services/LoggerService";
247
+
248
+ class Greeter {
249
+ constructor(
250
+ @Component(LoggerService) private logger: LoggerService,
251
+ private greeting: string,
252
+ ) {}
253
+ }
254
+
255
+ const greeter = container.construct(Greeter, { 1: "hello world" });
256
+ ```
257
+
258
+ ### `container.fork(options?)`
259
+
260
+ Clone the container registrations (prototype pattern) into a new container. Pass `{ carrySingletons: true }` to reuse existing singleton instances; default is to start with fresh instances.
261
+
262
+ ```typescript
263
+ const testContainer = container.fork({ carrySingletons: false });
264
+ ```
265
+
266
+ ## Advanced Examples
267
+
268
+ ### Multiple Dependencies
269
+
270
+ ```typescript
271
+ @Container()
272
+ export class ApplicationContext {
273
+ constructor(
274
+ @Component(DatabaseService) private db: DatabaseService,
275
+ @Component(LoggerService) private logger: LoggerService,
276
+ @Component(AuthService) private auth: AuthService,
277
+ ) {}
278
+
279
+ async initialize() {
280
+ this.logger.log("Initializing application...");
281
+ await this.db.connect();
282
+ this.auth.setup();
283
+ }
284
+ }
285
+ ```
286
+
287
+ ### Transient (Non-Singleton) Services
288
+
289
+ ```typescript
290
+ @Container({ singleton: false })
291
+ export class RequestContext {
292
+ id = Math.random().toString();
293
+
294
+ constructor(@Component(LoggerService) private logger: LoggerService) {
295
+ this.logger.log(`Request context created: ${this.id}`);
296
+ }
297
+ }
298
+
299
+ // Each resolve creates a new instance
300
+ const ctx1 = container.resolve(RequestContext); // new instance
301
+ const ctx2 = container.resolve(RequestContext); // different instance
302
+ ```
303
+
304
+ ### Lifecycle Methods
305
+
306
+ Services can optionally implement lifecycle methods:
307
+
308
+ ```typescript
309
+ @Container()
310
+ export class DatabaseService {
311
+ private connected = false;
312
+
313
+ setEnv(env: Record<string, any>) {
314
+ // Called to initialize environment-specific config
315
+ console.log("DB URL:", env.DATABASE_URL);
316
+ }
317
+
318
+ setCtx(context: any) {
319
+ // Called to set execution context
320
+ console.log("Context:", context);
321
+ }
322
+
323
+ connect() {
324
+ this.connected = true;
325
+ }
326
+ }
327
+
328
+ // Calling lifecycle methods
329
+ const db = container.resolve(DatabaseService);
330
+ db.setEnv(process.env);
331
+ db.setCtx({ userId: "123" });
332
+ db.connect();
333
+ ```
334
+
335
+ ### Factory Functions
336
+
337
+ ```typescript
338
+ container.registerFactory(
339
+ "apiClient",
340
+ () => {
341
+ return new HttpClient({
342
+ baseUrl: process.env.API_URL,
343
+ timeout: 5000,
344
+ });
345
+ },
346
+ { singleton: true },
347
+ );
348
+
349
+ // Use in services
350
+ @Container()
351
+ export class UserService {
352
+ constructor(@Component("apiClient") private api: any) {}
353
+ }
354
+ ```
355
+
356
+ ## How It Works
357
+
358
+ 1. **Decoration**: When you decorate a class with `@Container()`, the decorator registers it with the global container
359
+ 2. **Registration**: The class is stored in the container with metadata about its dependencies
360
+ 3. **Resolution**: When you call `container.resolve(ServiceClass)`:
361
+ - The container creates a new instance (or returns existing singleton)
362
+ - It examines the constructor parameters and their types
363
+ - It recursively resolves each dependency
364
+ - Dependencies are injected into the constructor
365
+ - The configured instance is returned
366
+ 4. **Caching**: Singleton instances are cached and reused
367
+
368
+ ## Comparison with SAMPLE.ts
369
+
370
+ ### Before (Manual - SAMPLE.ts)
371
+
372
+ ```typescript
373
+ const createServerContext = (env, ctx) => {
374
+ if (!instanceState.member) {
375
+ const contextInstance = Context.create({
376
+ contactService: ContactService.create({}),
377
+ assetService: AssetService.create({}),
378
+ transactionService: TransactionService.create({}),
379
+ // ... 20+ more services manually created and wired
380
+ chatService: ChatService.create({
381
+ openAIApiKey: env.OPENAI_API_KEY,
382
+ // ... manual configuration
383
+ }),
384
+ });
385
+ instanceState.member = contextInstance;
386
+ }
387
+
388
+ instanceState.member.setEnv(env);
389
+ instanceState.member.setCtx(ctx);
390
+ // ... manual dependency wiring
391
+ instanceState.member.knowledgeService.setAttachmentService(
392
+ instanceState.member.attachmentService,
393
+ );
394
+ return instanceState.member;
395
+ };
396
+ ```
397
+
398
+ ### After (DI Framework)
399
+
400
+ ```typescript
401
+ @Container()
402
+ export class ApplicationContext {
403
+ constructor(
404
+ @Component(ContactService) private contactService: ContactService,
405
+ @Component(AssetService) private assetService: AssetService,
406
+ @Component(TransactionService)
407
+ private transactionService: TransactionService,
408
+ // ... all services automatically injected
409
+ @Component(ChatService) private chatService: ChatService,
410
+ ) {}
411
+
412
+ setEnv(env: Record<string, any>) {
413
+ // Services are already available via constructor injection
414
+ this.chatService.initialize(env.OPENAI_API_KEY);
415
+ }
416
+
417
+ setCtx(ctx: any) {
418
+ // All services have access to context
419
+ }
420
+ }
421
+
422
+ // Usage
423
+ const container = useContainer();
424
+ const appContext = container.resolve(ApplicationContext);
425
+ appContext.setEnv(env);
426
+ appContext.setCtx(ctx);
427
+ ```
428
+
429
+ **Benefits:**
430
+
431
+ - No manual service instantiation
432
+ - No manual dependency wiring
433
+ - Automatic singleton management
434
+ - Type-safe dependency resolution
435
+ - Easier to test (mock services simply by registering test implementations)
436
+ - Scales better as services grow
437
+
438
+ ## Error Handling
439
+
440
+ ### Circular Dependencies
441
+
442
+ ```typescript
443
+ // This will be detected and throw an error:
444
+ @Container()
445
+ class ServiceA {
446
+ constructor(@Component(ServiceB) private b: ServiceB) {}
447
+ }
448
+
449
+ @Container()
450
+ class ServiceB {
451
+ constructor(@Component(ServiceA) private a: ServiceA) {}
452
+ }
453
+
454
+ // Error: Circular dependency detected while resolving ServiceA
455
+ ```
456
+
457
+ ### Unregistered Services
458
+
459
+ ```typescript
460
+ @Container()
461
+ class MyService {
462
+ constructor(@Component(UnregisteredService) private s: UnregisteredService) {}
463
+ }
464
+
465
+ // Error: Service 'UnregisteredService' is not registered in the DI container
466
+ ```
467
+
468
+ ## Best Practices
469
+
470
+ 1. **Mark all services with `@Container()`** - Makes dependency management explicit
471
+ 2. **Use constructor injection** - Preferred over property injection for mandatory dependencies
472
+ 3. **Use property injection for optional dependencies** - Keep it minimal
473
+ 4. **No need to import reflect-metadata** - This framework uses a lightweight metadata store
474
+ 5. **Separate service interfaces from implementations** - For easier testing
475
+ 6. **Use singletons for stateless services** - Most services should be singletons
476
+ 7. **Use transient (non-singleton) for stateful services** - Request/session scoped services
477
+
478
+ ## Testing
479
+
480
+ ```typescript
481
+ // Create a test container
482
+ import { Container as DIContainer } from "@di-framework/di-framework/container";
483
+
484
+ const testContainer = new DIContainer();
485
+
486
+ // Register mock implementations
487
+ class MockDatabaseService {
488
+ query() {
489
+ return { mock: true };
490
+ }
491
+ }
492
+
493
+ testContainer.register(MockDatabaseService);
494
+
495
+ // Register dependencies
496
+ testContainer.register(UserService);
497
+
498
+ // Test the service with mocked dependencies
499
+ const userService = testContainer.resolve(UserService);
500
+ expect(userService.getUser("1")).toEqual({ mock: true });
501
+ ```
502
+
503
+ ## License
504
+
505
+ MIT
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Dependency Injection Container
3
+ *
4
+ * Manages service registration, dependency resolution, and instance lifecycle.
5
+ * Supports singleton pattern and automatic dependency injection via decorators.
6
+ * Works with SWC and TypeScript's native decorator support.
7
+ */
8
+ type Constructor<T = any> = new (...args: any[]) => T;
9
+ type ServiceFactory<T = any> = () => T;
10
+ type ContainerEventPayloads = {
11
+ registered: {
12
+ key: string | Constructor;
13
+ singleton: boolean;
14
+ kind: "class" | "factory";
15
+ };
16
+ resolved: {
17
+ key: string | Constructor;
18
+ instance: any;
19
+ singleton: boolean;
20
+ fromCache: boolean;
21
+ };
22
+ constructed: {
23
+ key: Constructor;
24
+ instance: any;
25
+ overrides: Record<number, any>;
26
+ };
27
+ cleared: {
28
+ count: number;
29
+ };
30
+ telemetry: {
31
+ className: string;
32
+ methodName: string;
33
+ args: any[];
34
+ startTime: number;
35
+ endTime?: number;
36
+ result?: any;
37
+ error?: any;
38
+ };
39
+ };
40
+ type Listener<T> = (payload: T) => void;
41
+ export declare const TELEMETRY_METADATA_KEY = "di:telemetry";
42
+ export declare const TELEMETRY_LISTENER_METADATA_KEY = "di:telemetry-listener";
43
+ declare function defineMetadata(key: string | symbol, value: any, target: any): void;
44
+ declare function getMetadata(key: string | symbol, target: any): any;
45
+ declare function hasMetadata(key: string | symbol, target: any): boolean;
46
+ declare function getOwnMetadata(key: string | symbol, target: any): any;
47
+ export declare class Container {
48
+ private services;
49
+ private resolutionStack;
50
+ private listeners;
51
+ /**
52
+ * Register a service class as injectable
53
+ */
54
+ register<T>(serviceClass: Constructor<T>, options?: {
55
+ singleton?: boolean;
56
+ }): this;
57
+ /**
58
+ * Register a service using a factory function
59
+ */
60
+ registerFactory<T>(name: string, factory: ServiceFactory<T>, options?: {
61
+ singleton?: boolean;
62
+ }): this;
63
+ /**
64
+ * Get or create a service instance
65
+ */
66
+ resolve<T>(serviceClass: Constructor<T> | string): T;
67
+ /**
68
+ * Construct a new instance without registering it in the container.
69
+ * Supports constructor overrides for primitives/config (constructor pattern).
70
+ * Always returns a fresh instance (no caching).
71
+ */
72
+ construct<T>(serviceClass: Constructor<T>, overrides?: Record<number, any>): T;
73
+ /**
74
+ * Check if a service is registered
75
+ */
76
+ has(serviceClass: Constructor | string): boolean;
77
+ /**
78
+ * Clear all registered services
79
+ */
80
+ clear(): void;
81
+ /**
82
+ * Get all registered service names
83
+ */
84
+ getServiceNames(): string[];
85
+ /**
86
+ * Fork the container (prototype pattern): clone registrations into a new container.
87
+ * Optionally carry over existing singleton instances.
88
+ */
89
+ fork(options?: {
90
+ carrySingletons?: boolean;
91
+ }): Container;
92
+ /**
93
+ * Subscribe to container lifecycle events (observer pattern).
94
+ * Returns an unsubscribe function.
95
+ */
96
+ on<K extends keyof ContainerEventPayloads>(event: K, listener: Listener<ContainerEventPayloads[K]>): () => void;
97
+ /**
98
+ * Remove a previously registered listener
99
+ */
100
+ off<K extends keyof ContainerEventPayloads>(event: K, listener: Listener<ContainerEventPayloads[K]>): void;
101
+ private emit;
102
+ /**
103
+ * Private method to instantiate a service
104
+ */
105
+ private instantiate;
106
+ /**
107
+ * Apply telemetry tracking and listeners to an instance
108
+ */
109
+ private applyTelemetry;
110
+ /**
111
+ * Check if a function is a class constructor
112
+ */
113
+ private isClass;
114
+ /**
115
+ * Extract parameter names from constructor
116
+ */
117
+ private getConstructorParamNames;
118
+ /**
119
+ * Extract parameter types from TypeScript compiled code
120
+ * Looks for type annotations in the compiled constructor signature
121
+ */
122
+ private extractParamTypesFromSource;
123
+ }
124
+ /**
125
+ * Global DI container instance
126
+ */
127
+ export declare const container: Container;
128
+ /**
129
+ * Get the global DI container
130
+ */
131
+ export declare function useContainer(): Container;
132
+ /**
133
+ * Export metadata functions for use in decorators
134
+ * These provide a simple, reflect-metadata-free way to store and access metadata
135
+ */
136
+ export { defineMetadata, getMetadata, hasMetadata, getOwnMetadata };