Skip to content

Commit 1b53a16

Browse files
committed
with default values, code is now cleaner + changed order of cli param output
1 parent 5f23049 commit 1b53a16

6 files changed

Lines changed: 57 additions & 65 deletions

File tree

‎src/targets/curl.js‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,27 @@ module.exports = function (options) {
1818
code.push(opts.short ? '-0' : '--http1.0');
1919
}
2020

21+
// construct headers
22+
this.source.headers.map(function (header) {
23+
code.push(util.format('%s "%s: %s"', opts.short ? '-H' : '--header', header.name, header.value));
24+
});
25+
2126
// construct cookies
22-
if (this.source.cookies && this.source.cookies.length) {
23-
var cookies = this.source.cookies.map(function (cookie) {
24-
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
25-
});
27+
var cookies = this.source.cookies.map(function (cookie) {
28+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
29+
});
2630

31+
if (cookies.length) {
2732
code.push(util.format('%s "%s"', opts.short ? '-b' : '--cookie', cookies.join('; ')));
2833
}
2934

30-
// construct headers
31-
if (this.source.headers && this.source.headers.length) {
32-
this.source.headers.map(function (header) {
33-
code.push(util.format('%s "%s: %s"', opts.short ? '-H' : '--header', header.name, header.value));
34-
});
35-
}
36-
3735
// request body
38-
if (this.source.postData && this.source.postData.text) {
36+
if (this.source.postData.text) {
3937
code.push(util.format('%s %s', opts.short ? '-F' : '--form', JSON.stringify(this.source.postData.text)));
4038
}
4139

4240
// construct post params
43-
if (this.source.postData && !this.source.postData.text && this.source.postData.params && this.source.postData.params.length) {
41+
if (!this.source.postData.text && this.source.postData.params && this.source.postData.params.length) {
4442
this.source.postData.params.map(function (param) {
4543
code.push(util.format('%s "%s=%s"', opts.short ? '-d' : '--data', param.name, param.value));
4644
});

‎src/targets/httpie.js‎

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,47 +68,43 @@ module.exports = function (options) {
6868
var self = this;
6969

7070
// construct query params
71-
if (opts.queryParams && this.source.queryString) {
71+
if (opts.queryParams) {
7272
var queryStringKeys = Object.keys(this.source.queryString);
7373

74-
if (queryStringKeys.length) {
75-
queryStringKeys.map(function (name) {
76-
var value = self.source.queryString[name];
77-
78-
if (util.isArray(value)) {
79-
value.map(function (val) {
80-
code.push(util.format('%s==%s', name, val));
81-
});
82-
} else {
83-
code.push(util.format('%s==%s', name, value));
84-
}
85-
});
86-
}
87-
}
74+
queryStringKeys.map(function (name) {
75+
var value = self.source.queryString[name];
8876

89-
// construct post params
90-
if (this.source.postData && !this.source.postData.text && this.source.postData.params && this.source.postData.params.length) {
91-
this.source.postData.params.map(function (param) {
92-
code.push(util.format('%s:%s', param.name, param.value));
77+
if (util.isArray(value)) {
78+
value.map(function (val) {
79+
code.push(util.format('%s==%s', name, val));
80+
});
81+
} else {
82+
code.push(util.format('%s==%s', name, value));
83+
}
9384
});
9485
}
9586

9687
// construct headers
97-
if (this.source.headers && this.source.headers.length) {
98-
this.source.headers.map(function (header) {
99-
code.push(util.format('%s:%s', header.name, header.value));
100-
});
101-
}
88+
this.source.headers.map(function (header) {
89+
code.push(util.format('%s:%s', header.name, header.value));
90+
});
10291

10392
// construct cookies argument
104-
if (this.source.cookies && this.source.cookies.length) {
105-
var cookies = this.source.cookies.map(function (cookie) {
106-
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
107-
});
93+
var cookies = this.source.cookies.map(function (cookie) {
94+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
95+
});
10896

97+
if (cookies.length) {
10998
code.push(util.format('Cookie:%s', cookies.join('; ')));
11099
}
111100

101+
// construct post params
102+
if (!this.source.postData.text && this.source.postData.params && this.source.postData.params.length) {
103+
this.source.postData.params.map(function (param) {
104+
code.push(util.format('%s:%s', param.name, param.value));
105+
});
106+
}
107+
112108
return code.join(opts.indent !== false ? ' \\\n' + opts.indent : ' ');
113109
};
114110

‎src/targets/node/native.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ module.exports = function (options) {
1818
};
1919

2020
// construct cookies argument
21-
if (this.source.cookies && this.source.cookies.length) {
22-
var cookies = this.source.cookies.map(function (cookie) {
23-
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
24-
});
21+
var cookies = this.source.cookies.map(function (cookie) {
22+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
23+
});
2524

25+
if (cookies.length) {
2626
reqOpts.headers.Cookie = cookies.join('; ');
2727
}
2828

‎src/targets/php/curl.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,20 @@ module.exports = function (options) {
7070
});
7171

7272
// construct cookies
73-
if (this.source.cookies && this.source.cookies.length) {
74-
var cookies = this.source.cookies.map(function (cookie) {
75-
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
76-
});
73+
var cookies = this.source.cookies.map(function (cookie) {
74+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
75+
});
7776

77+
if (cookies.length) {
7878
curlopts.push(util.format('CURLOPT_COOKIE => "%s",', cookies.join('; ')));
7979
}
8080

8181
// construct cookies
82-
if (this.source.headers && this.source.headers.length) {
83-
var headers = this.source.headers.map(function (header) {
84-
return util.format('"%s: %s"', header.name, header.value);
85-
});
82+
var headers = this.source.headers.map(function (header) {
83+
return util.format('"%s: %s"', header.name, header.value);
84+
});
8685

86+
if (headers.length) {
8787
curlopts.push(util.format('CURLOPT_HTTPHEADER => array(\n%s%s%s\n%s),', opts.indent, opts.indent, headers.join(',\n' + opts.indent + opts.indent), opts.indent));
8888
}
8989

‎src/targets/wget.js‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@ module.exports = function (options) {
2020
code.push(util.format('--method %s', this.source.method));
2121

2222
// construct cookies argument
23-
if (this.source.cookies && this.source.cookies.length) {
24-
var cookies = this.source.cookies.map(function (cookie) {
25-
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
26-
});
23+
var cookies = this.source.cookies.map(function (cookie) {
24+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
25+
});
2726

27+
if (cookies.length) {
2828
code.push(util.format('--header "Cookie: %s"', cookies.join('; ')));
2929
}
3030

31-
if (this.source.headers && this.source.headers.length) {
32-
this.source.headers.map(function (header) {
33-
code.push(util.format('--header "%s: %s"', header.name, header.value));
34-
});
35-
}
31+
this.source.headers.map(function (header) {
32+
code.push(util.format('--header "%s: %s"', header.name, header.value));
33+
});
3634

3735
if (this.source.postData) {
3836
code.push('--body-data ' + JSON.stringify(this.source.postData.text));

‎test/targets/curl.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('cURL', function () {
1111
});
1212

1313
result.should.be.a.String;
14-
result.should.eql('curl --request POST --url "http://httpconsole.com/debug?foo=bar" --cookie "bar=baz" --header "Content-Type: application/json" --form "{\\"foo\\": \\"bar\\"}"');
14+
result.should.eql('curl --request POST --url "http://httpconsole.com/debug?foo=bar" --header "Content-Type: application/json" --cookie "bar=baz" --form "{\\"foo\\": \\"bar\\"}"');
1515

1616
done();
1717
});
@@ -23,7 +23,7 @@ describe('cURL', function () {
2323
});
2424

2525
result.should.be.a.String;
26-
result.should.eql('curl -X POST "http://httpconsole.com/debug?foo=bar" -b "bar=baz" -H "Content-Type: application/json" -F "{\\"foo\\": \\"bar\\"}"');
26+
result.should.eql('curl -X POST "http://httpconsole.com/debug?foo=bar" -H "Content-Type: application/json" -b "bar=baz" -F "{\\"foo\\": \\"bar\\"}"');
2727

2828
done();
2929
});
@@ -45,7 +45,7 @@ describe('cURL', function () {
4545
});
4646

4747
result.should.be.a.String;
48-
result.replace(/\\\n/g, '').should.eql('curl --request POST @--url "http://httpconsole.com/debug?foo=bar" @--cookie "bar=baz" @--header "Content-Type: application/json" @--form "{\\"foo\\": \\"bar\\"}"');
48+
result.replace(/\\\n/g, '').should.eql('curl --request POST @--url "http://httpconsole.com/debug?foo=bar" @--header "Content-Type: application/json" @--cookie "bar=baz" @--form "{\\"foo\\": \\"bar\\"}"');
4949

5050
done();
5151
});

0 commit comments

Comments
 (0)