@tsed/di 7.0.0-alpha.1 → 7.0.0-beta.3

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.
Files changed (282) hide show
  1. package/lib/cjs/constants/index.js +7 -0
  2. package/lib/cjs/constants/index.js.map +1 -0
  3. package/lib/cjs/decorators/configuration.js +24 -0
  4. package/lib/cjs/decorators/configuration.js.map +1 -0
  5. package/lib/cjs/decorators/constant.js +55 -0
  6. package/lib/cjs/decorators/constant.js.map +1 -0
  7. package/lib/cjs/decorators/controller.js +53 -0
  8. package/lib/cjs/decorators/controller.js.map +1 -0
  9. package/lib/cjs/decorators/index.js +19 -0
  10. package/lib/cjs/decorators/index.js.map +1 -0
  11. package/lib/cjs/decorators/inject.js +79 -0
  12. package/lib/cjs/decorators/inject.js.map +1 -0
  13. package/lib/cjs/decorators/injectable.js +34 -0
  14. package/lib/cjs/decorators/injectable.js.map +1 -0
  15. package/lib/cjs/decorators/intercept.js +28 -0
  16. package/lib/cjs/decorators/intercept.js.map +1 -0
  17. package/lib/cjs/decorators/interceptor.js +21 -0
  18. package/lib/cjs/decorators/interceptor.js.map +1 -0
  19. package/lib/cjs/decorators/lazyInject.js +71 -0
  20. package/lib/cjs/decorators/lazyInject.js.map +1 -0
  21. package/lib/cjs/decorators/module.js +31 -0
  22. package/lib/cjs/decorators/module.js.map +1 -0
  23. package/lib/cjs/decorators/opts.js +54 -0
  24. package/lib/cjs/decorators/opts.js.map +1 -0
  25. package/lib/cjs/decorators/overrideProvider.js +17 -0
  26. package/lib/cjs/decorators/overrideProvider.js.map +1 -0
  27. package/lib/cjs/decorators/scope.js +16 -0
  28. package/lib/cjs/decorators/scope.js.map +1 -0
  29. package/lib/cjs/decorators/service.js +18 -0
  30. package/lib/cjs/decorators/service.js.map +1 -0
  31. package/lib/cjs/decorators/useOpts.js +65 -0
  32. package/lib/cjs/decorators/useOpts.js.map +1 -0
  33. package/lib/cjs/decorators/value.js +55 -0
  34. package/lib/cjs/decorators/value.js.map +1 -0
  35. package/lib/cjs/domain/Container.js +72 -0
  36. package/lib/cjs/domain/Container.js.map +1 -0
  37. package/lib/cjs/domain/ContextLogger.js +85 -0
  38. package/lib/cjs/domain/ContextLogger.js.map +1 -0
  39. package/lib/cjs/domain/DIContext.js +45 -0
  40. package/lib/cjs/domain/DIContext.js.map +1 -0
  41. package/lib/cjs/domain/InjectablePropertyType.js +12 -0
  42. package/lib/cjs/domain/InjectablePropertyType.js.map +1 -0
  43. package/lib/cjs/domain/LocalsContainer.js +55 -0
  44. package/lib/cjs/domain/LocalsContainer.js.map +1 -0
  45. package/lib/cjs/domain/Provider.js +122 -0
  46. package/lib/cjs/domain/Provider.js.map +1 -0
  47. package/lib/cjs/domain/ProviderScope.js +10 -0
  48. package/lib/cjs/domain/ProviderScope.js.map +1 -0
  49. package/lib/cjs/domain/ProviderType.js +15 -0
  50. package/lib/cjs/domain/ProviderType.js.map +1 -0
  51. package/lib/cjs/domain/index.js +12 -0
  52. package/lib/cjs/domain/index.js.map +1 -0
  53. package/lib/cjs/errors/InjectionError.js +58 -0
  54. package/lib/cjs/errors/InjectionError.js.map +1 -0
  55. package/lib/cjs/errors/UndefinedTokenError.js +11 -0
  56. package/lib/cjs/errors/UndefinedTokenError.js.map +1 -0
  57. package/lib/cjs/index.js +21 -0
  58. package/lib/cjs/index.js.map +1 -0
  59. package/lib/cjs/interfaces/DIConfigurationOptions.js +3 -0
  60. package/lib/cjs/interfaces/DIConfigurationOptions.js.map +1 -0
  61. package/lib/cjs/interfaces/DILogger.js +3 -0
  62. package/lib/cjs/interfaces/DILogger.js.map +1 -0
  63. package/lib/cjs/interfaces/DIResolver.js +3 -0
  64. package/lib/cjs/interfaces/DIResolver.js.map +1 -0
  65. package/lib/cjs/interfaces/IProvider.js +3 -0
  66. package/lib/cjs/interfaces/IProvider.js.map +1 -0
  67. package/lib/cjs/interfaces/InjectableProperties.js +3 -0
  68. package/lib/cjs/interfaces/InjectableProperties.js.map +1 -0
  69. package/lib/cjs/interfaces/InterceptorContext.js +3 -0
  70. package/lib/cjs/interfaces/InterceptorContext.js.map +1 -0
  71. package/lib/cjs/interfaces/InterceptorMethods.js +3 -0
  72. package/lib/cjs/interfaces/InterceptorMethods.js.map +1 -0
  73. package/lib/cjs/interfaces/InvokeOptions.js +3 -0
  74. package/lib/cjs/interfaces/InvokeOptions.js.map +1 -0
  75. package/lib/cjs/interfaces/OnDestroy.js +3 -0
  76. package/lib/cjs/interfaces/OnDestroy.js.map +1 -0
  77. package/lib/cjs/interfaces/OnInit.js +3 -0
  78. package/lib/cjs/interfaces/OnInit.js.map +1 -0
  79. package/lib/cjs/interfaces/RegistrySettings.js +3 -0
  80. package/lib/cjs/interfaces/RegistrySettings.js.map +1 -0
  81. package/lib/cjs/interfaces/ResolvedInvokeOptions.js +3 -0
  82. package/lib/cjs/interfaces/ResolvedInvokeOptions.js.map +1 -0
  83. package/lib/cjs/interfaces/TokenProvider.js +3 -0
  84. package/lib/cjs/interfaces/TokenProvider.js.map +1 -0
  85. package/lib/cjs/interfaces/index.js +18 -0
  86. package/lib/cjs/interfaces/index.js.map +1 -0
  87. package/lib/cjs/registries/GlobalProviders.js +124 -0
  88. package/lib/cjs/registries/GlobalProviders.js.map +1 -0
  89. package/lib/cjs/registries/ProviderRegistry.js +85 -0
  90. package/lib/cjs/registries/ProviderRegistry.js.map +1 -0
  91. package/lib/cjs/services/DIConfiguration.js +129 -0
  92. package/lib/cjs/services/DIConfiguration.js.map +1 -0
  93. package/lib/cjs/services/DILogger.js +13 -0
  94. package/lib/cjs/services/DILogger.js.map +1 -0
  95. package/lib/cjs/services/DITest.js +108 -0
  96. package/lib/cjs/services/DITest.js.map +1 -0
  97. package/lib/cjs/services/InjectorService.js +566 -0
  98. package/lib/cjs/services/InjectorService.js.map +1 -0
  99. package/lib/cjs/utils/colors.js +10 -0
  100. package/lib/cjs/utils/colors.js.map +1 -0
  101. package/lib/cjs/utils/createContainer.js +14 -0
  102. package/lib/cjs/utils/createContainer.js.map +1 -0
  103. package/lib/cjs/utils/data/Test1.js +8 -0
  104. package/lib/cjs/utils/data/Test1.js.map +1 -0
  105. package/lib/cjs/utils/data/Test2.js +8 -0
  106. package/lib/cjs/utils/data/Test2.js.map +1 -0
  107. package/lib/cjs/utils/getConfiguration.js +20 -0
  108. package/lib/cjs/utils/getConfiguration.js.map +1 -0
  109. package/lib/cjs/utils/setLoggerLevel.js +16 -0
  110. package/lib/cjs/utils/setLoggerLevel.js.map +1 -0
  111. package/lib/esm/constants/index.js +4 -0
  112. package/lib/esm/constants/index.js.map +1 -0
  113. package/lib/esm/decorators/configuration.js +20 -0
  114. package/lib/esm/decorators/configuration.js.map +1 -0
  115. package/lib/esm/decorators/constant.js +51 -0
  116. package/lib/esm/decorators/constant.js.map +1 -0
  117. package/lib/esm/decorators/controller.js +49 -0
  118. package/lib/esm/decorators/controller.js.map +1 -0
  119. package/lib/esm/decorators/index.js +16 -0
  120. package/lib/esm/decorators/index.js.map +1 -0
  121. package/lib/esm/decorators/inject.js +74 -0
  122. package/lib/esm/decorators/inject.js.map +1 -0
  123. package/lib/esm/decorators/injectable.js +30 -0
  124. package/lib/esm/decorators/injectable.js.map +1 -0
  125. package/lib/esm/decorators/intercept.js +24 -0
  126. package/lib/esm/decorators/intercept.js.map +1 -0
  127. package/lib/esm/decorators/interceptor.js +17 -0
  128. package/lib/esm/decorators/interceptor.js.map +1 -0
  129. package/lib/esm/decorators/lazyInject.js +66 -0
  130. package/lib/esm/decorators/lazyInject.js.map +1 -0
  131. package/lib/esm/decorators/module.js +27 -0
  132. package/lib/esm/decorators/module.js.map +1 -0
  133. package/lib/esm/decorators/opts.js +50 -0
  134. package/lib/esm/decorators/opts.js.map +1 -0
  135. package/lib/esm/decorators/overrideProvider.js +13 -0
  136. package/lib/esm/decorators/overrideProvider.js.map +1 -0
  137. package/lib/esm/decorators/scope.js +12 -0
  138. package/lib/esm/decorators/scope.js.map +1 -0
  139. package/lib/esm/decorators/service.js +14 -0
  140. package/lib/esm/decorators/service.js.map +1 -0
  141. package/lib/esm/decorators/useOpts.js +61 -0
  142. package/lib/esm/decorators/useOpts.js.map +1 -0
  143. package/lib/esm/decorators/value.js +51 -0
  144. package/lib/esm/decorators/value.js.map +1 -0
  145. package/lib/esm/domain/Container.js +68 -0
  146. package/lib/esm/domain/Container.js.map +1 -0
  147. package/lib/esm/domain/ContextLogger.js +81 -0
  148. package/lib/esm/domain/ContextLogger.js.map +1 -0
  149. package/lib/esm/domain/DIContext.js +41 -0
  150. package/lib/esm/domain/DIContext.js.map +1 -0
  151. package/lib/esm/domain/InjectablePropertyType.js +9 -0
  152. package/lib/esm/domain/InjectablePropertyType.js.map +1 -0
  153. package/lib/esm/domain/LocalsContainer.js +51 -0
  154. package/lib/esm/domain/LocalsContainer.js.map +1 -0
  155. package/lib/esm/domain/Provider.js +118 -0
  156. package/lib/esm/domain/Provider.js.map +1 -0
  157. package/lib/esm/domain/ProviderScope.js +7 -0
  158. package/lib/esm/domain/ProviderScope.js.map +1 -0
  159. package/lib/esm/domain/ProviderType.js +12 -0
  160. package/lib/esm/domain/ProviderType.js.map +1 -0
  161. package/lib/esm/domain/index.js +9 -0
  162. package/lib/esm/domain/index.js.map +1 -0
  163. package/lib/esm/errors/InjectionError.js +54 -0
  164. package/lib/esm/errors/InjectionError.js.map +1 -0
  165. package/lib/esm/errors/UndefinedTokenError.js +7 -0
  166. package/lib/esm/errors/UndefinedTokenError.js.map +1 -0
  167. package/lib/esm/index.js +18 -0
  168. package/lib/esm/index.js.map +1 -0
  169. package/lib/esm/interfaces/DIConfigurationOptions.js +2 -0
  170. package/lib/esm/interfaces/DIConfigurationOptions.js.map +1 -0
  171. package/lib/esm/interfaces/DILogger.js +2 -0
  172. package/lib/esm/interfaces/DILogger.js.map +1 -0
  173. package/lib/esm/interfaces/DIResolver.js +2 -0
  174. package/lib/esm/interfaces/DIResolver.js.map +1 -0
  175. package/lib/esm/interfaces/IProvider.js +2 -0
  176. package/lib/esm/interfaces/IProvider.js.map +1 -0
  177. package/lib/esm/interfaces/InjectableProperties.js +2 -0
  178. package/lib/esm/interfaces/InjectableProperties.js.map +1 -0
  179. package/lib/esm/interfaces/InterceptorContext.js +2 -0
  180. package/lib/esm/interfaces/InterceptorContext.js.map +1 -0
  181. package/lib/esm/interfaces/InterceptorMethods.js +2 -0
  182. package/lib/esm/interfaces/InterceptorMethods.js.map +1 -0
  183. package/lib/esm/interfaces/InvokeOptions.js +2 -0
  184. package/lib/esm/interfaces/InvokeOptions.js.map +1 -0
  185. package/lib/esm/interfaces/OnDestroy.js +2 -0
  186. package/lib/esm/interfaces/OnDestroy.js.map +1 -0
  187. package/lib/esm/interfaces/OnInit.js +2 -0
  188. package/lib/esm/interfaces/OnInit.js.map +1 -0
  189. package/lib/esm/interfaces/RegistrySettings.js +2 -0
  190. package/lib/esm/interfaces/RegistrySettings.js.map +1 -0
  191. package/lib/esm/interfaces/ResolvedInvokeOptions.js +2 -0
  192. package/lib/esm/interfaces/ResolvedInvokeOptions.js.map +1 -0
  193. package/lib/esm/interfaces/TokenProvider.js +2 -0
  194. package/lib/esm/interfaces/TokenProvider.js.map +1 -0
  195. package/lib/esm/interfaces/index.js +15 -0
  196. package/lib/esm/interfaces/index.js.map +1 -0
  197. package/lib/esm/registries/GlobalProviders.js +120 -0
  198. package/lib/esm/registries/GlobalProviders.js.map +1 -0
  199. package/lib/esm/registries/ProviderRegistry.js +80 -0
  200. package/lib/esm/registries/ProviderRegistry.js.map +1 -0
  201. package/lib/esm/services/DIConfiguration.js +125 -0
  202. package/lib/esm/services/DIConfiguration.js.map +1 -0
  203. package/lib/esm/services/DILogger.js +11 -0
  204. package/lib/esm/services/DILogger.js.map +1 -0
  205. package/lib/esm/services/DITest.js +104 -0
  206. package/lib/esm/services/DITest.js.map +1 -0
  207. package/lib/esm/services/InjectorService.js +563 -0
  208. package/lib/esm/services/InjectorService.js.map +1 -0
  209. package/lib/esm/utils/colors.js +7 -0
  210. package/lib/esm/utils/colors.js.map +1 -0
  211. package/lib/esm/utils/createContainer.js +10 -0
  212. package/lib/esm/utils/createContainer.js.map +1 -0
  213. package/lib/esm/utils/data/Test1.js +4 -0
  214. package/lib/esm/utils/data/Test1.js.map +1 -0
  215. package/lib/esm/utils/data/Test2.js +4 -0
  216. package/lib/esm/utils/data/Test2.js.map +1 -0
  217. package/lib/esm/utils/getConfiguration.js +16 -0
  218. package/lib/esm/utils/getConfiguration.js.map +1 -0
  219. package/lib/esm/utils/setLoggerLevel.js +12 -0
  220. package/lib/esm/utils/setLoggerLevel.js.map +1 -0
  221. package/lib/{constants → types/constants}/index.d.ts +0 -0
  222. package/lib/{decorators → types/decorators}/configuration.d.ts +0 -0
  223. package/lib/{decorators → types/decorators}/constant.d.ts +0 -0
  224. package/lib/types/decorators/controller.d.ts +43 -0
  225. package/lib/{decorators → types/decorators}/index.d.ts +2 -0
  226. package/lib/{decorators → types/decorators}/inject.d.ts +2 -0
  227. package/lib/{decorators → types/decorators}/injectable.d.ts +2 -1
  228. package/lib/{decorators → types/decorators}/intercept.d.ts +0 -0
  229. package/lib/{decorators → types/decorators}/interceptor.d.ts +0 -0
  230. package/lib/types/decorators/lazyInject.d.ts +43 -0
  231. package/lib/{decorators → types/decorators}/module.d.ts +0 -0
  232. package/lib/{decorators → types/decorators}/opts.d.ts +0 -0
  233. package/lib/{decorators → types/decorators}/overrideProvider.d.ts +0 -0
  234. package/lib/{decorators → types/decorators}/scope.d.ts +0 -0
  235. package/lib/{decorators → types/decorators}/service.d.ts +0 -0
  236. package/lib/{decorators → types/decorators}/useOpts.d.ts +0 -0
  237. package/lib/{decorators → types/decorators}/value.d.ts +0 -0
  238. package/lib/{domain → types/domain}/Container.d.ts +2 -3
  239. package/lib/{domain → types/domain}/ContextLogger.d.ts +5 -1
  240. package/lib/{domain → types/domain}/DIContext.d.ts +6 -7
  241. package/lib/{domain → types/domain}/InjectablePropertyType.d.ts +0 -0
  242. package/lib/{domain → types/domain}/LocalsContainer.d.ts +0 -0
  243. package/lib/{domain → types/domain}/Provider.d.ts +17 -3
  244. package/lib/{domain → types/domain}/ProviderScope.d.ts +0 -0
  245. package/lib/{domain → types/domain}/ProviderType.d.ts +0 -0
  246. package/lib/{domain → types/domain}/index.d.ts +0 -0
  247. package/lib/{errors → types/errors}/InjectionError.d.ts +0 -0
  248. package/lib/{errors → types/errors}/UndefinedTokenError.d.ts +0 -0
  249. package/lib/{index.d.ts → types/index.d.ts} +1 -0
  250. package/lib/{interfaces → types/interfaces}/DIConfigurationOptions.d.ts +0 -0
  251. package/lib/{interfaces → types/interfaces}/DILogger.d.ts +0 -0
  252. package/lib/{interfaces → types/interfaces}/DIResolver.d.ts +0 -0
  253. package/lib/{interfaces → types/interfaces}/IProvider.d.ts +0 -0
  254. package/lib/{interfaces → types/interfaces}/InjectableProperties.d.ts +8 -4
  255. package/lib/{interfaces → types/interfaces}/InterceptorContext.d.ts +0 -0
  256. package/lib/{interfaces → types/interfaces}/InterceptorMethods.d.ts +0 -0
  257. package/lib/{interfaces → types/interfaces}/InvokeOptions.d.ts +0 -0
  258. package/lib/{interfaces → types/interfaces}/OnDestroy.d.ts +0 -0
  259. package/lib/{interfaces → types/interfaces}/OnInit.d.ts +0 -0
  260. package/lib/{interfaces → types/interfaces}/RegistrySettings.d.ts +6 -2
  261. package/lib/types/interfaces/ResolvedInvokeOptions.d.ts +12 -0
  262. package/lib/{interfaces → types/interfaces}/TokenProvider.d.ts +0 -0
  263. package/lib/{interfaces → types/interfaces}/index.d.ts +1 -0
  264. package/lib/{registries → types/registries}/GlobalProviders.d.ts +5 -1
  265. package/lib/{registries → types/registries}/ProviderRegistry.d.ts +0 -0
  266. package/lib/{services → types/services}/DIConfiguration.d.ts +0 -0
  267. package/lib/{services → types/services}/DILogger.d.ts +0 -0
  268. package/lib/{services → types/services}/DITest.d.ts +5 -1
  269. package/lib/{services → types/services}/InjectorService.d.ts +31 -14
  270. package/lib/types/utils/colors.d.ts +5 -0
  271. package/lib/{utils → types/utils}/createContainer.d.ts +0 -0
  272. package/lib/types/utils/data/Test1.d.ts +3 -0
  273. package/lib/types/utils/data/Test2.d.ts +3 -0
  274. package/lib/{utils → types/utils}/getConfiguration.d.ts +0 -0
  275. package/lib/{utils → types/utils}/setLoggerLevel.d.ts +0 -0
  276. package/package.json +15 -11
  277. package/readme.md +1 -1
  278. package/tsconfig.compile.esm.json +24 -0
  279. package/lib/index.js +0 -2271
  280. package/lib/index.js.map +0 -1
  281. package/lib/index.modern.js +0 -2235
  282. package/lib/index.modern.js.map +0 -1
@@ -0,0 +1,104 @@
1
+ import { Env, getValue } from "@tsed/core";
2
+ import { $log } from "@tsed/logger";
3
+ import { createContainer } from "../utils/createContainer";
4
+ import { LocalsContainer } from "../domain/LocalsContainer";
5
+ import { setLoggerLevel } from "../utils/setLoggerLevel";
6
+ import { InjectorService } from "./InjectorService";
7
+ /**
8
+ * Tool to run test with lightweight DI sandbox.
9
+ */
10
+ export class DITest {
11
+ static get injector() {
12
+ if (DITest._injector) {
13
+ return DITest._injector;
14
+ }
15
+ /* istanbul ignore next */
16
+ throw new Error("PlatformTest.injector is not initialized. Use PlatformTest.create(): Promise before PlatformTest.invoke() or PlatformTest.injector.\n" +
17
+ "Example:\n" +
18
+ "before(async () => {\n" +
19
+ " await PlatformTest.create()\n" +
20
+ " await PlatformTest.invoke(MyService, [])\n" +
21
+ "})");
22
+ }
23
+ static set injector(injector) {
24
+ DITest._injector = injector;
25
+ }
26
+ static hasInjector() {
27
+ return !!DITest._injector;
28
+ }
29
+ static async create(settings = {}) {
30
+ DITest.injector = DITest.createInjector(settings);
31
+ await this.createContainer(settings);
32
+ }
33
+ static async createContainer(settings = {}) {
34
+ var _a;
35
+ const container = createContainer();
36
+ (_a = settings.imports) === null || _a === void 0 ? void 0 : _a.forEach(({ token, use }) => {
37
+ container.addProvider(token, {
38
+ useValue: use
39
+ });
40
+ });
41
+ await DITest.injector.load(container);
42
+ }
43
+ /**
44
+ * Create a new injector with the right default services
45
+ */
46
+ static createInjector(settings = {}) {
47
+ const injector = new InjectorService();
48
+ injector.logger = $log;
49
+ // @ts-ignore
50
+ injector.settings.set(DITest.configure(settings));
51
+ setLoggerLevel(injector);
52
+ return injector;
53
+ }
54
+ /**
55
+ * Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
56
+ */
57
+ static async reset() {
58
+ if (DITest.hasInjector()) {
59
+ await DITest.injector.destroy();
60
+ DITest._injector = null;
61
+ }
62
+ }
63
+ /**
64
+ * Invoke a provider and return a fresh instance
65
+ * @param target
66
+ * @param providers
67
+ */
68
+ static invoke(target, providers = []) {
69
+ const locals = new LocalsContainer();
70
+ providers.forEach((p) => {
71
+ locals.set(p.token, p.use);
72
+ });
73
+ locals.set(InjectorService, DITest.injector);
74
+ const instance = DITest.injector.invoke(target, locals, { rebuild: true });
75
+ if (instance && instance.$onInit) {
76
+ // await instance.$onInit();
77
+ const result = instance.$onInit();
78
+ if (result instanceof Promise) {
79
+ return result.then(() => instance);
80
+ }
81
+ }
82
+ return instance;
83
+ }
84
+ /**
85
+ * Return the instance from injector registry
86
+ * @param target
87
+ * @param options
88
+ */
89
+ static get(target, options = {}) {
90
+ return DITest.injector.get(target, options);
91
+ }
92
+ static configure(settings = {}) {
93
+ return {
94
+ ...settings,
95
+ env: getValue(settings, "env", Env.TEST),
96
+ logger: {
97
+ ...getValue(settings, "logger", {}),
98
+ level: getValue(settings, "logger.level", "off")
99
+ }
100
+ };
101
+ }
102
+ }
103
+ DITest._injector = null;
104
+ //# sourceMappingURL=DITest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DITest.js","sourceRoot":"","sources":["../../../src/services/DITest.ts"],"names":[],"mappings":"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","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {$log} from \"@tsed/logger\";\nimport {createContainer} from \"../utils/createContainer\";\nimport {LocalsContainer} from \"../domain/LocalsContainer\";\nimport {OnInit} from \"../interfaces/OnInit\";\nimport {TokenProvider} from \"../interfaces/TokenProvider\";\nimport {setLoggerLevel} from \"../utils/setLoggerLevel\";\nimport {InjectorService} from \"./InjectorService\";\n\nexport interface DITestInvokeOptions {\n token?: TokenProvider;\n use: any;\n}\n\nexport interface DITestOptions extends Partial<TsED.Configuration> {\n imports?: DITestInvokeOptions[];\n}\n\n/**\n * Tool to run test with lightweight DI sandbox.\n */\nexport class DITest {\n protected static _injector: InjectorService | null = null;\n\n static get injector(): InjectorService {\n if (DITest._injector) {\n return DITest._injector!;\n }\n\n /* istanbul ignore next */\n throw new Error(\n \"PlatformTest.injector is not initialized. Use PlatformTest.create(): Promise before PlatformTest.invoke() or PlatformTest.injector.\\n\" +\n \"Example:\\n\" +\n \"before(async () => {\\n\" +\n \" await PlatformTest.create()\\n\" +\n \" await PlatformTest.invoke(MyService, [])\\n\" +\n \"})\"\n );\n }\n\n static set injector(injector: InjectorService) {\n DITest._injector = injector;\n }\n\n static hasInjector() {\n return !!DITest._injector;\n }\n\n static async create(settings: DITestOptions = {}) {\n DITest.injector = DITest.createInjector(settings);\n\n await this.createContainer(settings);\n }\n\n static async createContainer(settings: DITestOptions = {}) {\n const container = createContainer();\n\n settings.imports?.forEach(({token, use}) => {\n container.addProvider(token, {\n useValue: use\n });\n });\n\n await DITest.injector.load(container);\n }\n\n /**\n * Create a new injector with the right default services\n */\n static createInjector(settings: any = {}): InjectorService {\n const injector = new InjectorService();\n injector.logger = $log;\n\n // @ts-ignore\n injector.settings.set(DITest.configure(settings));\n\n setLoggerLevel(injector);\n\n return injector;\n }\n\n /**\n * Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.\n */\n static async reset() {\n if (DITest.hasInjector()) {\n await DITest.injector.destroy();\n DITest._injector = null;\n }\n }\n\n /**\n * Invoke a provider and return a fresh instance\n * @param target\n * @param providers\n */\n static invoke<T = any>(target: TokenProvider, providers: DITestInvokeOptions[] = []): T | Promise<T> {\n const locals = new LocalsContainer();\n providers.forEach((p) => {\n locals.set(p.token, p.use);\n });\n\n locals.set(InjectorService, DITest.injector);\n\n const instance: OnInit = DITest.injector.invoke(target, locals, {rebuild: true});\n\n if (instance && instance.$onInit) {\n // await instance.$onInit();\n const result = instance.$onInit();\n if (result instanceof Promise) {\n return result.then(() => instance as any);\n }\n }\n\n return instance as any;\n }\n\n /**\n * Return the instance from injector registry\n * @param target\n * @param options\n */\n static get<T = any>(target: TokenProvider, options: any = {}): T {\n return DITest.injector.get<T>(target, options)!;\n }\n\n protected static configure(settings: Partial<TsED.Configuration> = {}): Partial<TsED.Configuration> {\n return {\n ...settings,\n env: getValue(settings, \"env\", Env.TEST),\n logger: {\n ...getValue(settings, \"logger\", {}),\n level: getValue(settings, \"logger.level\", \"off\")\n }\n };\n }\n}\n"]}
@@ -0,0 +1,563 @@
1
+ var _InjectorService_cache;
2
+ var InjectorService_1;
3
+ import { __classPrivateFieldGet, __decorate, __metadata } from "tslib";
4
+ import { ancestorsOf, catchError, classOf, deepClone, deepMerge, isClass, isFunction, isInheritedFrom, Metadata, nameOf, prototypeOf, Store } from "@tsed/core";
5
+ import { DI_PARAM_OPTIONS, INJECTABLE_PROP } from "../constants";
6
+ import { Configuration } from "../decorators/configuration";
7
+ import { Injectable } from "../decorators/injectable";
8
+ import { InjectablePropertyType, ProviderScope } from "../domain";
9
+ import { Container } from "../domain/Container";
10
+ import { LocalsContainer } from "../domain/LocalsContainer";
11
+ import { Provider } from "../domain/Provider";
12
+ import { InjectionError } from "../errors/InjectionError";
13
+ import { UndefinedTokenError } from "../errors/UndefinedTokenError";
14
+ import { GlobalProviders } from "../registries/GlobalProviders";
15
+ import { createContainer } from "../utils/createContainer";
16
+ import { DIConfiguration } from "./DIConfiguration";
17
+ /**
18
+ * This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
19
+ *
20
+ * ### Example:
21
+ *
22
+ * ```typescript
23
+ * import {InjectorService} from "@tsed/di";
24
+ *
25
+ * // Import the services (all services are decorated with @Service()";
26
+ * import MyService1 from "./services/service1";
27
+ * import MyService2 from "./services/service2";
28
+ * import MyService3 from "./services/service3";
29
+ *
30
+ * // When all services is imported you can load InjectorService.
31
+ * const injector = new InjectorService()
32
+ *
33
+ * await injector.load();
34
+ *
35
+ * const myService1 = injector.get<MyService1>(MyServcice1);
36
+ * ```
37
+ */
38
+ let InjectorService = InjectorService_1 = class InjectorService extends Container {
39
+ constructor() {
40
+ super();
41
+ this.settings = new DIConfiguration();
42
+ this.logger = console;
43
+ this.resolvedConfiguration = false;
44
+ _InjectorService_cache.set(this, new LocalsContainer());
45
+ __classPrivateFieldGet(this, _InjectorService_cache, "f").set(InjectorService_1, this);
46
+ }
47
+ get resolvers() {
48
+ return this.settings.resolvers;
49
+ }
50
+ get scopes() {
51
+ return this.settings.scopes || {};
52
+ }
53
+ /**
54
+ * Retrieve default scope for a given provider.
55
+ * @param provider
56
+ */
57
+ scopeOf(provider) {
58
+ return provider.scope || this.scopes[provider.type] || ProviderScope.SINGLETON;
59
+ }
60
+ /**
61
+ * Return a list of instance build by the injector.
62
+ */
63
+ toArray() {
64
+ return __classPrivateFieldGet(this, _InjectorService_cache, "f").toArray();
65
+ }
66
+ /**
67
+ * Get a service or factory already constructed from his symbol or class.
68
+ *
69
+ * #### Example
70
+ *
71
+ * ```typescript
72
+ * import {InjectorService} from "@tsed/di";
73
+ * import MyService from "./services";
74
+ *
75
+ * class OtherService {
76
+ * constructor(injectorService: InjectorService) {
77
+ * const myService = injectorService.get<MyService>(MyService);
78
+ * }
79
+ * }
80
+ * ```
81
+ *
82
+ * @param token The class or symbol registered in InjectorService.
83
+ * @param options
84
+ * @returns {boolean}
85
+ */
86
+ get(token, options = {}) {
87
+ const instance = this.getInstance(token);
88
+ if (instance !== undefined) {
89
+ return instance;
90
+ }
91
+ if (!this.hasProvider(token)) {
92
+ for (const resolver of this.resolvers) {
93
+ const result = resolver.get(token, options);
94
+ if (result !== undefined) {
95
+ return result;
96
+ }
97
+ }
98
+ }
99
+ }
100
+ /**
101
+ * The has() method returns a boolean indicating whether an element with the specified key exists or not.
102
+ * @returns {boolean}
103
+ * @param token
104
+ */
105
+ has(token) {
106
+ return __classPrivateFieldGet(this, _InjectorService_cache, "f").get(token) !== undefined;
107
+ }
108
+ /**
109
+ * Invoke the class and inject all services that required by the class constructor.
110
+ *
111
+ * #### Example
112
+ *
113
+ * ```typescript
114
+ * import {InjectorService} from "@tsed/di";
115
+ * import MyService from "./services";
116
+ *
117
+ * class OtherService {
118
+ * constructor(injectorService: InjectorService) {
119
+ * const myService = injectorService.invoke<MyService>(MyService);
120
+ * }
121
+ * }
122
+ * ```
123
+ *
124
+ * @param token The injectable class to invoke. Class parameters are injected according constructor signature.
125
+ * @param locals Optional object. If preset then any argument Class are read from this object first, before the `InjectorService` is consulted.
126
+ * @param options
127
+ * @returns {T} The class constructed.
128
+ */
129
+ invoke(token, locals = new LocalsContainer(), options = {}) {
130
+ const provider = this.ensureProvider(token);
131
+ let instance;
132
+ !locals.has(Configuration) && locals.set(Configuration, this.settings);
133
+ if (locals.has(token)) {
134
+ return locals.get(token);
135
+ }
136
+ if (token === DI_PARAM_OPTIONS) {
137
+ return {};
138
+ }
139
+ if (!provider || options.rebuild) {
140
+ instance = this.resolve(token, locals, options);
141
+ if (this.hasProvider(token)) {
142
+ __classPrivateFieldGet(this, _InjectorService_cache, "f").set(token, instance);
143
+ }
144
+ return instance;
145
+ }
146
+ switch (this.scopeOf(provider)) {
147
+ case ProviderScope.SINGLETON:
148
+ if (!this.has(token)) {
149
+ __classPrivateFieldGet(this, _InjectorService_cache, "f").set(token, this.resolve(token, locals, options));
150
+ if (provider.isAsync()) {
151
+ __classPrivateFieldGet(this, _InjectorService_cache, "f").get(token).then((instance) => {
152
+ __classPrivateFieldGet(this, _InjectorService_cache, "f").set(token, instance);
153
+ });
154
+ }
155
+ }
156
+ instance = this.get(token);
157
+ break;
158
+ case ProviderScope.REQUEST:
159
+ instance = this.resolve(token, locals, options);
160
+ locals.set(token, instance);
161
+ break;
162
+ case ProviderScope.INSTANCE:
163
+ instance = this.resolve(provider.provide, locals, options);
164
+ break;
165
+ }
166
+ return instance;
167
+ }
168
+ /**
169
+ * Build only providers which are asynchronous.
170
+ */
171
+ async loadAsync(locals = new LocalsContainer()) {
172
+ for (const [, provider] of this) {
173
+ if (!locals.has(provider.token)) {
174
+ if (provider.isAsync()) {
175
+ await this.invoke(provider.token, locals);
176
+ }
177
+ const instance = __classPrivateFieldGet(this, _InjectorService_cache, "f").get(provider.token);
178
+ if (instance !== undefined) {
179
+ locals.set(provider.token, instance);
180
+ }
181
+ }
182
+ }
183
+ return locals;
184
+ }
185
+ loadSync(locals = new LocalsContainer()) {
186
+ for (const [, provider] of this) {
187
+ if (!locals.has(provider.token) && this.scopeOf(provider) === ProviderScope.SINGLETON) {
188
+ this.invoke(provider.token, locals);
189
+ }
190
+ const instance = __classPrivateFieldGet(this, _InjectorService_cache, "f").get(provider.token);
191
+ if (instance !== undefined) {
192
+ locals.set(provider.token, instance);
193
+ }
194
+ }
195
+ return locals;
196
+ }
197
+ /**
198
+ * Boostrap injector from container and resolve configuration.
199
+ *
200
+ * @param container
201
+ */
202
+ bootstrap(container = createContainer()) {
203
+ // Clone all providers in the container
204
+ this.addProviders(container);
205
+ // Resolve all configuration
206
+ this.resolveConfiguration();
207
+ return this;
208
+ }
209
+ /**
210
+ * Build all providers from given container (or GlobalProviders) and emit `$onInit` event.
211
+ *
212
+ * @param container
213
+ * @param rootModule
214
+ */
215
+ async load(container = createContainer(), rootModule) {
216
+ this.bootstrap(container);
217
+ // build async and sync provider
218
+ let locals = await this.loadAsync();
219
+ if (rootModule) {
220
+ await this.invoke(rootModule);
221
+ }
222
+ // load sync provider
223
+ locals = this.loadSync(locals);
224
+ await locals.emit("$beforeInit");
225
+ await locals.emit("$onInit");
226
+ return locals;
227
+ }
228
+ /**
229
+ * Load all configurations registered on providers
230
+ */
231
+ resolveConfiguration() {
232
+ if (this.resolvedConfiguration) {
233
+ return;
234
+ }
235
+ const mergedConfiguration = new Map();
236
+ super.forEach((provider) => {
237
+ if (provider.configuration && provider.type !== "server:module") {
238
+ Object.entries(provider.configuration).forEach(([key, value]) => {
239
+ value = mergedConfiguration.has(key) ? deepMerge(mergedConfiguration.get(key), value) : deepClone(value);
240
+ mergedConfiguration.set(key, value);
241
+ });
242
+ }
243
+ if (provider.resolvers) {
244
+ this.resolvers.push(...provider.resolvers);
245
+ }
246
+ });
247
+ mergedConfiguration.forEach((value, key) => {
248
+ this.settings[key] = deepMerge(value, this.settings[key]);
249
+ });
250
+ this.settings.build();
251
+ this.resolvedConfiguration = true;
252
+ }
253
+ /**
254
+ *
255
+ * @param instance
256
+ * @param locals
257
+ * @param options
258
+ */
259
+ bindInjectableProperties(instance, locals, options) {
260
+ const properties = ancestorsOf(classOf(instance)).reduce((properties, target) => {
261
+ const store = Store.from(target);
262
+ return {
263
+ ...properties,
264
+ ...(store.get(INJECTABLE_PROP) || {})
265
+ };
266
+ }, {});
267
+ Object.values(properties).forEach((definition) => {
268
+ switch (definition.bindingType) {
269
+ case InjectablePropertyType.METHOD:
270
+ this.bindMethod(instance, definition);
271
+ break;
272
+ case InjectablePropertyType.PROPERTY:
273
+ this.bindProperty(instance, definition, locals, options);
274
+ break;
275
+ case InjectablePropertyType.CONSTANT:
276
+ this.bindConstant(instance, definition);
277
+ break;
278
+ case InjectablePropertyType.VALUE:
279
+ this.bindValue(instance, definition);
280
+ break;
281
+ case InjectablePropertyType.INTERCEPTOR:
282
+ this.bindInterceptor(instance, definition);
283
+ break;
284
+ }
285
+ });
286
+ }
287
+ /**
288
+ *
289
+ * @param instance
290
+ * @param {string} propertyKey
291
+ */
292
+ bindMethod(instance, { propertyKey }) {
293
+ const target = classOf(instance);
294
+ const originalMethod = instance[propertyKey];
295
+ const deps = Metadata.getParamTypes(prototypeOf(target), propertyKey);
296
+ instance[propertyKey] = () => {
297
+ const services = deps.map((dependency) => this.get(dependency));
298
+ return originalMethod.call(instance, ...services);
299
+ };
300
+ }
301
+ /**
302
+ * Create an injectable property.
303
+ *
304
+ * @param instance
305
+ * @param {string} propertyKey
306
+ * @param {any} useType
307
+ * @param resolver
308
+ * @param options
309
+ * @param locals
310
+ * @param invokeOptions
311
+ */
312
+ bindProperty(instance, { propertyKey, resolver, options = {} }, locals, invokeOptions) {
313
+ let get;
314
+ get = resolver(this, locals, { ...invokeOptions, options });
315
+ catchError(() => Object.defineProperty(instance, propertyKey, {
316
+ get
317
+ }));
318
+ }
319
+ /**
320
+ *
321
+ * @param instance
322
+ * @param {string} propertyKey
323
+ * @param {any} useType
324
+ */
325
+ bindValue(instance, { propertyKey, expression, defaultValue }) {
326
+ const descriptor = {
327
+ get: () => this.settings.get(expression) || defaultValue,
328
+ set: (value) => this.settings.set(expression, value),
329
+ enumerable: true,
330
+ configurable: true
331
+ };
332
+ catchError(() => Object.defineProperty(instance, propertyKey, descriptor));
333
+ }
334
+ /**
335
+ *
336
+ * @param instance
337
+ * @param {string} propertyKey
338
+ * @param {any} useType
339
+ */
340
+ bindConstant(instance, { propertyKey, expression, defaultValue }) {
341
+ let bean;
342
+ const get = () => {
343
+ if (bean !== undefined) {
344
+ return bean;
345
+ }
346
+ const value = this.settings.get(expression, defaultValue);
347
+ bean = Object.freeze(deepClone(value));
348
+ return bean;
349
+ };
350
+ const descriptor = {
351
+ get,
352
+ enumerable: true,
353
+ configurable: true
354
+ };
355
+ catchError(() => Object.defineProperty(instance, propertyKey, descriptor));
356
+ }
357
+ /**
358
+ *
359
+ * @param instance
360
+ * @param propertyKey
361
+ * @param useType
362
+ * @param options
363
+ */
364
+ bindInterceptor(instance, { propertyKey, useType, options }) {
365
+ const target = classOf(instance);
366
+ const originalMethod = instance[propertyKey];
367
+ instance[propertyKey] = (...args) => {
368
+ const next = (err) => {
369
+ if (!err) {
370
+ return originalMethod.apply(instance, args);
371
+ }
372
+ throw err;
373
+ };
374
+ const context = {
375
+ target,
376
+ propertyKey,
377
+ args,
378
+ options,
379
+ next
380
+ };
381
+ const interceptor = this.get(useType);
382
+ return interceptor.intercept({
383
+ ...context,
384
+ options
385
+ }, next);
386
+ };
387
+ }
388
+ /**
389
+ * Allow handler hack for AsyncHookContext plugin.
390
+ * @param ctx
391
+ * @param cb
392
+ * @protected
393
+ */
394
+ runInContext(ctx, cb) {
395
+ return cb();
396
+ }
397
+ async lazyInvoke(token) {
398
+ let instance = this.getInstance(token);
399
+ if (!instance) {
400
+ instance = await this.invoke(token);
401
+ if (isFunction(instance === null || instance === void 0 ? void 0 : instance.$onInit)) {
402
+ await instance.$onInit();
403
+ }
404
+ }
405
+ return instance;
406
+ }
407
+ /**
408
+ * Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
409
+ * @param eventName The event name to emit at all services.
410
+ * @param args List of the parameters to give to each services.
411
+ * @returns {Promise<any[]>} A list of promises.
412
+ */
413
+ async emit(eventName, ...args) {
414
+ return __classPrivateFieldGet(this, _InjectorService_cache, "f").emit(eventName, ...args);
415
+ }
416
+ /**
417
+ * @param eventName
418
+ * @param value
419
+ * @param args
420
+ */
421
+ alter(eventName, value, ...args) {
422
+ return __classPrivateFieldGet(this, _InjectorService_cache, "f").alter(eventName, value, ...args);
423
+ }
424
+ /**
425
+ * @param eventName
426
+ * @param value
427
+ * @param args
428
+ */
429
+ async alterAsync(eventName, value, ...args) {
430
+ return __classPrivateFieldGet(this, _InjectorService_cache, "f").alterAsync(eventName, value, ...args);
431
+ }
432
+ async destroy() {
433
+ await __classPrivateFieldGet(this, _InjectorService_cache, "f").destroy();
434
+ }
435
+ ensureProvider(token) {
436
+ if (!this.hasProvider(token) && GlobalProviders.has(token)) {
437
+ this.addProvider(token);
438
+ }
439
+ return this.getProvider(token);
440
+ }
441
+ getInstance(token) {
442
+ return __classPrivateFieldGet(this, _InjectorService_cache, "f").get(token);
443
+ }
444
+ /**
445
+ * Invoke a class method and inject service.
446
+ *
447
+ * #### IInjectableMethod options
448
+ *
449
+ * * **target**: Optional. The class instance.
450
+ * * **methodName**: `string` Optional. The method name.
451
+ * * **designParamTypes**: `any[]` Optional. List of injectable types.
452
+ * * **locals**: `Map<Function, any>` Optional. If preset then any argument Class are read from this object first, before the `InjectorService` is consulted.
453
+ *
454
+ * #### Example
455
+ *
456
+ * @param target
457
+ * @param locals
458
+ * @param options
459
+ * @private
460
+ */
461
+ resolve(target, locals, options = {}) {
462
+ const resolvedOpts = this.mapInvokeOptions(target, locals, options);
463
+ const { token, deps, construct, imports, provider } = resolvedOpts;
464
+ if (provider) {
465
+ GlobalProviders.onInvoke(provider, locals, { ...resolvedOpts, injector: this });
466
+ }
467
+ let instance;
468
+ let currentDependency = false;
469
+ try {
470
+ const invokeDependency = (parent) => (token, index) => {
471
+ var _a;
472
+ currentDependency = { token, index, deps };
473
+ if (token !== DI_PARAM_OPTIONS) {
474
+ const options = (_a = provider === null || provider === void 0 ? void 0 : provider.store) === null || _a === void 0 ? void 0 : _a.get(`${DI_PARAM_OPTIONS}:${index}`);
475
+ locals.set(DI_PARAM_OPTIONS, options || {});
476
+ }
477
+ return isInheritedFrom(token, Provider, 1) ? provider : this.invoke(token, locals, { parent });
478
+ };
479
+ // Invoke manually imported providers
480
+ imports.forEach(invokeDependency());
481
+ // Inject dependencies
482
+ const services = deps.map(invokeDependency(token));
483
+ currentDependency = false;
484
+ instance = construct(services);
485
+ }
486
+ catch (error) {
487
+ InjectionError.throwInjectorError(token, currentDependency, error);
488
+ }
489
+ if (instance === undefined) {
490
+ throw new InjectionError(token, `Unable to create new instance from undefined value. Check your provider declaration for ${nameOf(token)}`);
491
+ }
492
+ if (instance && isClass(classOf(instance))) {
493
+ this.bindInjectableProperties(instance, locals, options);
494
+ }
495
+ return instance;
496
+ }
497
+ /**
498
+ * Create options to invoke a provider or class.
499
+ * @param token
500
+ * @param locals
501
+ * @param options
502
+ */
503
+ mapInvokeOptions(token, locals, options) {
504
+ let imports = options.imports;
505
+ let deps = options.deps;
506
+ let scope = options.scope;
507
+ let construct;
508
+ if (!token) {
509
+ throw new UndefinedTokenError();
510
+ }
511
+ let provider;
512
+ if (!this.hasProvider(token)) {
513
+ provider = new Provider(token);
514
+ this.resolvers.forEach((resolver) => {
515
+ const result = resolver.get(token, locals.get(DI_PARAM_OPTIONS));
516
+ if (result !== undefined) {
517
+ provider.useFactory = () => result;
518
+ }
519
+ });
520
+ }
521
+ else {
522
+ provider = this.getProvider(token);
523
+ }
524
+ scope = scope || this.scopeOf(provider);
525
+ deps = deps || provider.deps;
526
+ imports = imports || provider.imports;
527
+ if (provider.useValue !== undefined) {
528
+ construct = () => (isFunction(provider.useValue) ? provider.useValue() : provider.useValue);
529
+ }
530
+ else if (provider.useFactory) {
531
+ construct = (deps) => provider.useFactory(...deps);
532
+ }
533
+ else if (provider.useAsyncFactory) {
534
+ construct = async (deps) => {
535
+ deps = await Promise.all(deps);
536
+ return provider.useAsyncFactory(...deps);
537
+ };
538
+ }
539
+ else {
540
+ // useClass
541
+ deps = deps || Metadata.getParamTypes(provider.useClass);
542
+ construct = (deps) => new provider.useClass(...deps);
543
+ }
544
+ return {
545
+ token,
546
+ scope: scope || Store.from(token).get("scope") || ProviderScope.SINGLETON,
547
+ deps: deps || [],
548
+ imports: imports || [],
549
+ construct,
550
+ provider
551
+ };
552
+ }
553
+ };
554
+ _InjectorService_cache = new WeakMap();
555
+ InjectorService = InjectorService_1 = __decorate([
556
+ Injectable({
557
+ scope: ProviderScope.SINGLETON,
558
+ global: true
559
+ }),
560
+ __metadata("design:paramtypes", [])
561
+ ], InjectorService);
562
+ export { InjectorService };
563
+ //# sourceMappingURL=InjectorService.js.map