-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathparseRequest.js
180 lines (173 loc) · 6.42 KB
/
parseRequest.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
var _ = require('./lodash'),
sanitize = require('./util').sanitize,
path = require('path');
/**
* parses body of request when type of the request body is formdata or urlencoded and
* returns code snippet for nodejs to add body
*
* @param {Array<Object>} dataArray - array containing body elements of request
* @param {String} indentString - string required for indentation
* @param {Boolean} trimBody - indicates whether to trim body or not
*/
function extractFormData (dataArray, indentString, trimBody) {
if (!dataArray) {
return '';
}
var snippetString = _.reduce(dataArray, (accumalator, item) => {
if (item.disabled) {
return accumalator;
}
/* istanbul ignore next */
if (item.type === 'file') {
/**
* creating snippet to send file in nodejs request
* for example:
* 'fieldname': {
* 'value': fs.createStream('filename.ext'),
* 'options': {
* 'filename': 'filename.ext',
* 'contentType: null
* }
* }
* }
*/
if (Array.isArray(item.src) && item.src.length) {
let fileSnippet = '',
fileArray = [];
_.forEach(item.src, (filePath) => {
fileArray.push(`${indentString.repeat(3)}fs.createReadStream('${sanitize(filePath, trimBody)}')`);
});
if (fileArray.length) {
fileSnippet += `${indentString.repeat(2)}'${sanitize(item.key, trimBody)}': ` +
`[\n${fileArray.join(',\n')}\n${indentString.repeat(2)}]`;
accumalator.push(fileSnippet);
}
else {
return accumalator;
}
}
else if (typeof item.src !== 'string') {
accumalator.push([
indentString.repeat(2) + `'${sanitize(item.key, trimBody)}': {`,
indentString.repeat(3) + '\'value\': fs.createReadStream(\'/path/to/file\'),',
indentString.repeat(3) + '\'options\': {',
indentString.repeat(4) + '\'filename\': \'filename\'',
indentString.repeat(4) + '\'contentType\': null',
indentString.repeat(3) + '}',
indentString.repeat(2) + '}'
].join('\n'));
}
else {
var pathArray = item.src.split(path.sep),
fileName = pathArray[pathArray.length - 1];
accumalator.push([
indentString.repeat(2) + `'${sanitize(item.key, trimBody)}': {`,
indentString.repeat(3) + `'value': fs.createReadStream('${sanitize(item.src, trimBody)}'),`,
indentString.repeat(3) + '\'options\': {',
indentString.repeat(4) + `'filename': '${sanitize(fileName, trimBody)}',`,
indentString.repeat(4) + '\'contentType\': null',
indentString.repeat(3) + '}',
indentString.repeat(2) + '}'
].join('\n'));
}
}
else {
accumalator.push(
indentString.repeat(2) +
`'${sanitize(item.key, trimBody)}': '${sanitize(item.value, trimBody)}'`
);
}
return accumalator;
}, []);
return snippetString.join(',\n') + '\n';
}
/**
* Parses body object based on mode of body and returns code snippet
*
* @param {Object} requestbody - json object for body of request
* @param {String} indentString - string for indentation
* @param {Boolean} trimBody - indicates whether to trim body fields or not
* @param {String} contentType Content type of the body being sent
*/
function parseBody (requestbody, indentString, trimBody, contentType) {
if (requestbody) {
switch (requestbody.mode) {
case 'raw':
if (contentType === 'application/json') {
try {
let jsonBody = JSON.parse(requestbody[requestbody.mode]);
return `body: JSON.stringify(${JSON.stringify(jsonBody)})\n`;
}
catch (error) {
return `body: '${sanitize(requestbody[requestbody.mode])}'\n`;
}
}
return `body: '${sanitize(requestbody[requestbody.mode])}'\n`;
// eslint-disable-next-line no-case-declarations
case 'graphql':
let query = requestbody[requestbody.mode].query,
graphqlVariables;
try {
graphqlVariables = JSON.parse(requestbody[requestbody.mode].variables);
}
catch (e) {
graphqlVariables = {};
}
return 'body: JSON.stringify({\n' +
`${indentString.repeat(2)}query: '${sanitize(query, trimBody)}',\n` +
`${indentString.repeat(2)}variables: ${JSON.stringify(graphqlVariables)}\n` +
`${indentString}})`;
case 'formdata':
return `formData: {\n${extractFormData(requestbody[requestbody.mode], indentString, trimBody)}` +
indentString + '}';
case 'urlencoded':
return `form: {\n${extractFormData(requestbody[requestbody.mode], indentString, trimBody)}` +
indentString + '}';
/* istanbul ignore next */
case 'file':
// return 'formData: {\n' +
// extractFormData(requestbody[requestbody.mode], indentString, trimBody) +
// indentString + '}';
return 'body: "<file contents here>"\n';
default:
return '';
}
}
return '';
}
/**
* parses header of request object and returns code snippet of nodejs request to add header
*
* @param {Object} request - Postman SDK request object
* @param {String} indentString - indentation required in code snippet
* @returns {String} - code snippet of nodejs request to add header
*/
function parseHeader (request, indentString) {
var headerObject = request.getHeaders({enabled: true}),
headerSnippet = indentString + '\'headers\': {\n';
if (!_.isEmpty(headerObject)) {
headerSnippet += _.reduce(Object.keys(headerObject), function (accumalator, key) {
if (Array.isArray(headerObject[key])) {
var headerValues = [];
_.forEach(headerObject[key], (value) => {
headerValues.push(`'${sanitize(value)}'`);
});
accumalator.push(
indentString.repeat(2) + `'${sanitize(key, true)}': [${headerValues.join(', ')}]`
);
}
else {
accumalator.push(
indentString.repeat(2) + `'${sanitize(key, true)}': '${sanitize(headerObject[key])}'`
);
}
return accumalator;
}, []).join(',\n') + '\n';
}
headerSnippet += indentString + '}';
return headerSnippet;
}
module.exports = {
parseBody: parseBody,
parseHeader: parseHeader
};