-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathrHttr.js
278 lines (255 loc) · 8.05 KB
/
rHttr.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
const getOptions = require('./options').getOptions,
sanitizeString = require('./util/sanitize').sanitizeString,
sanitizeOptions = require('./util/sanitize').sanitizeOptions,
parseBody = require('./util/parseBody').parseBody;
/**
* Returns the snippet header
*
* @module convert
*
* @returns {string} the snippet headers (uses)
*/
function getSnippetHeader () {
return 'library(httr)\n\n';
}
/**
* Returns the snippet footer
*
* @module convert
* @returns {string} the snippet headers (uses)
*/
function getSnippetFooter () {
return 'cat(content(res, \'text\'))';
}
/**
* Gets the defined indentation from options
*
* @param {object} options - process options
* @returns {String} - indentation characters
*/
function getIndentation (options) {
if (options && options.indentType && options.indentCount) {
let charIndentation = options.indentType === 'Tab' ? '\t' : ' ';
return charIndentation.repeat(options.indentCount);
}
return ' ';
}
/**
* Used to get the headers and put them in the desired form of the language
*
* @param {Object} request - postman SDK-request object
* @returns {String} - request headers in the desired format
*/
function getRequestHeaders (request) {
return request.headers.members;
}
/**
* Returns the request's url in string format
*
* @param {Object} request - postman SDK-request object
* @returns {String} - request url in string representation
*/
function getRequestURL (request) {
return request.url.toString();
}
/**
* Validates if the input is a function
*
* @module convert
*
* @param {*} validateFunction - postman SDK-request object
* @returns {boolean} true if is a function otherwise false
*/
function validateIsFunction (validateFunction) {
return typeof validateFunction === 'function';
}
/**
* Returns the request's url in string format
*
* @param {Object} request - postman SDK-request object
* @returns {String} - request url in string representation
*/
function getRequestMethod (request) {
return request.method;
}
/**
* Transforms an array of headers into the desired form of the language
*
* @param {Array} mapToSnippetArray - array of key values
* @param {String} indentation - used for indenting snippet's structure
* @param {boolean} sanitize - whether to sanitize the key and values
* @returns {String} - array in the form of [ key => value ]
*/
function getSnippetArray (mapToSnippetArray, indentation, sanitize) {
let mappedArray = mapToSnippetArray.map((entry) => {
return `${indentation}'${sanitize ? sanitizeString(entry.key, true) : entry.key}' = ` +
`${sanitize ? '\'' + sanitizeString(entry.value) + '\'' : entry.value}`;
});
return `c(\n${mappedArray.join(',\n')}\n)`;
}
/**
* Groups the headers with the same key value
*
* @param {array} headers The array of headers
* @returns {array} An array with grouped headers
*/
function groupHeadersSameKey (headers) {
let grouped = {},
groupedList = [];
headers.forEach((header) => {
if (grouped.hasOwnProperty(header.key)) {
grouped[header.key].push(header.value);
}
else {
grouped[header.key] = [header.value];
}
});
groupedList = Object.keys(grouped).map((key) => {
return {
key,
value: grouped[key].join(', ')
};
});
return groupedList;
}
/**
* Transforms an array of headers into the desired form of the language
*
* @param {Array} headers - postman SDK-headers
* @param {String} indentation - used for indenting snippet's structure
* @returns {String} - request headers in the desired format
*/
function getSnippetHeaders (headers, indentation) {
if (headers.length > 0) {
headers = headers.filter((header) => { return !header.disabled; });
headers = groupHeadersSameKey(headers);
return `headers = ${getSnippetArray(headers, indentation, true)}\n\n`;
}
return '';
}
/**
* Return an encode snippet depending on the mode
*
* @param {object} mode - The body mode from request
* @returns {string} the encode snippet
*/
function getEncodeSnippetByMode (mode) {
const isForm = ['urlencoded'].includes(mode),
isMultipart = ['formdata'].includes(mode);
let snippet = '';
if (isForm) {
snippet = ', encode = \'form\'';
}
else if (isMultipart) {
snippet = ', encode = \'multipart\'';
}
return snippet;
}
/**
* Creates the snippet request for the postForm method
*
* @module convert
*
* @param {string} url - string url of the service
* @param {boolean} hasParams - wheter or not include the params
* @param {boolean} hasHeaders - wheter or not include the headers
* @param {string} methodUC - The method upper cased
* @param {string} mode - the request body mode
* @param {number} requestTimeout - The request timeout in the options
* @returns {String} - returns generated snippet
*/
function getSnippetFromMethod (url, hasParams, hasHeaders, methodUC, mode, requestTimeout = 0) {
let paramsSnippet = hasParams ? ', body = body' : '',
headersSnippet = hasHeaders ? ', add_headers(headers)' : '',
encodeSnippet = getEncodeSnippetByMode(mode),
timeoutSnippet = requestTimeout ? `, timeout(${requestTimeout})` : '';
return `res <- VERB("${methodUC}", url = "${url}"` +
`${paramsSnippet}${headersSnippet}${encodeSnippet}${timeoutSnippet})\n\n`;
}
/**
* Creates the snippet request for either get ulr or post form
*
* @module convert
*
* @param {object} requestData - an object that includes:
* {string} method - request http method
* {boolean} hasParams - wheter or not include the params
* {boolean} hasHeaders - wheter or not include the headers
* {string} mode - the request body mode
* {number} requestTimeout - The request timeout from the options
* @returns {String} - returns generated snippet
*/
function getSnippetRequest ({url, method, hasParams, hasHeaders, mode, requestTimeout}) {
const methodUC = method.toUpperCase();
let snippetRequest = '';
if (methodUC && methodUC !== '') {
snippetRequest = getSnippetFromMethod(url, hasParams, hasHeaders, methodUC, mode, requestTimeout);
}
return snippetRequest;
}
/**
* Gets the defined body trim from options
*
* @param {object} options - process options
* @returns {boolea} - wheter to trim the request body
*/
function getBodyTrim (options) {
if (options && options.trimRequestBody) {
return options.trimRequestBody;
}
return false;
}
/**
* Used to convert the postman sdk-request object in PHP-Guzzle request snippet
*
* @module convert
*
* @param {Object} request - postman SDK-request object
* @param {object} options - process options
* @param {Function} callback - function with parameters (error, snippet)
* @returns {String} - returns generated PHP-Guzzle snippet via callback
*/
function convert (request, options, callback) {
if (!validateIsFunction(callback)) {
throw new Error('R-Httr~convert: Callback is not a function');
}
let snippet = '';
options = sanitizeOptions(options, getOptions());
const method = getRequestMethod(request),
indentation = getIndentation(options),
url = getRequestURL(request),
mode = request.body ? request.body.mode : '',
snippetHeaders = getSnippetHeaders(getRequestHeaders(request), indentation),
snippetHeader = getSnippetHeader(),
snippetFooter = getSnippetFooter(),
snippetbody = parseBody(request.body, indentation, getBodyTrim(options), request.headers.get('Content-Type')),
snippetRequest = getSnippetRequest({
url: url,
method: method,
hasParams: snippetbody !== '',
hasHeaders: snippetHeaders !== '',
mode: mode,
requestTimeout: options.requestTimeout
});
snippet += snippetHeader;
snippet += snippetHeaders;
snippet += snippetbody;
snippet += snippetRequest;
snippet += snippetFooter;
return callback(null, snippet);
}
module.exports = {
/**
* Used in order to get options for generation of R-rCurl code snippet
*
* @module getOptions
*
* @returns {Array} Options specific to generation of R-rCurl code snippet
*/
getOptions,
convert,
getSnippetHeaders,
getSnippetFromMethod,
getSnippetRequest,
getIndentation
};