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
export default async function handler(request: Request) {
if (request.method !== "POST") {
return Response.json({ message: "This val responds to POST requests." }, { status: 400 });
}
try {
const body = await request.text(); // await request.json();
console.log("request.text", body);
let result = generateSnippet(body);
return new Response(result);
} catch (e) {
console.log(e);
return Response.json({ message: "The body of this request was not JSON-encoded." }, {
status: 400,
});
}
}
function writeLog(text = "Hello World") {
console.log(text);
}
function generateSnippet(inputText) {
console.clear();
// Frontend protection
if (typeof inputText === "object") throw "Input is an object so no go!";
// Check if we have valid json
try {
inputText = JSON.parse(inputText);
} catch (error) {
let msg = "ERROR: generateSnippet - JSON Parse error from input!";
console.error(msg);
return msg;
throw msg;
}
// console.log({inputText})
let sections = Object.keys(inputText);
let urlCode = "";
let headerCode = "";
let authCode = "";
let methodCode = "";
let bodyCode = "";
let triggerCode = "";
sections.forEach((section) => {
if (section.toLowerCase() === "url") { urlCode = genUrlCode(inputText[section]); }
if (section.toLowerCase() === "header") { headerCode = genHeaderCode(inputText[section]); }
if (section.toLowerCase() === "auth") { authCode = genAuthCode(inputText[section]); }
if (section.toLowerCase() === "method") { methodCode = genMethodCode(inputText[section]); }
if (section.toLowerCase() === "body") { bodyCode = genBodyCode(inputText[section]); }
});
triggerCode = genTriggerCode();
// incase there are no headers
if (!headerCode) {
headerCode = genHeaderCode({});
}
// check for errors
let sectionVariables = [urlCode, headerCode, authCode, methodCode, bodyCode];
for (let codeResult in sectionVariables) {
// console.log(sectionVariables[codeResult])
if (sectionVariables[codeResult].startsWith("ERROR")) {
return sectionVariables[codeResult];
}
}
let finalCode = "//\n// Url code\n//\n";
finalCode += urlCode;
finalCode += "\n\n//\n// Header code\n//\n";
finalCode += headerCode;
if (authCode) {
finalCode += "\n\n//\n// Auth code\n//\n";
finalCode += authCode;
}
finalCode += "\n\n//\n// Method code\n//\n";
finalCode += methodCode;
finalCode += "\n\n//\n// Body code\n//\n";
finalCode += bodyCode;
finalCode += "\n\n//\n// Trigger code\n//\n";
finalCode += triggerCode;
// console.log(finalCode)
// let outputText = JSON.stringify(inputText,null,2)
return finalCode;
}
function genUrlCode(jsonObj) {
// console.log(jsonObj)
let url = getUrl(jsonObj);
let queryParamsArray = getQueryString(jsonObj);
// TODO: Handle variable code
let urlCode = "ASSIGN RestProxy_Url = \"" + url + "\"";
// console.log({queryParamsArray})
// Check if there is query params
if (queryParamsArray) {
urlCode += "\n\n// Url Query Params";
urlCode += "\nASSIGN RestProxy_UrlQueryParams = \"?\"";