fragment-ts 1.0.30 → 1.0.32

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.
Files changed (52) hide show
  1. package/API.md +752 -0
  2. package/DOCS.md +555 -0
  3. package/README.md +76 -339
  4. package/USAGE.md +309 -1306
  5. package/dist/cli/commands/init.command.js +1 -1
  6. package/dist/core/container/di-container.d.ts.map +1 -1
  7. package/dist/core/container/di-container.js +62 -106
  8. package/dist/core/container/di-container.js.map +1 -1
  9. package/dist/core/decorators/exception-filter.decorator.d.ts +5 -0
  10. package/dist/core/decorators/exception-filter.decorator.d.ts.map +1 -0
  11. package/dist/core/decorators/exception-filter.decorator.js +23 -0
  12. package/dist/core/decorators/exception-filter.decorator.js.map +1 -0
  13. package/dist/core/decorators/guard.decorator.d.ts +5 -0
  14. package/dist/core/decorators/guard.decorator.d.ts.map +1 -0
  15. package/dist/core/decorators/guard.decorator.js +23 -0
  16. package/dist/core/decorators/guard.decorator.js.map +1 -0
  17. package/dist/core/decorators/injection.decorators.d.ts.map +1 -1
  18. package/dist/core/decorators/injection.decorators.js +5 -0
  19. package/dist/core/decorators/injection.decorators.js.map +1 -1
  20. package/dist/core/decorators/interceptor.decorator.d.ts +5 -0
  21. package/dist/core/decorators/interceptor.decorator.d.ts.map +1 -0
  22. package/dist/core/decorators/interceptor.decorator.js +23 -0
  23. package/dist/core/decorators/interceptor.decorator.js.map +1 -0
  24. package/dist/core/decorators/middleware.decorator.d.ts +8 -0
  25. package/dist/core/decorators/middleware.decorator.d.ts.map +1 -0
  26. package/dist/core/decorators/middleware.decorator.js +28 -0
  27. package/dist/core/decorators/middleware.decorator.js.map +1 -0
  28. package/dist/core/metadata/metadata-keys.d.ts +1 -0
  29. package/dist/core/metadata/metadata-keys.d.ts.map +1 -1
  30. package/dist/core/metadata/metadata-keys.js +1 -0
  31. package/dist/core/metadata/metadata-keys.js.map +1 -1
  32. package/dist/core/metadata/metadata-storage.d.ts +20 -4
  33. package/dist/core/metadata/metadata-storage.d.ts.map +1 -1
  34. package/dist/core/metadata/metadata-storage.js +94 -14
  35. package/dist/core/metadata/metadata-storage.js.map +1 -1
  36. package/dist/web/application.d.ts.map +1 -1
  37. package/dist/web/application.js +79 -10
  38. package/dist/web/application.js.map +1 -1
  39. package/dist/web/interfaces.d.ts +5 -1
  40. package/dist/web/interfaces.d.ts.map +1 -1
  41. package/package.json +1 -1
  42. package/src/cli/commands/init.command.ts +1 -1
  43. package/src/core/container/di-container.ts +95 -177
  44. package/src/core/decorators/exception-filter.decorator.ts +20 -0
  45. package/src/core/decorators/guard.decorator.ts +20 -0
  46. package/src/core/decorators/injection.decorators.ts +5 -0
  47. package/src/core/decorators/interceptor.decorator.ts +20 -0
  48. package/src/core/decorators/middleware.decorator.ts +25 -0
  49. package/src/core/metadata/metadata-keys.ts +1 -0
  50. package/src/core/metadata/metadata-storage.ts +128 -24
  51. package/src/web/application.ts +207 -53
  52. package/src/web/interfaces.ts +11 -2
package/API.md ADDED
@@ -0,0 +1,752 @@
1
+ # API Reference
2
+
3
+ ## Core Decorators
4
+
5
+ ### `@FragmentApplication(options?)`
6
+
7
+ Application class decorator that configures the web server.
8
+
9
+ **Parameters:**
10
+ - `options` (ApplicationOptions, optional):
11
+ - `port`: number (default: 3000) - Server port
12
+ - `host`: string (default: "0.0.0.0") - Server host
13
+ - `configPath`: string (default: "fragment.json") - TypeORM config path
14
+ - `autoScan`: boolean (default: true) - Enable automatic component scanning
15
+
16
+ **Example:**
17
+ ```typescript
18
+ @FragmentApplication({
19
+ port: 8080,
20
+ host: 'localhost',
21
+ autoScan: true
22
+ })
23
+ export class Application {}
24
+ ```
25
+
26
+ ### `@Controller(path?)`
27
+
28
+ Class decorator that marks a class as an HTTP controller.
29
+
30
+ **Parameters:**
31
+ - `path`: string (optional) - Base route path for all methods in this controller
32
+
33
+ **Example:**
34
+ ```typescript
35
+ @Controller('/users')
36
+ export class UserController {}
37
+ ```
38
+
39
+ ### `@Service()`
40
+
41
+ Class decorator that marks a class as a service (singleton-scoped by default).
42
+
43
+ **Example:**
44
+ ```typescript
45
+ @Service()
46
+ export class UserService {}
47
+ ```
48
+
49
+ ### `@Repository()`
50
+
51
+ Class decorator that marks a class as a repository (singleton-scoped by default).
52
+
53
+ **Example:**
54
+ ```typescript
55
+ @Repository()
56
+ export class UserRepository {}
57
+ ```
58
+
59
+ ### `@AutoConfiguration()`
60
+
61
+ Class decorator that marks a class as an auto-configuration component. These are loaded automatically and can conditionally register beans.
62
+
63
+ **Example:**
64
+ ```typescript
65
+ @AutoConfiguration()
66
+ export class DatabaseAutoConfiguration {}
67
+ ```
68
+
69
+ ### `@Injectable(scope?)`
70
+
71
+ Class decorator that marks a class as injectable with a specific scope.
72
+
73
+ **Parameters:**
74
+ - `scope`: "singleton" | "request" | "transient" (default: "singleton")
75
+
76
+ **Example:**
77
+ ```typescript
78
+ @Injectable('transient')
79
+ export class RequestLogger {}
80
+ ```
81
+
82
+ ## Property Decorators
83
+
84
+ ### `@Autowired()`
85
+
86
+ Property decorator that injects a dependency based on its type.
87
+
88
+ **Example:**
89
+ ```typescript
90
+ class MyService {
91
+ @Autowired()
92
+ private userService!: UserService;
93
+ }
94
+ ```
95
+
96
+ ### `@Inject(token)`
97
+
98
+ Property decorator that injects a dependency by token (string or class).
99
+
100
+ **Parameters:**
101
+ - `token`: string | Function - Token to resolve the dependency
102
+
103
+ **Example:**
104
+ ```typescript
105
+ class MyService {
106
+ @Inject('Logger')
107
+ private logger!: Logger;
108
+
109
+ @Inject(CacheService)
110
+ private cacheService!: CacheService;
111
+ }
112
+ ```
113
+
114
+ ### `@InjectRepository(entity)`
115
+
116
+ Property decorator that injects a TypeORM repository for the specified entity.
117
+
118
+ **Parameters:**
119
+ - `entity`: Function - Entity class for which to get the repository
120
+
121
+ **Example:**
122
+ ```typescript
123
+ @Repository()
124
+ class UserRepository {
125
+ @InjectRepository(User)
126
+ private repo!: Repository<User>;
127
+ }
128
+ ```
129
+
130
+ ### `@Value(expression)`
131
+
132
+ Property decorator that injects configuration values from environment variables or config.
133
+
134
+ **Parameters:**
135
+ - `expression`: string - Configuration expression (e.g., "${PORT:3000}")
136
+
137
+ **Example:**
138
+ ```typescript
139
+ @Service()
140
+ class ConfigService {
141
+ @Value('${PORT:3000}')
142
+ private port!: number;
143
+
144
+ @Value('${DB_HOST:localhost}')
145
+ private dbHost!: string;
146
+ }
147
+ ```
148
+
149
+ ### `@Qualifier(name)`
150
+
151
+ Property decorator that specifies which bean to inject when multiple implementations exist.
152
+
153
+ **Parameters:**
154
+ - `name`: string - Qualifier name to match
155
+
156
+ **Example:**
157
+ ```typescript
158
+ class PaymentService {
159
+ @Qualifier('creditCard')
160
+ @Autowired()
161
+ private paymentProcessor!: PaymentProcessor;
162
+ }
163
+ ```
164
+
165
+ ### `@Optional()`
166
+
167
+ Property decorator that marks a dependency as optional (won't throw if not found).
168
+
169
+ **Example:**
170
+ ```typescript
171
+ class FeatureService {
172
+ @Autowired()
173
+ @Optional()
174
+ private experimentalService?: ExperimentalService;
175
+ }
176
+ ```
177
+
178
+ ### `@Lazy()`
179
+
180
+ Property decorator that lazily loads a dependency on first access.
181
+
182
+ **Example:**
183
+ ```typescript
184
+ class HeavyService {
185
+ @Lazy()
186
+ @Autowired()
187
+ private resourceIntensiveService!: ResourceIntensiveService;
188
+ }
189
+ ```
190
+
191
+ ## Method Decorators
192
+
193
+ ### `@PostConstruct()`
194
+
195
+ Method decorator that marks a method to be called after dependency injection is complete.
196
+
197
+ **Example:**
198
+ ```typescript
199
+ @Service()
200
+ class DatabaseService {
201
+ @PostConstruct()
202
+ init() {
203
+ // Initialize connections
204
+ }
205
+ }
206
+ ```
207
+
208
+ ### `@PreDestroy()`
209
+
210
+ Method decorator that marks a method to be called before the bean is destroyed.
211
+
212
+ **Example:**
213
+ ```typescript
214
+ @Service()
215
+ class DatabaseService {
216
+ @PreDestroy()
217
+ cleanup() {
218
+ // Close connections
219
+ }
220
+ }
221
+ ```
222
+
223
+ ## HTTP Route Decorators
224
+
225
+ ### `@Get(path?)`
226
+
227
+ Method decorator for GET HTTP endpoints.
228
+
229
+ **Parameters:**
230
+ - `path`: string (optional) - Route path
231
+
232
+ **Example:**
233
+ ```typescript
234
+ @Controller('/users')
235
+ class UserController {
236
+ @Get()
237
+ findAll() {}
238
+
239
+ @Get('/:id')
240
+ findById(@Param('id') id: string) {}
241
+ }
242
+ ```
243
+
244
+ ### `@Post(path?)`
245
+
246
+ Method decorator for POST HTTP endpoints.
247
+
248
+ **Parameters:**
249
+ - `path`: string (optional) - Route path
250
+
251
+ **Example:**
252
+ ```typescript
253
+ @Controller('/users')
254
+ class UserController {
255
+ @Post()
256
+ create(@Body() createUserDto: CreateUserDto) {}
257
+ }
258
+ ```
259
+
260
+ ### `@Put(path?)`
261
+
262
+ Method decorator for PUT HTTP endpoints.
263
+
264
+ **Parameters:**
265
+ - `path`: string (optional) - Route path
266
+
267
+ **Example:**
268
+ ```typescript
269
+ @Controller('/users')
270
+ class UserController {
271
+ @Put('/:id')
272
+ update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {}
273
+ }
274
+ ```
275
+
276
+ ### `@Delete(path?)`
277
+
278
+ Method decorator for DELETE HTTP endpoints.
279
+
280
+ **Parameters:**
281
+ - `path`: string (optional) - Route path
282
+
283
+ **Example:**
284
+ ```typescript
285
+ @Controller('/users')
286
+ class UserController {
287
+ @Delete('/:id')
288
+ delete(@Param('id') id: string) {}
289
+ }
290
+ ```
291
+
292
+ ### `@Patch(path?)`
293
+
294
+ Method decorator for PATCH HTTP endpoints.
295
+
296
+ **Parameters:**
297
+ - `path`: string (optional) - Route path
298
+
299
+ **Example:**
300
+ ```typescript
301
+ @Controller('/users')
302
+ class UserController {
303
+ @Patch('/:id')
304
+ partialUpdate(@Param('id') id: string, @Body() partialUser: Partial<User>) {}
305
+ }
306
+ ```
307
+
308
+ ## Parameter Decorators
309
+
310
+ ### `@Body()`
311
+
312
+ Parameter decorator to inject the request body.
313
+
314
+ **Example:**
315
+ ```typescript
316
+ @Post()
317
+ create(@Body() createUserDto: CreateUserDto) {}
318
+ ```
319
+
320
+ ### `@Param(name?)`
321
+
322
+ Parameter decorator to inject route parameters.
323
+
324
+ **Parameters:**
325
+ - `name`: string (optional) - Specific parameter name to extract
326
+
327
+ **Example:**
328
+ ```typescript
329
+ @Get('/:id')
330
+ findById(@Param('id') id: string) {}
331
+
332
+ @Get('/:userId/posts/:postId')
333
+ findPost(@Param() params: { userId: string, postId: string }) {}
334
+ ```
335
+
336
+ ### `@Query(name?)`
337
+
338
+ Parameter decorator to inject query parameters.
339
+
340
+ **Parameters:**
341
+ - `name`: string (optional) - Specific query parameter name to extract
342
+
343
+ **Example:**
344
+ ```typescript
345
+ @Get()
346
+ search(@Query('q') query: string, @Query('page') page: number) {}
347
+
348
+ @Get('/filter')
349
+ filter(@Query() queryParams: { category?: string, minPrice?: number }) {}
350
+ ```
351
+
352
+ ### `@Header(name?)`
353
+
354
+ Parameter decorator to inject request headers.
355
+
356
+ **Parameters:**
357
+ - `name`: string (optional) - Specific header name to extract
358
+
359
+ **Example:**
360
+ ```typescript
361
+ @Get()
362
+ getWithHeaders(@Header('authorization') authHeader: string) {}
363
+
364
+ @Get('/info')
365
+ getInfo(@Header() headers: Record<string, string>) {}
366
+ ```
367
+
368
+ ### `@Req()`
369
+
370
+ Parameter decorator to inject the Express Request object.
371
+
372
+ **Example:**
373
+ ```typescript
374
+ @Get()
375
+ handleRequest(@Req() req: Request) {
376
+ return { ip: req.ip, userAgent: req.get('user-agent') };
377
+ }
378
+ ```
379
+
380
+ ### `@Res()`
381
+
382
+ Parameter decorator to inject the Express Response object.
383
+
384
+ **Example:**
385
+ ```typescript
386
+ @Get('/file')
387
+ getFile(@Res() res: Response) {
388
+ res.download('/path/to/file.pdf');
389
+ }
390
+ ```
391
+
392
+ ## Conditional Decorators
393
+
394
+ ### `@ConditionalOnClass(classRef)`
395
+
396
+ Class decorator that registers the component only if the specified class is available.
397
+
398
+ **Parameters:**
399
+ - `classRef`: any - Reference to the class to check for
400
+
401
+ **Example:**
402
+ ```typescript
403
+ @AutoConfiguration()
404
+ @ConditionalOnClass(Redis)
405
+ class RedisAutoConfiguration {}
406
+ ```
407
+
408
+ ### `@ConditionalOnMissingBean(token)`
409
+
410
+ Class decorator that registers the component only if no bean of the specified type is already registered.
411
+
412
+ **Parameters:**
413
+ - `token`: any - Token or class to check for
414
+
415
+ **Example:**
416
+ ```typescript
417
+ @Service()
418
+ @ConditionalOnMissingBean(LoggerService)
419
+ class DefaultLoggerService implements LoggerService {}
420
+ ```
421
+
422
+ ### `@ConditionalOnProperty(key, expectedValue?)`
423
+
424
+ Class decorator that registers the component only if the specified environment property exists (and optionally matches a value).
425
+
426
+ **Parameters:**
427
+ - `key`: string - Environment variable key to check
428
+ - `expectedValue`: any (optional) - Expected value of the environment variable
429
+
430
+ **Example:**
431
+ ```typescript
432
+ @Service()
433
+ @ConditionalOnProperty('USE_CACHE', 'true')
434
+ class CacheService {}
435
+ ```
436
+
437
+ ## Container API
438
+
439
+ ### `DIContainer.getInstance()`
440
+
441
+ Returns the singleton instance of the dependency injection container.
442
+
443
+ **Returns:** DIContainer
444
+
445
+ **Example:**
446
+ ```typescript
447
+ const container = DIContainer.getInstance();
448
+ ```
449
+
450
+ ### `container.register(token, instance?, factory?)`
451
+
452
+ Registers a token with an instance or factory function.
453
+
454
+ **Parameters:**
455
+ - `token`: any - Token to register
456
+ - `instance`: any (optional) - Instance to register
457
+ - `factory`: () => any (optional) - Factory function to create instances
458
+
459
+ **Example:**
460
+ ```typescript
461
+ container.register(LoggerService, new ConsoleLogger());
462
+ container.register(DatabaseService, null, () => new DatabaseService());
463
+ ```
464
+
465
+ ### `container.resolve<T>(token)`
466
+
467
+ Resolves and returns an instance for the specified token.
468
+
469
+ **Parameters:**
470
+ - `token`: any - Token to resolve
471
+
472
+ **Returns:** T - Resolved instance
473
+
474
+ **Example:**
475
+ ```typescript
476
+ const userService = container.resolve(UserService);
477
+ ```
478
+
479
+ ### `container.has(token)`
480
+
481
+ Checks if a token has been registered with the container.
482
+
483
+ **Parameters:**
484
+ - `token`: any - Token to check
485
+
486
+ **Returns:** boolean
487
+
488
+ **Example:**
489
+ ```typescript
490
+ if (container.has(UserService)) {
491
+ // Service is registered
492
+ }
493
+ ```
494
+
495
+ ### `container.getAllInstances()`
496
+
497
+ Returns all singleton instances maintained by the container.
498
+
499
+ **Returns:** any[]
500
+
501
+ **Example:**
502
+ ```typescript
503
+ const allServices = container.getAllInstances();
504
+ ```
505
+
506
+ ### `container.clear()`
507
+
508
+ Clears all registered tokens and instances from the container.
509
+
510
+ **Example:**
511
+ ```typescript
512
+ container.clear();
513
+ ```
514
+
515
+ ### `container.reset()`
516
+
517
+ Resets the container to its initial state.
518
+
519
+ **Example:**
520
+ ```typescript
521
+ container.reset();
522
+ ```
523
+
524
+ ## Application API
525
+
526
+ ### `new FragmentWebApplication()`
527
+
528
+ Creates a new instance of the web application.
529
+
530
+ **Example:**
531
+ ```typescript
532
+ const app = new FragmentWebApplication();
533
+ ```
534
+
535
+ ### `app.bootstrap(appClass)`
536
+
537
+ Bootstraps the application with the specified application class.
538
+
539
+ **Parameters:**
540
+ - `appClass`: any - Application class decorated with `@FragmentApplication`
541
+
542
+ **Returns:** Promise<void>
543
+
544
+ **Example:**
545
+ ```typescript
546
+ await app.bootstrap(Application);
547
+ ```
548
+
549
+ ### `app.getExpressApp()`
550
+
551
+ Returns the underlying Express application instance.
552
+
553
+ **Returns:** Express
554
+
555
+ **Example:**
556
+ ```typescript
557
+ const expressApp = app.getExpressApp();
558
+ expressApp.use('/custom', customMiddleware);
559
+ ```
560
+
561
+ ## Metadata Storage API
562
+
563
+ ### `MetadataStorage.getInstance()`
564
+
565
+ Returns the singleton instance of the metadata storage.
566
+
567
+ **Returns:** MetadataStorage
568
+
569
+ **Example:**
570
+ ```typescript
571
+ const storage = MetadataStorage.getInstance();
572
+ ```
573
+
574
+ ### `storage.addClass(metadata)`
575
+
576
+ Adds class metadata to storage.
577
+
578
+ **Parameters:**
579
+ - `metadata`: ClassMetadata - Class metadata object
580
+
581
+ **Example:**
582
+ ```typescript
583
+ storage.addClass({
584
+ target: UserService,
585
+ type: 'service',
586
+ scope: 'singleton'
587
+ });
588
+ ```
589
+
590
+ ### `storage.getClass(target)`
591
+
592
+ Retrieves class metadata for the specified target.
593
+
594
+ **Parameters:**
595
+ - `target`: any - Target class
596
+
597
+ **Returns:** ClassMetadata | undefined
598
+
599
+ **Example:**
600
+ ```typescript
601
+ const metadata = storage.getClass(UserService);
602
+ ```
603
+
604
+ ### `storage.getAllClasses()`
605
+
606
+ Returns all registered class metadata.
607
+
608
+ **Returns:** ClassMetadata[]
609
+
610
+ **Example:**
611
+ ```typescript
612
+ const allClasses = storage.getAllClasses();
613
+ ```
614
+
615
+ ### `storage.addMethod(metadata)`
616
+
617
+ Adds method metadata to storage.
618
+
619
+ **Parameters:**
620
+ - `metadata`: MethodMetadata - Method metadata object
621
+
622
+ **Example:**
623
+ ```typescript
624
+ storage.addMethod({
625
+ target: UserController,
626
+ propertyKey: 'findAll',
627
+ method: 'GET',
628
+ path: '/',
629
+ paramMeta []
630
+ });
631
+ ```
632
+
633
+ ### `storage.getMethod(target, propertyKey)`
634
+
635
+ Retrieves method metadata for the specified target and property key.
636
+
637
+ **Parameters:**
638
+ - `target`: any - Target class
639
+ - `propertyKey`: string - Method name
640
+
641
+ **Returns:** MethodMetadata | undefined
642
+
643
+ **Example:**
644
+ ```typescript
645
+ const metadata = storage.getMethod(UserController, 'findAll');
646
+ ```
647
+
648
+ ### `storage.getAllMethods()`
649
+
650
+ Returns all registered method metadata.
651
+
652
+ **Returns:** MethodMetadata[]
653
+
654
+ **Example:**
655
+ ```typescript
656
+ const allMethods = storage.getAllMethods();
657
+ ```
658
+
659
+ ### `storage.addParam(metadata)`
660
+
661
+ Adds parameter metadata to storage.
662
+
663
+ **Parameters:**
664
+ - `metadata`: ParamMetadata - Parameter metadata object
665
+
666
+ **Example:**
667
+ ```typescript
668
+ storage.addParam({
669
+ target: UserController.prototype,
670
+ propertyKey: 'findById',
671
+ index: 0,
672
+ type: 'param',
673
+ paramName: 'id'
674
+ });
675
+ ```
676
+
677
+ ### `storage.getParams(target, propertyKey)`
678
+
679
+ Retrieves parameter metadata for the specified target and property key.
680
+
681
+ **Parameters:**
682
+ - `target`: any - Target class
683
+ - `propertyKey`: string - Method name
684
+
685
+ **Returns:** ParamMetadata[]
686
+
687
+ **Example:**
688
+ ```typescript
689
+ const params = storage.getParams(UserController.prototype, 'findById');
690
+ ```
691
+
692
+ ## Interfaces
693
+
694
+ ### `ApplicationOptions`
695
+
696
+ ```typescript
697
+ interface ApplicationOptions {
698
+ port?: number;
699
+ host?: string;
700
+ configPath?: string;
701
+ autoScan?: boolean;
702
+ }
703
+ ```
704
+
705
+ ### `ClassMetadata`
706
+
707
+ ```typescript
708
+ interface ClassMetadata {
709
+ target: any;
710
+ type: 'injectable' | 'service' | 'controller' | 'repository' | 'auto-configuration';
711
+ scope?: 'singleton' | 'request' | 'transient';
712
+ path?: string;
713
+ }
714
+ ```
715
+
716
+ ### `MethodMetadata`
717
+
718
+ ```typescript
719
+ interface MethodMetadata {
720
+ target: any;
721
+ propertyKey: string;
722
+ method: string;
723
+ path: string;
724
+ paramMeta ParamMetadata[];
725
+ }
726
+ ```
727
+
728
+ ### `ParamMetadata`
729
+
730
+ ```typescript
731
+ interface ParamMetadata {
732
+ target: any;
733
+ propertyKey: string;
734
+ index: number;
735
+ type: 'body' | 'param' | 'query' | 'header' | 'req' | 'res';
736
+ paramName?: string;
737
+ }
738
+ ```
739
+
740
+ ## Type Definitions
741
+
742
+ ### `Scope`
743
+
744
+ ```typescript
745
+ type Scope = 'singleton' | 'request' | 'transient';
746
+ ```
747
+
748
+ ### `MetadataKey`
749
+
750
+ ```typescript
751
+ type MetadataKey = (typeof METADATA_KEYS)[keyof typeof METADATA_KEYS];
752
+ ```