@tsed/di 8.0.0-beta.1 → 8.0.0-beta.11
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/controller.js +2 -5
- package/lib/esm/common/decorators/controller.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 +4 -3
- 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/interceptor.js +2 -2
- package/lib/esm/common/decorators/lazyInject.js +2 -3
- 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/domain/ControllerProvider.js.map +1 -1
- package/lib/esm/common/domain/Provider.js +6 -1
- package/lib/esm/common/domain/Provider.js.map +1 -1
- package/lib/esm/common/domain/ProviderType.js +0 -2
- package/lib/esm/common/domain/ProviderType.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 +57 -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/registries/GlobalProviders.js +1 -5
- package/lib/esm/common/registries/GlobalProviders.js.map +1 -1
- package/lib/esm/common/registries/ProviderRegistry.js +2 -71
- package/lib/esm/common/registries/ProviderRegistry.js.map +1 -1
- package/lib/esm/common/services/DIConfiguration.js +1 -14
- package/lib/esm/common/services/DIConfiguration.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/domain/DIContext.js +15 -10
- package/lib/esm/node/domain/DIContext.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 +3 -8
- 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/configuration.d.ts +1 -1
- package/lib/types/common/decorators/constant.d.ts +0 -2
- package/lib/types/common/decorators/controller.d.ts +1 -5
- package/lib/types/common/decorators/inject.d.ts +0 -3
- package/lib/types/common/decorators/interceptor.d.ts +2 -2
- package/lib/types/common/domain/ControllerProvider.d.ts +7 -3
- package/lib/types/common/domain/Provider.d.ts +1 -0
- package/lib/types/common/domain/ProviderType.d.ts +0 -2
- 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 +23 -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/interfaces/DIConfigurationOptions.d.ts +5 -0
- package/lib/types/common/registries/ProviderRegistry.d.ts +1 -53
- package/lib/types/common/services/DIConfiguration.d.ts +0 -6
- package/lib/types/common/services/InjectorService.d.ts +2 -12
- package/lib/types/node/domain/DIContext.d.ts +0 -4
- 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 +7 -7
- package/readme.md +2 -4
|
@@ -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":"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"}
|
|
@@ -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,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,KAAK,UAAU,YAAY,CAChC,GAA0B,EAC1B,EAAkC,EAClC,QAA0B;IAE1B,MAAM,GAAG,GAAG,aAAa,EAAE,CAAC;IAE5B,IAAI,GAAG,EAAE,CAAC;QACR,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;QACvB,OAAO,EAAE,EAAE,CAAC;IACd,CAAC;SAAM,CAAC;QACN,QAAQ,GAAG,GAAG,EAAE,QAAQ,IAAI,QAAQ,CAAC;QACrC,EAAE,GAAG,CAAC,MAAM,QAAQ,EAAE,UAAU,CAAC,oBAAoB,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QAElE,OAAO,OAAO,CAAC,GAAG,CAAC,EAAC,OAAO,EAAE,GAAG,EAAC,EAAE,EAAE,CAAC,CAAC;IACzC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,GAAc;IACvC,MAAM,GAAG,GAAG,aAAa,EAAE,CAAC;IAE5B,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;QACxB,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC;IACpB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,EAAO;IACjC,OAAO,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -5,4 +5,4 @@ import { DIConfiguration } from "../services/DIConfiguration.js";
|
|
|
5
5
|
* @decorator
|
|
6
6
|
*/
|
|
7
7
|
export declare function Configuration(configuration?: Partial<TsED.Configuration>): Function;
|
|
8
|
-
export type Configuration = TsED.
|
|
8
|
+
export type Configuration = TsED.DIConfiguration & DIConfiguration;
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
export declare function constant<Type>(expression: string): Type | undefined;
|
|
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,11 +1,7 @@
|
|
|
1
1
|
import { Type } from "@tsed/core";
|
|
2
|
+
import type { ControllerMiddlewares } from "../domain/ControllerProvider.js";
|
|
2
3
|
import { ProviderOpts } from "../interfaces/ProviderOpts.js";
|
|
3
4
|
export type PathType = string | RegExp | (string | RegExp)[];
|
|
4
|
-
export interface ControllerMiddlewares {
|
|
5
|
-
useBefore: any[];
|
|
6
|
-
use: any[];
|
|
7
|
-
useAfter: any[];
|
|
8
|
-
}
|
|
9
5
|
export interface ControllerOptions extends Partial<ProviderOpts<any>> {
|
|
10
6
|
path?: PathType;
|
|
11
7
|
children?: Type<any>[];
|
|
@@ -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>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The decorators `@
|
|
3
|
-
* All services annotated with `@
|
|
2
|
+
* The decorators `@Interceptor()` declare a new service can be injected in other service or controller on there `constructor`.
|
|
3
|
+
* All services annotated with `@Interceptor()` are constructed one time.
|
|
4
4
|
*
|
|
5
5
|
* > `@Service()` use the `reflect-metadata` to collect and inject service on controllers or other services.
|
|
6
6
|
*
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import { ControllerMiddlewares } from "../decorators/controller.js";
|
|
2
1
|
import { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
3
2
|
import { Provider } from "./Provider.js";
|
|
3
|
+
export interface ControllerMiddlewares {
|
|
4
|
+
useBefore: TokenProvider[];
|
|
5
|
+
use: TokenProvider[];
|
|
6
|
+
useAfter: TokenProvider[];
|
|
7
|
+
}
|
|
4
8
|
export declare class ControllerProvider<T = any> extends Provider<T> {
|
|
5
9
|
tokenRouter: string;
|
|
6
10
|
constructor(provide: TokenProvider, options?: Partial<Provider>);
|
|
@@ -8,10 +12,10 @@ export declare class ControllerProvider<T = any> extends Provider<T> {
|
|
|
8
12
|
*
|
|
9
13
|
* @returns {any[]}
|
|
10
14
|
*/
|
|
11
|
-
get middlewares(): ControllerMiddlewares
|
|
15
|
+
get middlewares(): Partial<ControllerMiddlewares>;
|
|
12
16
|
/**
|
|
13
17
|
*
|
|
14
18
|
* @param middlewares
|
|
15
19
|
*/
|
|
16
|
-
set middlewares(middlewares: ControllerMiddlewares);
|
|
20
|
+
set middlewares(middlewares: Partial<ControllerMiddlewares>);
|
|
17
21
|
}
|
|
@@ -54,6 +54,7 @@ export declare class Provider<T = any> implements ProviderOpts<T> {
|
|
|
54
54
|
get configuration(): Partial<TsED.Configuration>;
|
|
55
55
|
set configuration(configuration: Partial<TsED.Configuration>);
|
|
56
56
|
get children(): TokenProvider[];
|
|
57
|
+
set children(children: TokenProvider[]);
|
|
57
58
|
get(key: string): any;
|
|
58
59
|
isAsync(): boolean;
|
|
59
60
|
clone(): Provider;
|
|
@@ -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 type { 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,23 @@
|
|
|
1
|
+
import "../registries/ProviderRegistry.js";
|
|
2
|
+
import { Store, type Type } from "@tsed/core";
|
|
3
|
+
import { ControllerProvider } from "../domain/ControllerProvider.js";
|
|
4
|
+
import type { Provider } from "../domain/Provider.js";
|
|
5
|
+
import type { ProviderOpts } from "../interfaces/ProviderOpts.js";
|
|
6
|
+
import type { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
7
|
+
type ProviderBuilder<Token extends TokenProvider, BaseProvider, T extends object> = {
|
|
8
|
+
[K in keyof T as T[K] extends (...args: any[]) => any ? never : K]: (value: T[K]) => ProviderBuilder<Token, BaseProvider, T>;
|
|
9
|
+
} & {
|
|
10
|
+
inspect(): BaseProvider;
|
|
11
|
+
store(): Store;
|
|
12
|
+
token(): Token;
|
|
13
|
+
factory(f: (...args: unknown[]) => unknown): ProviderBuilder<Token, BaseProvider, T>;
|
|
14
|
+
asyncFactory(f: (...args: unknown[]) => Promise<unknown>): ProviderBuilder<Token, BaseProvider, T>;
|
|
15
|
+
value(v: unknown): ProviderBuilder<Token, BaseProvider, T>;
|
|
16
|
+
class(c: Type): ProviderBuilder<Token, BaseProvider, T>;
|
|
17
|
+
};
|
|
18
|
+
export declare function providerBuilder<Provider, Picked extends keyof Provider>(props: string[], baseOpts?: Partial<ProviderOpts<Provider>>): <Token extends TokenProvider>(token: Token, options?: Partial<ProviderOpts<Type>>) => ProviderBuilder<Token, Provider, Pick<Provider, Picked>>;
|
|
19
|
+
type PickedProps = "scope" | "path" | "alias" | "hooks" | "deps" | "resolvers" | "imports" | "configuration";
|
|
20
|
+
export declare const injectable: <Token extends TokenProvider>(token: Token, options?: Partial<ProviderOpts<Type>>) => ProviderBuilder<Token, Provider<any>, Pick<Provider<any>, "type" | PickedProps>>;
|
|
21
|
+
export declare const interceptor: <Token extends TokenProvider>(token: Token, options?: Partial<ProviderOpts<Type>>) => ProviderBuilder<Token, Provider<any>, Pick<Provider<any>, "type" | PickedProps>>;
|
|
22
|
+
export declare const controller: <Token extends TokenProvider>(token: Token, options?: Partial<ProviderOpts<Type>>) => ProviderBuilder<Token, ControllerProvider<any>, Pick<ControllerProvider<any>, "children" | "middlewares" | PickedProps>>;
|
|
23
|
+
export {};
|
|
@@ -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";
|
|
@@ -6,6 +6,11 @@ declare global {
|
|
|
6
6
|
namespace TsED {
|
|
7
7
|
interface Context {
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Here to allow extension on DIConfiguration base service
|
|
11
|
+
*/
|
|
12
|
+
interface DIConfiguration {
|
|
13
|
+
}
|
|
9
14
|
interface Configuration extends Record<string, any> {
|
|
10
15
|
scopes: {
|
|
11
16
|
[key: string]: ProviderScope;
|
|
@@ -1,58 +1,6 @@
|
|
|
1
|
-
import { Provider } from "../domain/Provider.js";
|
|
2
1
|
import type { ProviderOpts } from "../interfaces/ProviderOpts.js";
|
|
3
2
|
/**
|
|
4
3
|
* Register a provider configuration.
|
|
5
4
|
* @param {ProviderOpts<any>} provider
|
|
6
5
|
*/
|
|
7
|
-
export declare function registerProvider<Type = any>(provider: Partial<ProviderOpts<Type>>): Provider<any>;
|
|
8
|
-
/**
|
|
9
|
-
* Add a new value in the `ProviderRegistry`.
|
|
10
|
-
*
|
|
11
|
-
* #### Example with symbol definition
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import {registerValue, InjectorService} from "@tsed/di";
|
|
16
|
-
*
|
|
17
|
-
* const MyValue = Symbol.from("MyValue")
|
|
18
|
-
*
|
|
19
|
-
* registerValue({token: MyValue, useValue: "myValue"});
|
|
20
|
-
*
|
|
21
|
-
* @Service()
|
|
22
|
-
* export class OtherService {
|
|
23
|
-
* constructor(@Inject(MyValue) myValue: string){
|
|
24
|
-
* console.log(myValue); /// "myValue"
|
|
25
|
-
* }
|
|
26
|
-
* }
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export declare const registerValue: (provider: any | ProviderOpts<any>, value?: any) => void;
|
|
30
|
-
/**
|
|
31
|
-
* Add a new controller in the `ProviderRegistry`. This controller will be built when `InjectorService` will be loaded.
|
|
32
|
-
*
|
|
33
|
-
* #### Example
|
|
34
|
-
*
|
|
35
|
-
* ```typescript
|
|
36
|
-
* import {registerController, InjectorService} from "@tsed/di";
|
|
37
|
-
*
|
|
38
|
-
* export default class MyController {
|
|
39
|
-
* constructor(){}
|
|
40
|
-
* transform() {
|
|
41
|
-
* return "test";
|
|
42
|
-
* }
|
|
43
|
-
* }
|
|
44
|
-
*
|
|
45
|
-
* registerController({provide: MyController});
|
|
46
|
-
* // or
|
|
47
|
-
* registerController(MyController);
|
|
48
|
-
*
|
|
49
|
-
* const injector = new InjectorService();
|
|
50
|
-
* injector.load();
|
|
51
|
-
*
|
|
52
|
-
* const myController = injector.get<MyController>(MyController);
|
|
53
|
-
* myController.getFoo(); // test
|
|
54
|
-
* ```
|
|
55
|
-
*
|
|
56
|
-
* @param provider Provider configuration.
|
|
57
|
-
*/
|
|
58
|
-
export declare const registerController: (provider: any, instance?: any) => void;
|
|
6
|
+
export declare function registerProvider<Type = any>(provider: Partial<ProviderOpts<Type>> & Pick<ProviderOpts<Type>, "provide">): import("../index.js").Provider<any>;
|
|
@@ -47,11 +47,5 @@ export declare class DIConfiguration {
|
|
|
47
47
|
* @returns {undefined|any}
|
|
48
48
|
*/
|
|
49
49
|
get<T = any>(propertyKey: string, defaultValue?: T): T;
|
|
50
|
-
/**
|
|
51
|
-
*
|
|
52
|
-
* @param value
|
|
53
|
-
* @returns {any}
|
|
54
|
-
*/
|
|
55
|
-
resolve(value: any): any;
|
|
56
50
|
protected getRaw(propertyKey: string, defaultValue?: any): any;
|
|
57
51
|
}
|
|
@@ -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
|
|
@@ -2,8 +2,6 @@ import { InjectorService, LocalsContainer } from "../../common/index.js";
|
|
|
2
2
|
import { ContextLogger, ContextLoggerOptions } from "./ContextLogger.js";
|
|
3
3
|
export interface DIContextOptions extends Omit<ContextLoggerOptions, "dateStart"> {
|
|
4
4
|
id: string;
|
|
5
|
-
injector: InjectorService;
|
|
6
|
-
logger: any;
|
|
7
5
|
platform?: string;
|
|
8
6
|
}
|
|
9
7
|
export declare class DIContext {
|
|
@@ -49,8 +47,6 @@ export declare class DIContext {
|
|
|
49
47
|
*/
|
|
50
48
|
get container(): LocalsContainer;
|
|
51
49
|
destroy(): Promise<any>;
|
|
52
|
-
emit(eventName: string, ...args: any[]): Promise<void>;
|
|
53
|
-
runInContext<Result = unknown>(next: (...args: unknown[]) => Result): Promise<Result>;
|
|
54
50
|
cache<Value = any>(key: string, cb: () => Value): Value;
|
|
55
51
|
cacheAsync<Value = any>(key: string, cb: () => Promise<Value>): Promise<Value>;
|
|
56
52
|
delete(key: any): boolean;
|
|
@@ -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;
|