@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,64 +8,66 @@
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 methods = ['get', 'post', 'head', 'delete', 'patch', 'put', 'options']
21
- const code = new CodeBuilder(opts.indent)
19
+ const methods = ['get', 'post', 'head', 'delete', 'patch', 'put', 'options'];
20
+ const code = new CodeBuilder(opts.indent);
22
21
 
23
- code.push('open Cohttp_lwt_unix')
22
+ code
23
+ .push('open Cohttp_lwt_unix')
24
24
  .push('open Cohttp')
25
25
  .push('open Lwt')
26
26
  .blank()
27
- .push('let uri = Uri.of_string "%s" in', source.fullUrl)
27
+ .push('let uri = Uri.of_string "%s" in', source.fullUrl);
28
28
 
29
29
  // Add headers, including the cookies
30
- const headers = Object.keys(source.allHeaders)
30
+ const headers = Object.keys(source.allHeaders);
31
31
 
32
32
  if (headers.length === 1) {
33
- code.push('let headers = Header.add (Header.init ()) "%s" "%s" in', headers[0], source.allHeaders[headers[0]])
33
+ code.push('let headers = Header.add (Header.init ()) "%s" "%s" in', headers[0], source.allHeaders[headers[0]]);
34
34
  } else if (headers.length > 1) {
35
- code.push('let headers = Header.add_list (Header.init ()) [')
35
+ code.push('let headers = Header.add_list (Header.init ()) [');
36
36
 
37
37
  headers.forEach(function (key) {
38
- code.push(1, '("%s", "%s");', key, source.allHeaders[key])
39
- })
38
+ code.push(1, '("%s", "%s");', key, source.allHeaders[key]);
39
+ });
40
40
 
41
- code.push('] in')
41
+ code.push('] in');
42
42
  }
43
43
 
44
44
  // Add body
45
45
  if (source.postData.text) {
46
46
  // Just text
47
- code.push('let body = Cohttp_lwt_body.of_string %s in', JSON.stringify(source.postData.text))
47
+ code.push('let body = Cohttp_lwt_body.of_string %s in', JSON.stringify(source.postData.text));
48
48
  }
49
49
 
50
50
  // Do the request
51
- code.blank()
51
+ code.blank();
52
52
 
53
- code.push('Client.call %s%s%s uri',
53
+ code.push(
54
+ 'Client.call %s%s%s uri',
54
55
  headers.length ? '~headers ' : '',
55
56
  source.postData.text ? '~body ' : '',
56
- (methods.indexOf(source.method.toLowerCase()) >= 0 ? ('`' + source.method.toUpperCase()) : '(Code.method_of_string "' + source.method + '")')
57
- )
57
+ methods.indexOf(source.method.toLowerCase()) >= 0
58
+ ? `\`${source.method.toUpperCase()}`
59
+ : `(Code.method_of_string "${source.method}")`
60
+ );
58
61
 
59
62
  // Catch result
60
- code.push('>>= fun (res, body_stream) ->')
61
- .push(1, '(* Do stuff with the result *)')
63
+ code.push('>>= fun (res, body_stream) ->').push(1, '(* Do stuff with the result *)');
62
64
 
63
- return code.join()
64
- }
65
+ return code.join();
66
+ };
65
67
 
66
68
  module.exports.info = {
67
69
  key: 'cohttp',
68
70
  title: 'CoHTTP',
69
71
  link: 'https://github.com/mirage/ocaml-cohttp',
70
- description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml'
71
- }
72
+ description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml',
73
+ };
@@ -1,12 +1,10 @@
1
- 'use strict'
2
-
3
1
  module.exports = {
4
2
  info: {
5
3
  key: 'ocaml',
6
4
  title: 'OCaml',
7
5
  extname: '.ml',
8
- default: 'cohttp'
6
+ default: 'cohttp',
9
7
  },
10
8
 
11
- cohttp: require('./cohttp')
12
- }
9
+ cohttp: require('./cohttp'),
10
+ };
@@ -8,101 +8,108 @@
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 util = require('util')
14
- const CodeBuilder = require('../../helpers/code-builder')
11
+ const { format } = require('util');
12
+ const CodeBuilder = require('../../helpers/code-builder');
15
13
 
16
14
  module.exports = function (source, options) {
17
- const opts = Object.assign({
15
+ const opts = {
18
16
  closingTag: false,
19
17
  indent: ' ',
20
18
  maxRedirects: 10,
21
19
  namedErrors: false,
22
20
  noTags: false,
23
21
  shortTags: false,
24
- timeout: 30
25
- }, options)
22
+ timeout: 30,
23
+ ...options,
24
+ };
26
25
 
27
- const code = new CodeBuilder(opts.indent)
26
+ const code = new CodeBuilder(opts.indent);
28
27
 
29
28
  if (!opts.noTags) {
30
- code.push(opts.shortTags ? '<?' : '<?php')
31
- .blank()
29
+ code.push(opts.shortTags ? '<?' : '<?php').blank();
32
30
  }
33
31
 
34
- code.push('$curl = curl_init();')
35
- .blank()
36
-
37
- const curlOptions = [{
38
- escape: true,
39
- name: 'CURLOPT_PORT',
40
- value: source.uriObj.port
41
- }, {
42
- escape: true,
43
- name: 'CURLOPT_URL',
44
- value: source.fullUrl
45
- }, {
46
- escape: false,
47
- name: 'CURLOPT_RETURNTRANSFER',
48
- value: 'true'
49
- }, {
50
- escape: true,
51
- name: 'CURLOPT_ENCODING',
52
- value: ''
53
- }, {
54
- escape: false,
55
- name: 'CURLOPT_MAXREDIRS',
56
- value: opts.maxRedirects
57
- }, {
58
- escape: false,
59
- name: 'CURLOPT_TIMEOUT',
60
- value: opts.timeout
61
- }, {
62
- escape: false,
63
- name: 'CURLOPT_HTTP_VERSION',
64
- value: source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1'
65
- }, {
66
- escape: true,
67
- name: 'CURLOPT_CUSTOMREQUEST',
68
- value: source.method
69
- }, {
70
- escape: true,
71
- name: 'CURLOPT_POSTFIELDS',
72
- value: source.postData ? source.postData.text : undefined
73
- }]
74
-
75
- code.push('curl_setopt_array($curl, [')
76
-
77
- const curlopts = new CodeBuilder(opts.indent, '\n' + opts.indent)
32
+ code.push('$curl = curl_init();').blank();
33
+
34
+ const curlOptions = [
35
+ {
36
+ escape: true,
37
+ name: 'CURLOPT_PORT',
38
+ value: source.uriObj.port,
39
+ },
40
+ {
41
+ escape: true,
42
+ name: 'CURLOPT_URL',
43
+ value: source.fullUrl,
44
+ },
45
+ {
46
+ escape: false,
47
+ name: 'CURLOPT_RETURNTRANSFER',
48
+ value: 'true',
49
+ },
50
+ {
51
+ escape: true,
52
+ name: 'CURLOPT_ENCODING',
53
+ value: '',
54
+ },
55
+ {
56
+ escape: false,
57
+ name: 'CURLOPT_MAXREDIRS',
58
+ value: opts.maxRedirects,
59
+ },
60
+ {
61
+ escape: false,
62
+ name: 'CURLOPT_TIMEOUT',
63
+ value: opts.timeout,
64
+ },
65
+ {
66
+ escape: false,
67
+ name: 'CURLOPT_HTTP_VERSION',
68
+ value: source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1',
69
+ },
70
+ {
71
+ escape: true,
72
+ name: 'CURLOPT_CUSTOMREQUEST',
73
+ value: source.method,
74
+ },
75
+ {
76
+ escape: true,
77
+ name: 'CURLOPT_POSTFIELDS',
78
+ value: source.postData ? source.postData.text : undefined,
79
+ },
80
+ ];
81
+
82
+ code.push('curl_setopt_array($curl, [');
83
+
84
+ const curlopts = new CodeBuilder(opts.indent, `\n${opts.indent}`);
78
85
 
79
86
  curlOptions.forEach(function (option) {
80
- if (!~[null, undefined].indexOf(option.value)) {
81
- curlopts.push(util.format('%s => %s,', option.name, option.escape ? JSON.stringify(option.value) : option.value))
87
+ if (![null, undefined].includes(option.value)) {
88
+ curlopts.push(format('%s => %s,', option.name, option.escape ? JSON.stringify(option.value) : option.value));
82
89
  }
83
- })
90
+ });
84
91
 
85
92
  // construct cookies
86
- const cookies = source.cookies.map(function (cookie) {
87
- return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value)
88
- })
89
-
90
- if (cookies.length) {
91
- curlopts.push(util.format('CURLOPT_COOKIE => "%s",', cookies.join('; ')))
93
+ if (source.allHeaders.cookie) {
94
+ curlopts.push(format('CURLOPT_COOKIE => "%s",', source.allHeaders.cookie));
92
95
  }
93
96
 
94
97
  // construct cookies
95
- const headers = Object.keys(source.headersObj).sort().map(function (key) {
96
- return util.format('"%s: %s"', key, source.headersObj[key])
97
- })
98
+ const headers = Object.keys(source.headersObj)
99
+ .sort()
100
+ .map(function (key) {
101
+ return format('"%s: %s"', key, source.headersObj[key]);
102
+ });
98
103
 
99
104
  if (headers.length) {
100
- curlopts.push('CURLOPT_HTTPHEADER => [')
101
- .push(1, headers.join(',\n' + opts.indent + opts.indent))
102
- .push('],')
105
+ curlopts
106
+ .push('CURLOPT_HTTPHEADER => [')
107
+ .push(1, headers.join(`,\n${opts.indent}${opts.indent}`))
108
+ .push('],');
103
109
  }
104
110
 
105
- code.push(1, curlopts.join())
111
+ code
112
+ .push(1, curlopts.join())
106
113
  .push(']);')
107
114
  .blank()
108
115
  .push('$response = curl_exec($curl);')
@@ -110,29 +117,26 @@ module.exports = function (source, options) {
110
117
  .blank()
111
118
  .push('curl_close($curl);')
112
119
  .blank()
113
- .push('if ($err) {')
120
+ .push('if ($err) {');
114
121
 
115
122
  if (opts.namedErrors) {
116
- code.push(1, 'echo array_flip(get_defined_constants(true)["curl"])[$err];')
123
+ code.push(1, 'echo array_flip(get_defined_constants(true)["curl"])[$err];');
117
124
  } else {
118
- code.push(1, 'echo "cURL Error #:" . $err;')
125
+ code.push(1, 'echo "cURL Error #:" . $err;');
119
126
  }
120
127
 
121
- code.push('} else {')
122
- .push(1, 'echo $response;')
123
- .push('}')
128
+ code.push('} else {').push(1, 'echo $response;').push('}');
124
129
 
125
130
  if (!opts.noTags && opts.closingTag) {
126
- code.blank()
127
- .push('?>')
131
+ code.blank().push('?>');
128
132
  }
129
133
 
130
- return code.join()
131
- }
134
+ return code.join();
135
+ };
132
136
 
133
137
  module.exports.info = {
134
138
  key: 'curl',
135
139
  title: 'cURL',
136
140
  link: 'http://php.net/manual/en/book.curl.php',
137
- description: 'PHP with ext-curl'
138
- }
141
+ description: 'PHP with ext-curl',
142
+ };
@@ -0,0 +1,135 @@
1
+ /**
2
+ * @description
3
+ * HTTP code snippet generator for PHP using Guzzle.
4
+ *
5
+ * @author
6
+ * @RobertoArruda
7
+ *
8
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
+ */
10
+
11
+ const { format } = require('util');
12
+ const helpers = require('./helpers');
13
+ const headerHelpers = require('../../helpers/headers');
14
+ const CodeBuilder = require('../../helpers/code-builder');
15
+
16
+ module.exports = function (source, options) {
17
+ const opts = {
18
+ closingTag: false,
19
+ indent: ' ',
20
+ noTags: false,
21
+ shortTags: false,
22
+ ...options,
23
+ };
24
+
25
+ const code = new CodeBuilder(opts.indent);
26
+
27
+ if (!opts.noTags) {
28
+ code.push(opts.shortTags ? '<?' : '<?php');
29
+ }
30
+
31
+ code.push("require_once('vendor/autoload.php');").blank();
32
+
33
+ const requestOptions = new CodeBuilder(opts.indent);
34
+
35
+ switch (source.postData.mimeType) {
36
+ case 'application/x-www-form-urlencoded':
37
+ requestOptions.push(
38
+ 1,
39
+ "'form_params' => %s,",
40
+ helpers.convert(source.postData.paramsObj, opts.indent + opts.indent, opts.indent)
41
+ );
42
+ break;
43
+
44
+ case 'multipart/form-data': {
45
+ if (source.postData.params) {
46
+ const fields = [];
47
+
48
+ source.postData.params.forEach(function (param) {
49
+ if (param.fileName) {
50
+ const field = {
51
+ name: param.name,
52
+ filename: param.fileName,
53
+ contents: param.value,
54
+ };
55
+
56
+ if (param.contentType) {
57
+ field.headers = { 'Content-Type': param.contentType };
58
+ }
59
+
60
+ fields.push(field);
61
+ } else if (param.value) {
62
+ fields.push({
63
+ name: param.name,
64
+ contents: param.value,
65
+ });
66
+ }
67
+ });
68
+
69
+ if (fields.length) {
70
+ requestOptions.push(1, "'multipart' => %s", helpers.convert(fields, opts.indent + opts.indent, opts.indent));
71
+ }
72
+
73
+ // Guzzle adds its own boundary for multipart requests.
74
+ if (headerHelpers.hasHeader(source.headersObj, 'content-type')) {
75
+ if (headerHelpers.getHeader(source.headersObj, 'content-type').includes('boundary')) {
76
+ // eslint-disable-next-line no-param-reassign
77
+ delete source.headersObj[headerHelpers.getHeaderName(source.headersObj, 'content-type')];
78
+ }
79
+ }
80
+ }
81
+ break;
82
+ }
83
+
84
+ default:
85
+ if (source.postData.text) {
86
+ requestOptions.push(1, "'body' => %s,", helpers.convert(source.postData.text));
87
+ }
88
+ }
89
+
90
+ // construct headers
91
+ const headers = Object.keys(source.headersObj)
92
+ .sort()
93
+ .map(function (key) {
94
+ return opts.indent + opts.indent + format("'%s' => '%s',", key, source.headersObj[key]);
95
+ });
96
+
97
+ // construct cookies
98
+ const cookies = source.cookies.map(function (cookie) {
99
+ return `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`;
100
+ });
101
+
102
+ if (cookies.length) {
103
+ headers.push(opts.indent + opts.indent + format("'cookie' => '%s',", cookies.join('; ')));
104
+ }
105
+
106
+ if (headers.length) {
107
+ requestOptions.push(1, "'headers' => [").push(headers.join('\n')).push(1, '],');
108
+ }
109
+
110
+ code.push('$client = new \\GuzzleHttp\\Client();').blank();
111
+
112
+ if (requestOptions.code.length) {
113
+ code
114
+ .push(`$response = $client->request('${source.method}', '${source.fullUrl}', [`)
115
+ .push(requestOptions.join(','))
116
+ .push(']);');
117
+ } else {
118
+ code.push(`$response = $client->request('${source.method}', '${source.fullUrl}');`);
119
+ }
120
+
121
+ code.blank().push('echo $response->getBody();');
122
+
123
+ if (!opts.noTags && opts.closingTag) {
124
+ code.blank().push('?>');
125
+ }
126
+
127
+ return code.join();
128
+ };
129
+
130
+ module.exports.info = {
131
+ key: 'guzzle',
132
+ title: 'Guzzle v7',
133
+ link: 'http://docs.guzzlephp.org/en/stable/',
134
+ description: 'PHP with guzzle v7',
135
+ };
@@ -1,59 +1,60 @@
1
- 'use strict'
2
-
3
1
  const convert = function (obj, indent, lastIndent) {
4
- let i, result
2
+ let i;
3
+ let result;
5
4
 
6
5
  if (!lastIndent) {
7
- lastIndent = ''
6
+ // eslint-disable-next-line no-param-reassign
7
+ lastIndent = '';
8
8
  }
9
9
 
10
10
  switch (Object.prototype.toString.call(obj)) {
11
11
  case '[object Null]':
12
- result = 'null'
13
- break
12
+ result = 'null';
13
+ break;
14
14
 
15
15
  case '[object Undefined]':
16
- result = 'null'
17
- break
16
+ result = 'null';
17
+ break;
18
18
 
19
19
  case '[object String]':
20
- result = "'" + obj.replace(/\\/g, '\\\\').replace(/'/g, "'") + "'"
21
- break
20
+ result = `'${obj.replace(/\\/g, '\\\\').replace(/'/g, "'")}'`;
21
+ break;
22
22
 
23
23
  case '[object Number]':
24
- result = obj.toString()
25
- break
24
+ result = obj.toString();
25
+ break;
26
26
 
27
27
  case '[object Array]':
28
- result = []
28
+ result = [];
29
29
 
30
30
  obj.forEach(function (item) {
31
- result.push(convert(item, indent + indent, indent))
32
- })
31
+ result.push(convert(item, indent + indent, indent));
32
+ });
33
33
 
34
- result = '[\n' + indent + result.join(',\n' + indent) + '\n' + lastIndent + ']'
35
- break
34
+ result = `[\n${indent}${result.join(`,\n${indent}`)}\n${lastIndent}]`;
35
+ break;
36
36
 
37
37
  case '[object Object]':
38
- result = []
38
+ result = [];
39
+ // eslint-disable-next-line no-restricted-syntax
39
40
  for (i in obj) {
40
41
  // eslint-disable-next-line no-prototype-builtins
41
42
  if (obj.hasOwnProperty(i)) {
42
- result.push(convert(i, indent) + ' => ' + convert(obj[i], indent + indent, indent))
43
+ result.push(`${convert(i, indent)} => ${convert(obj[i], indent + indent, indent)}`);
43
44
  }
44
45
  }
45
- result = '[\n' + indent + result.join(',\n' + indent) + '\n' + lastIndent + ']'
46
- break
46
+ result = `[\n${indent}${result.join(`,\n${indent}`)}\n${lastIndent}]`;
47
+ break;
47
48
 
48
49
  default:
49
- result = 'null'
50
+ result = 'null';
50
51
  }
51
52
 
52
- return result
53
- }
53
+ return result;
54
+ };
54
55
 
55
56
  module.exports = {
56
- convert: convert,
57
+ convert,
57
58
  methods: [
58
59
  'ACL',
59
60
  'BASELINE_CONTROL',
@@ -81,6 +82,6 @@ module.exports = {
81
82
  'UNCHECKOUT',
82
83
  'UNLOCK',
83
84
  'UPDATE',
84
- 'VERSION_CONTROL'
85
- ]
86
- }
85
+ 'VERSION_CONTROL',
86
+ ],
87
+ };