@tsed/di 6.83.0 → 6.86.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.
@@ -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;
1017
+ const exports = await importPackage(packageName, resolver, optional);
1018
+ token = exports[key];
1025
1019
 
1026
- token = await importPackage(packageName, resolver, optional);
1027
- token = ((_token = token) == null ? void 0 : _token.default) || token;
1028
-
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
  /**
@@ -1396,9 +1410,18 @@ function Controller(options) {
1396
1410
  }, path && Path(path), Children(...children));
1397
1411
  }
1398
1412
 
1413
+ // istanbul ignore next
1399
1414
  const colorIfAllowed = colorFn => text => !process.env.NO_COLOR ? colorFn(text) : text;
1400
1415
 
1401
1416
  const red = colorIfAllowed(text => `\x1B[31m${text}\x1B[39m`);
1417
+ const yellow = colorIfAllowed(text => `\x1B[33m${text}\x1B[39m`);
1418
+ const green = colorIfAllowed(text => `\x1B[32m${text}\x1B[39m`);
1419
+ const colors = {
1420
+ red,
1421
+ yellow,
1422
+ green
1423
+ };
1424
+
1402
1425
  class InjectionError extends Error {
1403
1426
  constructor(token, origin) {
1404
1427
  super(isString(origin) ? origin : "");
@@ -1445,7 +1468,7 @@ function printDependencyInjectionError(token, options) {
1445
1468
  const args = getConstructorArgNames(token).map((arg, index) => {
1446
1469
  if (options.index === index) {
1447
1470
  erroredArg = arg;
1448
- arg = red(arg);
1471
+ arg = colors.red(arg);
1449
1472
  }
1450
1473
 
1451
1474
  return `${arg}: ${nameOf(options.deps[index])}`;
@@ -1453,7 +1476,7 @@ function printDependencyInjectionError(token, options) {
1453
1476
  const signature = nameOf(token) + "->constructor(" + args + ")";
1454
1477
  const indexOf = signature.indexOf(erroredArg) - 5;
1455
1478
 
1456
- const drawline = indexOf => " ".repeat(indexOf) + red("^" + "‾".repeat(erroredArg.length - 1));
1479
+ const drawline = indexOf => " ".repeat(indexOf) + colors.red("^" + "‾".repeat(erroredArg.length - 1));
1457
1480
 
1458
1481
  return "Unable to inject dependency. " + options.message + "\n\n" + signature + "\n" + (indexOf > -1 ? drawline(indexOf) : "");
1459
1482
  }
@@ -2419,5 +2442,5 @@ function getConfiguration(module, configuration = {}) {
2419
2442
  return config;
2420
2443
  }
2421
2444
 
2422
- export { Configuration, Constant, Container, ContextLogger, Controller, DIConfiguration, DIContext, DITest, DI_PARAMS, DI_PARAM_OPTIONS, GlobalProviderRegistry, GlobalProviders, INJECTABLE_PROP, Inject, Injectable, InjectablePropertyType, InjectionError, InjectorService, Intercept, Interceptor, LazyInject, LocalsContainer, Module, OptionalLazyInject, Opts, OverrideProvider, Provider, ProviderScope, ProviderType, Scope, Service, UndefinedTokenError, UseOpts, Value, createContainer, getConfiguration, injectProperty, registerController, registerFactory, registerProvider, registerValue, setLoggerLevel };
2445
+ export { Configuration, Constant, Container, ContextLogger, Controller, DIConfiguration, DIContext, DITest, DI_PARAMS, DI_PARAM_OPTIONS, GlobalProviderRegistry, GlobalProviders, INJECTABLE_PROP, Inject, Injectable, InjectablePropertyType, InjectionError, InjectorService, Intercept, Interceptor, LazyInject, LocalsContainer, Module, OptionalLazyInject, Opts, OverrideProvider, Provider, ProviderScope, ProviderType, Scope, Service, UndefinedTokenError, UseOpts, Value, colors, createContainer, getConfiguration, injectProperty, registerController, registerFactory, registerProvider, registerValue, setLoggerLevel };
2423
2446
  //# sourceMappingURL=index.modern.js.map