-
Notifications
You must be signed in to change notification settings - Fork 6
/
Messages.ts
104 lines (85 loc) · 2.52 KB
/
Messages.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
import {Steroid} from "./Steroids"
declare let console:any;
export interface IEvent
{
pathParameters:any;
httpMethod:string;
headers:any;
body:any;
files?:any;
queryStringParameters:any;
}
export interface IResponse {
success: boolean;
code: number;
error: any;
history: Array<any>;
response: any;
}
export interface ICallback {
(error: any, result: IResponse): void;
}
export class Composer{
private static _compose(steroid:Steroid, result, isError){
let response = {
success:true,
code:undefined,
error:undefined,
response: undefined,
history: undefined,
message:undefined
};
let params:any = steroid.response().getParams();
if (steroid !== undefined){
if (params.success !== undefined)
response.success = params.success;
if (params.code !== undefined)
response.code = params.code;
if (params.message !== undefined)
response.message = params.message;
}
if (isError == true){
if (result !== undefined){
if (result.exception !== undefined){
response.error = {
message: result.exception.message,
stack: result.exception.stack
}
if (result.requestTrace !== undefined)
response.error.requestTrace = result.requestTrace;
}
}
}else {
if (params.customError == undefined){
if (response.success == false){
response.error = {message:result};
}else
response.response = result;
}
else
response.error = params.customError;
}
return Composer.convertToLambdaProxyIntegration(steroid, response);
}
public static compose (steroid:Steroid, result){
return Composer._compose(steroid,result,false);
}
public static composeError(steroid:Steroid, result){
let composeResult = Composer._compose(steroid,result,true);
console.log("Error occured while processing the request : ", composeResult.code, composeResult.error);
return composeResult;
}
public static convertToLambdaProxyIntegration(steroid:Steroid, response:any){
let contextObj = steroid.internals().getGeneratedContext();
let extraSettings = steroid.internals().getExtraSettings();
if (typeof response === "string")
contextObj.body = response;
else {
if (extraSettings.canReturnWithoutStringify==false)
contextObj.body = JSON.stringify(response);
else
contextObj.body = response;
}
return contextObj;
}
}