@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.
@@ -1,4 +1,4 @@
1
- import { getClassOrSymbol, Store, isClass, classOf, nameOf, StoreSet, DecoratorTypes, decoratorTypeOf, UnsupportedDecoratorType, Metadata, isPromise, importPackage, isString, isFunction, isBoolean, useDecorators, isArrayOrArrayClass, getConstructorArgNames, proxyDelegation, setValue, getValue, deepMerge, deepClone, ancestorsOf, prototypeOf, catchError, isInheritedFrom, Env } from '@tsed/core';
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
- function mapOptions$1(args) {
987
- const options = args.reduce((options, item) => {
988
- if (isString(item)) {
989
- options.packageName = item;
990
- } else if (isFunction(item)) {
991
- options.resolver = item;
992
- } else if (isBoolean(item)) {
993
- options.optional = item;
994
- }
995
-
996
- return options;
997
- }, {
998
- optional: false
999
- });
1000
-
1001
- if (!options.packageName && options.resolver) {
1002
- options.packageName = options.resolver.toString();
1003
- }
1004
-
1005
- if (!options.resolver) {
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(...args) {
1013
- const {
1014
- packageName,
1015
- resolver,
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
- var _token;
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 && classOf(token) === Object && !optional) {
1030
- throw new Error(`Unable to lazy load the "${packageName}". Resolved token isn\'t a valid provider.`);
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
- function OptionalLazyInject(arg) {
1044
- return LazyInject(arg, true);
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
  /**