Skip to content

Commit ba5e2b7

Browse files
committed
node:unirest cleanup
1 parent 96f7e09 commit ba5e2b7

File tree

15 files changed

+246
-135
lines changed

15 files changed

+246
-135
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ postData: {
313313
###### application/json
314314

315315
- will match when `postData.mimeType` is one of: `application/json`, `text/json`, `text/x-json`, `application/x-json`
316-
- In case of failure to parse `postData.text` as a JSON object, `postData.mimeType` is set to `text/plain`. this is done so that the implementing target, would still attempt to post the raw body as is.
316+
- In case of failure to parse `postData.text` as a JSON object, `postData.mimeType` is set to `text/plain`, `postData.jsonObj` remains as `false`. this is done so that the implementing target, would still attempt to post the raw body as is.
317317
- This also emphasizes not to rely on `postData.mimeType` for the `Content-Type` header!
318318

319319
```js

src/targets/node/request.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = function (source, options) {
88
indent: ' '
99
}, options);
1010

11+
var includeFS = false;
1112
var code = ['var request = require("request");', null];
1213

1314
var reqOpts = {
@@ -23,16 +24,16 @@ module.exports = function (source, options) {
2324
reqOpts.headers = source.headersObj;
2425
}
2526

26-
var includeFS = false;
27-
2827
switch (source.postData.mimeType) {
2928
case 'application/x-www-form-urlencoded':
3029
reqOpts.form = source.postData.paramsObj;
3130
break;
3231

3332
case 'application/json':
34-
reqOpts.body = source.postData.jsonObj;
35-
reqOpts.json = true;
33+
if (source.postData.jsonObj) {
34+
reqOpts.body = source.postData.jsonObj;
35+
reqOpts.json = true;
36+
}
3637
break;
3738

3839
case 'multipart/form-data':
@@ -62,7 +63,9 @@ module.exports = function (source, options) {
6263
break;
6364

6465
default:
65-
reqOpts.body = source.postData.text;
66+
if (source.postData.text !== '') {
67+
reqOpts.body = source.postData.text;
68+
}
6669
}
6770

6871
// construct cookies argument
@@ -81,21 +84,17 @@ module.exports = function (source, options) {
8184
}
8285

8386
if (includeFS) {
84-
code.push('var fs = require("fs");');
87+
code.unshift('var fs = require("fs");');
8588
}
8689

87-
var content = JSON.stringify(reqOpts, null, opts.indent)
88-
.replace('"JAR"', 'jar')
89-
.replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")');
90-
91-
code.push(util.format('request(%s, %s', content, 'function (error, response, body) {'));
90+
code.push(util.format('request(%s, %s', JSON.stringify(reqOpts, null, opts.indent), 'function (error, response, body) {'));
9291
code.push(opts.indent + 'if (error) throw new Error(error);');
9392
code.push(null);
9493
code.push(opts.indent + 'console.log(body);');
9594
code.push('});');
9695
code.push(null);
9796

98-
return code.join('\n');
97+
return code.join('\n').replace('"JAR"', 'jar').replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")');
9998
};
10099

101100
module.exports.info = {

src/targets/node/unirest.js

Lines changed: 77 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,102 @@
33
var util = require('util');
44
var path = require('path');
55

6-
module.exports = function (options) {
6+
module.exports = function (source, options) {
77
var opts = util._extend({
88
indent: ' '
99
}, options);
1010

11+
var includeFS = false;
1112
var code = ['var unirest = require("unirest");', null];
12-
if(this.source.cookies.length){
13+
14+
code.push(util.format('var req = unirest("%s", "%s");', source.method, source.url));
15+
code.push(null);
16+
17+
if (source.cookies.length) {
1318
code.push('var CookieJar = unirest.jar();');
14-
var cookies = {};
15-
var url = this.source.url;
16-
this.source.cookies.forEach(function(cookie){
17-
code.push(util.format('CookieJar.add("%s=%s","%s")',encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url));
18-
})
19+
20+
source.cookies.forEach(function (cookie) {
21+
code.push(util.format('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url));
22+
});
23+
24+
code.push('req.jar(CookieJar);');
1925
code.push(null);
2026
}
21-
code.push(util.format('unirest.%s("%s")', this.source.method.toLowerCase(), this.source.url));
22-
if (Object.keys(this.source.queryObj).length) {
23-
code.push(opts.indent + util.format('.query(%s)',JSON.stringify(this.source.queryObj)))
27+
28+
if (Object.keys(source.queryObj).length) {
29+
code.push(util.format('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent)));
30+
code.push(null);
2431
}
25-
if(this.source.cookies.length){
26-
code.push(opts.indent + '.jar(CookieJar)')
32+
33+
if (Object.keys(source.headersObj).length) {
34+
code.push(util.format('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent)));
35+
code.push(null);
2736
}
28-
if (Object.keys(this.source.headersObj).length) {
29-
var self = this;
30-
var newHeadersObj = Object.keys(this.source.headersObj).reduce(function(finalHeader, headerName){
31-
if(headerName !== 'Content-Type'){
32-
finalHeader[headerName] = self.source.headersObj[headerName];
37+
38+
switch (source.postData.mimeType) {
39+
case 'application/x-www-form-urlencoded':
40+
if (source.postData.paramsObj) {
41+
code.push(util.format('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent)));
42+
}
43+
break;
44+
45+
case 'application/json':
46+
if (source.postData.jsonObj) {
47+
code.push('req.type("json");');
48+
code.push(util.format('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent)));
49+
}
50+
break;
51+
52+
case 'multipart/form-data':
53+
var multipart = [];
54+
55+
source.postData.params.forEach(function (param) {
56+
var part = {};
57+
58+
if (param.fileName && !param.value) {
59+
includeFS = true;
60+
61+
part.body = 'fs.createReadStream("' + param.fileName + '")';
62+
} else if (param.value) {
63+
part.body = param.value;
64+
}
65+
66+
if (part.body) {
67+
if (param.contentType) {
68+
part['content-type'] = param.contentType;
69+
}
70+
71+
multipart.push(part);
72+
}
73+
});
74+
75+
code.push(util.format('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent)));
76+
break;
77+
78+
default:
79+
if (source.postData.text) {
80+
code.push(opts.indent + util.format('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent)));
3381
}
34-
return finalHeader;
35-
}, {})
36-
if(Object.keys(newHeadersObj).length){
37-
code.push(opts.indent + util.format('.headers(%s)',JSON.stringify(newHeadersObj)));
38-
}
39-
}
40-
if(this.source.postData.mimeType){
41-
if(this.source.postData.mimeType !== 'application/octet-stream'){
42-
code.push(opts.indent + util.format('.type("%s")',this.source.postData.mimeType))
43-
}
44-
if(this.source.postData.mimeType === 'multipart/form-data'){
45-
// not sure how unirest handles multipart
46-
}
4782
}
48-
if(this.source.postData.paramsObj.length){
49-
code.push(opts.indent + util.format('.send(%s)', this.source.postData.paramsObj));
83+
84+
if (includeFS) {
85+
code.unshift('var fs = require("fs");');
5086
}
51-
code.push(opts.indent + '.end(function(response){')
52-
code.push(opts.indent + opts.indent + 'if (response.error) throw new Error(response.error);');
87+
88+
code.push(null);
89+
code.push('req.end(function (res) {');
90+
code.push(opts.indent + 'if (res.error) throw new Error(res.error);');
5391
code.push(null);
54-
code.push(opts.indent+opts.indent+ 'console.log(response.body);');
55-
code.push(opts.indent+'});');
92+
code.push(opts.indent + 'console.log(res.body);');
93+
code.push('});');
5694
code.push(null);
5795

58-
return code.join('\n');
96+
return code.join('\n').replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")');
5997
};
6098

6199
module.exports.info = {
62100
key: 'unirest',
63101
title: 'Unirest',
64-
link: 'https://github.com/request/request',
65-
description: 'Simplified HTTP request client.'
102+
link: 'http://unirest.io/',
103+
description: 'Lightweight HTTP Request Client Library'
66104
};

test/fixtures/available-targets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
{
4242
"key": "unirest",
4343
"title": "Unirest",
44-
"link": "https://github.com/request/request",
45-
"description": "Simplified HTTP request client"
44+
"link": "http://unirest.io/",
45+
"description": "Lightweight HTTP Request Client Library"
4646
}
4747
]
4848
},

test/fixtures/output/node/request/multipart-file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
var fs = require("fs");
12
var request = require("request");
23

3-
var fs = require("fs");
44
request({
55
"method": "POST",
66
"url": "http://mockbin.com/har",
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
var unirest = require("unirest");
22

3-
unirest.post("http://mockbin.com/har")
4-
.type("application/x-www-form-urlencoded")
5-
.end(function(response){
6-
if (response.error) throw new Error(response.error);
3+
var req = unirest("POST", "http://mockbin.com/har");
74

8-
console.log(response.body);
9-
});
5+
req.headers({
6+
"content-type": "application/x-www-form-urlencoded"
7+
});
8+
9+
req.form({
10+
"foo": "bar",
11+
"hello": "world"
12+
});
13+
14+
req.end(function (res) {
15+
if (res.error) throw new Error(res.error);
16+
17+
console.log(res.body);
18+
});
1019

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
var unirest = require("unirest");
22

3-
unirest.post("http://mockbin.com/har")
4-
.type("application/json")
5-
.end(function(response){
6-
if (response.error) throw new Error(response.error);
3+
var req = unirest("POST", "http://mockbin.com/har");
74

8-
console.log(response.body);
9-
});
5+
req.headers({
6+
"content-type": "application/json"
7+
});
8+
9+
req.type("json");
10+
req.send({
11+
"foo": "bar"
12+
});
13+
14+
req.end(function (res) {
15+
if (res.error) throw new Error(res.error);
16+
17+
console.log(res.body);
18+
});
1019

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
var unirest = require("unirest");
22

3+
var req = unirest("POST", "http://mockbin.com/har");
4+
35
var CookieJar = unirest.jar();
4-
CookieJar.add("foo=bar","http://mockbin.com/har")
5-
CookieJar.add("bar=baz","http://mockbin.com/har")
6+
CookieJar.add("foo=bar","http://mockbin.com/har");
7+
CookieJar.add("bar=baz","http://mockbin.com/har");
8+
req.jar(CookieJar);
9+
610

7-
unirest.post("http://mockbin.com/har")
8-
.jar(CookieJar)
9-
.end(function(response){
10-
if (response.error) throw new Error(response.error);
11+
req.end(function (res) {
12+
if (res.error) throw new Error(res.error);
1113

12-
console.log(response.body);
13-
});
14+
console.log(res.body);
15+
});
1416

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
var unirest = require("unirest");
22

3+
var req = unirest("POST", "http://mockbin.com/har");
4+
35
var CookieJar = unirest.jar();
4-
CookieJar.add("foo=bar","http://mockbin.com/har")
5-
CookieJar.add("bar=baz","http://mockbin.com/har")
6-
7-
unirest.post("http://mockbin.com/har")
8-
.query({"baz":"abc","foo":["bar","baz"]})
9-
.jar(CookieJar)
10-
.headers({"Accept":"application/json"})
11-
.type("application/x-www-form-urlencoded")
12-
.end(function(response){
13-
if (response.error) throw new Error(response.error);
14-
15-
console.log(response.body);
16-
});
6+
CookieJar.add("foo=bar","http://mockbin.com/har");
7+
CookieJar.add("bar=baz","http://mockbin.com/har");
8+
req.jar(CookieJar);
9+
10+
req.query({
11+
"foo": [
12+
"bar",
13+
"baz"
14+
],
15+
"baz": "abc",
16+
"key": "value"
17+
});
18+
19+
req.headers({
20+
"content-type": "application/x-www-form-urlencoded",
21+
"accept": "application/json"
22+
});
23+
24+
req.form({
25+
"foo": "bar"
26+
});
27+
28+
req.end(function (res) {
29+
if (res.error) throw new Error(res.error);
30+
31+
console.log(res.body);
32+
});
1733

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
var unirest = require("unirest");
22

3-
unirest.get("http://mockbin.com/har")
4-
.headers({"Accept":"application/json","X-Foo":"Bar"})
5-
.end(function(response){
6-
if (response.error) throw new Error(response.error);
3+
var req = unirest("GET", "http://mockbin.com/har");
74

8-
console.log(response.body);
9-
});
5+
req.headers({
6+
"x-foo": "Bar",
7+
"accept": "application/json"
8+
});
9+
10+
11+
req.end(function (res) {
12+
if (res.error) throw new Error(res.error);
13+
14+
console.log(res.body);
15+
});
1016

0 commit comments

Comments
 (0)