ts-ioc-container 37.3.0 → 38.0.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/README.md
CHANGED
|
@@ -85,7 +85,7 @@ And `tsconfig.json` should have next options:
|
|
|
85
85
|
|
|
86
86
|
```typescript
|
|
87
87
|
import 'reflect-metadata';
|
|
88
|
-
import { IContainer, by, Container, inject,
|
|
88
|
+
import { IContainer, by, Container, inject, Registration as R } from 'ts-ioc-container';
|
|
89
89
|
|
|
90
90
|
describe('Basic usage', function () {
|
|
91
91
|
class Logger {
|
|
@@ -97,7 +97,7 @@ describe('Basic usage', function () {
|
|
|
97
97
|
constructor(@inject(by.key('ILogger')) public logger: Logger) {}
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
const container = new Container(
|
|
100
|
+
const container = new Container().add(R.toClass(Logger).fromKey('ILogger'));
|
|
101
101
|
|
|
102
102
|
expect(container.resolve(App).logger.name).toBe('Logger');
|
|
103
103
|
});
|
|
@@ -107,7 +107,7 @@ describe('Basic usage', function () {
|
|
|
107
107
|
constructor(@inject(by.keys(['ILogger1', 'ILogger2'])) public loggers: Logger[]) {}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
const container = new Container(
|
|
110
|
+
const container = new Container()
|
|
111
111
|
.add(R.toClass(Logger).fromKey('ILogger1'))
|
|
112
112
|
.add(R.toClass(Logger).fromKey('ILogger2'));
|
|
113
113
|
|
|
@@ -115,7 +115,7 @@ describe('Basic usage', function () {
|
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
it('should inject current scope', function () {
|
|
118
|
-
const root = new Container(
|
|
118
|
+
const root = new Container({ tags: ['root'] });
|
|
119
119
|
|
|
120
120
|
class App {
|
|
121
121
|
constructor(@inject(by.scope.current) public scope: IContainer) {}
|
|
@@ -145,7 +145,6 @@ import {
|
|
|
145
145
|
IContainer,
|
|
146
146
|
inject,
|
|
147
147
|
key,
|
|
148
|
-
MetadataInjector,
|
|
149
148
|
provider,
|
|
150
149
|
register,
|
|
151
150
|
Registration as R,
|
|
@@ -159,7 +158,7 @@ class Logger {}
|
|
|
159
158
|
|
|
160
159
|
describe('Scopes', function () {
|
|
161
160
|
it('should resolve dependencies from scope', function () {
|
|
162
|
-
const root = new Container(
|
|
161
|
+
const root = new Container({ tags: ['root'] }).add(R.toClass(Logger));
|
|
163
162
|
const child = root.createScope({ tags: ['child'] });
|
|
164
163
|
|
|
165
164
|
expect(child.resolve('ILogger')).toBe(child.resolve('ILogger'));
|
|
@@ -167,7 +166,7 @@ describe('Scopes', function () {
|
|
|
167
166
|
});
|
|
168
167
|
|
|
169
168
|
it('should inject new scope', function () {
|
|
170
|
-
const root = new Container(
|
|
169
|
+
const root = new Container({ tags: ['root'] });
|
|
171
170
|
|
|
172
171
|
class App {
|
|
173
172
|
constructor(@inject(by.scope.create({ tags: ['child'] })) public scope: IContainer) {}
|
|
@@ -189,14 +188,14 @@ Sometimes you want to get all instances from container and its scopes. For examp
|
|
|
189
188
|
|
|
190
189
|
```typescript
|
|
191
190
|
import 'reflect-metadata';
|
|
192
|
-
import { inject, key, Registration as R, Container,
|
|
191
|
+
import { inject, key, Registration as R, Container, by, register } from 'ts-ioc-container';
|
|
193
192
|
|
|
194
193
|
describe('Instances', function () {
|
|
195
194
|
@register(key('ILogger'))
|
|
196
195
|
class Logger {}
|
|
197
196
|
|
|
198
197
|
it('should return injected instances', () => {
|
|
199
|
-
const container = new Container(
|
|
198
|
+
const container = new Container().add(R.toClass(Logger));
|
|
200
199
|
const scope = container.createScope();
|
|
201
200
|
|
|
202
201
|
const logger1 = container.resolve('ILogger');
|
|
@@ -213,7 +212,7 @@ describe('Instances', function () {
|
|
|
213
212
|
constructor(@inject(by.instances(isLogger)) public loggers: Logger[]) {}
|
|
214
213
|
}
|
|
215
214
|
|
|
216
|
-
const container = new Container(
|
|
215
|
+
const container = new Container().add(R.toClass(Logger));
|
|
217
216
|
|
|
218
217
|
const logger0 = container.resolve('ILogger');
|
|
219
218
|
const logger1 = container.resolve('ILogger');
|
|
@@ -236,13 +235,13 @@ Sometimes you want to dispose container and all its scopes. For example, when yo
|
|
|
236
235
|
|
|
237
236
|
```typescript
|
|
238
237
|
import 'reflect-metadata';
|
|
239
|
-
import { by, Container, ContainerDisposedError, EmptyContainer,
|
|
238
|
+
import { by, Container, ContainerDisposedError, EmptyContainer, Registration as R } from 'ts-ioc-container';
|
|
240
239
|
|
|
241
240
|
class Logger {}
|
|
242
241
|
|
|
243
242
|
describe('Disposing', function () {
|
|
244
243
|
it('should container and make it unavailable for the further usage', function () {
|
|
245
|
-
const root = new Container(
|
|
244
|
+
const root = new Container({ tags: ['root'] }).add(R.toClass(Logger).fromKey('ILogger'));
|
|
246
245
|
const child = root.createScope({ tags: ['child'] });
|
|
247
246
|
|
|
248
247
|
const logger = child.resolve('ILogger');
|
|
@@ -254,7 +253,7 @@ describe('Disposing', function () {
|
|
|
254
253
|
});
|
|
255
254
|
|
|
256
255
|
it('should dispose without cascade', function () {
|
|
257
|
-
const root = new Container(
|
|
256
|
+
const root = new Container({ tags: ['root'] });
|
|
258
257
|
const child1 = root.createScope({ tags: ['child1'] });
|
|
259
258
|
|
|
260
259
|
root.dispose({ cascade: false });
|
|
@@ -271,7 +270,7 @@ describe('Disposing', function () {
|
|
|
271
270
|
Sometimes you want to create dependency only when somebody want to invoke it's method or property. This is what `lazy` is for.
|
|
272
271
|
|
|
273
272
|
```typescript
|
|
274
|
-
import { by, Container, inject,
|
|
273
|
+
import { by, Container, inject, provider, Registration as R, singleton } from 'ts-ioc-container';
|
|
275
274
|
|
|
276
275
|
describe('lazy provider', () => {
|
|
277
276
|
@provider(singleton())
|
|
@@ -304,7 +303,7 @@ describe('lazy provider', () => {
|
|
|
304
303
|
}
|
|
305
304
|
|
|
306
305
|
function createContainer() {
|
|
307
|
-
const container = new Container(
|
|
306
|
+
const container = new Container();
|
|
308
307
|
container.add(R.toClass(Flag)).add(R.toClass(Service));
|
|
309
308
|
return container;
|
|
310
309
|
}
|
|
@@ -376,7 +375,7 @@ Also you can [inject property.](#inject-property)
|
|
|
376
375
|
|
|
377
376
|
```typescript
|
|
378
377
|
import 'reflect-metadata';
|
|
379
|
-
import { by, Container, inject,
|
|
378
|
+
import { by, Container, inject, Registration as R } from 'ts-ioc-container';
|
|
380
379
|
|
|
381
380
|
class Logger {
|
|
382
381
|
name = 'Logger';
|
|
@@ -396,7 +395,7 @@ class App {
|
|
|
396
395
|
|
|
397
396
|
describe('Reflection Injector', function () {
|
|
398
397
|
it('should inject dependencies by @inject decorator', function () {
|
|
399
|
-
const container = new Container(
|
|
398
|
+
const container = new Container().add(R.toClass(Logger).fromKey('ILogger'));
|
|
400
399
|
|
|
401
400
|
const app = container.resolve(App);
|
|
402
401
|
|
|
@@ -419,7 +418,7 @@ describe('SimpleInjector', function () {
|
|
|
419
418
|
constructor(public container: IContainer) {}
|
|
420
419
|
}
|
|
421
420
|
|
|
422
|
-
const container = new Container(new SimpleInjector()).add(R.toClass(App).fromKey('App'));
|
|
421
|
+
const container = new Container({ injector: new SimpleInjector() }).add(R.toClass(App).fromKey('App'));
|
|
423
422
|
const app = container.resolve<App>('App');
|
|
424
423
|
|
|
425
424
|
expect(app.container).toBeInstanceOf(Container);
|
|
@@ -433,7 +432,7 @@ describe('SimpleInjector', function () {
|
|
|
433
432
|
) {}
|
|
434
433
|
}
|
|
435
434
|
|
|
436
|
-
const container = new Container(new SimpleInjector()).add(R.toClass(App).fromKey('App'));
|
|
435
|
+
const container = new Container({ injector: new SimpleInjector() }).add(R.toClass(App).fromKey('App'));
|
|
437
436
|
const app = container.resolve<App>('App', { args: ['Hello world'] });
|
|
438
437
|
|
|
439
438
|
expect(app.greeting).toBe('Hello world');
|
|
@@ -461,7 +460,7 @@ describe('ProxyInjector', function () {
|
|
|
461
460
|
}
|
|
462
461
|
}
|
|
463
462
|
|
|
464
|
-
const container = new Container(new ProxyInjector()).add(R.toClass(Logger).fromKey('logger'));
|
|
463
|
+
const container = new Container({ injector: new ProxyInjector() }).add(R.toClass(Logger).fromKey('logger'));
|
|
465
464
|
|
|
466
465
|
const app = container.resolve(App);
|
|
467
466
|
expect(app.logger).toBeInstanceOf(Logger);
|
|
@@ -490,7 +489,7 @@ describe('ProxyInjector', function () {
|
|
|
490
489
|
|
|
491
490
|
const greetingTemplate = (name: string) => `Hello ${name}`;
|
|
492
491
|
|
|
493
|
-
const container = new Container(new ProxyInjector())
|
|
492
|
+
const container = new Container({ injector: new ProxyInjector() })
|
|
494
493
|
.add(R.toClass(App).fromKey('App').pipe(args({ greetingTemplate })))
|
|
495
494
|
.add(R.toClass(Logger).fromKey('logger'));
|
|
496
495
|
|
|
@@ -511,34 +510,31 @@ Provider is dependency factory which creates dependency.
|
|
|
511
510
|
|
|
512
511
|
```typescript
|
|
513
512
|
import 'reflect-metadata';
|
|
514
|
-
import { singleton, Container, Provider,
|
|
513
|
+
import { singleton, Container, Provider, scope } from 'ts-ioc-container';
|
|
515
514
|
|
|
516
515
|
class Logger {}
|
|
517
516
|
|
|
518
517
|
describe('Provider', function () {
|
|
519
518
|
it('can be registered as a function', function () {
|
|
520
|
-
const container = new Container(
|
|
519
|
+
const container = new Container().register('ILogger', new Provider(() => new Logger()));
|
|
521
520
|
|
|
522
521
|
expect(container.resolve('ILogger')).not.toBe(container.resolve('ILogger'));
|
|
523
522
|
});
|
|
524
523
|
|
|
525
524
|
it('can be registered as a value', function () {
|
|
526
|
-
const container = new Container(
|
|
525
|
+
const container = new Container().register('ILogger', Provider.fromValue(new Logger()));
|
|
527
526
|
|
|
528
527
|
expect(container.resolve('ILogger')).toBe(container.resolve('ILogger'));
|
|
529
528
|
});
|
|
530
529
|
|
|
531
530
|
it('can be registered as a class', function () {
|
|
532
|
-
const container = new Container(
|
|
531
|
+
const container = new Container().register('ILogger', Provider.fromClass(Logger));
|
|
533
532
|
|
|
534
533
|
expect(container.resolve('ILogger')).not.toBe(container.resolve('ILogger'));
|
|
535
534
|
});
|
|
536
535
|
|
|
537
536
|
it('can be featured by pipe method', function () {
|
|
538
|
-
const root = new Container(
|
|
539
|
-
'ILogger',
|
|
540
|
-
Provider.fromClass(Logger).pipe(singleton()),
|
|
541
|
-
);
|
|
537
|
+
const root = new Container({ tags: ['root'] }).register('ILogger', Provider.fromClass(Logger).pipe(singleton()));
|
|
542
538
|
|
|
543
539
|
expect(root.resolve('ILogger')).toBe(root.resolve('ILogger'));
|
|
544
540
|
});
|
|
@@ -554,7 +550,7 @@ Sometimes you need to create only one instance of dependency per scope. For exam
|
|
|
554
550
|
|
|
555
551
|
```typescript
|
|
556
552
|
import 'reflect-metadata';
|
|
557
|
-
import { singleton, Container, key, provider,
|
|
553
|
+
import { singleton, Container, key, provider, Registration as R, register } from 'ts-ioc-container';
|
|
558
554
|
|
|
559
555
|
@register(key('logger'))
|
|
560
556
|
@provider(singleton())
|
|
@@ -562,7 +558,7 @@ class Logger {}
|
|
|
562
558
|
|
|
563
559
|
describe('Singleton', function () {
|
|
564
560
|
function createContainer() {
|
|
565
|
-
return new Container(
|
|
561
|
+
return new Container();
|
|
566
562
|
}
|
|
567
563
|
|
|
568
564
|
it('should resolve the same container per every request', function () {
|
|
@@ -622,7 +618,7 @@ class Logger {
|
|
|
622
618
|
|
|
623
619
|
describe('ArgsProvider', function () {
|
|
624
620
|
function createContainer() {
|
|
625
|
-
return new Container(
|
|
621
|
+
return new Container();
|
|
626
622
|
}
|
|
627
623
|
|
|
628
624
|
it('can assign argument function to provider', function () {
|
|
@@ -746,7 +742,6 @@ import {
|
|
|
746
742
|
Container,
|
|
747
743
|
DependencyNotFoundError,
|
|
748
744
|
key,
|
|
749
|
-
MetadataInjector,
|
|
750
745
|
provider,
|
|
751
746
|
register,
|
|
752
747
|
Registration as R,
|
|
@@ -761,7 +756,7 @@ describe('Visibility', function () {
|
|
|
761
756
|
@provider(singleton(), visible(({ isParent }) => isParent))
|
|
762
757
|
class FileLogger {}
|
|
763
758
|
|
|
764
|
-
const parent = new Container(
|
|
759
|
+
const parent = new Container({ tags: ['root'] }).add(R.toClass(FileLogger));
|
|
765
760
|
|
|
766
761
|
const child = parent.createScope({ tags: ['child'] });
|
|
767
762
|
|
|
@@ -789,7 +784,6 @@ import {
|
|
|
789
784
|
IMemo,
|
|
790
785
|
IMemoKey,
|
|
791
786
|
inject,
|
|
792
|
-
MetadataInjector,
|
|
793
787
|
Provider,
|
|
794
788
|
provider,
|
|
795
789
|
register,
|
|
@@ -849,9 +843,7 @@ describe('alias', () => {
|
|
|
849
843
|
}
|
|
850
844
|
}
|
|
851
845
|
|
|
852
|
-
const container = new Container(
|
|
853
|
-
.add(R.toClass(LoggerMiddleware))
|
|
854
|
-
.add(R.toClass(ErrorHandlerMiddleware));
|
|
846
|
+
const container = new Container().add(R.toClass(LoggerMiddleware)).add(R.toClass(ErrorHandlerMiddleware));
|
|
855
847
|
|
|
856
848
|
const app = container.resolve(App);
|
|
857
849
|
app.run();
|
|
@@ -864,7 +856,7 @@ describe('alias', () => {
|
|
|
864
856
|
@provider(alias('ILogger'))
|
|
865
857
|
class FileLogger {}
|
|
866
858
|
|
|
867
|
-
const container = new Container(
|
|
859
|
+
const container = new Container().add(R.toClass(FileLogger));
|
|
868
860
|
|
|
869
861
|
expect(byAlias((aliases) => aliases.has('ILogger'))(container)).toBeInstanceOf(FileLogger);
|
|
870
862
|
expect(() => byAlias((aliases) => aliases.has('logger'))(container)).toThrowError(DependencyNotFoundError);
|
|
@@ -879,7 +871,7 @@ describe('alias', () => {
|
|
|
879
871
|
@register(scope((s) => s.hasTag('child')))
|
|
880
872
|
class DbLogger {}
|
|
881
873
|
|
|
882
|
-
const container = new Container(
|
|
874
|
+
const container = new Container({ tags: ['root'] })
|
|
883
875
|
.register(IMemoKey, Provider.fromValue<IMemo>(new Map()))
|
|
884
876
|
.add(R.toClass(FileLogger))
|
|
885
877
|
.add(R.toClass(DbLogger));
|
|
@@ -909,7 +901,7 @@ describe('alias', () => {
|
|
|
909
901
|
) {}
|
|
910
902
|
}
|
|
911
903
|
|
|
912
|
-
const container = new Container(
|
|
904
|
+
const container = new Container()
|
|
913
905
|
.register(IMemoKey, Provider.fromValue<IMemo>(new Map()))
|
|
914
906
|
.add(R.toClass(FileLogger));
|
|
915
907
|
|
|
@@ -935,7 +927,6 @@ import {
|
|
|
935
927
|
IContainer,
|
|
936
928
|
inject,
|
|
937
929
|
key,
|
|
938
|
-
MetadataInjector,
|
|
939
930
|
provider,
|
|
940
931
|
register,
|
|
941
932
|
Registration as R,
|
|
@@ -995,7 +986,7 @@ describe('lazy provider', () => {
|
|
|
995
986
|
}
|
|
996
987
|
|
|
997
988
|
function createContainer() {
|
|
998
|
-
const container = new Container(
|
|
989
|
+
const container = new Container();
|
|
999
990
|
container.add(R.toClass(TodoRepository)).add(R.toClass(Logger));
|
|
1000
991
|
return container;
|
|
1001
992
|
}
|
|
@@ -1032,11 +1023,11 @@ Sometimes you want to register provider with certain key. This is what `key` is
|
|
|
1032
1023
|
|
|
1033
1024
|
```typescript
|
|
1034
1025
|
import 'reflect-metadata';
|
|
1035
|
-
import { Container, key,
|
|
1026
|
+
import { Container, key, provider, register, Registration as R, scope, singleton } from 'ts-ioc-container';
|
|
1036
1027
|
import { DependencyMissingKeyError } from '../../lib/errors/DependencyMissingKeyError';
|
|
1037
1028
|
|
|
1038
1029
|
describe('Registration module', function () {
|
|
1039
|
-
const createContainer = () => new Container(
|
|
1030
|
+
const createContainer = () => new Container({ tags: ['root'] });
|
|
1040
1031
|
|
|
1041
1032
|
it('should register class', function () {
|
|
1042
1033
|
@register(key('ILogger'), scope((s) => s.hasTag('root')))
|
|
@@ -1082,7 +1073,7 @@ describe('Registration module', function () {
|
|
|
1082
1073
|
const root = createContainer().add(R.toClass(Logger));
|
|
1083
1074
|
|
|
1084
1075
|
expect(root.resolve('Logger')).toBeInstanceOf(Logger);
|
|
1085
|
-
expect(root.resolve('
|
|
1076
|
+
expect(root.resolve('ILogger')).toBeInstanceOf(Logger);
|
|
1086
1077
|
});
|
|
1087
1078
|
});
|
|
1088
1079
|
|
|
@@ -1095,14 +1086,14 @@ Sometimes you need to register provider only in scope which matches to certain c
|
|
|
1095
1086
|
|
|
1096
1087
|
```typescript
|
|
1097
1088
|
import 'reflect-metadata';
|
|
1098
|
-
import { singleton, Container, key, provider,
|
|
1089
|
+
import { singleton, Container, key, provider, Registration as R, scope, register } from 'ts-ioc-container';
|
|
1099
1090
|
|
|
1100
1091
|
@register(key('ILogger'), scope((s) => s.hasTag('root')))
|
|
1101
1092
|
@provider(singleton()) // the same as .pipe(singleton(), scope((s) => s.hasTag('root')))
|
|
1102
1093
|
class Logger {}
|
|
1103
1094
|
describe('ScopeProvider', function () {
|
|
1104
1095
|
it('should return the same instance', function () {
|
|
1105
|
-
const root = new Container(
|
|
1096
|
+
const root = new Container({ tags: ['root'] }).add(R.toClass(Logger));
|
|
1106
1097
|
const child = root.createScope();
|
|
1107
1098
|
expect(root.resolve('ILogger')).toBe(child.resolve('ILogger'));
|
|
1108
1099
|
});
|
|
@@ -1115,7 +1106,7 @@ Sometimes you want to encapsulate registration logic in separate module. This is
|
|
|
1115
1106
|
|
|
1116
1107
|
```typescript
|
|
1117
1108
|
import 'reflect-metadata';
|
|
1118
|
-
import { IContainerModule, Registration as R, IContainer, key, Container,
|
|
1109
|
+
import { IContainerModule, Registration as R, IContainer, key, Container, register } from 'ts-ioc-container';
|
|
1119
1110
|
|
|
1120
1111
|
@register(key('ILogger'))
|
|
1121
1112
|
class Logger {}
|
|
@@ -1137,7 +1128,7 @@ class Development implements IContainerModule {
|
|
|
1137
1128
|
|
|
1138
1129
|
describe('Container Modules', function () {
|
|
1139
1130
|
function createContainer(isProduction: boolean) {
|
|
1140
|
-
return new Container(
|
|
1131
|
+
return new Container().use(isProduction ? new Production() : new Development());
|
|
1141
1132
|
}
|
|
1142
1133
|
|
|
1143
1134
|
it('should register production dependencies', function () {
|
|
@@ -1203,7 +1194,7 @@ class Logger {
|
|
|
1203
1194
|
|
|
1204
1195
|
describe('onConstruct', function () {
|
|
1205
1196
|
it('should make logger be ready on resolve', function () {
|
|
1206
|
-
const container = new Container(new MyInjector()).add(R.toClass(Logger));
|
|
1197
|
+
const container = new Container({ injector: new MyInjector() }).add(R.toClass(Logger));
|
|
1207
1198
|
|
|
1208
1199
|
const logger = container.resolve<Logger>('logger');
|
|
1209
1200
|
|
|
@@ -1268,7 +1259,7 @@ class Logger {
|
|
|
1268
1259
|
|
|
1269
1260
|
describe('onDispose', function () {
|
|
1270
1261
|
it('should invoke hooks on all instances', async function () {
|
|
1271
|
-
const container = new Container(
|
|
1262
|
+
const container = new Container().add(R.toClass(Logger)).add(R.toClass(LogsRepo));
|
|
1272
1263
|
|
|
1273
1264
|
const logger = container.resolve<Logger>('logger');
|
|
1274
1265
|
logger.log('Hello');
|
|
@@ -1286,7 +1277,7 @@ describe('onDispose', function () {
|
|
|
1286
1277
|
### Inject property
|
|
1287
1278
|
|
|
1288
1279
|
```typescript
|
|
1289
|
-
import { by, Container, hook, injectProp,
|
|
1280
|
+
import { by, Container, hook, injectProp, Registration, runHooks, runHooksAsync } from 'ts-ioc-container';
|
|
1290
1281
|
|
|
1291
1282
|
describe('inject property', () => {
|
|
1292
1283
|
it('should inject property', () => {
|
|
@@ -1296,7 +1287,7 @@ describe('inject property', () => {
|
|
|
1296
1287
|
}
|
|
1297
1288
|
const expected = 'Hello world!';
|
|
1298
1289
|
|
|
1299
|
-
const container = new Container(
|
|
1290
|
+
const container = new Container().add(Registration.toValue(expected).fromKey('greeting'));
|
|
1300
1291
|
const app = container.resolve(App);
|
|
1301
1292
|
runHooks(app as object, 'onInit', { scope: container });
|
|
1302
1293
|
|
|
@@ -1310,7 +1301,7 @@ describe('inject property', () => {
|
|
|
1310
1301
|
Sometimes you need to automatically mock all dependencies in container. This is what `AutoMockedContainer` is for.
|
|
1311
1302
|
|
|
1312
1303
|
```typescript
|
|
1313
|
-
import { AutoMockedContainer, Container, DependencyKey
|
|
1304
|
+
import { AutoMockedContainer, Container, DependencyKey } from 'ts-ioc-container';
|
|
1314
1305
|
import { IMock, Mock } from 'moq.ts';
|
|
1315
1306
|
|
|
1316
1307
|
export class MoqContainer extends AutoMockedContainer {
|
|
@@ -1335,7 +1326,7 @@ interface IEngine {
|
|
|
1335
1326
|
describe('Mocking', () => {
|
|
1336
1327
|
it('should auto-mock dependencies', () => {
|
|
1337
1328
|
const mockContainer = new MoqContainer();
|
|
1338
|
-
const container = new Container(
|
|
1329
|
+
const container = new Container({ parent: mockContainer });
|
|
1339
1330
|
|
|
1340
1331
|
const engineMock = mockContainer.resolveMock<IEngine>('IEngine');
|
|
1341
1332
|
engineMock.setup((i) => i.getRegistrationNumber()).returns('123');
|
|
@@ -4,14 +4,15 @@ exports.Container = void 0;
|
|
|
4
4
|
const IContainer_1 = require("./IContainer");
|
|
5
5
|
const EmptyContainer_1 = require("./EmptyContainer");
|
|
6
6
|
const ContainerDisposedError_1 = require("../errors/ContainerDisposedError");
|
|
7
|
+
const MetadataInjector_1 = require("../injector/MetadataInjector");
|
|
7
8
|
class Container {
|
|
8
|
-
constructor(
|
|
9
|
-
this.injector = injector;
|
|
9
|
+
constructor(options = {}) {
|
|
10
10
|
this.isDisposed = false;
|
|
11
11
|
this.scopes = [];
|
|
12
12
|
this.instances = [];
|
|
13
13
|
this.providers = new Map();
|
|
14
14
|
this.registrations = [];
|
|
15
|
+
this.injector = options.injector ?? new MetadataInjector_1.MetadataInjector();
|
|
15
16
|
this.parent = options.parent ?? new EmptyContainer_1.EmptyContainer();
|
|
16
17
|
this.tags = new Set(options.tags ?? []);
|
|
17
18
|
this.onConstruct = options.onConstruct ?? (() => { });
|
|
@@ -42,7 +43,7 @@ class Container {
|
|
|
42
43
|
}
|
|
43
44
|
createScope({ tags = [] } = {}) {
|
|
44
45
|
this.validateContainer();
|
|
45
|
-
const scope = new Container(this.injector,
|
|
46
|
+
const scope = new Container({ injector: this.injector, parent: this, tags, onDispose: this.onDispose });
|
|
46
47
|
scope.applyRegistrationsFrom(this);
|
|
47
48
|
this.scopes.push(scope);
|
|
48
49
|
return scope;
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { isConstructor, } from './IContainer';
|
|
2
2
|
import { EmptyContainer } from './EmptyContainer';
|
|
3
3
|
import { ContainerDisposedError } from '../errors/ContainerDisposedError';
|
|
4
|
+
import { MetadataInjector } from '../injector/MetadataInjector';
|
|
4
5
|
export class Container {
|
|
5
|
-
constructor(
|
|
6
|
-
this.injector = injector;
|
|
6
|
+
constructor(options = {}) {
|
|
7
7
|
this.isDisposed = false;
|
|
8
8
|
this.scopes = [];
|
|
9
9
|
this.instances = [];
|
|
10
10
|
this.providers = new Map();
|
|
11
11
|
this.registrations = [];
|
|
12
|
+
this.injector = options.injector ?? new MetadataInjector();
|
|
12
13
|
this.parent = options.parent ?? new EmptyContainer();
|
|
13
14
|
this.tags = new Set(options.tags ?? []);
|
|
14
15
|
this.onConstruct = options.onConstruct ?? (() => { });
|
|
@@ -39,7 +40,7 @@ export class Container {
|
|
|
39
40
|
}
|
|
40
41
|
createScope({ tags = [] } = {}) {
|
|
41
42
|
this.validateContainer();
|
|
42
|
-
const scope = new Container(this.injector,
|
|
43
|
+
const scope = new Container({ injector: this.injector, parent: this, tags, onDispose: this.onDispose });
|
|
43
44
|
scope.applyRegistrationsFrom(this);
|
|
44
45
|
this.scopes.push(scope);
|
|
45
46
|
return scope;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "38.0.0",
|
|
4
4
|
"description": "Typescript IoC container",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -41,7 +41,9 @@
|
|
|
41
41
|
"build:cjm": "rimraf cjm && tsc -p tsconfig.production.json --outDir cjm --module CommonJS",
|
|
42
42
|
"build:esm": "rimraf esm && tsc -p tsconfig.production.json --outDir esm",
|
|
43
43
|
"build:types": "rimraf typings && tsc -p tsconfig.production.json --outDir typings --emitDeclarationOnly --declaration",
|
|
44
|
-
"generate:docs": "
|
|
44
|
+
"generate:docs": "scripts/generate-readme/generate-readme.ts && git add README.md",
|
|
45
|
+
"test-coverage:check": "scripts/test-coverage/check-coverage.ts",
|
|
46
|
+
"test-coverage:update": "scripts/test-coverage/update-coverage-results.ts",
|
|
45
47
|
"build": "npm run build:cjm && npm run build:esm && npm run build:types",
|
|
46
48
|
"coverage": "coveralls",
|
|
47
49
|
"test": "jest --coverage",
|
|
@@ -77,7 +79,7 @@
|
|
|
77
79
|
"*.{js,ts,tsx}": [
|
|
78
80
|
"eslint --fix",
|
|
79
81
|
"prettier --write",
|
|
80
|
-
"
|
|
82
|
+
"npm run test-coverage:check"
|
|
81
83
|
]
|
|
82
84
|
},
|
|
83
85
|
"gitHead": "ae10f302c7e0f55196b42669040735112479a854",
|
|
@@ -3,7 +3,6 @@ import { IInjector } from '../injector/IInjector';
|
|
|
3
3
|
import { IProvider } from '../provider/IProvider';
|
|
4
4
|
import { IRegistration } from '../registration/IRegistration';
|
|
5
5
|
export declare class Container implements IContainer {
|
|
6
|
-
private readonly injector;
|
|
7
6
|
isDisposed: boolean;
|
|
8
7
|
private parent;
|
|
9
8
|
private readonly scopes;
|
|
@@ -13,7 +12,9 @@ export declare class Container implements IContainer {
|
|
|
13
12
|
private readonly registrations;
|
|
14
13
|
private readonly onConstruct;
|
|
15
14
|
private readonly onDispose;
|
|
16
|
-
|
|
15
|
+
private readonly injector;
|
|
16
|
+
constructor(options?: {
|
|
17
|
+
injector?: IInjector;
|
|
17
18
|
parent?: IContainer;
|
|
18
19
|
tags?: Tag[];
|
|
19
20
|
onConstruct?: (instance: Instance, scope: IContainer) => void;
|