@readme/httpsnippet 2.5.0 → 3.0.2
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 -28
- package/src/helpers/code-builder.js +85 -80
- package/src/helpers/form-data.js +27 -25
- package/src/helpers/headers.js +10 -19
- package/src/helpers/reducer.js +10 -14
- package/src/index.js +167 -146
- package/src/targets/c/index.js +3 -5
- package/src/targets/c/libcurl.js +18 -23
- package/src/targets/clojure/clj_http.js +103 -87
- package/src/targets/clojure/index.js +3 -5
- package/src/targets/csharp/httpclient.js +90 -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 +46 -53
- package/src/targets/http/http1.1.js +26 -36
- 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 +43 -38
- package/src/targets/javascript/fetch.js +66 -68
- package/src/targets/javascript/index.js +3 -5
- package/src/targets/javascript/jquery.js +46 -40
- package/src/targets/javascript/xhr.js +42 -38
- package/src/targets/kotlin/index.js +3 -5
- package/src/targets/kotlin/okhttp.js +31 -31
- package/src/targets/node/axios.js +33 -30
- package/src/targets/node/fetch.js +89 -83
- package/src/targets/node/index.js +4 -5
- package/src/targets/node/native.js +36 -30
- package/src/targets/node/request.js +64 -64
- package/src/targets/node/unirest.js +56 -55
- package/src/targets/objc/helpers.js +24 -23
- package/src/targets/objc/index.js +3 -5
- package/src/targets/objc/nsurlsession.js +73 -45
- package/src/targets/ocaml/cohttp.js +29 -27
- 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 +29 -28
- package/src/targets/php/http1.js +33 -44
- package/src/targets/php/http2.js +70 -71
- 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 +37 -34
- package/src/targets/python/index.js +4 -5
- package/src/targets/python/python3.js +33 -44
- package/src/targets/python/requests.js +47 -72
- package/src/targets/r/httr.js +59 -70
- package/src/targets/r/index.js +3 -5
- package/src/targets/ruby/index.js +3 -5
- package/src/targets/ruby/native.js +39 -33
- package/src/targets/shell/curl.js +78 -51
- package/src/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 +35 -32
- package/src/targets/swift/index.js +3 -5
- package/src/targets/swift/nsurlsession.js +53 -39
- package/src/.DS_Store +0 -0
|
@@ -8,71 +8,71 @@
|
|
|
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
|
-
const CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
14
12
|
|
|
15
13
|
module.exports = function (source, options) {
|
|
16
|
-
const opts =
|
|
17
|
-
indent: ' '
|
|
18
|
-
|
|
14
|
+
const opts = {
|
|
15
|
+
indent: ' ',
|
|
16
|
+
...options,
|
|
17
|
+
};
|
|
19
18
|
|
|
20
|
-
const code = new CodeBuilder(opts.indent)
|
|
19
|
+
const code = new CodeBuilder(opts.indent);
|
|
21
20
|
|
|
22
|
-
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD']
|
|
21
|
+
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
|
|
23
22
|
|
|
24
|
-
const methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH']
|
|
23
|
+
const methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH'];
|
|
25
24
|
|
|
26
|
-
code.push('val client = OkHttpClient()')
|
|
27
|
-
.blank()
|
|
25
|
+
code.push('val client = OkHttpClient()').blank();
|
|
28
26
|
|
|
29
27
|
if (source.postData.text) {
|
|
30
28
|
if (source.postData.boundary) {
|
|
31
|
-
code.push(
|
|
29
|
+
code.push(
|
|
30
|
+
'val mediaType = MediaType.parse("%s; boundary=%s")',
|
|
31
|
+
source.postData.mimeType,
|
|
32
|
+
source.postData.boundary
|
|
33
|
+
);
|
|
32
34
|
} else {
|
|
33
|
-
code.push('val mediaType = MediaType.parse("%s")', source.postData.mimeType)
|
|
35
|
+
code.push('val mediaType = MediaType.parse("%s")', source.postData.mimeType);
|
|
34
36
|
}
|
|
35
|
-
code.push('val body = RequestBody.create(mediaType, %s)', JSON.stringify(source.postData.text))
|
|
37
|
+
code.push('val body = RequestBody.create(mediaType, %s)', JSON.stringify(source.postData.text));
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
code.push('val request = Request.Builder()')
|
|
39
|
-
code.push(1, '.url("%s")', source.fullUrl)
|
|
40
|
+
code.push('val request = Request.Builder()');
|
|
41
|
+
code.push(1, '.url("%s")', source.fullUrl);
|
|
40
42
|
if (methods.indexOf(source.method.toUpperCase()) === -1) {
|
|
41
43
|
if (source.postData.text) {
|
|
42
|
-
code.push(1, '.method("%s", body)', source.method.toUpperCase())
|
|
44
|
+
code.push(1, '.method("%s", body)', source.method.toUpperCase());
|
|
43
45
|
} else {
|
|
44
|
-
code.push(1, '.method("%s", null)', source.method.toUpperCase())
|
|
46
|
+
code.push(1, '.method("%s", null)', source.method.toUpperCase());
|
|
45
47
|
}
|
|
46
48
|
} else if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) {
|
|
47
49
|
if (source.postData.text) {
|
|
48
|
-
code.push(1, '.%s(body)', source.method.toLowerCase())
|
|
50
|
+
code.push(1, '.%s(body)', source.method.toLowerCase());
|
|
49
51
|
} else {
|
|
50
|
-
code.push(1, '.%s(null)', source.method.toLowerCase())
|
|
52
|
+
code.push(1, '.%s(null)', source.method.toLowerCase());
|
|
51
53
|
}
|
|
52
54
|
} else {
|
|
53
|
-
code.push(1, '.%s()', source.method.toLowerCase())
|
|
55
|
+
code.push(1, '.%s()', source.method.toLowerCase());
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
// Add headers, including the cookies
|
|
57
|
-
const headers = Object.keys(source.allHeaders)
|
|
59
|
+
const headers = Object.keys(source.allHeaders);
|
|
58
60
|
|
|
59
61
|
// construct headers
|
|
60
62
|
if (headers.length) {
|
|
61
63
|
headers.forEach(function (key) {
|
|
62
|
-
code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key])
|
|
63
|
-
})
|
|
64
|
+
code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key]);
|
|
65
|
+
});
|
|
64
66
|
}
|
|
65
67
|
|
|
66
|
-
code.push(1, '.build()')
|
|
67
|
-
.blank()
|
|
68
|
-
.push('val response = client.newCall(request).execute()')
|
|
68
|
+
code.push(1, '.build()').blank().push('val response = client.newCall(request).execute()');
|
|
69
69
|
|
|
70
|
-
return code.join()
|
|
71
|
-
}
|
|
70
|
+
return code.join();
|
|
71
|
+
};
|
|
72
72
|
|
|
73
73
|
module.exports.info = {
|
|
74
74
|
key: 'okhttp',
|
|
75
75
|
title: 'OkHttp',
|
|
76
76
|
link: 'http://square.github.io/okhttp/',
|
|
77
|
-
description: 'An HTTP Request Client Library'
|
|
78
|
-
}
|
|
77
|
+
description: 'An HTTP Request Client Library',
|
|
78
|
+
};
|
|
@@ -7,67 +7,70 @@
|
|
|
7
7
|
*
|
|
8
8
|
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
9
|
*/
|
|
10
|
-
'use strict'
|
|
11
10
|
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const stringifyObject = require('stringify-object');
|
|
12
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
15
13
|
|
|
16
14
|
module.exports = function (source, options) {
|
|
17
|
-
const opts =
|
|
18
|
-
indent: ' '
|
|
19
|
-
|
|
15
|
+
const opts = {
|
|
16
|
+
indent: ' ',
|
|
17
|
+
...options,
|
|
18
|
+
};
|
|
20
19
|
|
|
21
|
-
const code = new CodeBuilder(opts.indent)
|
|
20
|
+
const code = new CodeBuilder(opts.indent);
|
|
22
21
|
|
|
23
|
-
code.push('
|
|
24
|
-
.blank()
|
|
22
|
+
code.push('const axios = require("axios").default;');
|
|
25
23
|
|
|
26
24
|
const reqOpts = {
|
|
27
25
|
method: source.method,
|
|
28
|
-
url: source.
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (Object.keys(source.queryObj).length) {
|
|
32
|
-
reqOpts.params = source.queryObj
|
|
33
|
-
}
|
|
26
|
+
url: source.fullUrl,
|
|
27
|
+
};
|
|
34
28
|
|
|
35
29
|
if (Object.keys(source.allHeaders).length) {
|
|
36
|
-
reqOpts.headers = source.allHeaders
|
|
30
|
+
reqOpts.headers = source.allHeaders;
|
|
37
31
|
}
|
|
38
32
|
|
|
39
33
|
switch (source.postData.mimeType) {
|
|
40
34
|
case 'application/x-www-form-urlencoded':
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
code.push("const { URLSearchParams } = require('url');");
|
|
36
|
+
code.push('const encodedParams = new URLSearchParams();');
|
|
37
|
+
code.blank();
|
|
38
|
+
|
|
39
|
+
source.postData.params.forEach(function (param) {
|
|
40
|
+
code.push(`encodedParams.set('${param.name}', '${param.value}');`);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
reqOpts.data = 'encodedParams';
|
|
44
|
+
break;
|
|
43
45
|
|
|
44
46
|
case 'application/json':
|
|
45
47
|
if (source.postData.jsonObj) {
|
|
46
|
-
reqOpts.data = source.postData.jsonObj
|
|
48
|
+
reqOpts.data = source.postData.jsonObj;
|
|
47
49
|
}
|
|
48
|
-
break
|
|
50
|
+
break;
|
|
49
51
|
|
|
50
52
|
default:
|
|
51
53
|
if (source.postData.text) {
|
|
52
|
-
reqOpts.data = source.postData.text
|
|
54
|
+
reqOpts.data = source.postData.text;
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
|
|
56
|
-
code.
|
|
57
|
-
|
|
58
|
+
code.blank();
|
|
59
|
+
code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 })).blank();
|
|
58
60
|
|
|
59
|
-
code
|
|
61
|
+
code
|
|
62
|
+
.push('axios.request(options).then(function (response) {')
|
|
60
63
|
.push(1, 'console.log(response.data);')
|
|
61
64
|
.push('}).catch(%s', 'function (error) {')
|
|
62
65
|
.push(1, 'console.error(error);')
|
|
63
|
-
.push('});')
|
|
66
|
+
.push('});');
|
|
64
67
|
|
|
65
|
-
return code.join()
|
|
66
|
-
}
|
|
68
|
+
return code.join().replace(/'encodedParams'/, 'encodedParams');
|
|
69
|
+
};
|
|
67
70
|
|
|
68
71
|
module.exports.info = {
|
|
69
72
|
key: 'axios',
|
|
70
73
|
title: 'Axios',
|
|
71
74
|
link: 'https://github.com/axios/axios',
|
|
72
|
-
description: 'Promise based HTTP client for the browser and node.js'
|
|
73
|
-
}
|
|
75
|
+
description: 'Promise based HTTP client for the browser and node.js',
|
|
76
|
+
};
|
|
@@ -8,128 +8,134 @@
|
|
|
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
|
-
const
|
|
14
|
-
const CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const headerHelpers = require('../../helpers/headers');
|
|
12
|
+
const stringifyObject = require('stringify-object');
|
|
13
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
15
14
|
|
|
16
15
|
module.exports = function (source, options) {
|
|
17
|
-
const opts =
|
|
18
|
-
indent: ' '
|
|
19
|
-
|
|
16
|
+
const opts = {
|
|
17
|
+
indent: ' ',
|
|
18
|
+
...options,
|
|
19
|
+
};
|
|
20
20
|
|
|
21
|
-
let includeFS = false
|
|
22
|
-
const code = new CodeBuilder(opts.indent)
|
|
21
|
+
let includeFS = false;
|
|
22
|
+
const code = new CodeBuilder(opts.indent);
|
|
23
23
|
|
|
24
|
-
code.push(
|
|
25
|
-
const url = source.fullUrl
|
|
24
|
+
code.push("const fetch = require('node-fetch');");
|
|
25
|
+
const url = source.fullUrl;
|
|
26
26
|
const reqOpts = {
|
|
27
|
-
method: source.method
|
|
27
|
+
method: source.method,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// The `form-data` library automatically adds a `Content-Type` header for `multipart/form-data` content and if we
|
|
31
|
+
// add our own here, data won't be correctly transferred.
|
|
32
|
+
if (source.postData.mimeType === 'multipart/form-data') {
|
|
33
|
+
if (source.postData.params) {
|
|
34
|
+
const contentTypeHeader = headerHelpers.getHeaderName(source.allHeaders, 'content-type');
|
|
35
|
+
if (contentTypeHeader) {
|
|
36
|
+
// eslint-disable-next-line no-param-reassign
|
|
37
|
+
delete source.allHeaders[contentTypeHeader];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
28
40
|
}
|
|
29
41
|
|
|
30
|
-
if (Object.keys(source.
|
|
31
|
-
reqOpts.headers = source.
|
|
42
|
+
if (Object.keys(source.allHeaders).length) {
|
|
43
|
+
reqOpts.headers = source.allHeaders;
|
|
32
44
|
}
|
|
33
45
|
|
|
34
46
|
switch (source.postData.mimeType) {
|
|
35
47
|
case 'application/x-www-form-urlencoded':
|
|
36
|
-
code.unshift(
|
|
37
|
-
code.push('const encodedParams = new URLSearchParams();')
|
|
38
|
-
code.blank()
|
|
48
|
+
code.unshift("const { URLSearchParams } = require('url');");
|
|
49
|
+
code.push('const encodedParams = new URLSearchParams();');
|
|
50
|
+
code.blank();
|
|
39
51
|
|
|
40
52
|
source.postData.params.forEach(function (param) {
|
|
41
|
-
code.push(
|
|
42
|
-
})
|
|
53
|
+
code.push(`encodedParams.set('${param.name}', '${param.value}');`);
|
|
54
|
+
});
|
|
43
55
|
|
|
44
|
-
reqOpts.body = 'encodedParams'
|
|
45
|
-
break
|
|
56
|
+
reqOpts.body = 'encodedParams';
|
|
57
|
+
break;
|
|
46
58
|
|
|
47
59
|
case 'application/json':
|
|
48
60
|
if (source.postData.jsonObj) {
|
|
49
|
-
reqOpts.body =
|
|
61
|
+
reqOpts.body = source.postData.jsonObj;
|
|
50
62
|
}
|
|
51
|
-
break
|
|
63
|
+
break;
|
|
52
64
|
|
|
53
65
|
case 'multipart/form-data':
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
if (source.postData.params) {
|
|
67
|
+
code.unshift("const FormData = require('form-data');");
|
|
68
|
+
code.push('const formData = new FormData();');
|
|
69
|
+
code.blank();
|
|
70
|
+
|
|
71
|
+
source.postData.params.forEach(function (param) {
|
|
72
|
+
if (!param.fileName && !param.contentType) {
|
|
73
|
+
code.push(`formData.append('${param.name}', '${param.value}');`);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (param.fileName) {
|
|
78
|
+
includeFS = true;
|
|
79
|
+
code.push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
70
84
|
|
|
71
85
|
default:
|
|
72
86
|
if (source.postData.text) {
|
|
73
|
-
reqOpts.body = source.postData.text
|
|
87
|
+
reqOpts.body = source.postData.text;
|
|
74
88
|
}
|
|
75
89
|
}
|
|
76
90
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
// read, so if you pass the `useObjectBody` option we keep the object as a literal and use
|
|
100
|
-
// this transform function to wrap the literal in a `JSON.stringify` call.
|
|
101
|
-
transform: (object, property, originalResult) => {
|
|
102
|
-
if (property === 'body' && opts.useObjectBody && source.postData.mimeType === 'application/json') {
|
|
103
|
-
return 'JSON.stringify(' + originalResult + ')'
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return originalResult
|
|
107
|
-
}
|
|
108
|
-
}))
|
|
109
|
-
.blank()
|
|
91
|
+
code.blank();
|
|
92
|
+
code.push(`const url = '${url}';`);
|
|
93
|
+
code
|
|
94
|
+
.push(
|
|
95
|
+
'const options = %s;',
|
|
96
|
+
stringifyObject(reqOpts, {
|
|
97
|
+
indent: opts.indent,
|
|
98
|
+
inlineCharacterLimit: 80,
|
|
99
|
+
|
|
100
|
+
// The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
|
|
101
|
+
// read, so we keep the object as a literal and use this transform function to wrap the literal
|
|
102
|
+
// in a `JSON.stringify` call.
|
|
103
|
+
transform: (object, property, originalResult) => {
|
|
104
|
+
if (property === 'body' && source.postData.mimeType === 'application/json') {
|
|
105
|
+
return `JSON.stringify(${originalResult})`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return originalResult;
|
|
109
|
+
},
|
|
110
|
+
})
|
|
111
|
+
)
|
|
112
|
+
.blank();
|
|
110
113
|
|
|
111
114
|
if (includeFS) {
|
|
112
|
-
code.unshift(
|
|
115
|
+
code.unshift("const fs = require('fs');");
|
|
113
116
|
}
|
|
114
117
|
|
|
115
118
|
if (source.postData.mimeType === 'multipart/form-data') {
|
|
116
|
-
|
|
117
|
-
.blank()
|
|
119
|
+
if (source.postData.params) {
|
|
120
|
+
code.push('options.body = formData;').blank();
|
|
121
|
+
}
|
|
118
122
|
}
|
|
119
123
|
|
|
120
|
-
code
|
|
124
|
+
code
|
|
125
|
+
.push('fetch(url, options)')
|
|
121
126
|
.push(1, '.then(res => res.json())')
|
|
122
127
|
.push(1, '.then(json => console.log(json))')
|
|
123
|
-
.push(1,
|
|
128
|
+
.push(1, ".catch(err => console.error('error:' + err));");
|
|
124
129
|
|
|
125
|
-
return code
|
|
130
|
+
return code
|
|
131
|
+
.join()
|
|
126
132
|
.replace(/'encodedParams'/, 'encodedParams')
|
|
127
|
-
.replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")')
|
|
128
|
-
}
|
|
133
|
+
.replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
|
|
134
|
+
};
|
|
129
135
|
|
|
130
136
|
module.exports.info = {
|
|
131
137
|
key: 'fetch',
|
|
132
138
|
title: 'Fetch',
|
|
133
139
|
link: 'https://github.com/bitinn/node-fetch',
|
|
134
|
-
description: 'Simplified HTTP node-fetch client'
|
|
135
|
-
}
|
|
140
|
+
description: 'Simplified HTTP node-fetch client',
|
|
141
|
+
};
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
module.exports = {
|
|
4
2
|
info: {
|
|
5
3
|
key: 'node',
|
|
6
4
|
title: 'Node.js',
|
|
7
5
|
extname: '.js',
|
|
8
|
-
default: 'native'
|
|
6
|
+
default: 'native',
|
|
7
|
+
cli: 'node %s',
|
|
9
8
|
},
|
|
10
9
|
|
|
11
10
|
native: require('./native'),
|
|
12
11
|
request: require('./request'),
|
|
13
12
|
unirest: require('./unirest'),
|
|
14
13
|
axios: require('./axios'),
|
|
15
|
-
fetch: require('./fetch')
|
|
16
|
-
}
|
|
14
|
+
fetch: require('./fetch'),
|
|
15
|
+
};
|
|
@@ -8,29 +8,29 @@
|
|
|
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
|
-
const stringifyObject = require('stringify-object')
|
|
14
|
-
const CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const stringifyObject = require('stringify-object');
|
|
12
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
15
13
|
|
|
16
14
|
module.exports = function (source, options) {
|
|
17
|
-
const opts =
|
|
18
|
-
indent: ' '
|
|
19
|
-
|
|
15
|
+
const opts = {
|
|
16
|
+
indent: ' ',
|
|
17
|
+
...options,
|
|
18
|
+
};
|
|
20
19
|
|
|
21
|
-
const code = new CodeBuilder(opts.indent)
|
|
20
|
+
const code = new CodeBuilder(opts.indent);
|
|
22
21
|
|
|
23
22
|
const reqOpts = {
|
|
24
23
|
method: source.method,
|
|
25
24
|
hostname: source.uriObj.hostname,
|
|
26
25
|
port: source.uriObj.port,
|
|
27
26
|
path: source.uriObj.path,
|
|
28
|
-
headers: source.allHeaders
|
|
29
|
-
}
|
|
27
|
+
headers: source.allHeaders,
|
|
28
|
+
};
|
|
30
29
|
|
|
31
|
-
code.push('const http = require("%s");', source.uriObj.protocol.replace(':', ''))
|
|
30
|
+
code.push('const http = require("%s");', source.uriObj.protocol.replace(':', ''));
|
|
32
31
|
|
|
33
|
-
code
|
|
32
|
+
code
|
|
33
|
+
.blank()
|
|
34
34
|
.push('const options = %s;', JSON.stringify(reqOpts, null, opts.indent))
|
|
35
35
|
.blank()
|
|
36
36
|
.push('const req = http.request(options, function (res) {')
|
|
@@ -45,42 +45,48 @@ module.exports = function (source, options) {
|
|
|
45
45
|
.push(2, 'console.log(body.toString());')
|
|
46
46
|
.push(1, '});')
|
|
47
47
|
.push('});')
|
|
48
|
-
.blank()
|
|
48
|
+
.blank();
|
|
49
49
|
|
|
50
50
|
switch (source.postData.mimeType) {
|
|
51
51
|
case 'application/x-www-form-urlencoded':
|
|
52
52
|
if (source.postData.paramsObj) {
|
|
53
|
-
code.unshift('const qs = require("querystring");')
|
|
54
|
-
code.push(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
code.unshift('const qs = require("querystring");');
|
|
54
|
+
code.push(
|
|
55
|
+
'req.write(qs.stringify(%s));',
|
|
56
|
+
stringifyObject(source.postData.paramsObj, {
|
|
57
|
+
indent: ' ',
|
|
58
|
+
inlineCharacterLimit: 80,
|
|
59
|
+
})
|
|
60
|
+
);
|
|
58
61
|
}
|
|
59
|
-
break
|
|
62
|
+
break;
|
|
60
63
|
|
|
61
64
|
case 'application/json':
|
|
62
65
|
if (source.postData.jsonObj) {
|
|
63
|
-
code.push(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
66
|
+
code.push(
|
|
67
|
+
'req.write(JSON.stringify(%s));',
|
|
68
|
+
stringifyObject(source.postData.jsonObj, {
|
|
69
|
+
indent: ' ',
|
|
70
|
+
inlineCharacterLimit: 80,
|
|
71
|
+
})
|
|
72
|
+
);
|
|
67
73
|
}
|
|
68
|
-
break
|
|
74
|
+
break;
|
|
69
75
|
|
|
70
76
|
default:
|
|
71
77
|
if (source.postData.text) {
|
|
72
|
-
code.push('req.write(%s);', JSON.stringify(source.postData.text, null, opts.indent))
|
|
78
|
+
code.push('req.write(%s);', JSON.stringify(source.postData.text, null, opts.indent));
|
|
73
79
|
}
|
|
74
80
|
}
|
|
75
81
|
|
|
76
|
-
code.push('req.end();')
|
|
82
|
+
code.push('req.end();');
|
|
77
83
|
|
|
78
|
-
return code.join()
|
|
79
|
-
}
|
|
84
|
+
return code.join();
|
|
85
|
+
};
|
|
80
86
|
|
|
81
87
|
module.exports.info = {
|
|
82
88
|
key: 'native',
|
|
83
89
|
title: 'HTTP',
|
|
84
90
|
link: 'http://nodejs.org/api/http.html#http_http_request_options_callback',
|
|
85
|
-
description: 'Node.js native HTTP interface'
|
|
86
|
-
}
|
|
91
|
+
description: 'Node.js native HTTP interface',
|
|
92
|
+
};
|