@readme/httpsnippet 2.4.5 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -9
- package/package.json +24 -27
- package/src/helpers/code-builder.js +85 -80
- package/src/helpers/form-data.js +28 -26
- package/src/helpers/headers.js +18 -8
- package/src/helpers/reducer.js +10 -14
- package/src/index.js +175 -151
- package/src/targets/c/index.js +3 -5
- package/src/targets/c/libcurl.js +20 -25
- package/src/targets/clojure/clj_http.js +126 -102
- package/src/targets/clojure/index.js +3 -5
- package/src/targets/csharp/httpclient.js +93 -90
- package/src/targets/csharp/index.js +3 -5
- package/src/targets/csharp/restsharp.js +20 -22
- package/src/targets/go/index.js +3 -5
- package/src/targets/go/native.js +47 -54
- package/src/targets/http/http1.1.js +27 -37
- package/src/targets/http/index.js +3 -5
- package/src/targets/index.js +2 -4
- package/src/targets/java/asynchttp.js +22 -23
- package/src/targets/java/index.js +3 -5
- package/src/targets/java/nethttp.js +20 -24
- package/src/targets/java/okhttp.js +31 -31
- package/src/targets/java/unirest.js +22 -19
- package/src/targets/javascript/axios.js +47 -42
- package/src/targets/javascript/fetch.js +66 -68
- package/src/targets/javascript/index.js +3 -5
- package/src/targets/javascript/jquery.js +50 -44
- package/src/targets/javascript/xhr.js +48 -44
- package/src/targets/kotlin/index.js +3 -5
- package/src/targets/kotlin/okhttp.js +31 -31
- package/src/targets/node/axios.js +37 -34
- package/src/targets/node/fetch.js +94 -87
- package/src/targets/node/index.js +4 -5
- package/src/targets/node/native.js +51 -45
- package/src/targets/node/request.js +69 -69
- package/src/targets/node/unirest.js +70 -68
- package/src/targets/objc/helpers.js +32 -25
- package/src/targets/objc/index.js +3 -5
- package/src/targets/objc/nsurlsession.js +108 -79
- package/src/targets/ocaml/cohttp.js +32 -30
- package/src/targets/ocaml/index.js +3 -5
- package/src/targets/php/curl.js +86 -82
- package/src/targets/php/guzzle.js +135 -0
- package/src/targets/php/helpers.js +31 -29
- package/src/targets/php/http1.js +38 -49
- package/src/targets/php/http2.js +75 -75
- package/src/targets/php/index.js +5 -5
- package/src/targets/powershell/common.js +35 -30
- package/src/targets/powershell/index.js +3 -5
- package/src/targets/powershell/restmethod.js +3 -5
- package/src/targets/powershell/webrequest.js +3 -5
- package/src/targets/python/helpers.js +42 -37
- package/src/targets/python/index.js +4 -5
- package/src/targets/python/python3.js +33 -45
- package/src/targets/python/requests.js +48 -72
- package/src/targets/r/httr.js +60 -73
- package/src/targets/r/index.js +3 -5
- package/src/targets/ruby/index.js +3 -5
- package/src/targets/ruby/native.js +41 -35
- package/src/targets/shell/curl.js +108 -43
- package/src/{helpers/shell.js → targets/shell/helpers.js} +7 -9
- package/src/targets/shell/httpie.js +45 -39
- package/src/targets/shell/index.js +4 -5
- package/src/targets/shell/wget.js +20 -22
- package/src/targets/swift/helpers.js +44 -35
- package/src/targets/swift/index.js +3 -5
- package/src/targets/swift/nsurlsession.js +93 -79
- package/src/.DS_Store +0 -0
|
@@ -8,119 +8,119 @@
|
|
|
8
8
|
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var util = require('util')
|
|
14
|
-
var stringifyObject = require('stringify-object')
|
|
15
|
-
var CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const stringifyObject = require('stringify-object');
|
|
12
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
16
13
|
|
|
17
14
|
module.exports = function (source, options) {
|
|
18
|
-
|
|
19
|
-
indent: ' '
|
|
20
|
-
|
|
15
|
+
const opts = {
|
|
16
|
+
indent: ' ',
|
|
17
|
+
...options,
|
|
18
|
+
};
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
let includeFS = false;
|
|
21
|
+
const code = new CodeBuilder(opts.indent);
|
|
24
22
|
|
|
25
|
-
code.push("const request = require('request');")
|
|
26
|
-
.blank()
|
|
23
|
+
code.push("const request = require('request');").blank();
|
|
27
24
|
|
|
28
|
-
|
|
25
|
+
const reqOpts = {
|
|
29
26
|
method: source.method,
|
|
30
|
-
url: source.
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (Object.keys(source.queryObj).length) {
|
|
34
|
-
reqOpts.qs = source.queryObj
|
|
35
|
-
}
|
|
27
|
+
url: source.fullUrl,
|
|
28
|
+
};
|
|
36
29
|
|
|
37
30
|
if (Object.keys(source.headersObj).length) {
|
|
38
|
-
reqOpts.headers = source.headersObj
|
|
31
|
+
reqOpts.headers = source.headersObj;
|
|
39
32
|
}
|
|
40
33
|
|
|
41
34
|
switch (source.postData.mimeType) {
|
|
42
35
|
case 'application/x-www-form-urlencoded':
|
|
43
|
-
reqOpts.form = source.postData.paramsObj
|
|
44
|
-
break
|
|
36
|
+
reqOpts.form = source.postData.paramsObj;
|
|
37
|
+
break;
|
|
45
38
|
|
|
46
39
|
case 'application/json':
|
|
47
40
|
if (source.postData.jsonObj) {
|
|
48
|
-
reqOpts.body = source.postData.jsonObj
|
|
49
|
-
reqOpts.json = true
|
|
41
|
+
reqOpts.body = source.postData.jsonObj;
|
|
42
|
+
reqOpts.json = true;
|
|
50
43
|
}
|
|
51
|
-
break
|
|
44
|
+
break;
|
|
52
45
|
|
|
53
46
|
case 'multipart/form-data':
|
|
54
|
-
|
|
47
|
+
if (source.postData.params) {
|
|
48
|
+
reqOpts.formData = {};
|
|
55
49
|
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
source.postData.params.forEach(function (param) {
|
|
51
|
+
const attachment = {};
|
|
58
52
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
53
|
+
if (!param.fileName && !param.contentType) {
|
|
54
|
+
reqOpts.formData[param.name] = param.value;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
63
57
|
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
if (param.fileName) {
|
|
59
|
+
includeFS = true;
|
|
66
60
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
61
|
+
attachment.value = `fs.createReadStream("${param.fileName}")`;
|
|
62
|
+
} else if (param.value) {
|
|
63
|
+
attachment.value = param.value;
|
|
64
|
+
}
|
|
71
65
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
if (param.fileName) {
|
|
67
|
+
attachment.options = {
|
|
68
|
+
filename: param.fileName,
|
|
69
|
+
contentType: param.contentType ? param.contentType : null,
|
|
70
|
+
};
|
|
76
71
|
}
|
|
77
|
-
}
|
|
78
72
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
73
|
+
reqOpts.formData[param.name] = attachment;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
82
77
|
|
|
83
78
|
default:
|
|
84
79
|
if (source.postData.text) {
|
|
85
|
-
reqOpts.body = source.postData.text
|
|
80
|
+
reqOpts.body = source.postData.text;
|
|
86
81
|
}
|
|
87
82
|
}
|
|
88
83
|
|
|
89
84
|
// construct cookies argument
|
|
90
|
-
if (source.
|
|
91
|
-
reqOpts.jar = 'JAR'
|
|
85
|
+
if (source.allHeaders.cookie) {
|
|
86
|
+
reqOpts.jar = 'JAR';
|
|
92
87
|
|
|
93
|
-
code.push('const jar = request.jar();')
|
|
88
|
+
code.push('const jar = request.jar();');
|
|
94
89
|
|
|
95
|
-
|
|
90
|
+
const url = source.url;
|
|
96
91
|
|
|
97
|
-
source.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
92
|
+
// Cookies are already encoded within `source.allHeaders` so we can pull them out of that instead of doing our
|
|
93
|
+
// own encoding work.
|
|
94
|
+
source.allHeaders.cookie.split('; ').forEach(function (cookie) {
|
|
95
|
+
const [name, value] = cookie.split('=');
|
|
96
|
+
code.push("jar.setCookie(request.cookie('%s=%s'), '%s');", name, value, url);
|
|
97
|
+
});
|
|
98
|
+
code.blank();
|
|
101
99
|
}
|
|
102
100
|
|
|
103
101
|
if (includeFS) {
|
|
104
|
-
code.unshift("const fs = require('fs');")
|
|
102
|
+
code.unshift("const fs = require('fs');");
|
|
105
103
|
}
|
|
106
104
|
|
|
107
|
-
code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
|
|
108
|
-
.blank()
|
|
105
|
+
code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 })).blank();
|
|
109
106
|
|
|
110
|
-
code
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
107
|
+
code
|
|
108
|
+
.push('request(options, function (error, response, body) {')
|
|
109
|
+
.push(1, 'if (error) throw new Error(error);')
|
|
110
|
+
.blank()
|
|
111
|
+
.push(1, 'console.log(body);')
|
|
112
|
+
.push('});')
|
|
113
|
+
.blank();
|
|
117
114
|
|
|
118
|
-
return code
|
|
119
|
-
|
|
115
|
+
return code
|
|
116
|
+
.join()
|
|
117
|
+
.replace("'JAR'", 'jar')
|
|
118
|
+
.replace(/'fs\.createReadStream\("(.+)"\)'/g, "fs.createReadStream('$1')");
|
|
119
|
+
};
|
|
120
120
|
|
|
121
121
|
module.exports.info = {
|
|
122
122
|
key: 'request',
|
|
123
123
|
title: 'Request',
|
|
124
124
|
link: 'https://github.com/request/request',
|
|
125
|
-
description: 'Simplified HTTP request client'
|
|
126
|
-
}
|
|
125
|
+
description: 'Simplified HTTP request client',
|
|
126
|
+
};
|
|
@@ -8,111 +8,113 @@
|
|
|
8
8
|
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
14
12
|
|
|
15
13
|
module.exports = function (source, options) {
|
|
16
|
-
|
|
17
|
-
indent: ' '
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
.
|
|
14
|
+
const opts = {
|
|
15
|
+
indent: ' ',
|
|
16
|
+
...options,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
let includeFS = false;
|
|
20
|
+
const code = new CodeBuilder(opts.indent);
|
|
21
|
+
|
|
22
|
+
code
|
|
23
|
+
.push('const unirest = require("unirest");')
|
|
24
|
+
.blank()
|
|
25
|
+
.push('const req = unirest("%s", "%s");', source.method, source.url)
|
|
26
|
+
.blank();
|
|
27
|
+
|
|
28
|
+
if (source.allHeaders.cookie) {
|
|
29
|
+
code.push('const CookieJar = unirest.jar();');
|
|
30
|
+
|
|
31
|
+
// Cookies are already encoded within `source.allHeaders` so we can pull them out of that instead of doing our
|
|
32
|
+
// own encoding work.
|
|
33
|
+
source.allHeaders.cookie.split('; ').forEach(function (cookie) {
|
|
34
|
+
const [name, value] = cookie.split('=');
|
|
35
|
+
code.push('CookieJar.add("%s=%s","%s");', name, value, source.url);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
code.push('req.jar(CookieJar);').blank();
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
if (Object.keys(source.queryObj).length) {
|
|
40
|
-
code.push('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent))
|
|
41
|
-
.blank()
|
|
42
|
+
code.push('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent)).blank();
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
if (Object.keys(source.headersObj).length) {
|
|
45
|
-
code.push('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent))
|
|
46
|
-
.blank()
|
|
46
|
+
code.push('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent)).blank();
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
switch (source.postData.mimeType) {
|
|
50
50
|
case 'application/x-www-form-urlencoded':
|
|
51
51
|
if (source.postData.paramsObj) {
|
|
52
|
-
code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent))
|
|
53
|
-
.blank()
|
|
52
|
+
code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent)).blank();
|
|
54
53
|
}
|
|
55
|
-
break
|
|
54
|
+
break;
|
|
56
55
|
|
|
57
56
|
case 'application/json':
|
|
58
57
|
if (source.postData.jsonObj) {
|
|
59
|
-
code
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
code
|
|
59
|
+
.push('req.type("json");')
|
|
60
|
+
.push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
|
|
61
|
+
.blank();
|
|
62
62
|
}
|
|
63
|
-
break
|
|
64
|
-
|
|
65
|
-
case 'multipart/form-data':
|
|
66
|
-
var multipart = []
|
|
63
|
+
break;
|
|
67
64
|
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
case 'multipart/form-data': {
|
|
66
|
+
if (source.postData.params) {
|
|
67
|
+
const multipart = [];
|
|
70
68
|
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
source.postData.params.forEach(function (param) {
|
|
70
|
+
const part = {};
|
|
73
71
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
part.body = param.value
|
|
77
|
-
}
|
|
72
|
+
if (param.fileName && !param.value) {
|
|
73
|
+
includeFS = true;
|
|
78
74
|
|
|
79
|
-
|
|
80
|
-
if (param.
|
|
81
|
-
part
|
|
75
|
+
part.body = `fs.createReadStream("${param.fileName}")`;
|
|
76
|
+
} else if (param.value) {
|
|
77
|
+
part.body = param.value;
|
|
82
78
|
}
|
|
83
79
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
80
|
+
if (part.body) {
|
|
81
|
+
if (param.contentType) {
|
|
82
|
+
part['content-type'] = param.contentType;
|
|
83
|
+
}
|
|
87
84
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
85
|
+
multipart.push(part);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
code.push('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent)).blank();
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
91
93
|
|
|
92
94
|
default:
|
|
93
95
|
if (source.postData.text) {
|
|
94
|
-
code.push('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))
|
|
95
|
-
.blank()
|
|
96
|
+
code.push('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent)).blank();
|
|
96
97
|
}
|
|
97
98
|
}
|
|
98
99
|
|
|
99
100
|
if (includeFS) {
|
|
100
|
-
code.unshift('const fs = require("fs");')
|
|
101
|
+
code.unshift('const fs = require("fs");');
|
|
101
102
|
}
|
|
102
103
|
|
|
103
|
-
code
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
104
|
+
code
|
|
105
|
+
.push('req.end(function (res) {')
|
|
106
|
+
.push(1, 'if (res.error) throw new Error(res.error);')
|
|
107
|
+
.blank()
|
|
108
|
+
.push(1, 'console.log(res.body);')
|
|
109
|
+
.push('});')
|
|
110
|
+
.blank();
|
|
109
111
|
|
|
110
|
-
return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")')
|
|
111
|
-
}
|
|
112
|
+
return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
|
|
113
|
+
};
|
|
112
114
|
|
|
113
115
|
module.exports.info = {
|
|
114
116
|
key: 'unirest',
|
|
115
117
|
title: 'Unirest',
|
|
116
118
|
link: 'http://unirest.io/nodejs.html',
|
|
117
|
-
description: 'Lightweight HTTP Request Client Library'
|
|
118
|
-
}
|
|
119
|
+
description: 'Lightweight HTTP Request Client Library',
|
|
120
|
+
};
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var util = require('util')
|
|
1
|
+
const { format } = require('util');
|
|
4
2
|
|
|
5
3
|
module.exports = {
|
|
6
4
|
/**
|
|
@@ -10,7 +8,8 @@ module.exports = {
|
|
|
10
8
|
* @return {string}
|
|
11
9
|
*/
|
|
12
10
|
blankString: function (length) {
|
|
13
|
-
|
|
11
|
+
// eslint-disable-next-line prefer-spread
|
|
12
|
+
return Array.apply(null, new Array(length)).map(String.prototype.valueOf, ' ').join('');
|
|
14
13
|
},
|
|
15
14
|
|
|
16
15
|
/**
|
|
@@ -33,9 +32,9 @@ module.exports = {
|
|
|
33
32
|
* NSDictionary *params = @{ @"a": @"b", @"c": @"d" };
|
|
34
33
|
*/
|
|
35
34
|
nsDeclaration: function (nsClass, name, parameters, indent) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return opening + literal
|
|
35
|
+
const opening = `${nsClass} *${name} = `;
|
|
36
|
+
const literal = this.literalRepresentation(parameters, indent ? opening.length : undefined);
|
|
37
|
+
return `${opening + literal};`;
|
|
39
38
|
},
|
|
40
39
|
|
|
41
40
|
/**
|
|
@@ -45,29 +44,37 @@ module.exports = {
|
|
|
45
44
|
* @return {string}
|
|
46
45
|
*/
|
|
47
46
|
literalRepresentation: function (value, indentation) {
|
|
48
|
-
|
|
47
|
+
const join = indentation === undefined ? ', ' : `,\n ${this.blankString(indentation)}`;
|
|
49
48
|
|
|
50
49
|
switch (Object.prototype.toString.call(value)) {
|
|
51
50
|
case '[object Number]':
|
|
52
|
-
return
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
51
|
+
return `@${value}`;
|
|
52
|
+
|
|
53
|
+
case '[object Array]': {
|
|
54
|
+
const valuesRepresentation = value.map(
|
|
55
|
+
function (v) {
|
|
56
|
+
return this.literalRepresentation(v);
|
|
57
|
+
}.bind(this)
|
|
58
|
+
);
|
|
59
|
+
return `@[ ${valuesRepresentation.join(join)} ]`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
case '[object Object]': {
|
|
63
|
+
const keyValuePairs = [];
|
|
64
|
+
Object.keys(value).forEach(k => {
|
|
65
|
+
keyValuePairs.push(format('@"%s": %s', k, this.literalRepresentation(value[k])));
|
|
66
|
+
});
|
|
67
|
+
return `@{ ${keyValuePairs.join(join)} }`;
|
|
68
|
+
}
|
|
69
|
+
|
|
64
70
|
case '[object Boolean]':
|
|
65
|
-
return value ? '@YES' : '@NO'
|
|
71
|
+
return value ? '@YES' : '@NO';
|
|
72
|
+
|
|
66
73
|
default:
|
|
67
74
|
if (value === null || value === undefined) {
|
|
68
|
-
return ''
|
|
75
|
+
return '';
|
|
69
76
|
}
|
|
70
|
-
return
|
|
77
|
+
return `@"${value.toString().replace(/"/g, '\\"')}"`;
|
|
71
78
|
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
module.exports = {
|
|
4
2
|
info: {
|
|
5
3
|
key: 'objc',
|
|
6
4
|
title: 'Objective-C',
|
|
7
5
|
extname: '.m',
|
|
8
|
-
default: 'nsurlsession'
|
|
6
|
+
default: 'nsurlsession',
|
|
9
7
|
},
|
|
10
8
|
|
|
11
|
-
nsurlsession: require('./nsurlsession')
|
|
12
|
-
}
|
|
9
|
+
nsurlsession: require('./nsurlsession'),
|
|
10
|
+
};
|