-
Notifications
You must be signed in to change notification settings - Fork 2
/
iOSCodeGenerator.js
executable file
·145 lines (132 loc) · 6.21 KB
/
iOSCodeGenerator.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var XojoNewCodeGenerator = function() {
this.generate = function(context, requests, options) {
for(var i in requests) {
var request = requests[i];
var client_code = [];
if(request.name) {
client_code[client_code.length] = "// " + request.name;
}
if (request.description != "") {
client_code[client_code.length] = "// " + request.description;
}
client_code[client_code.length] = "";
var secure = (request.url.indexOf("https://")>-1);
if(!secure) {
client_code[client_code.length] = "// NOTE: Starting with Xojo 2018r4, use of insecure connections";
client_code[client_code.length] = "// in a macOS app requires the addition of the following plist key:";
client_code[client_code.length] = "// <key>NSAppTransportSecurity</key>";
client_code[client_code.length] = "// <dict><key>NSAllowsArbitraryLoads</key><true/></dict>";
client_code[client_code.length] = "";
}
var vars = request.variables;
if (vars.length > 0) {
client_code[client_code.length] = "// Variable Definitions"
for(i=0;i<vars.length;i++) {
var desc = "// " + vars[i].name + ": " + vars[i].description;
if (vars[i].required) {
desc += " (required)";
}
client_code[client_code.length] = desc;
client_code[client_code.length] = "// " + vars[i].type;
}
client_code[client_code.length] = "";
}
client_code[client_code.length] = "// Set up the socket";
client_code[client_code.length] = "// \"mySocket\" should be a property stored elsewhere so it will not go out of scope before the request completes";
client_code[client_code.length] = "// Property mySocket as Xojo.Net.HTTPSocket"
client_code[client_code.length] = "mySocket = new Xojo.Net.HTTPSocket";
var headers = request.headers;
for (var headerName in headers) {
var headerValue = headers[headerName];
if(!(request.body != "" && headerName == "Content-Type")) {
client_code[client_code.length] = "mySocket.RequestHeader(\"" + headerName + "\") = \"" + headerValue + "\"";
}
}
client_code[client_code.length] = "";
var body;
var mimeType;
// Figure out what kind of body the user specified
if(request.multipartBody) {
body = request.multipartBody;
if(Object.size(body) > 0) {
mimeType = "multipart/form-data";
client_code[client_code.length] = "// Multipart";
client_code[client_code.length] = "Dim textArr() as Text";
for(var propertyName in body) {
var key = propertyName;
var value = body[key];
client_code[client_code.length] = "textArr.append \"" + key + "=" + encodeURIComponent(value) + "\"";
}
client_code[client_code.length] = "Dim textData as Text = Text.Join(textArr,\"&\")";
client_code[client_code.length] = "";
client_code[client_code.length] = "// Convert Text to Memoryblock"
client_code[client_code.length] = "Dim data As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(textData)";
}
} else if(request.jsonBody) {
body = request.jsonBody;
mimeType = "application/json";
client_code[client_code.length] = "// JSON";
var jsontext = JSON.stringify(body).replace(/\"/g,"\\\"");
client_code[client_code.length] = "Dim json as Text = \"" + jsontext + "\"";
client_code[client_code.length] = "";
client_code[client_code.length] = "// Convert Text to Memoryblock"
client_code[client_code.length] = "Dim data As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(json)";
} else if(request.urlEncodedBody) {
body = request.urlEncodedBody;
if(Object.size(body) > 0) {
mimeType = "application/x-www-form-urlencoded";
client_code[client_code.length] = "Dim textArr() as Text";
for(var propertyName in body) {
var key = propertyName;
var value = body[key];
client_code[client_code.length] = "textArr.append \"" + key + "=" + encodeURIComponent(value) + "\"";
}
client_code[client_code.length] = "Dim textData as Text = Text.Join(textArr,\"&\")";
client_code[client_code.length] = "";
client_code[client_code.length] = "// Convert Text to Memoryblock"
client_code[client_code.length] = "Dim data As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(textData)";
}
} else if(request.body) {
if(request.body.length > 0) {
var replaceCRLF = new RegExp('\n', 'g');
var replaceQuotes = new RegExp('\"', 'g');
body = request.body;
//body = body.replace(replaceCRLF
mimeType = "text/plain";
// Some generic body data
client_code[client_code.length] = "// Make an EOL object (CRLF)"
client_code[client_code.length] = "Dim EOL as Text = Text.FromUnicodeCodepoint(13) + Text.FromUnicodeCodepoint(10)";
client_code[client_code.length] = "";
client_code[client_code.length] = "// Put raw data into a Text object";
client_code[client_code.length] = "Dim textData as Text = \"" + body.replace(replaceQuotes,"\"\"").replace(replaceCRLF, "\" + EOL + \"") + "\"";
client_code[client_code.length] = "";
client_code[client_code.length] = "// Convert Text to Memoryblock";
client_code[client_code.length] = "Dim data As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(textData)";
}
}
if(mimeType) {
client_code[client_code.length] = "";
client_code[client_code.length] = "// Assign to the Request's Content";
client_code[client_code.length] = "mySocket.SetRequestContent(data,\"" + mimeType + "\")";
client_code[client_code.length] = "";
}
client_code[client_code.length] = "// Set the URL";
client_code[client_code.length] = "dim url as Text = \"" + request.url + "\"";
client_code[client_code.length] = ""
client_code[client_code.length] = "// Send Asynchronous Request"
client_code[client_code.length] = "mySocket.Send(\"" + request.method + "\",url)";
return client_code.join("\r");
}
}
}
XojoNewCodeGenerator.identifier = "com.xojo.PawExtensions.iOSCodeGenerator";
XojoNewCodeGenerator.title = "Xojo iOS Framework";
XojoNewCodeGenerator.fileExtension = "xojo_code";
registerCodeGenerator(XojoNewCodeGenerator);
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};