@readme/httpsnippet 2.4.5 → 3.0.1

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 (69) hide show
  1. package/README.md +19 -9
  2. package/package.json +24 -27
  3. package/src/helpers/code-builder.js +85 -80
  4. package/src/helpers/form-data.js +28 -26
  5. package/src/helpers/headers.js +18 -8
  6. package/src/helpers/reducer.js +10 -14
  7. package/src/index.js +175 -151
  8. package/src/targets/c/index.js +3 -5
  9. package/src/targets/c/libcurl.js +20 -25
  10. package/src/targets/clojure/clj_http.js +126 -102
  11. package/src/targets/clojure/index.js +3 -5
  12. package/src/targets/csharp/httpclient.js +93 -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 +47 -54
  17. package/src/targets/http/http1.1.js +27 -37
  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 +47 -42
  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 +50 -44
  29. package/src/targets/javascript/xhr.js +48 -44
  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 +37 -34
  33. package/src/targets/node/fetch.js +94 -87
  34. package/src/targets/node/index.js +4 -5
  35. package/src/targets/node/native.js +51 -45
  36. package/src/targets/node/request.js +69 -69
  37. package/src/targets/node/unirest.js +70 -68
  38. package/src/targets/objc/helpers.js +32 -25
  39. package/src/targets/objc/index.js +3 -5
  40. package/src/targets/objc/nsurlsession.js +108 -79
  41. package/src/targets/ocaml/cohttp.js +32 -30
  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 +31 -29
  46. package/src/targets/php/http1.js +38 -49
  47. package/src/targets/php/http2.js +75 -75
  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 +42 -37
  54. package/src/targets/python/index.js +4 -5
  55. package/src/targets/python/python3.js +33 -45
  56. package/src/targets/python/requests.js +48 -72
  57. package/src/targets/r/httr.js +60 -73
  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 +41 -35
  61. package/src/targets/shell/curl.js +108 -43
  62. package/src/{helpers/shell.js → 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 +44 -35
  67. package/src/targets/swift/index.js +3 -5
  68. package/src/targets/swift/nsurlsession.js +93 -79
  69. package/src/.DS_Store +0 -0
@@ -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,58 +1,60 @@
1
- 'use strict'
2
-
3
- var convert = function (obj, indent, lastIndent) {
4
- var i, result
1
+ const convert = function (obj, indent, lastIndent) {
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) {
41
+ // eslint-disable-next-line no-prototype-builtins
40
42
  if (obj.hasOwnProperty(i)) {
41
- result.push(convert(i, indent) + ' => ' + convert(obj[i], indent + indent, indent))
43
+ result.push(`${convert(i, indent)} => ${convert(obj[i], indent + indent, indent)}`);
42
44
  }
43
45
  }
44
- result = '[\n' + indent + result.join(',\n' + indent) + '\n' + lastIndent + ']'
45
- break
46
+ result = `[\n${indent}${result.join(`,\n${indent}`)}\n${lastIndent}]`;
47
+ break;
46
48
 
47
49
  default:
48
- result = 'null'
50
+ result = 'null';
49
51
  }
50
52
 
51
- return result
52
- }
53
+ return result;
54
+ };
53
55
 
54
56
  module.exports = {
55
- convert: convert,
57
+ convert,
56
58
  methods: [
57
59
  'ACL',
58
60
  'BASELINE_CONTROL',
@@ -80,6 +82,6 @@ module.exports = {
80
82
  'UNCHECKOUT',
81
83
  'UNLOCK',
82
84
  'UPDATE',
83
- 'VERSION_CONTROL'
84
- ]
85
- }
85
+ 'VERSION_CONTROL',
86
+ ],
87
+ };
@@ -8,89 +8,78 @@
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
- var helpers = require('./helpers')
14
- var CodeBuilder = require('../../helpers/code-builder')
11
+ const helpers = require('./helpers');
12
+ const CodeBuilder = require('../../helpers/code-builder');
15
13
 
16
14
  module.exports = function (source, options) {
17
- var opts = Object.assign({
15
+ const opts = {
18
16
  closingTag: false,
19
17
  indent: ' ',
20
18
  noTags: false,
21
- shortTags: false
22
- }, options)
19
+ shortTags: false,
20
+ ...options,
21
+ };
23
22
 
24
- var code = new CodeBuilder(opts.indent)
23
+ const code = new CodeBuilder(opts.indent);
25
24
 
26
25
  if (!opts.noTags) {
27
- code.push(opts.shortTags ? '<?' : '<?php')
28
- .blank()
26
+ code.push(opts.shortTags ? '<?' : '<?php').blank();
29
27
  }
30
28
 
31
- if (!~helpers.methods.indexOf(source.method.toUpperCase())) {
32
- code.push('HttpRequest::methodRegister(\'%s\');', source.method)
29
+ if (!helpers.methods.includes(source.method.toUpperCase())) {
30
+ code.push("HttpRequest::methodRegister('%s');", source.method);
33
31
  }
34
32
 
35
- code.push('$request = new HttpRequest();')
36
- .push('$request->setUrl(%s);', helpers.convert(source.url))
33
+ code.push('$request = new HttpRequest();').push('$request->setUrl(%s);', helpers.convert(source.url));
37
34
 
38
- if (~helpers.methods.indexOf(source.method.toUpperCase())) {
39
- code.push('$request->setMethod(HTTP_METH_%s);', source.method.toUpperCase())
35
+ if (helpers.methods.includes(source.method.toUpperCase())) {
36
+ code.push('$request->setMethod(HTTP_METH_%s);', source.method.toUpperCase());
40
37
  } else {
41
- code.push('$request->setMethod(HttpRequest::HTTP_METH_%s);', source.method.toUpperCase())
38
+ code.push('$request->setMethod(HttpRequest::HTTP_METH_%s);', source.method.toUpperCase());
42
39
  }
43
40
 
44
- code.blank()
41
+ code.blank();
45
42
 
46
43
  if (Object.keys(source.queryObj).length) {
47
- code.push('$request->setQueryData(%s);', helpers.convert(source.queryObj, opts.indent))
48
- .blank()
44
+ code.push('$request->setQueryData(%s);', helpers.convert(source.queryObj, opts.indent)).blank();
49
45
  }
50
46
 
51
47
  if (Object.keys(source.headersObj).length) {
52
- code.push('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent))
53
- .blank()
48
+ code.push('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent)).blank();
54
49
  }
55
50
 
56
51
  if (Object.keys(source.cookiesObj).length) {
57
- code.push('$request->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent))
58
- .blank()
52
+ code.push('$request->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent)).blank();
59
53
  }
60
54
 
61
- switch (source.postData.mimeType) {
62
- case 'application/x-www-form-urlencoded':
63
- code.push('$request->setContentType(%s);', helpers.convert(source.postData.mimeType))
64
- .push('$request->setPostFields(%s);', helpers.convert(source.postData.paramsObj, opts.indent))
65
- .blank()
66
- break
67
-
68
- default:
69
- if (source.postData.text) {
70
- code.push('$request->setBody(%s);', helpers.convert(source.postData.text))
71
- .blank()
72
- }
55
+ if (source.postData.mimeType === 'application/x-www-form-urlencoded') {
56
+ code
57
+ .push('$request->setContentType(%s);', helpers.convert(source.postData.mimeType))
58
+ .push('$request->setPostFields(%s);', helpers.convert(source.postData.paramsObj, opts.indent))
59
+ .blank();
60
+ } else if (source.postData.text) {
61
+ code.push('$request->setBody(%s);', helpers.convert(source.postData.text)).blank();
73
62
  }
74
63
 
75
- code.push('try {')
76
- .push(1, '$response = $request->send();')
77
- .blank()
78
- .push(1, 'echo $response->getBody();')
79
- .push('} catch (HttpException $ex) {')
80
- .push(1, 'echo $ex;')
81
- .push('}')
64
+ code
65
+ .push('try {')
66
+ .push(1, '$response = $request->send();')
67
+ .blank()
68
+ .push(1, 'echo $response->getBody();')
69
+ .push('} catch (HttpException $ex) {')
70
+ .push(1, 'echo $ex;')
71
+ .push('}');
82
72
 
83
73
  if (!opts.noTags && opts.closingTag) {
84
- code.blank()
85
- .push('?>')
74
+ code.blank().push('?>');
86
75
  }
87
76
 
88
- return code.join()
89
- }
77
+ return code.join();
78
+ };
90
79
 
91
80
  module.exports.info = {
92
81
  key: 'http1',
93
82
  title: 'HTTP v1',
94
83
  link: 'http://php.net/manual/en/book.http.php',
95
- description: 'PHP with pecl/http v1'
96
- }
84
+ description: 'PHP with pecl/http v1',
85
+ };
@@ -8,124 +8,124 @@
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
- var helpers = require('./helpers')
14
- var headerHelpers = require('../../helpers/headers')
15
- var CodeBuilder = require('../../helpers/code-builder')
11
+ const helpers = require('./helpers');
12
+ const headerHelpers = require('../../helpers/headers');
13
+ const CodeBuilder = require('../../helpers/code-builder');
16
14
 
17
15
  module.exports = function (source, options) {
18
- var opts = Object.assign({
16
+ const opts = {
19
17
  closingTag: false,
20
18
  indent: ' ',
21
19
  noTags: false,
22
- shortTags: false
23
- }, options)
20
+ shortTags: false,
21
+ ...options,
22
+ };
24
23
 
25
- var code = new CodeBuilder(opts.indent)
26
- var hasBody = false
24
+ const code = new CodeBuilder(opts.indent);
25
+ let hasBody = false;
27
26
 
28
27
  if (!opts.noTags) {
29
- code.push(opts.shortTags ? '<?' : '<?php')
30
- .blank()
28
+ code.push(opts.shortTags ? '<?' : '<?php').blank();
31
29
  }
32
30
 
33
- code.push('$client = new http\\Client;')
34
- .push('$request = new http\\Client\\Request;')
35
- .blank()
31
+ code.push('$client = new http\\Client;').push('$request = new http\\Client\\Request;').blank();
36
32
 
37
33
  switch (source.postData.mimeType) {
38
34
  case 'application/x-www-form-urlencoded':
39
- code.push('$body = new http\\Message\\Body;')
40
- .push('$body->append(new http\\QueryString(%s));', helpers.convert(source.postData.paramsObj, opts.indent))
41
- .blank()
42
- hasBody = true
43
- break
44
-
45
- case 'multipart/form-data':
46
- var files = []
47
- var fields = {}
48
-
49
- source.postData.params.forEach(function (param) {
50
- if (param.fileName) {
51
- files.push({
52
- name: param.name,
53
- type: param.contentType,
54
- file: param.fileName,
55
- data: param.value
56
- })
57
- } else if (param.value) {
58
- fields[param.name] = param.value
59
- }
60
- })
61
-
62
- code.push('$body = new http\\Message\\Body;')
63
- .push('$body->addForm(%s, %s);',
35
+ code
36
+ .push('$body = new http\\Message\\Body;')
37
+ .push('$body->append(new http\\QueryString(%s));', helpers.convert(source.postData.paramsObj, opts.indent))
38
+ .blank();
39
+ hasBody = true;
40
+ break;
41
+
42
+ case 'multipart/form-data': {
43
+ if (source.postData.params) {
44
+ const files = [];
45
+ const fields = {};
46
+
47
+ source.postData.params.forEach(function (param) {
48
+ if (param.fileName) {
49
+ files.push({
50
+ name: param.name,
51
+ type: param.contentType,
52
+ file: param.fileName,
53
+ data: param.value,
54
+ });
55
+ } else if (param.value) {
56
+ fields[param.name] = param.value;
57
+ }
58
+ });
59
+
60
+ code
61
+ .push('$body = new http\\Message\\Body;')
62
+ .push(
63
+ '$body->addForm(%s, %s);',
64
64
  Object.keys(fields).length ? helpers.convert(fields, opts.indent) : 'null',
65
65
  files.length ? helpers.convert(files, opts.indent) : 'null'
66
- )
67
-
68
- // remove the contentType header
69
- if (headerHelpers.hasHeader(source.headersObj, 'content-type')) {
70
- if (headerHelpers.getHeader(source.headersObj, 'content-type').indexOf('boundary')) {
71
- delete source.headersObj[headerHelpers.getHeaderName(source.headersObj, 'content-type')]
66
+ );
67
+
68
+ // remove the contentType header
69
+ if (headerHelpers.hasHeader(source.headersObj, 'content-type')) {
70
+ if (headerHelpers.getHeader(source.headersObj, 'content-type').includes('boundary')) {
71
+ // eslint-disable-next-line no-param-reassign
72
+ delete source.headersObj[headerHelpers.getHeaderName(source.headersObj, 'content-type')];
73
+ }
72
74
  }
73
- }
74
75
 
75
- code.blank()
76
+ code.blank();
76
77
 
77
- hasBody = true
78
- break
78
+ hasBody = true;
79
+ }
80
+ break;
81
+ }
79
82
 
80
83
  default:
81
84
  if (source.postData.text) {
82
- code.push('$body = new http\\Message\\Body;')
83
- .push('$body->append(%s);', helpers.convert(source.postData.text))
84
- .blank()
85
- hasBody = true
85
+ code
86
+ .push('$body = new http\\Message\\Body;')
87
+ .push('$body->append(%s);', helpers.convert(source.postData.text))
88
+ .blank();
89
+ hasBody = true;
86
90
  }
87
91
  }
88
92
 
89
- code.push('$request->setRequestUrl(%s);', helpers.convert(source.url))
90
- .push('$request->setRequestMethod(%s);', helpers.convert(source.method))
93
+ code
94
+ .push('$request->setRequestUrl(%s);', helpers.convert(source.url))
95
+ .push('$request->setRequestMethod(%s);', helpers.convert(source.method));
91
96
 
92
97
  if (hasBody) {
93
- code.push('$request->setBody($body);')
94
- .blank()
98
+ code.push('$request->setBody($body);').blank();
95
99
  }
96
100
 
97
101
  if (Object.keys(source.queryObj).length) {
98
- code.push('$request->setQuery(new http\\QueryString(%s));', helpers.convert(source.queryObj, opts.indent))
99
- .blank()
102
+ code.push('$request->setQuery(new http\\QueryString(%s));', helpers.convert(source.queryObj, opts.indent)).blank();
100
103
  }
101
104
 
102
105
  if (Object.keys(source.headersObj).length) {
103
- code.push('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent))
104
- .blank()
106
+ code.push('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent)).blank();
105
107
  }
106
108
 
107
109
  if (Object.keys(source.cookiesObj).length) {
108
- code.blank()
109
- .push('$client->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent))
110
- .blank()
110
+ code.blank().push('$client->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent)).blank();
111
111
  }
112
112
 
113
- code.push('$client->enqueue($request)->send();')
114
- .push('$response = $client->getResponse();')
115
- .blank()
116
- .push('echo $response->getBody();')
113
+ code
114
+ .push('$client->enqueue($request)->send();')
115
+ .push('$response = $client->getResponse();')
116
+ .blank()
117
+ .push('echo $response->getBody();');
117
118
 
118
119
  if (!opts.noTags && opts.closingTag) {
119
- code.blank()
120
- .push('?>')
120
+ code.blank().push('?>');
121
121
  }
122
122
 
123
- return code.join()
124
- }
123
+ return code.join();
124
+ };
125
125
 
126
126
  module.exports.info = {
127
127
  key: 'http2',
128
128
  title: 'HTTP v2',
129
129
  link: 'http://devel-m6w6.rhcloud.com/mdref/http',
130
- description: 'PHP with pecl/http v2'
131
- }
130
+ description: 'PHP with pecl/http v2',
131
+ };
@@ -1,14 +1,14 @@
1
- 'use strict'
2
-
3
1
  module.exports = {
4
2
  info: {
5
3
  key: 'php',
6
4
  title: 'PHP',
7
5
  extname: '.php',
8
- default: 'curl'
6
+ default: 'curl',
7
+ cli: 'php %s',
9
8
  },
10
9
 
11
10
  curl: require('./curl'),
12
11
  http1: require('./http1'),
13
- http2: require('./http2')
14
- }
12
+ http2: require('./http2'),
13
+ guzzle: require('./guzzle'),
14
+ };
@@ -1,55 +1,60 @@
1
- 'use strict'
2
-
3
- var CodeBuilder = require('../../helpers/code-builder')
4
- var helpers = require('../../helpers/headers')
1
+ const CodeBuilder = require('../../helpers/code-builder');
2
+ const helpers = require('../../helpers/headers');
5
3
 
6
4
  module.exports = function (command) {
7
- return function (source, options) {
8
- var code = new CodeBuilder()
9
- var methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']
5
+ return function (source) {
6
+ const code = new CodeBuilder();
7
+ const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
10
8
 
11
- if (methods.indexOf(source.method.toUpperCase()) === -1) {
12
- return 'Method not supported'
9
+ if (!methods.includes(source.method.toUpperCase())) {
10
+ return 'Method not supported';
13
11
  }
14
12
 
15
- var commandOptions = []
13
+ const commandOptions = [];
16
14
 
17
15
  // Add headers, including the cookies
18
- var headers = Object.keys(source.headersObj)
16
+ const headers = Object.keys(source.headersObj);
19
17
 
20
18
  // construct headers
21
19
  if (headers.length) {
22
- code.push('$headers=@{}')
20
+ code.push('$headers=@{}');
23
21
  headers.forEach(function (key) {
24
- if (key !== 'connection') { // Not allowed
25
- code.push('$headers.Add("%s", "%s")', key, source.headersObj[key])
22
+ if (key !== 'connection') {
23
+ // Not allowed
24
+ code.push('$headers.Add("%s", "%s")', key, source.headersObj[key]);
26
25
  }
27
- })
28
- commandOptions.push('-Headers $headers')
26
+ });
27
+ commandOptions.push('-Headers $headers');
29
28
  }
30
29
 
31
30
  // construct cookies
32
31
  if (source.cookies.length) {
33
- code.push('$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession')
32
+ code.push('$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession');
34
33
 
35
34
  source.cookies.forEach(function (cookie) {
36
- code.push('$cookie = New-Object System.Net.Cookie')
35
+ code.push('$cookie = New-Object System.Net.Cookie');
37
36
 
38
- code.push("$cookie.Name = '%s'", cookie.name)
39
- code.push("$cookie.Value = '%s'", cookie.value)
40
- code.push("$cookie.Domain = '%s'", source.uriObj.host)
37
+ code.push("$cookie.Name = '%s'", cookie.name);
38
+ code.push("$cookie.Value = '%s'", cookie.value);
39
+ code.push("$cookie.Domain = '%s'", source.uriObj.host);
41
40
 
42
- code.push('$session.Cookies.Add($cookie)')
43
- })
44
- commandOptions.push('-WebSession $session')
41
+ code.push('$session.Cookies.Add($cookie)');
42
+ });
43
+ commandOptions.push('-WebSession $session');
45
44
  }
46
45
 
47
46
  if (source.postData.text) {
48
- commandOptions.push("-ContentType '" + helpers.getHeader(source.allHeaders, 'content-type') + "'")
49
- commandOptions.push("-Body '" + source.postData.text + "'")
47
+ commandOptions.push(`-ContentType '${helpers.getHeader(source.allHeaders, 'content-type')}'`);
48
+ commandOptions.push(`-Body '${source.postData.text}'`);
50
49
  }
51
50
 
52
- code.push("$response = %s -Uri '%s' -Method %s %s", command, source.fullUrl, source.method, commandOptions.join(' '))
53
- return code.join()
54
- }
55
- }
51
+ code.push(
52
+ "$response = %s -Uri '%s' -Method %s %s",
53
+ command,
54
+ source.fullUrl,
55
+ source.method,
56
+ commandOptions.join(' ')
57
+ );
58
+ return code.join();
59
+ };
60
+ };