@tdi2/di-core
Version:
TypeScript Dependency Injection 2 - Core DI framework
129 lines (127 loc) • 3.88 kB
TypeScript
type Constructor<T = object> = new (...args: any[]) => T;
interface ServiceMetadata {
token?: string | symbol;
scope?: "singleton" | "transient" | "scoped";
implementation: Constructor;
autoResolve?: boolean;
profiles?: string[];
primary?: boolean;
qualifier?: string;
}
interface DIContainer {
register<T>(token: string | symbol, implementation: Constructor<T>, scope?: "singleton" | "transient" | "scoped"): void;
resolve<T>(token: string | symbol): T;
has(token: string | symbol): boolean;
createScope(): DIContainer;
registerByInterface<T>(interfaceName: string, implementation: () => T, scope?: "singleton" | "transient" | "scoped"): void;
resolveByInterface<T>(interfaceName: string): T;
hasInterface(interfaceName: string): boolean;
}
interface ServiceOptions {
scope?: "singleton" | "transient" | "scoped";
token?: string | symbol;
profiles?: string[];
primary?: boolean;
qualifier?: string;
}
interface InjectMetadata {
token?: string | symbol;
propertyKey?: string | symbol;
parameterIndex?: number;
target: string;
autoResolve?: boolean;
optional?: boolean;
qualifier?: string;
}
type ServiceFactory<T> = (container?: any) => T;
type ServiceFactoryFactory<T> = (container?: any) => () => T;
interface DIMap {
[token: string]: {
factory: ServiceFactory<any> | ServiceFactoryFactory<any>;
scope: "singleton" | "transient" | "scoped";
dependencies: string[];
interfaceName?: string;
implementationClass: string;
isAutoResolved: boolean;
qualifier?: string;
};
}
interface InterfaceMapping {
[interfaceName: string]: {
implementations: string[];
primary?: string;
tokens: string[];
};
}
interface ContainerConfiguration {
diMap: DIMap;
interfaceMapping: InterfaceMapping;
profiles?: string[];
environment?: string;
}
interface ValidationResult {
isValid: boolean;
errors: ValidationError[];
warnings: ValidationWarning[];
}
interface ValidationError {
type: "missing-implementation" | "circular-dependency" | "ambiguous-implementation";
message: string;
details: any;
}
interface ValidationWarning {
type: "optional-missing" | "multiple-implementations" | "unused-service";
message: string;
details: any;
}
interface InterfaceResolutionData {
interfaceName: string;
sanitizedKey: string;
implementations: Array<{
className: string;
filePath: string;
isPrimary: boolean;
qualifier?: string;
scope: string;
}>;
isGeneric: boolean;
typeParameters: string[];
}
interface DependencyNode {
id: string;
interfaceName?: string;
dependencies: DependencyEdge[];
scope: string;
isAutoResolved: boolean;
}
interface DependencyEdge {
targetInterface: string;
targetImplementation?: string;
isOptional: boolean;
qualifier?: string;
}
interface EnvironmentConfig {
name: string;
profiles: string[];
overrides: {
[interfaceName: string]: string;
};
mocks: {
[interfaceName: string]: any;
};
}
interface DebugInfo {
configHash: string;
environment: string;
activeProfiles: string[];
interfaceResolutions: InterfaceResolutionData[];
dependencyGraph: DependencyNode[];
validation: ValidationResult;
containerStats: {
totalServices: number;
autoResolvedServices: number;
manualTokenServices: number;
interfacesWithMultipleImpls: number;
};
}
export type { Constructor, ContainerConfiguration, DIContainer, DIMap, DebugInfo, DependencyEdge, DependencyNode, EnvironmentConfig, InjectMetadata, InterfaceMapping, InterfaceResolutionData, ServiceFactory, ServiceMetadata, ServiceOptions, ValidationError, ValidationResult, ValidationWarning };