forked from postmanlabs/postman-code-generators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-http-converter.js
126 lines (119 loc) · 4.14 KB
/
code-http-converter.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
let utils = require('./util'),
_ = require('./lodash'),
sdk = require('postman-collection');
/**
* Used in order to get additional options for generation of C# code snippet (i.e. Include Boilerplate code)
*
* @module getOptions
*
* @returns {Array} Additional options specific to generation of http code snippet
*/
function getOptions () {
return [{
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'
}];
}
/**
* Converts a Postman SDK request to HTTP message
*
* @param {Object} request - Postman SDK request
* @param {Object} options - Options for converter
* @param {Boolean} options.trimRequestBody - determines whether to trim the body or not
* @param {Function} callback callback
* @returns {Function} returns the snippet with the callback function.
*/
function convert (request, options, callback) {
let snippet = '',
url, host, path, query, body, headers;
options = utils.sanitizeOptions(options, getOptions());
url = sdk.Url.parse(request.url.toString());
host = url.host ? url.host.join('.') : '';
host += url.port ? ':' + url.port : '';
path = url.path ? '/' + url.path.join('/') : '/';
query = url.query ? _.reduce(url.query, (accum, q) => {
accum.push(`${q.key}=${q.value}`);
return accum;
}, []) : [];
if (query.length > 0) {
query = '?' + query.join('&');
}
else {
query = '';
}
snippet = `${request.method} ${path}${query} HTTP/1.1\n`;
snippet += `Host: ${host}`;
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'
});
}
}
// 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;
// check if type is file or text
if (type === 'file') {
// if src is not of type string we check for array(multiple files)
if (typeof param.src !== 'string') {
// if src is an array(not empty), iterate over it and add files as separate form fields
if (Array.isArray(param.src) && param.src.length) {
param.src.forEach((filePath) => {
utils.addFormParam(formdataArray, key, param.type, filePath, disabled, contentType);
});
}
// if src is not an array or string, or is an empty array, add a placeholder for file path(no files case)
else {
utils.addFormParam(formdataArray, key, param.type, '/path/to/file', disabled, contentType);
}
}
// if src is string, directly add the param with src as filepath
else {
utils.addFormParam(formdataArray, key, param.type, param.src, disabled, contentType);
}
}
// if type is text, directly add it to formdata array
else {
utils.addFormParam(formdataArray, key, param.type, param.value, disabled, contentType);
}
});
request.body.update({
mode: 'formdata',
formdata: formdataArray
});
}
body = utils.getBody(request, options.trimRequestBody);
if (body && body.length !== 0 && !request.headers.has('Content-Length')) {
request.addHeader({
key: 'Content-Length',
value: body.length
});
}
headers = utils.getHeaders(request);
snippet += headers ? `\n${headers}` : '';
snippet += body ? `\n\n${body}` : '';
return callback(null, snippet);
}
module.exports = {
getOptions: getOptions,
convert: convert
};