@tsed/di 6.79.0 → 6.81.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,8 +1,7 @@
1
- import { getClassOrSymbol, Store, isClass, classOf, nameOf, StoreSet, decoratorTypeOf, UnsupportedDecoratorType, DecoratorTypes, Metadata, useDecorators, isArrayOrArrayClass, isString, getConstructorArgNames, proxyDelegation, setValue, getValue, deepMerge, deepClone, ancestorsOf, prototypeOf, isPromise, catchError, isInheritedFrom, isFunction, Env } from '@tsed/core';
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';
2
2
  import { levels, Logger, $log } from '@tsed/logger';
3
3
  import { Path, Children } from '@tsed/schema';
4
4
  import { __decorate, __metadata } from 'tslib';
5
- import chalk from 'chalk';
6
5
 
7
6
  const INJECTABLE_PROP = "DI:INJECTABLE_PROP";
8
7
  const DI_PARAMS = "DI:PARAMS";
@@ -627,7 +626,7 @@ class ContextLogger {
627
626
 
628
627
  }
629
628
 
630
- const _excluded$2 = ["id", "injector", "logger", "ignoreLog"];
629
+ const _excluded$3 = ["id", "injector", "logger", "ignoreLog"];
631
630
  class DIContext extends Map {
632
631
  constructor(_ref) {
633
632
  let {
@@ -636,7 +635,7 @@ class DIContext extends Map {
636
635
  logger,
637
636
  ignoreLog
638
637
  } = _ref,
639
- options = _objectWithoutPropertiesLoose(_ref, _excluded$2);
638
+ options = _objectWithoutPropertiesLoose(_ref, _excluded$3);
640
639
 
641
640
  super();
642
641
  /**
@@ -897,6 +896,15 @@ function OverrideProvider(originalProvider) {
897
896
  };
898
897
  }
899
898
 
899
+ const _excluded$2 = ["options"];
900
+ function injectProperty(target, propertyKey, options) {
901
+ Store.from(target).merge(INJECTABLE_PROP, {
902
+ [propertyKey]: _extends({
903
+ bindingType: DecoratorTypes.PROP,
904
+ propertyKey
905
+ }, options)
906
+ });
907
+ }
900
908
  /**
901
909
  * Inject a provider to another provider.
902
910
  *
@@ -916,7 +924,7 @@ function OverrideProvider(originalProvider) {
916
924
  * @decorator
917
925
  */
918
926
 
919
- function Inject(symbol, onGet) {
927
+ function Inject(symbol, onGet = bean => bean) {
920
928
  return (target, propertyKey, descriptor) => {
921
929
  const bindingType = decoratorTypeOf([target, propertyKey, descriptor]);
922
930
 
@@ -932,13 +940,31 @@ function Inject(symbol, onGet) {
932
940
  break;
933
941
 
934
942
  case DecoratorTypes.PROP:
935
- Store.from(target).merge(INJECTABLE_PROP, {
936
- [propertyKey]: {
937
- bindingType,
938
- propertyKey,
939
- onGet,
940
- useType: symbol || Metadata.getType(target, propertyKey)
943
+ injectProperty(target, propertyKey, {
944
+ resolver(injector, locals, _ref) {
945
+ let {
946
+ options
947
+ } = _ref,
948
+ invokeOptions = _objectWithoutPropertiesLoose(_ref, _excluded$2);
949
+
950
+ locals.set(DI_PARAM_OPTIONS, _extends({}, options));
951
+ let bean;
952
+
953
+ if (!bean) {
954
+ const useType = symbol || Metadata.getType(target, propertyKey);
955
+ bean = injector.invoke(useType, locals, invokeOptions);
956
+ locals.delete(DI_PARAM_OPTIONS);
957
+ }
958
+
959
+ if (isPromise(bean)) {
960
+ bean.then(result => {
961
+ bean = result;
962
+ });
963
+ }
964
+
965
+ return () => onGet(bean);
941
966
  }
967
+
942
968
  });
943
969
  break;
944
970
 
@@ -957,6 +983,67 @@ function Inject(symbol, onGet) {
957
983
  };
958
984
  }
959
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
+ }
1011
+
1012
+ function LazyInject(...args) {
1013
+ const {
1014
+ packageName,
1015
+ resolver,
1016
+ optional
1017
+ } = mapOptions$1(args);
1018
+ return (target, propertyKey) => {
1019
+ let bean, token;
1020
+ injectProperty(target, propertyKey, {
1021
+ resolver(injector) {
1022
+ return async () => {
1023
+ if (!token) {
1024
+ var _token;
1025
+
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.`);
1031
+ }
1032
+
1033
+ bean = await injector.lazyInvoke(token);
1034
+ }
1035
+
1036
+ return bean;
1037
+ };
1038
+ }
1039
+
1040
+ });
1041
+ };
1042
+ }
1043
+ function OptionalLazyInject(arg) {
1044
+ return LazyInject(arg, true);
1045
+ }
1046
+
960
1047
  /**
961
1048
  * Return value from Configuration.
962
1049
  *
@@ -1309,6 +1396,9 @@ function Controller(options) {
1309
1396
  }, path && Path(path), Children(...children));
1310
1397
  }
1311
1398
 
1399
+ const colorIfAllowed = colorFn => text => !process.env.NO_COLOR ? colorFn(text) : text;
1400
+
1401
+ const red = colorIfAllowed(text => `\x1B[31m${text}\x1B[39m`);
1312
1402
  class InjectionError extends Error {
1313
1403
  constructor(token, origin) {
1314
1404
  super(isString(origin) ? origin : "");
@@ -1355,7 +1445,7 @@ function printDependencyInjectionError(token, options) {
1355
1445
  const args = getConstructorArgNames(token).map((arg, index) => {
1356
1446
  if (options.index === index) {
1357
1447
  erroredArg = arg;
1358
- arg = chalk.red(arg);
1448
+ arg = red(arg);
1359
1449
  }
1360
1450
 
1361
1451
  return `${arg}: ${nameOf(options.deps[index])}`;
@@ -1363,7 +1453,7 @@ function printDependencyInjectionError(token, options) {
1363
1453
  const signature = nameOf(token) + "->constructor(" + args + ")";
1364
1454
  const indexOf = signature.indexOf(erroredArg) - 5;
1365
1455
 
1366
- const drawline = indexOf => " ".repeat(indexOf) + chalk.red("^" + "‾".repeat(erroredArg.length - 1));
1456
+ const drawline = indexOf => " ".repeat(indexOf) + red("^" + "‾".repeat(erroredArg.length - 1));
1367
1457
 
1368
1458
  return "Unable to inject dependency. " + options.message + "\n\n" + signature + "\n" + (indexOf > -1 ? drawline(indexOf) : "");
1369
1459
  }
@@ -1631,9 +1721,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
1631
1721
 
1632
1722
 
1633
1723
  get(token, options = {}) {
1634
- var _super$get;
1635
-
1636
- const instance = (_super$get = super.get(getClassOrSymbol(token))) == null ? void 0 : _super$get.instance;
1724
+ const instance = this.getInstance(token);
1637
1725
 
1638
1726
  if (instance !== undefined) {
1639
1727
  return instance;
@@ -1890,7 +1978,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
1890
1978
  * @param instance
1891
1979
  * @param {string} propertyKey
1892
1980
  * @param {any} useType
1893
- * @param onGet
1981
+ * @param resolver
1894
1982
  * @param options
1895
1983
  * @param locals
1896
1984
  * @param invokeOptions
@@ -1899,23 +1987,15 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
1899
1987
 
1900
1988
  bindProperty(instance, {
1901
1989
  propertyKey,
1902
- useType,
1903
- onGet = f => f,
1904
- options
1990
+ resolver,
1991
+ options = {}
1905
1992
  }, locals, invokeOptions) {
1906
- invokeOptions = _extends({}, invokeOptions);
1907
- locals.set(DI_PARAM_OPTIONS, _extends({}, options));
1908
- let bean = this.invoke(useType, locals, invokeOptions);
1909
- locals.delete(DI_PARAM_OPTIONS);
1910
-
1911
- if (isPromise(bean)) {
1912
- bean.then(result => {
1913
- bean = result;
1914
- });
1915
- }
1916
-
1993
+ let get;
1994
+ get = resolver(this, locals, _extends({}, invokeOptions, {
1995
+ options
1996
+ }));
1917
1997
  catchError(() => Object.defineProperty(instance, propertyKey, {
1918
- get: () => onGet(bean)
1998
+ get
1919
1999
  }));
1920
2000
  }
1921
2001
  /**
@@ -2022,6 +2102,22 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
2022
2102
  return cb();
2023
2103
  }
2024
2104
 
2105
+ async lazyInvoke(token) {
2106
+ let instance = this.getInstance(token);
2107
+
2108
+ if (!instance) {
2109
+ var _instance;
2110
+
2111
+ instance = await this.invoke(token);
2112
+
2113
+ if (isFunction((_instance = instance) == null ? void 0 : _instance.$onInit)) {
2114
+ await instance.$onInit();
2115
+ }
2116
+ }
2117
+
2118
+ return instance;
2119
+ }
2120
+
2025
2121
  ensureProvider(token) {
2026
2122
  if (!this.hasProvider(token) && GlobalProviders.has(token)) {
2027
2123
  this.addProvider(token);
@@ -2029,6 +2125,12 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
2029
2125
 
2030
2126
  return this.getProvider(token);
2031
2127
  }
2128
+
2129
+ getInstance(token) {
2130
+ var _super$get;
2131
+
2132
+ return (_super$get = super.get(getClassOrSymbol(token))) == null ? void 0 : _super$get.instance;
2133
+ }
2032
2134
  /**
2033
2135
  * Invoke a class method and inject service.
2034
2136
  *
@@ -2317,5 +2419,5 @@ function getConfiguration(module, configuration = {}) {
2317
2419
  return config;
2318
2420
  }
2319
2421
 
2320
- 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, LocalsContainer, Module, Opts, OverrideProvider, Provider, ProviderScope, ProviderType, Scope, Service, UndefinedTokenError, UseOpts, Value, createContainer, getConfiguration, registerController, registerFactory, registerProvider, registerValue, setLoggerLevel };
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 };
2321
2423
  //# sourceMappingURL=index.modern.js.map