-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathparseRequest.js
181 lines (163 loc) · 4.93 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
181
var _ = require('./lodash'),
sanitize = require('./util').sanitize;
/**
* Returns given content type or default if falsey
*
* @param {String} contentType
* @returns {String}
*/
function parseContentType (contentType) {
return contentType || 'text/plain';
}
/**
* Parses header in Postman-SDK request and returns code snippet of csharp-httpclient for adding headers
*
* @param {Object} builder - Code Builder
* @param {Object} requestJson - Postman SDK request object
*/
function parseHeader (builder, requestJson) {
// Possibly add support for the typed header properties
if (!Array.isArray(requestJson.header)) {
return;
}
requestJson.header.forEach((header) => {
if (!header.disabled && sanitize(header.key) !== 'Content-Type') {
builder.appendLine(`request.Headers.Add("${sanitize(header.key, true)}", "${sanitize(header.value)}");`);
}
});
}
/**
*
* @param {CodeBuilder} builder
* @param {Object} requestBody
*/
function parseFormUrlEncoded (builder, requestBody) {
let list = requestBody[requestBody.mode].reduce((collection, data) => {
if (data.disabled) {
return collection;
}
(!data.value) && (data.value = '');
collection.push('collection.Add(new' +
`("${sanitize(data.key)}", "${sanitize(data.value)}"));`);
return collection;
}, []);
if (list && !_.isEmpty(list)) {
builder.appendLine('var collection = new List<KeyValuePair<string, string>>();');
builder.appendLines(list);
builder.appendLine('var content = new FormUrlEncodedContent(collection);');
builder.appendLine('request.Content = content;');
builder.addUsing('System.Collections.Generic');
}
}
/**
*
* @param {CodeBuilder} builder
* @param {String} key
* @param {String} fileSrc
*/
function addFile (builder, key, fileSrc) {
builder.appendLine('content.Add(new StreamContent(File.OpenRead' +
`("${sanitize(fileSrc)}")), "${sanitize(key)}", "${sanitize(fileSrc)}");`);
}
/**
*
* @param {CodeBuilder} builder
* @param {Object} requestBody
*/
function parseFormData (builder, requestBody) {
var allDisabled = requestBody[requestBody.mode].every((i) => {
return i.disabled;
});
if (allDisabled) {
return;
}
builder.appendLine('var content = new MultipartFormDataContent();');
requestBody[requestBody.mode].forEach((i) => {
if (i.disabled) {
return;
}
if (i.type === 'text') {
builder.appendLine('content.Add(new StringContent(' +
`"${sanitize(i.value)}"), "${sanitize(i.key)}");`);
}
else if (i.type === 'file') {
if (Array.isArray(i.src)) {
// Multiple files
i.src.forEach((file) => {
addFile(builder, i.key, file);
});
}
else {
// Single file
addFile(builder, i.key, i.src);
}
}
});
builder.appendLine('request.Content = content;');
}
/**
*
* @param {CodeBuilder} builder
* @param {Object} requestBody
*/
function parseGraphQL (builder, requestBody) {
let query = requestBody.graphql.query,
graphqlVariables;
try {
graphqlVariables = JSON.parse(requestBody.graphql.variables);
}
catch (e) {
graphqlVariables = {};
}
builder.appendLine('var content = new StringContent(' +
`"${sanitize(JSON.stringify({ query: query, variables: graphqlVariables }))}"` +
', null, "application/json");');
}
/**
*
* @param {CodeBuilder} builder
* @param {Object} request
*/
function parseBody (builder, request) {
var requestBody = request.body ? request.body.toJSON() : {},
contentType = request.getHeaders({ enabled: true, ignoreCase: true })['content-type'];
if (!_.isEmpty(requestBody)) {
switch (requestBody.mode) {
case 'urlencoded':
parseFormUrlEncoded(builder, requestBody);
break;
case 'formdata':
parseFormData(builder, requestBody);
break;
case 'raw':
builder.appendLine(
`var content = new StringContent(${JSON.stringify(requestBody[requestBody.mode])}, null, ` +
`"${parseContentType(contentType)}");`);
builder.appendLine('request.Content = content;');
break;
case 'graphql':
parseGraphQL(builder, requestBody);
builder.appendLine('request.Content = content;');
break;
case 'file':
builder
.appendLine('request.Content = new StreamContent(File.OpenRead("' +
`${sanitize(requestBody[requestBody.mode].src || '"<File path>"')}"));`);
builder.addUsing('System.IO');
break;
default:
}
}
else if (contentType) {
// The request has no body but sometimes it wants me to force a content-type anyways
builder.appendLine('var content = new StringContent(string.Empty);');
builder.appendLine('content.Headers.ContentType = new MediaTypeHeaderValue("' +
`${contentType}");`);
builder.addUsing('System.Net.Http.Headers');
builder.appendLine('request.Content = content;');
}
}
module.exports = {
parseHeader: parseHeader,
parseBody: parseBody
};