@readme/httpsnippet 2.5.1 → 3.0.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.
Files changed (68) hide show
  1. package/README.md +19 -9
  2. package/package.json +24 -28
  3. package/src/helpers/code-builder.js +85 -80
  4. package/src/helpers/form-data.js +27 -25
  5. package/src/helpers/headers.js +10 -19
  6. package/src/helpers/reducer.js +10 -14
  7. package/src/index.js +167 -146
  8. package/src/targets/c/index.js +3 -5
  9. package/src/targets/c/libcurl.js +18 -23
  10. package/src/targets/clojure/clj_http.js +103 -87
  11. package/src/targets/clojure/index.js +3 -5
  12. package/src/targets/csharp/httpclient.js +90 -90
  13. package/src/targets/csharp/index.js +3 -5
  14. package/src/targets/csharp/restsharp.js +20 -22
  15. package/src/targets/go/index.js +3 -5
  16. package/src/targets/go/native.js +46 -53
  17. package/src/targets/http/http1.1.js +26 -36
  18. package/src/targets/http/index.js +3 -5
  19. package/src/targets/index.js +2 -4
  20. package/src/targets/java/asynchttp.js +22 -23
  21. package/src/targets/java/index.js +3 -5
  22. package/src/targets/java/nethttp.js +20 -24
  23. package/src/targets/java/okhttp.js +31 -31
  24. package/src/targets/java/unirest.js +22 -19
  25. package/src/targets/javascript/axios.js +43 -38
  26. package/src/targets/javascript/fetch.js +66 -68
  27. package/src/targets/javascript/index.js +3 -5
  28. package/src/targets/javascript/jquery.js +46 -40
  29. package/src/targets/javascript/xhr.js +42 -38
  30. package/src/targets/kotlin/index.js +3 -5
  31. package/src/targets/kotlin/okhttp.js +31 -31
  32. package/src/targets/node/axios.js +33 -30
  33. package/src/targets/node/fetch.js +89 -83
  34. package/src/targets/node/index.js +4 -5
  35. package/src/targets/node/native.js +36 -30
  36. package/src/targets/node/request.js +64 -64
  37. package/src/targets/node/unirest.js +56 -55
  38. package/src/targets/objc/helpers.js +24 -23
  39. package/src/targets/objc/index.js +3 -5
  40. package/src/targets/objc/nsurlsession.js +73 -45
  41. package/src/targets/ocaml/cohttp.js +29 -27
  42. package/src/targets/ocaml/index.js +3 -5
  43. package/src/targets/php/curl.js +86 -82
  44. package/src/targets/php/guzzle.js +135 -0
  45. package/src/targets/php/helpers.js +29 -28
  46. package/src/targets/php/http1.js +33 -44
  47. package/src/targets/php/http2.js +70 -71
  48. package/src/targets/php/index.js +5 -5
  49. package/src/targets/powershell/common.js +35 -30
  50. package/src/targets/powershell/index.js +3 -5
  51. package/src/targets/powershell/restmethod.js +3 -5
  52. package/src/targets/powershell/webrequest.js +3 -5
  53. package/src/targets/python/helpers.js +37 -34
  54. package/src/targets/python/index.js +4 -5
  55. package/src/targets/python/python3.js +33 -44
  56. package/src/targets/python/requests.js +47 -72
  57. package/src/targets/r/httr.js +59 -70
  58. package/src/targets/r/index.js +3 -5
  59. package/src/targets/ruby/index.js +3 -5
  60. package/src/targets/ruby/native.js +39 -33
  61. package/src/targets/shell/curl.js +74 -52
  62. package/src/targets/shell/helpers.js +7 -9
  63. package/src/targets/shell/httpie.js +45 -39
  64. package/src/targets/shell/index.js +4 -5
  65. package/src/targets/shell/wget.js +20 -22
  66. package/src/targets/swift/helpers.js +35 -32
  67. package/src/targets/swift/index.js +3 -5
  68. package/src/targets/swift/nsurlsession.js +53 -39
@@ -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
- 'use strict'
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 = Object.assign({
17
- indent: ' '
18
- }, options)
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('val mediaType = MediaType.parse("%s; boundary=%s")', source.postData.mimeType, source.postData.boundary)
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 util = require('util')
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 = Object.assign({
18
- indent: ' '
19
- }, options)
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('var axios = require("axios").default;')
24
- .blank()
22
+ code.push('const axios = require("axios").default;');
25
23
 
26
24
  const reqOpts = {
27
25
  method: source.method,
28
- url: source.url
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
- reqOpts.data = source.postData.paramsObj
42
- break
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.push('var options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
57
- .blank()
58
+ code.blank();
59
+ code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 })).blank();
58
60
 
59
- code.push(util.format('axios.request(options).then(%s', 'function (response) {'))
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
- 'use strict'
12
-
13
- const stringifyObject = require('stringify-object')
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 = Object.assign({
18
- indent: ' '
19
- }, options)
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('const fetch = require(\'node-fetch\');')
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.headersObj).length) {
31
- reqOpts.headers = source.headersObj
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('const { URLSearchParams } = require(\'url\');')
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('encodedParams.set(\'' + param.name + '\', \'' + param.value + '\');')
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 = options.useObjectBody ? source.postData.jsonObj : JSON.stringify(source.postData.jsonObj)
61
+ reqOpts.body = source.postData.jsonObj;
50
62
  }
51
- break
63
+ break;
52
64
 
53
65
  case 'multipart/form-data':
54
- code.unshift('const FormData = require(\'form-data\');')
55
- code.push('const formData = new FormData();')
56
- code.blank()
57
-
58
- source.postData.params.forEach(function (param) {
59
- if (!param.fileName && !param.fileName && !param.contentType) {
60
- code.push('formData.append(\'' + param.name + '\', \'' + param.value + '\');')
61
- return
62
- }
63
-
64
- if (param.fileName) {
65
- includeFS = true
66
- code.push('formData.append(\'' + param.name + '\', fs.createReadStream(\'' + param.fileName + '\'));')
67
- }
68
- })
69
- break
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
- // construct cookies argument
78
- if (source.cookies.length) {
79
- let cookies = ''
80
- source.cookies.forEach(function (cookie) {
81
- cookies = cookies + encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value) + '; '
82
- })
83
-
84
- if (reqOpts.headers) {
85
- reqOpts.headers.cookie = cookies
86
- } else {
87
- reqOpts.headers = {}
88
- reqOpts.headers.cookie = cookies
89
- }
90
- }
91
-
92
- code.blank()
93
- code.push('const url = \'' + url + '\';')
94
- code.push('const options = %s;', stringifyObject(reqOpts, {
95
- indent: opts.indent,
96
- inlineCharacterLimit: 80,
97
-
98
- // The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
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('const fs = require(\'fs\');')
115
+ code.unshift("const fs = require('fs');");
113
116
  }
114
117
 
115
118
  if (source.postData.mimeType === 'multipart/form-data') {
116
- code.push('options.body = formData;')
117
- .blank()
119
+ if (source.postData.params) {
120
+ code.push('options.body = formData;').blank();
121
+ }
118
122
  }
119
123
 
120
- code.push('fetch(url, options)')
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, '.catch(err => console.error(\'error:\' + err));')
128
+ .push(1, ".catch(err => console.error('error:' + err));");
124
129
 
125
- return code.join()
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
- 'use strict'
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 = Object.assign({
18
- indent: ' '
19
- }, options)
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.blank()
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('req.write(qs.stringify(%s));', stringifyObject(source.postData.paramsObj, {
55
- indent: ' ',
56
- inlineCharacterLimit: 80
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('req.write(JSON.stringify(%s));', stringifyObject(source.postData.jsonObj, {
64
- indent: ' ',
65
- inlineCharacterLimit: 80
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
+ };