-
Notifications
You must be signed in to change notification settings - Fork 6
/
ServiceInvoker.ts
61 lines (51 loc) · 2.24 KB
/
ServiceInvoker.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
import {IEvent, ICallback, IResponse} from "./Messages"
import {AbstractService} from "./AbstractService"
import {Flask} from "./Flask"
declare function require(filename:string):any;
export class ServiceInvoker {
private static moduleCache = {};
public static invokeByFilename(moduleName:string, filename:string, event:IEvent, context:any, callback:ICallback){
Flask.initialize();
let handlerModule;
if (ServiceInvoker.moduleCache[filename])
handlerModule = ServiceInvoker.moduleCache[filename];
else {
handlerModule = require(filename);
ServiceInvoker.moduleCache[filename] = handlerModule;
}
let handlerObject = new handlerModule[moduleName](event,context,callback);
handlerObject.inject();
handlerObject.handle();
}
public static invoke<T extends AbstractService>(type: { new(event:IEvent, context:any, callback:ICallback): T ;}, event:IEvent, context:any, callback:ICallback){
Flask.initialize();
let handlerObject = new type(event,context,callback);
handlerObject.inject();
handlerObject.handle();
}
public static test <T extends AbstractService>(type: { new(event:IEvent, context:any, callback:ICallback): T ;}){
return {
withParameters: function(event:IEvent): Promise<IResponse>{
let promiseObj = new Promise<IResponse>(function(resolve,reject){
let mockContext = {succeed:(message)=>{
if (typeof message.body == "string")
message = JSON.parse(message.body);
resolve(message);
},fail:(error)=>{
reject(error);
}};
let handlerObject = new type(event,mockContext, function(error, message){
if (error)
resolve(error);
else{
resolve(message);
}
});
handlerObject.inject();
handlerObject.handle();
});
return promiseObj;
}
}
}
}