forked from postmanlabs/postman-code-generators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseBody.js
212 lines (205 loc) · 7.45 KB
/
parseBody.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
var _ = require('../lodash'),
sanitize = require('./sanitize').sanitize,
trueToken = '__PYTHON#%0True__',
falseToken = '__PYTHON#%0False__',
nullToken = '__PYTHON#%0NULL__',
contentTypeHeaderMap = {
'aac': 'audio/aac',
'abw': 'application/x-abiword',
'arc': 'application/x-freearc',
'avi': 'video/x-msvideo',
'azw': 'application/vnd.amazon.ebook',
'bin': 'application/octet-stream',
'bmp': 'image/bmp',
'bz': 'application/x-bzip',
'bz2': 'application/x-bzip2',
'csh': 'application/x-csh',
'css': 'text/css',
'csv': 'text/csv',
'doc': 'application/msword',
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'eot': 'application/vnd.ms-fontobject',
'epub': 'application/epub+zip',
'gif': 'image/gif',
'htm': 'text/html',
'html': 'text/html',
'ico': 'image/vnd.microsoft.icon',
'ics': 'text/calendar',
'jar': 'application/java-archive',
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'js': 'text/javascript',
'json': 'application/json',
'jsonld': 'application/ld+json',
'mid': 'audio/midi',
'midi': 'audio/midi',
'mjs': 'text/javascript',
'mp3': 'audio/mpeg',
'mpeg': 'video/mpeg',
'mpkg': 'application/vnd.apple.installer+xml',
'odp': 'application/vnd.oasis.opendocument.presentation',
'ods': 'application/vnd.oasis.opendocument.spreadsheet',
'odt': 'application/vnd.oasis.opendocument.text',
'oga': 'audio/ogg',
'ogv': 'video/ogg',
'ogx': 'application/ogg',
'otf': 'font/otf',
'png': 'image/png',
'pdf': 'application/pdf',
'ppt': 'application/vnd.ms-powerpoint',
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'rar': 'application/x-rar-compressed',
'rtf': 'application/rtf',
'sh': 'application/x-sh',
'svg': 'image/svg+xml',
'swf': 'application/x-shockwave-flash',
'tar': 'application/x-tar',
'tif': 'image/tiff',
'tiff': 'image/tiff',
'ts': 'video/mp2t',
'ttf': 'font/ttf',
'txt': 'text/plain',
'vsd': 'application/vnd.visio',
'wav': 'audio/wav',
'weba': 'audio/webm',
'webm': 'video/webm',
'webp': 'image/webp',
'woff': 'font/woff',
'woff2': 'font/woff2',
'xhtml': 'application/xhtml+xml',
'xls': 'application/vnd.ms-excel',
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'xml': 'text/xml',
'xul': 'application/vnd.mozilla.xul+xml',
'zip': 'application/zip',
'3gp': 'video/3gpp',
'7z': 'application/x-7z-compressed',
'7-zip': 'application/x-7z-compressed'
};
/**
* Convert true, false and null to Python equivalent True, False and None
*
* @param {String} key
* @param {Object} value
*/
function replacer (key, value) {
if (typeof value === 'boolean') {
return value ? trueToken : falseToken;
}
else if (value === null) {
return nullToken;
}
return value;
}
/**
* Convert JSON into a valid Python dict
* The "true", "false" and "null" tokens are not valid in Python
* so we need to convert them to "True", "False" and "None"
*
* @param {Object} jsonBody - JSON object to be converted
* @param {Number} indentCount - Number of spaces to insert at each indentation level
*/
function pythonify (jsonBody, indentCount) {
return JSON.stringify(jsonBody, replacer, indentCount)
.replace(new RegExp(`"${trueToken}"`, 'g'), 'True')
.replace(new RegExp(`"${falseToken}"`, 'g'), 'False')
.replace(new RegExp(`"${nullToken}"`, 'g'), 'None');
}
/**
* Used to parse the body of the postman SDK-request and return in the desired format
*
* @param {Object} request - postman SDK-request object
* @param {String} indentation - used for indenting snippet's structure
* @param {Boolean} bodyTrim - whether to trim request body fields
* @param {String} contentType - content-type of body
* @returns {String} - request body
*/
module.exports = function (request, indentation, bodyTrim, contentType) {
// used to check whether body is present in the request or not
if (request.body) {
var requestBody = '',
bodyDataMap,
bodyFileMap,
enabledBodyList;
switch (request.body.mode) {
case 'raw':
if (_.isEmpty(request.body[request.body.mode])) {
requestBody = 'payload = {}\n';
}
// Match any application type whose underlying structure is json
// For example application/vnd.api+json
// All of them have +json as suffix
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
try {
let jsonBody = JSON.parse(request.body[request.body.mode]);
return `payload = json.dumps(${pythonify(jsonBody, indentation.length)})\n`;
}
catch (error) {
// Do nothing
}
}
return `payload = ${sanitize(request.body[request.body.mode],
request.body.mode, bodyTrim)}\n`;
case 'graphql':
// eslint-disable-next-line no-case-declarations
let query = request.body[request.body.mode].query,
graphqlVariables;
try {
graphqlVariables = JSON.parse(request.body[request.body.mode].variables);
}
catch (e) {
graphqlVariables = {};
}
requestBody += `payload = ${sanitize(JSON.stringify({
query: query,
variables: graphqlVariables
}),
'raw', bodyTrim)}\n`;
return requestBody;
case 'urlencoded':
enabledBodyList = _.reject(request.body[request.body.mode], 'disabled');
if (!_.isEmpty(enabledBodyList)) {
bodyDataMap = _.map(enabledBodyList, function (value) {
return `${sanitize(value.key, request.body.mode, bodyTrim)}=` +
`${sanitize(value.value, request.body.mode, bodyTrim)}`;
});
requestBody += `payload = '${bodyDataMap.join('&')}'\n`;
}
else {
requestBody = 'payload = {}\n';
}
return requestBody;
case 'formdata':
enabledBodyList = _.reject(request.body[request.body.mode], 'disabled');
if (!_.isEmpty(enabledBodyList)) {
bodyDataMap = _.map(_.filter(enabledBodyList, {'type': 'text'}), function (value) {
return (`'${sanitize(value.key, request.body.mode, bodyTrim)}': ` +
`'${sanitize(value.value, request.body.mode, bodyTrim)}'`);
});
bodyFileMap = _.map(_.filter(enabledBodyList, {'type': 'file'}), function (value) {
var filesrc = value.src,
fileType = filesrc.split('.')[filesrc.split('.').length - 1],
contentType = contentTypeHeaderMap[fileType];
if (!contentType) {
contentType = 'application/octet-stream';
}
return `${indentation}('${value.key}',('${filesrc.split('/')[filesrc.split('/').length - 1]}'` +
`,open('${sanitize(filesrc, request.body.mode, bodyTrim)}','rb'),` +
`'${contentType}'))`;
});
requestBody = `payload = {${bodyDataMap.join(',\n')}}\nfiles=[\n${bodyFileMap.join(',\n')}\n]\n`;
}
else {
requestBody = 'payload = {}\nfiles={}\n';
}
return requestBody;
case 'file':
// return `payload = {open('${request.body[request.body.mode].src}', 'rb').read()\n}`;
return 'payload = "<file contents here>"\n';
default:
return 'payload = {}\n';
}
}
return 'payload = {}\n';
}
;