Skip to content

Commit c2d8b4c

Browse files
committed
Merge pull request Kong#15 from therebelrobot/add-node-request
Add node request
2 parents e3e69b7 + 2c04302 commit c2d8b4c

12 files changed

Lines changed: 265 additions & 0 deletions

File tree

‎src/targets/node/request.js‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
var reducer = require('.,/../../reducer');
5+
6+
module.exports = function(options) {
7+
var opts = util._extend({
8+
indent: ' '
9+
}, options);
10+
11+
var code = [];
12+
13+
var reqOpts = {
14+
url: this.source.fullUrl,
15+
headers: this.source.headersObj
16+
};
17+
var fsReplace = [];
18+
if (this.source.headersObj['Content-Type'] === 'application/x-www-form-urlencoded') {
19+
reqOpts.formData = this.source.postData.params.reduce(function(finalObj, thisParam) {
20+
finalObj[thisParam.name] = thisParam.value;
21+
return finalObj;
22+
}, {});
23+
}
24+
else if (this.source.headersObj['Content-Type'] === 'application/json') {
25+
if (this.source.postData.params) {
26+
reqOpts.body = JSON.stringify(
27+
this.source.postData.params.reduce(function(finalObj, thisParam) {
28+
finalObj[thisParam.name] = thisParam.value;
29+
return finalObj;
30+
}, {})
31+
);
32+
}
33+
reqOpts.json = true;
34+
}
35+
else if (this.source.headersObj["Content-Type"] === "multipart/form-data") {
36+
this.source.postData.params.forEach(function(param) {
37+
reqOpts.formData = reqOpts.formData || {};
38+
reqOpts.formData[param.name] = {};
39+
if (param.fileName) {
40+
if (!param.value.length) {
41+
param.value = 'fs.createReadStream(\'' + param.fileName + '\')';
42+
fsReplace.push(param.value);
43+
}
44+
reqOpts.formData[param.name].value = param.value;
45+
if (param.fileName.indexOf('/') > -1) {
46+
param.fileName = param.fileName.split('/');
47+
param.fileName = param.fileName[param.fileName.length - 1];
48+
}
49+
else if (param.fileName.indexOf('\\') > -1) {
50+
param.fileName = param.fileName.split('\\');
51+
param.fileName = param.fileName[param.fileName.length - 1];
52+
}
53+
reqOpts.formData[param.name].options = {
54+
filename: param.fileName,
55+
"content-type": param.contentType
56+
}
57+
}
58+
else {
59+
reqOpts.formData[param.name] = param.value;
60+
}
61+
})
62+
}
63+
else {
64+
reqOpts.body = this.source.postData.params;
65+
}
66+
67+
var simpleRequest =
68+
this.source.cookies.length === 0 &&
69+
this.source.headers.length === 0 && (
70+
!this.source.postData.params ||
71+
this.source.postData.params.length === 0
72+
);
73+
74+
// construct cookies argument
75+
var cookies = this.source.cookies.map(function(cookie) {
76+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
77+
});
78+
79+
if (cookies.length) {
80+
reqOpts.headers.Cookie = cookies.join('; ');
81+
}
82+
83+
code.push('var request = require(\'request\');');
84+
if (fsReplace.length > 0) {
85+
code.push('var fs = require(\'fs\');');
86+
}
87+
88+
if(!simpleRequest) code.push(null);
89+
90+
var options = !simpleRequest ? util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)) : null;
91+
fsReplace.forEach(function(fsToReplace) {
92+
options = options.replace('"' + fsToReplace + '"', fsToReplace);
93+
})
94+
95+
if(!simpleRequest) code.push(options);
96+
97+
code.push(null);
98+
var requestLine = 'request.';
99+
requestLine += this.source.method === 'DELETE' ? 'del(' : this.source.method.toLowerCase() + '(';
100+
requestLine += simpleRequest ? '\'' + this.source.fullUrl + '\'' : 'options';
101+
requestLine += ', function(error, response, body){';
102+
code.push(requestLine);
103+
code.push(opts.indent + 'if(error) throw new Error(error);');
104+
105+
code.push(null);
106+
107+
code.push('});');
108+
109+
code.push(null);
110+
111+
return code.join('\n');
112+
};
113+
114+
module.exports.info = {
115+
key: 'request',
116+
title: 'request',
117+
link: 'https://github.com/request/request',
118+
description: 'Simplified HTTP request client'
119+
};

‎test/fixtures/available-targets.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
"title": "HTTP",
3232
"link": "http://nodejs.org/api/http.html#http_http_request_options_callback",
3333
"description": "Node.js native HTTP interface"
34+
},
35+
{
36+
"key": "request",
37+
"title": "request",
38+
"link": "https://github.com/request/request",
39+
"description": "Simplified HTTP request client"
3440
}
3541
]
3642
},
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var request = require('request');
2+
3+
var options = {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
6+
"Content-Type": "application/x-www-form-urlencoded"
7+
},
8+
"formData": {
9+
"foo": "bar"
10+
}
11+
};
12+
13+
request.post(options, function(error, response, body){
14+
if(error) throw new Error(error);
15+
16+
});
17+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var request = require('request');
2+
3+
var options = {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
6+
"Content-Type": "application/json"
7+
},
8+
"json": true
9+
};
10+
11+
request.post(options, function(error, response, body){
12+
if(error) throw new Error(error);
13+
14+
});
15+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var request = require('request');
2+
3+
var options = {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
6+
"Cookie": "foo=bar; bar=baz"
7+
}
8+
};
9+
10+
request.post(options, function(error, response, body){
11+
if(error) throw new Error(error);
12+
13+
});
14+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var request = require('request');
2+
3+
var options = {
4+
"url": "http://mockbin.com/har?baz=abc&foo=bar&foo=baz",
5+
"headers": {
6+
"Accept": "application/json",
7+
"Content-Type": "application/x-www-form-urlencoded",
8+
"Cookie": "foo=bar; bar=baz"
9+
},
10+
"formData": {
11+
"foo": "bar"
12+
}
13+
};
14+
15+
request.post(options, function(error, response, body){
16+
if(error) throw new Error(error);
17+
18+
});
19+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var request = require('request');
2+
3+
var options = {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
6+
"Accept": "application/json",
7+
"X-Foo": "Bar"
8+
}
9+
};
10+
11+
request.get(options, function(error, response, body){
12+
if(error) throw new Error(error);
13+
14+
});
15+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var request = require('request');
2+
3+
var options = {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
6+
"Content-Type": "multipart/form-data"
7+
},
8+
"formData": {
9+
"foo": {
10+
"value": "hello world",
11+
"options": {
12+
"filename": "hello.txt",
13+
"content-type": "text/plain"
14+
}
15+
}
16+
}
17+
};
18+
19+
request.post(options, function(error, response, body){
20+
if(error) throw new Error(error);
21+
22+
});
23+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var request = require('request');
2+
3+
var options = {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
6+
"Content-Type": "multipart/form-data"
7+
},
8+
"formData": {
9+
"foo": "bar"
10+
}
11+
};
12+
13+
request.post(options, function(error, response, body){
14+
if(error) throw new Error(error);
15+
16+
});
17+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var request = require('request');
2+
3+
request.get('http://mockbin.com/har?key=value&baz=abc&foo=bar&foo=baz', function(error, response, body){
4+
if(error) throw new Error(error);
5+
6+
});
7+

0 commit comments

Comments
 (0)