UNPKG

@akala/core

Version:
76 lines (75 loc) 3.35 kB
import "reflect-metadata"; import { SimpleInjector } from './simple-injector.js'; import type { InjectedParameter } from "./shared.js"; export type PropertyInjection = ((i: SimpleInjector) => void); export type ParameterInjection = ((i: SimpleInjector) => InjectedParameter<unknown>); export declare const injectSymbol: unique symbol; export declare const afterInjectSymbol: unique symbol; export interface InjectableObject { [injectSymbol]: ((i: SimpleInjector) => void)[]; } /** * Decorator to mark class properties or constructor parameters for dependency injection. * * @param name - Optional dependency name. If omitted, uses the property name as the dependency key. * @returns Decorator function to apply injection metadata. */ export declare function inject(names: string[]): <T>(target: T, propertyKey?: keyof T) => void; export declare function inject(name?: string): (target: object | (new (...args: unknown[]) => unknown), propertyKey: string, parameterIndex?: number) => void; /** * Processes dependency injection metadata for an object and its prototype chain. * * @param {SimpleInjector} injector - The injector to resolve dependencies. * @param {object} obj - Target object to apply injections to. * @param {object} [prototype] - Optional prototype to traverse (used internally). */ export declare function applyInjector(injector: SimpleInjector, obj: object, prototype?: object): void; /** * Decorator to make a class injectable with dependency resolution. * * @template TInstance - Type of the class instance. * @template TClass - Type of the class constructor. * @param {TClass} ctor - Class constructor to wrap. * @param {SimpleInjector} injector - Optional injector instance (default uses constructor argument). * @returns {TClass} Injectable class with dependency injection setup. */ export declare function injectable<TInstance, TClass extends { new (...args: unknown[]): TInstance; }>(ctor: TClass, injector?: SimpleInjector): TClass; export type InjectableClass<T> = T & { new (injector: SimpleInjector): T; }; /** * Creates a class decorator to apply an injector to a class. * * @param {SimpleInjector} injector - Injector instance to bind to the class. * @returns {Function} Class decorator that configures the class for dependency injection. */ export declare function useInjector(injector: SimpleInjector): <TClass extends { new (...args: unknown[]): object; }>(ctor: TClass) => TClass; /** * Extends a class with an injector for dependency resolution. * * @template TClass - Type of the class to extend. * @param {SimpleInjector} injector - Injector instance to apply. * @param {TClass} constructor - Class to extend. * @returns {TClass} Extended class with injector configuration. */ export declare function extendInject<TClass extends { new (...args: unknown[]): object; }>(injector: SimpleInjector, constructor: TClass): TClass; /** * Reflection-based injector that resolves dependencies using metadata. * * @extends SimpleInjector */ export declare class ReflectionInjector extends SimpleInjector { protected parent?: SimpleInjector; /** * Creates a new ReflectionInjector instance. * * @param {SimpleInjector} [parent] - Optional parent injector to delegate unresolved dependencies to. */ constructor(parent?: SimpleInjector); }