@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
@@ -7,83 +7,88 @@
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('import axios from "axios";')
24
- .blank()
22
+ code.push('import axios from "axios";').blank();
25
23
 
26
24
  const reqOpts = {
27
25
  method: source.method,
28
- url: source.url
29
- }
26
+ url: source.url,
27
+ };
30
28
 
31
29
  if (Object.keys(source.queryObj).length) {
32
- reqOpts.params = source.queryObj
30
+ reqOpts.params = source.queryObj;
33
31
  }
34
32
 
35
33
  if (Object.keys(source.allHeaders).length) {
36
- reqOpts.headers = source.allHeaders
34
+ reqOpts.headers = source.allHeaders;
37
35
  }
38
36
 
39
37
  switch (source.postData.mimeType) {
40
38
  case 'application/x-www-form-urlencoded':
41
- reqOpts.data = source.postData.paramsObj
42
- break
39
+ reqOpts.data = source.postData.paramsObj;
40
+ break;
43
41
 
44
42
  case 'application/json':
45
43
  if (source.postData.jsonObj) {
46
- reqOpts.data = source.postData.jsonObj
44
+ reqOpts.data = source.postData.jsonObj;
47
45
  }
48
- break
46
+ break;
49
47
 
50
48
  case 'multipart/form-data':
51
- code.push('const form = new FormData();')
49
+ if (source.postData.params) {
50
+ code.push('const form = new FormData();');
52
51
 
53
- source.postData.params.forEach(function (param) {
54
- code.push(
55
- 'form.append(%s, %s);',
56
- JSON.stringify(param.name),
57
- JSON.stringify(param.value || param.fileName || '')
58
- )
59
- })
52
+ source.postData.params.forEach(function (param) {
53
+ code.push(
54
+ 'form.append(%s, %s);',
55
+ JSON.stringify(param.name),
56
+ JSON.stringify(param.value || param.fileName || '')
57
+ );
58
+ });
60
59
 
61
- code.blank()
60
+ code.blank();
62
61
 
63
- reqOpts.data = '[form]'
64
- break
62
+ reqOpts.data = '[form]';
63
+ }
64
+ break;
65
65
 
66
66
  default:
67
67
  if (source.postData.text) {
68
- reqOpts.data = source.postData.text
68
+ reqOpts.data = source.postData.text;
69
69
  }
70
70
  }
71
71
 
72
- code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }).replace('"[form]"', 'form'))
73
- .blank()
72
+ code
73
+ .push(
74
+ 'const options = %s;',
75
+ stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }).replace('"[form]"', 'form')
76
+ )
77
+ .blank();
74
78
 
75
- code.push(util.format('axios.request(options).then(%s', 'function (response) {'))
79
+ code
80
+ .push('axios.request(options).then(function (response) {')
76
81
  .push(1, 'console.log(response.data);')
77
82
  .push('}).catch(%s', 'function (error) {')
78
83
  .push(1, 'console.error(error);')
79
- .push('});')
84
+ .push('});');
80
85
 
81
- return code.join()
82
- }
86
+ return code.join();
87
+ };
83
88
 
84
89
  module.exports.info = {
85
90
  key: 'axios',
86
91
  title: 'Axios',
87
92
  link: 'https://github.com/axios/axios',
88
- description: 'Promise based HTTP client for the browser and node.js'
89
- }
93
+ description: 'Promise based HTTP client for the browser and node.js',
94
+ };
@@ -8,108 +8,106 @@
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
- {
19
- indent: ' ',
20
- credentials: null
21
- },
22
- options
23
- )
24
-
25
- const code = new CodeBuilder(opts.indent)
26
-
27
- options = {
28
- method: source.method
29
- }
15
+ const opts = {
16
+ indent: ' ',
17
+ credentials: null,
18
+ ...options,
19
+ };
20
+
21
+ const code = new CodeBuilder(opts.indent);
22
+
23
+ const reqOptions = {
24
+ method: source.method,
25
+ };
30
26
 
31
27
  if (Object.keys(source.allHeaders).length) {
32
- options.headers = source.allHeaders
28
+ reqOptions.headers = source.allHeaders;
33
29
  }
34
30
 
35
31
  if (opts.credentials !== null) {
36
- options.credentials = opts.credentials
32
+ reqOptions.credentials = opts.credentials;
37
33
  }
38
34
 
39
35
  switch (source.postData.mimeType) {
40
36
  case 'application/x-www-form-urlencoded':
41
- options.body = source.postData.paramsObj
42
- ? source.postData.paramsObj
43
- : source.postData.text
44
- break
37
+ reqOptions.body = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text;
38
+ break;
45
39
 
46
40
  case 'application/json':
47
41
  if (source.postData.jsonObj) {
48
- options.body = opts.useObjectBody ? source.postData.jsonObj : JSON.stringify(source.postData.jsonObj)
42
+ reqOptions.body = source.postData.jsonObj;
49
43
  }
50
- break
44
+ break;
51
45
 
52
46
  case 'multipart/form-data':
53
- code.push('const form = new FormData();')
54
-
55
- source.postData.params.forEach(function (param) {
56
- code.push(
57
- 'form.append(%s, %s);',
58
- JSON.stringify(param.name),
59
- JSON.stringify(param.value || param.fileName || '')
60
- )
61
- })
62
-
63
- code.blank()
64
- break
47
+ if (source.postData.params) {
48
+ code.push('const form = new FormData();');
49
+
50
+ source.postData.params.forEach(function (param) {
51
+ code.push(
52
+ 'form.append(%s, %s);',
53
+ JSON.stringify(param.name),
54
+ JSON.stringify(param.value || param.fileName || '')
55
+ );
56
+ });
57
+
58
+ code.blank();
59
+ }
60
+ break;
65
61
 
66
62
  default:
67
63
  if (source.postData.text) {
68
- options.body = source.postData.text
64
+ reqOptions.body = source.postData.text;
69
65
  }
70
66
  }
71
67
 
72
- code.push('const options = %s;', stringifyObject(options, {
73
- indent: opts.indent,
74
- inlineCharacterLimit: 80,
75
-
76
- // The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
77
- // read, so if you pass the `useObjectBody` option we keep the object as a literal and use
78
- // this transform function to wrap the literal in a `JSON.stringify` call.
79
- transform: (object, property, originalResult) => {
80
- if (property === 'body') {
81
- if (source.postData.mimeType === 'application/x-www-form-urlencoded') {
82
- return `new URLSearchParams(${originalResult})`
83
- }
84
-
85
- if (opts.useObjectBody) {
86
- if (source.postData.mimeType === 'application/json') {
87
- return 'JSON.stringify(' + originalResult + ')'
68
+ code
69
+ .push(
70
+ 'const options = %s;',
71
+ stringifyObject(reqOptions, {
72
+ indent: opts.indent,
73
+ inlineCharacterLimit: 80,
74
+
75
+ // The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
76
+ // read, so we keep the object as a literal and use this transform function to wrap the literal
77
+ // in a `JSON.stringify` call.
78
+ transform: (object, property, originalResult) => {
79
+ if (property === 'body') {
80
+ if (source.postData.mimeType === 'application/x-www-form-urlencoded') {
81
+ return `new URLSearchParams(${originalResult})`;
82
+ } else if (source.postData.mimeType === 'application/json') {
83
+ return `JSON.stringify(${originalResult})`;
84
+ }
88
85
  }
89
- }
90
- }
91
86
 
92
- return originalResult
93
- }
94
- }))
95
- .blank()
87
+ return originalResult;
88
+ },
89
+ })
90
+ )
91
+ .blank();
96
92
 
97
93
  if (source.postData.mimeType === 'multipart/form-data') {
98
- code.push('options.body = form;')
99
- .blank()
94
+ if (source.postData.params) {
95
+ code.push('options.body = form;').blank();
96
+ }
100
97
  }
101
98
 
102
- code.push("fetch('%s', options)", source.fullUrl)
99
+ code
100
+ .push("fetch('%s', options)", source.fullUrl)
103
101
  .push(1, '.then(response => response.json())')
104
102
  .push(1, '.then(response => console.log(response))')
105
- .push(1, '.catch(err => console.error(err));')
103
+ .push(1, '.catch(err => console.error(err));');
106
104
 
107
- return code.join()
108
- }
105
+ return code.join();
106
+ };
109
107
 
110
108
  module.exports.info = {
111
109
  key: 'fetch',
112
110
  title: 'fetch',
113
111
  link: 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch',
114
- description: 'Perform asynchronous HTTP requests with the Fetch API'
115
- }
112
+ description: 'Perform asynchronous HTTP requests with the Fetch API',
113
+ };
@@ -1,15 +1,13 @@
1
- 'use strict'
2
-
3
1
  module.exports = {
4
2
  info: {
5
3
  key: 'javascript',
6
4
  title: 'JavaScript',
7
5
  extname: '.js',
8
- default: 'xhr'
6
+ default: 'xhr',
9
7
  },
10
8
 
11
9
  jquery: require('./jquery'),
12
10
  fetch: require('./fetch'),
13
11
  xhr: require('./xhr'),
14
- axios: require('./axios')
15
- }
12
+ axios: require('./axios'),
13
+ };
@@ -8,76 +8,82 @@
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')
14
- const helpers = require('../../helpers/headers')
11
+ const CodeBuilder = require('../../helpers/code-builder');
12
+ const helpers = require('../../helpers/headers');
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 settings = {
24
23
  async: true,
25
24
  crossDomain: true,
26
25
  url: source.fullUrl,
27
26
  method: source.method,
28
- headers: source.allHeaders
29
- }
27
+ headers: source.allHeaders,
28
+ };
30
29
 
31
30
  switch (source.postData.mimeType) {
32
31
  case 'application/x-www-form-urlencoded':
33
- settings.data = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text
34
- break
32
+ settings.data = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text;
33
+ break;
35
34
 
36
35
  case 'application/json':
37
- settings.processData = false
38
- settings.data = source.postData.text
39
- break
36
+ settings.processData = false;
37
+ settings.data = source.postData.text;
38
+ break;
40
39
 
41
40
  case 'multipart/form-data':
42
- code.push('const form = new FormData();')
43
-
44
- source.postData.params.forEach(function (param) {
45
- code.push('form.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))
46
- })
47
-
48
- settings.processData = false
49
- settings.contentType = false
50
- settings.mimeType = 'multipart/form-data'
51
- settings.data = '[form]'
52
-
53
- // remove the contentType header
54
- if (helpers.hasHeader(settings.headers, 'content-type')) {
55
- if (helpers.getHeader(settings.headers, 'content-type').indexOf('boundary')) {
56
- delete settings.headers[helpers.getHeaderName(settings.headers, 'content-type')]
41
+ if (source.postData.params) {
42
+ code.push('const form = new FormData();');
43
+
44
+ source.postData.params.forEach(function (param) {
45
+ code.push(
46
+ 'form.append(%s, %s);',
47
+ JSON.stringify(param.name),
48
+ JSON.stringify(param.value || param.fileName || '')
49
+ );
50
+ });
51
+
52
+ settings.processData = false;
53
+ settings.contentType = false;
54
+ settings.mimeType = 'multipart/form-data';
55
+ settings.data = '[form]';
56
+
57
+ // remove the contentType header
58
+ if (helpers.hasHeader(settings.headers, 'content-type')) {
59
+ if (helpers.getHeader(settings.headers, 'content-type').indexOf('boundary')) {
60
+ delete settings.headers[helpers.getHeaderName(settings.headers, 'content-type')];
61
+ }
57
62
  }
58
- }
59
63
 
60
- code.blank()
61
- break
64
+ code.blank();
65
+ }
66
+ break;
62
67
 
63
68
  default:
64
69
  if (source.postData.text) {
65
- settings.data = source.postData.text
70
+ settings.data = source.postData.text;
66
71
  }
67
72
  }
68
73
 
69
- code.push('const settings = ' + JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form') + ';')
74
+ code
75
+ .push(`const settings = ${JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form')};`)
70
76
  .blank()
71
77
  .push('$.ajax(settings).done(function (response) {')
72
78
  .push(1, 'console.log(response);')
73
- .push('});')
79
+ .push('});');
74
80
 
75
- return code.join()
76
- }
81
+ return code.join();
82
+ };
77
83
 
78
84
  module.exports.info = {
79
85
  key: 'jquery',
80
86
  title: 'jQuery',
81
87
  link: 'http://api.jquery.com/jquery.ajax/',
82
- description: 'Perform an asynchronous HTTP (Ajax) requests with jQuery'
83
- }
88
+ description: 'Perform an asynchronous HTTP (Ajax) requests with jQuery',
89
+ };
@@ -8,75 +8,79 @@
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')
14
- const helpers = require('../../helpers/headers')
11
+ const CodeBuilder = require('../../helpers/code-builder');
12
+ const helpers = require('../../helpers/headers');
15
13
 
16
14
  module.exports = function (source, options) {
17
- const opts = Object.assign({
15
+ const opts = {
18
16
  indent: ' ',
19
- cors: true
20
- }, options)
17
+ cors: true,
18
+ ...options,
19
+ };
21
20
 
22
- const code = new CodeBuilder(opts.indent)
21
+ const code = new CodeBuilder(opts.indent);
23
22
 
24
23
  switch (source.postData.mimeType) {
25
24
  case 'application/json':
26
- code.push('const data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
27
- .push(null)
28
- break
25
+ code.push('const data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent)).blank();
26
+ break;
29
27
 
30
28
  case 'multipart/form-data':
31
- code.push('const data = new FormData();')
32
-
33
- source.postData.params.forEach(function (param) {
34
- code.push('data.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))
35
- })
36
-
37
- // remove the contentType header
38
- if (helpers.hasHeader(source.allHeaders, 'content-type')) {
39
- if (helpers.getHeader(source.allHeaders, 'content-type').indexOf('boundary')) {
40
- delete source.allHeaders[helpers.getHeaderName(source.allHeaders, 'content-type')]
29
+ if (source.postData.params) {
30
+ code.push('const data = new FormData();');
31
+
32
+ source.postData.params.forEach(function (param) {
33
+ code.push(
34
+ 'data.append(%s, %s);',
35
+ JSON.stringify(param.name),
36
+ JSON.stringify(param.value || param.fileName || '')
37
+ );
38
+ });
39
+
40
+ // remove the contentType header
41
+ if (helpers.hasHeader(source.allHeaders, 'content-type')) {
42
+ if (helpers.getHeader(source.allHeaders, 'content-type').indexOf('boundary')) {
43
+ // eslint-disable-next-line no-param-reassign
44
+ delete source.allHeaders[helpers.getHeaderName(source.allHeaders, 'content-type')];
45
+ }
41
46
  }
42
- }
43
47
 
44
- code.blank()
45
- break
48
+ code.blank();
49
+ }
50
+ break;
46
51
 
47
52
  default:
48
- code.push('const data = %s;', JSON.stringify(source.postData.text || null))
49
- .blank()
53
+ code.push('const data = %s;', JSON.stringify(source.postData.text || null)).blank();
50
54
  }
51
55
 
52
- code.push('const xhr = new XMLHttpRequest();')
56
+ code.push('const xhr = new XMLHttpRequest();');
53
57
 
54
58
  if (opts.cors) {
55
- code.push('xhr.withCredentials = true;')
59
+ code.push('xhr.withCredentials = true;');
56
60
  }
57
61
 
58
- code.blank()
62
+ code
63
+ .blank()
59
64
  .push('xhr.addEventListener("readystatechange", function () {')
60
65
  .push(1, 'if (this.readyState === this.DONE) {')
61
66
  .push(2, 'console.log(this.responseText);')
62
67
  .push(1, '}')
63
68
  .push('});')
64
69
  .blank()
65
- .push('xhr.open(%s, %s);', JSON.stringify(source.method), JSON.stringify(source.fullUrl))
70
+ .push('xhr.open(%s, %s);', JSON.stringify(source.method), JSON.stringify(source.fullUrl));
66
71
 
67
72
  Object.keys(source.allHeaders).forEach(function (key) {
68
- code.push('xhr.setRequestHeader(%s, %s);', JSON.stringify(key), JSON.stringify(source.allHeaders[key]))
69
- })
73
+ code.push('xhr.setRequestHeader(%s, %s);', JSON.stringify(key), JSON.stringify(source.allHeaders[key]));
74
+ });
70
75
 
71
- code.blank()
72
- .push('xhr.send(data);')
76
+ code.blank().push('xhr.send(data);');
73
77
 
74
- return code.join()
75
- }
78
+ return code.join();
79
+ };
76
80
 
77
81
  module.exports.info = {
78
82
  key: 'xhr',
79
83
  title: 'XMLHttpRequest',
80
84
  link: 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest',
81
- description: 'W3C Standard API that provides scripted client functionality'
82
- }
85
+ description: 'W3C Standard API that provides scripted client functionality',
86
+ };
@@ -1,12 +1,10 @@
1
- 'use strict'
2
-
3
1
  module.exports = {
4
2
  info: {
5
3
  key: 'kotlin',
6
4
  title: 'Kotlin',
7
5
  extname: '.kt',
8
- default: 'okhttp'
6
+ default: 'okhttp',
9
7
  },
10
8
 
11
- okhttp: require('./okhttp')
12
- }
9
+ okhttp: require('./okhttp'),
10
+ };