@akala/core
Version:
291 lines (290 loc) • 13.8 kB
TypeScript
/**
* Type representing a function that returns an injected value.
* @template T - The type of the injected value.
* @param instance - Optional instance context for the injected value.
* @returns The injected value of type T.
*/
export type Injected<T> = (instance?: unknown) => T;
/**
* Type representing an injectable function.
* @template T - The return type of the function.
* @template TArgs - The argument types of the function.
*/
export type Injectable<T, TArgs extends unknown[]> = (...args: TArgs) => T;
/**
* Type representing an injectable constructor.
* @template T - The instance type created by the constructor.
* @template TArgs - The argument types passed to the constructor.
*/
export type InjectableConstructor<T, TArgs extends unknown[]> = new (...args: TArgs) => T;
/**
* Type representing an injectable function with a typed 'this' context.
* @template T - The return type of the function.
* @template U - The type of the 'this' context.
* @template TArgs - The argument types of the function.
*/
export type InjectableWithTypedThis<T, U, TArgs extends unknown[]> = (this: U, ...args: TArgs) => T;
/**
* Type representing an injectable asynchronous function.
* @template T - The resolved type of the promise.
* @template TArgs - The argument types of the function.
*/
export type InjectableAsync<T, TArgs extends unknown[]> = Injectable<Promise<T>, TArgs>;
/**
* Type representing an injectable asynchronous function with a typed 'this' context.
* @template T - The resolved type of the promise.
* @template U - The type of the 'this' context.
* @template TArgs - The argument types of the function.
*/
export type InjectableAsyncWithTypedThis<T, U, TArgs extends unknown[]> = (this: U, ...args: TArgs) => PromiseLike<T>;
/**
* Type representing a parameter with its index in the argument list.
* @template T - The type of the parameter value.
*/
export type InjectedParameter<T> = {
index: number;
value: T;
};
/**
* Type representing a parameter map for dependency injection.
* @template T - The type of the object being mapped.
*/
export type InjectMap<T = object> = T extends object ? {
[key in keyof T]: Resolvable<T[key]>;
} : never;
/**
* Type representing a resolvable parameter value.
* @template T - The type of the resolved value.
*/
type SimpleResolvable<T> = InjectMap<T> | string | symbol;
export type ResolvableArray<T> = SimpleResolvable<T>[] | [ICustomResolver, ...SimpleResolvable<T>[]];
export type Resolvable<T = object> = ResolvableArray<T> | SimpleResolvable<T>;
export declare const injectorLog: import("../logging/index.browser.js").LoggerWrapper<import("../logging/index.browser.js").LoggerRoute>;
/**
* Converts a constructor to a function that creates new instances.
* @template T - The parameter types of the constructor.
* @template TResult - The instance type created by the constructor.
* @param {new (...args: T) => TResult} ctor - The constructor to convert.
* @returns {(...parameters: T) => TResult} A function that creates new instances of the constructor.
*/
export declare function ctorToFunction<T extends unknown[], TResult>(ctor: new (...args: T) => TResult): (...parameters: T) => TResult;
export declare const customResolve: unique symbol;
export interface ICustomResolver {
[customResolve]<T>(param: Resolvable): T;
}
export declare function isCustomResolver(x: unknown): x is ICustomResolver;
export type SpecificInjector<T> = Injector & {
resolve(param: Resolvable): T;
};
/**
* The `Injector` abstract class provides a framework for dependency injection,
* allowing the resolution and injection of parameters, functions, and constructors.
* It supports synchronous and asynchronous resolution of dependencies, as well as
* advanced features like nested property resolution and fallback mechanisms.
*
* Key Features:
* - Resolves parameters and injects them into functions or constructors.
* - Supports both synchronous and asynchronous dependency resolution.
* - Handles nested property resolution and fallback mechanisms for unresolved keys.
* - Provides utilities for merging arguments, collecting parameter maps, and applying them.
* - Allows injection of new instances of constructors or asynchronous functions.
*
* Usage:
* Extend this class to implement custom dependency injection logic by overriding
* the abstract methods `resolve`, `onResolve`, and `inspect`.
*
* Example:
* ```typescript
* class MyInjector extends Injector {
* resolve<T>(param: Resolvable): T {
* // Custom resolution logic
* }
* onResolve<T>(name: Resolvable): PromiseLike<T> {
* // Custom asynchronous resolution logic
* }
* inspect(): void {
* // Custom inspection logic
* }
* }
* ```
*
* @template T - The type of the resolved value.
*/
export declare abstract class Injector implements ICustomResolver {
static readonly customResolve: symbol;
[customResolve]<T>(param: Resolvable): T;
/**
* Applies a collected map to resolved values.
* @param {InjectMap<T>} param - The parameter map.
* @param {{ [k: string | symbol]: any }} resolved - The resolved values.
* @returns {T} The applied map.
*/
static applyCollectedMap<T>(param: InjectMap<T> | ArrayLike<InjectMap>, resolved: {
[k: string | symbol]: any;
}): T | Promise<T>;
/**
* Collects a map of parameters.
* @param {InjectMap} param - The parameter map.
* @returns {(string | symbol)[]} The collected map.
*/
static collectMap(param: InjectMap): Resolvable<object>[];
/**
* Merges arrays of resolved arguments and other arguments.
* @param {InjectedParameter<unknown>[]} resolvedArgs - The resolved arguments.
* @param {...unknown[]} otherArgs - The other arguments.
* @returns {unknown[]} The merged array.
*/
static mergeArrays(resolvedArgs: InjectedParameter<unknown>[], ...otherArgs: unknown[]): {
promisedArgs: Promise<any[]>;
args: any[];
} | {
args: any[];
promisedArgs?: undefined;
};
/**
* Gets the arguments to inject.
* @param {(Resolvable)[]} toInject - The resolvable parameters to inject.
* @returns {InjectedParameter<unknown>[]} The injected parameters.
*/
protected getArguments(toInject: (Resolvable)[]): InjectedParameter<unknown>[];
/**
* Resolves a series of keys on a given source object, traversing through nested properties.
* If the resolution encounters an `Injector` instance, it delegates the resolution to the `Injector`.
* If the resolution encounters a promise-like object, it resolves the promise and continues the resolution.
* If a key cannot be resolved and a fallback function is provided, the fallback is invoked with the remaining keys.
*
* @template T - The type of the resolved value.
* @param source - The initial object to resolve the keys from.
* @param keys - An array of keys (strings or symbols) to resolve on the source object.
* @param fallback - A function to handle unresolved keys, invoked with the remaining keys if resolution fails.
* @returns The resolved value of type `T`, or the result of the fallback function if provided.
*/
static resolveKeys<T>(source: unknown, keys: ResolvableArray<object>, fallback?: (keys: Resolvable[]) => T): T;
/**
* Resolves a parameter asynchronously and returns a promise.
* @param name - The name of the parameter to resolve.
* @returns A promise resolving to the resolved value.
*/
abstract onResolve<T = unknown>(name: Resolvable): PromiseLike<T>;
/**
* Resolves a parameter asynchronously and invokes a handler with the result.
* @param name - The name of the parameter to resolve.
* @param handler - Callback to execute with the resolved value.
*/
abstract onResolve<T = unknown>(name: Resolvable, handler: (value: T) => void): void;
/**
* Injects a function or property.
* @param {Injectable<T, TArgs> | Resolvable} a - The function or resolvable parameter.
* @param {...Resolvable[]} b - Additional resolvable parameters.
* @returns {Injected<T> | ((b: TypedPropertyDescriptor<Injectable<T, TArgs>>) => void)} The injected function or property.
*/
inject<T, TArgs extends unknown[]>(a: Injectable<T, TArgs>): Injected<T>;
inject<T, TArgs extends unknown[]>(...a: (Resolvable)[]): (b: TypedPropertyDescriptor<Injectable<T, TArgs>>) => void;
inject<T, TArgs extends unknown[]>(a: Injectable<T, TArgs> | Resolvable, ...b: (Resolvable)[]): Injected<T> | ((b: TypedPropertyDescriptor<Injectable<T, TArgs>>) => void);
/**
* Injects an asynchronous function or property.
* @param {Injectable<T, TArgs> | Resolvable} a - The function or resolvable parameter.
* @param {...Resolvable[]} b - Additional resolvable parameters.
* @returns {Injected<T> | ((b: TypedPropertyDescriptor<InjectableAsync<U, TArgs>>) => void)} The injected function or property.
*/
injectAsync<T, TArgs extends unknown[]>(a: Injectable<T, TArgs>): Injected<T>;
injectAsync<T, TArgs extends unknown[]>(...a: (Resolvable)[]): Injectable<T, TArgs>;
/**
* Injects a new instance of a constructor.
* @param {InjectableConstructor<T, TArgs>} ctor - The constructor to inject.
* @returns {Injected<T>} The injected instance.
*/
injectNew<T, TArgs extends unknown[]>(ctor: InjectableConstructor<T, TArgs>): Injected<T>;
/**
* Resolves a parameter.
* @param {Resolvable} param - The parameter to resolve.
* @returns {T} The resolved parameter.
*/
abstract resolve<T>(param: Resolvable): T;
/**
* Inspects the injector.
*/
abstract inspect(): void;
/**
* Injects a new instance of a constructor with specified parameters.
* @param {Resolvable[]} toInject - The parameters to inject.
* @param {InjectableConstructor<T, TArgs>} ctor - The constructor to inject.
* @returns {Injected<T>} The injected instance.
*/
injectNewWithName<T, TArgs extends unknown[]>(toInject: Resolvable[], ctor: InjectableConstructor<T, TArgs>): Injected<T>;
/**
* Injects an asynchronous function with specified parameters.
* @param {Resolvable[]} toInject - The parameters to inject.
* @param {InjectableAsync<T, TArgs> | Injectable<T, TArgs>} a - The function to inject.
* @returns {Promise<T>} The injected function.
*/
injectWithNameAsync<T, TArgs extends unknown[]>(toInject: (Resolvable)[], a: Injectable<T | PromiseLike<T>, TArgs>): Injected<T | PromiseLike<T>>;
/**
* Injects a function with specified parameters.
* @param {Resolvable[]} toInject - The parameters to inject.
* @param {Injectable<T, TArgs>} a - The function to inject.
* @returns {Injected<T>} The injected function.
*/
injectWithName<T, TArgs extends unknown[]>(toInject: Resolvable[], a: Injectable<T, TArgs>): Injected<T>;
/**
* Executes a function with specified parameters.
* @param {...Resolvable[]} toInject - The parameters to inject.
* @returns {(f: Injectable<T, TArgs>) => T} The executed function.
*/
exec<T, TArgs extends unknown[]>(...toInject: (Resolvable)[]): (f: Injectable<T, TArgs>) => T;
}
export declare abstract class LocalInjector extends Injector {
protected parent?: Injector | null;
constructor(parent?: Injector | null);
/**
* Unregisters a parameter.
* @param {string | symbol} name - The name of the parameter to unregister.
*/
abstract unregister(name: string | symbol): void;
/**
* Registers a parameter with a value.
* @param {string | symbol} name - The name of the parameter to register.
* @param {T} value - The value to register.
* @param {boolean} [override] - Whether to override the existing value.
* @returns {T} The registered value.
*/
register<T>(name: string | symbol, value: T, override?: boolean): T;
/**
* Registers a factory function.
* @param {string} name - The name of the factory.
* @param {(() => unknown)} value - The factory function.
* @param {boolean} [override] - Whether to override the existing value.
* @returns {(() => unknown)} The registered factory function.
*/
registerFactory(name: string | symbol, value: (() => unknown), override?: boolean): (() => unknown);
/**
* Creates a factory function.
* @param {string} name - The name of the factory.
* @param {boolean} [override] - Whether to override the existing value.
* @returns {(fact: (() => unknown)) => void} The factory function.
*/
factory(name: string, override?: boolean): (fact: (() => unknown)) => void;
/**
* Registers a service.
* @param {string | symbol} name - The name of the service.
* @param {...Resolvable[]} toInject - The parameters to inject.
*/
service(name: string | symbol, ...toInject: Resolvable[]): any;
service(name: string | symbol, override?: boolean, ...toInject: Resolvable[]): any;
/**
* Registers a descriptor.
* @param {string | symbol} name - The name of the descriptor.
* @param {PropertyDescriptor} value - The descriptor value.
* @param {boolean} [override] - Whether to override the existing value.
*/
abstract registerDescriptor(name: string | symbol, value: PropertyDescriptor, override?: boolean): void;
}
export declare class InjectorMap extends Injector {
private map;
constructor(map: ((Resolvable: any) => any));
onResolve<T = unknown>(name: Resolvable): PromiseLike<T>;
onResolve<T = unknown>(name: Resolvable, handler: (value: T) => void): void;
resolve<T>(param: Resolvable): T;
inspect(): void;
}
export {};