@readme/httpsnippet 3.1.1 → 3.1.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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.1.1",
2
+ "version": "3.1.2",
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": "^7.1.7",
59
- "jest": "^27.2.0",
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
  },
@@ -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;',
@@ -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 v7',
132
+ title: 'Guzzle',
133
133
  link: 'http://docs.guzzlephp.org/en/stable/',
134
- description: 'PHP with guzzle v7',
134
+ description: 'PHP with guzzle',
135
135
  };
@@ -75,24 +75,20 @@ module.exports = function (source) {
75
75
  }
76
76
 
77
77
  // Construct headers
78
- const headers = source.allHeaders;
79
- let headerCount = 0;
80
- let header = '';
78
+ const headers = [];
81
79
  let cookies;
82
80
  let accept;
83
81
 
84
- Object.keys(headers).forEach(head => {
85
- if (head.toLowerCase() === 'accept') {
86
- accept = `, accept("${headers[head]}")`;
87
- headerCount += 1;
88
- } else if (head.toLowerCase() === 'cookie') {
89
- cookies = `, set_cookies(\`${headers[head].replace(/;/g, '", `').replace(/` /g, '`').replace(/=/g, '` = "')}")`;
90
- headerCount += 1;
91
- } else if (head.toLowerCase() !== 'content-type') {
92
- header = `${header + head.replace('-', '_')} = '${headers[head]}`;
93
- if (headerCount > 1) {
94
- header += "', ";
95
- }
82
+ Object.keys(source.allHeaders).forEach(header => {
83
+ if (header.toLowerCase() === 'accept') {
84
+ accept = `, accept("${source.allHeaders[header]}")`;
85
+ } else if (header.toLowerCase() === 'cookie') {
86
+ cookies = `, set_cookies(\`${source.allHeaders[header]
87
+ .replace(/;/g, '", `')
88
+ .replace(/` /g, '`')
89
+ .replace(/=/g, '` = "')}")`;
90
+ } else if (header.toLowerCase() !== 'content-type') {
91
+ headers.push(`'${header}' = '${source.allHeaders[header]}'`);
96
92
  }
97
93
  });
98
94
 
@@ -104,8 +100,8 @@ module.exports = function (source) {
104
100
  request += ', body = payload';
105
101
  }
106
102
 
107
- if (header !== '') {
108
- request += `, add_headers(${header}')`;
103
+ if (headers.length) {
104
+ request += `, add_headers(${headers.join(', ')})`;
109
105
  }
110
106
 
111
107
  if (source.queryString.length) {