async-injection
Version:
A robust lightweight dependency injection library for TypeScript.
70 lines (69 loc) • 5.2 kB
TypeScript
import { InjectableId } from './injector';
/**
* Placed just before the class declaration, this class decorator applies metadata to the class constructor indicating that the user intends to bind the class into the container.
* This decorator will throw if not placed on a class declaration, or if placed more than once on a class declaration.
*/
export declare function Injectable(): ClassDecorator;
/**
* Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of Object that will be injected into the parameter.
* In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).
* This decorator may also be placed on a parameter of a method annotated with @PostConstruct, in which case the container will resolve and inject the value before invoking that method.
*
* @param id The identifier of the bound type that should be injected.
*/
export declare function Inject(id: InjectableId<any>): ParameterDecorator;
/**
* This is a helper function used by the container to retrieve the @Inject metadata for a specifically indexed constructor parameter
*
* @param target The constructor function of the class (we don't allow @Inject on anything else).
* @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list
* @see Inject
*/
export declare function _getInjectedIdAt(target: Function, parameterIndex: number): InjectableId<any>;
/**
* Retrieve the @Inject metadata for a specifically indexed parameter of a named method.
*/
export declare function _getInjectedIdForMethod(prototype: object, methodName: string | symbol, parameterIndex: number): InjectableId<any>;
/**
* Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for *any* reason it is unable to otherwise resolve the type of the parameter.
* This decorator may also be placed on a parameter of a method annotated with @PostConstruct.
* WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.
*/
export declare function Optional(alt?: any): ParameterDecorator;
/**
* This is a helper function used by the container to retrieve the @Optional metadata for a specifically indexed constructor parameter
*
* @param target The constructor function of the class (we don't allow @Optional on anything else).
* @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list
* @see Optional
* @returns an object containing the value provided in the decorator, or undefined if no annotation was present.
*/
export declare function _getOptionalDefaultAt(target: Function, parameterIndex: number): {
value: any;
};
/**
* Retrieve the @Optional metadata for a specifically indexed parameter of a named method.
*/
export declare function _getOptionalDefaultForMethod(prototype: object, methodName: string | symbol, parameterIndex: number): {
value: any;
};
/**
* Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service.
* The method will be assumed to be synchronous unless the method signature explicitly declares its return type to be ": Promise<something>".
* Parameters will be resolved by the container just as they are for constructors.
* This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
*/
export declare function PostConstruct(): MethodDecorator;
/**
* Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.
* If invoked by the container, the container will drop any references it has to the object when the method returns.
* Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).
* This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.
* You should *not* use this decorator as a general "object finalization" method. It has very limited scope and purpose.
* The decorated method must complete normally (no throwing), as "release" is not an abort-able process.
* This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
* The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.
* Also @see Container.releaseSingletons for the intended usage of this decorator.
* It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).
*/
export declare function Release(): MethodDecorator;