ts-ioc-container 27.4.0 → 27.4.1

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 (2) hide show
  1. package/README.md +43 -96
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -78,16 +78,7 @@ And `tsconfig.json` should have next options:
78
78
 
79
79
  ```typescript
80
80
  import 'reflect-metadata';
81
- import {
82
- IContainer,
83
- by,
84
- Container,
85
- inject,
86
- ReflectionInjector,
87
- RegistrationConflictError,
88
- Registration,
89
- key,
90
- } from 'ts-ioc-container';
81
+ import { IContainer, by, Container, inject, ReflectionInjector, Registration as R } from 'ts-ioc-container';
91
82
 
92
83
  describe('Basic usage', function () {
93
84
  class Logger {
@@ -99,7 +90,7 @@ describe('Basic usage', function () {
99
90
  constructor(@inject(by.key('ILogger')) public logger: Logger) {}
100
91
  }
101
92
 
102
- const container = new Container(new ReflectionInjector()).use(Registration.fromClass(Logger).to('ILogger'));
93
+ const container = new Container(new ReflectionInjector()).use(R.fromClass(Logger).to('ILogger'));
103
94
 
104
95
  expect(container.resolve(App).logger.name).toBe('Logger');
105
96
  });
@@ -110,8 +101,8 @@ describe('Basic usage', function () {
110
101
  }
111
102
 
112
103
  const container = new Container(new ReflectionInjector())
113
- .use(Registration.fromClass(Logger).to('ILogger1'))
114
- .use(Registration.fromClass(Logger).to('ILogger2'));
104
+ .use(R.fromClass(Logger).to('ILogger1'))
105
+ .use(R.fromClass(Logger).to('ILogger2'));
115
106
 
116
107
  expect(container.resolve(App).loggers).toHaveLength(2);
117
108
  });
@@ -127,46 +118,6 @@ describe('Basic usage', function () {
127
118
 
128
119
  expect(app.scope).toBe(root);
129
120
  });
130
-
131
- it('should not raise an error when key is busy', () => {
132
- expect(() => {
133
- new Container(new ReflectionInjector()).use(Registration.fromClass(Logger)).use(Registration.fromClass(Logger));
134
- }).not.toThrowError(RegistrationConflictError);
135
- });
136
-
137
- it('registration -> should raise an error when key is busy', () => {
138
- expect(() => {
139
- new Container(new ReflectionInjector())
140
- .use(Registration.fromClass(Logger))
141
- .use(Registration.fromClass(Logger).throwErrorOnConflict());
142
- }).toThrowError(RegistrationConflictError);
143
- });
144
-
145
- it('registration -> should not raise an error when key is busy', () => {
146
- expect(() => {
147
- new Container(new ReflectionInjector()).use(Registration.fromClass(Logger)).use(Registration.fromClass(Logger));
148
- }).not.toThrowError(RegistrationConflictError);
149
- });
150
-
151
- it('@key -> should raise an error when key is busy', () => {
152
- @key('Logger')
153
- class Logger1 {}
154
-
155
- @key('Logger')
156
- class Logger2 {}
157
-
158
- expect(() => {
159
- new Container(new ReflectionInjector())
160
- .use(Registration.fromClass(Logger1))
161
- .use(Registration.fromClass(Logger2).throwErrorOnConflict());
162
- }).toThrowError(RegistrationConflictError);
163
- });
164
-
165
- it('registration -> should not raise an error when key is busy', () => {
166
- expect(() => {
167
- new Container(new ReflectionInjector()).use(Registration.fromClass(Logger)).use(Registration.fromClass(Logger));
168
- }).not.toThrowError(RegistrationConflictError);
169
- });
170
121
  });
171
122
 
172
123
  ```
@@ -190,7 +141,7 @@ import {
190
141
  tags,
191
142
  provider,
192
143
  ReflectionInjector,
193
- Registration,
144
+ Registration as R,
194
145
  by,
195
146
  } from 'ts-ioc-container';
196
147
 
@@ -200,7 +151,7 @@ class Logger {}
200
151
 
201
152
  describe('Scopes', function () {
202
153
  it('should resolve dependencies from scope', function () {
203
- const root = new Container(new ReflectionInjector(), { tags: ['root'] }).use(Registration.fromClass(Logger));
154
+ const root = new Container(new ReflectionInjector(), { tags: ['root'] }).use(R.fromClass(Logger));
204
155
  const child = root.createScope('child');
205
156
 
206
157
  expect(child.resolve('ILogger')).toBe(child.resolve('ILogger'));
@@ -230,14 +181,14 @@ Sometimes you want to get all instances from container and its scopes. For examp
230
181
 
231
182
  ```typescript
232
183
  import 'reflect-metadata';
233
- import { inject, key, Registration, Container, ReflectionInjector, by } from 'ts-ioc-container';
184
+ import { inject, key, Registration as R, Container, ReflectionInjector, by } from 'ts-ioc-container';
234
185
 
235
186
  describe('Instances', function () {
236
187
  @key('ILogger')
237
188
  class Logger {}
238
189
 
239
190
  it('should return injected instances', () => {
240
- const container = new Container(new ReflectionInjector()).use(Registration.fromClass(Logger));
191
+ const container = new Container(new ReflectionInjector()).use(R.fromClass(Logger));
241
192
  const scope = container.createScope();
242
193
 
243
194
  const logger1 = container.resolve('ILogger');
@@ -254,7 +205,7 @@ describe('Instances', function () {
254
205
  constructor(@inject(by.instances(isLogger)) public loggers: Logger[]) {}
255
206
  }
256
207
 
257
- const container = new Container(new ReflectionInjector()).use(Registration.fromClass(Logger));
208
+ const container = new Container(new ReflectionInjector()).use(R.fromClass(Logger));
258
209
 
259
210
  const logger0 = container.resolve('ILogger');
260
211
  const logger1 = container.resolve('ILogger');
@@ -277,15 +228,13 @@ Sometimes you want to dispose container and all its scopes. For example, when yo
277
228
 
278
229
  ```typescript
279
230
  import 'reflect-metadata';
280
- import { Container, ContainerDisposedError, ReflectionInjector, Registration } from 'ts-ioc-container';
231
+ import { Container, ContainerDisposedError, ReflectionInjector, Registration as R } from 'ts-ioc-container';
281
232
 
282
233
  class Logger {}
283
234
 
284
235
  describe('Disposing', function () {
285
236
  it('should container and make it unavailable for the further usage', function () {
286
- const root = new Container(new ReflectionInjector(), { tags: ['root'] }).use(
287
- Registration.fromClass(Logger).to('ILogger'),
288
- );
237
+ const root = new Container(new ReflectionInjector(), { tags: ['root'] }).use(R.fromClass(Logger).to('ILogger'));
289
238
  const child = root.createScope('child');
290
239
 
291
240
  const logger = child.resolve('ILogger');
@@ -311,7 +260,7 @@ This type of injector uses `@inject` decorator to mark where dependencies should
311
260
 
312
261
  ```typescript
313
262
  import 'reflect-metadata';
314
- import { by, Container, inject, ReflectionInjector, Registration } from 'ts-ioc-container';
263
+ import { by, Container, inject, ReflectionInjector, Registration as R } from 'ts-ioc-container';
315
264
 
316
265
  class Logger {
317
266
  name = 'Logger';
@@ -331,7 +280,7 @@ class App {
331
280
 
332
281
  describe('Reflection Injector', function () {
333
282
  it('should inject dependencies by @inject decorator', function () {
334
- const container = new Container(new ReflectionInjector()).use(Registration.fromClass(Logger).to('ILogger'));
283
+ const container = new Container(new ReflectionInjector()).use(R.fromClass(Logger).to('ILogger'));
335
284
 
336
285
  const app = container.resolve(App);
337
286
 
@@ -346,7 +295,7 @@ This type of injector just passes container to constructor with others arguments
346
295
 
347
296
  ```typescript
348
297
  import 'reflect-metadata';
349
- import { Container, IContainer, Registration, SimpleInjector } from 'ts-ioc-container';
298
+ import { Container, IContainer, Registration as R, SimpleInjector } from 'ts-ioc-container';
350
299
 
351
300
  describe('SimpleInjector', function () {
352
301
  it('should pass container as first parameter', function () {
@@ -354,7 +303,7 @@ describe('SimpleInjector', function () {
354
303
  constructor(public container: IContainer) {}
355
304
  }
356
305
 
357
- const container = new Container(new SimpleInjector()).use(Registration.fromClass(App).to('App'));
306
+ const container = new Container(new SimpleInjector()).use(R.fromClass(App).to('App'));
358
307
  const app = container.resolve<App>('App');
359
308
 
360
309
  expect(app.container).toBeInstanceOf(Container);
@@ -365,7 +314,7 @@ describe('SimpleInjector', function () {
365
314
  constructor(container: IContainer, public greeting: string) {}
366
315
  }
367
316
 
368
- const container = new Container(new SimpleInjector()).use(Registration.fromClass(App).to('App'));
317
+ const container = new Container(new SimpleInjector()).use(R.fromClass(App).to('App'));
369
318
  const app = container.resolve<App>('App', 'Hello world');
370
319
 
371
320
  expect(app.greeting).toBe('Hello world');
@@ -379,7 +328,7 @@ This type of injector injects dependencies as dictionary `Record<string, unknown
379
328
 
380
329
  ```typescript
381
330
  import 'reflect-metadata';
382
- import { Container, ProxyInjector, args, Registration } from 'ts-ioc-container';
331
+ import { Container, ProxyInjector, args, Registration as R } from 'ts-ioc-container';
383
332
 
384
333
  describe('ProxyInjector', function () {
385
334
  it('should pass dependency to constructor as dictionary', function () {
@@ -393,7 +342,7 @@ describe('ProxyInjector', function () {
393
342
  }
394
343
  }
395
344
 
396
- const container = new Container(new ProxyInjector()).use(Registration.fromClass(Logger).to('logger'));
345
+ const container = new Container(new ProxyInjector()).use(R.fromClass(Logger).to('logger'));
397
346
 
398
347
  const app = container.resolve(App);
399
348
  expect(app.logger).toBeInstanceOf(Logger);
@@ -423,8 +372,8 @@ describe('ProxyInjector', function () {
423
372
  const greetingTemplate = (name: string) => `Hello ${name}`;
424
373
 
425
374
  const container = new Container(new ProxyInjector())
426
- .use(Registration.fromClass(App).to('App').pipe(args({ greetingTemplate })))
427
- .use(Registration.fromClass(Logger).to('logger'));
375
+ .use(R.fromClass(App).to('App').pipe(args({ greetingTemplate })))
376
+ .use(R.fromClass(Logger).to('logger'));
428
377
 
429
378
  const app = container.resolve<App>('App', { name: `world` });
430
379
  expect(app.greeting).toBe('Hello world');
@@ -492,7 +441,7 @@ Sometimes you need to create only one instance of dependency per scope. For exam
492
441
 
493
442
  ```typescript
494
443
  import 'reflect-metadata';
495
- import { singleton, Container, key, provider, ReflectionInjector, Registration } from 'ts-ioc-container';
444
+ import { singleton, Container, key, provider, ReflectionInjector, Registration as R } from 'ts-ioc-container';
496
445
 
497
446
  @key('logger')
498
447
  @provider(singleton())
@@ -504,20 +453,20 @@ describe('Singleton', function () {
504
453
  }
505
454
 
506
455
  it('should resolve the same container per every request', function () {
507
- const container = createContainer().use(Registration.fromClass(Logger));
456
+ const container = createContainer().use(R.fromClass(Logger));
508
457
 
509
458
  expect(container.resolve('logger')).toBe(container.resolve('logger'));
510
459
  });
511
460
 
512
461
  it('should resolve different dependency per scope', function () {
513
- const container = createContainer().use(Registration.fromClass(Logger));
462
+ const container = createContainer().use(R.fromClass(Logger));
514
463
  const child = container.createScope();
515
464
 
516
465
  expect(container.resolve('logger')).not.toBe(child.resolve('logger'));
517
466
  });
518
467
 
519
468
  it('should resolve the same dependency for scope', function () {
520
- const container = createContainer().use(Registration.fromClass(Logger));
469
+ const container = createContainer().use(R.fromClass(Logger));
521
470
  const child = container.createScope();
522
471
 
523
472
  expect(child.resolve('logger')).toBe(child.resolve('logger'));
@@ -532,14 +481,14 @@ Sometimes you need to resolve provider only from container with certain tags and
532
481
 
533
482
  ```typescript
534
483
  import 'reflect-metadata';
535
- import { singleton, Container, key, tags, provider, ReflectionInjector, Registration } from 'ts-ioc-container';
484
+ import { singleton, Container, key, tags, provider, ReflectionInjector, Registration as R } from 'ts-ioc-container';
536
485
 
537
486
  @key('ILogger')
538
487
  @provider(singleton(), tags('root')) // the same as .pipe(singleton(), tags('root'))
539
488
  class Logger {}
540
489
  describe('TaggedProvider', function () {
541
490
  it('should return the same instance', function () {
542
- const root = new Container(new ReflectionInjector(), { tags: ['root'] }).use(Registration.fromClass(Logger));
491
+ const root = new Container(new ReflectionInjector(), { tags: ['root'] }).use(R.fromClass(Logger));
543
492
  const child = root.createScope();
544
493
  expect(root.resolve('ILogger')).toBe(child.resolve('ILogger'));
545
494
  });
@@ -553,7 +502,7 @@ Sometimes you want to bind some arguments to provider. This is what `ArgsProvide
553
502
 
554
503
  ```typescript
555
504
  import 'reflect-metadata';
556
- import { Container, key, argsFn, args, ReflectionInjector, Registration } from 'ts-ioc-container';
505
+ import { Container, key, argsFn, args, ReflectionInjector, Registration as R } from 'ts-ioc-container';
557
506
 
558
507
  @key('logger')
559
508
  class Logger {
@@ -566,21 +515,21 @@ describe('ArgsProvider', function () {
566
515
  }
567
516
 
568
517
  it('can assign argument function to provider', function () {
569
- const root = createContainer().use(Registration.fromClass(Logger).pipe(argsFn((container, ...args) => ['name'])));
518
+ const root = createContainer().use(R.fromClass(Logger).pipe(argsFn((container, ...args) => ['name'])));
570
519
 
571
520
  const logger = root.createScope().resolve<Logger>('logger');
572
521
  expect(logger.name).toBe('name');
573
522
  });
574
523
 
575
524
  it('can assign argument to provider', function () {
576
- const root = createContainer().use(Registration.fromClass(Logger).pipe(args('name')));
525
+ const root = createContainer().use(R.fromClass(Logger).pipe(args('name')));
577
526
 
578
527
  const logger = root.resolve<Logger>('logger');
579
528
  expect(logger.name).toBe('name');
580
529
  });
581
530
 
582
531
  it('should set provider arguments with highest priority in compare to resolve arguments', function () {
583
- const root = createContainer().use(Registration.fromClass(Logger).pipe(args('name')));
532
+ const root = createContainer().use(R.fromClass(Logger).pipe(args('name')));
584
533
 
585
534
  const logger = root.resolve<Logger>('logger', 'file');
586
535
 
@@ -599,7 +548,7 @@ Sometimes you want to register the same provider with different keys. This is wh
599
548
 
600
549
  ```typescript
601
550
  import 'reflect-metadata';
602
- import { alias, by, Container, inject, provider, ReflectionInjector, Registration } from 'ts-ioc-container';
551
+ import { alias, by, Container, inject, provider, ReflectionInjector, Registration as R } from 'ts-ioc-container';
603
552
 
604
553
  describe('alias', () => {
605
554
  const IMiddlewareKey = Symbol('IMiddleware');
@@ -653,8 +602,8 @@ describe('alias', () => {
653
602
  }
654
603
 
655
604
  const container = new Container(new ReflectionInjector())
656
- .use(Registration.fromClass(LoggerMiddleware))
657
- .use(Registration.fromClass(ErrorHandlerMiddleware));
605
+ .use(R.fromClass(LoggerMiddleware))
606
+ .use(R.fromClass(ErrorHandlerMiddleware));
658
607
 
659
608
  const app = container.resolve(App);
660
609
  app.run();
@@ -673,7 +622,7 @@ Sometimes you want to encapsulate registration logic in separate module. This is
673
622
 
674
623
  ```typescript
675
624
  import 'reflect-metadata';
676
- import { IContainerModule, Registration, IContainer, key, Container, ReflectionInjector } from 'ts-ioc-container';
625
+ import { IContainerModule, Registration as R, IContainer, key, Container, ReflectionInjector } from 'ts-ioc-container';
677
626
 
678
627
  @key('ILogger')
679
628
  class Logger {}
@@ -683,13 +632,13 @@ class TestLogger {}
683
632
 
684
633
  class Production implements IContainerModule {
685
634
  applyTo(container: IContainer): void {
686
- container.use(Registration.fromClass(Logger));
635
+ container.use(R.fromClass(Logger));
687
636
  }
688
637
  }
689
638
 
690
639
  class Development implements IContainerModule {
691
640
  applyTo(container: IContainer): void {
692
- container.use(Registration.fromClass(TestLogger));
641
+ container.use(R.fromClass(TestLogger));
693
642
  }
694
643
  }
695
644
 
@@ -718,7 +667,7 @@ Sometimes you need to keep dependency key with class together. For example, you
718
667
 
719
668
  ```typescript
720
669
  import 'reflect-metadata';
721
- import { singleton, Container, tags, provider, ReflectionInjector, Registration, key } from 'ts-ioc-container';
670
+ import { singleton, Container, tags, provider, ReflectionInjector, Registration as R, key } from 'ts-ioc-container';
722
671
 
723
672
  describe('Registration module', function () {
724
673
  const createContainer = () => new Container(new ReflectionInjector(), { tags: ['root'] });
@@ -728,7 +677,7 @@ describe('Registration module', function () {
728
677
  @provider(singleton(), tags('root'))
729
678
  class Logger {}
730
679
 
731
- const root = createContainer().use(Registration.fromClass(Logger));
680
+ const root = createContainer().use(R.fromClass(Logger));
732
681
 
733
682
  expect(root.resolve('ILogger')).toBeInstanceOf(Logger);
734
683
  });
@@ -736,7 +685,7 @@ describe('Registration module', function () {
736
685
  it('should register dependency by class name if @key is not provided', function () {
737
686
  class FileLogger {}
738
687
 
739
- const root = createContainer().use(Registration.fromClass(FileLogger));
688
+ const root = createContainer().use(R.fromClass(FileLogger));
740
689
 
741
690
  expect(root.resolve('FileLogger')).toBeInstanceOf(FileLogger);
742
691
  });
@@ -759,7 +708,7 @@ import {
759
708
  IContainer,
760
709
  IInjector,
761
710
  ReflectionInjector,
762
- Registration,
711
+ Registration as R,
763
712
  } from 'ts-ioc-container';
764
713
 
765
714
  class MyInjector implements IInjector {
@@ -792,7 +741,7 @@ class Logger {
792
741
 
793
742
  describe('onConstruct', function () {
794
743
  it('should make logger be ready on resolve', function () {
795
- const container = new Container(new MyInjector()).use(Registration.fromClass(Logger));
744
+ const container = new Container(new MyInjector()).use(R.fromClass(Logger));
796
745
 
797
746
  const logger = container.resolve<Logger>('logger');
798
747
 
@@ -814,7 +763,7 @@ import {
814
763
  hook,
815
764
  inject,
816
765
  provider,
817
- Registration,
766
+ Registration as R,
818
767
  ReflectionInjector,
819
768
  } from 'ts-ioc-container';
820
769
 
@@ -847,9 +796,7 @@ class Logger {
847
796
 
848
797
  describe('onDispose', function () {
849
798
  it('should invoke hooks on all instances', async function () {
850
- const container = new Container(new ReflectionInjector())
851
- .use(Registration.fromClass(Logger))
852
- .use(Registration.fromClass(LogsRepo));
799
+ const container = new Container(new ReflectionInjector()).use(R.fromClass(Logger)).use(R.fromClass(LogsRepo));
853
800
 
854
801
  const logger = container.resolve<Logger>('logger');
855
802
  logger.log('Hello');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "27.4.0",
3
+ "version": "27.4.1",
4
4
  "description": "Typescript IoC container",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -60,5 +60,5 @@
60
60
  "ts-node": "^10.9.1",
61
61
  "typescript": "4.4.3"
62
62
  },
63
- "gitHead": "917c4879c7452ab78bb70482479e963e451361a3"
63
+ "gitHead": "8e2634055ea996588cdf1a3c8aa76b83c489ab39"
64
64
  }