-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (44 loc) · 1.27 KB
/
index.js
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
const placeHolderMessage = "value will be encoded as json string __JSONInception__";
module.exports.templateTags = [
{
name: 'inception_json',
displayName: 'JSONString',
description: 'encode json as json string',
args: [],
run(context) {
return placeHolderMessage;
},
},
];
function processNode(node) {
if (node == null || typeof node != 'object') {
return;
}
for (var prop in node) {
processNode(node[prop]);
if (prop.indexOf(placeHolderMessage) >= 0) {
node[prop.replace(placeHolderMessage, '')] = JSON.stringify(node[prop]);
delete node[prop];
}
}
}
function processJson(jsonStr) {
var json = JSON.parse(jsonStr);
processNode(json);
return JSON.stringify(json);
}
function processBody(body) {
var placeHolderIndex = body.indexOf(placeHolderMessage);
if (placeHolderIndex == 0) {
return JSON.stringify(processJson(body.replace(placeHolderMessage, '')));
} else if (placeHolderIndex > 0) {
return processJson(body);
} else {
return body;
}
}
module.exports.requestHooks = [
context => {
context.request.setBodyText(processBody(context.request.getBodyText()));
}
];