-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.d.ts
275 lines (242 loc) · 9.94 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// 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> | void;
onAfterPluginInit? : (c : Container) => Promise<void> | void;
onBeforeConfigResolve? : (c : Container) => Promise<void> | void;
onAfterConfigResolve? : (c : Container) => Promise<void> | void;
onBeforeComponentRegister? : (name : string, com: any, isObj: boolean) => Promise<void> | void;
onAfterComponentRegister? : (name : string, desc : {deps:string[], object: any, loader: IClosure<any> | IClass<any>, factory: IClosure<any> | IClass<any>}) => Promise<void> | void;
onBeforeComponentResolve? : (name : string, deps : IHash<any>, prev : string[]) => Promise<void> | void;
onAfterComponentResolve? : (name : string, component : any) => Promise<void> | void;
onComponentInstantiate? : (name : string, component : any) => Promise<void> | void;
onUncaughtException? : (e : Error) => Promise<void> | void;
}
export interface IContainerOptions extends IContainerEventHandlers {
basepath : string;
config : Json;
extConfig? : Json;
envConfig? : IHash<Json>;
delimiters? : {left:string, right:string};
}
export interface IClass<T> {
prototype : T;
new() : T;
}
export interface IClass<T> {
new (...args : any[]) : T;
}
export interface IEventHandler {
(...args : any[]) : void;
}
export interface IAsyncEventHandler {
(...args : any[]) : Promise<void>;
}
export interface IComponent {
initialize() : Promise<void>;
}
export interface IClosure<T> {
(...deps : any []) : T;
}
export type Json = null|string|number|boolean|JsonObject|JsonArray;
export interface JsonObject {
[x: string]: Json;
}
export interface JsonArray extends Array<Json> { }
export interface IComponentResolver<T> {
(type : string, options : Json) : T;
}
export interface IComponentDescriptor<T> {
deps?: string[],
object?: Component,
factory?: IClosure<T> | IClass<T>,
loader?: IClosure<T> | IClass<T>
}
export interface IHash<T> {
[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<void>;
/**
* Create a component
*/
constructor ();
static mixin<T>(klass : {new(): T}) : IClass<Component & T>;
}
export interface IAsyncEmitter<T> {
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<void>;
}
/**
* Component with asynchronous event emitter
*/
export class AsyncEmitter<T> implements IAsyncEmitter<T> {
/**
* 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<void>;
/**
* Create an AsyncEmitter
*/
constructor();
}
export interface IContainer {
alias(aliasName : string, originName : string) : void;
register<T>(name : string, type : string | IClosure<T> | T, options : boolean | Json) : void;
registerComponentType<T>(type : string, resolver : IComponentResolver<T>) : void;
resolve<T>(name : string, deps : IHash<any>, prev : string[]) : Promise<T> | null;
start() : Promise<void>;
stop() : Promise<void>;
get<T>(name : string) : Promise<T>;
initPlugins(desc : string[] | IHash<Json>) : void;
initPlugin(name : string, options : Json) : void;
registerPlugin(name : string, plugin : IPluginDescriptor) : void;
initialize() : Promise<void>;
}
export class Container extends AsyncEmitter<string> implements IContainer {
/**
* Link component to other name
*/
alias(aliasName : string, originName : string) : void;
/**
* Register a component
*/
register<T>(name : string, type : string | IClosure<T> | T, options? : boolean | Json) : void;
/**
* Register a component type
*/
registerComponentType<T>(type : string, resolver : IComponentResolver<T>) : void;
/**
* Resolve a component
*/
resolve<T>(name : string, deps : IHash<any>, prev : string[]) : Promise<T> | null;
/**
* Start container
*/
start() : Promise<void>;
/**
* Stop container
*/
stop() : Promise<void>;
/**
* Get component synchronously
*/
get<T>(name : string) : T;
/**
* Initialize plugins from node_modules
*/
initPlugins(desc : string[] | IHash<Json>) : void;
/**
* Initialize plugin from node_modules
*/
initPlugin(name : string, options : Json) : void;
/**
* Register plugin manually
*/
registerPlugin(name : string, plugin : IPluginDescriptor) : void;
initialize() : Promise<void>;
/**
*
*/
constructor (options : IContainerOptions);
}
export interface IConfigReader {
<T>(path : string, ignore? : boolean) : T;
get() : IHash<Json>;
get<T>(path : string, ignore? : boolean) : T;
has(path : string) : boolean;
default<T>(path : string, value : T) : T;
flatten(data? : Json) : IHash<any>;
path(s : string) : IConfigReader;
resolve<T>(path : string) : T;
}
export interface IConfig extends IConfigReader {
(path : string | IHash<Json>, value? : any, ignore? : boolean) : void;
set(path : string | IHash<Json>, value? : any, ignore? : boolean) : void;
resolve<T>(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<Json>;
get<T>(path : string, ignore? : boolean) : T;
set(path : string | IHash<Json>, value? : any, ignore? : boolean) : void;
has(path : string) : boolean;
default<T>(path : string, value : T) : T;
flatten(data? : Json) : IHash<any>;
resolve<T>(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<T>(name : string) : IComponentDescriptor<T>;
alias(aliasName : string, originalName : string) : void;
register<T>(name : string, type? : string | IClosure<T> | T | IComponentDescriptor<T>, options? : boolean | Json) : void;
resolve<T>(name : string, deps? : IHash<any>, prev? : string[]) : Promise<T|null>;
resolveMethod(str : string) : Function | null;
execute(fn : Function) : any;
dependencies(names : string[], deps? : IHash<any>, prev? : string[]) : Component[];
create(options : IHash<any>) : Injector;
}
export class Injector extends Component implements IInjector {
getComponentNames() : string[];
getComponentDescriptor<T>(name : string) : IComponentDescriptor<T>;
alias(aliasName : string, originalName : string) : void;
register<T>(name : string, type? : string | IClosure<T> | T | IComponentDescriptor<T>, options? : boolean | Json) : void;
resolve<T>(name : string, deps? : IHash<any>, prev? : string[]) : Promise<T|null>;
resolveMethod(str : string) : Function | null;
execute(fn : Function) : any;
dependencies(names : string[], deps? : IHash<any>, prev? : string[]) : Component[];
create(options : IHash<any>) : Injector;
constructor(options : IHash<any>);
}
export module utils {
function instantiate<T>(Class : {new(): T}, args : any[]) : T;
function isPromise(obj : any) : obj is Promise<any>;
function isIterator(obj : any) : obj is Iterator<any>;
function isGeneratorFunction(obj : any) : obj is GeneratorFunction;
function isArray(obj : any) : obj is Array<any>;
function getAllPropertyNames(obj : Object) : string[];
function dependencyNames(fn : Function) : string[];
function compile(str: string) : (dict : IHash<any>) => string;
function extend<X, Y>(target: X, origin : Y) : X & Y;
function extend<X, Y, Z>(target: X, a : Y, b : Z) : X & Y & Z;
}
export function async<T>(fn : Function) : (...args: any[]) => Promise<T>;
}