@tsed/di 8.0.0-alpha.9 → 8.0.0-beta.2
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.
- package/.barrelsby.json +1 -1
- package/lib/browser/di.umd.min.js +1 -1
- package/lib/browser/di.umd.min.js.map +1 -1
- package/lib/esm/common/decorators/autoInjectable.js +3 -5
- package/lib/esm/common/decorators/autoInjectable.js.map +1 -1
- package/lib/esm/common/decorators/constant.js +1 -4
- package/lib/esm/common/decorators/constant.js.map +1 -1
- package/lib/esm/common/decorators/inject.js +2 -7
- package/lib/esm/common/decorators/inject.js.map +1 -1
- package/lib/esm/common/decorators/injectable.js +2 -2
- package/lib/esm/common/decorators/injectable.js.map +1 -1
- package/lib/esm/common/decorators/intercept.js +1 -1
- package/lib/esm/common/decorators/intercept.js.map +1 -1
- package/lib/esm/common/decorators/lazyInject.js +3 -4
- package/lib/esm/common/decorators/lazyInject.js.map +1 -1
- package/lib/esm/common/decorators/value.js +3 -3
- package/lib/esm/common/decorators/value.js.map +1 -1
- package/lib/esm/common/fn/configuration.js +5 -0
- package/lib/esm/common/fn/configuration.js.map +1 -0
- package/lib/esm/common/fn/constant.js +5 -0
- package/lib/esm/common/fn/constant.js.map +1 -0
- package/lib/esm/common/fn/events.js +31 -0
- package/lib/esm/common/fn/events.js.map +1 -0
- package/lib/esm/common/fn/inject.js +26 -0
- package/lib/esm/common/fn/inject.js.map +1 -0
- package/lib/esm/common/fn/injectMany.js +6 -0
- package/lib/esm/common/fn/injectMany.js.map +1 -0
- package/lib/esm/common/fn/injectable.js +6 -0
- package/lib/esm/common/fn/injectable.js.map +1 -0
- package/lib/esm/common/fn/injector.js +36 -0
- package/lib/esm/common/fn/injector.js.map +1 -0
- package/lib/esm/common/fn/localsContainer.js +40 -0
- package/lib/esm/common/fn/localsContainer.js.map +1 -0
- package/lib/esm/common/fn/refValue.js +12 -0
- package/lib/esm/common/fn/refValue.js.map +1 -0
- package/lib/esm/common/index.js +9 -0
- package/lib/esm/common/index.js.map +1 -1
- package/lib/esm/common/services/InjectorService.js +5 -32
- package/lib/esm/common/services/InjectorService.js.map +1 -1
- package/lib/esm/node/decorators/injectContext.js +2 -10
- package/lib/esm/node/decorators/injectContext.js.map +1 -1
- package/lib/esm/node/fn/context.js +21 -0
- package/lib/esm/node/fn/context.js.map +1 -0
- package/lib/esm/node/fn/logger.js +16 -0
- package/lib/esm/node/fn/logger.js.map +1 -0
- package/lib/esm/node/index.js +2 -0
- package/lib/esm/node/index.js.map +1 -1
- package/lib/esm/node/services/DILogger.js +4 -6
- package/lib/esm/node/services/DILogger.js.map +1 -1
- package/lib/esm/node/services/DITest.js +23 -49
- package/lib/esm/node/services/DITest.js.map +1 -1
- package/lib/esm/node/utils/asyncHookContext.js +1 -2
- package/lib/esm/node/utils/asyncHookContext.js.map +1 -1
- package/lib/types/common/decorators/constant.d.ts +0 -2
- package/lib/types/common/decorators/inject.d.ts +0 -3
- package/lib/types/common/fn/configuration.d.ts +2 -0
- package/lib/types/common/fn/constant.d.ts +2 -0
- package/lib/types/common/fn/events.d.ts +23 -0
- package/lib/types/common/fn/inject.d.ts +20 -0
- package/lib/types/common/fn/injectMany.d.ts +2 -0
- package/lib/types/common/fn/injectable.d.ts +5 -0
- package/lib/types/common/fn/injector.d.ts +24 -0
- package/lib/types/common/fn/localsContainer.d.ts +15 -0
- package/lib/types/common/fn/refValue.d.ts +25 -0
- package/lib/types/common/index.d.ts +9 -0
- package/lib/types/common/services/InjectorService.d.ts +2 -12
- package/lib/types/node/fn/context.d.ts +9 -0
- package/lib/types/node/fn/logger.d.ts +9 -0
- package/lib/types/node/index.d.ts +2 -0
- package/lib/types/node/services/DITest.d.ts +0 -5
- package/lib/types/node/utils/asyncHookContext.d.ts +1 -2
- package/package.json +12 -12
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { catchError } from "@tsed/core";
|
|
2
|
-
import {
|
|
3
|
-
import { DIContext } from "../domain/DIContext.js";
|
|
4
|
-
import { getContext } from "../utils/asyncHookContext.js";
|
|
2
|
+
import { context } from "../fn/context.js";
|
|
5
3
|
/**
|
|
6
4
|
* Inject a context like PlatformContext or any BaseContext.
|
|
7
5
|
*
|
|
@@ -21,13 +19,7 @@ export function InjectContext(transform = (o) => o) {
|
|
|
21
19
|
catchError(() => Reflect.deleteProperty(target, propertyKey));
|
|
22
20
|
Reflect.defineProperty(target, propertyKey, {
|
|
23
21
|
get() {
|
|
24
|
-
return transform(
|
|
25
|
-
new DIContext({
|
|
26
|
-
id: "",
|
|
27
|
-
logger: InjectorService.getInstance().logger,
|
|
28
|
-
injector: InjectorService.getInstance(),
|
|
29
|
-
maxStackSize: 0
|
|
30
|
-
}));
|
|
22
|
+
return transform(context());
|
|
31
23
|
}
|
|
32
24
|
});
|
|
33
25
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectContext.js","sourceRoot":"","sources":["../../../../src/node/decorators/injectContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"injectContext.js","sourceRoot":"","sources":["../../../../src/node/decorators/injectContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AAGtC,OAAO,EAAC,OAAO,EAAC,MAAM,kBAAkB,CAAC;AAEzC;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,aAAa,CAAC,YAA0C,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC9E,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAc,EAAE;QACtD,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;QAC9D,OAAO,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;YAC1C,GAAG;gBACD,OAAO,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC;YAC9B,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { v4 } from "uuid";
|
|
2
|
+
import { injector } from "../../common/index.js";
|
|
3
|
+
import { DIContext } from "../domain/DIContext.js";
|
|
4
|
+
import { getContext } from "../utils/asyncHookContext.js";
|
|
5
|
+
/**
|
|
6
|
+
* Get the current DIContext instance using async hook node.js api.
|
|
7
|
+
*
|
|
8
|
+
* ::: warning
|
|
9
|
+
* This function isn't available in the browser context.
|
|
10
|
+
* :::
|
|
11
|
+
*/
|
|
12
|
+
export function context() {
|
|
13
|
+
return (getContext() ||
|
|
14
|
+
new DIContext({
|
|
15
|
+
id: v4(),
|
|
16
|
+
logger: injector().logger,
|
|
17
|
+
injector: injector(),
|
|
18
|
+
maxStackSize: 0
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../../src/node/fn/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,EAAE,EAAC,MAAM,MAAM,CAAC;AAExB,OAAO,EAAC,QAAQ,EAAC,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAC,SAAS,EAAC,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAC,UAAU,EAAC,MAAM,8BAA8B,CAAC;AAExD;;;;;;GAMG;AACH,MAAM,UAAU,OAAO;IACrB,OAAO,CACL,UAAU,EAAO;QAChB,IAAI,SAAS,CAAC;YACb,EAAE,EAAE,EAAE,EAAE;YACR,MAAM,EAAE,QAAQ,EAAE,CAAC,MAAM;YACzB,QAAQ,EAAE,QAAQ,EAAE;YACpB,YAAY,EAAE,CAAC;SAChB,CAAS,CACX,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { injector } from "../../common/index.js";
|
|
2
|
+
import { context } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* Get the current `injector.logger` instance.
|
|
5
|
+
*/
|
|
6
|
+
export function logger() {
|
|
7
|
+
return injector().logger;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Get the current logger instance. If the logger() function is invoked in Request context, it will return the logger instance attached to the request context.
|
|
11
|
+
* Otherwise, it will return the logger instance attached to the DI context.
|
|
12
|
+
*/
|
|
13
|
+
export function contextLogger() {
|
|
14
|
+
return context().logger;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../../src/node/fn/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAC,OAAO,EAAC,MAAM,cAAc,CAAC;AAErC;;GAEG;AACH,MAAM,UAAU,MAAM;IACpB,OAAO,QAAQ,EAAE,CAAC,MAAM,CAAC;AAC3B,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa;IAC3B,OAAO,OAAO,EAAE,CAAC,MAAM,CAAC;AAC1B,CAAC"}
|
package/lib/esm/node/index.js
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
export * from "./decorators/injectContext.js";
|
|
5
5
|
export * from "./domain/ContextLogger.js";
|
|
6
6
|
export * from "./domain/DIContext.js";
|
|
7
|
+
export * from "./fn/context.js";
|
|
8
|
+
export * from "./fn/logger.js";
|
|
7
9
|
export * from "./interfaces/AlterRunInContext.js";
|
|
8
10
|
export * from "./services/DILogger.js";
|
|
9
11
|
export * from "./services/DITest.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,mCAAmC,CAAC;AAClD,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,mCAAmC,CAAC;AAClD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mCAAmC,CAAC;AAClD,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,mCAAmC,CAAC;AAClD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,CAAC"}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { Logger } from "@tsed/logger";
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { injectable } from "../../common/fn/injectable.js";
|
|
3
|
+
import { logger } from "../fn/logger.js";
|
|
4
|
+
injectable({
|
|
4
5
|
provide: Logger,
|
|
5
|
-
|
|
6
|
-
useFactory(injector) {
|
|
7
|
-
return injector.logger;
|
|
8
|
-
}
|
|
6
|
+
useFactory: logger
|
|
9
7
|
});
|
|
10
8
|
//# sourceMappingURL=DILogger.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DILogger.js","sourceRoot":"","sources":["../../../../src/node/services/DILogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AAEpC,OAAO,EAAC,
|
|
1
|
+
{"version":3,"file":"DILogger.js","sourceRoot":"","sources":["../../../../src/node/services/DILogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AAEpC,OAAO,EAAC,UAAU,EAAC,MAAM,+BAA+B,CAAC;AACzD,OAAO,EAAC,MAAM,EAAC,MAAM,iBAAiB,CAAC;AAEvC,UAAU,CAAC;IACT,OAAO,EAAE,MAAM;IACf,UAAU,EAAE,MAAM;CACnB,CAAC,CAAC"}
|
|
@@ -1,65 +1,43 @@
|
|
|
1
|
-
import { Env, getValue, isClass, isObject
|
|
1
|
+
import { Env, getValue, isClass, isObject } from "@tsed/core";
|
|
2
2
|
import { $log } from "@tsed/logger";
|
|
3
|
-
import {
|
|
3
|
+
import { cleanAllLocalsContainer, detachLocalsContainer, localsContainer } from "../../common/fn/localsContainer.js";
|
|
4
|
+
import { createContainer, destroyInjector, DI_INJECTABLE_PROPS, hasInjector, inject, injector } from "../../common/index.js";
|
|
4
5
|
import { DIContext } from "../domain/DIContext.js";
|
|
6
|
+
import { logger } from "../fn/logger.js";
|
|
5
7
|
import { setLoggerConfiguration } from "../utils/setLoggerConfiguration.js";
|
|
6
8
|
/**
|
|
7
9
|
* Tool to run test with lightweight DI sandbox.
|
|
8
10
|
*/
|
|
9
11
|
export class DITest {
|
|
10
|
-
static { this.options = {}; }
|
|
11
|
-
static { this._injector = null; }
|
|
12
12
|
static get injector() {
|
|
13
|
-
|
|
14
|
-
return DITest._injector;
|
|
15
|
-
}
|
|
16
|
-
/* istanbul ignore next */
|
|
17
|
-
throw new Error("PlatformTest.injector is not initialized. Use PlatformTest.create(): Promise before PlatformTest.invoke() or PlatformTest.injector.\n" +
|
|
18
|
-
"Example:\n" +
|
|
19
|
-
"before(async () => {\n" +
|
|
20
|
-
" await PlatformTest.create()\n" +
|
|
21
|
-
" await PlatformTest.invoke(MyService, [])\n" +
|
|
22
|
-
"})");
|
|
23
|
-
}
|
|
24
|
-
static set injector(injector) {
|
|
25
|
-
DITest._injector = injector;
|
|
26
|
-
}
|
|
27
|
-
static set(key, value) {
|
|
28
|
-
setValue(DITest.options, key, value);
|
|
29
|
-
}
|
|
30
|
-
static hasInjector() {
|
|
31
|
-
return !!DITest._injector;
|
|
13
|
+
return injector();
|
|
32
14
|
}
|
|
33
15
|
static async create(settings = {}) {
|
|
34
|
-
settings
|
|
35
|
-
...DITest.options,
|
|
36
|
-
...settings
|
|
37
|
-
};
|
|
38
|
-
DITest.injector = DITest.createInjector(settings);
|
|
16
|
+
DITest.createInjector(settings);
|
|
39
17
|
await DITest.createContainer();
|
|
40
18
|
}
|
|
41
19
|
static async createContainer() {
|
|
42
|
-
await
|
|
20
|
+
await injector().load(createContainer());
|
|
43
21
|
}
|
|
44
22
|
/**
|
|
45
23
|
* Create a new injector with the right default services
|
|
46
24
|
*/
|
|
47
25
|
static createInjector(settings = {}) {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
26
|
+
const inj = injector({
|
|
27
|
+
rebuild: true,
|
|
28
|
+
logger: $log,
|
|
29
|
+
settings: DITest.configure(settings)
|
|
30
|
+
});
|
|
31
|
+
setLoggerConfiguration(inj);
|
|
32
|
+
return inj;
|
|
54
33
|
}
|
|
55
34
|
/**
|
|
56
35
|
* Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
|
|
57
36
|
*/
|
|
58
37
|
static async reset() {
|
|
59
|
-
if (
|
|
60
|
-
await
|
|
61
|
-
|
|
62
|
-
DITest._injector = null;
|
|
38
|
+
if (hasInjector()) {
|
|
39
|
+
await destroyInjector();
|
|
40
|
+
cleanAllLocalsContainer();
|
|
63
41
|
}
|
|
64
42
|
}
|
|
65
43
|
/**
|
|
@@ -68,12 +46,8 @@ export class DITest {
|
|
|
68
46
|
* @param providers
|
|
69
47
|
*/
|
|
70
48
|
static async invoke(target, providers = []) {
|
|
71
|
-
const locals =
|
|
72
|
-
|
|
73
|
-
locals.set(p.token, p.use);
|
|
74
|
-
});
|
|
75
|
-
locals.set(InjectorService, DITest.injector);
|
|
76
|
-
const instance = DITest.injector.invoke(target, locals, { rebuild: true });
|
|
49
|
+
const locals = localsContainer({ providers, rebuild: true });
|
|
50
|
+
const instance = inject(target, { locals, rebuild: true });
|
|
77
51
|
if (instance && isObject(instance) && "$onInit" in instance) {
|
|
78
52
|
const result = instance.$onInit();
|
|
79
53
|
if (result instanceof Promise) {
|
|
@@ -90,7 +64,7 @@ export class DITest {
|
|
|
90
64
|
await Promise.all([...keys.keys()].map((key) => instance[key]));
|
|
91
65
|
}
|
|
92
66
|
}
|
|
93
|
-
|
|
67
|
+
detachLocalsContainer();
|
|
94
68
|
return instance;
|
|
95
69
|
}
|
|
96
70
|
/**
|
|
@@ -99,13 +73,13 @@ export class DITest {
|
|
|
99
73
|
* @param options
|
|
100
74
|
*/
|
|
101
75
|
static get(target, options = {}) {
|
|
102
|
-
return
|
|
76
|
+
return injector().get(target, options);
|
|
103
77
|
}
|
|
104
78
|
static createDIContext() {
|
|
105
79
|
return new DIContext({
|
|
106
80
|
id: "id",
|
|
107
|
-
injector:
|
|
108
|
-
logger:
|
|
81
|
+
injector: injector(),
|
|
82
|
+
logger: logger()
|
|
109
83
|
});
|
|
110
84
|
}
|
|
111
85
|
static configure(settings = {}) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DITest.js","sourceRoot":"","sources":["../../../../src/node/services/DITest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"DITest.js","sourceRoot":"","sources":["../../../../src/node/services/DITest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAC,IAAI,EAAC,MAAM,cAAc,CAAC;AAElC,OAAO,EAAC,uBAAuB,EAAE,qBAAqB,EAAE,eAAe,EAAC,MAAM,oCAAoC,CAAC;AACnH,OAAO,EAC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nE,OAAO;YACL,GAAG,QAAQ;YACX,GAAG,EAAE,QAAQ,CAAC,QAAQ,EAAE,KAAK,EAAE,GAAG,CAAC,IAAI,CAAC;YACxC,MAAM,EAAE;gBACN,GAAG,QAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE,CAAC;gBACnC,KAAK,EAAE,QAAQ,CAAC,QAAQ,EAAE,cAAc,EAAE,KAAK,CAAC;aACjD;SACF,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -6,10 +6,9 @@ export function getAsyncStore() {
|
|
|
6
6
|
export function useContextRef() {
|
|
7
7
|
return getAsyncStore().getStore();
|
|
8
8
|
}
|
|
9
|
-
export function
|
|
9
|
+
export function getContext(initialValue) {
|
|
10
10
|
return initialValue || useContextRef()?.current;
|
|
11
11
|
}
|
|
12
|
-
export const getContext = useContext;
|
|
13
12
|
export async function runInContext(ctx, cb, injector) {
|
|
14
13
|
const ref = useContextRef();
|
|
15
14
|
if (ref) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncHookContext.js","sourceRoot":"","sources":["../../../../src/node/utils/asyncHookContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;AAK7D,MAAM,OAAO,GAAwD,IAAI,iBAAiB,EAAE,CAAC;AAE7F,MAAM,UAAU,aAAa;IAC3B,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,aAAa;IAC3B,OAAO,aAAa,EAAE,CAAC,QAAQ,EAAE,CAAC;AACpC,CAAC;AAED,MAAM,UAAU,UAAU,CAAsB,YAAwB;IACtE,OAAO,YAAY,IAAK,aAAa,EAAE,EAAE,OAAe,CAAC;AAC3D,CAAC;AAED,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"asyncHookContext.js","sourceRoot":"","sources":["../../../../src/node/utils/asyncHookContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAE,aAAa,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|
2
|
-
export declare function constant<Type>(expression: string, defaultValue: Type | undefined): Type;
|
|
3
1
|
export declare function bindConstant(target: any, propertyKey: string | symbol, expression: string, defaultValue?: any): void;
|
|
4
2
|
/**
|
|
5
3
|
* Return value from Configuration.
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { type Type } from "@tsed/core";
|
|
2
|
-
import type { InvokeOptions } from "../interfaces/InvokeOptions.js";
|
|
3
2
|
import { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
4
|
-
export declare function inject<T>(token: TokenProvider<T>, opts?: Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>): T;
|
|
5
|
-
export declare function injectMany<T>(token: string | symbol, opts?: Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>): T[];
|
|
6
3
|
export type TransformInjectedProviderCB<T, Klass = any> = (instance: T, { target, propertyKey }: {
|
|
7
4
|
self: Klass;
|
|
8
5
|
target: Type<Klass>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alter value attached to an event asynchronously.
|
|
3
|
+
* @param eventName
|
|
4
|
+
* @param value
|
|
5
|
+
* @param args
|
|
6
|
+
* @param callThis
|
|
7
|
+
*/
|
|
8
|
+
export declare function $alterAsync<T = any>(eventName: string, value: any, ...args: unknown[]): Promise<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
|
|
11
|
+
* @param eventName The event name to emit at all services.
|
|
12
|
+
* @param args List of the parameters to give to each service.
|
|
13
|
+
* @returns A list of promises.
|
|
14
|
+
*/
|
|
15
|
+
export declare function $emit(eventName: string, ...args: unknown[]): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Alter value attached to an event.
|
|
18
|
+
* @param eventName
|
|
19
|
+
* @param value
|
|
20
|
+
* @param args
|
|
21
|
+
* @param callThis
|
|
22
|
+
*/
|
|
23
|
+
export declare function $alter<T = any>(eventName: string, value: unknown, ...args: unknown[]): T;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { InvokeOptions } from "../interfaces/InvokeOptions.js";
|
|
2
|
+
import { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
3
|
+
/**
|
|
4
|
+
* Inject a provider to another provider.
|
|
5
|
+
*
|
|
6
|
+
* Use this function to inject a custom provider on constructor parameter or property.
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* @Injectable()
|
|
10
|
+
* export class MyService {
|
|
11
|
+
* connection = inject(CONNECTION);
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param token A token provider or token provider group
|
|
16
|
+
* @param opts
|
|
17
|
+
* @returns {Function}
|
|
18
|
+
* @decorator
|
|
19
|
+
*/
|
|
20
|
+
export declare function inject<T>(token: TokenProvider<T>, opts?: Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>): T;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { InjectorService } from "../services/InjectorService.js";
|
|
2
|
+
type InjectorFnOpts = {
|
|
3
|
+
rebuild?: boolean;
|
|
4
|
+
logger?: any;
|
|
5
|
+
settings?: Partial<TsED.Configuration>;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Create or return the existing injector service.
|
|
9
|
+
*
|
|
10
|
+
* Example:
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import {injector, Injectable} from "@tsed/di";
|
|
14
|
+
*
|
|
15
|
+
* @Injectable()
|
|
16
|
+
* class MyService {
|
|
17
|
+
* injector = injector();
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function injector(opts?: InjectorFnOpts): InjectorService;
|
|
22
|
+
export declare function hasInjector(): boolean;
|
|
23
|
+
export declare function destroyInjector(): Promise<void>;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { LocalsContainer } from "../domain/LocalsContainer.js";
|
|
2
|
+
import type { UseImportTokenProviderOpts } from "../interfaces/ImportTokenProviderOpts.js";
|
|
3
|
+
/**
|
|
4
|
+
* Get the locals container initiated by DITest or .bootstrap() method.
|
|
5
|
+
*/
|
|
6
|
+
export declare function localsContainer({ providers, rebuild }?: {
|
|
7
|
+
providers?: UseImportTokenProviderOpts[];
|
|
8
|
+
rebuild?: boolean;
|
|
9
|
+
}): LocalsContainer;
|
|
10
|
+
export declare function invokeOptions(): any;
|
|
11
|
+
/**
|
|
12
|
+
* Reset the locals container.
|
|
13
|
+
*/
|
|
14
|
+
export declare function detachLocalsContainer(): void;
|
|
15
|
+
export declare function cleanAllLocalsContainer(): void;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a value from the `injector.settings`.
|
|
3
|
+
*
|
|
4
|
+
* ## Example
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import {refValue, Injectable} from "@tsed/di";
|
|
8
|
+
*
|
|
9
|
+
* @Injectable()
|
|
10
|
+
* class Test {
|
|
11
|
+
* test = refValue("logger.level", "default value");
|
|
12
|
+
*
|
|
13
|
+
* constructor() {
|
|
14
|
+
* console.log(this.test.value); // "off"
|
|
15
|
+
* }
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* @param expression The expression to get the value from the `injector.settings`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function refValue<Type>(expression: string): {
|
|
21
|
+
value: Type | undefined;
|
|
22
|
+
};
|
|
23
|
+
export declare function refValue<Type>(expression: string, defaultValue: Type | undefined): {
|
|
24
|
+
value: Type;
|
|
25
|
+
};
|
|
@@ -27,6 +27,15 @@ export * from "./domain/ProviderScope.js";
|
|
|
27
27
|
export * from "./domain/ProviderType.js";
|
|
28
28
|
export * from "./errors/InjectionError.js";
|
|
29
29
|
export * from "./errors/InvalidPropertyTokenError.js";
|
|
30
|
+
export * from "./fn/configuration.js";
|
|
31
|
+
export * from "./fn/constant.js";
|
|
32
|
+
export * from "./fn/events.js";
|
|
33
|
+
export * from "./fn/inject.js";
|
|
34
|
+
export * from "./fn/injectable.js";
|
|
35
|
+
export * from "./fn/injectMany.js";
|
|
36
|
+
export * from "./fn/injector.js";
|
|
37
|
+
export * from "./fn/localsContainer.js";
|
|
38
|
+
export * from "./fn/refValue.js";
|
|
30
39
|
export * from "./interfaces/DIConfigurationOptions.js";
|
|
31
40
|
export * from "./interfaces/DILogger.js";
|
|
32
41
|
export * from "./interfaces/DILoggerOptions.js";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Hooks } from "@tsed/core";
|
|
1
2
|
import { Container } from "../domain/Container.js";
|
|
2
3
|
import { LocalsContainer } from "../domain/LocalsContainer.js";
|
|
3
4
|
import { Provider } from "../domain/Provider.js";
|
|
@@ -32,21 +33,10 @@ export declare class InjectorService extends Container {
|
|
|
32
33
|
settings: DIConfiguration;
|
|
33
34
|
logger: DILogger;
|
|
34
35
|
private resolvedConfiguration;
|
|
36
|
+
readonly hooks: Hooks;
|
|
35
37
|
constructor();
|
|
36
38
|
get resolvers(): import("../index.js").DIResolver[];
|
|
37
39
|
get scopes(): Record<string, ProviderScope>;
|
|
38
|
-
/**
|
|
39
|
-
* Return the current injector service.
|
|
40
|
-
*/
|
|
41
|
-
static getInstance(): InjectorService;
|
|
42
|
-
/**
|
|
43
|
-
* Get the locals container initiated by DITest or .bootstrap() method.
|
|
44
|
-
*/
|
|
45
|
-
static getLocals(): LocalsContainer;
|
|
46
|
-
/**
|
|
47
|
-
* Reset the locals container.
|
|
48
|
-
*/
|
|
49
|
-
static unsetLocals(): void;
|
|
50
40
|
/**
|
|
51
41
|
* Retrieve default scope for a given provider.
|
|
52
42
|
* @param provider
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DIContext } from "../domain/DIContext.js";
|
|
2
|
+
/**
|
|
3
|
+
* Get the current DIContext instance using async hook node.js api.
|
|
4
|
+
*
|
|
5
|
+
* ::: warning
|
|
6
|
+
* This function isn't available in the browser context.
|
|
7
|
+
* :::
|
|
8
|
+
*/
|
|
9
|
+
export declare function context<Ctx = DIContext>(): Ctx;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the current `injector.logger` instance.
|
|
3
|
+
*/
|
|
4
|
+
export declare function logger(): import("../../common/index.js").DILogger;
|
|
5
|
+
/**
|
|
6
|
+
* Get the current logger instance. If the logger() function is invoked in Request context, it will return the logger instance attached to the request context.
|
|
7
|
+
* Otherwise, it will return the logger instance attached to the DI context.
|
|
8
|
+
*/
|
|
9
|
+
export declare function contextLogger(): import("../index.js").ContextLogger;
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
export * from "./decorators/injectContext.js";
|
|
5
5
|
export * from "./domain/ContextLogger.js";
|
|
6
6
|
export * from "./domain/DIContext.js";
|
|
7
|
+
export * from "./fn/context.js";
|
|
8
|
+
export * from "./fn/logger.js";
|
|
7
9
|
export * from "./interfaces/AlterRunInContext.js";
|
|
8
10
|
export * from "./services/DILogger.js";
|
|
9
11
|
export * from "./services/DITest.js";
|
|
@@ -4,12 +4,7 @@ import { DIContext } from "../domain/DIContext.js";
|
|
|
4
4
|
* Tool to run test with lightweight DI sandbox.
|
|
5
5
|
*/
|
|
6
6
|
export declare class DITest {
|
|
7
|
-
static options: Partial<TsED.Configuration>;
|
|
8
|
-
protected static _injector: InjectorService | null;
|
|
9
7
|
static get injector(): InjectorService;
|
|
10
|
-
static set injector(injector: InjectorService);
|
|
11
|
-
static set(key: string, value: any): void;
|
|
12
|
-
static hasInjector(): boolean;
|
|
13
8
|
static create(settings?: Partial<TsED.Configuration>): Promise<void>;
|
|
14
9
|
static createContainer(): Promise<void>;
|
|
15
10
|
/**
|
|
@@ -8,8 +8,7 @@ export declare function getAsyncStore(): AsyncLocalStorage<{
|
|
|
8
8
|
export declare function useContextRef(): {
|
|
9
9
|
current: DIContext | undefined;
|
|
10
10
|
} | undefined;
|
|
11
|
-
export declare function
|
|
12
|
-
export declare const getContext: typeof useContext;
|
|
11
|
+
export declare function getContext<Context = DIContext>(initialValue?: DIContext): Context | undefined;
|
|
13
12
|
export declare function runInContext<Result = unknown>(ctx: DIContext | undefined, cb: (...args: unknown[]) => Result, injector?: InjectorService): Promise<Result>;
|
|
14
13
|
export declare function setContext(ctx: DIContext): void;
|
|
15
14
|
export declare function bindContext(cb: any): any;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/di",
|
|
3
3
|
"description": "DI module for Ts.ED Framework",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.0.0-
|
|
5
|
+
"version": "8.0.0-beta.2",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -26,24 +26,24 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"tslib": "2.7.0",
|
|
29
|
-
"uuid": "
|
|
29
|
+
"uuid": "^10.0.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@tsed/barrels": "8.0.0-
|
|
33
|
-
"@tsed/core": "8.0.0-
|
|
34
|
-
"@tsed/logger": "
|
|
35
|
-
"@tsed/schema": "8.0.0-
|
|
36
|
-
"@tsed/typescript": "8.0.0-
|
|
32
|
+
"@tsed/barrels": "8.0.0-beta.2",
|
|
33
|
+
"@tsed/core": "8.0.0-beta.2",
|
|
34
|
+
"@tsed/logger": "^6.7.8",
|
|
35
|
+
"@tsed/schema": "8.0.0-beta.2",
|
|
36
|
+
"@tsed/typescript": "8.0.0-beta.2",
|
|
37
37
|
"cross-env": "7.0.3",
|
|
38
|
-
"eslint": "9.
|
|
38
|
+
"eslint": "9.12.0",
|
|
39
39
|
"typescript": "5.4.5",
|
|
40
|
-
"vitest": "2.
|
|
40
|
+
"vitest": "2.1.2",
|
|
41
41
|
"webpack": "^5.75.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@tsed/core": "8.0.0-
|
|
44
|
+
"@tsed/core": "8.0.0-beta.2",
|
|
45
45
|
"@tsed/logger": ">=6.7.5",
|
|
46
|
-
"@tsed/schema": "8.0.0-
|
|
46
|
+
"@tsed/schema": "8.0.0-beta.2"
|
|
47
47
|
},
|
|
48
48
|
"peerDependenciesMeta": {
|
|
49
49
|
"@tsed/core": {
|
|
@@ -64,6 +64,6 @@
|
|
|
64
64
|
"author": "Romain Lenzotti",
|
|
65
65
|
"license": "MIT",
|
|
66
66
|
"publishConfig": {
|
|
67
|
-
"tag": "
|
|
67
|
+
"tag": "beta"
|
|
68
68
|
}
|
|
69
69
|
}
|