@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.
@@ -6,31 +6,22 @@
6
6
  *
7
7
  * @Injectable()
8
8
  * export class MyService {
9
- * @LazyInject("@tsed/platform-exceptions") // work if the module have a class exported with "default"
10
- * exceptions: Promise<PlatformException>;
11
- * }
12
- *
13
- * @Injectable()
14
- * export class MyService {
15
- * @LazyInject(() => import("@tsed/platform-exceptions")) // use this notation is you use webpack
16
- * exceptions: Promise<PlatformException>;
17
- * }
18
- *
19
- * @Injectable()
20
- * export class MyService {
21
- * @LazyInject(async () => (await import("@tsed/platform-exceptions")).PlatformException)
9
+ * @LazyInject("PlatformException", () => import("@tsed/platform-exceptions"))
22
10
  * exceptions: Promise<PlatformException>;
23
11
  * }
24
12
  * ```
25
13
  *
14
+ * @param key
26
15
  * @param packageName
27
16
  * @param resolver
28
17
  * @param optional
29
18
  * @returns {Function}
30
19
  * @decorator
31
20
  */
32
- export declare function LazyInject(resolver: () => any, optional?: boolean): PropertyDecorator;
33
- export declare function LazyInject(packageName: string, optional?: boolean): PropertyDecorator;
21
+ export declare function LazyInject(key: string, resolver: () => any, { optional, packageName }?: {
22
+ optional?: boolean;
23
+ packageName?: string;
24
+ }): PropertyDecorator;
34
25
  /**
35
26
  * Optionally Lazy load a provider from his package and invoke only when the provider is used
36
27
  *
@@ -44,10 +35,9 @@ export declare function LazyInject(packageName: string, optional?: boolean): Pro
44
35
  * }
45
36
  * ```
46
37
  *
47
- * @param packageName
38
+ * @param key
48
39
  * @param resolver
49
40
  * @returns {Function}
50
41
  * @decorator
51
42
  */
52
- export declare function OptionalLazyInject(resolver: () => any): PropertyDecorator;
53
- export declare function OptionalLazyInject(packageName: string): PropertyDecorator;
43
+ export declare function OptionalLazyInject(key: string, resolver: () => any): PropertyDecorator;
package/lib/index.js CHANGED
@@ -3,26 +3,6 @@ var logger = require('@tsed/logger');
3
3
  var schema = require('@tsed/schema');
4
4
  var tslib = require('tslib');
5
5
 
6
- function _interopNamespace(e) {
7
- if (e && e.__esModule) return e;
8
- var n = Object.create(null);
9
- if (e) {
10
- Object.keys(e).forEach(function (k) {
11
- if (k !== 'default') {
12
- var d = Object.getOwnPropertyDescriptor(e, k);
13
- Object.defineProperty(n, k, d.get ? d : {
14
- enumerable: true,
15
- get: function () {
16
- return e[k];
17
- }
18
- });
19
- }
20
- });
21
- }
22
- n['default'] = e;
23
- return n;
24
- }
25
-
26
6
  const INJECTABLE_PROP = "DI:INJECTABLE_PROP";
27
7
  const DI_PARAMS = "DI:PARAMS";
28
8
  const DI_PARAM_OPTIONS = "DI:PARAM:OPTIONS";
@@ -1003,54 +983,47 @@ function Inject(symbol, onGet = bean => bean) {
1003
983
  };
1004
984
  }
1005
985
 
1006
- function mapOptions$1(args) {
1007
- const options = args.reduce((options, item) => {
1008
- if (core.isString(item)) {
1009
- options.packageName = item;
1010
- } else if (core.isFunction(item)) {
1011
- options.resolver = item;
1012
- } else if (core.isBoolean(item)) {
1013
- options.optional = item;
1014
- }
1015
-
1016
- return options;
1017
- }, {
1018
- optional: false
1019
- });
1020
-
1021
- if (!options.packageName && options.resolver) {
1022
- options.packageName = options.resolver.toString();
1023
- }
1024
-
1025
- if (!options.resolver) {
1026
- options.resolver = () => Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(options.packageName)); });
1027
- }
1028
-
1029
- return options;
1030
- }
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
+ */
1031
1006
 
1032
- function LazyInject(...args) {
1033
- const {
1034
- packageName,
1035
- resolver,
1036
- optional
1037
- } = mapOptions$1(args);
1007
+ function LazyInject(key, resolver, {
1008
+ optional = false,
1009
+ packageName = resolver.toString()
1010
+ } = {}) {
1038
1011
  return (target, propertyKey) => {
1039
1012
  let bean, token;
1040
1013
  injectProperty(target, propertyKey, {
1041
1014
  resolver(injector) {
1042
1015
  return async () => {
1043
1016
  if (!token) {
1044
- var _token;
1045
-
1046
- token = await core.importPackage(packageName, resolver, optional);
1047
- token = ((_token = token) == null ? void 0 : _token.default) || token;
1017
+ const exports = await core.importPackage(packageName, resolver, optional);
1018
+ token = exports[key];
1048
1019
 
1049
- if (token && core.classOf(token) === Object && !optional) {
1050
- 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
+ }
1051
1024
  }
1052
1025
 
1053
- bean = await injector.lazyInvoke(token);
1026
+ bean = token ? await injector.lazyInvoke(token) : {};
1054
1027
  }
1055
1028
 
1056
1029
  return bean;
@@ -1060,8 +1033,29 @@ function LazyInject(...args) {
1060
1033
  });
1061
1034
  };
1062
1035
  }
1063
- function OptionalLazyInject(arg) {
1064
- 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
+ });
1065
1059
  }
1066
1060
 
1067
1061
  /**