@tsed/di 6.81.0 → 6.84.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.
- package/lib/decorators/lazyInject.d.ts +8 -18
- package/lib/index.js +54 -60
- package/lib/index.js.map +1 -1
- package/lib/index.modern.js +55 -41
- package/lib/index.modern.js.map +1 -1
- package/package.json +3 -3
package/lib/index.modern.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getClassOrSymbol, Store, isClass, classOf, nameOf, StoreSet, DecoratorTypes, decoratorTypeOf, UnsupportedDecoratorType, Metadata, isPromise, importPackage,
|
|
1
|
+
import { getClassOrSymbol, Store, isClass, classOf, nameOf, StoreSet, DecoratorTypes, decoratorTypeOf, UnsupportedDecoratorType, Metadata, isPromise, importPackage, useDecorators, isArrayOrArrayClass, isString, getConstructorArgNames, proxyDelegation, setValue, getValue, deepMerge, deepClone, ancestorsOf, prototypeOf, catchError, isFunction, isInheritedFrom, Env } from '@tsed/core';
|
|
2
2
|
import { levels, Logger, $log } from '@tsed/logger';
|
|
3
3
|
import { Path, Children } from '@tsed/schema';
|
|
4
4
|
import { __decorate, __metadata } from 'tslib';
|
|
@@ -983,54 +983,47 @@ function Inject(symbol, onGet = bean => bean) {
|
|
|
983
983
|
};
|
|
984
984
|
}
|
|
985
985
|
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
options.resolver = () => import(options.packageName);
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
return options;
|
|
1010
|
-
}
|
|
986
|
+
/**
|
|
987
|
+
* Lazy load a provider from his package and invoke only when the provider is used
|
|
988
|
+
*
|
|
989
|
+
* ```typescript
|
|
990
|
+
* import type {PlatformException} from "@tsed/platform-exceptions";
|
|
991
|
+
*
|
|
992
|
+
* @Injectable()
|
|
993
|
+
* export class MyService {
|
|
994
|
+
* @LazyInject("PlatformException", () => import("@tsed/platform-exceptions"))
|
|
995
|
+
* exceptions: Promise<PlatformException>;
|
|
996
|
+
* }
|
|
997
|
+
* ```
|
|
998
|
+
*
|
|
999
|
+
* @param key
|
|
1000
|
+
* @param packageName
|
|
1001
|
+
* @param resolver
|
|
1002
|
+
* @param optional
|
|
1003
|
+
* @returns {Function}
|
|
1004
|
+
* @decorator
|
|
1005
|
+
*/
|
|
1011
1006
|
|
|
1012
|
-
function LazyInject(
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
optional
|
|
1017
|
-
} = mapOptions$1(args);
|
|
1007
|
+
function LazyInject(key, resolver, {
|
|
1008
|
+
optional = false,
|
|
1009
|
+
packageName = resolver.toString()
|
|
1010
|
+
} = {}) {
|
|
1018
1011
|
return (target, propertyKey) => {
|
|
1019
1012
|
let bean, token;
|
|
1020
1013
|
injectProperty(target, propertyKey, {
|
|
1021
1014
|
resolver(injector) {
|
|
1022
1015
|
return async () => {
|
|
1023
1016
|
if (!token) {
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
token = await importPackage(packageName, resolver, optional);
|
|
1027
|
-
token = ((_token = token) == null ? void 0 : _token.default) || token;
|
|
1017
|
+
const exports = await importPackage(packageName, resolver, optional);
|
|
1018
|
+
token = exports[key];
|
|
1028
1019
|
|
|
1029
|
-
if (token
|
|
1030
|
-
|
|
1020
|
+
if (!token) {
|
|
1021
|
+
if (!optional) {
|
|
1022
|
+
throw new Error(`Unable to lazy load the "${key}". The token isn\'t a valid token provider.`);
|
|
1023
|
+
}
|
|
1031
1024
|
}
|
|
1032
1025
|
|
|
1033
|
-
bean = await injector.lazyInvoke(token);
|
|
1026
|
+
bean = token ? await injector.lazyInvoke(token) : {};
|
|
1034
1027
|
}
|
|
1035
1028
|
|
|
1036
1029
|
return bean;
|
|
@@ -1040,8 +1033,29 @@ function LazyInject(...args) {
|
|
|
1040
1033
|
});
|
|
1041
1034
|
};
|
|
1042
1035
|
}
|
|
1043
|
-
|
|
1044
|
-
|
|
1036
|
+
/**
|
|
1037
|
+
* Optionally Lazy load a provider from his package and invoke only when the provider is used
|
|
1038
|
+
*
|
|
1039
|
+
* ```typescript
|
|
1040
|
+
* import type {PlatformException} from "@tsed/platform-exceptions";
|
|
1041
|
+
*
|
|
1042
|
+
* @Injectable()
|
|
1043
|
+
* export class MyService {
|
|
1044
|
+
* @OptionalLazyInject("@tsed/platform-exceptions")
|
|
1045
|
+
* exceptions: Promise<PlatformException>;
|
|
1046
|
+
* }
|
|
1047
|
+
* ```
|
|
1048
|
+
*
|
|
1049
|
+
* @param key
|
|
1050
|
+
* @param resolver
|
|
1051
|
+
* @returns {Function}
|
|
1052
|
+
* @decorator
|
|
1053
|
+
*/
|
|
1054
|
+
|
|
1055
|
+
function OptionalLazyInject(key, resolver) {
|
|
1056
|
+
return LazyInject(key, resolver, {
|
|
1057
|
+
optional: true
|
|
1058
|
+
});
|
|
1045
1059
|
}
|
|
1046
1060
|
|
|
1047
1061
|
/**
|