@tsed/di 8.18.2 → 8.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/common/decorators/autoInjectable.js +28 -9
- package/lib/esm/common/decorators/constant.js +26 -22
- package/lib/esm/common/decorators/controller.js +40 -20
- package/lib/esm/common/decorators/injectable.js +30 -10
- package/lib/esm/common/decorators/intercept.js +41 -3
- package/lib/esm/common/decorators/interceptor.js +31 -4
- package/lib/esm/common/decorators/module.js +36 -5
- package/lib/esm/common/decorators/opts.js +20 -12
- package/lib/esm/common/decorators/overrideProvider.js +38 -4
- package/lib/esm/common/decorators/scope.js +32 -3
- package/lib/esm/common/decorators/service.js +24 -4
- package/lib/esm/common/decorators/useOpts.js +21 -13
- package/lib/esm/common/decorators/value.js +28 -22
- package/lib/esm/common/domain/Container.js +20 -0
- package/lib/esm/common/domain/ContextLogger.js +21 -0
- package/lib/esm/common/domain/ControllerProvider.js +22 -0
- package/lib/esm/common/domain/DIContext.js +22 -0
- package/lib/esm/common/domain/InjectablePropertyType.js +16 -0
- package/lib/esm/common/domain/LocalsContainer.js +21 -0
- package/lib/esm/common/domain/Provider.js +19 -0
- package/lib/esm/common/domain/ProviderScope.js +25 -0
- package/lib/esm/common/domain/ProviderType.js +25 -0
- package/lib/esm/common/errors/InjectionError.js +24 -0
- package/lib/esm/common/errors/InvalidPropertyTokenError.js +15 -0
- package/lib/esm/common/fn/inject.js +19 -6
- package/lib/esm/common/fn/injectMany.js +24 -6
- package/lib/esm/common/fn/injectable.js +57 -0
- package/lib/esm/common/fn/injector.js +21 -2
- package/lib/esm/common/fn/lazyInject.js +48 -1
- package/lib/esm/common/fn/localsContainer.js +31 -2
- package/lib/esm/common/fn/logger.js +15 -1
- package/lib/esm/common/services/DIConfiguration.js +24 -0
- package/lib/esm/common/services/DILogger.js +23 -0
- package/lib/esm/common/services/InjectorService.js +89 -43
- package/lib/types/common/decorators/autoInjectable.d.ts +31 -10
- package/lib/types/common/decorators/configuration.d.ts +32 -2
- package/lib/types/common/decorators/constant.d.ts +26 -22
- package/lib/types/common/decorators/controller.d.ts +40 -20
- package/lib/types/common/decorators/inject.d.ts +29 -7
- package/lib/types/common/decorators/injectable.d.ts +30 -10
- package/lib/types/common/decorators/intercept.d.ts +41 -3
- package/lib/types/common/decorators/interceptor.d.ts +31 -4
- package/lib/types/common/decorators/lazyInject.d.ts +43 -9
- package/lib/types/common/decorators/module.d.ts +36 -5
- package/lib/types/common/decorators/opts.d.ts +20 -12
- package/lib/types/common/decorators/overrideProvider.d.ts +38 -4
- package/lib/types/common/decorators/scope.d.ts +32 -3
- package/lib/types/common/decorators/service.d.ts +24 -4
- package/lib/types/common/decorators/useOpts.d.ts +21 -13
- package/lib/types/common/decorators/value.d.ts +28 -22
- package/lib/types/common/domain/Container.d.ts +20 -0
- package/lib/types/common/domain/ContextLogger.d.ts +26 -0
- package/lib/types/common/domain/ControllerProvider.d.ts +29 -0
- package/lib/types/common/domain/DIContext.d.ts +27 -0
- package/lib/types/common/domain/InjectablePropertyType.d.ts +16 -0
- package/lib/types/common/domain/LocalsContainer.d.ts +21 -0
- package/lib/types/common/domain/Provider.d.ts +25 -0
- package/lib/types/common/domain/ProviderScope.d.ts +25 -0
- package/lib/types/common/domain/ProviderType.d.ts +25 -0
- package/lib/types/common/errors/InjectionError.d.ts +24 -0
- package/lib/types/common/errors/InvalidPropertyTokenError.d.ts +15 -0
- package/lib/types/common/fn/configuration.d.ts +31 -0
- package/lib/types/common/fn/constant.d.ts +22 -0
- package/lib/types/common/fn/inject.d.ts +19 -6
- package/lib/types/common/fn/injectMany.d.ts +24 -6
- package/lib/types/common/fn/injectable.d.ts +57 -0
- package/lib/types/common/fn/injector.d.ts +21 -2
- package/lib/types/common/fn/lazyInject.d.ts +48 -1
- package/lib/types/common/fn/localsContainer.d.ts +31 -2
- package/lib/types/common/fn/logger.d.ts +15 -1
- package/lib/types/common/fn/refValue.d.ts +15 -6
- package/lib/types/common/interfaces/AlterRunInContext.d.ts +26 -0
- package/lib/types/common/interfaces/DILogger.d.ts +36 -0
- package/lib/types/common/interfaces/DILoggerOptions.d.ts +23 -0
- package/lib/types/common/interfaces/ImportTokenProviderOpts.d.ts +43 -0
- package/lib/types/common/interfaces/InterceptorContext.d.ts +35 -0
- package/lib/types/common/interfaces/InterceptorMethods.d.ts +27 -0
- package/lib/types/common/interfaces/InvokeOptions.d.ts +8 -0
- package/lib/types/common/interfaces/OnDestroy.d.ts +22 -0
- package/lib/types/common/interfaces/OnInit.d.ts +22 -0
- package/lib/types/common/interfaces/ProviderOpts.d.ts +31 -0
- package/lib/types/common/interfaces/ResolvedInvokeOptions.d.ts +9 -0
- package/lib/types/common/interfaces/TokenProvider.d.ts +32 -2
- package/lib/types/common/interfaces/TokenRoute.d.ts +16 -0
- package/lib/types/common/services/DIConfiguration.d.ts +24 -0
- package/lib/types/common/services/DILogger.d.ts +28 -0
- package/lib/types/common/services/InjectorService.d.ts +89 -43
- package/lib/types/node/utils/asyncHookContext.d.ts +0 -1
- package/package.json +6 -6
|
@@ -1,10 +1,39 @@
|
|
|
1
1
|
import { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
2
2
|
import { Provider } from "./Provider.js";
|
|
3
|
+
/**
|
|
4
|
+
* Middleware configuration for a controller.
|
|
5
|
+
*
|
|
6
|
+
* Defines middleware tokens to be applied at different stages of request processing.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
3
10
|
export interface ControllerMiddlewares {
|
|
4
11
|
useBefore: TokenProvider[];
|
|
5
12
|
use: TokenProvider[];
|
|
6
13
|
useAfter: TokenProvider[];
|
|
7
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Specialized provider for HTTP controllers.
|
|
17
|
+
*
|
|
18
|
+
* Extends the base `Provider` class with controller-specific metadata like middleware configuration
|
|
19
|
+
* and routing information. Used internally to manage HTTP route controllers in the DI system.
|
|
20
|
+
*
|
|
21
|
+
* ### Usage
|
|
22
|
+
*
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import {ControllerProvider} from "@tsed/di";
|
|
25
|
+
*
|
|
26
|
+
* const provider = new ControllerProvider(MyController);
|
|
27
|
+
* provider.middlewares = {
|
|
28
|
+
* useBefore: [AuthMiddleware],
|
|
29
|
+
* use: [ValidationMiddleware],
|
|
30
|
+
* useAfter: [LoggingMiddleware]
|
|
31
|
+
* };
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @typeParam T - The type of controller class
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
8
37
|
export declare class ControllerProvider<T = any> extends Provider<T> {
|
|
9
38
|
tokenRouter: string;
|
|
10
39
|
constructor(token: TokenProvider, options?: Partial<Provider>);
|
|
@@ -1,9 +1,36 @@
|
|
|
1
1
|
import { InjectorService, LocalsContainer } from "../index.js";
|
|
2
2
|
import { ContextLogger, ContextLoggerOptions } from "./ContextLogger.js";
|
|
3
|
+
/**
|
|
4
|
+
* Options for creating a DI context instance.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
3
8
|
export interface DIContextOptions extends Omit<ContextLoggerOptions, "dateStart"> {
|
|
4
9
|
id: string;
|
|
5
10
|
platform?: string;
|
|
6
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Execution context for dependency injection operations.
|
|
14
|
+
*
|
|
15
|
+
* Represents a single execution scope (typically an HTTP request) with its own
|
|
16
|
+
* scoped provider instances, logger, and lifecycle management. Provides access to
|
|
17
|
+
* request-scoped services and context-specific data.
|
|
18
|
+
*
|
|
19
|
+
* ### Usage
|
|
20
|
+
*
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import {DIContext} from "@tsed/di";
|
|
23
|
+
*
|
|
24
|
+
* const context = new DIContext({id: "req-123"});
|
|
25
|
+
*
|
|
26
|
+
* context.logger.info("Processing request");
|
|
27
|
+
* const service = await context.injector.invoke(MyService, context.container);
|
|
28
|
+
*
|
|
29
|
+
* await context.destroy(); // Cleanup when done
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
7
34
|
export declare class DIContext {
|
|
8
35
|
#private;
|
|
9
36
|
opts: DIContextOptions;
|
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enumeration defining types of injectable properties and decorators.
|
|
3
|
+
*
|
|
4
|
+
* Used internally to track and classify different kinds of injected dependencies
|
|
5
|
+
* and decorated members within a class.
|
|
6
|
+
*
|
|
7
|
+
* ### Types
|
|
8
|
+
*
|
|
9
|
+
* - `METHOD`: Injectable method parameter
|
|
10
|
+
* - `PROPERTY`: Injectable class property
|
|
11
|
+
* - `CONSTANT`: Injected constant value
|
|
12
|
+
* - `VALUE`: Injected configured value
|
|
13
|
+
* - `INTERCEPTOR`: Interceptor applied to a method
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
1
17
|
export declare enum InjectablePropertyType {
|
|
2
18
|
METHOD = "method",
|
|
3
19
|
PROPERTY = "property",
|
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
import { Hooks } from "@tsed/hooks";
|
|
2
2
|
import type { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
3
|
+
/**
|
|
4
|
+
* Container for request-scoped and instance-scoped provider instances.
|
|
5
|
+
*
|
|
6
|
+
* Stores instances created within a specific execution context (like an HTTP request).
|
|
7
|
+
* Manages lifecycle hooks and destruction of scoped instances when the context ends.
|
|
8
|
+
*
|
|
9
|
+
* ### Usage
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import {LocalsContainer} from "@tsed/di";
|
|
13
|
+
*
|
|
14
|
+
* const locals = new LocalsContainer();
|
|
15
|
+
* locals.set(MyService, serviceInstance);
|
|
16
|
+
*
|
|
17
|
+
* const instance = locals.get(MyService);
|
|
18
|
+
*
|
|
19
|
+
* await locals.destroy(); // Cleanup when context ends
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
3
24
|
export declare class LocalsContainer extends Map<TokenProvider, any> {
|
|
4
25
|
readonly hooks: Hooks;
|
|
5
26
|
destroy(): Promise<void>;
|
|
@@ -4,7 +4,32 @@ import type { ProviderOpts } from "../interfaces/ProviderOpts.js";
|
|
|
4
4
|
import type { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
5
5
|
import { ProviderScope } from "./ProviderScope.js";
|
|
6
6
|
import { ProviderType } from "./ProviderType.js";
|
|
7
|
+
/**
|
|
8
|
+
* Callback function type for provider lifecycle hooks.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T - The type of provider instance
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
7
13
|
export type ProviderHookCallback<T = any> = (instance: T, ...args: unknown[]) => Promise<void> | void;
|
|
14
|
+
/**
|
|
15
|
+
* Provider metadata class managing dependency injection configuration.
|
|
16
|
+
*
|
|
17
|
+
* Encapsulates all information needed to create, configure, and manage a provider instance
|
|
18
|
+
* within the DI container. Tracks dependencies, lifecycle hooks, creation strategies, and metadata.
|
|
19
|
+
*
|
|
20
|
+
* ### Usage
|
|
21
|
+
*
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import {Provider, ProviderType} from "@tsed/di";
|
|
24
|
+
*
|
|
25
|
+
* const provider = new Provider(MyService);
|
|
26
|
+
* provider.type = ProviderType.PROVIDER;
|
|
27
|
+
* provider.deps = [DatabaseService, LoggerService];
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @typeParam T - The type of instance this provider creates
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
8
33
|
export declare class Provider<T = any> implements ProviderOpts<T> {
|
|
9
34
|
#private;
|
|
10
35
|
/**
|
|
@@ -1,3 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enumeration defining the lifecycle scopes for provider instances.
|
|
3
|
+
*
|
|
4
|
+
* Determines when and how provider instances are created and shared across the application.
|
|
5
|
+
*
|
|
6
|
+
* ### Scopes
|
|
7
|
+
*
|
|
8
|
+
* - `SINGLETON`: One instance shared across the entire application (default)
|
|
9
|
+
* - `REQUEST`: New instance created for each request/context
|
|
10
|
+
* - `INSTANCE`: New instance created on every injection
|
|
11
|
+
*
|
|
12
|
+
* ### Usage
|
|
13
|
+
*
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import {Injectable, Scope, ProviderScope} from "@tsed/di";
|
|
16
|
+
*
|
|
17
|
+
* @Injectable()
|
|
18
|
+
* @Scope(ProviderScope.REQUEST)
|
|
19
|
+
* class RequestScopedService {
|
|
20
|
+
* // New instance per request
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
1
26
|
export declare enum ProviderScope {
|
|
2
27
|
SINGLETON = "singleton",
|
|
3
28
|
REQUEST = "request",
|
|
@@ -1,3 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enumeration categorizing different types of providers in the DI system.
|
|
3
|
+
*
|
|
4
|
+
* Used to classify and filter providers based on their role in the application architecture.
|
|
5
|
+
* Each type represents a distinct pattern or responsibility within the framework.
|
|
6
|
+
*
|
|
7
|
+
* ### Types
|
|
8
|
+
*
|
|
9
|
+
* - `VALUE`: Simple value providers (constants, configuration)
|
|
10
|
+
* - `PROVIDER`: Standard service providers
|
|
11
|
+
* - `MODULE`: Module providers organizing related functionality
|
|
12
|
+
* - `CONTROLLER`: HTTP route controllers
|
|
13
|
+
* - `INTERCEPTOR`: Method interceptors for cross-cutting concerns
|
|
14
|
+
* - `MIDDLEWARE`: Request/response middleware
|
|
15
|
+
*
|
|
16
|
+
* ### Usage
|
|
17
|
+
*
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import {ProviderType} from "@tsed/di";
|
|
20
|
+
*
|
|
21
|
+
* const providers = container.getProviders(ProviderType.CONTROLLER);
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
1
26
|
export declare enum ProviderType {
|
|
2
27
|
VALUE = "value",
|
|
3
28
|
PROVIDER = "provider",
|
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
import { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when dependency injection fails.
|
|
4
|
+
*
|
|
5
|
+
* Provides detailed information about the failed injection including the dependency chain
|
|
6
|
+
* and the original error. Helps diagnose circular dependencies, missing providers, and
|
|
7
|
+
* configuration issues in the DI system.
|
|
8
|
+
*
|
|
9
|
+
* ### Usage
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import {InjectionError} from "@tsed/di";
|
|
13
|
+
*
|
|
14
|
+
* try {
|
|
15
|
+
* injector.invoke(MyService);
|
|
16
|
+
* } catch (error) {
|
|
17
|
+
* if (error instanceof InjectionError) {
|
|
18
|
+
* console.log("Dependency chain:", error.tokens);
|
|
19
|
+
* console.log("Original error:", error.origin);
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
2
26
|
export declare class InjectionError extends Error {
|
|
3
27
|
name: string;
|
|
4
28
|
tokens: TokenProvider[];
|
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when an invalid token is used for property injection.
|
|
3
|
+
*
|
|
4
|
+
* Typically indicates a circular reference, missing import, or undefined token
|
|
5
|
+
* in property decorators like `@Inject()` or `@Constant()`.
|
|
6
|
+
*
|
|
7
|
+
* ### Common causes
|
|
8
|
+
*
|
|
9
|
+
* - Circular dependency between classes
|
|
10
|
+
* - Missing or incorrect import statement
|
|
11
|
+
* - Using `undefined` as a token
|
|
12
|
+
* - Forward reference not properly handled
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
1
16
|
export declare class InvalidPropertyTokenError extends Error {
|
|
2
17
|
name: string;
|
|
3
18
|
constructor(target: any, propertyKey: string);
|
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
import type { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
2
2
|
import { DIConfiguration } from "../services/DIConfiguration.js";
|
|
3
|
+
/**
|
|
4
|
+
* Get or set configuration for a provider or the global injector settings.
|
|
5
|
+
*
|
|
6
|
+
* ### Overloads
|
|
7
|
+
*
|
|
8
|
+
* - `configuration()` - Returns the global DI configuration
|
|
9
|
+
* - `configuration(token)` - Returns the stored configuration for a provider
|
|
10
|
+
* - `configuration(token, config)` - Sets and returns configuration for a provider
|
|
11
|
+
*
|
|
12
|
+
* ### Usage
|
|
13
|
+
*
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import {configuration, Injectable} from "@tsed/di";
|
|
16
|
+
*
|
|
17
|
+
* // Get global configuration
|
|
18
|
+
* const settings = configuration();
|
|
19
|
+
*
|
|
20
|
+
* // Set provider configuration
|
|
21
|
+
* @Injectable()
|
|
22
|
+
* class MyService {}
|
|
23
|
+
* configuration(MyService, {custom: "value"});
|
|
24
|
+
*
|
|
25
|
+
* // Get provider configuration
|
|
26
|
+
* const config = configuration(MyService);
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param token Optional provider token to get/set configuration for
|
|
30
|
+
* @param configuration Optional configuration to set
|
|
31
|
+
* @returns The configuration object
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
3
34
|
export declare function configuration(): TsED.Configuration & DIConfiguration;
|
|
4
35
|
export declare function configuration(token: TokenProvider): Partial<TsED.Configuration>;
|
|
5
36
|
export declare function configuration(token: TokenProvider, configuration: Partial<TsED.Configuration>): Partial<TsED.Configuration>;
|
|
@@ -1,2 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a constant value from the injector configuration settings.
|
|
3
|
+
*
|
|
4
|
+
* Retrieves a configuration value using dot notation path syntax.
|
|
5
|
+
* Useful for accessing environment variables, configuration constants, or settings.
|
|
6
|
+
*
|
|
7
|
+
* ### Usage
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import {constant} from "@tsed/di";
|
|
11
|
+
*
|
|
12
|
+
* const apiKey = constant<string>("api.key");
|
|
13
|
+
* const port = constant<number>("server.port", 3000);
|
|
14
|
+
* const debug = constant<boolean>("logger.debug", false);
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @typeParam Type The expected type of the constant value
|
|
18
|
+
* @param expression Dot-notation path to the configuration value (e.g., "logger.level")
|
|
19
|
+
* @param defaultValue Optional default value if the expression is not found
|
|
20
|
+
* @returns The configuration value or undefined
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
1
23
|
export declare function constant<Type>(expression: string): Type | undefined;
|
|
2
24
|
export declare function constant<Type>(expression: string, defaultValue: Type | undefined): Type;
|
|
@@ -1,20 +1,33 @@
|
|
|
1
1
|
import type { InvokeOptions } from "../interfaces/InvokeOptions.js";
|
|
2
2
|
import type { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Programmatically inject a provider instance.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* Resolves and returns a provider instance using the global injector.
|
|
7
|
+
* Useful for injecting dependencies outside of decorators or in property initializers.
|
|
8
|
+
*
|
|
9
|
+
* ### Usage
|
|
7
10
|
*
|
|
8
11
|
* ```typescript
|
|
12
|
+
* import {inject, Injectable} from "@tsed/di";
|
|
13
|
+
*
|
|
9
14
|
* @Injectable()
|
|
10
15
|
* export class MyService {
|
|
16
|
+
* // Property injection
|
|
11
17
|
* connection = inject(CONNECTION);
|
|
18
|
+
*
|
|
19
|
+
* // Function injection
|
|
20
|
+
* getData() {
|
|
21
|
+
* const api = inject(ApiService);
|
|
22
|
+
* return api.fetch();
|
|
23
|
+
* }
|
|
12
24
|
* }
|
|
13
25
|
* ```
|
|
14
26
|
*
|
|
15
|
-
* @
|
|
16
|
-
* @param
|
|
17
|
-
* @
|
|
18
|
-
* @
|
|
27
|
+
* @typeParam TokenType The type of the injected provider
|
|
28
|
+
* @param token The provider token to resolve
|
|
29
|
+
* @param opts Optional invocation options (locals, rebuild, useOpts)
|
|
30
|
+
* @returns The resolved provider instance
|
|
31
|
+
* @public
|
|
19
32
|
*/
|
|
20
33
|
export declare function inject<TokenType>(token: TokenProvider<TokenType>, opts?: Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>): TokenType;
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import type { InvokeOptions } from "../interfaces/InvokeOptions.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
3
|
+
* Inject all providers matching a given token type.
|
|
4
|
+
*
|
|
5
|
+
* Resolves multiple provider instances that share the same type or group token.
|
|
6
|
+
* Useful for getting all providers of a specific category (e.g., all controllers, all middleware).
|
|
7
|
+
*
|
|
8
|
+
* ### Usage
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import {injectMany, ProviderType} from "@tsed/di";
|
|
12
|
+
*
|
|
13
|
+
* // Get all controllers
|
|
14
|
+
* const controllers = injectMany(ProviderType.CONTROLLER);
|
|
15
|
+
*
|
|
16
|
+
* // Get all providers of a custom type
|
|
17
|
+
* const plugins = injectMany(Symbol.for("PLUGIN"));
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @typeParam T The type of instances to return
|
|
21
|
+
* @param token The injection token or type to resolve
|
|
22
|
+
* @param opts Optional invocation options
|
|
23
|
+
* @param opts.useOpts Options passed to instance creation
|
|
24
|
+
* @param opts.rebuild Whether to rebuild instances
|
|
25
|
+
* @param opts.locals Local container overrides
|
|
9
26
|
* @returns Array of resolved instances
|
|
27
|
+
* @public
|
|
10
28
|
*/
|
|
11
29
|
export declare function injectMany<T>(token: string | symbol, opts?: Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>): T[];
|
|
@@ -1,7 +1,64 @@
|
|
|
1
1
|
import { ControllerProvider } from "../domain/ControllerProvider.js";
|
|
2
2
|
import type { Provider } from "../domain/Provider.js";
|
|
3
3
|
type PickedProps = "scope" | "path" | "alias" | "hooks" | "deps" | "imports" | "configuration" | "priority";
|
|
4
|
+
/**
|
|
5
|
+
* Fluent builder for registering providers programmatically.
|
|
6
|
+
*
|
|
7
|
+
* Creates or configures a provider with the specified options.
|
|
8
|
+
* Can be chained with additional configuration methods.
|
|
9
|
+
*
|
|
10
|
+
* ### Usage
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import {injectable} from "@tsed/di";
|
|
14
|
+
*
|
|
15
|
+
* injectable(MyService)
|
|
16
|
+
* .scope(ProviderScope.REQUEST)
|
|
17
|
+
* .deps([DatabaseService])
|
|
18
|
+
* .build();
|
|
19
|
+
*
|
|
20
|
+
* // With factory
|
|
21
|
+
* injectable("CONFIG")
|
|
22
|
+
* .factory(() => loadConfig())
|
|
23
|
+
* .build();
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
4
28
|
export declare const injectable: import("../utils/providerBuilder.js").ProviderBuilderFn<Provider<any>, "type" | PickedProps>;
|
|
29
|
+
/**
|
|
30
|
+
* Fluent builder for registering interceptor providers.
|
|
31
|
+
*
|
|
32
|
+
* Specialized version of `injectable` that automatically sets the provider type to `INTERCEPTOR`.
|
|
33
|
+
*
|
|
34
|
+
* ### Usage
|
|
35
|
+
*
|
|
36
|
+
* ```typescript
|
|
37
|
+
* import {interceptor} from "@tsed/di";
|
|
38
|
+
*
|
|
39
|
+
* interceptor(LogInterceptor).build();
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
5
44
|
export declare const interceptor: import("../utils/providerBuilder.js").ProviderBuilderFn<Provider<any>, "type" | PickedProps>;
|
|
45
|
+
/**
|
|
46
|
+
* Fluent builder for registering controller providers.
|
|
47
|
+
*
|
|
48
|
+
* Specialized version of `injectable` that automatically sets the provider type to `CONTROLLER`
|
|
49
|
+
* and supports controller-specific options like middlewares.
|
|
50
|
+
*
|
|
51
|
+
* ### Usage
|
|
52
|
+
*
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import {controller} from "@tsed/di";
|
|
55
|
+
*
|
|
56
|
+
* controller(UsersController)
|
|
57
|
+
* .middlewares({useBefore: [AuthMiddleware]})
|
|
58
|
+
* .build();
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
6
63
|
export declare const controller: import("../utils/providerBuilder.js").ProviderBuilderFn<ControllerProvider<any>, "children" | "middlewares" | PickedProps>;
|
|
7
64
|
export {};
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { InjectorService } from "../services/InjectorService.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Get the global injector service instance.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* Returns the singleton instance of `InjectorService` managing all registered providers.
|
|
6
|
+
* Use this to access the DI container programmatically.
|
|
7
|
+
*
|
|
8
|
+
* ### Usage
|
|
6
9
|
*
|
|
7
10
|
* ```typescript
|
|
8
11
|
* import {injector, Injectable} from "@tsed/di";
|
|
@@ -10,8 +13,24 @@ import { InjectorService } from "../services/InjectorService.js";
|
|
|
10
13
|
* @Injectable()
|
|
11
14
|
* class MyService {
|
|
12
15
|
* injector = injector();
|
|
16
|
+
*
|
|
17
|
+
* getService() {
|
|
18
|
+
* return injector().get(OtherService);
|
|
19
|
+
* }
|
|
13
20
|
* }
|
|
14
21
|
* ```
|
|
22
|
+
*
|
|
23
|
+
* @returns The global injector service instance
|
|
24
|
+
* @public
|
|
15
25
|
*/
|
|
16
26
|
export declare function injector(): InjectorService;
|
|
27
|
+
/**
|
|
28
|
+
* Destroy the current injector and create a fresh instance.
|
|
29
|
+
*
|
|
30
|
+
* Calls the `destroy()` lifecycle hook on all providers and resets the global injector.
|
|
31
|
+
* Primarily used in testing to reset the DI container between tests.
|
|
32
|
+
*
|
|
33
|
+
* @returns Promise that resolves when destruction is complete
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
17
36
|
export declare function destroyInjector(): Promise<void>;
|
|
@@ -1,10 +1,57 @@
|
|
|
1
1
|
import type { TokenProvider } from "../interfaces/TokenProvider.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Lazily load and inject a provider from a dynamic import.
|
|
4
|
+
*
|
|
5
|
+
* Loads the provider module asynchronously, registers it if needed, calls `$onInit` if present,
|
|
6
|
+
* and returns the resolved instance. Useful for code splitting and lazy loading dependencies.
|
|
7
|
+
*
|
|
8
|
+
* ### Usage
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import {lazyInject} from "@tsed/di";
|
|
12
|
+
*
|
|
13
|
+
* // Lazy load a service
|
|
14
|
+
* const service = await lazyInject(() => import("./HeavyService"));
|
|
15
|
+
*
|
|
16
|
+
* // Use in a method
|
|
17
|
+
* async loadPlugin() {
|
|
18
|
+
* const plugin = await lazyInject(() => import("./plugins/MyPlugin"));
|
|
19
|
+
* return plugin.initialize();
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @typeParam Token The type of the provider to inject
|
|
24
|
+
* @param factory Function returning a dynamic import promise with a default export
|
|
25
|
+
* @returns Promise resolving to the provider instance
|
|
26
|
+
* @public
|
|
4
27
|
*/
|
|
5
28
|
export declare function lazyInject<Token>(factory: () => Promise<{
|
|
6
29
|
default: TokenProvider<Token>;
|
|
7
30
|
}>): Promise<Token>;
|
|
31
|
+
/**
|
|
32
|
+
* Attempt to lazily load and inject a provider, returning undefined on failure.
|
|
33
|
+
*
|
|
34
|
+
* Similar to `lazyInject` but catches errors and returns undefined instead of throwing.
|
|
35
|
+
* Useful for optional dependencies that may not be available.
|
|
36
|
+
*
|
|
37
|
+
* ### Usage
|
|
38
|
+
*
|
|
39
|
+
* ```typescript
|
|
40
|
+
* import {optionalLazyInject} from "@tsed/di";
|
|
41
|
+
*
|
|
42
|
+
* // Try to load optional plugin
|
|
43
|
+
* const plugin = await optionalLazyInject(() => import("./optional/Plugin"));
|
|
44
|
+
*
|
|
45
|
+
* if (plugin) {
|
|
46
|
+
* plugin.activate();
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @typeParam Token The type of the provider to inject
|
|
51
|
+
* @param factory Function returning a dynamic import promise with a default export
|
|
52
|
+
* @returns Promise resolving to the provider instance or undefined if loading fails
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
8
55
|
export declare function optionalLazyInject<Token>(factory: () => Promise<{
|
|
9
56
|
default: TokenProvider;
|
|
10
57
|
}>): Promise<Token | undefined>;
|
|
@@ -1,7 +1,31 @@
|
|
|
1
1
|
import { LocalsContainer } from "../domain/LocalsContainer.js";
|
|
2
2
|
import type { UseImportTokenProviderOpts } from "../interfaces/ImportTokenProviderOpts.js";
|
|
3
3
|
/**
|
|
4
|
-
* Get the locals container
|
|
4
|
+
* Get or create the current locals container for request-scoped providers.
|
|
5
|
+
*
|
|
6
|
+
* Returns the global locals container used for storing request-scoped provider instances.
|
|
7
|
+
* Can optionally create a new container with custom providers.
|
|
8
|
+
*
|
|
9
|
+
* ### Usage
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import {localsContainer} from "@tsed/di";
|
|
13
|
+
*
|
|
14
|
+
* // Get current locals
|
|
15
|
+
* const locals = localsContainer();
|
|
16
|
+
*
|
|
17
|
+
* // Create with custom providers
|
|
18
|
+
* const locals = localsContainer({
|
|
19
|
+
* providers: [{token: MyService, use: mockInstance}],
|
|
20
|
+
* rebuild: true
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @param options Configuration options
|
|
25
|
+
* @param options.providers Optional array of provider overrides
|
|
26
|
+
* @param options.rebuild Whether to mark providers for rebuilding
|
|
27
|
+
* @returns The locals container instance
|
|
28
|
+
* @public
|
|
5
29
|
*/
|
|
6
30
|
export declare function localsContainer({ providers, rebuild }?: {
|
|
7
31
|
providers?: UseImportTokenProviderOpts[];
|
|
@@ -9,7 +33,12 @@ export declare function localsContainer({ providers, rebuild }?: {
|
|
|
9
33
|
}): LocalsContainer;
|
|
10
34
|
export declare function invokeOptions(): any;
|
|
11
35
|
/**
|
|
12
|
-
*
|
|
36
|
+
* Detach and stage the current locals container.
|
|
37
|
+
*
|
|
38
|
+
* Removes the global locals reference and saves it for cleanup.
|
|
39
|
+
* Used internally to manage request scope lifecycle.
|
|
40
|
+
*
|
|
41
|
+
* @public
|
|
13
42
|
*/
|
|
14
43
|
export declare function detachLocalsContainer(): void;
|
|
15
44
|
export declare function cleanAllLocalsContainer(): void;
|
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Get the current
|
|
2
|
+
* Get the current logger instance from the injector.
|
|
3
|
+
*
|
|
4
|
+
* Returns the configured logger (defaults to console if not set).
|
|
5
|
+
*
|
|
6
|
+
* ### Usage
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import {logger} from "@tsed/di";
|
|
10
|
+
*
|
|
11
|
+
* logger().info("Application started");
|
|
12
|
+
* logger().error("An error occurred", error);
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @returns The logger instance
|
|
16
|
+
* @public
|
|
3
17
|
*/
|
|
4
18
|
export declare function logger(): import("../index.js").DILogger;
|
|
@@ -1,21 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Create a reactive reference to a configuration value.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Returns an object with a `value` getter that always fetches the current configuration value.
|
|
5
|
+
* Useful for creating properties that stay synchronized with configuration changes.
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
+
* ### Usage
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
7
10
|
* import {refValue, Injectable} from "@tsed/di";
|
|
8
11
|
*
|
|
9
12
|
* @Injectable()
|
|
10
13
|
* class Test {
|
|
11
|
-
*
|
|
14
|
+
* logLevel = refValue<string>("logger.level", "info");
|
|
15
|
+
* port = refValue<number>("server.port");
|
|
12
16
|
*
|
|
13
17
|
* constructor() {
|
|
14
|
-
* console.log(this.
|
|
18
|
+
* console.log(this.logLevel.value); // Always current value
|
|
15
19
|
* }
|
|
16
20
|
* }
|
|
21
|
+
* ```
|
|
17
22
|
*
|
|
18
|
-
* @
|
|
23
|
+
* @typeParam Type The expected type of the value
|
|
24
|
+
* @param expression Dot-notation path to the configuration value
|
|
25
|
+
* @param defaultValue Optional default value if the expression is not found
|
|
26
|
+
* @returns A frozen object with a reactive `value` property
|
|
27
|
+
* @public
|
|
19
28
|
*/
|
|
20
29
|
export declare function refValue<Type>(expression: string): {
|
|
21
30
|
value: Type | undefined;
|