-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUtils.ts
96 lines (88 loc) · 2.55 KB
/
Utils.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
'use strict';
import * as winston from "winston";
import * as shortid from "shortid";
// Configure logging
winston.configure({
level: process.env.LOG_LEVEL || "debug",
transports: [ new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
)
})
]
});
export default class Utils
{
/**
* logInfo: Helper to log Info messages
*
*/
public static logInfo(msg: any)
{
winston.info(msg);
}
/**
* logError: Helper to log Error messages
*
*/
public static logError(msg: any)
{
winston.error(msg);
}
/**
* prettyPrintJson: helper to pretty print a flat JSON string
*
*/
public static prettyPrintJson(jsonString: string)
{
return JSON.stringify(JSON.parse(jsonString), null, 2);
}
/**
* initSampleDataAndRenderView: Called to render /apidemo and /appdemo views
*
* Helper to init sample JSON data for this session and pass the session to the view
* This lets the view access session variables (e.g. JWT JSON and sample data) for display purposes.
*
*/
public static initSampleDataAndRenderView(req: any, res: any, viewName: string)
{
Utils.initSampleData()
.then((sampleData: string) => {
req.session.sampleJsonData = sampleData;
res.render(viewName, { session: req.session });
});
}
/**
* initSampleData: Called on session start to generate sample JSON data to insert into Data Extension
*
*/
private static initSampleData() : Promise<string>
{
Utils.logInfo("initSampleData called.");
return new Promise<string>((resolve, reject) =>
{
let sampleData = [
{
keys: {
id: shortid.generate()
},
values: {
name: 'Sanjay - ' + shortid.generate(),
email: 'sanjay-' + shortid.generate() + '@sanjay.com',
}
},
{
keys: {
id: shortid.generate()
},
values: {
name: 'Savita - ' + shortid.generate(),
email: 'savita-' + shortid.generate() + '@savita.com'
}
}
];
resolve(Utils.prettyPrintJson(JSON.stringify(sampleData)));
});
}
}