@readme/httpsnippet 3.1.0 → 3.1.3
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/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "3.1.
|
|
2
|
+
"version": "3.1.3",
|
|
3
3
|
"name": "@readme/httpsnippet",
|
|
4
4
|
"description": "HTTP Request snippet generator for *most* languages",
|
|
5
5
|
"homepage": "https://github.com/readmeio/httpsnippet",
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@readme/eslint-config": "^8.0.2",
|
|
57
57
|
"eslint": "^8.3.0",
|
|
58
|
-
"glob": "^
|
|
59
|
-
"jest": "^
|
|
58
|
+
"glob": "^8.0.1",
|
|
59
|
+
"jest": "^28.0.3",
|
|
60
60
|
"prettier": "^2.5.0",
|
|
61
61
|
"require-directory": "^2.1.1"
|
|
62
62
|
},
|
|
@@ -36,7 +36,14 @@ module.exports = function (source, options) {
|
|
|
36
36
|
|
|
37
37
|
switch (source.postData.mimeType) {
|
|
38
38
|
case 'application/x-www-form-urlencoded':
|
|
39
|
-
|
|
39
|
+
code.push('const encodedParams = new URLSearchParams();');
|
|
40
|
+
source.postData.params.forEach(function (param) {
|
|
41
|
+
code.push(`encodedParams.set('${param.name}', '${param.value}');`);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
code.blank();
|
|
45
|
+
|
|
46
|
+
reqOpts.data = 'encodedParams';
|
|
40
47
|
break;
|
|
41
48
|
|
|
42
49
|
case 'application/json':
|
|
@@ -83,7 +90,7 @@ module.exports = function (source, options) {
|
|
|
83
90
|
.push(1, 'console.error(error);')
|
|
84
91
|
.push('});');
|
|
85
92
|
|
|
86
|
-
return code.join();
|
|
93
|
+
return code.join().replace(/'encodedParams'/, 'encodedParams,');
|
|
87
94
|
};
|
|
88
95
|
|
|
89
96
|
module.exports.info = {
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
const stringifyObject = require('stringify-object');
|
|
12
12
|
const CodeBuilder = require('../../helpers/code-builder');
|
|
13
|
+
const { getHeaderName } = require('../../helpers/headers');
|
|
13
14
|
|
|
14
15
|
module.exports = function (source, options) {
|
|
15
16
|
const opts = {
|
|
@@ -45,6 +46,15 @@ module.exports = function (source, options) {
|
|
|
45
46
|
|
|
46
47
|
case 'multipart/form-data':
|
|
47
48
|
if (source.postData.params) {
|
|
49
|
+
// The FormData API automatically adds a `Content-Type` header for `multipart/form-data`
|
|
50
|
+
// content and if we add our own here data won't be correctly transmitted.
|
|
51
|
+
if (reqOptions.headers) {
|
|
52
|
+
const contentTypeHeader = getHeaderName(reqOptions.headers, 'content-type');
|
|
53
|
+
if (contentTypeHeader) {
|
|
54
|
+
delete reqOptions.headers[contentTypeHeader];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
48
58
|
code.push('const form = new FormData();');
|
|
49
59
|
|
|
50
60
|
source.postData.params.forEach(function (param) {
|
|
@@ -65,6 +75,12 @@ module.exports = function (source, options) {
|
|
|
65
75
|
}
|
|
66
76
|
}
|
|
67
77
|
|
|
78
|
+
// If we ultimately don't have any headers to send then we shouldn't add an empty object into the
|
|
79
|
+
// request options.
|
|
80
|
+
if (reqOptions.headers && !Object.keys(reqOptions.headers).length) {
|
|
81
|
+
delete reqOptions.headers;
|
|
82
|
+
}
|
|
83
|
+
|
|
68
84
|
code
|
|
69
85
|
.push(
|
|
70
86
|
'const options = %s;',
|
|
@@ -33,9 +33,9 @@ module.exports = function (source, options) {
|
|
|
33
33
|
switch (source.postData.mimeType) {
|
|
34
34
|
case 'application/x-www-form-urlencoded':
|
|
35
35
|
code.push("const { URLSearchParams } = require('url');");
|
|
36
|
-
code.push('const encodedParams = new URLSearchParams();');
|
|
37
36
|
code.blank();
|
|
38
37
|
|
|
38
|
+
code.push('const encodedParams = new URLSearchParams();');
|
|
39
39
|
source.postData.params.forEach(function (param) {
|
|
40
40
|
code.push(`encodedParams.set('${param.name}', '${param.value}');`);
|
|
41
41
|
});
|
|
@@ -65,7 +65,7 @@ module.exports = function (source, options) {
|
|
|
65
65
|
.push(1, 'console.error(error);')
|
|
66
66
|
.push('});');
|
|
67
67
|
|
|
68
|
-
return code.join().replace(/'encodedParams'/, 'encodedParams');
|
|
68
|
+
return code.join().replace(/'encodedParams'/, 'encodedParams,');
|
|
69
69
|
};
|
|
70
70
|
|
|
71
71
|
module.exports.info = {
|
|
@@ -27,18 +27,6 @@ module.exports = function (source, options) {
|
|
|
27
27
|
method: source.method,
|
|
28
28
|
};
|
|
29
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
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
30
|
if (Object.keys(source.allHeaders).length) {
|
|
43
31
|
reqOpts.headers = source.allHeaders;
|
|
44
32
|
}
|
|
@@ -64,6 +52,16 @@ module.exports = function (source, options) {
|
|
|
64
52
|
|
|
65
53
|
case 'multipart/form-data':
|
|
66
54
|
if (source.postData.params) {
|
|
55
|
+
// The `form-data` library automatically adds a `Content-Type` header for
|
|
56
|
+
// `multipart/form-data` content and if we add our own here, data won't be correctly
|
|
57
|
+
// transferred.
|
|
58
|
+
if (reqOpts.headers) {
|
|
59
|
+
const contentTypeHeader = headerHelpers.getHeaderName(reqOpts.headers, 'content-type');
|
|
60
|
+
if (contentTypeHeader) {
|
|
61
|
+
delete reqOpts.headers[contentTypeHeader];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
67
65
|
code.unshift("const FormData = require('form-data');");
|
|
68
66
|
code.push('const formData = new FormData();');
|
|
69
67
|
code.blank();
|
|
@@ -88,6 +86,12 @@ module.exports = function (source, options) {
|
|
|
88
86
|
}
|
|
89
87
|
}
|
|
90
88
|
|
|
89
|
+
// If we ultimately don't have any headers to send then we shouldn't add an empty object into
|
|
90
|
+
// the request options.
|
|
91
|
+
if (reqOpts.headers && !Object.keys(reqOpts.headers).length) {
|
|
92
|
+
delete reqOpts.headers;
|
|
93
|
+
}
|
|
94
|
+
|
|
91
95
|
code.blank();
|
|
92
96
|
code.push(`const url = '${url}';`);
|
|
93
97
|
code
|
|
@@ -129,7 +129,7 @@ module.exports = function (source, options) {
|
|
|
129
129
|
|
|
130
130
|
module.exports.info = {
|
|
131
131
|
key: 'guzzle',
|
|
132
|
-
title: 'Guzzle
|
|
132
|
+
title: 'Guzzle',
|
|
133
133
|
link: 'http://docs.guzzlephp.org/en/stable/',
|
|
134
|
-
description: 'PHP with guzzle
|
|
134
|
+
description: 'PHP with guzzle',
|
|
135
135
|
};
|
package/src/targets/r/httr.js
CHANGED
|
@@ -23,20 +23,17 @@ module.exports = function (source) {
|
|
|
23
23
|
|
|
24
24
|
// Construct query string
|
|
25
25
|
const qs = source.queryObj;
|
|
26
|
-
const queryCount = Object.keys(qs).length;
|
|
27
26
|
// eslint-disable-next-line no-param-reassign
|
|
28
27
|
delete source.queryObj.key;
|
|
29
28
|
|
|
30
|
-
|
|
29
|
+
const queryCount = Object.keys(qs).length;
|
|
30
|
+
if (queryCount === 1) {
|
|
31
31
|
code.push('queryString <- list(%s = "%s")', Object.keys(qs), Object.values(qs).toString()).blank();
|
|
32
|
-
} else if (
|
|
33
|
-
let count = 1;
|
|
34
|
-
|
|
32
|
+
} else if (queryCount > 1) {
|
|
35
33
|
code.push('queryString <- list(');
|
|
36
34
|
|
|
37
|
-
Object.keys(qs).forEach(query => {
|
|
38
|
-
|
|
39
|
-
if (count++ !== queryCount - 1) {
|
|
35
|
+
Object.keys(qs).forEach((query, i) => {
|
|
36
|
+
if (i !== queryCount - 1) {
|
|
40
37
|
code.push(' %s = "%s",', query, qs[query].toString());
|
|
41
38
|
} else {
|
|
42
39
|
code.push(' %s = "%s"', query, qs[query].toString());
|
|
@@ -75,24 +72,20 @@ module.exports = function (source) {
|
|
|
75
72
|
}
|
|
76
73
|
|
|
77
74
|
// Construct headers
|
|
78
|
-
const headers =
|
|
79
|
-
let headerCount = 0;
|
|
80
|
-
let header = '';
|
|
75
|
+
const headers = [];
|
|
81
76
|
let cookies;
|
|
82
77
|
let accept;
|
|
83
78
|
|
|
84
|
-
Object.keys(
|
|
85
|
-
if (
|
|
86
|
-
accept = `, accept("${
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
header += "', ";
|
|
95
|
-
}
|
|
79
|
+
Object.keys(source.allHeaders).forEach(header => {
|
|
80
|
+
if (header.toLowerCase() === 'accept') {
|
|
81
|
+
accept = `, accept("${source.allHeaders[header]}")`;
|
|
82
|
+
} else if (header.toLowerCase() === 'cookie') {
|
|
83
|
+
cookies = `, set_cookies(\`${source.allHeaders[header]
|
|
84
|
+
.replace(/;/g, '", `')
|
|
85
|
+
.replace(/` /g, '`')
|
|
86
|
+
.replace(/=/g, '` = "')}")`;
|
|
87
|
+
} else if (header.toLowerCase() !== 'content-type') {
|
|
88
|
+
headers.push(`'${header}' = '${source.allHeaders[header]}'`);
|
|
96
89
|
}
|
|
97
90
|
});
|
|
98
91
|
|
|
@@ -104,8 +97,8 @@ module.exports = function (source) {
|
|
|
104
97
|
request += ', body = payload';
|
|
105
98
|
}
|
|
106
99
|
|
|
107
|
-
if (
|
|
108
|
-
request += `, add_headers(${
|
|
100
|
+
if (headers.length) {
|
|
101
|
+
request += `, add_headers(${headers.join(', ')})`;
|
|
109
102
|
}
|
|
110
103
|
|
|
111
104
|
if (source.queryString.length) {
|