ts-ioc-container 33.5.0 → 34.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 +47 -47
- package/cjm/by.js +1 -1
- package/cjm/provider/Provider.js +3 -0
- package/cjm/registration/IRegistration.js +2 -8
- package/cjm/registration/Registration.js +11 -6
- package/esm/by.js +1 -1
- package/esm/provider/Provider.js +3 -0
- package/esm/registration/IRegistration.js +2 -8
- package/esm/registration/Registration.js +11 -6
- package/package.json +2 -2
- package/typings/provider/Provider.d.ts +1 -0
- package/typings/registration/IRegistration.d.ts +3 -3
- package/typings/registration/Registration.d.ts +6 -5
package/README.md
CHANGED
|
@@ -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(new MetadataInjector()).add(R.
|
|
100
|
+
const container = new Container(new MetadataInjector()).add(R.toClass(Logger).fromKey('ILogger'));
|
|
101
101
|
|
|
102
102
|
expect(container.resolve(App).logger.name).toBe('Logger');
|
|
103
103
|
});
|
|
@@ -108,8 +108,8 @@ describe('Basic usage', function () {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
const container = new Container(new MetadataInjector())
|
|
111
|
-
.add(R.
|
|
112
|
-
.add(R.
|
|
111
|
+
.add(R.toClass(Logger).fromKey('ILogger1'))
|
|
112
|
+
.add(R.toClass(Logger).fromKey('ILogger2'));
|
|
113
113
|
|
|
114
114
|
expect(container.resolve(App).loggers).toHaveLength(2);
|
|
115
115
|
});
|
|
@@ -160,7 +160,7 @@ class Logger {}
|
|
|
160
160
|
|
|
161
161
|
describe('Scopes', function () {
|
|
162
162
|
it('should resolve dependencies from scope', function () {
|
|
163
|
-
const root = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.
|
|
163
|
+
const root = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.toClass(Logger));
|
|
164
164
|
const child = root.createScope('child');
|
|
165
165
|
|
|
166
166
|
expect(child.resolve('ILogger')).toBe(child.resolve('ILogger'));
|
|
@@ -214,7 +214,7 @@ describe('Instances', function () {
|
|
|
214
214
|
class Logger {}
|
|
215
215
|
|
|
216
216
|
it('should return injected instances', () => {
|
|
217
|
-
const container = new Container(new MetadataInjector()).add(R.
|
|
217
|
+
const container = new Container(new MetadataInjector()).add(R.toClass(Logger));
|
|
218
218
|
const scope = container.createScope();
|
|
219
219
|
|
|
220
220
|
const logger1 = container.resolve('ILogger');
|
|
@@ -231,7 +231,7 @@ describe('Instances', function () {
|
|
|
231
231
|
constructor(@inject(by.instances(isLogger)) public loggers: Logger[]) {}
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
-
const container = new Container(new MetadataInjector()).add(R.
|
|
234
|
+
const container = new Container(new MetadataInjector()).add(R.toClass(Logger));
|
|
235
235
|
|
|
236
236
|
const logger0 = container.resolve('ILogger');
|
|
237
237
|
const logger1 = container.resolve('ILogger');
|
|
@@ -260,7 +260,7 @@ class Logger {}
|
|
|
260
260
|
|
|
261
261
|
describe('Disposing', function () {
|
|
262
262
|
it('should container and make it unavailable for the further usage', function () {
|
|
263
|
-
const root = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.
|
|
263
|
+
const root = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.toClass(Logger).fromKey('ILogger'));
|
|
264
264
|
const child = root.createScope('child');
|
|
265
265
|
|
|
266
266
|
const logger = child.resolve('ILogger');
|
|
@@ -312,7 +312,7 @@ describe('lazy provider', () => {
|
|
|
312
312
|
|
|
313
313
|
function createContainer() {
|
|
314
314
|
const container = new Container(new MetadataInjector());
|
|
315
|
-
container.add(R.
|
|
315
|
+
container.add(R.toClass(Flag)).add(R.toClass(Service));
|
|
316
316
|
return container;
|
|
317
317
|
}
|
|
318
318
|
|
|
@@ -403,7 +403,7 @@ class App {
|
|
|
403
403
|
|
|
404
404
|
describe('Reflection Injector', function () {
|
|
405
405
|
it('should inject dependencies by @inject decorator', function () {
|
|
406
|
-
const container = new Container(new MetadataInjector()).add(R.
|
|
406
|
+
const container = new Container(new MetadataInjector()).add(R.toClass(Logger).fromKey('ILogger'));
|
|
407
407
|
|
|
408
408
|
const app = container.resolve(App);
|
|
409
409
|
|
|
@@ -426,7 +426,7 @@ describe('SimpleInjector', function () {
|
|
|
426
426
|
constructor(public container: IContainer) {}
|
|
427
427
|
}
|
|
428
428
|
|
|
429
|
-
const container = new Container(new SimpleInjector()).add(R.
|
|
429
|
+
const container = new Container(new SimpleInjector()).add(R.toClass(App).fromKey('App'));
|
|
430
430
|
const app = container.resolve<App>('App');
|
|
431
431
|
|
|
432
432
|
expect(app.container).toBeInstanceOf(Container);
|
|
@@ -440,7 +440,7 @@ describe('SimpleInjector', function () {
|
|
|
440
440
|
) {}
|
|
441
441
|
}
|
|
442
442
|
|
|
443
|
-
const container = new Container(new SimpleInjector()).add(R.
|
|
443
|
+
const container = new Container(new SimpleInjector()).add(R.toClass(App).fromKey('App'));
|
|
444
444
|
const app = container.resolve<App>('App', { args: ['Hello world'] });
|
|
445
445
|
|
|
446
446
|
expect(app.greeting).toBe('Hello world');
|
|
@@ -468,7 +468,7 @@ describe('ProxyInjector', function () {
|
|
|
468
468
|
}
|
|
469
469
|
}
|
|
470
470
|
|
|
471
|
-
const container = new Container(new ProxyInjector()).add(R.
|
|
471
|
+
const container = new Container(new ProxyInjector()).add(R.toClass(Logger).fromKey('logger'));
|
|
472
472
|
|
|
473
473
|
const app = container.resolve(App);
|
|
474
474
|
expect(app.logger).toBeInstanceOf(Logger);
|
|
@@ -498,8 +498,8 @@ describe('ProxyInjector', function () {
|
|
|
498
498
|
const greetingTemplate = (name: string) => `Hello ${name}`;
|
|
499
499
|
|
|
500
500
|
const container = new Container(new ProxyInjector())
|
|
501
|
-
.add(R.
|
|
502
|
-
.add(R.
|
|
501
|
+
.add(R.toClass(App).fromKey('App').pipe(args({ greetingTemplate })))
|
|
502
|
+
.add(R.toClass(Logger).fromKey('logger'));
|
|
503
503
|
|
|
504
504
|
const app = container.resolve<App>('App', { args: [{ name: `world` }] });
|
|
505
505
|
expect(app.greeting).toBe('Hello world');
|
|
@@ -573,20 +573,20 @@ describe('Singleton', function () {
|
|
|
573
573
|
}
|
|
574
574
|
|
|
575
575
|
it('should resolve the same container per every request', function () {
|
|
576
|
-
const container = createContainer().add(R.
|
|
576
|
+
const container = createContainer().add(R.toClass(Logger));
|
|
577
577
|
|
|
578
578
|
expect(container.resolve('logger')).toBe(container.resolve('logger'));
|
|
579
579
|
});
|
|
580
580
|
|
|
581
581
|
it('should resolve different dependency per scope', function () {
|
|
582
|
-
const container = createContainer().add(R.
|
|
582
|
+
const container = createContainer().add(R.toClass(Logger));
|
|
583
583
|
const child = container.createScope();
|
|
584
584
|
|
|
585
585
|
expect(container.resolve('logger')).not.toBe(child.resolve('logger'));
|
|
586
586
|
});
|
|
587
587
|
|
|
588
588
|
it('should resolve the same dependency for scope', function () {
|
|
589
|
-
const container = createContainer().add(R.
|
|
589
|
+
const container = createContainer().add(R.toClass(Logger));
|
|
590
590
|
const child = container.createScope();
|
|
591
591
|
|
|
592
592
|
expect(child.resolve('logger')).toBe(child.resolve('logger'));
|
|
@@ -633,21 +633,21 @@ describe('ArgsProvider', function () {
|
|
|
633
633
|
}
|
|
634
634
|
|
|
635
635
|
it('can assign argument function to provider', function () {
|
|
636
|
-
const root = createContainer().add(R.
|
|
636
|
+
const root = createContainer().add(R.toClass(Logger).pipe(argsFn((container, ...args) => ['name'])));
|
|
637
637
|
|
|
638
638
|
const logger = root.createScope().resolve<Logger>('logger');
|
|
639
639
|
expect(logger.name).toBe('name');
|
|
640
640
|
});
|
|
641
641
|
|
|
642
642
|
it('can assign argument to provider', function () {
|
|
643
|
-
const root = createContainer().add(R.
|
|
643
|
+
const root = createContainer().add(R.toClass(Logger).pipe(args('name')));
|
|
644
644
|
|
|
645
645
|
const logger = root.resolve<Logger>('logger');
|
|
646
646
|
expect(logger.name).toBe('name');
|
|
647
647
|
});
|
|
648
648
|
|
|
649
649
|
it('should set provider arguments with highest priority in compare to resolve arguments', function () {
|
|
650
|
-
const root = createContainer().add(R.
|
|
650
|
+
const root = createContainer().add(R.toClass(Logger).pipe(args('name')));
|
|
651
651
|
|
|
652
652
|
const logger = root.resolve<Logger>('logger', { args: ['file'] });
|
|
653
653
|
|
|
@@ -684,9 +684,9 @@ describe('ArgsProvider', function () {
|
|
|
684
684
|
}
|
|
685
685
|
|
|
686
686
|
const root = createContainer()
|
|
687
|
-
.add(R.
|
|
688
|
-
.add(R.
|
|
689
|
-
.add(R.
|
|
687
|
+
.add(R.toClass(EntityManager))
|
|
688
|
+
.add(R.toClass(UserRepository))
|
|
689
|
+
.add(R.toClass(TodoRepository));
|
|
690
690
|
const main = root.resolve(Main);
|
|
691
691
|
|
|
692
692
|
expect(main.userEntities.repository).toBeInstanceOf(UserRepository);
|
|
@@ -725,9 +725,9 @@ describe('ArgsProvider', function () {
|
|
|
725
725
|
}
|
|
726
726
|
|
|
727
727
|
const root = createContainer()
|
|
728
|
-
.add(R.
|
|
729
|
-
.add(R.
|
|
730
|
-
.add(R.
|
|
728
|
+
.add(R.toClass(EntityManager))
|
|
729
|
+
.add(R.toClass(UserRepository))
|
|
730
|
+
.add(R.toClass(TodoRepository));
|
|
731
731
|
const main = root.resolve(Main);
|
|
732
732
|
|
|
733
733
|
const userRepository = root.resolve<EntityManager>('EntityManager', { args: ['UserRepository'] }).repository;
|
|
@@ -768,7 +768,7 @@ describe('Visibility', function () {
|
|
|
768
768
|
@provider(singleton(), visible(({ isParent }) => isParent))
|
|
769
769
|
class FileLogger {}
|
|
770
770
|
|
|
771
|
-
const parent = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.
|
|
771
|
+
const parent = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.toClass(FileLogger));
|
|
772
772
|
|
|
773
773
|
const child = parent.createScope('child');
|
|
774
774
|
|
|
@@ -857,8 +857,8 @@ describe('alias', () => {
|
|
|
857
857
|
}
|
|
858
858
|
|
|
859
859
|
const container = new Container(new MetadataInjector())
|
|
860
|
-
.add(R.
|
|
861
|
-
.add(R.
|
|
860
|
+
.add(R.toClass(LoggerMiddleware))
|
|
861
|
+
.add(R.toClass(ErrorHandlerMiddleware));
|
|
862
862
|
|
|
863
863
|
const app = container.resolve(App);
|
|
864
864
|
app.run();
|
|
@@ -871,7 +871,7 @@ describe('alias', () => {
|
|
|
871
871
|
@provider(alias('ILogger'))
|
|
872
872
|
class FileLogger {}
|
|
873
873
|
|
|
874
|
-
const container = new Container(new MetadataInjector()).add(R.
|
|
874
|
+
const container = new Container(new MetadataInjector()).add(R.toClass(FileLogger));
|
|
875
875
|
|
|
876
876
|
expect(byAlias((aliases) => aliases.has('ILogger'))(container)).toBeInstanceOf(FileLogger);
|
|
877
877
|
expect(() => byAlias((aliases) => aliases.has('logger'))(container)).toThrowError(DependencyNotFoundError);
|
|
@@ -888,8 +888,8 @@ describe('alias', () => {
|
|
|
888
888
|
|
|
889
889
|
const container = new Container(new MetadataInjector(), { tags: ['root'] })
|
|
890
890
|
.register(IMemoKey, Provider.fromValue<IMemo>(new Map()))
|
|
891
|
-
.add(R.
|
|
892
|
-
.add(R.
|
|
891
|
+
.add(R.toClass(FileLogger))
|
|
892
|
+
.add(R.toClass(DbLogger));
|
|
893
893
|
|
|
894
894
|
const result1 = byAlias((aliases) => aliases.has('ILogger'), { memoize: constant('ILogger') })(container);
|
|
895
895
|
const child = container.createScope('child');
|
|
@@ -919,10 +919,10 @@ describe('alias', () => {
|
|
|
919
919
|
|
|
920
920
|
const container = new Container(new MetadataInjector())
|
|
921
921
|
.register(IMemoKey, Provider.fromValue<IMemo>(new Map()))
|
|
922
|
-
.add(R.
|
|
922
|
+
.add(R.toClass(FileLogger));
|
|
923
923
|
|
|
924
924
|
const loggers = container.resolve(App).loggers;
|
|
925
|
-
container.add(R.
|
|
925
|
+
container.add(R.toClass(DbLogger));
|
|
926
926
|
const loggers2 = container.resolve(App).loggers;
|
|
927
927
|
|
|
928
928
|
expect(loggers).toEqual(loggers2);
|
|
@@ -1006,7 +1006,7 @@ describe('lazy provider', () => {
|
|
|
1006
1006
|
|
|
1007
1007
|
function createContainer() {
|
|
1008
1008
|
const container = new Container(new MetadataInjector());
|
|
1009
|
-
container.add(R.
|
|
1009
|
+
container.add(R.toClass(TodoRepository)).add(R.toClass(Logger));
|
|
1010
1010
|
return container;
|
|
1011
1011
|
}
|
|
1012
1012
|
|
|
@@ -1053,33 +1053,33 @@ describe('Registration module', function () {
|
|
|
1053
1053
|
@provider(singleton())
|
|
1054
1054
|
class Logger {}
|
|
1055
1055
|
|
|
1056
|
-
const root = createContainer().add(R.
|
|
1056
|
+
const root = createContainer().add(R.toClass(Logger));
|
|
1057
1057
|
|
|
1058
1058
|
expect(root.resolve('ILogger')).toBeInstanceOf(Logger);
|
|
1059
1059
|
});
|
|
1060
1060
|
|
|
1061
1061
|
it('should register value', function () {
|
|
1062
|
-
const root = createContainer().add(R.
|
|
1062
|
+
const root = createContainer().add(R.toValue('smth').fromKey('ISmth'));
|
|
1063
1063
|
|
|
1064
1064
|
expect(root.resolve('ISmth')).toBe('smth');
|
|
1065
1065
|
});
|
|
1066
1066
|
|
|
1067
1067
|
it('should register fn', function () {
|
|
1068
|
-
const root = createContainer().add(R.
|
|
1068
|
+
const root = createContainer().add(R.toFn(() => 'smth').fromKey('ISmth'));
|
|
1069
1069
|
|
|
1070
1070
|
expect(root.resolve('ISmth')).toBe('smth');
|
|
1071
1071
|
});
|
|
1072
1072
|
|
|
1073
1073
|
it('should raise an error if key is not provider', () => {
|
|
1074
1074
|
expect(() => {
|
|
1075
|
-
createContainer().add(R.
|
|
1075
|
+
createContainer().add(R.toValue('smth'));
|
|
1076
1076
|
}).toThrowError(DependencyMissingKeyError);
|
|
1077
1077
|
});
|
|
1078
1078
|
|
|
1079
1079
|
it('should register dependency by class name if @key is not provided', function () {
|
|
1080
1080
|
class FileLogger {}
|
|
1081
1081
|
|
|
1082
|
-
const root = createContainer().add(R.
|
|
1082
|
+
const root = createContainer().add(R.toClass(FileLogger));
|
|
1083
1083
|
|
|
1084
1084
|
expect(root.resolve('FileLogger')).toBeInstanceOf(FileLogger);
|
|
1085
1085
|
});
|
|
@@ -1089,7 +1089,7 @@ describe('Registration module', function () {
|
|
|
1089
1089
|
@provider(singleton())
|
|
1090
1090
|
class Logger {}
|
|
1091
1091
|
|
|
1092
|
-
const root = createContainer().add(R.
|
|
1092
|
+
const root = createContainer().add(R.toClass(Logger));
|
|
1093
1093
|
|
|
1094
1094
|
expect(root.resolve('Logger')).toBeInstanceOf(Logger);
|
|
1095
1095
|
expect(root.resolve('Logger')).toBe(root.resolve('ILogger'));
|
|
@@ -1112,7 +1112,7 @@ import { singleton, Container, key, provider, MetadataInjector, Registration as
|
|
|
1112
1112
|
class Logger {}
|
|
1113
1113
|
describe('ScopeProvider', function () {
|
|
1114
1114
|
it('should return the same instance', function () {
|
|
1115
|
-
const root = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.
|
|
1115
|
+
const root = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.toClass(Logger));
|
|
1116
1116
|
const child = root.createScope();
|
|
1117
1117
|
expect(root.resolve('ILogger')).toBe(child.resolve('ILogger'));
|
|
1118
1118
|
});
|
|
@@ -1135,13 +1135,13 @@ class TestLogger {}
|
|
|
1135
1135
|
|
|
1136
1136
|
class Production implements IContainerModule {
|
|
1137
1137
|
applyTo(container: IContainer): void {
|
|
1138
|
-
container.add(R.
|
|
1138
|
+
container.add(R.toClass(Logger));
|
|
1139
1139
|
}
|
|
1140
1140
|
}
|
|
1141
1141
|
|
|
1142
1142
|
class Development implements IContainerModule {
|
|
1143
1143
|
applyTo(container: IContainer): void {
|
|
1144
|
-
container.add(R.
|
|
1144
|
+
container.add(R.toClass(TestLogger));
|
|
1145
1145
|
}
|
|
1146
1146
|
}
|
|
1147
1147
|
|
|
@@ -1213,7 +1213,7 @@ class Logger {
|
|
|
1213
1213
|
|
|
1214
1214
|
describe('onConstruct', function () {
|
|
1215
1215
|
it('should make logger be ready on resolve', function () {
|
|
1216
|
-
const container = new Container(new MyInjector()).add(R.
|
|
1216
|
+
const container = new Container(new MyInjector()).add(R.toClass(Logger));
|
|
1217
1217
|
|
|
1218
1218
|
const logger = container.resolve<Logger>('logger');
|
|
1219
1219
|
|
|
@@ -1278,7 +1278,7 @@ class Logger {
|
|
|
1278
1278
|
|
|
1279
1279
|
describe('onDispose', function () {
|
|
1280
1280
|
it('should invoke hooks on all instances', async function () {
|
|
1281
|
-
const container = new Container(new MetadataInjector()).add(R.
|
|
1281
|
+
const container = new Container(new MetadataInjector()).add(R.toClass(Logger)).add(R.toClass(LogsRepo));
|
|
1282
1282
|
|
|
1283
1283
|
const logger = container.resolve<Logger>('logger');
|
|
1284
1284
|
logger.log('Hello');
|
|
@@ -1306,7 +1306,7 @@ describe('inject property', () => {
|
|
|
1306
1306
|
}
|
|
1307
1307
|
const expected = 'Hello world!';
|
|
1308
1308
|
|
|
1309
|
-
const container = new Container(new MetadataInjector()).add(Registration.
|
|
1309
|
+
const container = new Container(new MetadataInjector()).add(Registration.toValue(expected).fromKey('greeting'));
|
|
1310
1310
|
const app = container.resolve(App);
|
|
1311
1311
|
runHooks(app as object, 'onInit', { scope: container });
|
|
1312
1312
|
|
package/cjm/by.js
CHANGED
|
@@ -54,7 +54,7 @@ const depKey = (key = (0, utils_1.generateUUID)()) => {
|
|
|
54
54
|
return {
|
|
55
55
|
key,
|
|
56
56
|
assignTo: (registration) => {
|
|
57
|
-
let reg = registration.pipe(...mappers).
|
|
57
|
+
let reg = registration.pipe(...mappers).fromKey(key);
|
|
58
58
|
if (isValidWhen) {
|
|
59
59
|
reg = registration.when(isValidWhen);
|
|
60
60
|
}
|
package/cjm/provider/Provider.js
CHANGED
|
@@ -12,6 +12,9 @@ class Provider {
|
|
|
12
12
|
const mappers = (0, utils_1.isConstructor)(value) ? (0, IProvider_1.getTransformers)(value) ?? [] : [];
|
|
13
13
|
return new Provider(() => value).pipe(...mappers);
|
|
14
14
|
}
|
|
15
|
+
static fromKey(key) {
|
|
16
|
+
return new Provider((c) => c.resolve(key));
|
|
17
|
+
}
|
|
15
18
|
constructor(resolveDependency) {
|
|
16
19
|
this.resolveDependency = resolveDependency;
|
|
17
20
|
this.aliases = new Set();
|
|
@@ -4,16 +4,10 @@ exports.register = exports.getTransformers = exports.scope = exports.redirectFro
|
|
|
4
4
|
const metadata_1 = require("../metadata");
|
|
5
5
|
const key = (...keys) => (r) => {
|
|
6
6
|
const [originalKey, ...redirectKeys] = keys;
|
|
7
|
-
|
|
8
|
-
for (const key of redirectKeys) {
|
|
9
|
-
registration = registration.redirectFrom(key);
|
|
10
|
-
}
|
|
11
|
-
return registration;
|
|
7
|
+
return r.fromKey(originalKey).redirectFrom(...redirectKeys);
|
|
12
8
|
};
|
|
13
9
|
exports.key = key;
|
|
14
|
-
const redirectFrom = (
|
|
15
|
-
return r.redirectFrom(redirectKey);
|
|
16
|
-
};
|
|
10
|
+
const redirectFrom = (...keys) => (r) => r.redirectFrom(...keys);
|
|
17
11
|
exports.redirectFrom = redirectFrom;
|
|
18
12
|
const scope = (predicate) => (r) => r.when(predicate);
|
|
19
13
|
exports.scope = scope;
|
|
@@ -6,20 +6,23 @@ const Provider_1 = require("../provider/Provider");
|
|
|
6
6
|
const DependencyMissingKeyError_1 = require("../errors/DependencyMissingKeyError");
|
|
7
7
|
const IRegistration_1 = require("./IRegistration");
|
|
8
8
|
class Registration {
|
|
9
|
-
static
|
|
9
|
+
static toClass(Target) {
|
|
10
10
|
const transform = (0, utils_1.pipe)(...(0, IRegistration_1.getTransformers)(Target));
|
|
11
11
|
return transform(new Registration(() => Provider_1.Provider.fromClass(Target), Target.name));
|
|
12
12
|
}
|
|
13
|
-
static
|
|
13
|
+
static toValue(value) {
|
|
14
14
|
if ((0, utils_1.isConstructor)(value)) {
|
|
15
15
|
const transform = (0, utils_1.pipe)(...(0, IRegistration_1.getTransformers)(value));
|
|
16
16
|
return transform(new Registration(() => Provider_1.Provider.fromValue(value), value.name));
|
|
17
17
|
}
|
|
18
18
|
return new Registration(() => Provider_1.Provider.fromValue(value));
|
|
19
19
|
}
|
|
20
|
-
static
|
|
20
|
+
static toFn(fn) {
|
|
21
21
|
return new Registration(() => new Provider_1.Provider(fn));
|
|
22
22
|
}
|
|
23
|
+
static toKey(key) {
|
|
24
|
+
return new Registration(() => Provider_1.Provider.fromKey(key));
|
|
25
|
+
}
|
|
23
26
|
constructor(createProvider, key, matchScope = () => true) {
|
|
24
27
|
this.createProvider = createProvider;
|
|
25
28
|
this.key = key;
|
|
@@ -27,12 +30,14 @@ class Registration {
|
|
|
27
30
|
this.redirectKeys = new Set();
|
|
28
31
|
this.mappers = [];
|
|
29
32
|
}
|
|
30
|
-
|
|
33
|
+
fromKey(key) {
|
|
31
34
|
this.key = key;
|
|
32
35
|
return this;
|
|
33
36
|
}
|
|
34
|
-
redirectFrom(
|
|
35
|
-
|
|
37
|
+
redirectFrom(...keys) {
|
|
38
|
+
for (const key of keys) {
|
|
39
|
+
this.redirectKeys.add(key);
|
|
40
|
+
}
|
|
36
41
|
return this;
|
|
37
42
|
}
|
|
38
43
|
pipe(...mappers) {
|
package/esm/by.js
CHANGED
|
@@ -48,7 +48,7 @@ export const depKey = (key = generateUUID()) => {
|
|
|
48
48
|
return {
|
|
49
49
|
key,
|
|
50
50
|
assignTo: (registration) => {
|
|
51
|
-
let reg = registration.pipe(...mappers).
|
|
51
|
+
let reg = registration.pipe(...mappers).fromKey(key);
|
|
52
52
|
if (isValidWhen) {
|
|
53
53
|
reg = registration.when(isValidWhen);
|
|
54
54
|
}
|
package/esm/provider/Provider.js
CHANGED
|
@@ -9,6 +9,9 @@ export class Provider {
|
|
|
9
9
|
const mappers = isConstructor(value) ? getTransformers(value) ?? [] : [];
|
|
10
10
|
return new Provider(() => value).pipe(...mappers);
|
|
11
11
|
}
|
|
12
|
+
static fromKey(key) {
|
|
13
|
+
return new Provider((c) => c.resolve(key));
|
|
14
|
+
}
|
|
12
15
|
constructor(resolveDependency) {
|
|
13
16
|
this.resolveDependency = resolveDependency;
|
|
14
17
|
this.aliases = new Set();
|
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
import { getMetadata, setMetadata } from '../metadata';
|
|
2
2
|
export const key = (...keys) => (r) => {
|
|
3
3
|
const [originalKey, ...redirectKeys] = keys;
|
|
4
|
-
|
|
5
|
-
for (const key of redirectKeys) {
|
|
6
|
-
registration = registration.redirectFrom(key);
|
|
7
|
-
}
|
|
8
|
-
return registration;
|
|
9
|
-
};
|
|
10
|
-
export const redirectFrom = (redirectKey) => (r) => {
|
|
11
|
-
return r.redirectFrom(redirectKey);
|
|
4
|
+
return r.fromKey(originalKey).redirectFrom(...redirectKeys);
|
|
12
5
|
};
|
|
6
|
+
export const redirectFrom = (...keys) => (r) => r.redirectFrom(...keys);
|
|
13
7
|
export const scope = (predicate) => (r) => r.when(predicate);
|
|
14
8
|
const METADATA_KEY = 'registration';
|
|
15
9
|
export const getTransformers = (Target) => getMetadata(Target, METADATA_KEY) ?? [];
|
|
@@ -3,20 +3,23 @@ import { Provider } from '../provider/Provider';
|
|
|
3
3
|
import { DependencyMissingKeyError } from '../errors/DependencyMissingKeyError';
|
|
4
4
|
import { getTransformers } from './IRegistration';
|
|
5
5
|
export class Registration {
|
|
6
|
-
static
|
|
6
|
+
static toClass(Target) {
|
|
7
7
|
const transform = pipe(...getTransformers(Target));
|
|
8
8
|
return transform(new Registration(() => Provider.fromClass(Target), Target.name));
|
|
9
9
|
}
|
|
10
|
-
static
|
|
10
|
+
static toValue(value) {
|
|
11
11
|
if (isConstructor(value)) {
|
|
12
12
|
const transform = pipe(...getTransformers(value));
|
|
13
13
|
return transform(new Registration(() => Provider.fromValue(value), value.name));
|
|
14
14
|
}
|
|
15
15
|
return new Registration(() => Provider.fromValue(value));
|
|
16
16
|
}
|
|
17
|
-
static
|
|
17
|
+
static toFn(fn) {
|
|
18
18
|
return new Registration(() => new Provider(fn));
|
|
19
19
|
}
|
|
20
|
+
static toKey(key) {
|
|
21
|
+
return new Registration(() => Provider.fromKey(key));
|
|
22
|
+
}
|
|
20
23
|
constructor(createProvider, key, matchScope = () => true) {
|
|
21
24
|
this.createProvider = createProvider;
|
|
22
25
|
this.key = key;
|
|
@@ -24,12 +27,14 @@ export class Registration {
|
|
|
24
27
|
this.redirectKeys = new Set();
|
|
25
28
|
this.mappers = [];
|
|
26
29
|
}
|
|
27
|
-
|
|
30
|
+
fromKey(key) {
|
|
28
31
|
this.key = key;
|
|
29
32
|
return this;
|
|
30
33
|
}
|
|
31
|
-
redirectFrom(
|
|
32
|
-
|
|
34
|
+
redirectFrom(...keys) {
|
|
35
|
+
for (const key of keys) {
|
|
36
|
+
this.redirectKeys.add(key);
|
|
37
|
+
}
|
|
33
38
|
return this;
|
|
34
39
|
}
|
|
35
40
|
pipe(...mappers) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "34.0.0",
|
|
4
4
|
"description": "Typescript IoC container",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"ts-node": "^10.9.1",
|
|
60
60
|
"typescript": "5.4.3"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "54c0f0048be52409045c3336677f900275baf425"
|
|
63
63
|
}
|
|
@@ -5,6 +5,7 @@ export declare class Provider<T = any> implements IProvider<T> {
|
|
|
5
5
|
private readonly resolveDependency;
|
|
6
6
|
static fromClass<T>(Target: constructor<T>): IProvider<T>;
|
|
7
7
|
static fromValue<T>(value: T): IProvider<T>;
|
|
8
|
+
static fromKey<T>(key: DependencyKey): Provider<T>;
|
|
8
9
|
key?: DependencyKey;
|
|
9
10
|
private readonly aliases;
|
|
10
11
|
private argsFn;
|
|
@@ -4,13 +4,13 @@ import { IProvider } from '../provider/IProvider';
|
|
|
4
4
|
export type ScopePredicate = (s: IContainer) => boolean;
|
|
5
5
|
export interface IRegistration<T = any> extends IContainerModule {
|
|
6
6
|
when(isValidWhen: ScopePredicate): this;
|
|
7
|
-
|
|
7
|
+
fromKey(key: DependencyKey): this;
|
|
8
8
|
pipe(...mappers: MapFn<IProvider<T>>[]): this;
|
|
9
|
-
redirectFrom(
|
|
9
|
+
redirectFrom(...keys: DependencyKey[]): this;
|
|
10
10
|
}
|
|
11
11
|
export type ReturnTypeOfRegistration<T> = T extends IRegistration<infer R> ? R : never;
|
|
12
12
|
export declare const key: (...keys: DependencyKey[]) => MapFn<IRegistration>;
|
|
13
|
-
export declare const redirectFrom: (
|
|
13
|
+
export declare const redirectFrom: (...keys: DependencyKey[]) => MapFn<IRegistration>;
|
|
14
14
|
export declare const scope: (predicate: ScopePredicate) => MapFn<IRegistration>;
|
|
15
15
|
export declare const getTransformers: (Target: constructor<unknown>) => MapFn<IRegistration<any>>[];
|
|
16
16
|
export declare const register: (...mappers: MapFn<IRegistration>[]) => ClassDecorator;
|
|
@@ -7,13 +7,14 @@ export declare class Registration<T = any> implements IRegistration<T> {
|
|
|
7
7
|
private key?;
|
|
8
8
|
private matchScope;
|
|
9
9
|
private redirectKeys;
|
|
10
|
-
static
|
|
11
|
-
static
|
|
12
|
-
static
|
|
10
|
+
static toClass<T>(Target: constructor<T>): IRegistration<any>;
|
|
11
|
+
static toValue<T>(value: T): IRegistration<any>;
|
|
12
|
+
static toFn<T>(fn: ResolveDependency<T>): Registration<T>;
|
|
13
|
+
static toKey<T>(key: DependencyKey): Registration<T>;
|
|
13
14
|
private mappers;
|
|
14
15
|
constructor(createProvider: (key: DependencyKey) => IProvider<T>, key?: DependencyKey | undefined, matchScope?: ScopePredicate);
|
|
15
|
-
|
|
16
|
-
redirectFrom(
|
|
16
|
+
fromKey(key: DependencyKey): this;
|
|
17
|
+
redirectFrom(...keys: DependencyKey[]): this;
|
|
17
18
|
pipe(...mappers: MapFn<IProvider<T>>[]): this;
|
|
18
19
|
when(isValidWhen: ScopePredicate): this;
|
|
19
20
|
applyTo(container: IContainer): void;
|