// Type definitions for merapi // Project: merapi // Definitions by: Ahmad Rizqi Meydiarso declare module "merapi" { export default function merapi (options : IContainerOptions) : Container; export interface IContainerEventHandlers { onBeforePluginInit? : (c : Container) => Promise | void; onAfterPluginInit? : (c : Container) => Promise | void; onBeforeConfigResolve? : (c : Container) => Promise | void; onAfterConfigResolve? : (c : Container) => Promise | void; onBeforeComponentRegister? : (name : string, com: any, isObj: boolean) => Promise | void; onAfterComponentRegister? : (name : string, desc : {deps:string[], object: any, loader: IClosure | IClass, factory: IClosure | IClass}) => Promise | void; onBeforeComponentResolve? : (name : string, deps : IHash, prev : string[]) => Promise | void; onAfterComponentResolve? : (name : string, component : any) => Promise | void; onComponentInstantiate? : (name : string, component : any) => Promise | void; onUncaughtException? : (e : Error) => Promise | void; } export interface IContainerOptions extends IContainerEventHandlers { basepath : string; config : Json; extConfig? : Json; envConfig? : IHash; delimiters? : {left:string, right:string}; } export interface IClass { prototype : T; new() : T; } export interface IClass { new (...args : any[]) : T; } export interface IEventHandler { (...args : any[]) : void; } export interface IAsyncEventHandler { (...args : any[]) : Promise; } export interface IComponent { initialize() : Promise; } export interface IClosure { (...deps : any []) : T; } export type Json = null|string|number|boolean|JsonObject|JsonArray; export interface JsonObject { [x: string]: Json; } export interface JsonArray extends Array { } export interface IComponentResolver { (type : string, options : Json) : T; } export interface IComponentDescriptor { deps?: string[], object?: Component, factory?: IClosure | IClass, loader?: IClosure | IClass } export interface IHash { [i : string] : T; } export interface IPluginDescriptor { [i : string] : Function; } /** * Simple Injectable Component */ export class Component implements IComponent { /** * Called when component is initialized. * Will convert generator functions to promises */ initialize() : Promise; /** * Create a component */ constructor (); static mixin(klass : {new(): T}) : IClass; } export interface IAsyncEmitter { on(event : T, fn : IEventHandler | IAsyncEventHandler, once? : boolean) : number; once(event : T, fn : IEventHandler | IAsyncEventHandler) : number; removeListener(event : T, id : number) : void; emit(event : T, ...args : any[]) : Promise; } /** * Component with asynchronous event emitter */ export class AsyncEmitter implements IAsyncEmitter { /** * Attach an event handler */ on(event : T, fn : IEventHandler | IAsyncEventHandler, once? : boolean) : number; /** * Attach an event handler for one trigger * only */ once(event : T, fn : IEventHandler | IAsyncEventHandler) : number; /** * Remove listener by specific id */ removeListener(event : T, id : number) : void; /** * Emit an event with provided arguments * asynchronously */ emit(event : T, ...args : any[]) : Promise; /** * Create an AsyncEmitter */ constructor(); } export interface IContainer { alias(aliasName : string, originName : string) : void; register(name : string, type : string | IClosure | T, options : boolean | Json) : void; registerComponentType(type : string, resolver : IComponentResolver) : void; resolve(name : string, deps : IHash, prev : string[]) : Promise | null; start() : Promise; stop() : Promise; get(name : string) : Promise; initPlugins(desc : string[] | IHash) : void; initPlugin(name : string, options : Json) : void; registerPlugin(name : string, plugin : IPluginDescriptor) : void; initialize() : Promise; } export class Container extends AsyncEmitter implements IContainer { /** * Link component to other name */ alias(aliasName : string, originName : string) : void; /** * Register a component */ register(name : string, type : string | IClosure | T, options? : boolean | Json) : void; /** * Register a component type */ registerComponentType(type : string, resolver : IComponentResolver) : void; /** * Resolve a component */ resolve(name : string, deps : IHash, prev : string[]) : Promise | null; /** * Start container */ start() : Promise; /** * Stop container */ stop() : Promise; /** * Get component synchronously */ get(name : string) : T; /** * Initialize plugins from node_modules */ initPlugins(desc : string[] | IHash) : void; /** * Initialize plugin from node_modules */ initPlugin(name : string, options : Json) : void; /** * Register plugin manually */ registerPlugin(name : string, plugin : IPluginDescriptor) : void; initialize() : Promise; /** * */ constructor (options : IContainerOptions); } export interface IConfigReader { (path : string, ignore? : boolean) : T; get() : IHash; get(path : string, ignore? : boolean) : T; has(path : string) : boolean; default(path : string, value : T) : T; flatten(data? : Json) : IHash; path(s : string) : IConfigReader; resolve(path : string) : T; } export interface IConfig extends IConfigReader { (path : string | IHash, value? : any, ignore? : boolean) : void; set(path : string | IHash, value? : any, ignore? : boolean) : void; resolve(path? : string) : T; path(s : string) : IConfig; extend(data : Json) : IConfig; create(data : Json, opts? : {left?:string, right?:string, lazy?: boolean, recursive?: boolean}) : IConfig; } export class Config { get() : IHash; get(path : string, ignore? : boolean) : T; set(path : string | IHash, value? : any, ignore? : boolean) : void; has(path : string) : boolean; default(path : string, value : T) : T; flatten(data? : Json) : IHash; resolve(path? : string) : T; path(s : string) : IConfig; extend(data : Json) : IConfig; create(data : Json, opts? : {left?:string, right?:string, lazy?: boolean, recursive?: boolean}) : IConfig; static create(data : Json, opts? : {left?:string, right?:string, lazy?: boolean, recursive?: boolean}) : IConfig; constructor(data : Json, opts? : {left?:string, right?:string, lazy?: boolean, recursive?: boolean}); } export interface ILogger extends Console { } export interface IInjector { getComponentNames() : string[]; getComponentDescriptor(name : string) : IComponentDescriptor; alias(aliasName : string, originalName : string) : void; register(name : string, type? : string | IClosure | T | IComponentDescriptor, options? : boolean | Json) : void; resolve(name : string, deps? : IHash, prev? : string[]) : Promise; resolveMethod(str : string) : Function | null; execute(fn : Function) : any; dependencies(names : string[], deps? : IHash, prev? : string[]) : Component[]; create(options : IHash) : Injector; } export class Injector extends Component implements IInjector { getComponentNames() : string[]; getComponentDescriptor(name : string) : IComponentDescriptor; alias(aliasName : string, originalName : string) : void; register(name : string, type? : string | IClosure | T | IComponentDescriptor, options? : boolean | Json) : void; resolve(name : string, deps? : IHash, prev? : string[]) : Promise; resolveMethod(str : string) : Function | null; execute(fn : Function) : any; dependencies(names : string[], deps? : IHash, prev? : string[]) : Component[]; create(options : IHash) : Injector; constructor(options : IHash); } export module utils { function instantiate(Class : {new(): T}, args : any[]) : T; function isPromise(obj : any) : obj is Promise; function isIterator(obj : any) : obj is Iterator; function isGeneratorFunction(obj : any) : obj is GeneratorFunction; function isArray(obj : any) : obj is Array; function getAllPropertyNames(obj : Object) : string[]; function dependencyNames(fn : Function) : string[]; function compile(str: string) : (dict : IHash) => string; function extend(target: X, origin : Y) : X & Y; function extend(target: X, a : Y, b : Z) : X & Y & Z; } export function async(fn : Function) : (...args: any[]) => Promise; }