@foal/core
Version:
Full-featured Node.js framework, with no complexity
76 lines (75 loc) • 2.83 kB
TypeScript
import 'reflect-metadata';
import { Class, ClassOrAbstractClass } from './class.interface';
export interface IDependency {
propertyKey: string;
serviceClassOrID: string | Class;
}
/**
* Decorator injecting a service inside a controller or another service.
*
* @param id {string} - The service ID.
*/
export declare function Dependency(id: string): (target: any, propertyKey: string) => void;
/**
* Decorator injecting a service inside a controller or another service.
*
* @export
*/
export declare function dependency(target: any, propertyKey: string): void;
/**
* Create a new service with its dependencies.
*
* @export
* @template Service
* @param {ClassOrAbstractClass<Service>} serviceClass - The service class.
* @param {object} [dependencies] - An object which key/values are the service properties/instances.
* @returns {Service} - The created service.
*/
export declare function createService<Service extends object>(serviceClass: ClassOrAbstractClass<Service>, dependencies?: object): Service;
export declare function createControllerOrService<T extends object>(serviceClass: ClassOrAbstractClass<T>, dependencies?: object): T;
/**
* Identity Mapper that instantiates and returns service singletons.
*
* @export
* @class ServiceManager
*/
export declare class ServiceManager {
private readonly map;
/**
* Boot all services : call the method "boot" of each service if it exists.
*
* If a service identifier is provided, only this service will be booted.
*
* Services are only booted once.
*
* @param {(string|ClassOrAbstractClass)} [identifier] - The service ID or the service class.
* @returns {Promise<void>}
* @memberof ServiceManager
*/
boot(identifier?: string | ClassOrAbstractClass): Promise<void>;
/**
* Add manually a service to the identity mapper.
*
* @param {string|ClassOrAbstractClass} identifier - The service ID or the service class.
* @param {*} service - The service object (or mock).
* @param {{ boot: boolean }} [options={ boot: false }] If `boot` is true, the service method "boot"
* will be executed when calling `ServiceManager.boot` is called.
* @returns {this} The service manager.
* @memberof ServiceManager
*/
set(identifier: string | ClassOrAbstractClass, service: any, options?: {
boot: boolean;
}): this;
/**
* Get (and create if necessary) the service singleton.
*
* @param {string|ClassOrAbstractClass} identifier - The service ID or the service class.
* @returns {*} - The service instance.
* @memberof ServiceManager
*/
get<T>(identifier: ClassOrAbstractClass<T>): T;
get(identifier: string): any;
private bootService;
private getConcreteClassFromConfig;
private getProperty;
}