-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathindex.js
347 lines (327 loc) · 12 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
var _ = require('./lodash'),
sanitize = require('./util').sanitize,
sanitizeMultiline = require('./util').sanitizeMultiline,
sanitizeOptions = require('./util').sanitizeOptions,
addFormParam = require('./util').addFormParam,
getUrlStringfromUrlObject = require('./util').getUrlStringfromUrlObject,
isFile = false,
self;
/**
* Parses Raw data to fetch syntax
*
* @param {Object} body Raw body data
* @param {boolean} trim trim body option
*/
function parseRawBody (body, trim) {
var bodySnippet;
bodySnippet = `payload := strings.NewReader(\`${sanitizeMultiline(body.toString(), trim)}\`)`;
return bodySnippet;
}
/**
* Parses graphql data to golang syntax
*
* @param {Object} body Raw body data
* @param {boolean} trim trim body option
*/
function parseGraphQL (body, trim) {
let query = body ? body.query : '',
graphqlVariables,
bodySnippet;
try {
graphqlVariables = JSON.parse(body.variables);
}
catch (e) {
graphqlVariables = {};
}
bodySnippet = `payload := strings.NewReader("${sanitize(JSON.stringify({
query: query || '',
variables: graphqlVariables
}), trim)}")`;
return bodySnippet;
}
/**
* Parses URLEncoded body from request to fetch syntax
*
* @param {Object} body URLEncoded Body
* @param {boolean} trim trim body option
*/
function parseURLEncodedBody (body, trim) {
var payload, bodySnippet;
payload = _.reduce(body, function (accumulator, data) {
if (!data.disabled) {
accumulator.push(`${encodeURIComponent(data.key, trim)}=${encodeURIComponent(data.value, trim)}`);
}
return accumulator;
}, []).join('&');
bodySnippet = `payload := strings.NewReader("${payload}")`;
return bodySnippet;
}
/**
* Parses formData body from request to fetch syntax
*
* @param {Object} body formData Body
* @param {boolean} trim trim body option
* @param {string} indent indent string
*/
function parseFormData (body, trim, indent) {
var bodySnippet = `payload := &bytes.Buffer{}\n${indent}writer := multipart.NewWriter(payload)\n`;
_.forEach(body, function (data, index) {
if (!data.disabled) {
if (data.type === 'file') {
isFile = true;
bodySnippet += `${indent}file, errFile${index + 1} := os.Open("${data.src}")\n`;
bodySnippet += `${indent}defer file.Close()\n`;
bodySnippet += `${indent}part${index + 1},
errFile${index + 1} := writer.CreateFormFile("${sanitize(data.key, trim)}",` +
`filepath.Base("${data.src}"))\n`;
bodySnippet += `${indent}_, errFile${index + 1} = io.Copy(part${index + 1}, file)\n`;
bodySnippet += `${indent}if errFile${index + 1} != nil {` +
`\n${indent.repeat(2)}fmt.Println(errFile${index + 1})\n` +
`${indent.repeat(2)}return\n${indent}}\n`;
}
else if (data.contentType) {
bodySnippet += `\n${indent}mimeHeader${index + 1} := make(map[string][]string)\n`;
bodySnippet += `${indent}mimeHeader${index + 1}["Content-Disposition"] = `;
bodySnippet += `append(mimeHeader${index + 1}["Content-Disposition"], "form-data; `;
bodySnippet += `name=\\"${sanitize(data.key, trim)}\\"")\n`;
bodySnippet += `${indent}mimeHeader${index + 1}["Content-Type"] = append(`;
bodySnippet += `mimeHeader${index + 1}["Content-Type"], "${data.contentType}")\n`;
bodySnippet += `${indent}fieldWriter${index + 1}, _ := writer.CreatePart(mimeHeader${index + 1})\n`;
bodySnippet += `${indent}fieldWriter${index + 1}.Write([]byte("${sanitize(data.value, trim)}"))\n\n`;
}
else {
bodySnippet += `${indent}_ = writer.WriteField("${sanitize(data.key, trim)}",`;
bodySnippet += ` "${sanitize(data.value, trim)}")\n`;
}
}
});
bodySnippet += `${indent}err := writer.Close()\n${indent}if err != nil ` +
`{\n${indent.repeat(2)}fmt.Println(err)\n` +
`${indent.repeat(2)}return\n${indent}}\n`;
return bodySnippet;
}
/**
* Parses file body from the Request
*
*/
function parseFile () {
// var bodySnippet = `payload := &bytes.Buffer{}\n${indent}writer := multipart.NewWriter(payload)\n`;
// isFile = true;
// bodySnippet += `${indent}// add your file name in the next statement in place of path\n`;
// bodySnippet += `${indent}file, err := os.Open(path)\n`;
// bodySnippet += `${indent}defer file.Close()\n`;
// bodySnippet += `${indent}part, err := writer.CreateFormFile("file", filepath.Base(path))\n`;
// bodySnippet += `${indent}_, err := io.Copy(part, file)\n`;
// bodySnippet += `${indent}err := writer.Close()\n${indent}if err != nil {${indent}fmt.Println(err)}\n`;
var bodySnippet = 'payload := strings.NewReader("<file contents here>")\n';
return bodySnippet;
}
/**
* Parses Body from the Request
*
* @param {Object} body body object from request.
* @param {boolean} trim trim body option
* @param {string} indent indent string
*/
function parseBody (body, trim, indent) {
if (!_.isEmpty(body)) {
switch (body.mode) {
case 'urlencoded':
return parseURLEncodedBody(body.urlencoded, trim);
case 'raw':
return parseRawBody(body.raw, trim);
case 'graphql':
return parseGraphQL(body.graphql, trim);
case 'formdata':
return parseFormData(body.formdata, trim, indent);
case 'file':
return parseFile(body.file, trim, indent);
default:
return '';
}
}
return '';
}
/**
* Parses headers from the request.
*
* @param {Object} headers headers from the request.
* @param {string} indent indent string
*/
function parseHeaders (headers, indent) {
var headerSnippet = '';
if (!_.isEmpty(headers)) {
headers = _.reject(headers, 'disabled');
_.forEach(headers, function (header) {
headerSnippet += `${indent}req.Header.Add("${sanitize(header.key, true)}", "${sanitize(header.value)}")\n`;
});
}
return headerSnippet;
}
self = module.exports = {
convert: function (request, options, callback) {
if (!_.isFunction(callback)) {
throw new Error('GoLang-Converter: callback is not valid function');
}
options = sanitizeOptions(options, self.getOptions());
var codeSnippet, indent, trim, timeout, followRedirect,
bodySnippet = '',
responseSnippet = '',
headerSnippet = '';
indent = options.indentType === 'Tab' ? '\t' : ' ';
indent = indent.repeat(options.indentCount);
timeout = options.requestTimeout;
followRedirect = options.followRedirect;
trim = options.trimRequestBody;
// The following code handles multiple files in the same formdata param.
// It removes the form data params where the src property is an array of filepath strings
// Splits that array into different form data params with src set as a single filepath string
if (request.body && request.body.mode === 'formdata') {
let formdata = request.body.formdata,
formdataArray = [];
formdata.members.forEach((param) => {
let key = param.key,
type = param.type,
disabled = param.disabled,
contentType = param.contentType;
if (type === 'file') {
if (typeof param.src !== 'string') {
if (Array.isArray(param.src) && param.src.length) {
param.src.forEach((filePath) => {
addFormParam(formdataArray, key, param.type, filePath, disabled, contentType);
});
}
else {
addFormParam(formdataArray, key, param.type, '/path/to/file', disabled, contentType);
}
}
else {
addFormParam(formdataArray, key, param.type, param.src, disabled, contentType);
}
}
else {
addFormParam(formdataArray, key, param.type, param.value, disabled, contentType);
}
});
request.body.update({
mode: 'formdata',
formdata: formdataArray
});
}
if (request.body) {
bodySnippet = parseBody(request.body.toJSON(), trim, indent);
}
codeSnippet = 'package main\n\n';
codeSnippet += `import (\n${indent}"fmt"\n`;
if (timeout > 0) {
codeSnippet += `${indent}"time"\n`;
}
if (request.body && request.body.toJSON().mode === 'formdata') {
codeSnippet += `${indent}"bytes"\n${indent}"mime/multipart"\n`;
}
else if (bodySnippet !== '') {
codeSnippet += `${indent}"strings"\n`;
}
if (isFile) {
codeSnippet += `${indent}"os"\n${indent}"path/filepath"\n`;
// Setting isFile as false for further calls to this function
isFile = false;
}
codeSnippet += `${indent}"net/http"\n${indent}"io"\n)\n\n`;
codeSnippet += `func main() {\n\n${indent}url := "${getUrlStringfromUrlObject(request.url)}"\n`;
codeSnippet += `${indent}method := "${request.method}"\n\n`;
if (bodySnippet !== '') {
codeSnippet += indent + bodySnippet + '\n\n';
}
if (timeout > 0) {
codeSnippet += `${indent}timeout := time.Duration(${timeout / 1000} * time.Second)\n`;
}
codeSnippet += indent + 'client := &http.Client {\n';
if (!followRedirect) {
codeSnippet += indent.repeat(2) + 'CheckRedirect: func(req *http.Request, via []*http.Request) ';
codeSnippet += 'error {\n';
codeSnippet += `${indent.repeat(3)}return http.ErrUseLastResponse\n${indent.repeat(2)}},\n`;
}
if (timeout > 0) {
codeSnippet += indent.repeat(2) + 'Timeout: timeout,\n';
}
codeSnippet += indent + '}\n';
if (bodySnippet !== '') {
codeSnippet += `${indent}req, err := http.NewRequest(method, url, payload)\n\n`;
}
else {
codeSnippet += `${indent}req, err := http.NewRequest(method, url, nil)\n\n`;
}
codeSnippet += `${indent}if err != nil {\n${indent.repeat(2)}fmt.Println(err)\n`;
codeSnippet += `${indent.repeat(2)}return\n${indent}}\n`;
if (request.body && !request.headers.has('Content-Type')) {
if (request.body.mode === 'file') {
request.addHeader({
key: 'Content-Type',
value: 'text/plain'
});
}
else if (request.body.mode === 'graphql') {
request.addHeader({
key: 'Content-Type',
value: 'application/json'
});
}
}
headerSnippet = parseHeaders(request.toJSON().header, indent);
if (headerSnippet !== '') {
codeSnippet += headerSnippet + '\n';
}
if (request.body && (request.body.toJSON().mode === 'formdata')) {
codeSnippet += `${indent}req.Header.Set("Content-Type", writer.FormDataContentType())\n`;
}
responseSnippet = `${indent}res, err := client.Do(req)\n`;
responseSnippet += `${indent}if err != nil {\n${indent.repeat(2)}fmt.Println(err)\n`;
responseSnippet += `${indent.repeat(2)}return\n${indent}}\n`;
responseSnippet += `${indent}defer res.Body.Close()\n\n${indent}body, err := io.ReadAll(res.Body)\n`;
responseSnippet += `${indent}if err != nil {\n${indent.repeat(2)}fmt.Println(err)\n`;
responseSnippet += `${indent.repeat(2)}return\n${indent}}\n`;
responseSnippet += `${indent}fmt.Println(string(body))\n}`;
codeSnippet += responseSnippet;
callback(null, codeSnippet);
},
getOptions: function () {
return [{
name: 'Set indentation count',
id: 'indentCount',
type: 'positiveInteger',
default: 2,
description: 'Set the number of indentation characters to add per code level'
},
{
name: 'Set indentation type',
id: 'indentType',
type: 'enum',
availableOptions: ['Tab', 'Space'],
default: 'Space',
description: 'Select the character used to indent lines of code'
},
{
name: 'Set request timeout',
id: 'requestTimeout',
type: 'positiveInteger',
default: 0,
description: 'Set number of milliseconds the request should wait for a ' +
'response before timing out (use 0 for infinity)'
},
{
name: 'Follow redirects',
id: 'followRedirect',
type: 'boolean',
default: true,
description: 'Automatically follow HTTP redirects'
},
{
name: 'Trim request body fields',
id: 'trimRequestBody',
type: 'boolean',
default: false,
description: 'Remove white space and additional lines that may affect the server\'s response'
}];
}
};